blob: e343657f29a20ef9b42ae17a7a5deeec8f436514 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
David Brazdil58282f42016-01-14 12:45:10 +000050static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000051static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070052 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr SRegister kFpuCalleeSaves[] =
54 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000056// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
Vladimir Markof3e0ee22015-12-17 15:23:13 +000060static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070061
Roland Levillain7cbd27f2016-08-11 23:53:33 +010062// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
63#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070064#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Artem Serovf4d6aee2016-07-11 10:41:45 +010066static constexpr int kRegListThreshold = 4;
67
Artem Serovd300d8f2016-07-15 14:00:56 +010068// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
69// for each live D registers they treat two corresponding S registers as live ones.
70//
71// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
72// from a list of contiguous S registers a list of contiguous D registers (processing first/last
73// S registers corner cases) and save/restore this new list treating them as D registers.
74// - decreasing code size
75// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
76// restored and then used in regular non SlowPath code as D register.
77//
78// For the following example (v means the S register is live):
79// D names: | D0 | D1 | D2 | D4 | ...
80// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
81// Live? | | v | v | v | v | v | v | | ...
82//
83// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
84// as D registers.
85static size_t SaveContiguousSRegisterList(size_t first,
86 size_t last,
87 CodeGenerator* codegen,
88 size_t stack_offset) {
89 DCHECK_LE(first, last);
90 if ((first == last) && (first == 0)) {
91 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first);
92 return stack_offset;
93 }
94 if (first % 2 == 1) {
95 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first++);
96 }
97
98 bool save_last = false;
99 if (last % 2 == 0) {
100 save_last = true;
101 --last;
102 }
103
104 if (first < last) {
105 DRegister d_reg = static_cast<DRegister>(first / 2);
106 DCHECK_EQ((last - first + 1) % 2, 0u);
107 size_t number_of_d_regs = (last - first + 1) / 2;
108
109 if (number_of_d_regs == 1) {
110 __ StoreDToOffset(d_reg, SP, stack_offset);
111 } else if (number_of_d_regs > 1) {
112 __ add(IP, SP, ShifterOperand(stack_offset));
113 __ vstmiad(IP, d_reg, number_of_d_regs);
114 }
115 stack_offset += number_of_d_regs * kArmWordSize * 2;
116 }
117
118 if (save_last) {
119 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, last + 1);
120 }
121
122 return stack_offset;
123}
124
125static size_t RestoreContiguousSRegisterList(size_t first,
126 size_t last,
127 CodeGenerator* codegen,
128 size_t stack_offset) {
129 DCHECK_LE(first, last);
130 if ((first == last) && (first == 0)) {
131 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first);
132 return stack_offset;
133 }
134 if (first % 2 == 1) {
135 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first++);
136 }
137
138 bool restore_last = false;
139 if (last % 2 == 0) {
140 restore_last = true;
141 --last;
142 }
143
144 if (first < last) {
145 DRegister d_reg = static_cast<DRegister>(first / 2);
146 DCHECK_EQ((last - first + 1) % 2, 0u);
147 size_t number_of_d_regs = (last - first + 1) / 2;
148 if (number_of_d_regs == 1) {
149 __ LoadDFromOffset(d_reg, SP, stack_offset);
150 } else if (number_of_d_regs > 1) {
151 __ add(IP, SP, ShifterOperand(stack_offset));
152 __ vldmiad(IP, d_reg, number_of_d_regs);
153 }
154 stack_offset += number_of_d_regs * kArmWordSize * 2;
155 }
156
157 if (restore_last) {
158 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, last + 1);
159 }
160
161 return stack_offset;
162}
163
Artem Serovf4d6aee2016-07-11 10:41:45 +0100164void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
165 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
166 size_t orig_offset = stack_offset;
167
168 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
169 for (uint32_t i : LowToHighBits(core_spills)) {
170 // If the register holds an object, update the stack mask.
171 if (locations->RegisterContainsObject(i)) {
172 locations->SetStackBit(stack_offset / kVRegSize);
173 }
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_core_stack_offsets_[i] = stack_offset;
177 stack_offset += kArmWordSize;
178 }
179
180 int reg_num = POPCOUNT(core_spills);
181 if (reg_num != 0) {
182 if (reg_num > kRegListThreshold) {
183 __ StoreList(RegList(core_spills), orig_offset);
184 } else {
185 stack_offset = orig_offset;
186 for (uint32_t i : LowToHighBits(core_spills)) {
187 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
188 }
189 }
190 }
191
Artem Serovd300d8f2016-07-15 14:00:56 +0100192 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
193 orig_offset = stack_offset;
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 for (uint32_t i : LowToHighBits(fp_spills)) {
Artem Serovf4d6aee2016-07-11 10:41:45 +0100195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serovd300d8f2016-07-15 14:00:56 +0100197 stack_offset += kArmWordSize;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100198 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100199
200 stack_offset = orig_offset;
201 while (fp_spills != 0u) {
202 uint32_t begin = CTZ(fp_spills);
203 uint32_t tmp = fp_spills + (1u << begin);
204 fp_spills &= tmp; // Clear the contiguous range of 1s.
205 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
206 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
207 }
208 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100209}
210
211void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
212 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
213 size_t orig_offset = stack_offset;
214
215 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
216 for (uint32_t i : LowToHighBits(core_spills)) {
217 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
218 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
219 stack_offset += kArmWordSize;
220 }
221
222 int reg_num = POPCOUNT(core_spills);
223 if (reg_num != 0) {
224 if (reg_num > kRegListThreshold) {
225 __ LoadList(RegList(core_spills), orig_offset);
226 } else {
227 stack_offset = orig_offset;
228 for (uint32_t i : LowToHighBits(core_spills)) {
229 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
230 }
231 }
232 }
233
Artem Serovd300d8f2016-07-15 14:00:56 +0100234 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
235 while (fp_spills != 0u) {
236 uint32_t begin = CTZ(fp_spills);
237 uint32_t tmp = fp_spills + (1u << begin);
238 fp_spills &= tmp; // Clear the contiguous range of 1s.
239 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
240 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
Artem Serovf4d6aee2016-07-11 10:41:45 +0100241 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100242 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100243}
244
245class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100246 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100247 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100248
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100250 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100251 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000252 if (instruction_->CanThrowIntoCatchBlock()) {
253 // Live registers will be restored in the catch block if caught.
254 SaveLiveRegisters(codegen, instruction_->GetLocations());
255 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100256 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
257 instruction_,
258 instruction_->GetDexPc(),
259 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000260 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100261 }
262
Alexandre Rames8158f282015-08-07 10:26:17 +0100263 bool IsFatal() const OVERRIDE { return true; }
264
Alexandre Rames9931f312015-06-19 14:47:01 +0100265 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
266
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100267 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100268 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
269};
270
Artem Serovf4d6aee2016-07-11 10:41:45 +0100271class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000272 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100273 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000274
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000276 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
277 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100278 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000279 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000280 }
281
Alexandre Rames8158f282015-08-07 10:26:17 +0100282 bool IsFatal() const OVERRIDE { return true; }
283
Alexandre Rames9931f312015-06-19 14:47:01 +0100284 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
285
Calin Juravled0d48522014-11-04 16:40:20 +0000286 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000287 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
288};
289
Artem Serovf4d6aee2016-07-11 10:41:45 +0100290class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000291 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100293 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000294
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100296 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000297 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100298 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000299 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100300 if (successor_ == nullptr) {
301 __ b(GetReturnLabel());
302 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100303 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100304 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000305 }
306
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100307 Label* GetReturnLabel() {
308 DCHECK(successor_ == nullptr);
309 return &return_label_;
310 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000311
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100312 HBasicBlock* GetSuccessor() const {
313 return successor_;
314 }
315
Alexandre Rames9931f312015-06-19 14:47:01 +0100316 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
317
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000318 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100319 // If not null, the block to branch to after the suspend check.
320 HBasicBlock* const successor_;
321
322 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000323 Label return_label_;
324
325 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
326};
327
Artem Serovf4d6aee2016-07-11 10:41:45 +0100328class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100329 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100330 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100331 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100332
Alexandre Rames67555f72014-11-18 10:55:16 +0000333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100334 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100335 LocationSummary* locations = instruction_->GetLocations();
336
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100337 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000338 if (instruction_->CanThrowIntoCatchBlock()) {
339 // Live registers will be restored in the catch block if caught.
340 SaveLiveRegisters(codegen, instruction_->GetLocations());
341 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000342 // We're moving two locations to locations that could overlap, so we need a parallel
343 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100344 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000345 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100346 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000347 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100348 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100349 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100350 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
351 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100352 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
353 ? kQuickThrowStringBounds
354 : kQuickThrowArrayBounds;
355 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100356 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000357 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100358 }
359
Alexandre Rames8158f282015-08-07 10:26:17 +0100360 bool IsFatal() const OVERRIDE { return true; }
361
Alexandre Rames9931f312015-06-19 14:47:01 +0100362 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
363
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100364 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100365 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
366};
367
Artem Serovf4d6aee2016-07-11 10:41:45 +0100368class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100369 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000370 LoadClassSlowPathARM(HLoadClass* cls,
371 HInstruction* at,
372 uint32_t dex_pc,
373 bool do_clinit)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100374 : SlowPathCodeARM(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000375 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
376 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100377
Alexandre Rames67555f72014-11-18 10:55:16 +0000378 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000379 LocationSummary* locations = at_->GetLocations();
380
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100381 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
382 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000383 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100385 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000386 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100387 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
388 : kQuickInitializeType;
389 arm_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000390 if (do_clinit_) {
391 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
392 } else {
393 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
394 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000395
396 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000397 Location out = locations->Out();
398 if (out.IsValid()) {
399 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000400 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
401 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000402 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100403 __ b(GetExitLabel());
404 }
405
Alexandre Rames9931f312015-06-19 14:47:01 +0100406 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
407
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100408 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000409 // The class this slow path will load.
410 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100411
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000412 // The instruction where this slow path is happening.
413 // (Might be the load class or an initialization check).
414 HInstruction* const at_;
415
416 // The dex PC of `at_`.
417 const uint32_t dex_pc_;
418
419 // Whether to initialize the class.
420 const bool do_clinit_;
421
422 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100423};
424
Vladimir Marko63dccbbe2016-09-21 13:51:10 +0100425class LoadStringSlowPathARM : public SlowPathCodeARM {
426 public:
427 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
428
429 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
430 LocationSummary* locations = instruction_->GetLocations();
431 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
432
433 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
434 __ Bind(GetEntryLabel());
435 SaveLiveRegisters(codegen, locations);
436
437 InvokeRuntimeCallingConvention calling_convention;
438 HLoadString* load = instruction_->AsLoadString();
439 const uint32_t string_index = load->GetStringIndex();
440 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index);
441 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
442 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
443 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
444
445 RestoreLiveRegisters(codegen, locations);
446
447 // Store the resolved String to the BSS entry.
448 // TODO: Change art_quick_resolve_string to kSaveEverything and use a temporary for the
449 // .bss entry address in the fast path, so that we can avoid another calculation here.
450 CodeGeneratorARM::PcRelativePatchInfo* labels =
451 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
452 __ BindTrackedLabel(&labels->movw_label);
453 __ movw(IP, /* placeholder */ 0u);
454 __ BindTrackedLabel(&labels->movt_label);
455 __ movt(IP, /* placeholder */ 0u);
456 __ BindTrackedLabel(&labels->add_pc_label);
457 __ add(IP, IP, ShifterOperand(PC));
458 __ str(locations->Out().AsRegister<Register>(), Address(IP));
459
460 __ b(GetExitLabel());
461 }
462
463 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
464
465 private:
466 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
467};
468
Artem Serovf4d6aee2016-07-11 10:41:45 +0100469class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000470 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000471 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100472 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000473
Alexandre Rames67555f72014-11-18 10:55:16 +0000474 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000475 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100476 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
477 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000478 DCHECK(instruction_->IsCheckCast()
479 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000480
481 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
482 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000483
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000484 if (!is_fatal_) {
485 SaveLiveRegisters(codegen, locations);
486 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000487
488 // We're moving two locations to locations that could overlap, so we need a parallel
489 // move resolver.
490 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000491 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100492 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000493 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100494 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100495 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100496 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
497 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000498
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000499 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100500 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100501 instruction_,
502 instruction_->GetDexPc(),
503 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000504 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700505 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000506 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
507 } else {
508 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100509 arm_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000510 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000511 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000512
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000513 if (!is_fatal_) {
514 RestoreLiveRegisters(codegen, locations);
515 __ b(GetExitLabel());
516 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000517 }
518
Alexandre Rames9931f312015-06-19 14:47:01 +0100519 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
520
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000521 bool IsFatal() const OVERRIDE { return is_fatal_; }
522
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000523 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000524 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000525
526 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
527};
528
Artem Serovf4d6aee2016-07-11 10:41:45 +0100529class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700530 public:
Aart Bik42249c32016-01-07 15:33:50 -0800531 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100532 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700533
534 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800535 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700536 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100537 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000538 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700539 }
540
Alexandre Rames9931f312015-06-19 14:47:01 +0100541 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
542
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700543 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700544 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
545};
546
Artem Serovf4d6aee2016-07-11 10:41:45 +0100547class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100548 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100549 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100550
551 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
552 LocationSummary* locations = instruction_->GetLocations();
553 __ Bind(GetEntryLabel());
554 SaveLiveRegisters(codegen, locations);
555
556 InvokeRuntimeCallingConvention calling_convention;
557 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
558 parallel_move.AddMove(
559 locations->InAt(0),
560 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
561 Primitive::kPrimNot,
562 nullptr);
563 parallel_move.AddMove(
564 locations->InAt(1),
565 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
566 Primitive::kPrimInt,
567 nullptr);
568 parallel_move.AddMove(
569 locations->InAt(2),
570 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
571 Primitive::kPrimNot,
572 nullptr);
573 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
574
575 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100576 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000577 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100578 RestoreLiveRegisters(codegen, locations);
579 __ b(GetExitLabel());
580 }
581
582 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
583
584 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100585 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
586};
587
Roland Levillainc9285912015-12-18 10:38:42 +0000588// Slow path marking an object during a read barrier.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100589class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000590 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100591 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location obj)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100592 : SlowPathCodeARM(instruction), obj_(obj) {
Roland Levillainc9285912015-12-18 10:38:42 +0000593 DCHECK(kEmitCompilerReadBarrier);
594 }
595
596 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
597
598 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
599 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100600 Register reg = obj_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000601 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100602 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillainc9285912015-12-18 10:38:42 +0000603 DCHECK(instruction_->IsInstanceFieldGet() ||
604 instruction_->IsStaticFieldGet() ||
605 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100606 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000607 instruction_->IsLoadClass() ||
608 instruction_->IsLoadString() ||
609 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100610 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100611 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
612 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000613 << "Unexpected instruction in read barrier marking slow path: "
614 << instruction_->DebugName();
615
616 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100617 // No need to save live registers; it's taken care of by the
618 // entrypoint. Also, there is no need to update the stack mask,
619 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000620 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100621 DCHECK_NE(reg, SP);
622 DCHECK_NE(reg, LR);
623 DCHECK_NE(reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100624 // IP is used internally by the ReadBarrierMarkRegX entry point
625 // as a temporary, it cannot be the entry point's input/output.
626 DCHECK_NE(reg, IP);
Roland Levillain02b75802016-07-13 11:54:35 +0100627 DCHECK(0 <= reg && reg < kNumberOfCoreRegisters) << reg;
628 // "Compact" slow path, saving two moves.
629 //
630 // Instead of using the standard runtime calling convention (input
631 // and output in R0):
632 //
633 // R0 <- obj
634 // R0 <- ReadBarrierMark(R0)
635 // obj <- R0
636 //
637 // we just use rX (the register holding `obj`) as input and output
638 // of a dedicated entrypoint:
639 //
640 // rX <- ReadBarrierMarkRegX(rX)
641 //
642 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700643 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100644 // This runtime call does not require a stack map.
645 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000646 __ b(GetExitLabel());
647 }
648
649 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000650 const Location obj_;
651
652 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
653};
654
Roland Levillain3b359c72015-11-17 19:35:12 +0000655// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100656class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000657 public:
658 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
659 Location out,
660 Location ref,
661 Location obj,
662 uint32_t offset,
663 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100664 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000665 out_(out),
666 ref_(ref),
667 obj_(obj),
668 offset_(offset),
669 index_(index) {
670 DCHECK(kEmitCompilerReadBarrier);
671 // If `obj` is equal to `out` or `ref`, it means the initial object
672 // has been overwritten by (or after) the heap object reference load
673 // to be instrumented, e.g.:
674 //
675 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000676 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000677 //
678 // In that case, we have lost the information about the original
679 // object, and the emitted read barrier cannot work properly.
680 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
681 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
682 }
683
684 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
685 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
686 LocationSummary* locations = instruction_->GetLocations();
687 Register reg_out = out_.AsRegister<Register>();
688 DCHECK(locations->CanCall());
689 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100690 DCHECK(instruction_->IsInstanceFieldGet() ||
691 instruction_->IsStaticFieldGet() ||
692 instruction_->IsArrayGet() ||
693 instruction_->IsInstanceOf() ||
694 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100695 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000696 << "Unexpected instruction in read barrier for heap reference slow path: "
697 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000698
699 __ Bind(GetEntryLabel());
700 SaveLiveRegisters(codegen, locations);
701
702 // We may have to change the index's value, but as `index_` is a
703 // constant member (like other "inputs" of this slow path),
704 // introduce a copy of it, `index`.
705 Location index = index_;
706 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100707 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000708 if (instruction_->IsArrayGet()) {
709 // Compute the actual memory offset and store it in `index`.
710 Register index_reg = index_.AsRegister<Register>();
711 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
712 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
713 // We are about to change the value of `index_reg` (see the
714 // calls to art::arm::Thumb2Assembler::Lsl and
715 // art::arm::Thumb2Assembler::AddConstant below), but it has
716 // not been saved by the previous call to
717 // art::SlowPathCode::SaveLiveRegisters, as it is a
718 // callee-save register --
719 // art::SlowPathCode::SaveLiveRegisters does not consider
720 // callee-save registers, as it has been designed with the
721 // assumption that callee-save registers are supposed to be
722 // handled by the called function. So, as a callee-save
723 // register, `index_reg` _would_ eventually be saved onto
724 // the stack, but it would be too late: we would have
725 // changed its value earlier. Therefore, we manually save
726 // it here into another freely available register,
727 // `free_reg`, chosen of course among the caller-save
728 // registers (as a callee-save `free_reg` register would
729 // exhibit the same problem).
730 //
731 // Note we could have requested a temporary register from
732 // the register allocator instead; but we prefer not to, as
733 // this is a slow path, and we know we can find a
734 // caller-save register that is available.
735 Register free_reg = FindAvailableCallerSaveRegister(codegen);
736 __ Mov(free_reg, index_reg);
737 index_reg = free_reg;
738 index = Location::RegisterLocation(index_reg);
739 } else {
740 // The initial register stored in `index_` has already been
741 // saved in the call to art::SlowPathCode::SaveLiveRegisters
742 // (as it is not a callee-save register), so we can freely
743 // use it.
744 }
745 // Shifting the index value contained in `index_reg` by the scale
746 // factor (2) cannot overflow in practice, as the runtime is
747 // unable to allocate object arrays with a size larger than
748 // 2^26 - 1 (that is, 2^28 - 4 bytes).
749 __ Lsl(index_reg, index_reg, TIMES_4);
750 static_assert(
751 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
752 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
753 __ AddConstant(index_reg, index_reg, offset_);
754 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100755 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
756 // intrinsics, `index_` is not shifted by a scale factor of 2
757 // (as in the case of ArrayGet), as it is actually an offset
758 // to an object field within an object.
759 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000760 DCHECK(instruction_->GetLocations()->Intrinsified());
761 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
762 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
763 << instruction_->AsInvoke()->GetIntrinsic();
764 DCHECK_EQ(offset_, 0U);
765 DCHECK(index_.IsRegisterPair());
766 // UnsafeGet's offset location is a register pair, the low
767 // part contains the correct offset.
768 index = index_.ToLow();
769 }
770 }
771
772 // We're moving two or three locations to locations that could
773 // overlap, so we need a parallel move resolver.
774 InvokeRuntimeCallingConvention calling_convention;
775 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
776 parallel_move.AddMove(ref_,
777 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
778 Primitive::kPrimNot,
779 nullptr);
780 parallel_move.AddMove(obj_,
781 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
782 Primitive::kPrimNot,
783 nullptr);
784 if (index.IsValid()) {
785 parallel_move.AddMove(index,
786 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
787 Primitive::kPrimInt,
788 nullptr);
789 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
790 } else {
791 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
792 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
793 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100794 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000795 CheckEntrypointTypes<
796 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
797 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
798
799 RestoreLiveRegisters(codegen, locations);
800 __ b(GetExitLabel());
801 }
802
803 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
804
805 private:
806 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
807 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
808 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
809 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
810 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
811 return static_cast<Register>(i);
812 }
813 }
814 // We shall never fail to find a free caller-save register, as
815 // there are more than two core caller-save registers on ARM
816 // (meaning it is possible to find one which is different from
817 // `ref` and `obj`).
818 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
819 LOG(FATAL) << "Could not find a free caller-save register";
820 UNREACHABLE();
821 }
822
Roland Levillain3b359c72015-11-17 19:35:12 +0000823 const Location out_;
824 const Location ref_;
825 const Location obj_;
826 const uint32_t offset_;
827 // An additional location containing an index to an array.
828 // Only used for HArrayGet and the UnsafeGetObject &
829 // UnsafeGetObjectVolatile intrinsics.
830 const Location index_;
831
832 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
833};
834
835// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100836class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000837 public:
838 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100839 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000840 DCHECK(kEmitCompilerReadBarrier);
841 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000842
843 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
844 LocationSummary* locations = instruction_->GetLocations();
845 Register reg_out = out_.AsRegister<Register>();
846 DCHECK(locations->CanCall());
847 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000848 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
849 << "Unexpected instruction in read barrier for GC root slow path: "
850 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000851
852 __ Bind(GetEntryLabel());
853 SaveLiveRegisters(codegen, locations);
854
855 InvokeRuntimeCallingConvention calling_convention;
856 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
857 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100858 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +0000859 instruction_,
860 instruction_->GetDexPc(),
861 this);
862 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
863 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
864
865 RestoreLiveRegisters(codegen, locations);
866 __ b(GetExitLabel());
867 }
868
869 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
870
871 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000872 const Location out_;
873 const Location root_;
874
875 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
876};
877
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000878#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100879// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
880#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700881
Aart Bike9f37602015-10-09 11:15:55 -0700882inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700883 switch (cond) {
884 case kCondEQ: return EQ;
885 case kCondNE: return NE;
886 case kCondLT: return LT;
887 case kCondLE: return LE;
888 case kCondGT: return GT;
889 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700890 case kCondB: return LO;
891 case kCondBE: return LS;
892 case kCondA: return HI;
893 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700894 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100895 LOG(FATAL) << "Unreachable";
896 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700897}
898
Aart Bike9f37602015-10-09 11:15:55 -0700899// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100900inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700901 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100902 case kCondEQ: return EQ;
903 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700904 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100905 case kCondLT: return LO;
906 case kCondLE: return LS;
907 case kCondGT: return HI;
908 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700909 // Unsigned remain unchanged.
910 case kCondB: return LO;
911 case kCondBE: return LS;
912 case kCondA: return HI;
913 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700914 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100915 LOG(FATAL) << "Unreachable";
916 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700917}
918
Vladimir Markod6e069b2016-01-18 11:11:01 +0000919inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
920 // The ARM condition codes can express all the necessary branches, see the
921 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
922 // There is no dex instruction or HIR that would need the missing conditions
923 // "equal or unordered" or "not equal".
924 switch (cond) {
925 case kCondEQ: return EQ;
926 case kCondNE: return NE /* unordered */;
927 case kCondLT: return gt_bias ? CC : LT /* unordered */;
928 case kCondLE: return gt_bias ? LS : LE /* unordered */;
929 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
930 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
931 default:
932 LOG(FATAL) << "UNREACHABLE";
933 UNREACHABLE();
934 }
935}
936
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100937void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100938 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100939}
940
941void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100942 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100943}
944
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100945size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
946 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
947 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100948}
949
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100950size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
951 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
952 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100953}
954
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000955size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
956 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
957 return kArmWordSize;
958}
959
960size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
961 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
962 return kArmWordSize;
963}
964
Calin Juravle34166012014-12-19 17:22:29 +0000965CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000966 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100967 const CompilerOptions& compiler_options,
968 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000969 : CodeGenerator(graph,
970 kNumberOfCoreRegisters,
971 kNumberOfSRegisters,
972 kNumberOfRegisterPairs,
973 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
974 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000975 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
976 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100977 compiler_options,
978 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100979 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100980 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100981 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100982 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100983 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000984 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000985 uint32_literals_(std::less<uint32_t>(),
986 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100987 method_patches_(MethodReferenceComparator(),
988 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
989 call_patches_(MethodReferenceComparator(),
990 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000991 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000992 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
993 boot_image_string_patches_(StringReferenceValueComparator(),
994 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
995 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100996 boot_image_type_patches_(TypeReferenceValueComparator(),
997 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
998 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000999 boot_image_address_patches_(std::less<uint32_t>(),
1000 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001001 // Always save the LR register to mimic Quick.
1002 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001003}
1004
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001005void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1006 // Ensure that we fix up branches and literal loads and emit the literal pool.
1007 __ FinalizeCode();
1008
1009 // Adjust native pc offsets in stack maps.
1010 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
1011 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
1012 uint32_t new_position = __ GetAdjustedPosition(old_position);
1013 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1014 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001015 // Adjust pc offsets for the disassembly information.
1016 if (disasm_info_ != nullptr) {
1017 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1018 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1019 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1020 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1021 it.second.start = __ GetAdjustedPosition(it.second.start);
1022 it.second.end = __ GetAdjustedPosition(it.second.end);
1023 }
1024 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1025 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1026 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1027 }
1028 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001029
1030 CodeGenerator::Finalize(allocator);
1031}
1032
David Brazdil58282f42016-01-14 12:45:10 +00001033void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001034 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001035 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001036
1037 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001038 blocked_core_registers_[SP] = true;
1039 blocked_core_registers_[LR] = true;
1040 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001041
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001042 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001043 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001044
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001045 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001046 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001047
David Brazdil58282f42016-01-14 12:45:10 +00001048 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001049 // Stubs do not save callee-save floating point registers. If the graph
1050 // is debuggable, we need to deal with these registers differently. For
1051 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001052 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1053 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1054 }
1055 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001056
1057 UpdateBlockedPairRegisters();
1058}
1059
1060void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
1061 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1062 ArmManagedRegister current =
1063 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1064 if (blocked_core_registers_[current.AsRegisterPairLow()]
1065 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1066 blocked_register_pairs_[i] = true;
1067 }
1068 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001069}
1070
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001071InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001072 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001073 assembler_(codegen->GetAssembler()),
1074 codegen_(codegen) {}
1075
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001076void CodeGeneratorARM::ComputeSpillMask() {
1077 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1078 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001079 // There is no easy instruction to restore just the PC on thumb2. We spill and
1080 // restore another arbitrary register.
1081 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001082 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1083 // We use vpush and vpop for saving and restoring floating point registers, which take
1084 // a SRegister and the number of registers to save/restore after that SRegister. We
1085 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1086 // but in the range.
1087 if (fpu_spill_mask_ != 0) {
1088 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1089 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1090 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1091 fpu_spill_mask_ |= (1 << i);
1092 }
1093 }
1094}
1095
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001096static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001097 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001098}
1099
1100static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001101 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001102}
1103
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001104void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001105 bool skip_overflow_check =
1106 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001107 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001108 __ Bind(&frame_entry_label_);
1109
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001110 if (HasEmptyFrame()) {
1111 return;
1112 }
1113
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001114 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001115 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1116 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1117 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001118 }
1119
Andreas Gampe501fd632015-09-10 16:11:06 -07001120 __ PushList(core_spill_mask_);
1121 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1122 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001123 if (fpu_spill_mask_ != 0) {
1124 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1125 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001126 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001127 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001128 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001129 int adjust = GetFrameSize() - FrameEntrySpillSize();
1130 __ AddConstant(SP, -adjust);
1131 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001132 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001133}
1134
1135void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001136 if (HasEmptyFrame()) {
1137 __ bx(LR);
1138 return;
1139 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001140 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001141 int adjust = GetFrameSize() - FrameEntrySpillSize();
1142 __ AddConstant(SP, adjust);
1143 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001144 if (fpu_spill_mask_ != 0) {
1145 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1146 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001147 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001148 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001149 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001150 // Pop LR into PC to return.
1151 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1152 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1153 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001154 __ cfi().RestoreState();
1155 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001156}
1157
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001158void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001159 Label* label = GetLabelOf(block);
1160 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001161}
1162
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001163Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001164 switch (type) {
1165 case Primitive::kPrimBoolean:
1166 case Primitive::kPrimByte:
1167 case Primitive::kPrimChar:
1168 case Primitive::kPrimShort:
1169 case Primitive::kPrimInt:
1170 case Primitive::kPrimNot: {
1171 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001172 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001173 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001174 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001175 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001176 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001177 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001178 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001179
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001180 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001181 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001182 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001183 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001184 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001185 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001186 if (calling_convention.GetRegisterAt(index) == R1) {
1187 // Skip R1, and use R2_R3 instead.
1188 gp_index_++;
1189 index++;
1190 }
1191 }
1192 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1193 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001194 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001195
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001196 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001197 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001198 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001199 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1200 }
1201 }
1202
1203 case Primitive::kPrimFloat: {
1204 uint32_t stack_index = stack_index_++;
1205 if (float_index_ % 2 == 0) {
1206 float_index_ = std::max(double_index_, float_index_);
1207 }
1208 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1209 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1210 } else {
1211 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1212 }
1213 }
1214
1215 case Primitive::kPrimDouble: {
1216 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1217 uint32_t stack_index = stack_index_;
1218 stack_index_ += 2;
1219 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1220 uint32_t index = double_index_;
1221 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001222 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001223 calling_convention.GetFpuRegisterAt(index),
1224 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001225 DCHECK(ExpectedPairLayout(result));
1226 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001227 } else {
1228 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001229 }
1230 }
1231
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001232 case Primitive::kPrimVoid:
1233 LOG(FATAL) << "Unexpected parameter type " << type;
1234 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001235 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001236 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001237}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001238
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001239Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001240 switch (type) {
1241 case Primitive::kPrimBoolean:
1242 case Primitive::kPrimByte:
1243 case Primitive::kPrimChar:
1244 case Primitive::kPrimShort:
1245 case Primitive::kPrimInt:
1246 case Primitive::kPrimNot: {
1247 return Location::RegisterLocation(R0);
1248 }
1249
1250 case Primitive::kPrimFloat: {
1251 return Location::FpuRegisterLocation(S0);
1252 }
1253
1254 case Primitive::kPrimLong: {
1255 return Location::RegisterPairLocation(R0, R1);
1256 }
1257
1258 case Primitive::kPrimDouble: {
1259 return Location::FpuRegisterPairLocation(S0, S1);
1260 }
1261
1262 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001263 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001264 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001265
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001266 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001267}
1268
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001269Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1270 return Location::RegisterLocation(kMethodRegisterArgument);
1271}
1272
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001273void CodeGeneratorARM::Move32(Location destination, Location source) {
1274 if (source.Equals(destination)) {
1275 return;
1276 }
1277 if (destination.IsRegister()) {
1278 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001279 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001280 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001281 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001282 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001283 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001284 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001285 } else if (destination.IsFpuRegister()) {
1286 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001287 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001288 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001289 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001290 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001291 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001292 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001293 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001294 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001295 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001296 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001297 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001298 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001299 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001300 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001301 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1302 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001303 }
1304 }
1305}
1306
1307void CodeGeneratorARM::Move64(Location destination, Location source) {
1308 if (source.Equals(destination)) {
1309 return;
1310 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001311 if (destination.IsRegisterPair()) {
1312 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001313 EmitParallelMoves(
1314 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1315 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001316 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001317 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001318 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1319 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001320 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001321 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001322 } else if (source.IsFpuRegisterPair()) {
1323 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1324 destination.AsRegisterPairHigh<Register>(),
1325 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001326 } else {
1327 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001328 DCHECK(ExpectedPairLayout(destination));
1329 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1330 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001331 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001332 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001333 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001334 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1335 SP,
1336 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001337 } else if (source.IsRegisterPair()) {
1338 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1339 source.AsRegisterPairLow<Register>(),
1340 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001341 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001342 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001343 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001344 } else {
1345 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001346 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001347 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001348 if (source.AsRegisterPairLow<Register>() == R1) {
1349 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001350 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1351 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001352 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001353 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001354 SP, destination.GetStackIndex());
1355 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001356 } else if (source.IsFpuRegisterPair()) {
1357 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1358 SP,
1359 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001360 } else {
1361 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001362 EmitParallelMoves(
1363 Location::StackSlot(source.GetStackIndex()),
1364 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001365 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001366 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001367 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1368 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001369 }
1370 }
1371}
1372
Calin Juravle175dc732015-08-25 15:42:32 +01001373void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1374 DCHECK(location.IsRegister());
1375 __ LoadImmediate(location.AsRegister<Register>(), value);
1376}
1377
Calin Juravlee460d1d2015-09-29 04:52:17 +01001378void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001379 HParallelMove move(GetGraph()->GetArena());
1380 move.AddMove(src, dst, dst_type, nullptr);
1381 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001382}
1383
1384void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1385 if (location.IsRegister()) {
1386 locations->AddTemp(location);
1387 } else if (location.IsRegisterPair()) {
1388 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1389 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1390 } else {
1391 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1392 }
1393}
1394
Calin Juravle175dc732015-08-25 15:42:32 +01001395void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1396 HInstruction* instruction,
1397 uint32_t dex_pc,
1398 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001399 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001400 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001401 if (EntrypointRequiresStackMap(entrypoint)) {
1402 RecordPcInfo(instruction, dex_pc, slow_path);
1403 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001404}
1405
Roland Levillaindec8f632016-07-22 17:10:06 +01001406void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1407 HInstruction* instruction,
1408 SlowPathCode* slow_path) {
1409 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001410 GenerateInvokeRuntime(entry_point_offset);
1411}
1412
1413void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001414 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1415 __ blx(LR);
1416}
1417
David Brazdilfc6a86a2015-06-26 10:33:45 +00001418void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001419 DCHECK(!successor->IsExitBlock());
1420
1421 HBasicBlock* block = got->GetBlock();
1422 HInstruction* previous = got->GetPrevious();
1423
1424 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001425 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001426 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1427 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1428 return;
1429 }
1430
1431 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1432 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1433 }
1434 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001435 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001436 }
1437}
1438
David Brazdilfc6a86a2015-06-26 10:33:45 +00001439void LocationsBuilderARM::VisitGoto(HGoto* got) {
1440 got->SetLocations(nullptr);
1441}
1442
1443void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1444 HandleGoto(got, got->GetSuccessor());
1445}
1446
1447void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1448 try_boundary->SetLocations(nullptr);
1449}
1450
1451void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1452 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1453 if (!successor->IsExitBlock()) {
1454 HandleGoto(try_boundary, successor);
1455 }
1456}
1457
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001458void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001459 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001460}
1461
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001462void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001463}
1464
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001465void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1466 Primitive::Type type = instruction->InputAt(0)->GetType();
1467 Location lhs_loc = instruction->GetLocations()->InAt(0);
1468 Location rhs_loc = instruction->GetLocations()->InAt(1);
1469 if (rhs_loc.IsConstant()) {
1470 // 0.0 is the only immediate that can be encoded directly in
1471 // a VCMP instruction.
1472 //
1473 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1474 // specify that in a floating-point comparison, positive zero
1475 // and negative zero are considered equal, so we can use the
1476 // literal 0.0 for both cases here.
1477 //
1478 // Note however that some methods (Float.equal, Float.compare,
1479 // Float.compareTo, Double.equal, Double.compare,
1480 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1481 // StrictMath.min) consider 0.0 to be (strictly) greater than
1482 // -0.0. So if we ever translate calls to these methods into a
1483 // HCompare instruction, we must handle the -0.0 case with
1484 // care here.
1485 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1486 if (type == Primitive::kPrimFloat) {
1487 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1488 } else {
1489 DCHECK_EQ(type, Primitive::kPrimDouble);
1490 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1491 }
1492 } else {
1493 if (type == Primitive::kPrimFloat) {
1494 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1495 } else {
1496 DCHECK_EQ(type, Primitive::kPrimDouble);
1497 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1498 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1499 }
1500 }
1501}
1502
Roland Levillain4fa13f62015-07-06 18:11:54 +01001503void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1504 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001505 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001506 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001507 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001508}
1509
1510void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1511 Label* true_label,
1512 Label* false_label) {
1513 LocationSummary* locations = cond->GetLocations();
1514 Location left = locations->InAt(0);
1515 Location right = locations->InAt(1);
1516 IfCondition if_cond = cond->GetCondition();
1517
1518 Register left_high = left.AsRegisterPairHigh<Register>();
1519 Register left_low = left.AsRegisterPairLow<Register>();
1520 IfCondition true_high_cond = if_cond;
1521 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001522 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001523
1524 // Set the conditions for the test, remembering that == needs to be
1525 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001526 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001527 switch (if_cond) {
1528 case kCondEQ:
1529 case kCondNE:
1530 // Nothing to do.
1531 break;
1532 case kCondLT:
1533 false_high_cond = kCondGT;
1534 break;
1535 case kCondLE:
1536 true_high_cond = kCondLT;
1537 break;
1538 case kCondGT:
1539 false_high_cond = kCondLT;
1540 break;
1541 case kCondGE:
1542 true_high_cond = kCondGT;
1543 break;
Aart Bike9f37602015-10-09 11:15:55 -07001544 case kCondB:
1545 false_high_cond = kCondA;
1546 break;
1547 case kCondBE:
1548 true_high_cond = kCondB;
1549 break;
1550 case kCondA:
1551 false_high_cond = kCondB;
1552 break;
1553 case kCondAE:
1554 true_high_cond = kCondA;
1555 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001556 }
1557 if (right.IsConstant()) {
1558 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1559 int32_t val_low = Low32Bits(value);
1560 int32_t val_high = High32Bits(value);
1561
Vladimir Markoac6ac102015-12-17 12:14:00 +00001562 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001563 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001564 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001565 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001566 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001567 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001568 __ b(true_label, ARMCondition(true_high_cond));
1569 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001570 }
1571 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001572 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001573 } else {
1574 Register right_high = right.AsRegisterPairHigh<Register>();
1575 Register right_low = right.AsRegisterPairLow<Register>();
1576
1577 __ cmp(left_high, ShifterOperand(right_high));
1578 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001579 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001580 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001581 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001582 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001583 __ b(true_label, ARMCondition(true_high_cond));
1584 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001585 }
1586 // Must be equal high, so compare the lows.
1587 __ cmp(left_low, ShifterOperand(right_low));
1588 }
1589 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001590 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001591 __ b(true_label, final_condition);
1592}
1593
David Brazdil0debae72015-11-12 18:37:00 +00001594void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1595 Label* true_target_in,
1596 Label* false_target_in) {
1597 // Generated branching requires both targets to be explicit. If either of the
1598 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1599 Label fallthrough_target;
1600 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1601 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1602
Roland Levillain4fa13f62015-07-06 18:11:54 +01001603 Primitive::Type type = condition->InputAt(0)->GetType();
1604 switch (type) {
1605 case Primitive::kPrimLong:
1606 GenerateLongComparesAndJumps(condition, true_target, false_target);
1607 break;
1608 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001609 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001610 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001611 GenerateFPJumps(condition, true_target, false_target);
1612 break;
1613 default:
1614 LOG(FATAL) << "Unexpected compare type " << type;
1615 }
1616
David Brazdil0debae72015-11-12 18:37:00 +00001617 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001618 __ b(false_target);
1619 }
David Brazdil0debae72015-11-12 18:37:00 +00001620
1621 if (fallthrough_target.IsLinked()) {
1622 __ Bind(&fallthrough_target);
1623 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001624}
1625
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001626void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001627 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001628 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001629 Label* false_target) {
1630 HInstruction* cond = instruction->InputAt(condition_input_index);
1631
1632 if (true_target == nullptr && false_target == nullptr) {
1633 // Nothing to do. The code always falls through.
1634 return;
1635 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001636 // Constant condition, statically compared against "true" (integer value 1).
1637 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001638 if (true_target != nullptr) {
1639 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001640 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001641 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001642 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001643 if (false_target != nullptr) {
1644 __ b(false_target);
1645 }
1646 }
1647 return;
1648 }
1649
1650 // The following code generates these patterns:
1651 // (1) true_target == nullptr && false_target != nullptr
1652 // - opposite condition true => branch to false_target
1653 // (2) true_target != nullptr && false_target == nullptr
1654 // - condition true => branch to true_target
1655 // (3) true_target != nullptr && false_target != nullptr
1656 // - condition true => branch to true_target
1657 // - branch to false_target
1658 if (IsBooleanValueOrMaterializedCondition(cond)) {
1659 // Condition has been materialized, compare the output to 0.
1660 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1661 DCHECK(cond_val.IsRegister());
1662 if (true_target == nullptr) {
1663 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1664 } else {
1665 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001666 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001667 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001668 // Condition has not been materialized. Use its inputs as the comparison and
1669 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001670 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001671
1672 // If this is a long or FP comparison that has been folded into
1673 // the HCondition, generate the comparison directly.
1674 Primitive::Type type = condition->InputAt(0)->GetType();
1675 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1676 GenerateCompareTestAndBranch(condition, true_target, false_target);
1677 return;
1678 }
1679
1680 LocationSummary* locations = cond->GetLocations();
1681 DCHECK(locations->InAt(0).IsRegister());
1682 Register left = locations->InAt(0).AsRegister<Register>();
1683 Location right = locations->InAt(1);
1684 if (right.IsRegister()) {
1685 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001686 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001687 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001688 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001689 }
1690 if (true_target == nullptr) {
1691 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1692 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001693 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001694 }
Dave Allison20dfc792014-06-16 20:44:29 -07001695 }
David Brazdil0debae72015-11-12 18:37:00 +00001696
1697 // If neither branch falls through (case 3), the conditional branch to `true_target`
1698 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1699 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001700 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001701 }
1702}
1703
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001704void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001705 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1706 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001707 locations->SetInAt(0, Location::RequiresRegister());
1708 }
1709}
1710
1711void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001712 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1713 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1714 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1715 nullptr : codegen_->GetLabelOf(true_successor);
1716 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1717 nullptr : codegen_->GetLabelOf(false_successor);
1718 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001719}
1720
1721void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1722 LocationSummary* locations = new (GetGraph()->GetArena())
1723 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001724 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001725 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001726 locations->SetInAt(0, Location::RequiresRegister());
1727 }
1728}
1729
1730void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001731 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001732 GenerateTestAndBranch(deoptimize,
1733 /* condition_input_index */ 0,
1734 slow_path->GetEntryLabel(),
1735 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001736}
Dave Allison20dfc792014-06-16 20:44:29 -07001737
David Brazdil74eb1b22015-12-14 11:44:01 +00001738void LocationsBuilderARM::VisitSelect(HSelect* select) {
1739 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1740 if (Primitive::IsFloatingPointType(select->GetType())) {
1741 locations->SetInAt(0, Location::RequiresFpuRegister());
1742 locations->SetInAt(1, Location::RequiresFpuRegister());
1743 } else {
1744 locations->SetInAt(0, Location::RequiresRegister());
1745 locations->SetInAt(1, Location::RequiresRegister());
1746 }
1747 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1748 locations->SetInAt(2, Location::RequiresRegister());
1749 }
1750 locations->SetOut(Location::SameAsFirstInput());
1751}
1752
1753void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1754 LocationSummary* locations = select->GetLocations();
1755 Label false_target;
1756 GenerateTestAndBranch(select,
1757 /* condition_input_index */ 2,
1758 /* true_target */ nullptr,
1759 &false_target);
1760 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1761 __ Bind(&false_target);
1762}
1763
David Srbecky0cf44932015-12-09 14:09:59 +00001764void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1765 new (GetGraph()->GetArena()) LocationSummary(info);
1766}
1767
David Srbeckyd28f4a02016-03-14 17:14:24 +00001768void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1769 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001770}
1771
1772void CodeGeneratorARM::GenerateNop() {
1773 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001774}
1775
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001777 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001778 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001779 // Handle the long/FP comparisons made in instruction simplification.
1780 switch (cond->InputAt(0)->GetType()) {
1781 case Primitive::kPrimLong:
1782 locations->SetInAt(0, Location::RequiresRegister());
1783 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001784 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001785 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1786 }
1787 break;
1788
1789 case Primitive::kPrimFloat:
1790 case Primitive::kPrimDouble:
1791 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001792 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001793 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001794 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1795 }
1796 break;
1797
1798 default:
1799 locations->SetInAt(0, Location::RequiresRegister());
1800 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001801 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001802 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1803 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001804 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001805}
1806
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001807void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001808 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001809 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001810 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001811
1812 LocationSummary* locations = cond->GetLocations();
1813 Location left = locations->InAt(0);
1814 Location right = locations->InAt(1);
1815 Register out = locations->Out().AsRegister<Register>();
1816 Label true_label, false_label;
1817
1818 switch (cond->InputAt(0)->GetType()) {
1819 default: {
1820 // Integer case.
1821 if (right.IsRegister()) {
1822 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1823 } else {
1824 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001825 __ CmpConstant(left.AsRegister<Register>(),
1826 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001827 }
Aart Bike9f37602015-10-09 11:15:55 -07001828 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001829 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001830 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001831 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001832 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001833 return;
1834 }
1835 case Primitive::kPrimLong:
1836 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1837 break;
1838 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001839 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001840 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001841 GenerateFPJumps(cond, &true_label, &false_label);
1842 break;
1843 }
1844
1845 // Convert the jumps into the result.
1846 Label done_label;
1847
1848 // False case: result = 0.
1849 __ Bind(&false_label);
1850 __ LoadImmediate(out, 0);
1851 __ b(&done_label);
1852
1853 // True case: result = 1.
1854 __ Bind(&true_label);
1855 __ LoadImmediate(out, 1);
1856 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001857}
1858
1859void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001860 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001861}
1862
1863void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001864 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001865}
1866
1867void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001868 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001869}
1870
1871void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001872 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001873}
1874
1875void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001876 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001877}
1878
1879void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001880 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001881}
1882
1883void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001884 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001885}
1886
1887void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001888 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001889}
1890
1891void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001892 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001893}
1894
1895void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001896 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001897}
1898
1899void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001900 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001901}
1902
1903void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001904 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001905}
1906
Aart Bike9f37602015-10-09 11:15:55 -07001907void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001908 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001909}
1910
1911void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001912 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001913}
1914
1915void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001916 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001917}
1918
1919void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001920 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001921}
1922
1923void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001924 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001925}
1926
1927void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001928 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001929}
1930
1931void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001932 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001933}
1934
1935void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001936 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001937}
1938
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001939void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001940 LocationSummary* locations =
1941 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001942 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001943}
1944
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001945void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001946 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001947}
1948
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001949void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1950 LocationSummary* locations =
1951 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1952 locations->SetOut(Location::ConstantLocation(constant));
1953}
1954
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001955void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001956 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001957}
1958
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001959void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001960 LocationSummary* locations =
1961 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001962 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001963}
1964
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001965void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001966 // Will be generated at use site.
1967}
1968
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001969void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1970 LocationSummary* locations =
1971 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1972 locations->SetOut(Location::ConstantLocation(constant));
1973}
1974
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001975void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001976 // Will be generated at use site.
1977}
1978
1979void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1980 LocationSummary* locations =
1981 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1982 locations->SetOut(Location::ConstantLocation(constant));
1983}
1984
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001985void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001986 // Will be generated at use site.
1987}
1988
Calin Juravle27df7582015-04-17 19:12:31 +01001989void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1990 memory_barrier->SetLocations(nullptr);
1991}
1992
1993void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001994 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001995}
1996
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001997void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001998 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001999}
2000
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002001void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002002 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002003}
2004
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002005void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002006 LocationSummary* locations =
2007 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002008 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002009}
2010
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002011void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002012 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002013}
2014
Calin Juravle175dc732015-08-25 15:42:32 +01002015void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2016 // The trampoline uses the same calling convention as dex calling conventions,
2017 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2018 // the method_idx.
2019 HandleInvoke(invoke);
2020}
2021
2022void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2023 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2024}
2025
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002026void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002027 // Explicit clinit checks triggered by static invokes must have been pruned by
2028 // art::PrepareForRegisterAllocation.
2029 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002030
Vladimir Marko68c981f2016-08-26 13:13:33 +01002031 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002032 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002033 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2034 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2035 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002036 return;
2037 }
2038
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002039 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002040
2041 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2042 if (invoke->HasPcRelativeDexCache()) {
2043 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2044 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002045}
2046
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002047static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2048 if (invoke->GetLocations()->Intrinsified()) {
2049 IntrinsicCodeGeneratorARM intrinsic(codegen);
2050 intrinsic.Dispatch(invoke);
2051 return true;
2052 }
2053 return false;
2054}
2055
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002056void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002057 // Explicit clinit checks triggered by static invokes must have been pruned by
2058 // art::PrepareForRegisterAllocation.
2059 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002060
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002061 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2062 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002063 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002064
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002065 LocationSummary* locations = invoke->GetLocations();
2066 codegen_->GenerateStaticOrDirectCall(
2067 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002068 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002069}
2070
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002071void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002072 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002073 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002074}
2075
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002076void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002077 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002078 if (intrinsic.TryDispatch(invoke)) {
2079 return;
2080 }
2081
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002082 HandleInvoke(invoke);
2083}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002084
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002085void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002086 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2087 return;
2088 }
2089
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002090 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002091 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002092 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002093}
2094
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002095void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2096 HandleInvoke(invoke);
2097 // Add the hidden argument.
2098 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2099}
2100
2101void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2102 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002103 LocationSummary* locations = invoke->GetLocations();
2104 Register temp = locations->GetTemp(0).AsRegister<Register>();
2105 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002106 Location receiver = locations->InAt(0);
2107 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2108
Roland Levillain3b359c72015-11-17 19:35:12 +00002109 // Set the hidden argument. This is safe to do this here, as R12
2110 // won't be modified thereafter, before the `blx` (call) instruction.
2111 DCHECK_EQ(R12, hidden_reg);
2112 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002113
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002114 if (receiver.IsStackSlot()) {
2115 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002116 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002117 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2118 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002119 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002120 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002121 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002122 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002123 // Instead of simply (possibly) unpoisoning `temp` here, we should
2124 // emit a read barrier for the previous class reference load.
2125 // However this is not required in practice, as this is an
2126 // intermediate/temporary reference and because the current
2127 // concurrent copying collector keeps the from-space memory
2128 // intact/accessible until the end of the marking phase (the
2129 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002130 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002131 __ LoadFromOffset(kLoadWord, temp, temp,
2132 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2133 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002134 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002135 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002136 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002137 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002138 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002139 // LR = temp->GetEntryPoint();
2140 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2141 // LR();
2142 __ blx(LR);
2143 DCHECK(!codegen_->IsLeafMethod());
2144 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2145}
2146
Roland Levillain88cb1752014-10-20 16:36:47 +01002147void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2148 LocationSummary* locations =
2149 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2150 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002151 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002152 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002153 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2154 break;
2155 }
2156 case Primitive::kPrimLong: {
2157 locations->SetInAt(0, Location::RequiresRegister());
2158 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002159 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002160 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002161
Roland Levillain88cb1752014-10-20 16:36:47 +01002162 case Primitive::kPrimFloat:
2163 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002164 locations->SetInAt(0, Location::RequiresFpuRegister());
2165 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002166 break;
2167
2168 default:
2169 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2170 }
2171}
2172
2173void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2174 LocationSummary* locations = neg->GetLocations();
2175 Location out = locations->Out();
2176 Location in = locations->InAt(0);
2177 switch (neg->GetResultType()) {
2178 case Primitive::kPrimInt:
2179 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002180 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002181 break;
2182
2183 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002184 DCHECK(in.IsRegisterPair());
2185 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2186 __ rsbs(out.AsRegisterPairLow<Register>(),
2187 in.AsRegisterPairLow<Register>(),
2188 ShifterOperand(0));
2189 // We cannot emit an RSC (Reverse Subtract with Carry)
2190 // instruction here, as it does not exist in the Thumb-2
2191 // instruction set. We use the following approach
2192 // using SBC and SUB instead.
2193 //
2194 // out.hi = -C
2195 __ sbc(out.AsRegisterPairHigh<Register>(),
2196 out.AsRegisterPairHigh<Register>(),
2197 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2198 // out.hi = out.hi - in.hi
2199 __ sub(out.AsRegisterPairHigh<Register>(),
2200 out.AsRegisterPairHigh<Register>(),
2201 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2202 break;
2203
Roland Levillain88cb1752014-10-20 16:36:47 +01002204 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002205 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002206 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002207 break;
2208
Roland Levillain88cb1752014-10-20 16:36:47 +01002209 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002210 DCHECK(in.IsFpuRegisterPair());
2211 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2212 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002213 break;
2214
2215 default:
2216 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2217 }
2218}
2219
Roland Levillaindff1f282014-11-05 14:15:05 +00002220void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002221 Primitive::Type result_type = conversion->GetResultType();
2222 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002223 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002224
Roland Levillain5b3ee562015-04-14 16:02:41 +01002225 // The float-to-long, double-to-long and long-to-float type conversions
2226 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002227 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002228 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2229 && result_type == Primitive::kPrimLong)
2230 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002231 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002232 : LocationSummary::kNoCall;
2233 LocationSummary* locations =
2234 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2235
David Brazdilb2bd1c52015-03-25 11:17:37 +00002236 // The Java language does not allow treating boolean as an integral type but
2237 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002238
Roland Levillaindff1f282014-11-05 14:15:05 +00002239 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002240 case Primitive::kPrimByte:
2241 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002242 case Primitive::kPrimLong:
2243 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002244 case Primitive::kPrimBoolean:
2245 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002246 case Primitive::kPrimShort:
2247 case Primitive::kPrimInt:
2248 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002249 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002250 locations->SetInAt(0, Location::RequiresRegister());
2251 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2252 break;
2253
2254 default:
2255 LOG(FATAL) << "Unexpected type conversion from " << input_type
2256 << " to " << result_type;
2257 }
2258 break;
2259
Roland Levillain01a8d712014-11-14 16:27:39 +00002260 case Primitive::kPrimShort:
2261 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002262 case Primitive::kPrimLong:
2263 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002264 case Primitive::kPrimBoolean:
2265 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002266 case Primitive::kPrimByte:
2267 case Primitive::kPrimInt:
2268 case Primitive::kPrimChar:
2269 // Processing a Dex `int-to-short' instruction.
2270 locations->SetInAt(0, Location::RequiresRegister());
2271 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2272 break;
2273
2274 default:
2275 LOG(FATAL) << "Unexpected type conversion from " << input_type
2276 << " to " << result_type;
2277 }
2278 break;
2279
Roland Levillain946e1432014-11-11 17:35:19 +00002280 case Primitive::kPrimInt:
2281 switch (input_type) {
2282 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002283 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002284 locations->SetInAt(0, Location::Any());
2285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2286 break;
2287
2288 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002289 // Processing a Dex `float-to-int' instruction.
2290 locations->SetInAt(0, Location::RequiresFpuRegister());
2291 locations->SetOut(Location::RequiresRegister());
2292 locations->AddTemp(Location::RequiresFpuRegister());
2293 break;
2294
Roland Levillain946e1432014-11-11 17:35:19 +00002295 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002296 // Processing a Dex `double-to-int' instruction.
2297 locations->SetInAt(0, Location::RequiresFpuRegister());
2298 locations->SetOut(Location::RequiresRegister());
2299 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002300 break;
2301
2302 default:
2303 LOG(FATAL) << "Unexpected type conversion from " << input_type
2304 << " to " << result_type;
2305 }
2306 break;
2307
Roland Levillaindff1f282014-11-05 14:15:05 +00002308 case Primitive::kPrimLong:
2309 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002310 case Primitive::kPrimBoolean:
2311 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002312 case Primitive::kPrimByte:
2313 case Primitive::kPrimShort:
2314 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002315 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002316 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002317 locations->SetInAt(0, Location::RequiresRegister());
2318 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2319 break;
2320
Roland Levillain624279f2014-12-04 11:54:28 +00002321 case Primitive::kPrimFloat: {
2322 // Processing a Dex `float-to-long' instruction.
2323 InvokeRuntimeCallingConvention calling_convention;
2324 locations->SetInAt(0, Location::FpuRegisterLocation(
2325 calling_convention.GetFpuRegisterAt(0)));
2326 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2327 break;
2328 }
2329
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002330 case Primitive::kPrimDouble: {
2331 // Processing a Dex `double-to-long' instruction.
2332 InvokeRuntimeCallingConvention calling_convention;
2333 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2334 calling_convention.GetFpuRegisterAt(0),
2335 calling_convention.GetFpuRegisterAt(1)));
2336 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002337 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002338 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002339
2340 default:
2341 LOG(FATAL) << "Unexpected type conversion from " << input_type
2342 << " to " << result_type;
2343 }
2344 break;
2345
Roland Levillain981e4542014-11-14 11:47:14 +00002346 case Primitive::kPrimChar:
2347 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002348 case Primitive::kPrimLong:
2349 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002350 case Primitive::kPrimBoolean:
2351 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002352 case Primitive::kPrimByte:
2353 case Primitive::kPrimShort:
2354 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002355 // Processing a Dex `int-to-char' instruction.
2356 locations->SetInAt(0, Location::RequiresRegister());
2357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2358 break;
2359
2360 default:
2361 LOG(FATAL) << "Unexpected type conversion from " << input_type
2362 << " to " << result_type;
2363 }
2364 break;
2365
Roland Levillaindff1f282014-11-05 14:15:05 +00002366 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002367 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002368 case Primitive::kPrimBoolean:
2369 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002370 case Primitive::kPrimByte:
2371 case Primitive::kPrimShort:
2372 case Primitive::kPrimInt:
2373 case Primitive::kPrimChar:
2374 // Processing a Dex `int-to-float' instruction.
2375 locations->SetInAt(0, Location::RequiresRegister());
2376 locations->SetOut(Location::RequiresFpuRegister());
2377 break;
2378
Roland Levillain5b3ee562015-04-14 16:02:41 +01002379 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002380 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002381 InvokeRuntimeCallingConvention calling_convention;
2382 locations->SetInAt(0, Location::RegisterPairLocation(
2383 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2384 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002385 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002386 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002387
Roland Levillaincff13742014-11-17 14:32:17 +00002388 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002389 // Processing a Dex `double-to-float' instruction.
2390 locations->SetInAt(0, Location::RequiresFpuRegister());
2391 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002392 break;
2393
2394 default:
2395 LOG(FATAL) << "Unexpected type conversion from " << input_type
2396 << " to " << result_type;
2397 };
2398 break;
2399
Roland Levillaindff1f282014-11-05 14:15:05 +00002400 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002401 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002402 case Primitive::kPrimBoolean:
2403 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002404 case Primitive::kPrimByte:
2405 case Primitive::kPrimShort:
2406 case Primitive::kPrimInt:
2407 case Primitive::kPrimChar:
2408 // Processing a Dex `int-to-double' instruction.
2409 locations->SetInAt(0, Location::RequiresRegister());
2410 locations->SetOut(Location::RequiresFpuRegister());
2411 break;
2412
2413 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002414 // Processing a Dex `long-to-double' instruction.
2415 locations->SetInAt(0, Location::RequiresRegister());
2416 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002417 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002418 locations->AddTemp(Location::RequiresFpuRegister());
2419 break;
2420
Roland Levillaincff13742014-11-17 14:32:17 +00002421 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002422 // Processing a Dex `float-to-double' instruction.
2423 locations->SetInAt(0, Location::RequiresFpuRegister());
2424 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002425 break;
2426
2427 default:
2428 LOG(FATAL) << "Unexpected type conversion from " << input_type
2429 << " to " << result_type;
2430 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002431 break;
2432
2433 default:
2434 LOG(FATAL) << "Unexpected type conversion from " << input_type
2435 << " to " << result_type;
2436 }
2437}
2438
2439void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2440 LocationSummary* locations = conversion->GetLocations();
2441 Location out = locations->Out();
2442 Location in = locations->InAt(0);
2443 Primitive::Type result_type = conversion->GetResultType();
2444 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002445 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002446 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002447 case Primitive::kPrimByte:
2448 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002449 case Primitive::kPrimLong:
2450 // Type conversion from long to byte is a result of code transformations.
2451 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2452 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002453 case Primitive::kPrimBoolean:
2454 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002455 case Primitive::kPrimShort:
2456 case Primitive::kPrimInt:
2457 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002458 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002459 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002460 break;
2461
2462 default:
2463 LOG(FATAL) << "Unexpected type conversion from " << input_type
2464 << " to " << result_type;
2465 }
2466 break;
2467
Roland Levillain01a8d712014-11-14 16:27:39 +00002468 case Primitive::kPrimShort:
2469 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002470 case Primitive::kPrimLong:
2471 // Type conversion from long to short is a result of code transformations.
2472 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2473 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002474 case Primitive::kPrimBoolean:
2475 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002476 case Primitive::kPrimByte:
2477 case Primitive::kPrimInt:
2478 case Primitive::kPrimChar:
2479 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002480 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002481 break;
2482
2483 default:
2484 LOG(FATAL) << "Unexpected type conversion from " << input_type
2485 << " to " << result_type;
2486 }
2487 break;
2488
Roland Levillain946e1432014-11-11 17:35:19 +00002489 case Primitive::kPrimInt:
2490 switch (input_type) {
2491 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002492 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002493 DCHECK(out.IsRegister());
2494 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002495 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002496 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002497 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002498 } else {
2499 DCHECK(in.IsConstant());
2500 DCHECK(in.GetConstant()->IsLongConstant());
2501 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002502 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002503 }
2504 break;
2505
Roland Levillain3f8f9362014-12-02 17:45:01 +00002506 case Primitive::kPrimFloat: {
2507 // Processing a Dex `float-to-int' instruction.
2508 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002509 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002510 __ vmovrs(out.AsRegister<Register>(), temp);
2511 break;
2512 }
2513
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002514 case Primitive::kPrimDouble: {
2515 // Processing a Dex `double-to-int' instruction.
2516 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002517 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002518 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002519 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002520 }
Roland Levillain946e1432014-11-11 17:35:19 +00002521
2522 default:
2523 LOG(FATAL) << "Unexpected type conversion from " << input_type
2524 << " to " << result_type;
2525 }
2526 break;
2527
Roland Levillaindff1f282014-11-05 14:15:05 +00002528 case Primitive::kPrimLong:
2529 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002530 case Primitive::kPrimBoolean:
2531 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002532 case Primitive::kPrimByte:
2533 case Primitive::kPrimShort:
2534 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002535 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002536 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002537 DCHECK(out.IsRegisterPair());
2538 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002539 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002540 // Sign extension.
2541 __ Asr(out.AsRegisterPairHigh<Register>(),
2542 out.AsRegisterPairLow<Register>(),
2543 31);
2544 break;
2545
2546 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002547 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002548 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002549 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002550 break;
2551
Roland Levillaindff1f282014-11-05 14:15:05 +00002552 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002553 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002554 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002555 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002556 break;
2557
2558 default:
2559 LOG(FATAL) << "Unexpected type conversion from " << input_type
2560 << " to " << result_type;
2561 }
2562 break;
2563
Roland Levillain981e4542014-11-14 11:47:14 +00002564 case Primitive::kPrimChar:
2565 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002566 case Primitive::kPrimLong:
2567 // Type conversion from long to char is a result of code transformations.
2568 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2569 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002570 case Primitive::kPrimBoolean:
2571 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002572 case Primitive::kPrimByte:
2573 case Primitive::kPrimShort:
2574 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002575 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002576 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002577 break;
2578
2579 default:
2580 LOG(FATAL) << "Unexpected type conversion from " << input_type
2581 << " to " << result_type;
2582 }
2583 break;
2584
Roland Levillaindff1f282014-11-05 14:15:05 +00002585 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002586 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002587 case Primitive::kPrimBoolean:
2588 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002589 case Primitive::kPrimByte:
2590 case Primitive::kPrimShort:
2591 case Primitive::kPrimInt:
2592 case Primitive::kPrimChar: {
2593 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2595 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002596 break;
2597 }
2598
Roland Levillain5b3ee562015-04-14 16:02:41 +01002599 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002600 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002601 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002602 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002603 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002604
Roland Levillaincff13742014-11-17 14:32:17 +00002605 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002606 // Processing a Dex `double-to-float' instruction.
2607 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2608 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002609 break;
2610
2611 default:
2612 LOG(FATAL) << "Unexpected type conversion from " << input_type
2613 << " to " << result_type;
2614 };
2615 break;
2616
Roland Levillaindff1f282014-11-05 14:15:05 +00002617 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002618 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002619 case Primitive::kPrimBoolean:
2620 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002621 case Primitive::kPrimByte:
2622 case Primitive::kPrimShort:
2623 case Primitive::kPrimInt:
2624 case Primitive::kPrimChar: {
2625 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002626 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002627 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2628 out.AsFpuRegisterPairLow<SRegister>());
2629 break;
2630 }
2631
Roland Levillain647b9ed2014-11-27 12:06:00 +00002632 case Primitive::kPrimLong: {
2633 // Processing a Dex `long-to-double' instruction.
2634 Register low = in.AsRegisterPairLow<Register>();
2635 Register high = in.AsRegisterPairHigh<Register>();
2636 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2637 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002638 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002639 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002640 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2641 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002642
Roland Levillain682393c2015-04-14 15:57:52 +01002643 // temp_d = int-to-double(high)
2644 __ vmovsr(temp_s, high);
2645 __ vcvtdi(temp_d, temp_s);
2646 // constant_d = k2Pow32EncodingForDouble
2647 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2648 // out_d = unsigned-to-double(low)
2649 __ vmovsr(out_s, low);
2650 __ vcvtdu(out_d, out_s);
2651 // out_d += temp_d * constant_d
2652 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002653 break;
2654 }
2655
Roland Levillaincff13742014-11-17 14:32:17 +00002656 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002657 // Processing a Dex `float-to-double' instruction.
2658 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2659 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002660 break;
2661
2662 default:
2663 LOG(FATAL) << "Unexpected type conversion from " << input_type
2664 << " to " << result_type;
2665 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002666 break;
2667
2668 default:
2669 LOG(FATAL) << "Unexpected type conversion from " << input_type
2670 << " to " << result_type;
2671 }
2672}
2673
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002674void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002675 LocationSummary* locations =
2676 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002677 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002678 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002679 locations->SetInAt(0, Location::RequiresRegister());
2680 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002681 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2682 break;
2683 }
2684
2685 case Primitive::kPrimLong: {
2686 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002687 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002688 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002689 break;
2690 }
2691
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002692 case Primitive::kPrimFloat:
2693 case Primitive::kPrimDouble: {
2694 locations->SetInAt(0, Location::RequiresFpuRegister());
2695 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002696 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002697 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002698 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002699
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002700 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002701 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002702 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002703}
2704
2705void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2706 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002707 Location out = locations->Out();
2708 Location first = locations->InAt(0);
2709 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002710 switch (add->GetResultType()) {
2711 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002712 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002713 __ add(out.AsRegister<Register>(),
2714 first.AsRegister<Register>(),
2715 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002716 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002717 __ AddConstant(out.AsRegister<Register>(),
2718 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002719 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002720 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002721 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002722
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002723 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002724 if (second.IsConstant()) {
2725 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2726 GenerateAddLongConst(out, first, value);
2727 } else {
2728 DCHECK(second.IsRegisterPair());
2729 __ adds(out.AsRegisterPairLow<Register>(),
2730 first.AsRegisterPairLow<Register>(),
2731 ShifterOperand(second.AsRegisterPairLow<Register>()));
2732 __ adc(out.AsRegisterPairHigh<Register>(),
2733 first.AsRegisterPairHigh<Register>(),
2734 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2735 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002736 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002737 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002738
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002739 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002740 __ vadds(out.AsFpuRegister<SRegister>(),
2741 first.AsFpuRegister<SRegister>(),
2742 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002743 break;
2744
2745 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002746 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2747 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2748 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002749 break;
2750
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002751 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002752 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002753 }
2754}
2755
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002756void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002757 LocationSummary* locations =
2758 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002759 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002760 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002761 locations->SetInAt(0, Location::RequiresRegister());
2762 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002763 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2764 break;
2765 }
2766
2767 case Primitive::kPrimLong: {
2768 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002769 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002770 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002771 break;
2772 }
Calin Juravle11351682014-10-23 15:38:15 +01002773 case Primitive::kPrimFloat:
2774 case Primitive::kPrimDouble: {
2775 locations->SetInAt(0, Location::RequiresFpuRegister());
2776 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002777 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002778 break;
Calin Juravle11351682014-10-23 15:38:15 +01002779 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002780 default:
Calin Juravle11351682014-10-23 15:38:15 +01002781 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002782 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002783}
2784
2785void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2786 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002787 Location out = locations->Out();
2788 Location first = locations->InAt(0);
2789 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002790 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002791 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002792 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002793 __ sub(out.AsRegister<Register>(),
2794 first.AsRegister<Register>(),
2795 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002796 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002797 __ AddConstant(out.AsRegister<Register>(),
2798 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002799 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002800 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002801 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002802 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002803
Calin Juravle11351682014-10-23 15:38:15 +01002804 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002805 if (second.IsConstant()) {
2806 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2807 GenerateAddLongConst(out, first, -value);
2808 } else {
2809 DCHECK(second.IsRegisterPair());
2810 __ subs(out.AsRegisterPairLow<Register>(),
2811 first.AsRegisterPairLow<Register>(),
2812 ShifterOperand(second.AsRegisterPairLow<Register>()));
2813 __ sbc(out.AsRegisterPairHigh<Register>(),
2814 first.AsRegisterPairHigh<Register>(),
2815 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2816 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002817 break;
Calin Juravle11351682014-10-23 15:38:15 +01002818 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002819
Calin Juravle11351682014-10-23 15:38:15 +01002820 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002821 __ vsubs(out.AsFpuRegister<SRegister>(),
2822 first.AsFpuRegister<SRegister>(),
2823 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002824 break;
Calin Juravle11351682014-10-23 15:38:15 +01002825 }
2826
2827 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002828 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2829 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2830 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002831 break;
2832 }
2833
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002834
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002835 default:
Calin Juravle11351682014-10-23 15:38:15 +01002836 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002837 }
2838}
2839
Calin Juravle34bacdf2014-10-07 20:23:36 +01002840void LocationsBuilderARM::VisitMul(HMul* mul) {
2841 LocationSummary* locations =
2842 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2843 switch (mul->GetResultType()) {
2844 case Primitive::kPrimInt:
2845 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002846 locations->SetInAt(0, Location::RequiresRegister());
2847 locations->SetInAt(1, Location::RequiresRegister());
2848 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002849 break;
2850 }
2851
Calin Juravleb5bfa962014-10-21 18:02:24 +01002852 case Primitive::kPrimFloat:
2853 case Primitive::kPrimDouble: {
2854 locations->SetInAt(0, Location::RequiresFpuRegister());
2855 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002856 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002857 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002858 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002859
2860 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002861 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002862 }
2863}
2864
2865void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2866 LocationSummary* locations = mul->GetLocations();
2867 Location out = locations->Out();
2868 Location first = locations->InAt(0);
2869 Location second = locations->InAt(1);
2870 switch (mul->GetResultType()) {
2871 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002872 __ mul(out.AsRegister<Register>(),
2873 first.AsRegister<Register>(),
2874 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002875 break;
2876 }
2877 case Primitive::kPrimLong: {
2878 Register out_hi = out.AsRegisterPairHigh<Register>();
2879 Register out_lo = out.AsRegisterPairLow<Register>();
2880 Register in1_hi = first.AsRegisterPairHigh<Register>();
2881 Register in1_lo = first.AsRegisterPairLow<Register>();
2882 Register in2_hi = second.AsRegisterPairHigh<Register>();
2883 Register in2_lo = second.AsRegisterPairLow<Register>();
2884
2885 // Extra checks to protect caused by the existence of R1_R2.
2886 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2887 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2888 DCHECK_NE(out_hi, in1_lo);
2889 DCHECK_NE(out_hi, in2_lo);
2890
2891 // input: in1 - 64 bits, in2 - 64 bits
2892 // output: out
2893 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2894 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2895 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2896
2897 // IP <- in1.lo * in2.hi
2898 __ mul(IP, in1_lo, in2_hi);
2899 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2900 __ mla(out_hi, in1_hi, in2_lo, IP);
2901 // out.lo <- (in1.lo * in2.lo)[31:0];
2902 __ umull(out_lo, IP, in1_lo, in2_lo);
2903 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2904 __ add(out_hi, out_hi, ShifterOperand(IP));
2905 break;
2906 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002907
2908 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002909 __ vmuls(out.AsFpuRegister<SRegister>(),
2910 first.AsFpuRegister<SRegister>(),
2911 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002912 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002913 }
2914
2915 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002916 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2917 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2918 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002919 break;
2920 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002921
2922 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002923 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002924 }
2925}
2926
Zheng Xuc6667102015-05-15 16:08:45 +08002927void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2928 DCHECK(instruction->IsDiv() || instruction->IsRem());
2929 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2930
2931 LocationSummary* locations = instruction->GetLocations();
2932 Location second = locations->InAt(1);
2933 DCHECK(second.IsConstant());
2934
2935 Register out = locations->Out().AsRegister<Register>();
2936 Register dividend = locations->InAt(0).AsRegister<Register>();
2937 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2938 DCHECK(imm == 1 || imm == -1);
2939
2940 if (instruction->IsRem()) {
2941 __ LoadImmediate(out, 0);
2942 } else {
2943 if (imm == 1) {
2944 __ Mov(out, dividend);
2945 } else {
2946 __ rsb(out, dividend, ShifterOperand(0));
2947 }
2948 }
2949}
2950
2951void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2952 DCHECK(instruction->IsDiv() || instruction->IsRem());
2953 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2954
2955 LocationSummary* locations = instruction->GetLocations();
2956 Location second = locations->InAt(1);
2957 DCHECK(second.IsConstant());
2958
2959 Register out = locations->Out().AsRegister<Register>();
2960 Register dividend = locations->InAt(0).AsRegister<Register>();
2961 Register temp = locations->GetTemp(0).AsRegister<Register>();
2962 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002963 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002964 int ctz_imm = CTZ(abs_imm);
2965
2966 if (ctz_imm == 1) {
2967 __ Lsr(temp, dividend, 32 - ctz_imm);
2968 } else {
2969 __ Asr(temp, dividend, 31);
2970 __ Lsr(temp, temp, 32 - ctz_imm);
2971 }
2972 __ add(out, temp, ShifterOperand(dividend));
2973
2974 if (instruction->IsDiv()) {
2975 __ Asr(out, out, ctz_imm);
2976 if (imm < 0) {
2977 __ rsb(out, out, ShifterOperand(0));
2978 }
2979 } else {
2980 __ ubfx(out, out, 0, ctz_imm);
2981 __ sub(out, out, ShifterOperand(temp));
2982 }
2983}
2984
2985void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2986 DCHECK(instruction->IsDiv() || instruction->IsRem());
2987 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2988
2989 LocationSummary* locations = instruction->GetLocations();
2990 Location second = locations->InAt(1);
2991 DCHECK(second.IsConstant());
2992
2993 Register out = locations->Out().AsRegister<Register>();
2994 Register dividend = locations->InAt(0).AsRegister<Register>();
2995 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2996 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2997 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2998
2999 int64_t magic;
3000 int shift;
3001 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3002
3003 __ LoadImmediate(temp1, magic);
3004 __ smull(temp2, temp1, dividend, temp1);
3005
3006 if (imm > 0 && magic < 0) {
3007 __ add(temp1, temp1, ShifterOperand(dividend));
3008 } else if (imm < 0 && magic > 0) {
3009 __ sub(temp1, temp1, ShifterOperand(dividend));
3010 }
3011
3012 if (shift != 0) {
3013 __ Asr(temp1, temp1, shift);
3014 }
3015
3016 if (instruction->IsDiv()) {
3017 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3018 } else {
3019 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3020 // TODO: Strength reduction for mls.
3021 __ LoadImmediate(temp2, imm);
3022 __ mls(out, temp1, temp2, dividend);
3023 }
3024}
3025
3026void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3027 DCHECK(instruction->IsDiv() || instruction->IsRem());
3028 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3029
3030 LocationSummary* locations = instruction->GetLocations();
3031 Location second = locations->InAt(1);
3032 DCHECK(second.IsConstant());
3033
3034 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3035 if (imm == 0) {
3036 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3037 } else if (imm == 1 || imm == -1) {
3038 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003039 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003040 DivRemByPowerOfTwo(instruction);
3041 } else {
3042 DCHECK(imm <= -2 || imm >= 2);
3043 GenerateDivRemWithAnyConstant(instruction);
3044 }
3045}
3046
Calin Juravle7c4954d2014-10-28 16:57:40 +00003047void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003048 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3049 if (div->GetResultType() == Primitive::kPrimLong) {
3050 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003051 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003052 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3053 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003054 } else if (div->GetResultType() == Primitive::kPrimInt &&
3055 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3056 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003057 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003058 }
3059
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003060 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3061
Calin Juravle7c4954d2014-10-28 16:57:40 +00003062 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003063 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003064 if (div->InputAt(1)->IsConstant()) {
3065 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003066 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003067 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003068 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3069 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003070 // No temp register required.
3071 } else {
3072 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003073 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003074 locations->AddTemp(Location::RequiresRegister());
3075 }
3076 }
3077 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003078 locations->SetInAt(0, Location::RequiresRegister());
3079 locations->SetInAt(1, Location::RequiresRegister());
3080 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3081 } else {
3082 InvokeRuntimeCallingConvention calling_convention;
3083 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3084 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3085 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3086 // we only need the former.
3087 locations->SetOut(Location::RegisterLocation(R0));
3088 }
Calin Juravled0d48522014-11-04 16:40:20 +00003089 break;
3090 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003091 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003092 InvokeRuntimeCallingConvention calling_convention;
3093 locations->SetInAt(0, Location::RegisterPairLocation(
3094 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3095 locations->SetInAt(1, Location::RegisterPairLocation(
3096 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003097 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003098 break;
3099 }
3100 case Primitive::kPrimFloat:
3101 case Primitive::kPrimDouble: {
3102 locations->SetInAt(0, Location::RequiresFpuRegister());
3103 locations->SetInAt(1, Location::RequiresFpuRegister());
3104 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3105 break;
3106 }
3107
3108 default:
3109 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3110 }
3111}
3112
3113void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3114 LocationSummary* locations = div->GetLocations();
3115 Location out = locations->Out();
3116 Location first = locations->InAt(0);
3117 Location second = locations->InAt(1);
3118
3119 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003120 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003121 if (second.IsConstant()) {
3122 GenerateDivRemConstantIntegral(div);
3123 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003124 __ sdiv(out.AsRegister<Register>(),
3125 first.AsRegister<Register>(),
3126 second.AsRegister<Register>());
3127 } else {
3128 InvokeRuntimeCallingConvention calling_convention;
3129 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3130 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3131 DCHECK_EQ(R0, out.AsRegister<Register>());
3132
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003133 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003134 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003135 }
Calin Juravled0d48522014-11-04 16:40:20 +00003136 break;
3137 }
3138
Calin Juravle7c4954d2014-10-28 16:57:40 +00003139 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003140 InvokeRuntimeCallingConvention calling_convention;
3141 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3142 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3143 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3144 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3145 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003146 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003147
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003148 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003149 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003150 break;
3151 }
3152
3153 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003154 __ vdivs(out.AsFpuRegister<SRegister>(),
3155 first.AsFpuRegister<SRegister>(),
3156 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003157 break;
3158 }
3159
3160 case Primitive::kPrimDouble: {
3161 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3162 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3163 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3164 break;
3165 }
3166
3167 default:
3168 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3169 }
3170}
3171
Calin Juravlebacfec32014-11-14 15:54:36 +00003172void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003173 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003174
3175 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003176 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003177 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3178 // sdiv will be replaced by other instruction sequence.
3179 call_kind = LocationSummary::kNoCall;
3180 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3181 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003182 // Have hardware divide instruction for int, do it with three instructions.
3183 call_kind = LocationSummary::kNoCall;
3184 }
3185
Calin Juravlebacfec32014-11-14 15:54:36 +00003186 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3187
Calin Juravled2ec87d2014-12-08 14:24:46 +00003188 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003189 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003190 if (rem->InputAt(1)->IsConstant()) {
3191 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003192 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003193 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003194 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3195 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003196 // No temp register required.
3197 } else {
3198 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003199 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003200 locations->AddTemp(Location::RequiresRegister());
3201 }
3202 }
3203 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003204 locations->SetInAt(0, Location::RequiresRegister());
3205 locations->SetInAt(1, Location::RequiresRegister());
3206 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3207 locations->AddTemp(Location::RequiresRegister());
3208 } else {
3209 InvokeRuntimeCallingConvention calling_convention;
3210 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3211 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3212 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3213 // we only need the latter.
3214 locations->SetOut(Location::RegisterLocation(R1));
3215 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003216 break;
3217 }
3218 case Primitive::kPrimLong: {
3219 InvokeRuntimeCallingConvention calling_convention;
3220 locations->SetInAt(0, Location::RegisterPairLocation(
3221 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3222 locations->SetInAt(1, Location::RegisterPairLocation(
3223 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3224 // The runtime helper puts the output in R2,R3.
3225 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3226 break;
3227 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003228 case Primitive::kPrimFloat: {
3229 InvokeRuntimeCallingConvention calling_convention;
3230 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3231 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3232 locations->SetOut(Location::FpuRegisterLocation(S0));
3233 break;
3234 }
3235
Calin Juravlebacfec32014-11-14 15:54:36 +00003236 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003237 InvokeRuntimeCallingConvention calling_convention;
3238 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3239 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3240 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3241 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3242 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003243 break;
3244 }
3245
3246 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003247 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003248 }
3249}
3250
3251void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3252 LocationSummary* locations = rem->GetLocations();
3253 Location out = locations->Out();
3254 Location first = locations->InAt(0);
3255 Location second = locations->InAt(1);
3256
Calin Juravled2ec87d2014-12-08 14:24:46 +00003257 Primitive::Type type = rem->GetResultType();
3258 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003259 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003260 if (second.IsConstant()) {
3261 GenerateDivRemConstantIntegral(rem);
3262 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003263 Register reg1 = first.AsRegister<Register>();
3264 Register reg2 = second.AsRegister<Register>();
3265 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003266
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003267 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003268 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003269 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003270 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003271 } else {
3272 InvokeRuntimeCallingConvention calling_convention;
3273 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3274 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3275 DCHECK_EQ(R1, out.AsRegister<Register>());
3276
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003277 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003278 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003279 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003280 break;
3281 }
3282
3283 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003284 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003285 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003286 break;
3287 }
3288
Calin Juravled2ec87d2014-12-08 14:24:46 +00003289 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003290 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003291 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003292 break;
3293 }
3294
Calin Juravlebacfec32014-11-14 15:54:36 +00003295 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003296 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003297 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003298 break;
3299 }
3300
3301 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003302 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003303 }
3304}
3305
Calin Juravled0d48522014-11-04 16:40:20 +00003306void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003307 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003308 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003309}
3310
3311void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003312 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003313 codegen_->AddSlowPath(slow_path);
3314
3315 LocationSummary* locations = instruction->GetLocations();
3316 Location value = locations->InAt(0);
3317
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003318 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003319 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003320 case Primitive::kPrimByte:
3321 case Primitive::kPrimChar:
3322 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003323 case Primitive::kPrimInt: {
3324 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003325 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003326 } else {
3327 DCHECK(value.IsConstant()) << value;
3328 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3329 __ b(slow_path->GetEntryLabel());
3330 }
3331 }
3332 break;
3333 }
3334 case Primitive::kPrimLong: {
3335 if (value.IsRegisterPair()) {
3336 __ orrs(IP,
3337 value.AsRegisterPairLow<Register>(),
3338 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3339 __ b(slow_path->GetEntryLabel(), EQ);
3340 } else {
3341 DCHECK(value.IsConstant()) << value;
3342 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3343 __ b(slow_path->GetEntryLabel());
3344 }
3345 }
3346 break;
3347 default:
3348 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3349 }
3350 }
Calin Juravled0d48522014-11-04 16:40:20 +00003351}
3352
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003353void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3354 Register in = locations->InAt(0).AsRegister<Register>();
3355 Location rhs = locations->InAt(1);
3356 Register out = locations->Out().AsRegister<Register>();
3357
3358 if (rhs.IsConstant()) {
3359 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3360 // so map all rotations to a +ve. equivalent in that range.
3361 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3362 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3363 if (rot) {
3364 // Rotate, mapping left rotations to right equivalents if necessary.
3365 // (e.g. left by 2 bits == right by 30.)
3366 __ Ror(out, in, rot);
3367 } else if (out != in) {
3368 __ Mov(out, in);
3369 }
3370 } else {
3371 __ Ror(out, in, rhs.AsRegister<Register>());
3372 }
3373}
3374
3375// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3376// rotates by swapping input regs (effectively rotating by the first 32-bits of
3377// a larger rotation) or flipping direction (thus treating larger right/left
3378// rotations as sub-word sized rotations in the other direction) as appropriate.
3379void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3380 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3381 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3382 Location rhs = locations->InAt(1);
3383 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3384 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3385
3386 if (rhs.IsConstant()) {
3387 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3388 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003389 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003390 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3391 // logic below to a simple pair of binary orr.
3392 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3393 if (rot >= kArmBitsPerWord) {
3394 rot -= kArmBitsPerWord;
3395 std::swap(in_reg_hi, in_reg_lo);
3396 }
3397 // Rotate, or mov to out for zero or word size rotations.
3398 if (rot != 0u) {
3399 __ Lsr(out_reg_hi, in_reg_hi, rot);
3400 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3401 __ Lsr(out_reg_lo, in_reg_lo, rot);
3402 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3403 } else {
3404 __ Mov(out_reg_lo, in_reg_lo);
3405 __ Mov(out_reg_hi, in_reg_hi);
3406 }
3407 } else {
3408 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3409 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3410 Label end;
3411 Label shift_by_32_plus_shift_right;
3412
3413 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3414 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3415 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3416 __ b(&shift_by_32_plus_shift_right, CC);
3417
3418 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3419 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3420 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3421 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3422 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3423 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3424 __ Lsr(shift_left, in_reg_hi, shift_right);
3425 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3426 __ b(&end);
3427
3428 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3429 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3430 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3431 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3432 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3433 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3434 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3435 __ Lsl(shift_right, in_reg_hi, shift_left);
3436 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3437
3438 __ Bind(&end);
3439 }
3440}
Roland Levillain22c49222016-03-18 14:04:28 +00003441
3442void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003443 LocationSummary* locations =
3444 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3445 switch (ror->GetResultType()) {
3446 case Primitive::kPrimInt: {
3447 locations->SetInAt(0, Location::RequiresRegister());
3448 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3450 break;
3451 }
3452 case Primitive::kPrimLong: {
3453 locations->SetInAt(0, Location::RequiresRegister());
3454 if (ror->InputAt(1)->IsConstant()) {
3455 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3456 } else {
3457 locations->SetInAt(1, Location::RequiresRegister());
3458 locations->AddTemp(Location::RequiresRegister());
3459 locations->AddTemp(Location::RequiresRegister());
3460 }
3461 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3462 break;
3463 }
3464 default:
3465 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3466 }
3467}
3468
Roland Levillain22c49222016-03-18 14:04:28 +00003469void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003470 LocationSummary* locations = ror->GetLocations();
3471 Primitive::Type type = ror->GetResultType();
3472 switch (type) {
3473 case Primitive::kPrimInt: {
3474 HandleIntegerRotate(locations);
3475 break;
3476 }
3477 case Primitive::kPrimLong: {
3478 HandleLongRotate(locations);
3479 break;
3480 }
3481 default:
3482 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003483 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003484 }
3485}
3486
Calin Juravle9aec02f2014-11-18 23:06:35 +00003487void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3488 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3489
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003490 LocationSummary* locations =
3491 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003492
3493 switch (op->GetResultType()) {
3494 case Primitive::kPrimInt: {
3495 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003496 if (op->InputAt(1)->IsConstant()) {
3497 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3498 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3499 } else {
3500 locations->SetInAt(1, Location::RequiresRegister());
3501 // Make the output overlap, as it will be used to hold the masked
3502 // second input.
3503 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3504 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003505 break;
3506 }
3507 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003508 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003509 if (op->InputAt(1)->IsConstant()) {
3510 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3511 // For simplicity, use kOutputOverlap even though we only require that low registers
3512 // don't clash with high registers which the register allocator currently guarantees.
3513 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3514 } else {
3515 locations->SetInAt(1, Location::RequiresRegister());
3516 locations->AddTemp(Location::RequiresRegister());
3517 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3518 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003519 break;
3520 }
3521 default:
3522 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3523 }
3524}
3525
3526void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3527 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3528
3529 LocationSummary* locations = op->GetLocations();
3530 Location out = locations->Out();
3531 Location first = locations->InAt(0);
3532 Location second = locations->InAt(1);
3533
3534 Primitive::Type type = op->GetResultType();
3535 switch (type) {
3536 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003537 Register out_reg = out.AsRegister<Register>();
3538 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003539 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003540 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003541 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003542 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003543 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003544 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003545 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003546 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003547 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003548 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003549 }
3550 } else {
3551 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003552 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003553 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003554 __ Mov(out_reg, first_reg);
3555 } else if (op->IsShl()) {
3556 __ Lsl(out_reg, first_reg, shift_value);
3557 } else if (op->IsShr()) {
3558 __ Asr(out_reg, first_reg, shift_value);
3559 } else {
3560 __ Lsr(out_reg, first_reg, shift_value);
3561 }
3562 }
3563 break;
3564 }
3565 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003566 Register o_h = out.AsRegisterPairHigh<Register>();
3567 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003568
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003569 Register high = first.AsRegisterPairHigh<Register>();
3570 Register low = first.AsRegisterPairLow<Register>();
3571
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003572 if (second.IsRegister()) {
3573 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003574
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003575 Register second_reg = second.AsRegister<Register>();
3576
3577 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003578 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003579 // Shift the high part
3580 __ Lsl(o_h, high, o_l);
3581 // Shift the low part and `or` what overflew on the high part
3582 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3583 __ Lsr(temp, low, temp);
3584 __ orr(o_h, o_h, ShifterOperand(temp));
3585 // If the shift is > 32 bits, override the high part
3586 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3587 __ it(PL);
3588 __ Lsl(o_h, low, temp, PL);
3589 // Shift the low part
3590 __ Lsl(o_l, low, o_l);
3591 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003592 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003593 // Shift the low part
3594 __ Lsr(o_l, low, o_h);
3595 // Shift the high part and `or` what underflew on the low part
3596 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3597 __ Lsl(temp, high, temp);
3598 __ orr(o_l, o_l, ShifterOperand(temp));
3599 // If the shift is > 32 bits, override the low part
3600 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3601 __ it(PL);
3602 __ Asr(o_l, high, temp, PL);
3603 // Shift the high part
3604 __ Asr(o_h, high, o_h);
3605 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003606 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003607 // same as Shr except we use `Lsr`s and not `Asr`s
3608 __ Lsr(o_l, low, o_h);
3609 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3610 __ Lsl(temp, high, temp);
3611 __ orr(o_l, o_l, ShifterOperand(temp));
3612 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3613 __ it(PL);
3614 __ Lsr(o_l, high, temp, PL);
3615 __ Lsr(o_h, high, o_h);
3616 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003617 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003618 // Register allocator doesn't create partial overlap.
3619 DCHECK_NE(o_l, high);
3620 DCHECK_NE(o_h, low);
3621 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003622 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003623 if (shift_value > 32) {
3624 if (op->IsShl()) {
3625 __ Lsl(o_h, low, shift_value - 32);
3626 __ LoadImmediate(o_l, 0);
3627 } else if (op->IsShr()) {
3628 __ Asr(o_l, high, shift_value - 32);
3629 __ Asr(o_h, high, 31);
3630 } else {
3631 __ Lsr(o_l, high, shift_value - 32);
3632 __ LoadImmediate(o_h, 0);
3633 }
3634 } else if (shift_value == 32) {
3635 if (op->IsShl()) {
3636 __ mov(o_h, ShifterOperand(low));
3637 __ LoadImmediate(o_l, 0);
3638 } else if (op->IsShr()) {
3639 __ mov(o_l, ShifterOperand(high));
3640 __ Asr(o_h, high, 31);
3641 } else {
3642 __ mov(o_l, ShifterOperand(high));
3643 __ LoadImmediate(o_h, 0);
3644 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003645 } else if (shift_value == 1) {
3646 if (op->IsShl()) {
3647 __ Lsls(o_l, low, 1);
3648 __ adc(o_h, high, ShifterOperand(high));
3649 } else if (op->IsShr()) {
3650 __ Asrs(o_h, high, 1);
3651 __ Rrx(o_l, low);
3652 } else {
3653 __ Lsrs(o_h, high, 1);
3654 __ Rrx(o_l, low);
3655 }
3656 } else {
3657 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003658 if (op->IsShl()) {
3659 __ Lsl(o_h, high, shift_value);
3660 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3661 __ Lsl(o_l, low, shift_value);
3662 } else if (op->IsShr()) {
3663 __ Lsr(o_l, low, shift_value);
3664 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3665 __ Asr(o_h, high, shift_value);
3666 } else {
3667 __ Lsr(o_l, low, shift_value);
3668 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3669 __ Lsr(o_h, high, shift_value);
3670 }
3671 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003672 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003673 break;
3674 }
3675 default:
3676 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003677 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003678 }
3679}
3680
3681void LocationsBuilderARM::VisitShl(HShl* shl) {
3682 HandleShift(shl);
3683}
3684
3685void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3686 HandleShift(shl);
3687}
3688
3689void LocationsBuilderARM::VisitShr(HShr* shr) {
3690 HandleShift(shr);
3691}
3692
3693void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3694 HandleShift(shr);
3695}
3696
3697void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3698 HandleShift(ushr);
3699}
3700
3701void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3702 HandleShift(ushr);
3703}
3704
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003705void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003706 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003707 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003708 if (instruction->IsStringAlloc()) {
3709 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3710 } else {
3711 InvokeRuntimeCallingConvention calling_convention;
3712 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3713 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3714 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003715 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003716}
3717
3718void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003719 // Note: if heap poisoning is enabled, the entry point takes cares
3720 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003721 if (instruction->IsStringAlloc()) {
3722 // String is allocated through StringFactory. Call NewEmptyString entry point.
3723 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003724 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003725 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3726 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3727 __ blx(LR);
3728 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3729 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003730 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003731 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3732 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003733}
3734
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003735void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3736 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003737 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003738 InvokeRuntimeCallingConvention calling_convention;
3739 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003740 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003741 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003742 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003743}
3744
3745void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3746 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003747 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003748 // Note: if heap poisoning is enabled, the entry point takes cares
3749 // of poisoning the reference.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003750 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003751 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003752}
3753
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003754void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003755 LocationSummary* locations =
3756 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003757 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3758 if (location.IsStackSlot()) {
3759 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3760 } else if (location.IsDoubleStackSlot()) {
3761 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003762 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003763 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003764}
3765
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003766void InstructionCodeGeneratorARM::VisitParameterValue(
3767 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003768 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003769}
3770
3771void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3772 LocationSummary* locations =
3773 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3774 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3775}
3776
3777void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3778 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003779}
3780
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003781void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003782 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003783 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003784 locations->SetInAt(0, Location::RequiresRegister());
3785 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003786}
3787
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003788void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3789 LocationSummary* locations = not_->GetLocations();
3790 Location out = locations->Out();
3791 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003792 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003793 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003794 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003795 break;
3796
3797 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003798 __ mvn(out.AsRegisterPairLow<Register>(),
3799 ShifterOperand(in.AsRegisterPairLow<Register>()));
3800 __ mvn(out.AsRegisterPairHigh<Register>(),
3801 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003802 break;
3803
3804 default:
3805 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3806 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003807}
3808
David Brazdil66d126e2015-04-03 16:02:44 +01003809void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3810 LocationSummary* locations =
3811 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3812 locations->SetInAt(0, Location::RequiresRegister());
3813 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3814}
3815
3816void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003817 LocationSummary* locations = bool_not->GetLocations();
3818 Location out = locations->Out();
3819 Location in = locations->InAt(0);
3820 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3821}
3822
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003823void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003824 LocationSummary* locations =
3825 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003826 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003827 case Primitive::kPrimBoolean:
3828 case Primitive::kPrimByte:
3829 case Primitive::kPrimShort:
3830 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003831 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003832 case Primitive::kPrimLong: {
3833 locations->SetInAt(0, Location::RequiresRegister());
3834 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003835 // Output overlaps because it is written before doing the low comparison.
3836 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003837 break;
3838 }
3839 case Primitive::kPrimFloat:
3840 case Primitive::kPrimDouble: {
3841 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003842 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00003843 locations->SetOut(Location::RequiresRegister());
3844 break;
3845 }
3846 default:
3847 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3848 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003849}
3850
3851void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003852 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003853 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003854 Location left = locations->InAt(0);
3855 Location right = locations->InAt(1);
3856
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003857 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003858 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003859 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003860 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003861 case Primitive::kPrimBoolean:
3862 case Primitive::kPrimByte:
3863 case Primitive::kPrimShort:
3864 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003865 case Primitive::kPrimInt: {
3866 __ LoadImmediate(out, 0);
3867 __ cmp(left.AsRegister<Register>(),
3868 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3869 less_cond = LT;
3870 break;
3871 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003872 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003873 __ cmp(left.AsRegisterPairHigh<Register>(),
3874 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003875 __ b(&less, LT);
3876 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003877 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003878 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003879 __ cmp(left.AsRegisterPairLow<Register>(),
3880 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003881 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003882 break;
3883 }
3884 case Primitive::kPrimFloat:
3885 case Primitive::kPrimDouble: {
3886 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003887 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00003888 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003889 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003890 break;
3891 }
3892 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003893 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003894 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003895 }
Aart Bika19616e2016-02-01 18:57:58 -08003896
Calin Juravleddb7df22014-11-25 20:56:51 +00003897 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003898 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003899
3900 __ Bind(&greater);
3901 __ LoadImmediate(out, 1);
3902 __ b(&done);
3903
3904 __ Bind(&less);
3905 __ LoadImmediate(out, -1);
3906
3907 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003908}
3909
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003910void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003911 LocationSummary* locations =
3912 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003913 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003914 locations->SetInAt(i, Location::Any());
3915 }
3916 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003917}
3918
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003919void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003920 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003921}
3922
Roland Levillainc9285912015-12-18 10:38:42 +00003923void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3924 // TODO (ported from quick): revisit ARM barrier kinds.
3925 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003926 switch (kind) {
3927 case MemBarrierKind::kAnyStore:
3928 case MemBarrierKind::kLoadAny:
3929 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003930 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003931 break;
3932 }
3933 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003934 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003935 break;
3936 }
3937 default:
3938 LOG(FATAL) << "Unexpected memory barrier " << kind;
3939 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003940 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003941}
3942
3943void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3944 uint32_t offset,
3945 Register out_lo,
3946 Register out_hi) {
3947 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003948 // Ensure `out_lo` is different from `addr`, so that loading
3949 // `offset` into `out_lo` does not clutter `addr`.
3950 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003951 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003952 __ add(IP, addr, ShifterOperand(out_lo));
3953 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003954 }
3955 __ ldrexd(out_lo, out_hi, addr);
3956}
3957
3958void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3959 uint32_t offset,
3960 Register value_lo,
3961 Register value_hi,
3962 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003963 Register temp2,
3964 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003965 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003966 if (offset != 0) {
3967 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003968 __ add(IP, addr, ShifterOperand(temp1));
3969 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003970 }
3971 __ Bind(&fail);
3972 // We need a load followed by store. (The address used in a STREX instruction must
3973 // be the same as the address in the most recently executed LDREX instruction.)
3974 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003975 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003976 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003977 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003978}
3979
3980void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3981 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3982
Nicolas Geoffray39468442014-09-02 15:17:15 +01003983 LocationSummary* locations =
3984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003985 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003986
Calin Juravle52c48962014-12-16 17:02:57 +00003987 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003988 if (Primitive::IsFloatingPointType(field_type)) {
3989 locations->SetInAt(1, Location::RequiresFpuRegister());
3990 } else {
3991 locations->SetInAt(1, Location::RequiresRegister());
3992 }
3993
Calin Juravle52c48962014-12-16 17:02:57 +00003994 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003995 bool generate_volatile = field_info.IsVolatile()
3996 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003997 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003998 bool needs_write_barrier =
3999 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004000 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004001 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004002 if (needs_write_barrier) {
4003 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004004 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004005 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004006 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004007 // - registers need to be consecutive
4008 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004009 // We don't test for ARM yet, and the assertion makes sure that we
4010 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004011 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4012
4013 locations->AddTemp(Location::RequiresRegister());
4014 locations->AddTemp(Location::RequiresRegister());
4015 if (field_type == Primitive::kPrimDouble) {
4016 // For doubles we need two more registers to copy the value.
4017 locations->AddTemp(Location::RegisterLocation(R2));
4018 locations->AddTemp(Location::RegisterLocation(R3));
4019 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004020 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004021}
4022
Calin Juravle52c48962014-12-16 17:02:57 +00004023void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004024 const FieldInfo& field_info,
4025 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004026 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4027
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004028 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004029 Register base = locations->InAt(0).AsRegister<Register>();
4030 Location value = locations->InAt(1);
4031
4032 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004033 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004034 Primitive::Type field_type = field_info.GetFieldType();
4035 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004036 bool needs_write_barrier =
4037 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004038
4039 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004040 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004041 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004042
4043 switch (field_type) {
4044 case Primitive::kPrimBoolean:
4045 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004046 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004047 break;
4048 }
4049
4050 case Primitive::kPrimShort:
4051 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004052 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004053 break;
4054 }
4055
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004056 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004057 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004058 if (kPoisonHeapReferences && needs_write_barrier) {
4059 // Note that in the case where `value` is a null reference,
4060 // we do not enter this block, as a null reference does not
4061 // need poisoning.
4062 DCHECK_EQ(field_type, Primitive::kPrimNot);
4063 Register temp = locations->GetTemp(0).AsRegister<Register>();
4064 __ Mov(temp, value.AsRegister<Register>());
4065 __ PoisonHeapReference(temp);
4066 __ StoreToOffset(kStoreWord, temp, base, offset);
4067 } else {
4068 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4069 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004070 break;
4071 }
4072
4073 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004074 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004075 GenerateWideAtomicStore(base, offset,
4076 value.AsRegisterPairLow<Register>(),
4077 value.AsRegisterPairHigh<Register>(),
4078 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004079 locations->GetTemp(1).AsRegister<Register>(),
4080 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004081 } else {
4082 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004083 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004084 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004085 break;
4086 }
4087
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004088 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004089 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004090 break;
4091 }
4092
4093 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004094 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004095 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004096 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4097 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4098
4099 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4100
4101 GenerateWideAtomicStore(base, offset,
4102 value_reg_lo,
4103 value_reg_hi,
4104 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004105 locations->GetTemp(3).AsRegister<Register>(),
4106 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004107 } else {
4108 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004109 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004110 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004111 break;
4112 }
4113
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004114 case Primitive::kPrimVoid:
4115 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004116 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004117 }
Calin Juravle52c48962014-12-16 17:02:57 +00004118
Calin Juravle77520bc2015-01-12 18:45:46 +00004119 // Longs and doubles are handled in the switch.
4120 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4121 codegen_->MaybeRecordImplicitNullCheck(instruction);
4122 }
4123
4124 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4125 Register temp = locations->GetTemp(0).AsRegister<Register>();
4126 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004127 codegen_->MarkGCCard(
4128 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004129 }
4130
Calin Juravle52c48962014-12-16 17:02:57 +00004131 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004132 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004133 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004134}
4135
Calin Juravle52c48962014-12-16 17:02:57 +00004136void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4137 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004138
4139 bool object_field_get_with_read_barrier =
4140 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004141 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004142 new (GetGraph()->GetArena()) LocationSummary(instruction,
4143 object_field_get_with_read_barrier ?
4144 LocationSummary::kCallOnSlowPath :
4145 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004146 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004147 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004148 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004149 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004150
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004151 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004152 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004153 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004154 // The output overlaps in case of volatile long: we don't want the
4155 // code generated by GenerateWideAtomicLoad to overwrite the
4156 // object's location. Likewise, in the case of an object field get
4157 // with read barriers enabled, we do not want the load to overwrite
4158 // the object's location, as we need it to emit the read barrier.
4159 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4160 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004161
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004162 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4163 locations->SetOut(Location::RequiresFpuRegister());
4164 } else {
4165 locations->SetOut(Location::RequiresRegister(),
4166 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4167 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004168 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004169 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004170 // - registers need to be consecutive
4171 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004172 // We don't test for ARM yet, and the assertion makes sure that we
4173 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004174 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4175 locations->AddTemp(Location::RequiresRegister());
4176 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004177 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4178 // We need a temporary register for the read barrier marking slow
4179 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4180 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004181 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004182}
4183
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004184Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4185 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4186 << input->GetType();
4187 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4188 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4189 return Location::ConstantLocation(input->AsConstant());
4190 } else {
4191 return Location::RequiresFpuRegister();
4192 }
4193}
4194
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004195Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4196 Opcode opcode) {
4197 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4198 if (constant->IsConstant() &&
4199 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4200 return Location::ConstantLocation(constant->AsConstant());
4201 }
4202 return Location::RequiresRegister();
4203}
4204
4205bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4206 Opcode opcode) {
4207 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4208 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004209 Opcode high_opcode = opcode;
4210 SetCc low_set_cc = kCcDontCare;
4211 switch (opcode) {
4212 case SUB:
4213 // Flip the operation to an ADD.
4214 value = -value;
4215 opcode = ADD;
4216 FALLTHROUGH_INTENDED;
4217 case ADD:
4218 if (Low32Bits(value) == 0u) {
4219 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4220 }
4221 high_opcode = ADC;
4222 low_set_cc = kCcSet;
4223 break;
4224 default:
4225 break;
4226 }
4227 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4228 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004229 } else {
4230 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4231 }
4232}
4233
Vladimir Marko59751a72016-08-05 14:37:27 +01004234bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4235 Opcode opcode,
4236 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004237 ShifterOperand so;
4238 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004239 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004240 return true;
4241 }
4242 Opcode neg_opcode = kNoOperand;
4243 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004244 case AND: neg_opcode = BIC; value = ~value; break;
4245 case ORR: neg_opcode = ORN; value = ~value; break;
4246 case ADD: neg_opcode = SUB; value = -value; break;
4247 case ADC: neg_opcode = SBC; value = ~value; break;
4248 case SUB: neg_opcode = ADD; value = -value; break;
4249 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004250 default:
4251 return false;
4252 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004253 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004254}
4255
Calin Juravle52c48962014-12-16 17:02:57 +00004256void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4257 const FieldInfo& field_info) {
4258 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004259
Calin Juravle52c48962014-12-16 17:02:57 +00004260 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004261 Location base_loc = locations->InAt(0);
4262 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004263 Location out = locations->Out();
4264 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004265 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004266 Primitive::Type field_type = field_info.GetFieldType();
4267 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4268
4269 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004270 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004271 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004272 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004273
Roland Levillainc9285912015-12-18 10:38:42 +00004274 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004275 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004276 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004277
Roland Levillainc9285912015-12-18 10:38:42 +00004278 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004279 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004280 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004281
Roland Levillainc9285912015-12-18 10:38:42 +00004282 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004283 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004284 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004285
4286 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004287 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004288 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004289
4290 case Primitive::kPrimNot: {
4291 // /* HeapReference<Object> */ out = *(base + offset)
4292 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4293 Location temp_loc = locations->GetTemp(0);
4294 // Note that a potential implicit null check is handled in this
4295 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4296 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4297 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4298 if (is_volatile) {
4299 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4300 }
4301 } else {
4302 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4303 codegen_->MaybeRecordImplicitNullCheck(instruction);
4304 if (is_volatile) {
4305 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4306 }
4307 // If read barriers are enabled, emit read barriers other than
4308 // Baker's using a slow path (and also unpoison the loaded
4309 // reference, if heap poisoning is enabled).
4310 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4311 }
4312 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004313 }
4314
Roland Levillainc9285912015-12-18 10:38:42 +00004315 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004316 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004317 GenerateWideAtomicLoad(base, offset,
4318 out.AsRegisterPairLow<Register>(),
4319 out.AsRegisterPairHigh<Register>());
4320 } else {
4321 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4322 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004323 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004324
Roland Levillainc9285912015-12-18 10:38:42 +00004325 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004326 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004327 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004328
4329 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004330 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004331 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004332 Register lo = locations->GetTemp(0).AsRegister<Register>();
4333 Register hi = locations->GetTemp(1).AsRegister<Register>();
4334 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004335 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004336 __ vmovdrr(out_reg, lo, hi);
4337 } else {
4338 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004339 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004340 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004341 break;
4342 }
4343
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004344 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004345 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004346 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004347 }
Calin Juravle52c48962014-12-16 17:02:57 +00004348
Roland Levillainc9285912015-12-18 10:38:42 +00004349 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4350 // Potential implicit null checks, in the case of reference or
4351 // double fields, are handled in the previous switch statement.
4352 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004353 codegen_->MaybeRecordImplicitNullCheck(instruction);
4354 }
4355
Calin Juravle52c48962014-12-16 17:02:57 +00004356 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004357 if (field_type == Primitive::kPrimNot) {
4358 // Memory barriers, in the case of references, are also handled
4359 // in the previous switch statement.
4360 } else {
4361 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4362 }
Roland Levillain4d027112015-07-01 15:41:14 +01004363 }
Calin Juravle52c48962014-12-16 17:02:57 +00004364}
4365
4366void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4367 HandleFieldSet(instruction, instruction->GetFieldInfo());
4368}
4369
4370void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004371 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004372}
4373
4374void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4375 HandleFieldGet(instruction, instruction->GetFieldInfo());
4376}
4377
4378void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4379 HandleFieldGet(instruction, instruction->GetFieldInfo());
4380}
4381
4382void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4383 HandleFieldGet(instruction, instruction->GetFieldInfo());
4384}
4385
4386void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4387 HandleFieldGet(instruction, instruction->GetFieldInfo());
4388}
4389
4390void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4391 HandleFieldSet(instruction, instruction->GetFieldInfo());
4392}
4393
4394void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004395 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004396}
4397
Calin Juravlee460d1d2015-09-29 04:52:17 +01004398void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4399 HUnresolvedInstanceFieldGet* instruction) {
4400 FieldAccessCallingConventionARM calling_convention;
4401 codegen_->CreateUnresolvedFieldLocationSummary(
4402 instruction, instruction->GetFieldType(), calling_convention);
4403}
4404
4405void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4406 HUnresolvedInstanceFieldGet* instruction) {
4407 FieldAccessCallingConventionARM calling_convention;
4408 codegen_->GenerateUnresolvedFieldAccess(instruction,
4409 instruction->GetFieldType(),
4410 instruction->GetFieldIndex(),
4411 instruction->GetDexPc(),
4412 calling_convention);
4413}
4414
4415void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4416 HUnresolvedInstanceFieldSet* instruction) {
4417 FieldAccessCallingConventionARM calling_convention;
4418 codegen_->CreateUnresolvedFieldLocationSummary(
4419 instruction, instruction->GetFieldType(), calling_convention);
4420}
4421
4422void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4423 HUnresolvedInstanceFieldSet* instruction) {
4424 FieldAccessCallingConventionARM calling_convention;
4425 codegen_->GenerateUnresolvedFieldAccess(instruction,
4426 instruction->GetFieldType(),
4427 instruction->GetFieldIndex(),
4428 instruction->GetDexPc(),
4429 calling_convention);
4430}
4431
4432void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4433 HUnresolvedStaticFieldGet* instruction) {
4434 FieldAccessCallingConventionARM calling_convention;
4435 codegen_->CreateUnresolvedFieldLocationSummary(
4436 instruction, instruction->GetFieldType(), calling_convention);
4437}
4438
4439void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4440 HUnresolvedStaticFieldGet* instruction) {
4441 FieldAccessCallingConventionARM calling_convention;
4442 codegen_->GenerateUnresolvedFieldAccess(instruction,
4443 instruction->GetFieldType(),
4444 instruction->GetFieldIndex(),
4445 instruction->GetDexPc(),
4446 calling_convention);
4447}
4448
4449void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4450 HUnresolvedStaticFieldSet* instruction) {
4451 FieldAccessCallingConventionARM calling_convention;
4452 codegen_->CreateUnresolvedFieldLocationSummary(
4453 instruction, instruction->GetFieldType(), calling_convention);
4454}
4455
4456void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4457 HUnresolvedStaticFieldSet* instruction) {
4458 FieldAccessCallingConventionARM calling_convention;
4459 codegen_->GenerateUnresolvedFieldAccess(instruction,
4460 instruction->GetFieldType(),
4461 instruction->GetFieldIndex(),
4462 instruction->GetDexPc(),
4463 calling_convention);
4464}
4465
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004466void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004467 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4468 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004469}
4470
Calin Juravle2ae48182016-03-16 14:05:09 +00004471void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4472 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004473 return;
4474 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004475 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004476
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004477 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004478 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004479}
4480
Calin Juravle2ae48182016-03-16 14:05:09 +00004481void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004482 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004483 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484
4485 LocationSummary* locations = instruction->GetLocations();
4486 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004487
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004488 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004489}
4490
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004491void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004492 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004493}
4494
Artem Serov6c916792016-07-11 14:02:34 +01004495static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4496 switch (type) {
4497 case Primitive::kPrimNot:
4498 return kLoadWord;
4499 case Primitive::kPrimBoolean:
4500 return kLoadUnsignedByte;
4501 case Primitive::kPrimByte:
4502 return kLoadSignedByte;
4503 case Primitive::kPrimChar:
4504 return kLoadUnsignedHalfword;
4505 case Primitive::kPrimShort:
4506 return kLoadSignedHalfword;
4507 case Primitive::kPrimInt:
4508 return kLoadWord;
4509 case Primitive::kPrimLong:
4510 return kLoadWordPair;
4511 case Primitive::kPrimFloat:
4512 return kLoadSWord;
4513 case Primitive::kPrimDouble:
4514 return kLoadDWord;
4515 default:
4516 LOG(FATAL) << "Unreachable type " << type;
4517 UNREACHABLE();
4518 }
4519}
4520
4521static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4522 switch (type) {
4523 case Primitive::kPrimNot:
4524 return kStoreWord;
4525 case Primitive::kPrimBoolean:
4526 case Primitive::kPrimByte:
4527 return kStoreByte;
4528 case Primitive::kPrimChar:
4529 case Primitive::kPrimShort:
4530 return kStoreHalfword;
4531 case Primitive::kPrimInt:
4532 return kStoreWord;
4533 case Primitive::kPrimLong:
4534 return kStoreWordPair;
4535 case Primitive::kPrimFloat:
4536 return kStoreSWord;
4537 case Primitive::kPrimDouble:
4538 return kStoreDWord;
4539 default:
4540 LOG(FATAL) << "Unreachable type " << type;
4541 UNREACHABLE();
4542 }
4543}
4544
4545void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4546 Location out_loc,
4547 Register base,
4548 Register reg_offset,
4549 Condition cond) {
4550 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4551 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4552
4553 switch (type) {
4554 case Primitive::kPrimByte:
4555 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4556 break;
4557 case Primitive::kPrimBoolean:
4558 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4559 break;
4560 case Primitive::kPrimShort:
4561 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4562 break;
4563 case Primitive::kPrimChar:
4564 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4565 break;
4566 case Primitive::kPrimNot:
4567 case Primitive::kPrimInt:
4568 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4569 break;
4570 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4571 case Primitive::kPrimLong:
4572 case Primitive::kPrimFloat:
4573 case Primitive::kPrimDouble:
4574 default:
4575 LOG(FATAL) << "Unreachable type " << type;
4576 UNREACHABLE();
4577 }
4578}
4579
4580void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4581 Location loc,
4582 Register base,
4583 Register reg_offset,
4584 Condition cond) {
4585 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4586 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4587
4588 switch (type) {
4589 case Primitive::kPrimByte:
4590 case Primitive::kPrimBoolean:
4591 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4592 break;
4593 case Primitive::kPrimShort:
4594 case Primitive::kPrimChar:
4595 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4596 break;
4597 case Primitive::kPrimNot:
4598 case Primitive::kPrimInt:
4599 __ str(loc.AsRegister<Register>(), mem_address, cond);
4600 break;
4601 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4602 case Primitive::kPrimLong:
4603 case Primitive::kPrimFloat:
4604 case Primitive::kPrimDouble:
4605 default:
4606 LOG(FATAL) << "Unreachable type " << type;
4607 UNREACHABLE();
4608 }
4609}
4610
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004611void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004612 bool object_array_get_with_read_barrier =
4613 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004614 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004615 new (GetGraph()->GetArena()) LocationSummary(instruction,
4616 object_array_get_with_read_barrier ?
4617 LocationSummary::kCallOnSlowPath :
4618 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004619 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004620 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004621 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004622 locations->SetInAt(0, Location::RequiresRegister());
4623 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004624 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4625 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4626 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004627 // The output overlaps in the case of an object array get with
4628 // read barriers enabled: we do not want the move to overwrite the
4629 // array's location, as we need it to emit the read barrier.
4630 locations->SetOut(
4631 Location::RequiresRegister(),
4632 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004633 }
Roland Levillainc9285912015-12-18 10:38:42 +00004634 // We need a temporary register for the read barrier marking slow
4635 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4636 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4637 locations->AddTemp(Location::RequiresRegister());
4638 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004639}
4640
4641void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4642 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004643 Location obj_loc = locations->InAt(0);
4644 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004645 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004646 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004647 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004648 Primitive::Type type = instruction->GetType();
Artem Serov328429f2016-07-06 16:23:04 +01004649 HInstruction* array_instr = instruction->GetArray();
4650 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004651 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4652 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Artem Serov6c916792016-07-11 14:02:34 +01004653
Roland Levillain4d027112015-07-01 15:41:14 +01004654 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004655 case Primitive::kPrimBoolean:
4656 case Primitive::kPrimByte:
4657 case Primitive::kPrimShort:
4658 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004659 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004660 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004661 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4662 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4663
4664 LoadOperandType load_type = GetLoadOperandType(type);
4665 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004666 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004667 Register temp = IP;
4668
4669 if (has_intermediate_address) {
4670 // We do not need to compute the intermediate address from the array: the
4671 // input instruction has done it already. See the comment in
4672 // `TryExtractArrayAccessAddress()`.
4673 if (kIsDebugBuild) {
4674 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4675 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4676 }
4677 temp = obj;
4678 } else {
4679 __ add(temp, obj, ShifterOperand(data_offset));
4680 }
4681 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004682 }
4683 break;
4684 }
4685
Roland Levillainc9285912015-12-18 10:38:42 +00004686 case Primitive::kPrimNot: {
4687 static_assert(
4688 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4689 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004690 // /* HeapReference<Object> */ out =
4691 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4692 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4693 Location temp = locations->GetTemp(0);
4694 // Note that a potential implicit null check is handled in this
4695 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4696 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4697 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4698 } else {
4699 Register out = out_loc.AsRegister<Register>();
4700 if (index.IsConstant()) {
4701 size_t offset =
4702 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4703 __ LoadFromOffset(kLoadWord, out, obj, offset);
4704 codegen_->MaybeRecordImplicitNullCheck(instruction);
4705 // If read barriers are enabled, emit read barriers other than
4706 // Baker's using a slow path (and also unpoison the loaded
4707 // reference, if heap poisoning is enabled).
4708 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4709 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004710 Register temp = IP;
4711
4712 if (has_intermediate_address) {
4713 // We do not need to compute the intermediate address from the array: the
4714 // input instruction has done it already. See the comment in
4715 // `TryExtractArrayAccessAddress()`.
4716 if (kIsDebugBuild) {
4717 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4718 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4719 }
4720 temp = obj;
4721 } else {
4722 __ add(temp, obj, ShifterOperand(data_offset));
4723 }
4724 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004725
Roland Levillainc9285912015-12-18 10:38:42 +00004726 codegen_->MaybeRecordImplicitNullCheck(instruction);
4727 // If read barriers are enabled, emit read barriers other than
4728 // Baker's using a slow path (and also unpoison the loaded
4729 // reference, if heap poisoning is enabled).
4730 codegen_->MaybeGenerateReadBarrierSlow(
4731 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4732 }
4733 }
4734 break;
4735 }
4736
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004737 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004738 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004739 size_t offset =
4740 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004741 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004742 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004743 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004744 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004745 }
4746 break;
4747 }
4748
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004749 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004750 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004751 if (index.IsConstant()) {
4752 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004753 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004754 } else {
4755 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004756 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004757 }
4758 break;
4759 }
4760
4761 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004762 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004763 if (index.IsConstant()) {
4764 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004765 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004766 } else {
4767 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004768 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004769 }
4770 break;
4771 }
4772
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004773 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004774 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004775 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004776 }
Roland Levillain4d027112015-07-01 15:41:14 +01004777
4778 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004779 // Potential implicit null checks, in the case of reference
4780 // arrays, are handled in the previous switch statement.
4781 } else {
4782 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004783 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004784}
4785
4786void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004787 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004788
4789 bool needs_write_barrier =
4790 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004791 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004792
Nicolas Geoffray39468442014-09-02 15:17:15 +01004793 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004794 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004795 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00004796 LocationSummary::kCallOnSlowPath :
4797 LocationSummary::kNoCall);
4798
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004799 locations->SetInAt(0, Location::RequiresRegister());
4800 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4801 if (Primitive::IsFloatingPointType(value_type)) {
4802 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004803 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004804 locations->SetInAt(2, Location::RequiresRegister());
4805 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004806 if (needs_write_barrier) {
4807 // Temporary registers for the write barrier.
4808 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004809 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004810 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004811}
4812
4813void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4814 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004815 Location array_loc = locations->InAt(0);
4816 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004817 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004818 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004819 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004820 bool needs_write_barrier =
4821 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01004822 uint32_t data_offset =
4823 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4824 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01004825 HInstruction* array_instr = instruction->GetArray();
4826 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004827 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4828 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004829
4830 switch (value_type) {
4831 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01004832 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004833 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01004834 case Primitive::kPrimChar:
4835 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004836 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004837 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4838 uint32_t full_offset =
4839 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4840 StoreOperandType store_type = GetStoreOperandType(value_type);
4841 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004842 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004843 Register temp = IP;
4844
4845 if (has_intermediate_address) {
4846 // We do not need to compute the intermediate address from the array: the
4847 // input instruction has done it already. See the comment in
4848 // `TryExtractArrayAccessAddress()`.
4849 if (kIsDebugBuild) {
4850 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4851 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4852 }
4853 temp = array;
4854 } else {
4855 __ add(temp, array, ShifterOperand(data_offset));
4856 }
Artem Serov6c916792016-07-11 14:02:34 +01004857 codegen_->StoreToShiftedRegOffset(value_type,
4858 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01004859 temp,
Artem Serov6c916792016-07-11 14:02:34 +01004860 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004861 }
4862 break;
4863 }
4864
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004865 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004866 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01004867 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4868 // See the comment in instruction_simplifier_shared.cc.
4869 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004870
4871 if (instruction->InputAt(2)->IsNullConstant()) {
4872 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004873 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004874 size_t offset =
4875 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01004876 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004877 } else {
4878 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004879 __ add(IP, array, ShifterOperand(data_offset));
4880 codegen_->StoreToShiftedRegOffset(value_type,
4881 value_loc,
4882 IP,
4883 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004884 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004885 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004886 DCHECK(!needs_write_barrier);
4887 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004888 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004889 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004890
4891 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01004892 Location temp1_loc = locations->GetTemp(0);
4893 Register temp1 = temp1_loc.AsRegister<Register>();
4894 Location temp2_loc = locations->GetTemp(1);
4895 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004896 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4897 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4898 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4899 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01004900 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004901
Roland Levillain3b359c72015-11-17 19:35:12 +00004902 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004903 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4904 codegen_->AddSlowPath(slow_path);
4905 if (instruction->GetValueCanBeNull()) {
4906 Label non_zero;
4907 __ CompareAndBranchIfNonZero(value, &non_zero);
4908 if (index.IsConstant()) {
4909 size_t offset =
4910 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4911 __ StoreToOffset(kStoreWord, value, array, offset);
4912 } else {
4913 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004914 __ add(IP, array, ShifterOperand(data_offset));
4915 codegen_->StoreToShiftedRegOffset(value_type,
4916 value_loc,
4917 IP,
4918 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004919 }
4920 codegen_->MaybeRecordImplicitNullCheck(instruction);
4921 __ b(&done);
4922 __ Bind(&non_zero);
4923 }
4924
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004925 // Note that when read barriers are enabled, the type checks
4926 // are performed without read barriers. This is fine, even in
4927 // the case where a class object is in the from-space after
4928 // the flip, as a comparison involving such a type would not
4929 // produce a false positive; it may of course produce a false
4930 // negative, in which case we would take the ArraySet slow
4931 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004932
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004933 // /* HeapReference<Class> */ temp1 = array->klass_
4934 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4935 codegen_->MaybeRecordImplicitNullCheck(instruction);
4936 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01004937
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004938 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4939 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4940 // /* HeapReference<Class> */ temp2 = value->klass_
4941 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4942 // If heap poisoning is enabled, no need to unpoison `temp1`
4943 // nor `temp2`, as we are comparing two poisoned references.
4944 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01004945
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004946 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4947 Label do_put;
4948 __ b(&do_put, EQ);
4949 // If heap poisoning is enabled, the `temp1` reference has
4950 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00004951 __ MaybeUnpoisonHeapReference(temp1);
4952
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004953 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4954 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4955 // If heap poisoning is enabled, no need to unpoison
4956 // `temp1`, as we are comparing against null below.
4957 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4958 __ Bind(&do_put);
4959 } else {
4960 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004961 }
4962 }
4963
Artem Serov6c916792016-07-11 14:02:34 +01004964 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004965 if (kPoisonHeapReferences) {
4966 // Note that in the case where `value` is a null reference,
4967 // we do not enter this block, as a null reference does not
4968 // need poisoning.
4969 DCHECK_EQ(value_type, Primitive::kPrimNot);
4970 __ Mov(temp1, value);
4971 __ PoisonHeapReference(temp1);
4972 source = temp1;
4973 }
4974
4975 if (index.IsConstant()) {
4976 size_t offset =
4977 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4978 __ StoreToOffset(kStoreWord, source, array, offset);
4979 } else {
4980 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004981
4982 __ add(IP, array, ShifterOperand(data_offset));
4983 codegen_->StoreToShiftedRegOffset(value_type,
4984 Location::RegisterLocation(source),
4985 IP,
4986 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004987 }
4988
Roland Levillain3b359c72015-11-17 19:35:12 +00004989 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004990 codegen_->MaybeRecordImplicitNullCheck(instruction);
4991 }
4992
4993 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4994
4995 if (done.IsLinked()) {
4996 __ Bind(&done);
4997 }
4998
4999 if (slow_path != nullptr) {
5000 __ Bind(slow_path->GetExitLabel());
5001 }
5002
5003 break;
5004 }
5005
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005006 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005007 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005008 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005009 size_t offset =
5010 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005011 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005012 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005013 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005014 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005015 }
5016 break;
5017 }
5018
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005019 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005020 Location value = locations->InAt(2);
5021 DCHECK(value.IsFpuRegister());
5022 if (index.IsConstant()) {
5023 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005024 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005025 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005026 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005027 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5028 }
5029 break;
5030 }
5031
5032 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005033 Location value = locations->InAt(2);
5034 DCHECK(value.IsFpuRegisterPair());
5035 if (index.IsConstant()) {
5036 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005037 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005038 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005039 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005040 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5041 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005042
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005043 break;
5044 }
5045
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005046 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005047 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005048 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005049 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005050
Roland Levillain80e67092016-01-08 16:04:55 +00005051 // Objects are handled in the switch.
5052 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005053 codegen_->MaybeRecordImplicitNullCheck(instruction);
5054 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005055}
5056
5057void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005058 LocationSummary* locations =
5059 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005060 locations->SetInAt(0, Location::RequiresRegister());
5061 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005062}
5063
5064void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5065 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005066 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005067 Register obj = locations->InAt(0).AsRegister<Register>();
5068 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005070 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071}
5072
Artem Serov328429f2016-07-06 16:23:04 +01005073void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00005074 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
5075 DCHECK(!kEmitCompilerReadBarrier);
Artem Serov328429f2016-07-06 16:23:04 +01005076 LocationSummary* locations =
5077 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5078
5079 locations->SetInAt(0, Location::RequiresRegister());
5080 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5081 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5082}
5083
5084void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5085 LocationSummary* locations = instruction->GetLocations();
5086 Location out = locations->Out();
5087 Location first = locations->InAt(0);
5088 Location second = locations->InAt(1);
5089
Roland Levillain4a3aa572016-08-15 13:17:06 +00005090 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
5091 DCHECK(!kEmitCompilerReadBarrier);
5092
Artem Serov328429f2016-07-06 16:23:04 +01005093 if (second.IsRegister()) {
5094 __ add(out.AsRegister<Register>(),
5095 first.AsRegister<Register>(),
5096 ShifterOperand(second.AsRegister<Register>()));
5097 } else {
5098 __ AddConstant(out.AsRegister<Register>(),
5099 first.AsRegister<Register>(),
5100 second.GetConstant()->AsIntConstant()->GetValue());
5101 }
5102}
5103
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005105 RegisterSet caller_saves = RegisterSet::Empty();
5106 InvokeRuntimeCallingConvention calling_convention;
5107 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5108 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5109 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 locations->SetInAt(0, Location::RequiresRegister());
5111 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112}
5113
5114void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5115 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005116 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005117 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005118 codegen_->AddSlowPath(slow_path);
5119
Roland Levillain271ab9c2014-11-27 15:23:57 +00005120 Register index = locations->InAt(0).AsRegister<Register>();
5121 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122
5123 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005124 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005125}
5126
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005127void CodeGeneratorARM::MarkGCCard(Register temp,
5128 Register card,
5129 Register object,
5130 Register value,
5131 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005132 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005133 if (can_be_null) {
5134 __ CompareAndBranchIfZero(value, &is_null);
5135 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005136 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5138 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005139 if (can_be_null) {
5140 __ Bind(&is_null);
5141 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005142}
5143
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005144void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005145 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005146}
5147
5148void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005149 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5150}
5151
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005152void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005153 LocationSummary* locations =
5154 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005155 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005156}
5157
5158void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005159 HBasicBlock* block = instruction->GetBlock();
5160 if (block->GetLoopInformation() != nullptr) {
5161 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5162 // The back edge will generate the suspend check.
5163 return;
5164 }
5165 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5166 // The goto will generate the suspend check.
5167 return;
5168 }
5169 GenerateSuspendCheck(instruction, nullptr);
5170}
5171
5172void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5173 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005174 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005175 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5176 if (slow_path == nullptr) {
5177 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5178 instruction->SetSlowPath(slow_path);
5179 codegen_->AddSlowPath(slow_path);
5180 if (successor != nullptr) {
5181 DCHECK(successor->IsLoopHeader());
5182 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5183 }
5184 } else {
5185 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5186 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005187
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005188 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005189 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005190 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005191 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005192 __ Bind(slow_path->GetReturnLabel());
5193 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005194 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005195 __ b(slow_path->GetEntryLabel());
5196 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005197}
5198
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005199ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5200 return codegen_->GetAssembler();
5201}
5202
5203void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005204 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005205 Location source = move->GetSource();
5206 Location destination = move->GetDestination();
5207
5208 if (source.IsRegister()) {
5209 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005210 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005211 } else if (destination.IsFpuRegister()) {
5212 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005213 } else {
5214 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005215 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005216 SP, destination.GetStackIndex());
5217 }
5218 } else if (source.IsStackSlot()) {
5219 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005220 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005221 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005222 } else if (destination.IsFpuRegister()) {
5223 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005224 } else {
5225 DCHECK(destination.IsStackSlot());
5226 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5227 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5228 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005229 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005230 if (destination.IsRegister()) {
5231 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5232 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005233 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005234 } else {
5235 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005236 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5237 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005238 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005239 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005240 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5241 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005242 } else if (destination.IsRegisterPair()) {
5243 DCHECK(ExpectedPairLayout(destination));
5244 __ LoadFromOffset(
5245 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5246 } else {
5247 DCHECK(destination.IsFpuRegisterPair()) << destination;
5248 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5249 SP,
5250 source.GetStackIndex());
5251 }
5252 } else if (source.IsRegisterPair()) {
5253 if (destination.IsRegisterPair()) {
5254 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5255 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005256 } else if (destination.IsFpuRegisterPair()) {
5257 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5258 source.AsRegisterPairLow<Register>(),
5259 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005260 } else {
5261 DCHECK(destination.IsDoubleStackSlot()) << destination;
5262 DCHECK(ExpectedPairLayout(source));
5263 __ StoreToOffset(
5264 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5265 }
5266 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005267 if (destination.IsRegisterPair()) {
5268 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5269 destination.AsRegisterPairHigh<Register>(),
5270 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5271 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005272 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5273 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5274 } else {
5275 DCHECK(destination.IsDoubleStackSlot()) << destination;
5276 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5277 SP,
5278 destination.GetStackIndex());
5279 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005280 } else {
5281 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005282 HConstant* constant = source.GetConstant();
5283 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5284 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005285 if (destination.IsRegister()) {
5286 __ LoadImmediate(destination.AsRegister<Register>(), value);
5287 } else {
5288 DCHECK(destination.IsStackSlot());
5289 __ LoadImmediate(IP, value);
5290 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5291 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005292 } else if (constant->IsLongConstant()) {
5293 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005294 if (destination.IsRegisterPair()) {
5295 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5296 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005297 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005298 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005299 __ LoadImmediate(IP, Low32Bits(value));
5300 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5301 __ LoadImmediate(IP, High32Bits(value));
5302 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5303 }
5304 } else if (constant->IsDoubleConstant()) {
5305 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005306 if (destination.IsFpuRegisterPair()) {
5307 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005308 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005309 DCHECK(destination.IsDoubleStackSlot()) << destination;
5310 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005311 __ LoadImmediate(IP, Low32Bits(int_value));
5312 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5313 __ LoadImmediate(IP, High32Bits(int_value));
5314 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5315 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005316 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005317 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005318 float value = constant->AsFloatConstant()->GetValue();
5319 if (destination.IsFpuRegister()) {
5320 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5321 } else {
5322 DCHECK(destination.IsStackSlot());
5323 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5324 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5325 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005326 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005327 }
5328}
5329
5330void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5331 __ Mov(IP, reg);
5332 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5333 __ StoreToOffset(kStoreWord, IP, SP, mem);
5334}
5335
5336void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5337 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5338 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5339 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5340 SP, mem1 + stack_offset);
5341 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5342 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5343 SP, mem2 + stack_offset);
5344 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5345}
5346
5347void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005348 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005349 Location source = move->GetSource();
5350 Location destination = move->GetDestination();
5351
5352 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005353 DCHECK_NE(source.AsRegister<Register>(), IP);
5354 DCHECK_NE(destination.AsRegister<Register>(), IP);
5355 __ Mov(IP, source.AsRegister<Register>());
5356 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5357 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005358 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005359 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005360 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005361 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005362 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5363 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005364 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005365 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005366 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005367 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005368 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005369 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005370 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005371 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005372 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5373 destination.AsRegisterPairHigh<Register>(),
5374 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005375 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005376 Register low_reg = source.IsRegisterPair()
5377 ? source.AsRegisterPairLow<Register>()
5378 : destination.AsRegisterPairLow<Register>();
5379 int mem = source.IsRegisterPair()
5380 ? destination.GetStackIndex()
5381 : source.GetStackIndex();
5382 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005383 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005384 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005385 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005386 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005387 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5388 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005389 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005390 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005391 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005392 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5393 DRegister reg = source.IsFpuRegisterPair()
5394 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5395 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5396 int mem = source.IsFpuRegisterPair()
5397 ? destination.GetStackIndex()
5398 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005399 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005400 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005401 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005402 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5403 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5404 : destination.AsFpuRegister<SRegister>();
5405 int mem = source.IsFpuRegister()
5406 ? destination.GetStackIndex()
5407 : source.GetStackIndex();
5408
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005409 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005410 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005411 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005412 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005413 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5414 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005415 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005416 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005417 }
5418}
5419
5420void ParallelMoveResolverARM::SpillScratch(int reg) {
5421 __ Push(static_cast<Register>(reg));
5422}
5423
5424void ParallelMoveResolverARM::RestoreScratch(int reg) {
5425 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005426}
5427
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005428HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5429 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005430 switch (desired_class_load_kind) {
5431 case HLoadClass::LoadKind::kReferrersClass:
5432 break;
5433 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5434 DCHECK(!GetCompilerOptions().GetCompilePic());
5435 break;
5436 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5437 DCHECK(GetCompilerOptions().GetCompilePic());
5438 break;
5439 case HLoadClass::LoadKind::kBootImageAddress:
5440 break;
5441 case HLoadClass::LoadKind::kDexCacheAddress:
5442 DCHECK(Runtime::Current()->UseJitCompilation());
5443 break;
5444 case HLoadClass::LoadKind::kDexCachePcRelative:
5445 DCHECK(!Runtime::Current()->UseJitCompilation());
5446 // We disable pc-relative load when there is an irreducible loop, as the optimization
5447 // is incompatible with it.
5448 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5449 // with irreducible loops.
5450 if (GetGraph()->HasIrreducibleLoops()) {
5451 return HLoadClass::LoadKind::kDexCacheViaMethod;
5452 }
5453 break;
5454 case HLoadClass::LoadKind::kDexCacheViaMethod:
5455 break;
5456 }
5457 return desired_class_load_kind;
5458}
5459
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005460void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005461 if (cls->NeedsAccessCheck()) {
5462 InvokeRuntimeCallingConvention calling_convention;
5463 CodeGenerator::CreateLoadClassLocationSummary(
5464 cls,
5465 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5466 Location::RegisterLocation(R0),
5467 /* code_generator_supports_read_barrier */ true);
5468 return;
5469 }
5470
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005471 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5472 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005473 ? LocationSummary::kCallOnSlowPath
5474 : LocationSummary::kNoCall;
5475 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005476 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005477 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005478 }
5479
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005480 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5481 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5482 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5483 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5484 locations->SetInAt(0, Location::RequiresRegister());
5485 }
5486 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005487}
5488
5489void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005490 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005491 if (cls->NeedsAccessCheck()) {
5492 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005493 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005494 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005495 return;
5496 }
5497
Roland Levillain3b359c72015-11-17 19:35:12 +00005498 Location out_loc = locations->Out();
5499 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005500
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005501 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005502 bool generate_null_check = false;
5503 switch (cls->GetLoadKind()) {
5504 case HLoadClass::LoadKind::kReferrersClass: {
5505 DCHECK(!cls->CanCallRuntime());
5506 DCHECK(!cls->MustGenerateClinitCheck());
5507 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5508 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005509 GenerateGcRootFieldLoad(cls,
5510 out_loc,
5511 current_method,
5512 ArtMethod::DeclaringClassOffset().Int32Value(),
5513 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005514 break;
5515 }
5516 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005517 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005518 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5519 cls->GetTypeIndex()));
5520 break;
5521 }
5522 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005523 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005524 CodeGeneratorARM::PcRelativePatchInfo* labels =
5525 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5526 __ BindTrackedLabel(&labels->movw_label);
5527 __ movw(out, /* placeholder */ 0u);
5528 __ BindTrackedLabel(&labels->movt_label);
5529 __ movt(out, /* placeholder */ 0u);
5530 __ BindTrackedLabel(&labels->add_pc_label);
5531 __ add(out, out, ShifterOperand(PC));
5532 break;
5533 }
5534 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005535 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005536 DCHECK_NE(cls->GetAddress(), 0u);
5537 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5538 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5539 break;
5540 }
5541 case HLoadClass::LoadKind::kDexCacheAddress: {
5542 DCHECK_NE(cls->GetAddress(), 0u);
5543 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5544 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5545 // a 128B range. To try and reduce the number of literals if we load multiple types,
5546 // simply split the dex cache address to a 128B aligned base loaded from a literal
5547 // and the remaining offset embedded in the load.
5548 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5549 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5550 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5551 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5552 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5553 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5554 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005555 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005556 generate_null_check = !cls->IsInDexCache();
5557 break;
5558 }
5559 case HLoadClass::LoadKind::kDexCachePcRelative: {
5560 Register base_reg = locations->InAt(0).AsRegister<Register>();
5561 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5562 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5563 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005564 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005565 generate_null_check = !cls->IsInDexCache();
5566 break;
5567 }
5568 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5569 // /* GcRoot<mirror::Class>[] */ out =
5570 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5571 Register current_method = locations->InAt(0).AsRegister<Register>();
5572 __ LoadFromOffset(kLoadWord,
5573 out,
5574 current_method,
5575 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5576 // /* GcRoot<mirror::Class> */ out = out[type_index]
5577 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005578 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005579 generate_null_check = !cls->IsInDexCache();
5580 }
5581 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005582
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005583 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5584 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005585 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005586 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5587 codegen_->AddSlowPath(slow_path);
5588 if (generate_null_check) {
5589 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5590 }
5591 if (cls->MustGenerateClinitCheck()) {
5592 GenerateClassInitializationCheck(slow_path, out);
5593 } else {
5594 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005595 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005596 }
5597}
5598
5599void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5600 LocationSummary* locations =
5601 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5602 locations->SetInAt(0, Location::RequiresRegister());
5603 if (check->HasUses()) {
5604 locations->SetOut(Location::SameAsFirstInput());
5605 }
5606}
5607
5608void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005609 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005610 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005611 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005612 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005613 GenerateClassInitializationCheck(slow_path,
5614 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005615}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005616
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005617void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005618 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005619 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5620 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5621 __ b(slow_path->GetEntryLabel(), LT);
5622 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5623 // properly. Therefore, we do a memory fence.
5624 __ dmb(ISH);
5625 __ Bind(slow_path->GetExitLabel());
5626}
5627
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005628HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5629 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005630 switch (desired_string_load_kind) {
5631 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5632 DCHECK(!GetCompilerOptions().GetCompilePic());
5633 break;
5634 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5635 DCHECK(GetCompilerOptions().GetCompilePic());
5636 break;
5637 case HLoadString::LoadKind::kBootImageAddress:
5638 break;
5639 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005640 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005641 break;
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01005642 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005643 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005644 break;
5645 case HLoadString::LoadKind::kDexCacheViaMethod:
5646 break;
5647 }
5648 return desired_string_load_kind;
5649}
5650
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005651void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005652 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01005653 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5654 ? LocationSummary::kCallOnMainOnly
5655 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005656 : LocationSummary::kNoCall;
5657 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005658
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005659 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005660 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005661 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005662 locations->SetOut(Location::RegisterLocation(R0));
5663 } else {
5664 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005665 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005666}
5667
5668void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005669 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005670 Location out_loc = locations->Out();
5671 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005672 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005673
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005674 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005675 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005676 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5677 load->GetStringIndex()));
5678 return; // No dex cache slow path.
5679 }
5680 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01005681 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005682 CodeGeneratorARM::PcRelativePatchInfo* labels =
5683 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5684 __ BindTrackedLabel(&labels->movw_label);
5685 __ movw(out, /* placeholder */ 0u);
5686 __ BindTrackedLabel(&labels->movt_label);
5687 __ movt(out, /* placeholder */ 0u);
5688 __ BindTrackedLabel(&labels->add_pc_label);
5689 __ add(out, out, ShifterOperand(PC));
5690 return; // No dex cache slow path.
5691 }
5692 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005693 DCHECK_NE(load->GetAddress(), 0u);
5694 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5695 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5696 return; // No dex cache slow path.
5697 }
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01005698 case HLoadString::LoadKind::kBssEntry: {
5699 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5700 CodeGeneratorARM::PcRelativePatchInfo* labels =
5701 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5702 __ BindTrackedLabel(&labels->movw_label);
5703 __ movw(out, /* placeholder */ 0u);
5704 __ BindTrackedLabel(&labels->movt_label);
5705 __ movt(out, /* placeholder */ 0u);
5706 __ BindTrackedLabel(&labels->add_pc_label);
5707 __ add(out, out, ShifterOperand(PC));
5708 GenerateGcRootFieldLoad(load, out_loc, out, 0);
5709 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5710 codegen_->AddSlowPath(slow_path);
5711 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5712 __ Bind(slow_path->GetExitLabel());
5713 return;
5714 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005715 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005716 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005717 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005718
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005719 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
5720 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5721 InvokeRuntimeCallingConvention calling_convention;
5722 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5723 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5724 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005725}
5726
David Brazdilcb1c0552015-08-04 16:22:25 +01005727static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005728 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005729}
5730
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005731void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5732 LocationSummary* locations =
5733 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5734 locations->SetOut(Location::RequiresRegister());
5735}
5736
5737void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005738 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005739 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5740}
5741
5742void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5743 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5744}
5745
5746void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005747 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005748 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005749}
5750
5751void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5752 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005753 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005754 InvokeRuntimeCallingConvention calling_convention;
5755 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5756}
5757
5758void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005759 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005760 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005761}
5762
Roland Levillainc9285912015-12-18 10:38:42 +00005763static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5764 return kEmitCompilerReadBarrier &&
5765 (kUseBakerReadBarrier ||
5766 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5767 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5768 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5769}
5770
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005771void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005772 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005773 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005774 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00005775 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005776 case TypeCheckKind::kExactCheck:
5777 case TypeCheckKind::kAbstractClassCheck:
5778 case TypeCheckKind::kClassHierarchyCheck:
5779 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005780 call_kind =
5781 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005782 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005783 break;
5784 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005785 case TypeCheckKind::kUnresolvedCheck:
5786 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005787 call_kind = LocationSummary::kCallOnSlowPath;
5788 break;
5789 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005790
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005791 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005792 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005793 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005794 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005795 locations->SetInAt(0, Location::RequiresRegister());
5796 locations->SetInAt(1, Location::RequiresRegister());
5797 // The "out" register is used as a temporary, so it overlaps with the inputs.
5798 // Note that TypeCheckSlowPathARM uses this register too.
5799 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5800 // When read barriers are enabled, we need a temporary register for
5801 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005802 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005803 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005804 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005805}
5806
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005807void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005808 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005809 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005810 Location obj_loc = locations->InAt(0);
5811 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005812 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005813 Location out_loc = locations->Out();
5814 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005815 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005816 locations->GetTemp(0) :
5817 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005818 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005819 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5820 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5821 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005822 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005823 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005824
5825 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005826 // avoid null check if we know obj is not null.
5827 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005828 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005829 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005830
Roland Levillain3b359c72015-11-17 19:35:12 +00005831 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005832 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005833
Roland Levillainc9285912015-12-18 10:38:42 +00005834 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005835 case TypeCheckKind::kExactCheck: {
5836 __ cmp(out, ShifterOperand(cls));
5837 // Classes must be equal for the instanceof to succeed.
5838 __ b(&zero, NE);
5839 __ 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::kAbstractClassCheck: {
5845 // If the class is abstract, we eagerly fetch the super class of the
5846 // object to avoid doing a comparison we know will fail.
5847 Label loop;
5848 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005849 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005850 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005851 // If `out` is null, we use it for the result, and jump to `done`.
5852 __ CompareAndBranchIfZero(out, &done);
5853 __ cmp(out, ShifterOperand(cls));
5854 __ b(&loop, NE);
5855 __ LoadImmediate(out, 1);
5856 if (zero.IsLinked()) {
5857 __ b(&done);
5858 }
5859 break;
5860 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005861
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005862 case TypeCheckKind::kClassHierarchyCheck: {
5863 // Walk over the class hierarchy to find a match.
5864 Label loop, success;
5865 __ Bind(&loop);
5866 __ cmp(out, ShifterOperand(cls));
5867 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005868 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005869 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005870 __ CompareAndBranchIfNonZero(out, &loop);
5871 // If `out` is null, we use it for the result, and jump to `done`.
5872 __ b(&done);
5873 __ Bind(&success);
5874 __ LoadImmediate(out, 1);
5875 if (zero.IsLinked()) {
5876 __ b(&done);
5877 }
5878 break;
5879 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005880
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005881 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005882 // Do an exact check.
5883 Label exact_check;
5884 __ cmp(out, ShifterOperand(cls));
5885 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005886 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005887 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005888 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005889 // If `out` is null, we use it for the result, and jump to `done`.
5890 __ CompareAndBranchIfZero(out, &done);
5891 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5892 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5893 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005894 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005895 __ LoadImmediate(out, 1);
5896 __ b(&done);
5897 break;
5898 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005899
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005900 case TypeCheckKind::kArrayCheck: {
5901 __ cmp(out, ShifterOperand(cls));
5902 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005903 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5904 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005905 codegen_->AddSlowPath(slow_path);
5906 __ b(slow_path->GetEntryLabel(), NE);
5907 __ LoadImmediate(out, 1);
5908 if (zero.IsLinked()) {
5909 __ b(&done);
5910 }
5911 break;
5912 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005913
Calin Juravle98893e12015-10-02 21:05:03 +01005914 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005915 case TypeCheckKind::kInterfaceCheck: {
5916 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005917 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005918 // cases.
5919 //
5920 // We cannot directly call the InstanceofNonTrivial runtime
5921 // entry point without resorting to a type checking slow path
5922 // here (i.e. by calling InvokeRuntime directly), as it would
5923 // require to assign fixed registers for the inputs of this
5924 // HInstanceOf instruction (following the runtime calling
5925 // convention), which might be cluttered by the potential first
5926 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005927 //
5928 // TODO: Introduce a new runtime entry point taking the object
5929 // to test (instead of its class) as argument, and let it deal
5930 // with the read barrier issues. This will let us refactor this
5931 // case of the `switch` code as it was previously (with a direct
5932 // call to the runtime not using a type checking slow path).
5933 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005934 DCHECK(locations->OnlyCallsOnSlowPath());
5935 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5936 /* is_fatal */ false);
5937 codegen_->AddSlowPath(slow_path);
5938 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005939 if (zero.IsLinked()) {
5940 __ b(&done);
5941 }
5942 break;
5943 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005944 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005945
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005946 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005947 __ Bind(&zero);
5948 __ LoadImmediate(out, 0);
5949 }
5950
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005951 if (done.IsLinked()) {
5952 __ Bind(&done);
5953 }
5954
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005955 if (slow_path != nullptr) {
5956 __ Bind(slow_path->GetExitLabel());
5957 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005958}
5959
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005960void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005961 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5962 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5963
Roland Levillain3b359c72015-11-17 19:35:12 +00005964 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5965 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005966 case TypeCheckKind::kExactCheck:
5967 case TypeCheckKind::kAbstractClassCheck:
5968 case TypeCheckKind::kClassHierarchyCheck:
5969 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005970 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5971 LocationSummary::kCallOnSlowPath :
5972 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005973 break;
5974 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005975 case TypeCheckKind::kUnresolvedCheck:
5976 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005977 call_kind = LocationSummary::kCallOnSlowPath;
5978 break;
5979 }
5980
Roland Levillain3b359c72015-11-17 19:35:12 +00005981 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5982 locations->SetInAt(0, Location::RequiresRegister());
5983 locations->SetInAt(1, Location::RequiresRegister());
5984 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5985 locations->AddTemp(Location::RequiresRegister());
5986 // When read barriers are enabled, we need an additional temporary
5987 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005988 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005989 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005990 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005991}
5992
5993void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005994 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005995 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005996 Location obj_loc = locations->InAt(0);
5997 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005998 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005999 Location temp_loc = locations->GetTemp(0);
6000 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006001 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00006002 locations->GetTemp(1) :
6003 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006004 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006005 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6006 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6007 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006008
Roland Levillain3b359c72015-11-17 19:35:12 +00006009 bool is_type_check_slow_path_fatal =
6010 (type_check_kind == TypeCheckKind::kExactCheck ||
6011 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6012 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6013 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6014 !instruction->CanThrowIntoCatchBlock();
Artem Serovf4d6aee2016-07-11 10:41:45 +01006015 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006016 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6017 is_type_check_slow_path_fatal);
6018 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006019
6020 Label done;
6021 // Avoid null check if we know obj is not null.
6022 if (instruction->MustDoNullCheck()) {
6023 __ CompareAndBranchIfZero(obj, &done);
6024 }
6025
Roland Levillain3b359c72015-11-17 19:35:12 +00006026 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006027 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006028
Roland Levillain3b359c72015-11-17 19:35:12 +00006029 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006030 case TypeCheckKind::kExactCheck:
6031 case TypeCheckKind::kArrayCheck: {
6032 __ cmp(temp, ShifterOperand(cls));
6033 // Jump to slow path for throwing the exception or doing a
6034 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006035 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006036 break;
6037 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006038
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006039 case TypeCheckKind::kAbstractClassCheck: {
6040 // If the class is abstract, we eagerly fetch the super class of the
6041 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00006042 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006043 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006044 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006045 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006046
6047 // If the class reference currently in `temp` is not null, jump
6048 // to the `compare_classes` label to compare it with the checked
6049 // class.
6050 __ CompareAndBranchIfNonZero(temp, &compare_classes);
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(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006062 __ cmp(temp, ShifterOperand(cls));
6063 __ b(&loop, NE);
6064 break;
6065 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006066
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006067 case TypeCheckKind::kClassHierarchyCheck: {
6068 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006069 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006070 __ Bind(&loop);
6071 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006072 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006073
Roland Levillain3b359c72015-11-17 19:35:12 +00006074 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006075 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006076
6077 // If the class reference currently in `temp` is not null, jump
6078 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006079 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006080 // Otherwise, jump to the slow path to throw the exception.
6081 //
6082 // But before, move back the object's class into `temp` before
6083 // going into the slow path, as it has been overwritten in the
6084 // meantime.
6085 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006086 GenerateReferenceLoadTwoRegisters(
6087 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006088 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006089 break;
6090 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006091
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006092 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006093 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006094 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006095 __ cmp(temp, ShifterOperand(cls));
6096 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006097
6098 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006099 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006100 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006101
6102 // If the component type is not null (i.e. the object is indeed
6103 // an array), jump to label `check_non_primitive_component_type`
6104 // to further check that this component type is not a primitive
6105 // type.
6106 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
6107 // Otherwise, jump to the slow path to throw the exception.
6108 //
6109 // But before, move back the object's class into `temp` before
6110 // going into the slow path, as it has been overwritten in the
6111 // meantime.
6112 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006113 GenerateReferenceLoadTwoRegisters(
6114 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006115 __ b(type_check_slow_path->GetEntryLabel());
6116
6117 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006118 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006119 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
6120 __ CompareAndBranchIfZero(temp, &done);
6121 // Same comment as above regarding `temp` and the slow path.
6122 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006123 GenerateReferenceLoadTwoRegisters(
6124 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006125 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006126 break;
6127 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006128
Calin Juravle98893e12015-10-02 21:05:03 +01006129 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006130 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006131 // We always go into the type check slow path for the unresolved
6132 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00006133 //
6134 // We cannot directly call the CheckCast runtime entry point
6135 // without resorting to a type checking slow path here (i.e. by
6136 // calling InvokeRuntime directly), as it would require to
6137 // assign fixed registers for the inputs of this HInstanceOf
6138 // instruction (following the runtime calling convention), which
6139 // might be cluttered by the potential first read barrier
6140 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006141 //
6142 // TODO: Introduce a new runtime entry point taking the object
6143 // to test (instead of its class) as argument, and let it deal
6144 // with the read barrier issues. This will let us refactor this
6145 // case of the `switch` code as it was previously (with a direct
6146 // call to the runtime not using a type checking slow path).
6147 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006148 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006149 break;
6150 }
6151 __ Bind(&done);
6152
Roland Levillain3b359c72015-11-17 19:35:12 +00006153 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006154}
6155
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006156void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6157 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006158 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006159 InvokeRuntimeCallingConvention calling_convention;
6160 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6161}
6162
6163void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006164 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6165 instruction,
6166 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006167 if (instruction->IsEnter()) {
6168 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6169 } else {
6170 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6171 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006172}
6173
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006174void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6175void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6176void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006177
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006178void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006179 LocationSummary* locations =
6180 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6181 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6182 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006183 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006184 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006185 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006186 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006187}
6188
6189void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6190 HandleBitwiseOperation(instruction);
6191}
6192
6193void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6194 HandleBitwiseOperation(instruction);
6195}
6196
6197void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6198 HandleBitwiseOperation(instruction);
6199}
6200
Artem Serov7fc63502016-02-09 17:15:29 +00006201
6202void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6203 LocationSummary* locations =
6204 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6205 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6206 || instruction->GetResultType() == Primitive::kPrimLong);
6207
6208 locations->SetInAt(0, Location::RequiresRegister());
6209 locations->SetInAt(1, Location::RequiresRegister());
6210 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6211}
6212
6213void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6214 LocationSummary* locations = instruction->GetLocations();
6215 Location first = locations->InAt(0);
6216 Location second = locations->InAt(1);
6217 Location out = locations->Out();
6218
6219 if (instruction->GetResultType() == Primitive::kPrimInt) {
6220 Register first_reg = first.AsRegister<Register>();
6221 ShifterOperand second_reg(second.AsRegister<Register>());
6222 Register out_reg = out.AsRegister<Register>();
6223
6224 switch (instruction->GetOpKind()) {
6225 case HInstruction::kAnd:
6226 __ bic(out_reg, first_reg, second_reg);
6227 break;
6228 case HInstruction::kOr:
6229 __ orn(out_reg, first_reg, second_reg);
6230 break;
6231 // There is no EON on arm.
6232 case HInstruction::kXor:
6233 default:
6234 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6235 UNREACHABLE();
6236 }
6237 return;
6238
6239 } else {
6240 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6241 Register first_low = first.AsRegisterPairLow<Register>();
6242 Register first_high = first.AsRegisterPairHigh<Register>();
6243 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6244 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6245 Register out_low = out.AsRegisterPairLow<Register>();
6246 Register out_high = out.AsRegisterPairHigh<Register>();
6247
6248 switch (instruction->GetOpKind()) {
6249 case HInstruction::kAnd:
6250 __ bic(out_low, first_low, second_low);
6251 __ bic(out_high, first_high, second_high);
6252 break;
6253 case HInstruction::kOr:
6254 __ orn(out_low, first_low, second_low);
6255 __ orn(out_high, first_high, second_high);
6256 break;
6257 // There is no EON on arm.
6258 case HInstruction::kXor:
6259 default:
6260 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6261 UNREACHABLE();
6262 }
6263 }
6264}
6265
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006266void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6267 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6268 if (value == 0xffffffffu) {
6269 if (out != first) {
6270 __ mov(out, ShifterOperand(first));
6271 }
6272 return;
6273 }
6274 if (value == 0u) {
6275 __ mov(out, ShifterOperand(0));
6276 return;
6277 }
6278 ShifterOperand so;
6279 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6280 __ and_(out, first, so);
6281 } else {
6282 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6283 __ bic(out, first, ShifterOperand(~value));
6284 }
6285}
6286
6287void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6288 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6289 if (value == 0u) {
6290 if (out != first) {
6291 __ mov(out, ShifterOperand(first));
6292 }
6293 return;
6294 }
6295 if (value == 0xffffffffu) {
6296 __ mvn(out, ShifterOperand(0));
6297 return;
6298 }
6299 ShifterOperand so;
6300 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6301 __ orr(out, first, so);
6302 } else {
6303 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6304 __ orn(out, first, ShifterOperand(~value));
6305 }
6306}
6307
6308void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6309 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6310 if (value == 0u) {
6311 if (out != first) {
6312 __ mov(out, ShifterOperand(first));
6313 }
6314 return;
6315 }
6316 __ eor(out, first, ShifterOperand(value));
6317}
6318
Vladimir Marko59751a72016-08-05 14:37:27 +01006319void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6320 Location first,
6321 uint64_t value) {
6322 Register out_low = out.AsRegisterPairLow<Register>();
6323 Register out_high = out.AsRegisterPairHigh<Register>();
6324 Register first_low = first.AsRegisterPairLow<Register>();
6325 Register first_high = first.AsRegisterPairHigh<Register>();
6326 uint32_t value_low = Low32Bits(value);
6327 uint32_t value_high = High32Bits(value);
6328 if (value_low == 0u) {
6329 if (out_low != first_low) {
6330 __ mov(out_low, ShifterOperand(first_low));
6331 }
6332 __ AddConstant(out_high, first_high, value_high);
6333 return;
6334 }
6335 __ AddConstantSetFlags(out_low, first_low, value_low);
6336 ShifterOperand so;
6337 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6338 __ adc(out_high, first_high, so);
6339 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6340 __ sbc(out_high, first_high, so);
6341 } else {
6342 LOG(FATAL) << "Unexpected constant " << value_high;
6343 UNREACHABLE();
6344 }
6345}
6346
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006347void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6348 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006349 Location first = locations->InAt(0);
6350 Location second = locations->InAt(1);
6351 Location out = locations->Out();
6352
6353 if (second.IsConstant()) {
6354 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6355 uint32_t value_low = Low32Bits(value);
6356 if (instruction->GetResultType() == Primitive::kPrimInt) {
6357 Register first_reg = first.AsRegister<Register>();
6358 Register out_reg = out.AsRegister<Register>();
6359 if (instruction->IsAnd()) {
6360 GenerateAndConst(out_reg, first_reg, value_low);
6361 } else if (instruction->IsOr()) {
6362 GenerateOrrConst(out_reg, first_reg, value_low);
6363 } else {
6364 DCHECK(instruction->IsXor());
6365 GenerateEorConst(out_reg, first_reg, value_low);
6366 }
6367 } else {
6368 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6369 uint32_t value_high = High32Bits(value);
6370 Register first_low = first.AsRegisterPairLow<Register>();
6371 Register first_high = first.AsRegisterPairHigh<Register>();
6372 Register out_low = out.AsRegisterPairLow<Register>();
6373 Register out_high = out.AsRegisterPairHigh<Register>();
6374 if (instruction->IsAnd()) {
6375 GenerateAndConst(out_low, first_low, value_low);
6376 GenerateAndConst(out_high, first_high, value_high);
6377 } else if (instruction->IsOr()) {
6378 GenerateOrrConst(out_low, first_low, value_low);
6379 GenerateOrrConst(out_high, first_high, value_high);
6380 } else {
6381 DCHECK(instruction->IsXor());
6382 GenerateEorConst(out_low, first_low, value_low);
6383 GenerateEorConst(out_high, first_high, value_high);
6384 }
6385 }
6386 return;
6387 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006388
6389 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006390 Register first_reg = first.AsRegister<Register>();
6391 ShifterOperand second_reg(second.AsRegister<Register>());
6392 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006393 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006394 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006395 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006396 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006397 } else {
6398 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006399 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006400 }
6401 } else {
6402 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006403 Register first_low = first.AsRegisterPairLow<Register>();
6404 Register first_high = first.AsRegisterPairHigh<Register>();
6405 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6406 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6407 Register out_low = out.AsRegisterPairLow<Register>();
6408 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006409 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006410 __ and_(out_low, first_low, second_low);
6411 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006412 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006413 __ orr(out_low, first_low, second_low);
6414 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006415 } else {
6416 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006417 __ eor(out_low, first_low, second_low);
6418 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006419 }
6420 }
6421}
6422
Roland Levillainc9285912015-12-18 10:38:42 +00006423void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6424 Location out,
6425 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006426 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006427 Register out_reg = out.AsRegister<Register>();
6428 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006429 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006430 if (kUseBakerReadBarrier) {
6431 // Load with fast path based Baker's read barrier.
6432 // /* HeapReference<Object> */ out = *(out + offset)
6433 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006434 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006435 } else {
6436 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006437 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006438 // in the following move operation, as we will need it for the
6439 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006440 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006441 // /* HeapReference<Object> */ out = *(out + offset)
6442 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006443 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006444 }
6445 } else {
6446 // Plain load with no read barrier.
6447 // /* HeapReference<Object> */ out = *(out + offset)
6448 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6449 __ MaybeUnpoisonHeapReference(out_reg);
6450 }
6451}
6452
6453void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6454 Location out,
6455 Location obj,
6456 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006457 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006458 Register out_reg = out.AsRegister<Register>();
6459 Register obj_reg = obj.AsRegister<Register>();
6460 if (kEmitCompilerReadBarrier) {
6461 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006462 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006463 // Load with fast path based Baker's read barrier.
6464 // /* HeapReference<Object> */ out = *(obj + offset)
6465 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006466 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006467 } else {
6468 // Load with slow path based read barrier.
6469 // /* HeapReference<Object> */ out = *(obj + offset)
6470 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6471 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6472 }
6473 } else {
6474 // Plain load with no read barrier.
6475 // /* HeapReference<Object> */ out = *(obj + offset)
6476 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6477 __ MaybeUnpoisonHeapReference(out_reg);
6478 }
6479}
6480
6481void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6482 Location root,
6483 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006484 uint32_t offset,
6485 bool requires_read_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006486 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006487 if (requires_read_barrier) {
6488 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006489 if (kUseBakerReadBarrier) {
6490 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6491 // Baker's read barrier are used:
6492 //
6493 // root = obj.field;
6494 // if (Thread::Current()->GetIsGcMarking()) {
6495 // root = ReadBarrier::Mark(root)
6496 // }
6497
6498 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6499 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6500 static_assert(
6501 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6502 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6503 "have different sizes.");
6504 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6505 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6506 "have different sizes.");
6507
Vladimir Marko953437b2016-08-24 08:30:46 +00006508 // Slow path marking the GC root `root`.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006509 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006510 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006511 codegen_->AddSlowPath(slow_path);
6512
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006513 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006514 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006515 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006516 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6517 __ Bind(slow_path->GetExitLabel());
6518 } else {
6519 // GC root loaded through a slow path for read barriers other
6520 // than Baker's.
6521 // /* GcRoot<mirror::Object>* */ root = obj + offset
6522 __ AddConstant(root_reg, obj, offset);
6523 // /* mirror::Object* */ root = root->Read()
6524 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6525 }
6526 } else {
6527 // Plain GC root load with no read barrier.
6528 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6529 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6530 // Note that GC roots are not affected by heap poisoning, thus we
6531 // do not have to unpoison `root_reg` here.
6532 }
6533}
6534
6535void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6536 Location ref,
6537 Register obj,
6538 uint32_t offset,
6539 Location temp,
6540 bool needs_null_check) {
6541 DCHECK(kEmitCompilerReadBarrier);
6542 DCHECK(kUseBakerReadBarrier);
6543
6544 // /* HeapReference<Object> */ ref = *(obj + offset)
6545 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006546 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006547 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006548 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006549}
6550
6551void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6552 Location ref,
6553 Register obj,
6554 uint32_t data_offset,
6555 Location index,
6556 Location temp,
6557 bool needs_null_check) {
6558 DCHECK(kEmitCompilerReadBarrier);
6559 DCHECK(kUseBakerReadBarrier);
6560
Roland Levillainbfea3352016-06-23 13:48:47 +01006561 static_assert(
6562 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6563 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006564 // /* HeapReference<Object> */ ref =
6565 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006566 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006567 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006568 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006569}
6570
6571void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6572 Location ref,
6573 Register obj,
6574 uint32_t offset,
6575 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006576 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006577 Location temp,
6578 bool needs_null_check) {
6579 DCHECK(kEmitCompilerReadBarrier);
6580 DCHECK(kUseBakerReadBarrier);
6581
6582 // In slow path based read barriers, the read barrier call is
6583 // inserted after the original load. However, in fast path based
6584 // Baker's read barriers, we need to perform the load of
6585 // mirror::Object::monitor_ *before* the original reference load.
6586 // This load-load ordering is required by the read barrier.
6587 // The fast path/slow path (for Baker's algorithm) should look like:
6588 //
6589 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6590 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6591 // HeapReference<Object> ref = *src; // Original reference load.
6592 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6593 // if (is_gray) {
6594 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6595 // }
6596 //
6597 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006598 // slightly more complex as it performs additional checks that we do
6599 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006600
6601 Register ref_reg = ref.AsRegister<Register>();
6602 Register temp_reg = temp.AsRegister<Register>();
6603 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6604
6605 // /* int32_t */ monitor = obj->monitor_
6606 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6607 if (needs_null_check) {
6608 MaybeRecordImplicitNullCheck(instruction);
6609 }
6610 // /* LockWord */ lock_word = LockWord(monitor)
6611 static_assert(sizeof(LockWord) == sizeof(int32_t),
6612 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006613
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006614 // Introduce a dependency on the lock_word including the rb_state,
6615 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006616 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006617 // `obj` is unchanged by this operation, but its value now depends
6618 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006619 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006620
6621 // The actual reference load.
6622 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006623 // Load types involving an "index": ArrayGet and
6624 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6625 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006626 if (index.IsConstant()) {
6627 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006628 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006629 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6630 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006631 // Handle the special case of the
6632 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6633 // a register pair as index ("long offset"), of which only the low
6634 // part contains data.
6635 Register index_reg = index.IsRegisterPair()
6636 ? index.AsRegisterPairLow<Register>()
6637 : index.AsRegister<Register>();
6638 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006639 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6640 }
6641 } else {
6642 // /* HeapReference<Object> */ ref = *(obj + offset)
6643 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6644 }
6645
6646 // Object* ref = ref_addr->AsMirrorPtr()
6647 __ MaybeUnpoisonHeapReference(ref_reg);
6648
Vladimir Marko953437b2016-08-24 08:30:46 +00006649 // Slow path marking the object `ref` when it is gray.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006650 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006651 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006652 AddSlowPath(slow_path);
6653
6654 // if (rb_state == ReadBarrier::gray_ptr_)
6655 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006656 // Given the numeric representation, it's enough to check the low bit of the
6657 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6658 // which can be a 16-bit instruction unlike the TST immediate.
6659 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6660 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6661 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6662 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6663 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006664 __ Bind(slow_path->GetExitLabel());
6665}
6666
6667void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6668 Location out,
6669 Location ref,
6670 Location obj,
6671 uint32_t offset,
6672 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006673 DCHECK(kEmitCompilerReadBarrier);
6674
Roland Levillainc9285912015-12-18 10:38:42 +00006675 // Insert a slow path based read barrier *after* the reference load.
6676 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006677 // If heap poisoning is enabled, the unpoisoning of the loaded
6678 // reference will be carried out by the runtime within the slow
6679 // path.
6680 //
6681 // Note that `ref` currently does not get unpoisoned (when heap
6682 // poisoning is enabled), which is alright as the `ref` argument is
6683 // not used by the artReadBarrierSlow entry point.
6684 //
6685 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006686 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00006687 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6688 AddSlowPath(slow_path);
6689
Roland Levillain3b359c72015-11-17 19:35:12 +00006690 __ b(slow_path->GetEntryLabel());
6691 __ Bind(slow_path->GetExitLabel());
6692}
6693
Roland Levillainc9285912015-12-18 10:38:42 +00006694void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6695 Location out,
6696 Location ref,
6697 Location obj,
6698 uint32_t offset,
6699 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006700 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006701 // Baker's read barriers shall be handled by the fast path
6702 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6703 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006704 // If heap poisoning is enabled, unpoisoning will be taken care of
6705 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006706 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006707 } else if (kPoisonHeapReferences) {
6708 __ UnpoisonHeapReference(out.AsRegister<Register>());
6709 }
6710}
6711
Roland Levillainc9285912015-12-18 10:38:42 +00006712void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6713 Location out,
6714 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006715 DCHECK(kEmitCompilerReadBarrier);
6716
Roland Levillainc9285912015-12-18 10:38:42 +00006717 // Insert a slow path based read barrier *after* the GC root load.
6718 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006719 // Note that GC roots are not affected by heap poisoning, so we do
6720 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006721 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006722 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6723 AddSlowPath(slow_path);
6724
Roland Levillain3b359c72015-11-17 19:35:12 +00006725 __ b(slow_path->GetEntryLabel());
6726 __ Bind(slow_path->GetExitLabel());
6727}
6728
Vladimir Markodc151b22015-10-15 18:02:30 +01006729HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6730 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01006731 HInvokeStaticOrDirect* invoke) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006732 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6733 // We disable pc-relative load when there is an irreducible loop, as the optimization
6734 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006735 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6736 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006737 if (GetGraph()->HasIrreducibleLoops() &&
6738 (dispatch_info.method_load_kind ==
6739 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6740 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6741 }
6742
6743 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006744 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01006745 if (&outer_dex_file != invoke->GetTargetMethod().dex_file) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006746 // Calls across dex files are more likely to exceed the available BL range,
6747 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6748 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6749 (desired_dispatch_info.method_load_kind ==
6750 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6751 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6752 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6753 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006754 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006755 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006756 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006757 0u
6758 };
6759 }
6760 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006761 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006762}
6763
Vladimir Markob4536b72015-11-24 13:45:23 +00006764Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6765 Register temp) {
6766 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6767 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6768 if (!invoke->GetLocations()->Intrinsified()) {
6769 return location.AsRegister<Register>();
6770 }
6771 // For intrinsics we allow any location, so it may be on the stack.
6772 if (!location.IsRegister()) {
6773 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6774 return temp;
6775 }
6776 // For register locations, check if the register was saved. If so, get it from the stack.
6777 // Note: There is a chance that the register was saved but not overwritten, so we could
6778 // save one load. However, since this is just an intrinsic slow path we prefer this
6779 // simple and more robust approach rather that trying to determine if that's the case.
6780 SlowPathCode* slow_path = GetCurrentSlowPath();
6781 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6782 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6783 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6784 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6785 return temp;
6786 }
6787 return location.AsRegister<Register>();
6788}
6789
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006790void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006791 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006792 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006793 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6794 // LR = code address from literal pool with link-time patch.
6795 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006796 break;
6797 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6798 // LR = invoke->GetDirectCodePtr();
6799 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006800 break;
6801 default:
6802 break;
6803 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006804
Vladimir Marko58155012015-08-19 12:49:41 +00006805 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6806 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006807 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
6808 uint32_t offset =
6809 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00006810 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006811 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00006812 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01006813 }
Vladimir Marko58155012015-08-19 12:49:41 +00006814 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006815 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006816 break;
6817 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6818 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6819 break;
6820 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6821 __ LoadLiteral(temp.AsRegister<Register>(),
6822 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6823 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006824 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6825 HArmDexCacheArraysBase* base =
6826 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6827 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6828 temp.AsRegister<Register>());
6829 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6830 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6831 break;
6832 }
Vladimir Marko58155012015-08-19 12:49:41 +00006833 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006834 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006835 Register method_reg;
6836 Register reg = temp.AsRegister<Register>();
6837 if (current_method.IsRegister()) {
6838 method_reg = current_method.AsRegister<Register>();
6839 } else {
6840 DCHECK(invoke->GetLocations()->Intrinsified());
6841 DCHECK(!current_method.IsValid());
6842 method_reg = reg;
6843 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6844 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006845 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6846 __ LoadFromOffset(kLoadWord,
6847 reg,
6848 method_reg,
6849 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006850 // temp = temp[index_in_cache];
6851 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6852 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006853 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6854 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006855 }
Vladimir Marko58155012015-08-19 12:49:41 +00006856 }
6857
6858 switch (invoke->GetCodePtrLocation()) {
6859 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6860 __ bl(GetFrameEntryLabel());
6861 break;
6862 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01006863 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
6864 invoke->GetTargetMethod().dex_method_index);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006865 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006866 // Arbitrarily branch to the BL itself, override at link time.
6867 __ bl(&relative_call_patches_.back().label);
6868 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006869 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6870 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6871 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006872 // LR()
6873 __ blx(LR);
6874 break;
6875 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6876 // LR = callee_method->entry_point_from_quick_compiled_code_
6877 __ LoadFromOffset(
6878 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07006879 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006880 // LR()
6881 __ blx(LR);
6882 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006883 }
6884
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006885 DCHECK(!IsLeafMethod());
6886}
6887
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006888void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6889 Register temp = temp_location.AsRegister<Register>();
6890 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6891 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006892
6893 // Use the calling convention instead of the location of the receiver, as
6894 // intrinsics may have put the receiver in a different register. In the intrinsics
6895 // slow path, the arguments have been moved to the right place, so here we are
6896 // guaranteed that the receiver is the first register of the calling convention.
6897 InvokeDexCallingConvention calling_convention;
6898 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006899 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006900 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006901 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006902 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006903 // Instead of simply (possibly) unpoisoning `temp` here, we should
6904 // emit a read barrier for the previous class reference load.
6905 // However this is not required in practice, as this is an
6906 // intermediate/temporary reference and because the current
6907 // concurrent copying collector keeps the from-space memory
6908 // intact/accessible until the end of the marking phase (the
6909 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006910 __ MaybeUnpoisonHeapReference(temp);
6911 // temp = temp->GetMethodAt(method_offset);
6912 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006913 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006914 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6915 // LR = temp->GetEntryPoint();
6916 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6917 // LR();
6918 __ blx(LR);
6919}
6920
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006921CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6922 const DexFile& dex_file, uint32_t string_index) {
6923 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6924}
6925
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006926CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6927 const DexFile& dex_file, uint32_t type_index) {
6928 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6929}
6930
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006931CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6932 const DexFile& dex_file, uint32_t element_offset) {
6933 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6934}
6935
6936CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6937 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6938 patches->emplace_back(dex_file, offset_or_index);
6939 return &patches->back();
6940}
6941
6942Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6943 uint32_t string_index) {
6944 return boot_image_string_patches_.GetOrCreate(
6945 StringReference(&dex_file, string_index),
6946 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6947}
6948
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006949Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6950 uint32_t type_index) {
6951 return boot_image_type_patches_.GetOrCreate(
6952 TypeReference(&dex_file, type_index),
6953 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6954}
6955
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006956Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6957 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6958 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6959 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6960}
6961
6962Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6963 return DeduplicateUint32Literal(address, &uint32_literals_);
6964}
6965
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01006966template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
6967inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
6968 const ArenaDeque<PcRelativePatchInfo>& infos,
6969 ArenaVector<LinkerPatch>* linker_patches) {
6970 for (const PcRelativePatchInfo& info : infos) {
6971 const DexFile& dex_file = info.target_dex_file;
6972 size_t offset_or_index = info.offset_or_index;
6973 DCHECK(info.add_pc_label.IsBound());
6974 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6975 // Add MOVW patch.
6976 DCHECK(info.movw_label.IsBound());
6977 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6978 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
6979 // Add MOVT patch.
6980 DCHECK(info.movt_label.IsBound());
6981 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6982 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
6983 }
6984}
6985
Vladimir Marko58155012015-08-19 12:49:41 +00006986void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6987 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006988 size_t size =
6989 method_patches_.size() +
6990 call_patches_.size() +
6991 relative_call_patches_.size() +
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01006992 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006993 boot_image_string_patches_.size() +
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01006994 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006995 boot_image_type_patches_.size() +
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01006996 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006997 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006998 linker_patches->reserve(size);
6999 for (const auto& entry : method_patches_) {
7000 const MethodReference& target_method = entry.first;
7001 Literal* literal = entry.second;
7002 DCHECK(literal->GetLabel()->IsBound());
7003 uint32_t literal_offset = literal->GetLabel()->Position();
7004 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
7005 target_method.dex_file,
7006 target_method.dex_method_index));
7007 }
7008 for (const auto& entry : call_patches_) {
7009 const MethodReference& target_method = entry.first;
7010 Literal* literal = entry.second;
7011 DCHECK(literal->GetLabel()->IsBound());
7012 uint32_t literal_offset = literal->GetLabel()->Position();
7013 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
7014 target_method.dex_file,
7015 target_method.dex_method_index));
7016 }
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01007017 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko58155012015-08-19 12:49:41 +00007018 uint32_t literal_offset = info.label.Position();
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01007019 linker_patches->push_back(
7020 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00007021 }
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01007022 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7023 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007024 for (const auto& entry : boot_image_string_patches_) {
7025 const StringReference& target_string = entry.first;
7026 Literal* literal = entry.second;
7027 DCHECK(literal->GetLabel()->IsBound());
7028 uint32_t literal_offset = literal->GetLabel()->Position();
7029 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7030 target_string.dex_file,
7031 target_string.string_index));
7032 }
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01007033 if (!GetCompilerOptions().IsBootImage()) {
7034 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7035 linker_patches);
7036 } else {
7037 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7038 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007039 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007040 for (const auto& entry : boot_image_type_patches_) {
7041 const TypeReference& target_type = entry.first;
7042 Literal* literal = entry.second;
7043 DCHECK(literal->GetLabel()->IsBound());
7044 uint32_t literal_offset = literal->GetLabel()->Position();
7045 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7046 target_type.dex_file,
7047 target_type.type_index));
7048 }
Vladimir Marko63dccbbe2016-09-21 13:51:10 +01007049 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7050 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007051 for (const auto& entry : boot_image_address_patches_) {
7052 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7053 Literal* literal = entry.second;
7054 DCHECK(literal->GetLabel()->IsBound());
7055 uint32_t literal_offset = literal->GetLabel()->Position();
7056 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7057 }
7058}
7059
7060Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7061 return map->GetOrCreate(
7062 value,
7063 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007064}
7065
7066Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7067 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007068 return map->GetOrCreate(
7069 target_method,
7070 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007071}
7072
7073Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
7074 return DeduplicateMethodLiteral(target_method, &method_patches_);
7075}
7076
7077Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
7078 return DeduplicateMethodLiteral(target_method, &call_patches_);
7079}
7080
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007081void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7082 LocationSummary* locations =
7083 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7084 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7085 Location::RequiresRegister());
7086 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7087 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7088 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7089}
7090
7091void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7092 LocationSummary* locations = instr->GetLocations();
7093 Register res = locations->Out().AsRegister<Register>();
7094 Register accumulator =
7095 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7096 Register mul_left =
7097 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7098 Register mul_right =
7099 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7100
7101 if (instr->GetOpKind() == HInstruction::kAdd) {
7102 __ mla(res, mul_left, mul_right, accumulator);
7103 } else {
7104 __ mls(res, mul_left, mul_right, accumulator);
7105 }
7106}
7107
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007108void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007109 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007110 LOG(FATAL) << "Unreachable";
7111}
7112
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007113void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007114 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007115 LOG(FATAL) << "Unreachable";
7116}
7117
Mark Mendellfe57faa2015-09-18 09:26:15 -04007118// Simple implementation of packed switch - generate cascaded compare/jumps.
7119void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7120 LocationSummary* locations =
7121 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7122 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007123 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007124 codegen_->GetAssembler()->IsThumb()) {
7125 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7126 if (switch_instr->GetStartValue() != 0) {
7127 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7128 }
7129 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007130}
7131
7132void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7133 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007134 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007135 LocationSummary* locations = switch_instr->GetLocations();
7136 Register value_reg = locations->InAt(0).AsRegister<Register>();
7137 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7138
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007139 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007140 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007141 Register temp_reg = IP;
7142 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7143 // the immediate, because IP is used as the destination register. For the other
7144 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7145 // and they can be encoded in the instruction without making use of IP register.
7146 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7147
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007148 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007149 // Jump to successors[0] if value == lower_bound.
7150 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7151 int32_t last_index = 0;
7152 for (; num_entries - last_index > 2; last_index += 2) {
7153 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7154 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7155 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7156 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7157 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7158 }
7159 if (num_entries - last_index == 2) {
7160 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007161 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007162 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007163 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007164
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007165 // And the default for any other value.
7166 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7167 __ b(codegen_->GetLabelOf(default_block));
7168 }
7169 } else {
7170 // Create a table lookup.
7171 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7172
7173 // Materialize a pointer to the switch table
7174 std::vector<Label*> labels(num_entries);
7175 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7176 for (uint32_t i = 0; i < num_entries; i++) {
7177 labels[i] = codegen_->GetLabelOf(successors[i]);
7178 }
7179 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7180
7181 // Remove the bias.
7182 Register key_reg;
7183 if (lower_bound != 0) {
7184 key_reg = locations->GetTemp(1).AsRegister<Register>();
7185 __ AddConstant(key_reg, value_reg, -lower_bound);
7186 } else {
7187 key_reg = value_reg;
7188 }
7189
7190 // Check whether the value is in the table, jump to default block if not.
7191 __ CmpConstant(key_reg, num_entries - 1);
7192 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7193
7194 // Load the displacement from the table.
7195 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7196
7197 // Dispatch is a direct add to the PC (for Thumb2).
7198 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007199 }
7200}
7201
Vladimir Markob4536b72015-11-24 13:45:23 +00007202void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7203 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7204 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007205}
7206
7207void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7208 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007209 CodeGeneratorARM::PcRelativePatchInfo* labels =
7210 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007211 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007212 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007213 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007214 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007215 __ BindTrackedLabel(&labels->add_pc_label);
7216 __ add(base_reg, base_reg, ShifterOperand(PC));
7217}
7218
Andreas Gampe85b62f22015-09-09 13:15:38 -07007219void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7220 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007221 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007222 return;
7223 }
7224
7225 DCHECK_NE(type, Primitive::kPrimVoid);
7226
7227 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7228 if (return_loc.Equals(trg)) {
7229 return;
7230 }
7231
7232 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7233 // with the last branch.
7234 if (type == Primitive::kPrimLong) {
7235 HParallelMove parallel_move(GetGraph()->GetArena());
7236 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7237 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7238 GetMoveResolver()->EmitNativeCode(&parallel_move);
7239 } else if (type == Primitive::kPrimDouble) {
7240 HParallelMove parallel_move(GetGraph()->GetArena());
7241 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7242 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7243 GetMoveResolver()->EmitNativeCode(&parallel_move);
7244 } else {
7245 // Let the parallel move resolver take care of all of this.
7246 HParallelMove parallel_move(GetGraph()->GetArena());
7247 parallel_move.AddMove(return_loc, trg, type, nullptr);
7248 GetMoveResolver()->EmitNativeCode(&parallel_move);
7249 }
7250}
7251
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007252void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7253 LocationSummary* locations =
7254 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7255 locations->SetInAt(0, Location::RequiresRegister());
7256 locations->SetOut(Location::RequiresRegister());
7257}
7258
7259void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7260 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007261 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007262 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007263 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007264 __ LoadFromOffset(kLoadWord,
7265 locations->Out().AsRegister<Register>(),
7266 locations->InAt(0).AsRegister<Register>(),
7267 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007268 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007269 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007270 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007271 __ LoadFromOffset(kLoadWord,
7272 locations->Out().AsRegister<Register>(),
7273 locations->InAt(0).AsRegister<Register>(),
7274 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7275 __ LoadFromOffset(kLoadWord,
7276 locations->Out().AsRegister<Register>(),
7277 locations->Out().AsRegister<Register>(),
7278 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007279 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007280}
7281
Roland Levillain4d027112015-07-01 15:41:14 +01007282#undef __
7283#undef QUICK_ENTRY_POINT
7284
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007285} // namespace arm
7286} // namespace art