blob: 4c96867efbdd0a4b34fe60e037c175dd42f34457 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
David Brazdil58282f42016-01-14 12:45:10 +000050static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000051static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070052 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr SRegister kFpuCalleeSaves[] =
54 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000056// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
Vladimir Markof3e0ee22015-12-17 15:23:13 +000060static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070061
Roland Levillain7cbd27f2016-08-11 23:53:33 +010062// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
63#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070064#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Artem Serovf4d6aee2016-07-11 10:41:45 +010066static constexpr int kRegListThreshold = 4;
67
Artem Serovd300d8f2016-07-15 14:00:56 +010068// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
69// for each live D registers they treat two corresponding S registers as live ones.
70//
71// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
72// from a list of contiguous S registers a list of contiguous D registers (processing first/last
73// S registers corner cases) and save/restore this new list treating them as D registers.
74// - decreasing code size
75// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
76// restored and then used in regular non SlowPath code as D register.
77//
78// For the following example (v means the S register is live):
79// D names: | D0 | D1 | D2 | D4 | ...
80// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
81// Live? | | v | v | v | v | v | v | | ...
82//
83// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
84// as D registers.
85static size_t SaveContiguousSRegisterList(size_t first,
86 size_t last,
87 CodeGenerator* codegen,
88 size_t stack_offset) {
89 DCHECK_LE(first, last);
90 if ((first == last) && (first == 0)) {
91 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first);
92 return stack_offset;
93 }
94 if (first % 2 == 1) {
95 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first++);
96 }
97
98 bool save_last = false;
99 if (last % 2 == 0) {
100 save_last = true;
101 --last;
102 }
103
104 if (first < last) {
105 DRegister d_reg = static_cast<DRegister>(first / 2);
106 DCHECK_EQ((last - first + 1) % 2, 0u);
107 size_t number_of_d_regs = (last - first + 1) / 2;
108
109 if (number_of_d_regs == 1) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100110 __ StoreDToOffset(d_reg, SP, stack_offset);
Artem Serovd300d8f2016-07-15 14:00:56 +0100111 } else if (number_of_d_regs > 1) {
112 __ add(IP, SP, ShifterOperand(stack_offset));
113 __ vstmiad(IP, d_reg, number_of_d_regs);
114 }
115 stack_offset += number_of_d_regs * kArmWordSize * 2;
116 }
117
118 if (save_last) {
119 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, last + 1);
120 }
121
122 return stack_offset;
123}
124
125static size_t RestoreContiguousSRegisterList(size_t first,
126 size_t last,
127 CodeGenerator* codegen,
128 size_t stack_offset) {
129 DCHECK_LE(first, last);
130 if ((first == last) && (first == 0)) {
131 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first);
132 return stack_offset;
133 }
134 if (first % 2 == 1) {
135 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first++);
136 }
137
138 bool restore_last = false;
139 if (last % 2 == 0) {
140 restore_last = true;
141 --last;
142 }
143
144 if (first < last) {
145 DRegister d_reg = static_cast<DRegister>(first / 2);
146 DCHECK_EQ((last - first + 1) % 2, 0u);
147 size_t number_of_d_regs = (last - first + 1) / 2;
148 if (number_of_d_regs == 1) {
149 __ LoadDFromOffset(d_reg, SP, stack_offset);
150 } else if (number_of_d_regs > 1) {
151 __ add(IP, SP, ShifterOperand(stack_offset));
152 __ vldmiad(IP, d_reg, number_of_d_regs);
153 }
154 stack_offset += number_of_d_regs * kArmWordSize * 2;
155 }
156
157 if (restore_last) {
158 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, last + 1);
159 }
160
161 return stack_offset;
162}
163
Artem Serovf4d6aee2016-07-11 10:41:45 +0100164void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
165 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
166 size_t orig_offset = stack_offset;
167
168 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
169 for (uint32_t i : LowToHighBits(core_spills)) {
170 // If the register holds an object, update the stack mask.
171 if (locations->RegisterContainsObject(i)) {
172 locations->SetStackBit(stack_offset / kVRegSize);
173 }
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_core_stack_offsets_[i] = stack_offset;
177 stack_offset += kArmWordSize;
178 }
179
180 int reg_num = POPCOUNT(core_spills);
181 if (reg_num != 0) {
182 if (reg_num > kRegListThreshold) {
183 __ StoreList(RegList(core_spills), orig_offset);
184 } else {
185 stack_offset = orig_offset;
186 for (uint32_t i : LowToHighBits(core_spills)) {
187 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
188 }
189 }
190 }
191
Artem Serovd300d8f2016-07-15 14:00:56 +0100192 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
193 orig_offset = stack_offset;
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 for (uint32_t i : LowToHighBits(fp_spills)) {
Artem Serovf4d6aee2016-07-11 10:41:45 +0100195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serovd300d8f2016-07-15 14:00:56 +0100197 stack_offset += kArmWordSize;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100198 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100199
200 stack_offset = orig_offset;
201 while (fp_spills != 0u) {
202 uint32_t begin = CTZ(fp_spills);
203 uint32_t tmp = fp_spills + (1u << begin);
204 fp_spills &= tmp; // Clear the contiguous range of 1s.
205 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
206 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
207 }
208 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100209}
210
211void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
212 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
213 size_t orig_offset = stack_offset;
214
215 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
216 for (uint32_t i : LowToHighBits(core_spills)) {
217 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
218 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
219 stack_offset += kArmWordSize;
220 }
221
222 int reg_num = POPCOUNT(core_spills);
223 if (reg_num != 0) {
224 if (reg_num > kRegListThreshold) {
225 __ LoadList(RegList(core_spills), orig_offset);
226 } else {
227 stack_offset = orig_offset;
228 for (uint32_t i : LowToHighBits(core_spills)) {
229 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
230 }
231 }
232 }
233
Artem Serovd300d8f2016-07-15 14:00:56 +0100234 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
235 while (fp_spills != 0u) {
236 uint32_t begin = CTZ(fp_spills);
237 uint32_t tmp = fp_spills + (1u << begin);
238 fp_spills &= tmp; // Clear the contiguous range of 1s.
239 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
240 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
Artem Serovf4d6aee2016-07-11 10:41:45 +0100241 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100242 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100243}
244
245class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100246 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100247 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100248
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100250 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100251 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000252 if (instruction_->CanThrowIntoCatchBlock()) {
253 // Live registers will be restored in the catch block if caught.
254 SaveLiveRegisters(codegen, instruction_->GetLocations());
255 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100256 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
257 instruction_,
258 instruction_->GetDexPc(),
259 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000260 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100261 }
262
Alexandre Rames8158f282015-08-07 10:26:17 +0100263 bool IsFatal() const OVERRIDE { return true; }
264
Alexandre Rames9931f312015-06-19 14:47:01 +0100265 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
266
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100267 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100268 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
269};
270
Artem Serovf4d6aee2016-07-11 10:41:45 +0100271class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000272 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100273 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000274
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000276 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
277 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100278 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000279 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000280 }
281
Alexandre Rames8158f282015-08-07 10:26:17 +0100282 bool IsFatal() const OVERRIDE { return true; }
283
Alexandre Rames9931f312015-06-19 14:47:01 +0100284 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
285
Calin Juravled0d48522014-11-04 16:40:20 +0000286 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000287 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
288};
289
Artem Serovf4d6aee2016-07-11 10:41:45 +0100290class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000291 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100293 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000294
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100296 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000297 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100298 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000299 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100300 if (successor_ == nullptr) {
301 __ b(GetReturnLabel());
302 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100303 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100304 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000305 }
306
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100307 Label* GetReturnLabel() {
308 DCHECK(successor_ == nullptr);
309 return &return_label_;
310 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000311
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100312 HBasicBlock* GetSuccessor() const {
313 return successor_;
314 }
315
Alexandre Rames9931f312015-06-19 14:47:01 +0100316 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
317
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000318 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100319 // If not null, the block to branch to after the suspend check.
320 HBasicBlock* const successor_;
321
322 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000323 Label return_label_;
324
325 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
326};
327
Artem Serovf4d6aee2016-07-11 10:41:45 +0100328class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100329 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100330 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100331 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100332
Alexandre Rames67555f72014-11-18 10:55:16 +0000333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100334 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100335 LocationSummary* locations = instruction_->GetLocations();
336
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100337 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000338 if (instruction_->CanThrowIntoCatchBlock()) {
339 // Live registers will be restored in the catch block if caught.
340 SaveLiveRegisters(codegen, instruction_->GetLocations());
341 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000342 // We're moving two locations to locations that could overlap, so we need a parallel
343 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100344 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000345 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100346 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000347 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100348 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100349 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100350 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
351 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100352 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
353 ? kQuickThrowStringBounds
354 : kQuickThrowArrayBounds;
355 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100356 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000357 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100358 }
359
Alexandre Rames8158f282015-08-07 10:26:17 +0100360 bool IsFatal() const OVERRIDE { return true; }
361
Alexandre Rames9931f312015-06-19 14:47:01 +0100362 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
363
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100364 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100365 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
366};
367
Artem Serovf4d6aee2016-07-11 10:41:45 +0100368class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100369 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000370 LoadClassSlowPathARM(HLoadClass* cls,
371 HInstruction* at,
372 uint32_t dex_pc,
373 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000374 : SlowPathCodeARM(at), cls_(cls), 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 {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000379 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000380
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;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000386 dex::TypeIndex type_index = cls_->GetTypeIndex();
387 __ LoadImmediate(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100388 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
389 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000390 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000391 if (do_clinit_) {
392 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
393 } else {
394 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
395 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000396
397 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000398 Location out = locations->Out();
399 if (out.IsValid()) {
400 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000401 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
402 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000403 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000404 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
405 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
406 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
407 DCHECK(out.IsValid());
408 // TODO: Change art_quick_initialize_type/art_quick_initialize_static_storage to
409 // kSaveEverything and use a temporary for the .bss entry address in the fast path,
410 // so that we can avoid another calculation here.
411 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000412 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000413 __ BindTrackedLabel(&labels->movw_label);
414 __ movw(IP, /* placeholder */ 0u);
415 __ BindTrackedLabel(&labels->movt_label);
416 __ movt(IP, /* placeholder */ 0u);
417 __ BindTrackedLabel(&labels->add_pc_label);
418 __ add(IP, IP, ShifterOperand(PC));
419 __ str(locations->Out().AsRegister<Register>(), Address(IP));
420 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100421 __ b(GetExitLabel());
422 }
423
Alexandre Rames9931f312015-06-19 14:47:01 +0100424 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
425
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100426 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000427 // The class this slow path will load.
428 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100429
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000430 // The dex PC of `at_`.
431 const uint32_t dex_pc_;
432
433 // Whether to initialize the class.
434 const bool do_clinit_;
435
436 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100437};
438
Vladimir Markoaad75c62016-10-03 08:46:48 +0000439class LoadStringSlowPathARM : public SlowPathCodeARM {
440 public:
441 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
442
443 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
444 LocationSummary* locations = instruction_->GetLocations();
445 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100446 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000447 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100448 Register out = locations->Out().AsRegister<Register>();
449 Register temp = locations->GetTemp(0).AsRegister<Register>();
450 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000451
452 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
453 __ Bind(GetEntryLabel());
454 SaveLiveRegisters(codegen, locations);
455
456 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100457 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
458 // the kSaveEverything call (or use `out` for the address after non-kSaveEverything call).
459 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
460 Register entry_address = temp_is_r0 ? out : temp;
461 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
462 if (call_saves_everything_except_r0 && temp_is_r0) {
463 __ mov(entry_address, ShifterOperand(temp));
464 }
465
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000466 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000467 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
468 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100469
470 // Store the resolved String to the .bss entry.
471 if (call_saves_everything_except_r0) {
472 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
473 __ str(R0, Address(entry_address));
474 } else {
475 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
476 CodeGeneratorARM::PcRelativePatchInfo* labels =
477 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
478 __ BindTrackedLabel(&labels->movw_label);
479 __ movw(entry_address, /* placeholder */ 0u);
480 __ BindTrackedLabel(&labels->movt_label);
481 __ movt(entry_address, /* placeholder */ 0u);
482 __ BindTrackedLabel(&labels->add_pc_label);
483 __ add(entry_address, entry_address, ShifterOperand(PC));
484 __ str(R0, Address(entry_address));
485 }
486
Vladimir Markoaad75c62016-10-03 08:46:48 +0000487 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000488 RestoreLiveRegisters(codegen, locations);
489
Vladimir Markoaad75c62016-10-03 08:46:48 +0000490 __ b(GetExitLabel());
491 }
492
493 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
494
495 private:
496 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
497};
498
Artem Serovf4d6aee2016-07-11 10:41:45 +0100499class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000500 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000501 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100502 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000503
Alexandre Rames67555f72014-11-18 10:55:16 +0000504 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000505 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000506 DCHECK(instruction_->IsCheckCast()
507 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000508
509 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
510 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000511
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000512 if (!is_fatal_) {
513 SaveLiveRegisters(codegen, locations);
514 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000515
516 // We're moving two locations to locations that could overlap, so we need a parallel
517 // move resolver.
518 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800519 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800520 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
521 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800522 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800523 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
524 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000525 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100526 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100527 instruction_,
528 instruction_->GetDexPc(),
529 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800530 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000531 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
532 } else {
533 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800534 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
535 instruction_,
536 instruction_->GetDexPc(),
537 this);
538 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000539 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000540
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000541 if (!is_fatal_) {
542 RestoreLiveRegisters(codegen, locations);
543 __ b(GetExitLabel());
544 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000545 }
546
Alexandre Rames9931f312015-06-19 14:47:01 +0100547 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
548
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000549 bool IsFatal() const OVERRIDE { return is_fatal_; }
550
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000551 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000552 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000553
554 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
555};
556
Artem Serovf4d6aee2016-07-11 10:41:45 +0100557class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700558 public:
Aart Bik42249c32016-01-07 15:33:50 -0800559 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100560 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700561
562 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800563 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700564 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100565 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000566 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700567 }
568
Alexandre Rames9931f312015-06-19 14:47:01 +0100569 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
570
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700571 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700572 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
573};
574
Artem Serovf4d6aee2016-07-11 10:41:45 +0100575class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100576 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100577 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100578
579 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
580 LocationSummary* locations = instruction_->GetLocations();
581 __ Bind(GetEntryLabel());
582 SaveLiveRegisters(codegen, locations);
583
584 InvokeRuntimeCallingConvention calling_convention;
585 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
586 parallel_move.AddMove(
587 locations->InAt(0),
588 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
589 Primitive::kPrimNot,
590 nullptr);
591 parallel_move.AddMove(
592 locations->InAt(1),
593 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
594 Primitive::kPrimInt,
595 nullptr);
596 parallel_move.AddMove(
597 locations->InAt(2),
598 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
599 Primitive::kPrimNot,
600 nullptr);
601 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
602
603 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100604 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000605 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100606 RestoreLiveRegisters(codegen, locations);
607 __ b(GetExitLabel());
608 }
609
610 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
611
612 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100613 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
614};
615
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100616// Slow path marking an object reference `ref` during a read
617// barrier. The field `obj.field` in the object `obj` holding this
618// reference does not get updated by this slow path after marking (see
619// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
620//
621// This means that after the execution of this slow path, `ref` will
622// always be up-to-date, but `obj.field` may not; i.e., after the
623// flip, `ref` will be a to-space reference, but `obj.field` will
624// probably still be a from-space reference (unless it gets updated by
625// another thread, or if another thread installed another object
626// reference (different from `ref`) in `obj.field`).
Artem Serovf4d6aee2016-07-11 10:41:45 +0100627class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000628 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800629 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
630 Location ref,
631 Location entrypoint = Location::NoLocation())
632 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillainc9285912015-12-18 10:38:42 +0000633 DCHECK(kEmitCompilerReadBarrier);
634 }
635
636 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
637
638 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
639 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100640 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000641 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100642 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillainc9285912015-12-18 10:38:42 +0000643 DCHECK(instruction_->IsInstanceFieldGet() ||
644 instruction_->IsStaticFieldGet() ||
645 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100646 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000647 instruction_->IsLoadClass() ||
648 instruction_->IsLoadString() ||
649 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100650 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100651 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
652 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000653 << "Unexpected instruction in read barrier marking slow path: "
654 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000655 // The read barrier instrumentation of object ArrayGet
656 // instructions does not support the HIntermediateAddress
657 // instruction.
658 DCHECK(!(instruction_->IsArrayGet() &&
659 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillainc9285912015-12-18 10:38:42 +0000660
661 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100662 // No need to save live registers; it's taken care of by the
663 // entrypoint. Also, there is no need to update the stack mask,
664 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000665 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100666 DCHECK_NE(ref_reg, SP);
667 DCHECK_NE(ref_reg, LR);
668 DCHECK_NE(ref_reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100669 // IP is used internally by the ReadBarrierMarkRegX entry point
670 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100671 DCHECK_NE(ref_reg, IP);
672 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100673 // "Compact" slow path, saving two moves.
674 //
675 // Instead of using the standard runtime calling convention (input
676 // and output in R0):
677 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100678 // R0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100679 // R0 <- ReadBarrierMark(R0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100680 // ref <- R0
Roland Levillain02b75802016-07-13 11:54:35 +0100681 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100682 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100683 // of a dedicated entrypoint:
684 //
685 // rX <- ReadBarrierMarkRegX(rX)
686 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800687 if (entrypoint_.IsValid()) {
688 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
689 __ blx(entrypoint_.AsRegister<Register>());
690 } else {
691 int32_t entry_point_offset =
692 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
693 // This runtime call does not require a stack map.
694 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
695 }
Roland Levillainc9285912015-12-18 10:38:42 +0000696 __ b(GetExitLabel());
697 }
698
699 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100700 // The location (register) of the marked object reference.
701 const Location ref_;
Roland Levillainc9285912015-12-18 10:38:42 +0000702
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800703 // The location of the entrypoint if already loaded.
704 const Location entrypoint_;
705
Roland Levillainc9285912015-12-18 10:38:42 +0000706 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
707};
708
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100709// Slow path marking an object reference `ref` during a read barrier,
710// and if needed, atomically updating the field `obj.field` in the
711// object `obj` holding this reference after marking (contrary to
712// ReadBarrierMarkSlowPathARM above, which never tries to update
713// `obj.field`).
714//
715// This means that after the execution of this slow path, both `ref`
716// and `obj.field` will be up-to-date; i.e., after the flip, both will
717// hold the same to-space reference (unless another thread installed
718// another object reference (different from `ref`) in `obj.field`).
719class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
720 public:
721 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
722 Location ref,
723 Register obj,
724 Location field_offset,
725 Register temp1,
726 Register temp2)
727 : SlowPathCodeARM(instruction),
728 ref_(ref),
729 obj_(obj),
730 field_offset_(field_offset),
731 temp1_(temp1),
732 temp2_(temp2) {
733 DCHECK(kEmitCompilerReadBarrier);
734 }
735
736 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
737
738 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
739 LocationSummary* locations = instruction_->GetLocations();
740 Register ref_reg = ref_.AsRegister<Register>();
741 DCHECK(locations->CanCall());
742 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
743 // This slow path is only used by the UnsafeCASObject intrinsic.
744 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
745 << "Unexpected instruction in read barrier marking and field updating slow path: "
746 << instruction_->DebugName();
747 DCHECK(instruction_->GetLocations()->Intrinsified());
748 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
749 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
750
751 __ Bind(GetEntryLabel());
752
753 // Save the old reference.
754 // Note that we cannot use IP to save the old reference, as IP is
755 // used internally by the ReadBarrierMarkRegX entry point, and we
756 // need the old reference after the call to that entry point.
757 DCHECK_NE(temp1_, IP);
758 __ Mov(temp1_, ref_reg);
759
760 // No need to save live registers; it's taken care of by the
761 // entrypoint. Also, there is no need to update the stack mask,
762 // as this runtime call will not trigger a garbage collection.
763 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
764 DCHECK_NE(ref_reg, SP);
765 DCHECK_NE(ref_reg, LR);
766 DCHECK_NE(ref_reg, PC);
767 // IP is used internally by the ReadBarrierMarkRegX entry point
768 // as a temporary, it cannot be the entry point's input/output.
769 DCHECK_NE(ref_reg, IP);
770 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
771 // "Compact" slow path, saving two moves.
772 //
773 // Instead of using the standard runtime calling convention (input
774 // and output in R0):
775 //
776 // R0 <- ref
777 // R0 <- ReadBarrierMark(R0)
778 // ref <- R0
779 //
780 // we just use rX (the register containing `ref`) as input and output
781 // of a dedicated entrypoint:
782 //
783 // rX <- ReadBarrierMarkRegX(rX)
784 //
785 int32_t entry_point_offset =
786 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
787 // This runtime call does not require a stack map.
788 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
789
790 // If the new reference is different from the old reference,
791 // update the field in the holder (`*(obj_ + field_offset_)`).
792 //
793 // Note that this field could also hold a different object, if
794 // another thread had concurrently changed it. In that case, the
795 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
796 // (CAS) operation below would abort the CAS, leaving the field
797 // as-is.
798 Label done;
799 __ cmp(temp1_, ShifterOperand(ref_reg));
800 __ b(&done, EQ);
801
802 // Update the the holder's field atomically. This may fail if
803 // mutator updates before us, but it's OK. This is achieved
804 // using a strong compare-and-set (CAS) operation with relaxed
805 // memory synchronization ordering, where the expected value is
806 // the old reference and the desired value is the new reference.
807
808 // Convenience aliases.
809 Register base = obj_;
810 // The UnsafeCASObject intrinsic uses a register pair as field
811 // offset ("long offset"), of which only the low part contains
812 // data.
813 Register offset = field_offset_.AsRegisterPairLow<Register>();
814 Register expected = temp1_;
815 Register value = ref_reg;
816 Register tmp_ptr = IP; // Pointer to actual memory.
817 Register tmp = temp2_; // Value in memory.
818
819 __ add(tmp_ptr, base, ShifterOperand(offset));
820
821 if (kPoisonHeapReferences) {
822 __ PoisonHeapReference(expected);
823 if (value == expected) {
824 // Do not poison `value`, as it is the same register as
825 // `expected`, which has just been poisoned.
826 } else {
827 __ PoisonHeapReference(value);
828 }
829 }
830
831 // do {
832 // tmp = [r_ptr] - expected;
833 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
834
Roland Levillain24a4d112016-10-26 13:10:46 +0100835 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100836 __ Bind(&loop_head);
837
838 __ ldrex(tmp, tmp_ptr);
839
840 __ subs(tmp, tmp, ShifterOperand(expected));
841
Roland Levillain24a4d112016-10-26 13:10:46 +0100842 __ it(NE);
843 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100844
Roland Levillain24a4d112016-10-26 13:10:46 +0100845 __ b(&exit_loop, NE);
846
847 __ strex(tmp, value, tmp_ptr);
848 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100849 __ b(&loop_head, EQ);
850
Roland Levillain24a4d112016-10-26 13:10:46 +0100851 __ Bind(&exit_loop);
852
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100853 if (kPoisonHeapReferences) {
854 __ UnpoisonHeapReference(expected);
855 if (value == expected) {
856 // Do not unpoison `value`, as it is the same register as
857 // `expected`, which has just been unpoisoned.
858 } else {
859 __ UnpoisonHeapReference(value);
860 }
861 }
862
863 __ Bind(&done);
864 __ b(GetExitLabel());
865 }
866
867 private:
868 // The location (register) of the marked object reference.
869 const Location ref_;
870 // The register containing the object holding the marked object reference field.
871 const Register obj_;
872 // The location of the offset of the marked reference field within `obj_`.
873 Location field_offset_;
874
875 const Register temp1_;
876 const Register temp2_;
877
878 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
879};
880
Roland Levillain3b359c72015-11-17 19:35:12 +0000881// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100882class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000883 public:
884 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
885 Location out,
886 Location ref,
887 Location obj,
888 uint32_t offset,
889 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100890 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000891 out_(out),
892 ref_(ref),
893 obj_(obj),
894 offset_(offset),
895 index_(index) {
896 DCHECK(kEmitCompilerReadBarrier);
897 // If `obj` is equal to `out` or `ref`, it means the initial object
898 // has been overwritten by (or after) the heap object reference load
899 // to be instrumented, e.g.:
900 //
901 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000902 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000903 //
904 // In that case, we have lost the information about the original
905 // object, and the emitted read barrier cannot work properly.
906 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
907 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
908 }
909
910 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
911 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
912 LocationSummary* locations = instruction_->GetLocations();
913 Register reg_out = out_.AsRegister<Register>();
914 DCHECK(locations->CanCall());
915 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100916 DCHECK(instruction_->IsInstanceFieldGet() ||
917 instruction_->IsStaticFieldGet() ||
918 instruction_->IsArrayGet() ||
919 instruction_->IsInstanceOf() ||
920 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100921 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000922 << "Unexpected instruction in read barrier for heap reference slow path: "
923 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000924 // The read barrier instrumentation of object ArrayGet
925 // instructions does not support the HIntermediateAddress
926 // instruction.
927 DCHECK(!(instruction_->IsArrayGet() &&
928 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +0000929
930 __ Bind(GetEntryLabel());
931 SaveLiveRegisters(codegen, locations);
932
933 // We may have to change the index's value, but as `index_` is a
934 // constant member (like other "inputs" of this slow path),
935 // introduce a copy of it, `index`.
936 Location index = index_;
937 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100938 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000939 if (instruction_->IsArrayGet()) {
940 // Compute the actual memory offset and store it in `index`.
941 Register index_reg = index_.AsRegister<Register>();
942 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
943 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
944 // We are about to change the value of `index_reg` (see the
945 // calls to art::arm::Thumb2Assembler::Lsl and
946 // art::arm::Thumb2Assembler::AddConstant below), but it has
947 // not been saved by the previous call to
948 // art::SlowPathCode::SaveLiveRegisters, as it is a
949 // callee-save register --
950 // art::SlowPathCode::SaveLiveRegisters does not consider
951 // callee-save registers, as it has been designed with the
952 // assumption that callee-save registers are supposed to be
953 // handled by the called function. So, as a callee-save
954 // register, `index_reg` _would_ eventually be saved onto
955 // the stack, but it would be too late: we would have
956 // changed its value earlier. Therefore, we manually save
957 // it here into another freely available register,
958 // `free_reg`, chosen of course among the caller-save
959 // registers (as a callee-save `free_reg` register would
960 // exhibit the same problem).
961 //
962 // Note we could have requested a temporary register from
963 // the register allocator instead; but we prefer not to, as
964 // this is a slow path, and we know we can find a
965 // caller-save register that is available.
966 Register free_reg = FindAvailableCallerSaveRegister(codegen);
967 __ Mov(free_reg, index_reg);
968 index_reg = free_reg;
969 index = Location::RegisterLocation(index_reg);
970 } else {
971 // The initial register stored in `index_` has already been
972 // saved in the call to art::SlowPathCode::SaveLiveRegisters
973 // (as it is not a callee-save register), so we can freely
974 // use it.
975 }
976 // Shifting the index value contained in `index_reg` by the scale
977 // factor (2) cannot overflow in practice, as the runtime is
978 // unable to allocate object arrays with a size larger than
979 // 2^26 - 1 (that is, 2^28 - 4 bytes).
980 __ Lsl(index_reg, index_reg, TIMES_4);
981 static_assert(
982 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
983 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
984 __ AddConstant(index_reg, index_reg, offset_);
985 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100986 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
987 // intrinsics, `index_` is not shifted by a scale factor of 2
988 // (as in the case of ArrayGet), as it is actually an offset
989 // to an object field within an object.
990 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000991 DCHECK(instruction_->GetLocations()->Intrinsified());
992 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
993 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
994 << instruction_->AsInvoke()->GetIntrinsic();
995 DCHECK_EQ(offset_, 0U);
996 DCHECK(index_.IsRegisterPair());
997 // UnsafeGet's offset location is a register pair, the low
998 // part contains the correct offset.
999 index = index_.ToLow();
1000 }
1001 }
1002
1003 // We're moving two or three locations to locations that could
1004 // overlap, so we need a parallel move resolver.
1005 InvokeRuntimeCallingConvention calling_convention;
1006 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1007 parallel_move.AddMove(ref_,
1008 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1009 Primitive::kPrimNot,
1010 nullptr);
1011 parallel_move.AddMove(obj_,
1012 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1013 Primitive::kPrimNot,
1014 nullptr);
1015 if (index.IsValid()) {
1016 parallel_move.AddMove(index,
1017 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1018 Primitive::kPrimInt,
1019 nullptr);
1020 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1021 } else {
1022 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1023 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1024 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001025 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001026 CheckEntrypointTypes<
1027 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1028 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1029
1030 RestoreLiveRegisters(codegen, locations);
1031 __ b(GetExitLabel());
1032 }
1033
1034 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1035
1036 private:
1037 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1038 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1039 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1040 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1041 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1042 return static_cast<Register>(i);
1043 }
1044 }
1045 // We shall never fail to find a free caller-save register, as
1046 // there are more than two core caller-save registers on ARM
1047 // (meaning it is possible to find one which is different from
1048 // `ref` and `obj`).
1049 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1050 LOG(FATAL) << "Could not find a free caller-save register";
1051 UNREACHABLE();
1052 }
1053
Roland Levillain3b359c72015-11-17 19:35:12 +00001054 const Location out_;
1055 const Location ref_;
1056 const Location obj_;
1057 const uint32_t offset_;
1058 // An additional location containing an index to an array.
1059 // Only used for HArrayGet and the UnsafeGetObject &
1060 // UnsafeGetObjectVolatile intrinsics.
1061 const Location index_;
1062
1063 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1064};
1065
1066// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001067class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001068 public:
1069 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001070 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001071 DCHECK(kEmitCompilerReadBarrier);
1072 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001073
1074 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1075 LocationSummary* locations = instruction_->GetLocations();
1076 Register reg_out = out_.AsRegister<Register>();
1077 DCHECK(locations->CanCall());
1078 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001079 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1080 << "Unexpected instruction in read barrier for GC root slow path: "
1081 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001082
1083 __ Bind(GetEntryLabel());
1084 SaveLiveRegisters(codegen, locations);
1085
1086 InvokeRuntimeCallingConvention calling_convention;
1087 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1088 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001089 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001090 instruction_,
1091 instruction_->GetDexPc(),
1092 this);
1093 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1094 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1095
1096 RestoreLiveRegisters(codegen, locations);
1097 __ b(GetExitLabel());
1098 }
1099
1100 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1101
1102 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001103 const Location out_;
1104 const Location root_;
1105
1106 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1107};
1108
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001109#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001110// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1111#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -07001112
Aart Bike9f37602015-10-09 11:15:55 -07001113inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001114 switch (cond) {
1115 case kCondEQ: return EQ;
1116 case kCondNE: return NE;
1117 case kCondLT: return LT;
1118 case kCondLE: return LE;
1119 case kCondGT: return GT;
1120 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001121 case kCondB: return LO;
1122 case kCondBE: return LS;
1123 case kCondA: return HI;
1124 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001125 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001126 LOG(FATAL) << "Unreachable";
1127 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001128}
1129
Aart Bike9f37602015-10-09 11:15:55 -07001130// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001131inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001132 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001133 case kCondEQ: return EQ;
1134 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001135 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001136 case kCondLT: return LO;
1137 case kCondLE: return LS;
1138 case kCondGT: return HI;
1139 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001140 // Unsigned remain unchanged.
1141 case kCondB: return LO;
1142 case kCondBE: return LS;
1143 case kCondA: return HI;
1144 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001145 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001146 LOG(FATAL) << "Unreachable";
1147 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001148}
1149
Vladimir Markod6e069b2016-01-18 11:11:01 +00001150inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1151 // The ARM condition codes can express all the necessary branches, see the
1152 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1153 // There is no dex instruction or HIR that would need the missing conditions
1154 // "equal or unordered" or "not equal".
1155 switch (cond) {
1156 case kCondEQ: return EQ;
1157 case kCondNE: return NE /* unordered */;
1158 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1159 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1160 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1161 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1162 default:
1163 LOG(FATAL) << "UNREACHABLE";
1164 UNREACHABLE();
1165 }
1166}
1167
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001168void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001169 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001170}
1171
1172void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001173 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001174}
1175
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001176size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1177 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1178 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001179}
1180
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001181size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1182 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1183 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001184}
1185
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001186size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1187 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1188 return kArmWordSize;
1189}
1190
1191size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1192 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1193 return kArmWordSize;
1194}
1195
Calin Juravle34166012014-12-19 17:22:29 +00001196CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001197 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001198 const CompilerOptions& compiler_options,
1199 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001200 : CodeGenerator(graph,
1201 kNumberOfCoreRegisters,
1202 kNumberOfSRegisters,
1203 kNumberOfRegisterPairs,
1204 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1205 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001206 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1207 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001208 compiler_options,
1209 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001210 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001211 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001212 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001213 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001214 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001215 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001216 uint32_literals_(std::less<uint32_t>(),
1217 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001218 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1219 boot_image_string_patches_(StringReferenceValueComparator(),
1220 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1221 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001222 boot_image_type_patches_(TypeReferenceValueComparator(),
1223 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1224 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001225 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001226 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001227 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1228 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001229 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1230 jit_class_patches_(TypeReferenceValueComparator(),
1231 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001232 // Always save the LR register to mimic Quick.
1233 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001234}
1235
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001236void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1237 // Ensure that we fix up branches and literal loads and emit the literal pool.
1238 __ FinalizeCode();
1239
1240 // Adjust native pc offsets in stack maps.
1241 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001242 uint32_t old_position =
1243 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001244 uint32_t new_position = __ GetAdjustedPosition(old_position);
1245 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1246 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001247 // Adjust pc offsets for the disassembly information.
1248 if (disasm_info_ != nullptr) {
1249 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1250 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1251 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1252 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1253 it.second.start = __ GetAdjustedPosition(it.second.start);
1254 it.second.end = __ GetAdjustedPosition(it.second.end);
1255 }
1256 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1257 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1258 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1259 }
1260 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001261
1262 CodeGenerator::Finalize(allocator);
1263}
1264
David Brazdil58282f42016-01-14 12:45:10 +00001265void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001266 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001267 blocked_core_registers_[SP] = true;
1268 blocked_core_registers_[LR] = true;
1269 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001270
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001271 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001272 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001273
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001274 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001275 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001276
David Brazdil58282f42016-01-14 12:45:10 +00001277 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001278 // Stubs do not save callee-save floating point registers. If the graph
1279 // is debuggable, we need to deal with these registers differently. For
1280 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001281 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1282 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1283 }
1284 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001285}
1286
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001287InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001288 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001289 assembler_(codegen->GetAssembler()),
1290 codegen_(codegen) {}
1291
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001292void CodeGeneratorARM::ComputeSpillMask() {
1293 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1294 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001295 // There is no easy instruction to restore just the PC on thumb2. We spill and
1296 // restore another arbitrary register.
1297 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001298 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1299 // We use vpush and vpop for saving and restoring floating point registers, which take
1300 // a SRegister and the number of registers to save/restore after that SRegister. We
1301 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1302 // but in the range.
1303 if (fpu_spill_mask_ != 0) {
1304 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1305 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1306 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1307 fpu_spill_mask_ |= (1 << i);
1308 }
1309 }
1310}
1311
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001312static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001313 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001314}
1315
1316static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001317 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001318}
1319
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001320void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001321 bool skip_overflow_check =
1322 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001323 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001324 __ Bind(&frame_entry_label_);
1325
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001326 if (HasEmptyFrame()) {
1327 return;
1328 }
1329
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001330 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001331 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1332 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1333 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001334 }
1335
Andreas Gampe501fd632015-09-10 16:11:06 -07001336 __ PushList(core_spill_mask_);
1337 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1338 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001339 if (fpu_spill_mask_ != 0) {
1340 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1341 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001342 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001343 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001344 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001345
1346 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1347 // Initialize should_deoptimize flag to 0.
1348 __ mov(IP, ShifterOperand(0));
1349 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
1350 }
1351
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001352 int adjust = GetFrameSize() - FrameEntrySpillSize();
1353 __ AddConstant(SP, -adjust);
1354 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001355
1356 // Save the current method if we need it. Note that we do not
1357 // do this in HCurrentMethod, as the instruction might have been removed
1358 // in the SSA graph.
1359 if (RequiresCurrentMethod()) {
1360 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1361 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001362}
1363
1364void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001365 if (HasEmptyFrame()) {
1366 __ bx(LR);
1367 return;
1368 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001369 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001370 int adjust = GetFrameSize() - FrameEntrySpillSize();
1371 __ AddConstant(SP, adjust);
1372 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001373 if (fpu_spill_mask_ != 0) {
1374 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1375 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001376 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001377 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001378 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001379 // Pop LR into PC to return.
1380 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1381 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1382 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001383 __ cfi().RestoreState();
1384 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001385}
1386
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001387void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001388 Label* label = GetLabelOf(block);
1389 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001390}
1391
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001392Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001393 switch (type) {
1394 case Primitive::kPrimBoolean:
1395 case Primitive::kPrimByte:
1396 case Primitive::kPrimChar:
1397 case Primitive::kPrimShort:
1398 case Primitive::kPrimInt:
1399 case Primitive::kPrimNot: {
1400 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001401 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001402 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001403 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001404 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001405 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001406 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001407 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001408
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001409 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001410 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001411 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001412 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001413 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001414 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001415 if (calling_convention.GetRegisterAt(index) == R1) {
1416 // Skip R1, and use R2_R3 instead.
1417 gp_index_++;
1418 index++;
1419 }
1420 }
1421 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1422 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001423 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001424
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001425 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001426 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001427 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001428 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1429 }
1430 }
1431
1432 case Primitive::kPrimFloat: {
1433 uint32_t stack_index = stack_index_++;
1434 if (float_index_ % 2 == 0) {
1435 float_index_ = std::max(double_index_, float_index_);
1436 }
1437 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1438 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1439 } else {
1440 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1441 }
1442 }
1443
1444 case Primitive::kPrimDouble: {
1445 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1446 uint32_t stack_index = stack_index_;
1447 stack_index_ += 2;
1448 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1449 uint32_t index = double_index_;
1450 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001451 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001452 calling_convention.GetFpuRegisterAt(index),
1453 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001454 DCHECK(ExpectedPairLayout(result));
1455 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001456 } else {
1457 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001458 }
1459 }
1460
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001461 case Primitive::kPrimVoid:
1462 LOG(FATAL) << "Unexpected parameter type " << type;
1463 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001464 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001465 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001466}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001467
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001468Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001469 switch (type) {
1470 case Primitive::kPrimBoolean:
1471 case Primitive::kPrimByte:
1472 case Primitive::kPrimChar:
1473 case Primitive::kPrimShort:
1474 case Primitive::kPrimInt:
1475 case Primitive::kPrimNot: {
1476 return Location::RegisterLocation(R0);
1477 }
1478
1479 case Primitive::kPrimFloat: {
1480 return Location::FpuRegisterLocation(S0);
1481 }
1482
1483 case Primitive::kPrimLong: {
1484 return Location::RegisterPairLocation(R0, R1);
1485 }
1486
1487 case Primitive::kPrimDouble: {
1488 return Location::FpuRegisterPairLocation(S0, S1);
1489 }
1490
1491 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001492 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001493 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001494
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001495 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001496}
1497
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001498Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1499 return Location::RegisterLocation(kMethodRegisterArgument);
1500}
1501
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001502void CodeGeneratorARM::Move32(Location destination, Location source) {
1503 if (source.Equals(destination)) {
1504 return;
1505 }
1506 if (destination.IsRegister()) {
1507 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001508 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001509 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001510 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001511 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001512 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001513 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001514 } else if (destination.IsFpuRegister()) {
1515 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001516 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001517 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001518 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001519 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001520 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001521 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001522 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001523 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001524 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001525 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001526 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001527 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001528 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001529 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001530 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1531 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001532 }
1533 }
1534}
1535
1536void CodeGeneratorARM::Move64(Location destination, Location source) {
1537 if (source.Equals(destination)) {
1538 return;
1539 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001540 if (destination.IsRegisterPair()) {
1541 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001542 EmitParallelMoves(
1543 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1544 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001545 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001546 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001547 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1548 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001549 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001550 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001551 } else if (source.IsFpuRegisterPair()) {
1552 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1553 destination.AsRegisterPairHigh<Register>(),
1554 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001555 } else {
1556 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001557 DCHECK(ExpectedPairLayout(destination));
1558 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1559 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001560 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001561 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001562 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001563 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1564 SP,
1565 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001566 } else if (source.IsRegisterPair()) {
1567 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1568 source.AsRegisterPairLow<Register>(),
1569 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001570 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001571 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001572 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001573 } else {
1574 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001575 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001576 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001577 if (source.AsRegisterPairLow<Register>() == R1) {
1578 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001579 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1580 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001581 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001582 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001583 SP, destination.GetStackIndex());
1584 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001585 } else if (source.IsFpuRegisterPair()) {
1586 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1587 SP,
1588 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001589 } else {
1590 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001591 EmitParallelMoves(
1592 Location::StackSlot(source.GetStackIndex()),
1593 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001594 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001595 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001596 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1597 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001598 }
1599 }
1600}
1601
Calin Juravle175dc732015-08-25 15:42:32 +01001602void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1603 DCHECK(location.IsRegister());
1604 __ LoadImmediate(location.AsRegister<Register>(), value);
1605}
1606
Calin Juravlee460d1d2015-09-29 04:52:17 +01001607void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001608 HParallelMove move(GetGraph()->GetArena());
1609 move.AddMove(src, dst, dst_type, nullptr);
1610 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001611}
1612
1613void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1614 if (location.IsRegister()) {
1615 locations->AddTemp(location);
1616 } else if (location.IsRegisterPair()) {
1617 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1618 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1619 } else {
1620 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1621 }
1622}
1623
Calin Juravle175dc732015-08-25 15:42:32 +01001624void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1625 HInstruction* instruction,
1626 uint32_t dex_pc,
1627 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001628 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001629 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001630 if (EntrypointRequiresStackMap(entrypoint)) {
1631 RecordPcInfo(instruction, dex_pc, slow_path);
1632 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001633}
1634
Roland Levillaindec8f632016-07-22 17:10:06 +01001635void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1636 HInstruction* instruction,
1637 SlowPathCode* slow_path) {
1638 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001639 GenerateInvokeRuntime(entry_point_offset);
1640}
1641
1642void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001643 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1644 __ blx(LR);
1645}
1646
David Brazdilfc6a86a2015-06-26 10:33:45 +00001647void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001648 DCHECK(!successor->IsExitBlock());
1649
1650 HBasicBlock* block = got->GetBlock();
1651 HInstruction* previous = got->GetPrevious();
1652
1653 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001654 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001655 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1656 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1657 return;
1658 }
1659
1660 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1661 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1662 }
1663 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001664 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001665 }
1666}
1667
David Brazdilfc6a86a2015-06-26 10:33:45 +00001668void LocationsBuilderARM::VisitGoto(HGoto* got) {
1669 got->SetLocations(nullptr);
1670}
1671
1672void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1673 HandleGoto(got, got->GetSuccessor());
1674}
1675
1676void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1677 try_boundary->SetLocations(nullptr);
1678}
1679
1680void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1681 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1682 if (!successor->IsExitBlock()) {
1683 HandleGoto(try_boundary, successor);
1684 }
1685}
1686
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001687void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001688 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001689}
1690
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001691void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001692}
1693
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001694void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1695 Primitive::Type type = instruction->InputAt(0)->GetType();
1696 Location lhs_loc = instruction->GetLocations()->InAt(0);
1697 Location rhs_loc = instruction->GetLocations()->InAt(1);
1698 if (rhs_loc.IsConstant()) {
1699 // 0.0 is the only immediate that can be encoded directly in
1700 // a VCMP instruction.
1701 //
1702 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1703 // specify that in a floating-point comparison, positive zero
1704 // and negative zero are considered equal, so we can use the
1705 // literal 0.0 for both cases here.
1706 //
1707 // Note however that some methods (Float.equal, Float.compare,
1708 // Float.compareTo, Double.equal, Double.compare,
1709 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1710 // StrictMath.min) consider 0.0 to be (strictly) greater than
1711 // -0.0. So if we ever translate calls to these methods into a
1712 // HCompare instruction, we must handle the -0.0 case with
1713 // care here.
1714 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1715 if (type == Primitive::kPrimFloat) {
1716 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1717 } else {
1718 DCHECK_EQ(type, Primitive::kPrimDouble);
1719 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1720 }
1721 } else {
1722 if (type == Primitive::kPrimFloat) {
1723 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1724 } else {
1725 DCHECK_EQ(type, Primitive::kPrimDouble);
1726 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1727 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1728 }
1729 }
1730}
1731
Roland Levillain4fa13f62015-07-06 18:11:54 +01001732void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1733 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001734 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001735 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001736 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001737}
1738
1739void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1740 Label* true_label,
1741 Label* false_label) {
1742 LocationSummary* locations = cond->GetLocations();
1743 Location left = locations->InAt(0);
1744 Location right = locations->InAt(1);
1745 IfCondition if_cond = cond->GetCondition();
1746
1747 Register left_high = left.AsRegisterPairHigh<Register>();
1748 Register left_low = left.AsRegisterPairLow<Register>();
1749 IfCondition true_high_cond = if_cond;
1750 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001751 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001752
1753 // Set the conditions for the test, remembering that == needs to be
1754 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001755 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001756 switch (if_cond) {
1757 case kCondEQ:
1758 case kCondNE:
1759 // Nothing to do.
1760 break;
1761 case kCondLT:
1762 false_high_cond = kCondGT;
1763 break;
1764 case kCondLE:
1765 true_high_cond = kCondLT;
1766 break;
1767 case kCondGT:
1768 false_high_cond = kCondLT;
1769 break;
1770 case kCondGE:
1771 true_high_cond = kCondGT;
1772 break;
Aart Bike9f37602015-10-09 11:15:55 -07001773 case kCondB:
1774 false_high_cond = kCondA;
1775 break;
1776 case kCondBE:
1777 true_high_cond = kCondB;
1778 break;
1779 case kCondA:
1780 false_high_cond = kCondB;
1781 break;
1782 case kCondAE:
1783 true_high_cond = kCondA;
1784 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001785 }
1786 if (right.IsConstant()) {
1787 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1788 int32_t val_low = Low32Bits(value);
1789 int32_t val_high = High32Bits(value);
1790
Vladimir Markoac6ac102015-12-17 12:14:00 +00001791 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001792 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001793 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001794 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001795 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001796 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001797 __ b(true_label, ARMCondition(true_high_cond));
1798 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001799 }
1800 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001801 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001802 } else {
1803 Register right_high = right.AsRegisterPairHigh<Register>();
1804 Register right_low = right.AsRegisterPairLow<Register>();
1805
1806 __ cmp(left_high, ShifterOperand(right_high));
1807 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001808 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001809 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001810 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001811 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001812 __ b(true_label, ARMCondition(true_high_cond));
1813 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001814 }
1815 // Must be equal high, so compare the lows.
1816 __ cmp(left_low, ShifterOperand(right_low));
1817 }
1818 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001819 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001820 __ b(true_label, final_condition);
1821}
1822
David Brazdil0debae72015-11-12 18:37:00 +00001823void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1824 Label* true_target_in,
1825 Label* false_target_in) {
1826 // Generated branching requires both targets to be explicit. If either of the
1827 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1828 Label fallthrough_target;
1829 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1830 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1831
Roland Levillain4fa13f62015-07-06 18:11:54 +01001832 Primitive::Type type = condition->InputAt(0)->GetType();
1833 switch (type) {
1834 case Primitive::kPrimLong:
1835 GenerateLongComparesAndJumps(condition, true_target, false_target);
1836 break;
1837 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001838 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001839 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001840 GenerateFPJumps(condition, true_target, false_target);
1841 break;
1842 default:
1843 LOG(FATAL) << "Unexpected compare type " << type;
1844 }
1845
David Brazdil0debae72015-11-12 18:37:00 +00001846 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001847 __ b(false_target);
1848 }
David Brazdil0debae72015-11-12 18:37:00 +00001849
1850 if (fallthrough_target.IsLinked()) {
1851 __ Bind(&fallthrough_target);
1852 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001853}
1854
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001855void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001856 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001857 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001858 Label* false_target) {
1859 HInstruction* cond = instruction->InputAt(condition_input_index);
1860
1861 if (true_target == nullptr && false_target == nullptr) {
1862 // Nothing to do. The code always falls through.
1863 return;
1864 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001865 // Constant condition, statically compared against "true" (integer value 1).
1866 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001867 if (true_target != nullptr) {
1868 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001869 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001870 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001871 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001872 if (false_target != nullptr) {
1873 __ b(false_target);
1874 }
1875 }
1876 return;
1877 }
1878
1879 // The following code generates these patterns:
1880 // (1) true_target == nullptr && false_target != nullptr
1881 // - opposite condition true => branch to false_target
1882 // (2) true_target != nullptr && false_target == nullptr
1883 // - condition true => branch to true_target
1884 // (3) true_target != nullptr && false_target != nullptr
1885 // - condition true => branch to true_target
1886 // - branch to false_target
1887 if (IsBooleanValueOrMaterializedCondition(cond)) {
1888 // Condition has been materialized, compare the output to 0.
1889 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1890 DCHECK(cond_val.IsRegister());
1891 if (true_target == nullptr) {
1892 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1893 } else {
1894 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001895 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001896 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001897 // Condition has not been materialized. Use its inputs as the comparison and
1898 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001899 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001900
1901 // If this is a long or FP comparison that has been folded into
1902 // the HCondition, generate the comparison directly.
1903 Primitive::Type type = condition->InputAt(0)->GetType();
1904 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1905 GenerateCompareTestAndBranch(condition, true_target, false_target);
1906 return;
1907 }
1908
1909 LocationSummary* locations = cond->GetLocations();
1910 DCHECK(locations->InAt(0).IsRegister());
1911 Register left = locations->InAt(0).AsRegister<Register>();
1912 Location right = locations->InAt(1);
1913 if (right.IsRegister()) {
1914 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001915 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001916 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001917 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001918 }
1919 if (true_target == nullptr) {
1920 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1921 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001922 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001923 }
Dave Allison20dfc792014-06-16 20:44:29 -07001924 }
David Brazdil0debae72015-11-12 18:37:00 +00001925
1926 // If neither branch falls through (case 3), the conditional branch to `true_target`
1927 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1928 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001929 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001930 }
1931}
1932
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001933void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001934 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1935 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001936 locations->SetInAt(0, Location::RequiresRegister());
1937 }
1938}
1939
1940void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001941 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1942 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1943 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1944 nullptr : codegen_->GetLabelOf(true_successor);
1945 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1946 nullptr : codegen_->GetLabelOf(false_successor);
1947 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001948}
1949
1950void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1951 LocationSummary* locations = new (GetGraph()->GetArena())
1952 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001953 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001954 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001955 locations->SetInAt(0, Location::RequiresRegister());
1956 }
1957}
1958
1959void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001960 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001961 GenerateTestAndBranch(deoptimize,
1962 /* condition_input_index */ 0,
1963 slow_path->GetEntryLabel(),
1964 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001965}
Dave Allison20dfc792014-06-16 20:44:29 -07001966
Mingyao Yang063fc772016-08-02 11:02:54 -07001967void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1968 LocationSummary* locations = new (GetGraph()->GetArena())
1969 LocationSummary(flag, LocationSummary::kNoCall);
1970 locations->SetOut(Location::RequiresRegister());
1971}
1972
1973void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1974 __ LoadFromOffset(kLoadWord,
1975 flag->GetLocations()->Out().AsRegister<Register>(),
1976 SP,
1977 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
1978}
1979
David Brazdil74eb1b22015-12-14 11:44:01 +00001980void LocationsBuilderARM::VisitSelect(HSelect* select) {
1981 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1982 if (Primitive::IsFloatingPointType(select->GetType())) {
1983 locations->SetInAt(0, Location::RequiresFpuRegister());
1984 locations->SetInAt(1, Location::RequiresFpuRegister());
1985 } else {
1986 locations->SetInAt(0, Location::RequiresRegister());
1987 locations->SetInAt(1, Location::RequiresRegister());
1988 }
1989 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1990 locations->SetInAt(2, Location::RequiresRegister());
1991 }
1992 locations->SetOut(Location::SameAsFirstInput());
1993}
1994
1995void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1996 LocationSummary* locations = select->GetLocations();
1997 Label false_target;
1998 GenerateTestAndBranch(select,
1999 /* condition_input_index */ 2,
2000 /* true_target */ nullptr,
2001 &false_target);
2002 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2003 __ Bind(&false_target);
2004}
2005
David Srbecky0cf44932015-12-09 14:09:59 +00002006void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2007 new (GetGraph()->GetArena()) LocationSummary(info);
2008}
2009
David Srbeckyd28f4a02016-03-14 17:14:24 +00002010void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2011 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002012}
2013
2014void CodeGeneratorARM::GenerateNop() {
2015 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002016}
2017
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002018void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002019 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002020 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002021 // Handle the long/FP comparisons made in instruction simplification.
2022 switch (cond->InputAt(0)->GetType()) {
2023 case Primitive::kPrimLong:
2024 locations->SetInAt(0, Location::RequiresRegister());
2025 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002026 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002027 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2028 }
2029 break;
2030
2031 case Primitive::kPrimFloat:
2032 case Primitive::kPrimDouble:
2033 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002034 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002035 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002036 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2037 }
2038 break;
2039
2040 default:
2041 locations->SetInAt(0, Location::RequiresRegister());
2042 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002043 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002044 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2045 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002046 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002047}
2048
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002049void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002050 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002051 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002052 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002053
2054 LocationSummary* locations = cond->GetLocations();
2055 Location left = locations->InAt(0);
2056 Location right = locations->InAt(1);
2057 Register out = locations->Out().AsRegister<Register>();
2058 Label true_label, false_label;
2059
2060 switch (cond->InputAt(0)->GetType()) {
2061 default: {
2062 // Integer case.
2063 if (right.IsRegister()) {
2064 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2065 } else {
2066 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002067 __ CmpConstant(left.AsRegister<Register>(),
2068 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002069 }
Aart Bike9f37602015-10-09 11:15:55 -07002070 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002071 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002072 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002073 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002074 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002075 return;
2076 }
2077 case Primitive::kPrimLong:
2078 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2079 break;
2080 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002081 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002082 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002083 GenerateFPJumps(cond, &true_label, &false_label);
2084 break;
2085 }
2086
2087 // Convert the jumps into the result.
2088 Label done_label;
2089
2090 // False case: result = 0.
2091 __ Bind(&false_label);
2092 __ LoadImmediate(out, 0);
2093 __ b(&done_label);
2094
2095 // True case: result = 1.
2096 __ Bind(&true_label);
2097 __ LoadImmediate(out, 1);
2098 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002099}
2100
2101void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002102 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002103}
2104
2105void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002106 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002107}
2108
2109void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002110 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002111}
2112
2113void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002114 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002115}
2116
2117void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002118 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002119}
2120
2121void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002122 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002123}
2124
2125void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002126 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002127}
2128
2129void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002130 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002131}
2132
2133void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002134 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002135}
2136
2137void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002138 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002139}
2140
2141void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002142 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002143}
2144
2145void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002146 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002147}
2148
Aart Bike9f37602015-10-09 11:15:55 -07002149void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002150 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002151}
2152
2153void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002154 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002155}
2156
2157void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002158 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002159}
2160
2161void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002162 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002163}
2164
2165void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002166 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002167}
2168
2169void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002170 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002171}
2172
2173void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002174 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002175}
2176
2177void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002178 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002179}
2180
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002181void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002182 LocationSummary* locations =
2183 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002184 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002185}
2186
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002187void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002188 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002189}
2190
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002191void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2192 LocationSummary* locations =
2193 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2194 locations->SetOut(Location::ConstantLocation(constant));
2195}
2196
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002197void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002198 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002199}
2200
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002201void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002202 LocationSummary* locations =
2203 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002204 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002205}
2206
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002207void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002208 // Will be generated at use site.
2209}
2210
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002211void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2212 LocationSummary* locations =
2213 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2214 locations->SetOut(Location::ConstantLocation(constant));
2215}
2216
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002217void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002218 // Will be generated at use site.
2219}
2220
2221void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2222 LocationSummary* locations =
2223 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2224 locations->SetOut(Location::ConstantLocation(constant));
2225}
2226
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002227void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002228 // Will be generated at use site.
2229}
2230
Calin Juravle27df7582015-04-17 19:12:31 +01002231void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2232 memory_barrier->SetLocations(nullptr);
2233}
2234
2235void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002236 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002237}
2238
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002239void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002240 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002241}
2242
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002243void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002244 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002245}
2246
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002247void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002248 LocationSummary* locations =
2249 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002250 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002251}
2252
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002253void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002254 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002255}
2256
Calin Juravle175dc732015-08-25 15:42:32 +01002257void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2258 // The trampoline uses the same calling convention as dex calling conventions,
2259 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2260 // the method_idx.
2261 HandleInvoke(invoke);
2262}
2263
2264void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2265 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2266}
2267
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002268void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002269 // Explicit clinit checks triggered by static invokes must have been pruned by
2270 // art::PrepareForRegisterAllocation.
2271 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002272
Vladimir Marko68c981f2016-08-26 13:13:33 +01002273 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002274 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002275 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2276 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2277 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002278 return;
2279 }
2280
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002281 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002282
2283 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2284 if (invoke->HasPcRelativeDexCache()) {
2285 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2286 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002287}
2288
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002289static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2290 if (invoke->GetLocations()->Intrinsified()) {
2291 IntrinsicCodeGeneratorARM intrinsic(codegen);
2292 intrinsic.Dispatch(invoke);
2293 return true;
2294 }
2295 return false;
2296}
2297
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002298void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002299 // Explicit clinit checks triggered by static invokes must have been pruned by
2300 // art::PrepareForRegisterAllocation.
2301 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002302
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002303 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2304 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002305 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002306
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002307 LocationSummary* locations = invoke->GetLocations();
2308 codegen_->GenerateStaticOrDirectCall(
2309 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002310 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002311}
2312
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002313void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002314 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002315 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002316}
2317
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002318void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002319 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002320 if (intrinsic.TryDispatch(invoke)) {
2321 return;
2322 }
2323
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002324 HandleInvoke(invoke);
2325}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002326
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002327void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002328 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2329 return;
2330 }
2331
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002332 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002333 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002334 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002335}
2336
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002337void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2338 HandleInvoke(invoke);
2339 // Add the hidden argument.
2340 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2341}
2342
2343void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2344 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002345 LocationSummary* locations = invoke->GetLocations();
2346 Register temp = locations->GetTemp(0).AsRegister<Register>();
2347 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002348 Location receiver = locations->InAt(0);
2349 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2350
Roland Levillain3b359c72015-11-17 19:35:12 +00002351 // Set the hidden argument. This is safe to do this here, as R12
2352 // won't be modified thereafter, before the `blx` (call) instruction.
2353 DCHECK_EQ(R12, hidden_reg);
2354 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002355
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002356 if (receiver.IsStackSlot()) {
2357 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002358 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002359 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2360 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002361 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002362 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002363 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002364 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002365 // Instead of simply (possibly) unpoisoning `temp` here, we should
2366 // emit a read barrier for the previous class reference load.
2367 // However this is not required in practice, as this is an
2368 // intermediate/temporary reference and because the current
2369 // concurrent copying collector keeps the from-space memory
2370 // intact/accessible until the end of the marking phase (the
2371 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002372 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002373 __ LoadFromOffset(kLoadWord, temp, temp,
2374 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2375 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002376 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002377 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002378 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002379 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002380 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002381 // LR = temp->GetEntryPoint();
2382 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2383 // LR();
2384 __ blx(LR);
2385 DCHECK(!codegen_->IsLeafMethod());
2386 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2387}
2388
Orion Hodsonac141392017-01-13 11:53:47 +00002389void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2390 HandleInvoke(invoke);
2391}
2392
2393void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2394 codegen_->GenerateInvokePolymorphicCall(invoke);
2395}
2396
Roland Levillain88cb1752014-10-20 16:36:47 +01002397void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2398 LocationSummary* locations =
2399 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2400 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002401 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002402 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002403 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2404 break;
2405 }
2406 case Primitive::kPrimLong: {
2407 locations->SetInAt(0, Location::RequiresRegister());
2408 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002409 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002410 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002411
Roland Levillain88cb1752014-10-20 16:36:47 +01002412 case Primitive::kPrimFloat:
2413 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002414 locations->SetInAt(0, Location::RequiresFpuRegister());
2415 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002416 break;
2417
2418 default:
2419 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2420 }
2421}
2422
2423void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2424 LocationSummary* locations = neg->GetLocations();
2425 Location out = locations->Out();
2426 Location in = locations->InAt(0);
2427 switch (neg->GetResultType()) {
2428 case Primitive::kPrimInt:
2429 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002430 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002431 break;
2432
2433 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002434 DCHECK(in.IsRegisterPair());
2435 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2436 __ rsbs(out.AsRegisterPairLow<Register>(),
2437 in.AsRegisterPairLow<Register>(),
2438 ShifterOperand(0));
2439 // We cannot emit an RSC (Reverse Subtract with Carry)
2440 // instruction here, as it does not exist in the Thumb-2
2441 // instruction set. We use the following approach
2442 // using SBC and SUB instead.
2443 //
2444 // out.hi = -C
2445 __ sbc(out.AsRegisterPairHigh<Register>(),
2446 out.AsRegisterPairHigh<Register>(),
2447 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2448 // out.hi = out.hi - in.hi
2449 __ sub(out.AsRegisterPairHigh<Register>(),
2450 out.AsRegisterPairHigh<Register>(),
2451 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2452 break;
2453
Roland Levillain88cb1752014-10-20 16:36:47 +01002454 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002455 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002456 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002457 break;
2458
Roland Levillain88cb1752014-10-20 16:36:47 +01002459 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002460 DCHECK(in.IsFpuRegisterPair());
2461 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2462 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002463 break;
2464
2465 default:
2466 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2467 }
2468}
2469
Roland Levillaindff1f282014-11-05 14:15:05 +00002470void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002471 Primitive::Type result_type = conversion->GetResultType();
2472 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002473 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002474
Roland Levillain5b3ee562015-04-14 16:02:41 +01002475 // The float-to-long, double-to-long and long-to-float type conversions
2476 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002477 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002478 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2479 && result_type == Primitive::kPrimLong)
2480 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002481 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002482 : LocationSummary::kNoCall;
2483 LocationSummary* locations =
2484 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2485
David Brazdilb2bd1c52015-03-25 11:17:37 +00002486 // The Java language does not allow treating boolean as an integral type but
2487 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002488
Roland Levillaindff1f282014-11-05 14:15:05 +00002489 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002490 case Primitive::kPrimByte:
2491 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002492 case Primitive::kPrimLong:
2493 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002494 case Primitive::kPrimBoolean:
2495 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002496 case Primitive::kPrimShort:
2497 case Primitive::kPrimInt:
2498 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002499 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002500 locations->SetInAt(0, Location::RequiresRegister());
2501 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2502 break;
2503
2504 default:
2505 LOG(FATAL) << "Unexpected type conversion from " << input_type
2506 << " to " << result_type;
2507 }
2508 break;
2509
Roland Levillain01a8d712014-11-14 16:27:39 +00002510 case Primitive::kPrimShort:
2511 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002512 case Primitive::kPrimLong:
2513 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002514 case Primitive::kPrimBoolean:
2515 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002516 case Primitive::kPrimByte:
2517 case Primitive::kPrimInt:
2518 case Primitive::kPrimChar:
2519 // Processing a Dex `int-to-short' instruction.
2520 locations->SetInAt(0, Location::RequiresRegister());
2521 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2522 break;
2523
2524 default:
2525 LOG(FATAL) << "Unexpected type conversion from " << input_type
2526 << " to " << result_type;
2527 }
2528 break;
2529
Roland Levillain946e1432014-11-11 17:35:19 +00002530 case Primitive::kPrimInt:
2531 switch (input_type) {
2532 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002533 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002534 locations->SetInAt(0, Location::Any());
2535 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2536 break;
2537
2538 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002539 // Processing a Dex `float-to-int' instruction.
2540 locations->SetInAt(0, Location::RequiresFpuRegister());
2541 locations->SetOut(Location::RequiresRegister());
2542 locations->AddTemp(Location::RequiresFpuRegister());
2543 break;
2544
Roland Levillain946e1432014-11-11 17:35:19 +00002545 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002546 // Processing a Dex `double-to-int' instruction.
2547 locations->SetInAt(0, Location::RequiresFpuRegister());
2548 locations->SetOut(Location::RequiresRegister());
2549 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002550 break;
2551
2552 default:
2553 LOG(FATAL) << "Unexpected type conversion from " << input_type
2554 << " to " << result_type;
2555 }
2556 break;
2557
Roland Levillaindff1f282014-11-05 14:15:05 +00002558 case Primitive::kPrimLong:
2559 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002560 case Primitive::kPrimBoolean:
2561 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002562 case Primitive::kPrimByte:
2563 case Primitive::kPrimShort:
2564 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002565 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002566 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002567 locations->SetInAt(0, Location::RequiresRegister());
2568 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2569 break;
2570
Roland Levillain624279f2014-12-04 11:54:28 +00002571 case Primitive::kPrimFloat: {
2572 // Processing a Dex `float-to-long' instruction.
2573 InvokeRuntimeCallingConvention calling_convention;
2574 locations->SetInAt(0, Location::FpuRegisterLocation(
2575 calling_convention.GetFpuRegisterAt(0)));
2576 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2577 break;
2578 }
2579
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002580 case Primitive::kPrimDouble: {
2581 // Processing a Dex `double-to-long' instruction.
2582 InvokeRuntimeCallingConvention calling_convention;
2583 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2584 calling_convention.GetFpuRegisterAt(0),
2585 calling_convention.GetFpuRegisterAt(1)));
2586 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002587 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002588 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002589
2590 default:
2591 LOG(FATAL) << "Unexpected type conversion from " << input_type
2592 << " to " << result_type;
2593 }
2594 break;
2595
Roland Levillain981e4542014-11-14 11:47:14 +00002596 case Primitive::kPrimChar:
2597 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002598 case Primitive::kPrimLong:
2599 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002600 case Primitive::kPrimBoolean:
2601 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002602 case Primitive::kPrimByte:
2603 case Primitive::kPrimShort:
2604 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002605 // Processing a Dex `int-to-char' instruction.
2606 locations->SetInAt(0, Location::RequiresRegister());
2607 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2608 break;
2609
2610 default:
2611 LOG(FATAL) << "Unexpected type conversion from " << input_type
2612 << " to " << result_type;
2613 }
2614 break;
2615
Roland Levillaindff1f282014-11-05 14:15:05 +00002616 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002617 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002618 case Primitive::kPrimBoolean:
2619 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002620 case Primitive::kPrimByte:
2621 case Primitive::kPrimShort:
2622 case Primitive::kPrimInt:
2623 case Primitive::kPrimChar:
2624 // Processing a Dex `int-to-float' instruction.
2625 locations->SetInAt(0, Location::RequiresRegister());
2626 locations->SetOut(Location::RequiresFpuRegister());
2627 break;
2628
Roland Levillain5b3ee562015-04-14 16:02:41 +01002629 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002630 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002631 InvokeRuntimeCallingConvention calling_convention;
2632 locations->SetInAt(0, Location::RegisterPairLocation(
2633 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2634 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002635 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002636 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002637
Roland Levillaincff13742014-11-17 14:32:17 +00002638 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002639 // Processing a Dex `double-to-float' instruction.
2640 locations->SetInAt(0, Location::RequiresFpuRegister());
2641 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002642 break;
2643
2644 default:
2645 LOG(FATAL) << "Unexpected type conversion from " << input_type
2646 << " to " << result_type;
2647 };
2648 break;
2649
Roland Levillaindff1f282014-11-05 14:15:05 +00002650 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002651 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002652 case Primitive::kPrimBoolean:
2653 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002654 case Primitive::kPrimByte:
2655 case Primitive::kPrimShort:
2656 case Primitive::kPrimInt:
2657 case Primitive::kPrimChar:
2658 // Processing a Dex `int-to-double' instruction.
2659 locations->SetInAt(0, Location::RequiresRegister());
2660 locations->SetOut(Location::RequiresFpuRegister());
2661 break;
2662
2663 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002664 // Processing a Dex `long-to-double' instruction.
2665 locations->SetInAt(0, Location::RequiresRegister());
2666 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002667 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002668 locations->AddTemp(Location::RequiresFpuRegister());
2669 break;
2670
Roland Levillaincff13742014-11-17 14:32:17 +00002671 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002672 // Processing a Dex `float-to-double' instruction.
2673 locations->SetInAt(0, Location::RequiresFpuRegister());
2674 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002675 break;
2676
2677 default:
2678 LOG(FATAL) << "Unexpected type conversion from " << input_type
2679 << " to " << result_type;
2680 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002681 break;
2682
2683 default:
2684 LOG(FATAL) << "Unexpected type conversion from " << input_type
2685 << " to " << result_type;
2686 }
2687}
2688
2689void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2690 LocationSummary* locations = conversion->GetLocations();
2691 Location out = locations->Out();
2692 Location in = locations->InAt(0);
2693 Primitive::Type result_type = conversion->GetResultType();
2694 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002695 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002696 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002697 case Primitive::kPrimByte:
2698 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002699 case Primitive::kPrimLong:
2700 // Type conversion from long to byte is a result of code transformations.
2701 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2702 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002703 case Primitive::kPrimBoolean:
2704 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002705 case Primitive::kPrimShort:
2706 case Primitive::kPrimInt:
2707 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002708 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002709 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002710 break;
2711
2712 default:
2713 LOG(FATAL) << "Unexpected type conversion from " << input_type
2714 << " to " << result_type;
2715 }
2716 break;
2717
Roland Levillain01a8d712014-11-14 16:27:39 +00002718 case Primitive::kPrimShort:
2719 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002720 case Primitive::kPrimLong:
2721 // Type conversion from long to short is a result of code transformations.
2722 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2723 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002724 case Primitive::kPrimBoolean:
2725 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002726 case Primitive::kPrimByte:
2727 case Primitive::kPrimInt:
2728 case Primitive::kPrimChar:
2729 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002730 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002731 break;
2732
2733 default:
2734 LOG(FATAL) << "Unexpected type conversion from " << input_type
2735 << " to " << result_type;
2736 }
2737 break;
2738
Roland Levillain946e1432014-11-11 17:35:19 +00002739 case Primitive::kPrimInt:
2740 switch (input_type) {
2741 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002742 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002743 DCHECK(out.IsRegister());
2744 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002745 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002746 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002747 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002748 } else {
2749 DCHECK(in.IsConstant());
2750 DCHECK(in.GetConstant()->IsLongConstant());
2751 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002752 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002753 }
2754 break;
2755
Roland Levillain3f8f9362014-12-02 17:45:01 +00002756 case Primitive::kPrimFloat: {
2757 // Processing a Dex `float-to-int' instruction.
2758 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002759 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002760 __ vmovrs(out.AsRegister<Register>(), temp);
2761 break;
2762 }
2763
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002764 case Primitive::kPrimDouble: {
2765 // Processing a Dex `double-to-int' instruction.
2766 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002767 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002768 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002769 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002770 }
Roland Levillain946e1432014-11-11 17:35:19 +00002771
2772 default:
2773 LOG(FATAL) << "Unexpected type conversion from " << input_type
2774 << " to " << result_type;
2775 }
2776 break;
2777
Roland Levillaindff1f282014-11-05 14:15:05 +00002778 case Primitive::kPrimLong:
2779 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002780 case Primitive::kPrimBoolean:
2781 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002782 case Primitive::kPrimByte:
2783 case Primitive::kPrimShort:
2784 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002785 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002786 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002787 DCHECK(out.IsRegisterPair());
2788 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002789 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002790 // Sign extension.
2791 __ Asr(out.AsRegisterPairHigh<Register>(),
2792 out.AsRegisterPairLow<Register>(),
2793 31);
2794 break;
2795
2796 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002797 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002798 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002799 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002800 break;
2801
Roland Levillaindff1f282014-11-05 14:15:05 +00002802 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002803 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002804 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002805 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002806 break;
2807
2808 default:
2809 LOG(FATAL) << "Unexpected type conversion from " << input_type
2810 << " to " << result_type;
2811 }
2812 break;
2813
Roland Levillain981e4542014-11-14 11:47:14 +00002814 case Primitive::kPrimChar:
2815 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002816 case Primitive::kPrimLong:
2817 // Type conversion from long to char is a result of code transformations.
2818 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2819 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002820 case Primitive::kPrimBoolean:
2821 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002822 case Primitive::kPrimByte:
2823 case Primitive::kPrimShort:
2824 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002825 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002826 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002827 break;
2828
2829 default:
2830 LOG(FATAL) << "Unexpected type conversion from " << input_type
2831 << " to " << result_type;
2832 }
2833 break;
2834
Roland Levillaindff1f282014-11-05 14:15:05 +00002835 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002836 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002837 case Primitive::kPrimBoolean:
2838 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002839 case Primitive::kPrimByte:
2840 case Primitive::kPrimShort:
2841 case Primitive::kPrimInt:
2842 case Primitive::kPrimChar: {
2843 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002844 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2845 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002846 break;
2847 }
2848
Roland Levillain5b3ee562015-04-14 16:02:41 +01002849 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002850 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002851 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002852 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002853 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002854
Roland Levillaincff13742014-11-17 14:32:17 +00002855 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002856 // Processing a Dex `double-to-float' instruction.
2857 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2858 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002859 break;
2860
2861 default:
2862 LOG(FATAL) << "Unexpected type conversion from " << input_type
2863 << " to " << result_type;
2864 };
2865 break;
2866
Roland Levillaindff1f282014-11-05 14:15:05 +00002867 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002868 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002869 case Primitive::kPrimBoolean:
2870 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002871 case Primitive::kPrimByte:
2872 case Primitive::kPrimShort:
2873 case Primitive::kPrimInt:
2874 case Primitive::kPrimChar: {
2875 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002876 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002877 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2878 out.AsFpuRegisterPairLow<SRegister>());
2879 break;
2880 }
2881
Roland Levillain647b9ed2014-11-27 12:06:00 +00002882 case Primitive::kPrimLong: {
2883 // Processing a Dex `long-to-double' instruction.
2884 Register low = in.AsRegisterPairLow<Register>();
2885 Register high = in.AsRegisterPairHigh<Register>();
2886 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2887 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002888 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002889 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002890 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2891 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002892
Roland Levillain682393c2015-04-14 15:57:52 +01002893 // temp_d = int-to-double(high)
2894 __ vmovsr(temp_s, high);
2895 __ vcvtdi(temp_d, temp_s);
2896 // constant_d = k2Pow32EncodingForDouble
2897 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2898 // out_d = unsigned-to-double(low)
2899 __ vmovsr(out_s, low);
2900 __ vcvtdu(out_d, out_s);
2901 // out_d += temp_d * constant_d
2902 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002903 break;
2904 }
2905
Roland Levillaincff13742014-11-17 14:32:17 +00002906 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002907 // Processing a Dex `float-to-double' instruction.
2908 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2909 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002910 break;
2911
2912 default:
2913 LOG(FATAL) << "Unexpected type conversion from " << input_type
2914 << " to " << result_type;
2915 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002916 break;
2917
2918 default:
2919 LOG(FATAL) << "Unexpected type conversion from " << input_type
2920 << " to " << result_type;
2921 }
2922}
2923
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002924void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002925 LocationSummary* locations =
2926 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002927 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002928 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002929 locations->SetInAt(0, Location::RequiresRegister());
2930 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002931 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2932 break;
2933 }
2934
2935 case Primitive::kPrimLong: {
2936 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002937 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002938 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002939 break;
2940 }
2941
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002942 case Primitive::kPrimFloat:
2943 case Primitive::kPrimDouble: {
2944 locations->SetInAt(0, Location::RequiresFpuRegister());
2945 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002946 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002947 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002948 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002949
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002950 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002951 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002952 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002953}
2954
2955void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2956 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002957 Location out = locations->Out();
2958 Location first = locations->InAt(0);
2959 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002960 switch (add->GetResultType()) {
2961 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002962 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002963 __ add(out.AsRegister<Register>(),
2964 first.AsRegister<Register>(),
2965 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002966 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002967 __ AddConstant(out.AsRegister<Register>(),
2968 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002969 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002970 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002971 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002972
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002973 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002974 if (second.IsConstant()) {
2975 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2976 GenerateAddLongConst(out, first, value);
2977 } else {
2978 DCHECK(second.IsRegisterPair());
2979 __ adds(out.AsRegisterPairLow<Register>(),
2980 first.AsRegisterPairLow<Register>(),
2981 ShifterOperand(second.AsRegisterPairLow<Register>()));
2982 __ adc(out.AsRegisterPairHigh<Register>(),
2983 first.AsRegisterPairHigh<Register>(),
2984 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2985 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002986 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002987 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002988
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002989 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002990 __ vadds(out.AsFpuRegister<SRegister>(),
2991 first.AsFpuRegister<SRegister>(),
2992 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002993 break;
2994
2995 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002996 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2997 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2998 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002999 break;
3000
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003001 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003002 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003003 }
3004}
3005
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003006void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003007 LocationSummary* locations =
3008 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003009 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003010 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003011 locations->SetInAt(0, Location::RequiresRegister());
3012 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003013 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3014 break;
3015 }
3016
3017 case Primitive::kPrimLong: {
3018 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003019 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003020 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003021 break;
3022 }
Calin Juravle11351682014-10-23 15:38:15 +01003023 case Primitive::kPrimFloat:
3024 case Primitive::kPrimDouble: {
3025 locations->SetInAt(0, Location::RequiresFpuRegister());
3026 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003027 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003028 break;
Calin Juravle11351682014-10-23 15:38:15 +01003029 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003030 default:
Calin Juravle11351682014-10-23 15:38:15 +01003031 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003032 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003033}
3034
3035void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3036 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003037 Location out = locations->Out();
3038 Location first = locations->InAt(0);
3039 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003040 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003041 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003042 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003043 __ sub(out.AsRegister<Register>(),
3044 first.AsRegister<Register>(),
3045 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003046 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003047 __ AddConstant(out.AsRegister<Register>(),
3048 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003049 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003050 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003051 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003052 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003053
Calin Juravle11351682014-10-23 15:38:15 +01003054 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003055 if (second.IsConstant()) {
3056 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3057 GenerateAddLongConst(out, first, -value);
3058 } else {
3059 DCHECK(second.IsRegisterPair());
3060 __ subs(out.AsRegisterPairLow<Register>(),
3061 first.AsRegisterPairLow<Register>(),
3062 ShifterOperand(second.AsRegisterPairLow<Register>()));
3063 __ sbc(out.AsRegisterPairHigh<Register>(),
3064 first.AsRegisterPairHigh<Register>(),
3065 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3066 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003067 break;
Calin Juravle11351682014-10-23 15:38:15 +01003068 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003069
Calin Juravle11351682014-10-23 15:38:15 +01003070 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003071 __ vsubs(out.AsFpuRegister<SRegister>(),
3072 first.AsFpuRegister<SRegister>(),
3073 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003074 break;
Calin Juravle11351682014-10-23 15:38:15 +01003075 }
3076
3077 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003078 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3079 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3080 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003081 break;
3082 }
3083
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003084
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003085 default:
Calin Juravle11351682014-10-23 15:38:15 +01003086 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003087 }
3088}
3089
Calin Juravle34bacdf2014-10-07 20:23:36 +01003090void LocationsBuilderARM::VisitMul(HMul* mul) {
3091 LocationSummary* locations =
3092 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3093 switch (mul->GetResultType()) {
3094 case Primitive::kPrimInt:
3095 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003096 locations->SetInAt(0, Location::RequiresRegister());
3097 locations->SetInAt(1, Location::RequiresRegister());
3098 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003099 break;
3100 }
3101
Calin Juravleb5bfa962014-10-21 18:02:24 +01003102 case Primitive::kPrimFloat:
3103 case Primitive::kPrimDouble: {
3104 locations->SetInAt(0, Location::RequiresFpuRegister());
3105 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003106 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003107 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003108 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003109
3110 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003111 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003112 }
3113}
3114
3115void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3116 LocationSummary* locations = mul->GetLocations();
3117 Location out = locations->Out();
3118 Location first = locations->InAt(0);
3119 Location second = locations->InAt(1);
3120 switch (mul->GetResultType()) {
3121 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003122 __ mul(out.AsRegister<Register>(),
3123 first.AsRegister<Register>(),
3124 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003125 break;
3126 }
3127 case Primitive::kPrimLong: {
3128 Register out_hi = out.AsRegisterPairHigh<Register>();
3129 Register out_lo = out.AsRegisterPairLow<Register>();
3130 Register in1_hi = first.AsRegisterPairHigh<Register>();
3131 Register in1_lo = first.AsRegisterPairLow<Register>();
3132 Register in2_hi = second.AsRegisterPairHigh<Register>();
3133 Register in2_lo = second.AsRegisterPairLow<Register>();
3134
3135 // Extra checks to protect caused by the existence of R1_R2.
3136 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3137 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3138 DCHECK_NE(out_hi, in1_lo);
3139 DCHECK_NE(out_hi, in2_lo);
3140
3141 // input: in1 - 64 bits, in2 - 64 bits
3142 // output: out
3143 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3144 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3145 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3146
3147 // IP <- in1.lo * in2.hi
3148 __ mul(IP, in1_lo, in2_hi);
3149 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3150 __ mla(out_hi, in1_hi, in2_lo, IP);
3151 // out.lo <- (in1.lo * in2.lo)[31:0];
3152 __ umull(out_lo, IP, in1_lo, in2_lo);
3153 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3154 __ add(out_hi, out_hi, ShifterOperand(IP));
3155 break;
3156 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003157
3158 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003159 __ vmuls(out.AsFpuRegister<SRegister>(),
3160 first.AsFpuRegister<SRegister>(),
3161 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003162 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003163 }
3164
3165 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003166 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3167 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3168 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003169 break;
3170 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003171
3172 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003173 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003174 }
3175}
3176
Zheng Xuc6667102015-05-15 16:08:45 +08003177void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3178 DCHECK(instruction->IsDiv() || instruction->IsRem());
3179 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3180
3181 LocationSummary* locations = instruction->GetLocations();
3182 Location second = locations->InAt(1);
3183 DCHECK(second.IsConstant());
3184
3185 Register out = locations->Out().AsRegister<Register>();
3186 Register dividend = locations->InAt(0).AsRegister<Register>();
3187 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3188 DCHECK(imm == 1 || imm == -1);
3189
3190 if (instruction->IsRem()) {
3191 __ LoadImmediate(out, 0);
3192 } else {
3193 if (imm == 1) {
3194 __ Mov(out, dividend);
3195 } else {
3196 __ rsb(out, dividend, ShifterOperand(0));
3197 }
3198 }
3199}
3200
3201void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3202 DCHECK(instruction->IsDiv() || instruction->IsRem());
3203 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3204
3205 LocationSummary* locations = instruction->GetLocations();
3206 Location second = locations->InAt(1);
3207 DCHECK(second.IsConstant());
3208
3209 Register out = locations->Out().AsRegister<Register>();
3210 Register dividend = locations->InAt(0).AsRegister<Register>();
3211 Register temp = locations->GetTemp(0).AsRegister<Register>();
3212 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003213 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003214 int ctz_imm = CTZ(abs_imm);
3215
3216 if (ctz_imm == 1) {
3217 __ Lsr(temp, dividend, 32 - ctz_imm);
3218 } else {
3219 __ Asr(temp, dividend, 31);
3220 __ Lsr(temp, temp, 32 - ctz_imm);
3221 }
3222 __ add(out, temp, ShifterOperand(dividend));
3223
3224 if (instruction->IsDiv()) {
3225 __ Asr(out, out, ctz_imm);
3226 if (imm < 0) {
3227 __ rsb(out, out, ShifterOperand(0));
3228 }
3229 } else {
3230 __ ubfx(out, out, 0, ctz_imm);
3231 __ sub(out, out, ShifterOperand(temp));
3232 }
3233}
3234
3235void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3236 DCHECK(instruction->IsDiv() || instruction->IsRem());
3237 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3238
3239 LocationSummary* locations = instruction->GetLocations();
3240 Location second = locations->InAt(1);
3241 DCHECK(second.IsConstant());
3242
3243 Register out = locations->Out().AsRegister<Register>();
3244 Register dividend = locations->InAt(0).AsRegister<Register>();
3245 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3246 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3247 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3248
3249 int64_t magic;
3250 int shift;
3251 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3252
3253 __ LoadImmediate(temp1, magic);
3254 __ smull(temp2, temp1, dividend, temp1);
3255
3256 if (imm > 0 && magic < 0) {
3257 __ add(temp1, temp1, ShifterOperand(dividend));
3258 } else if (imm < 0 && magic > 0) {
3259 __ sub(temp1, temp1, ShifterOperand(dividend));
3260 }
3261
3262 if (shift != 0) {
3263 __ Asr(temp1, temp1, shift);
3264 }
3265
3266 if (instruction->IsDiv()) {
3267 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3268 } else {
3269 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3270 // TODO: Strength reduction for mls.
3271 __ LoadImmediate(temp2, imm);
3272 __ mls(out, temp1, temp2, dividend);
3273 }
3274}
3275
3276void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3277 DCHECK(instruction->IsDiv() || instruction->IsRem());
3278 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3279
3280 LocationSummary* locations = instruction->GetLocations();
3281 Location second = locations->InAt(1);
3282 DCHECK(second.IsConstant());
3283
3284 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3285 if (imm == 0) {
3286 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3287 } else if (imm == 1 || imm == -1) {
3288 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003289 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003290 DivRemByPowerOfTwo(instruction);
3291 } else {
3292 DCHECK(imm <= -2 || imm >= 2);
3293 GenerateDivRemWithAnyConstant(instruction);
3294 }
3295}
3296
Calin Juravle7c4954d2014-10-28 16:57:40 +00003297void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003298 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3299 if (div->GetResultType() == Primitive::kPrimLong) {
3300 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003301 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003302 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3303 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003304 } else if (div->GetResultType() == Primitive::kPrimInt &&
3305 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3306 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003307 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003308 }
3309
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003310 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3311
Calin Juravle7c4954d2014-10-28 16:57:40 +00003312 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003313 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003314 if (div->InputAt(1)->IsConstant()) {
3315 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003316 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003317 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003318 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3319 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003320 // No temp register required.
3321 } else {
3322 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003323 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003324 locations->AddTemp(Location::RequiresRegister());
3325 }
3326 }
3327 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003328 locations->SetInAt(0, Location::RequiresRegister());
3329 locations->SetInAt(1, Location::RequiresRegister());
3330 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3331 } else {
3332 InvokeRuntimeCallingConvention calling_convention;
3333 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3334 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3335 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3336 // we only need the former.
3337 locations->SetOut(Location::RegisterLocation(R0));
3338 }
Calin Juravled0d48522014-11-04 16:40:20 +00003339 break;
3340 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003341 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003342 InvokeRuntimeCallingConvention calling_convention;
3343 locations->SetInAt(0, Location::RegisterPairLocation(
3344 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3345 locations->SetInAt(1, Location::RegisterPairLocation(
3346 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003347 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003348 break;
3349 }
3350 case Primitive::kPrimFloat:
3351 case Primitive::kPrimDouble: {
3352 locations->SetInAt(0, Location::RequiresFpuRegister());
3353 locations->SetInAt(1, Location::RequiresFpuRegister());
3354 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3355 break;
3356 }
3357
3358 default:
3359 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3360 }
3361}
3362
3363void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3364 LocationSummary* locations = div->GetLocations();
3365 Location out = locations->Out();
3366 Location first = locations->InAt(0);
3367 Location second = locations->InAt(1);
3368
3369 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003370 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003371 if (second.IsConstant()) {
3372 GenerateDivRemConstantIntegral(div);
3373 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003374 __ sdiv(out.AsRegister<Register>(),
3375 first.AsRegister<Register>(),
3376 second.AsRegister<Register>());
3377 } else {
3378 InvokeRuntimeCallingConvention calling_convention;
3379 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3380 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3381 DCHECK_EQ(R0, out.AsRegister<Register>());
3382
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003383 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003384 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003385 }
Calin Juravled0d48522014-11-04 16:40:20 +00003386 break;
3387 }
3388
Calin Juravle7c4954d2014-10-28 16:57:40 +00003389 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003390 InvokeRuntimeCallingConvention calling_convention;
3391 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3392 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3393 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3394 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3395 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003396 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003397
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003398 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003399 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003400 break;
3401 }
3402
3403 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003404 __ vdivs(out.AsFpuRegister<SRegister>(),
3405 first.AsFpuRegister<SRegister>(),
3406 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003407 break;
3408 }
3409
3410 case Primitive::kPrimDouble: {
3411 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3412 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3413 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3414 break;
3415 }
3416
3417 default:
3418 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3419 }
3420}
3421
Calin Juravlebacfec32014-11-14 15:54:36 +00003422void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003423 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003424
3425 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003426 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003427 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3428 // sdiv will be replaced by other instruction sequence.
3429 call_kind = LocationSummary::kNoCall;
3430 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3431 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003432 // Have hardware divide instruction for int, do it with three instructions.
3433 call_kind = LocationSummary::kNoCall;
3434 }
3435
Calin Juravlebacfec32014-11-14 15:54:36 +00003436 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3437
Calin Juravled2ec87d2014-12-08 14:24:46 +00003438 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003439 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003440 if (rem->InputAt(1)->IsConstant()) {
3441 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003442 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003443 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003444 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3445 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003446 // No temp register required.
3447 } else {
3448 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003449 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003450 locations->AddTemp(Location::RequiresRegister());
3451 }
3452 }
3453 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003454 locations->SetInAt(0, Location::RequiresRegister());
3455 locations->SetInAt(1, Location::RequiresRegister());
3456 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3457 locations->AddTemp(Location::RequiresRegister());
3458 } else {
3459 InvokeRuntimeCallingConvention calling_convention;
3460 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3461 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3462 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3463 // we only need the latter.
3464 locations->SetOut(Location::RegisterLocation(R1));
3465 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003466 break;
3467 }
3468 case Primitive::kPrimLong: {
3469 InvokeRuntimeCallingConvention calling_convention;
3470 locations->SetInAt(0, Location::RegisterPairLocation(
3471 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3472 locations->SetInAt(1, Location::RegisterPairLocation(
3473 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3474 // The runtime helper puts the output in R2,R3.
3475 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3476 break;
3477 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003478 case Primitive::kPrimFloat: {
3479 InvokeRuntimeCallingConvention calling_convention;
3480 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3481 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3482 locations->SetOut(Location::FpuRegisterLocation(S0));
3483 break;
3484 }
3485
Calin Juravlebacfec32014-11-14 15:54:36 +00003486 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003487 InvokeRuntimeCallingConvention calling_convention;
3488 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3489 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3490 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3491 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3492 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003493 break;
3494 }
3495
3496 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003497 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003498 }
3499}
3500
3501void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3502 LocationSummary* locations = rem->GetLocations();
3503 Location out = locations->Out();
3504 Location first = locations->InAt(0);
3505 Location second = locations->InAt(1);
3506
Calin Juravled2ec87d2014-12-08 14:24:46 +00003507 Primitive::Type type = rem->GetResultType();
3508 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003509 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003510 if (second.IsConstant()) {
3511 GenerateDivRemConstantIntegral(rem);
3512 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003513 Register reg1 = first.AsRegister<Register>();
3514 Register reg2 = second.AsRegister<Register>();
3515 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003516
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003517 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003518 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003519 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003520 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003521 } else {
3522 InvokeRuntimeCallingConvention calling_convention;
3523 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3524 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3525 DCHECK_EQ(R1, out.AsRegister<Register>());
3526
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003527 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003528 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003529 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003530 break;
3531 }
3532
3533 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003534 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003535 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003536 break;
3537 }
3538
Calin Juravled2ec87d2014-12-08 14:24:46 +00003539 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003540 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003541 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003542 break;
3543 }
3544
Calin Juravlebacfec32014-11-14 15:54:36 +00003545 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003546 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003547 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003548 break;
3549 }
3550
3551 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003552 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003553 }
3554}
3555
Calin Juravled0d48522014-11-04 16:40:20 +00003556void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003557 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003558 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003559}
3560
3561void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003562 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003563 codegen_->AddSlowPath(slow_path);
3564
3565 LocationSummary* locations = instruction->GetLocations();
3566 Location value = locations->InAt(0);
3567
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003568 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003569 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003570 case Primitive::kPrimByte:
3571 case Primitive::kPrimChar:
3572 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003573 case Primitive::kPrimInt: {
3574 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003575 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003576 } else {
3577 DCHECK(value.IsConstant()) << value;
3578 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3579 __ b(slow_path->GetEntryLabel());
3580 }
3581 }
3582 break;
3583 }
3584 case Primitive::kPrimLong: {
3585 if (value.IsRegisterPair()) {
3586 __ orrs(IP,
3587 value.AsRegisterPairLow<Register>(),
3588 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3589 __ b(slow_path->GetEntryLabel(), EQ);
3590 } else {
3591 DCHECK(value.IsConstant()) << value;
3592 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3593 __ b(slow_path->GetEntryLabel());
3594 }
3595 }
3596 break;
3597 default:
3598 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3599 }
3600 }
Calin Juravled0d48522014-11-04 16:40:20 +00003601}
3602
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003603void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3604 Register in = locations->InAt(0).AsRegister<Register>();
3605 Location rhs = locations->InAt(1);
3606 Register out = locations->Out().AsRegister<Register>();
3607
3608 if (rhs.IsConstant()) {
3609 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3610 // so map all rotations to a +ve. equivalent in that range.
3611 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3612 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3613 if (rot) {
3614 // Rotate, mapping left rotations to right equivalents if necessary.
3615 // (e.g. left by 2 bits == right by 30.)
3616 __ Ror(out, in, rot);
3617 } else if (out != in) {
3618 __ Mov(out, in);
3619 }
3620 } else {
3621 __ Ror(out, in, rhs.AsRegister<Register>());
3622 }
3623}
3624
3625// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3626// rotates by swapping input regs (effectively rotating by the first 32-bits of
3627// a larger rotation) or flipping direction (thus treating larger right/left
3628// rotations as sub-word sized rotations in the other direction) as appropriate.
3629void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3630 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3631 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3632 Location rhs = locations->InAt(1);
3633 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3634 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3635
3636 if (rhs.IsConstant()) {
3637 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3638 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003639 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003640 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3641 // logic below to a simple pair of binary orr.
3642 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3643 if (rot >= kArmBitsPerWord) {
3644 rot -= kArmBitsPerWord;
3645 std::swap(in_reg_hi, in_reg_lo);
3646 }
3647 // Rotate, or mov to out for zero or word size rotations.
3648 if (rot != 0u) {
3649 __ Lsr(out_reg_hi, in_reg_hi, rot);
3650 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3651 __ Lsr(out_reg_lo, in_reg_lo, rot);
3652 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3653 } else {
3654 __ Mov(out_reg_lo, in_reg_lo);
3655 __ Mov(out_reg_hi, in_reg_hi);
3656 }
3657 } else {
3658 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3659 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3660 Label end;
3661 Label shift_by_32_plus_shift_right;
3662
3663 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3664 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3665 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3666 __ b(&shift_by_32_plus_shift_right, CC);
3667
3668 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3669 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3670 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3671 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3672 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3673 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3674 __ Lsr(shift_left, in_reg_hi, shift_right);
3675 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3676 __ b(&end);
3677
3678 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3679 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3680 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3681 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3682 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3683 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3684 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3685 __ Lsl(shift_right, in_reg_hi, shift_left);
3686 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3687
3688 __ Bind(&end);
3689 }
3690}
Roland Levillain22c49222016-03-18 14:04:28 +00003691
3692void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003693 LocationSummary* locations =
3694 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3695 switch (ror->GetResultType()) {
3696 case Primitive::kPrimInt: {
3697 locations->SetInAt(0, Location::RequiresRegister());
3698 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3699 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3700 break;
3701 }
3702 case Primitive::kPrimLong: {
3703 locations->SetInAt(0, Location::RequiresRegister());
3704 if (ror->InputAt(1)->IsConstant()) {
3705 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3706 } else {
3707 locations->SetInAt(1, Location::RequiresRegister());
3708 locations->AddTemp(Location::RequiresRegister());
3709 locations->AddTemp(Location::RequiresRegister());
3710 }
3711 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3712 break;
3713 }
3714 default:
3715 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3716 }
3717}
3718
Roland Levillain22c49222016-03-18 14:04:28 +00003719void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003720 LocationSummary* locations = ror->GetLocations();
3721 Primitive::Type type = ror->GetResultType();
3722 switch (type) {
3723 case Primitive::kPrimInt: {
3724 HandleIntegerRotate(locations);
3725 break;
3726 }
3727 case Primitive::kPrimLong: {
3728 HandleLongRotate(locations);
3729 break;
3730 }
3731 default:
3732 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003733 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003734 }
3735}
3736
Calin Juravle9aec02f2014-11-18 23:06:35 +00003737void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3738 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3739
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003740 LocationSummary* locations =
3741 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003742
3743 switch (op->GetResultType()) {
3744 case Primitive::kPrimInt: {
3745 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003746 if (op->InputAt(1)->IsConstant()) {
3747 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3748 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3749 } else {
3750 locations->SetInAt(1, Location::RequiresRegister());
3751 // Make the output overlap, as it will be used to hold the masked
3752 // second input.
3753 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3754 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003755 break;
3756 }
3757 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003758 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003759 if (op->InputAt(1)->IsConstant()) {
3760 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3761 // For simplicity, use kOutputOverlap even though we only require that low registers
3762 // don't clash with high registers which the register allocator currently guarantees.
3763 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3764 } else {
3765 locations->SetInAt(1, Location::RequiresRegister());
3766 locations->AddTemp(Location::RequiresRegister());
3767 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3768 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003769 break;
3770 }
3771 default:
3772 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3773 }
3774}
3775
3776void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3777 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3778
3779 LocationSummary* locations = op->GetLocations();
3780 Location out = locations->Out();
3781 Location first = locations->InAt(0);
3782 Location second = locations->InAt(1);
3783
3784 Primitive::Type type = op->GetResultType();
3785 switch (type) {
3786 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003787 Register out_reg = out.AsRegister<Register>();
3788 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003789 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003790 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003791 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003792 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003793 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003794 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003795 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003796 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003797 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003798 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003799 }
3800 } else {
3801 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003802 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003803 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003804 __ Mov(out_reg, first_reg);
3805 } else if (op->IsShl()) {
3806 __ Lsl(out_reg, first_reg, shift_value);
3807 } else if (op->IsShr()) {
3808 __ Asr(out_reg, first_reg, shift_value);
3809 } else {
3810 __ Lsr(out_reg, first_reg, shift_value);
3811 }
3812 }
3813 break;
3814 }
3815 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003816 Register o_h = out.AsRegisterPairHigh<Register>();
3817 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003818
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003819 Register high = first.AsRegisterPairHigh<Register>();
3820 Register low = first.AsRegisterPairLow<Register>();
3821
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003822 if (second.IsRegister()) {
3823 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003824
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003825 Register second_reg = second.AsRegister<Register>();
3826
3827 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003828 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003829 // Shift the high part
3830 __ Lsl(o_h, high, o_l);
3831 // Shift the low part and `or` what overflew on the high part
3832 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3833 __ Lsr(temp, low, temp);
3834 __ orr(o_h, o_h, ShifterOperand(temp));
3835 // If the shift is > 32 bits, override the high part
3836 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3837 __ it(PL);
3838 __ Lsl(o_h, low, temp, PL);
3839 // Shift the low part
3840 __ Lsl(o_l, low, o_l);
3841 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003842 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003843 // Shift the low part
3844 __ Lsr(o_l, low, o_h);
3845 // Shift the high part and `or` what underflew on the low part
3846 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3847 __ Lsl(temp, high, temp);
3848 __ orr(o_l, o_l, ShifterOperand(temp));
3849 // If the shift is > 32 bits, override the low part
3850 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3851 __ it(PL);
3852 __ Asr(o_l, high, temp, PL);
3853 // Shift the high part
3854 __ Asr(o_h, high, o_h);
3855 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003856 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003857 // same as Shr except we use `Lsr`s and not `Asr`s
3858 __ Lsr(o_l, low, o_h);
3859 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3860 __ Lsl(temp, high, temp);
3861 __ orr(o_l, o_l, ShifterOperand(temp));
3862 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3863 __ it(PL);
3864 __ Lsr(o_l, high, temp, PL);
3865 __ Lsr(o_h, high, o_h);
3866 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003867 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003868 // Register allocator doesn't create partial overlap.
3869 DCHECK_NE(o_l, high);
3870 DCHECK_NE(o_h, low);
3871 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003872 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003873 if (shift_value > 32) {
3874 if (op->IsShl()) {
3875 __ Lsl(o_h, low, shift_value - 32);
3876 __ LoadImmediate(o_l, 0);
3877 } else if (op->IsShr()) {
3878 __ Asr(o_l, high, shift_value - 32);
3879 __ Asr(o_h, high, 31);
3880 } else {
3881 __ Lsr(o_l, high, shift_value - 32);
3882 __ LoadImmediate(o_h, 0);
3883 }
3884 } else if (shift_value == 32) {
3885 if (op->IsShl()) {
3886 __ mov(o_h, ShifterOperand(low));
3887 __ LoadImmediate(o_l, 0);
3888 } else if (op->IsShr()) {
3889 __ mov(o_l, ShifterOperand(high));
3890 __ Asr(o_h, high, 31);
3891 } else {
3892 __ mov(o_l, ShifterOperand(high));
3893 __ LoadImmediate(o_h, 0);
3894 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003895 } else if (shift_value == 1) {
3896 if (op->IsShl()) {
3897 __ Lsls(o_l, low, 1);
3898 __ adc(o_h, high, ShifterOperand(high));
3899 } else if (op->IsShr()) {
3900 __ Asrs(o_h, high, 1);
3901 __ Rrx(o_l, low);
3902 } else {
3903 __ Lsrs(o_h, high, 1);
3904 __ Rrx(o_l, low);
3905 }
3906 } else {
3907 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003908 if (op->IsShl()) {
3909 __ Lsl(o_h, high, shift_value);
3910 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3911 __ Lsl(o_l, low, shift_value);
3912 } else if (op->IsShr()) {
3913 __ Lsr(o_l, low, shift_value);
3914 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3915 __ Asr(o_h, high, shift_value);
3916 } else {
3917 __ Lsr(o_l, low, shift_value);
3918 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3919 __ Lsr(o_h, high, shift_value);
3920 }
3921 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003922 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003923 break;
3924 }
3925 default:
3926 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003927 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003928 }
3929}
3930
3931void LocationsBuilderARM::VisitShl(HShl* shl) {
3932 HandleShift(shl);
3933}
3934
3935void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3936 HandleShift(shl);
3937}
3938
3939void LocationsBuilderARM::VisitShr(HShr* shr) {
3940 HandleShift(shr);
3941}
3942
3943void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3944 HandleShift(shr);
3945}
3946
3947void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3948 HandleShift(ushr);
3949}
3950
3951void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3952 HandleShift(ushr);
3953}
3954
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003955void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003956 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003957 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003958 if (instruction->IsStringAlloc()) {
3959 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3960 } else {
3961 InvokeRuntimeCallingConvention calling_convention;
3962 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00003963 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003964 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003965}
3966
3967void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003968 // Note: if heap poisoning is enabled, the entry point takes cares
3969 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003970 if (instruction->IsStringAlloc()) {
3971 // String is allocated through StringFactory. Call NewEmptyString entry point.
3972 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003973 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003974 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3975 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3976 __ blx(LR);
3977 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3978 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003979 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00003980 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00003981 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003982}
3983
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003984void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3985 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003986 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003987 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003988 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003989 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3990 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003991}
3992
3993void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003994 // Note: if heap poisoning is enabled, the entry point takes cares
3995 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003996 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
3997 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003998}
3999
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004000void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004001 LocationSummary* locations =
4002 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004003 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4004 if (location.IsStackSlot()) {
4005 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4006 } else if (location.IsDoubleStackSlot()) {
4007 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004008 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004009 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004010}
4011
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004012void InstructionCodeGeneratorARM::VisitParameterValue(
4013 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004014 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004015}
4016
4017void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4018 LocationSummary* locations =
4019 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4020 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4021}
4022
4023void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4024 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004025}
4026
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004027void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004028 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004029 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004030 locations->SetInAt(0, Location::RequiresRegister());
4031 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004032}
4033
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004034void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4035 LocationSummary* locations = not_->GetLocations();
4036 Location out = locations->Out();
4037 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004038 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004039 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004040 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004041 break;
4042
4043 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004044 __ mvn(out.AsRegisterPairLow<Register>(),
4045 ShifterOperand(in.AsRegisterPairLow<Register>()));
4046 __ mvn(out.AsRegisterPairHigh<Register>(),
4047 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004048 break;
4049
4050 default:
4051 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4052 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004053}
4054
David Brazdil66d126e2015-04-03 16:02:44 +01004055void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4056 LocationSummary* locations =
4057 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4058 locations->SetInAt(0, Location::RequiresRegister());
4059 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4060}
4061
4062void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004063 LocationSummary* locations = bool_not->GetLocations();
4064 Location out = locations->Out();
4065 Location in = locations->InAt(0);
4066 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4067}
4068
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004069void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004070 LocationSummary* locations =
4071 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004072 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004073 case Primitive::kPrimBoolean:
4074 case Primitive::kPrimByte:
4075 case Primitive::kPrimShort:
4076 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004077 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004078 case Primitive::kPrimLong: {
4079 locations->SetInAt(0, Location::RequiresRegister());
4080 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004081 // Output overlaps because it is written before doing the low comparison.
4082 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004083 break;
4084 }
4085 case Primitive::kPrimFloat:
4086 case Primitive::kPrimDouble: {
4087 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004088 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004089 locations->SetOut(Location::RequiresRegister());
4090 break;
4091 }
4092 default:
4093 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4094 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004095}
4096
4097void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004098 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004099 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004100 Location left = locations->InAt(0);
4101 Location right = locations->InAt(1);
4102
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004103 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004104 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004105 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004106 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004107 case Primitive::kPrimBoolean:
4108 case Primitive::kPrimByte:
4109 case Primitive::kPrimShort:
4110 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004111 case Primitive::kPrimInt: {
4112 __ LoadImmediate(out, 0);
4113 __ cmp(left.AsRegister<Register>(),
4114 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4115 less_cond = LT;
4116 break;
4117 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004118 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004119 __ cmp(left.AsRegisterPairHigh<Register>(),
4120 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004121 __ b(&less, LT);
4122 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004123 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004124 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004125 __ cmp(left.AsRegisterPairLow<Register>(),
4126 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004127 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004128 break;
4129 }
4130 case Primitive::kPrimFloat:
4131 case Primitive::kPrimDouble: {
4132 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004133 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004134 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004135 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004136 break;
4137 }
4138 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004139 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004140 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004141 }
Aart Bika19616e2016-02-01 18:57:58 -08004142
Calin Juravleddb7df22014-11-25 20:56:51 +00004143 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004144 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004145
4146 __ Bind(&greater);
4147 __ LoadImmediate(out, 1);
4148 __ b(&done);
4149
4150 __ Bind(&less);
4151 __ LoadImmediate(out, -1);
4152
4153 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004154}
4155
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004156void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004157 LocationSummary* locations =
4158 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004159 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004160 locations->SetInAt(i, Location::Any());
4161 }
4162 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004163}
4164
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004165void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004166 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004167}
4168
Roland Levillainc9285912015-12-18 10:38:42 +00004169void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4170 // TODO (ported from quick): revisit ARM barrier kinds.
4171 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004172 switch (kind) {
4173 case MemBarrierKind::kAnyStore:
4174 case MemBarrierKind::kLoadAny:
4175 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004176 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004177 break;
4178 }
4179 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004180 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004181 break;
4182 }
4183 default:
4184 LOG(FATAL) << "Unexpected memory barrier " << kind;
4185 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004186 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004187}
4188
4189void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4190 uint32_t offset,
4191 Register out_lo,
4192 Register out_hi) {
4193 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004194 // Ensure `out_lo` is different from `addr`, so that loading
4195 // `offset` into `out_lo` does not clutter `addr`.
4196 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004197 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004198 __ add(IP, addr, ShifterOperand(out_lo));
4199 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004200 }
4201 __ ldrexd(out_lo, out_hi, addr);
4202}
4203
4204void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4205 uint32_t offset,
4206 Register value_lo,
4207 Register value_hi,
4208 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004209 Register temp2,
4210 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004211 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004212 if (offset != 0) {
4213 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004214 __ add(IP, addr, ShifterOperand(temp1));
4215 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004216 }
4217 __ Bind(&fail);
4218 // We need a load followed by store. (The address used in a STREX instruction must
4219 // be the same as the address in the most recently executed LDREX instruction.)
4220 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004221 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004222 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004223 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004224}
4225
4226void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4227 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4228
Nicolas Geoffray39468442014-09-02 15:17:15 +01004229 LocationSummary* locations =
4230 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004231 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004232
Calin Juravle52c48962014-12-16 17:02:57 +00004233 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004234 if (Primitive::IsFloatingPointType(field_type)) {
4235 locations->SetInAt(1, Location::RequiresFpuRegister());
4236 } else {
4237 locations->SetInAt(1, Location::RequiresRegister());
4238 }
4239
Calin Juravle52c48962014-12-16 17:02:57 +00004240 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004241 bool generate_volatile = field_info.IsVolatile()
4242 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004243 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004244 bool needs_write_barrier =
4245 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004246 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004247 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004248 if (needs_write_barrier) {
4249 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004250 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004251 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004252 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004253 // - registers need to be consecutive
4254 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004255 // We don't test for ARM yet, and the assertion makes sure that we
4256 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004257 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4258
4259 locations->AddTemp(Location::RequiresRegister());
4260 locations->AddTemp(Location::RequiresRegister());
4261 if (field_type == Primitive::kPrimDouble) {
4262 // For doubles we need two more registers to copy the value.
4263 locations->AddTemp(Location::RegisterLocation(R2));
4264 locations->AddTemp(Location::RegisterLocation(R3));
4265 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004266 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004267}
4268
Calin Juravle52c48962014-12-16 17:02:57 +00004269void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004270 const FieldInfo& field_info,
4271 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004272 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4273
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004274 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004275 Register base = locations->InAt(0).AsRegister<Register>();
4276 Location value = locations->InAt(1);
4277
4278 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004279 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004280 Primitive::Type field_type = field_info.GetFieldType();
4281 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004282 bool needs_write_barrier =
4283 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004284
4285 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004286 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004287 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004288
4289 switch (field_type) {
4290 case Primitive::kPrimBoolean:
4291 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004292 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004293 break;
4294 }
4295
4296 case Primitive::kPrimShort:
4297 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004298 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004299 break;
4300 }
4301
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004302 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004303 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004304 if (kPoisonHeapReferences && needs_write_barrier) {
4305 // Note that in the case where `value` is a null reference,
4306 // we do not enter this block, as a null reference does not
4307 // need poisoning.
4308 DCHECK_EQ(field_type, Primitive::kPrimNot);
4309 Register temp = locations->GetTemp(0).AsRegister<Register>();
4310 __ Mov(temp, value.AsRegister<Register>());
4311 __ PoisonHeapReference(temp);
4312 __ StoreToOffset(kStoreWord, temp, base, offset);
4313 } else {
4314 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4315 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004316 break;
4317 }
4318
4319 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004320 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004321 GenerateWideAtomicStore(base, offset,
4322 value.AsRegisterPairLow<Register>(),
4323 value.AsRegisterPairHigh<Register>(),
4324 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004325 locations->GetTemp(1).AsRegister<Register>(),
4326 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004327 } else {
4328 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004329 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004330 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004331 break;
4332 }
4333
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004334 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004335 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004336 break;
4337 }
4338
4339 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004340 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004341 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004342 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4343 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4344
4345 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4346
4347 GenerateWideAtomicStore(base, offset,
4348 value_reg_lo,
4349 value_reg_hi,
4350 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004351 locations->GetTemp(3).AsRegister<Register>(),
4352 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004353 } else {
4354 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004355 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004356 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004357 break;
4358 }
4359
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004360 case Primitive::kPrimVoid:
4361 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004362 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004363 }
Calin Juravle52c48962014-12-16 17:02:57 +00004364
Calin Juravle77520bc2015-01-12 18:45:46 +00004365 // Longs and doubles are handled in the switch.
4366 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4367 codegen_->MaybeRecordImplicitNullCheck(instruction);
4368 }
4369
4370 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4371 Register temp = locations->GetTemp(0).AsRegister<Register>();
4372 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004373 codegen_->MarkGCCard(
4374 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004375 }
4376
Calin Juravle52c48962014-12-16 17:02:57 +00004377 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004378 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004379 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004380}
4381
Calin Juravle52c48962014-12-16 17:02:57 +00004382void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4383 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004384
4385 bool object_field_get_with_read_barrier =
4386 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004387 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004388 new (GetGraph()->GetArena()) LocationSummary(instruction,
4389 object_field_get_with_read_barrier ?
4390 LocationSummary::kCallOnSlowPath :
4391 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004392 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004393 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004394 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004395 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004396
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004397 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004398 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004399 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004400 // The output overlaps in case of volatile long: we don't want the
4401 // code generated by GenerateWideAtomicLoad to overwrite the
4402 // object's location. Likewise, in the case of an object field get
4403 // with read barriers enabled, we do not want the load to overwrite
4404 // the object's location, as we need it to emit the read barrier.
4405 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4406 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004407
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004408 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4409 locations->SetOut(Location::RequiresFpuRegister());
4410 } else {
4411 locations->SetOut(Location::RequiresRegister(),
4412 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4413 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004414 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004415 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004416 // - registers need to be consecutive
4417 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004418 // We don't test for ARM yet, and the assertion makes sure that we
4419 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004420 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4421 locations->AddTemp(Location::RequiresRegister());
4422 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004423 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4424 // We need a temporary register for the read barrier marking slow
4425 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4426 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004427 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004428}
4429
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004430Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4431 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4432 << input->GetType();
4433 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4434 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4435 return Location::ConstantLocation(input->AsConstant());
4436 } else {
4437 return Location::RequiresFpuRegister();
4438 }
4439}
4440
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004441Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4442 Opcode opcode) {
4443 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4444 if (constant->IsConstant() &&
4445 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4446 return Location::ConstantLocation(constant->AsConstant());
4447 }
4448 return Location::RequiresRegister();
4449}
4450
4451bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4452 Opcode opcode) {
4453 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4454 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004455 Opcode high_opcode = opcode;
4456 SetCc low_set_cc = kCcDontCare;
4457 switch (opcode) {
4458 case SUB:
4459 // Flip the operation to an ADD.
4460 value = -value;
4461 opcode = ADD;
4462 FALLTHROUGH_INTENDED;
4463 case ADD:
4464 if (Low32Bits(value) == 0u) {
4465 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4466 }
4467 high_opcode = ADC;
4468 low_set_cc = kCcSet;
4469 break;
4470 default:
4471 break;
4472 }
4473 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4474 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004475 } else {
4476 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4477 }
4478}
4479
Vladimir Marko59751a72016-08-05 14:37:27 +01004480bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4481 Opcode opcode,
4482 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004483 ShifterOperand so;
4484 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004485 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004486 return true;
4487 }
4488 Opcode neg_opcode = kNoOperand;
4489 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004490 case AND: neg_opcode = BIC; value = ~value; break;
4491 case ORR: neg_opcode = ORN; value = ~value; break;
4492 case ADD: neg_opcode = SUB; value = -value; break;
4493 case ADC: neg_opcode = SBC; value = ~value; break;
4494 case SUB: neg_opcode = ADD; value = -value; break;
4495 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004496 default:
4497 return false;
4498 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004499 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004500}
4501
Calin Juravle52c48962014-12-16 17:02:57 +00004502void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4503 const FieldInfo& field_info) {
4504 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004505
Calin Juravle52c48962014-12-16 17:02:57 +00004506 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004507 Location base_loc = locations->InAt(0);
4508 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004509 Location out = locations->Out();
4510 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004511 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004512 Primitive::Type field_type = field_info.GetFieldType();
4513 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4514
4515 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004516 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004517 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004518 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004519
Roland Levillainc9285912015-12-18 10:38:42 +00004520 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004521 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004522 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004523
Roland Levillainc9285912015-12-18 10:38:42 +00004524 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004525 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004526 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004527
Roland Levillainc9285912015-12-18 10:38:42 +00004528 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004529 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004530 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004531
4532 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004533 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004534 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004535
4536 case Primitive::kPrimNot: {
4537 // /* HeapReference<Object> */ out = *(base + offset)
4538 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4539 Location temp_loc = locations->GetTemp(0);
4540 // Note that a potential implicit null check is handled in this
4541 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4542 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4543 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4544 if (is_volatile) {
4545 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4546 }
4547 } else {
4548 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4549 codegen_->MaybeRecordImplicitNullCheck(instruction);
4550 if (is_volatile) {
4551 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4552 }
4553 // If read barriers are enabled, emit read barriers other than
4554 // Baker's using a slow path (and also unpoison the loaded
4555 // reference, if heap poisoning is enabled).
4556 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4557 }
4558 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004559 }
4560
Roland Levillainc9285912015-12-18 10:38:42 +00004561 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004562 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004563 GenerateWideAtomicLoad(base, offset,
4564 out.AsRegisterPairLow<Register>(),
4565 out.AsRegisterPairHigh<Register>());
4566 } else {
4567 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4568 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004569 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004570
Roland Levillainc9285912015-12-18 10:38:42 +00004571 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004572 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004573 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004574
4575 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004576 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004577 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004578 Register lo = locations->GetTemp(0).AsRegister<Register>();
4579 Register hi = locations->GetTemp(1).AsRegister<Register>();
4580 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004581 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004582 __ vmovdrr(out_reg, lo, hi);
4583 } else {
4584 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004585 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004586 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004587 break;
4588 }
4589
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004590 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004591 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004592 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004593 }
Calin Juravle52c48962014-12-16 17:02:57 +00004594
Roland Levillainc9285912015-12-18 10:38:42 +00004595 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4596 // Potential implicit null checks, in the case of reference or
4597 // double fields, are handled in the previous switch statement.
4598 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004599 codegen_->MaybeRecordImplicitNullCheck(instruction);
4600 }
4601
Calin Juravle52c48962014-12-16 17:02:57 +00004602 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004603 if (field_type == Primitive::kPrimNot) {
4604 // Memory barriers, in the case of references, are also handled
4605 // in the previous switch statement.
4606 } else {
4607 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4608 }
Roland Levillain4d027112015-07-01 15:41:14 +01004609 }
Calin Juravle52c48962014-12-16 17:02:57 +00004610}
4611
4612void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4613 HandleFieldSet(instruction, instruction->GetFieldInfo());
4614}
4615
4616void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004617 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004618}
4619
4620void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4621 HandleFieldGet(instruction, instruction->GetFieldInfo());
4622}
4623
4624void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4625 HandleFieldGet(instruction, instruction->GetFieldInfo());
4626}
4627
4628void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4629 HandleFieldGet(instruction, instruction->GetFieldInfo());
4630}
4631
4632void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4633 HandleFieldGet(instruction, instruction->GetFieldInfo());
4634}
4635
4636void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4637 HandleFieldSet(instruction, instruction->GetFieldInfo());
4638}
4639
4640void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004641 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004642}
4643
Calin Juravlee460d1d2015-09-29 04:52:17 +01004644void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4645 HUnresolvedInstanceFieldGet* instruction) {
4646 FieldAccessCallingConventionARM calling_convention;
4647 codegen_->CreateUnresolvedFieldLocationSummary(
4648 instruction, instruction->GetFieldType(), calling_convention);
4649}
4650
4651void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4652 HUnresolvedInstanceFieldGet* instruction) {
4653 FieldAccessCallingConventionARM calling_convention;
4654 codegen_->GenerateUnresolvedFieldAccess(instruction,
4655 instruction->GetFieldType(),
4656 instruction->GetFieldIndex(),
4657 instruction->GetDexPc(),
4658 calling_convention);
4659}
4660
4661void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4662 HUnresolvedInstanceFieldSet* instruction) {
4663 FieldAccessCallingConventionARM calling_convention;
4664 codegen_->CreateUnresolvedFieldLocationSummary(
4665 instruction, instruction->GetFieldType(), calling_convention);
4666}
4667
4668void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4669 HUnresolvedInstanceFieldSet* instruction) {
4670 FieldAccessCallingConventionARM calling_convention;
4671 codegen_->GenerateUnresolvedFieldAccess(instruction,
4672 instruction->GetFieldType(),
4673 instruction->GetFieldIndex(),
4674 instruction->GetDexPc(),
4675 calling_convention);
4676}
4677
4678void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4679 HUnresolvedStaticFieldGet* instruction) {
4680 FieldAccessCallingConventionARM calling_convention;
4681 codegen_->CreateUnresolvedFieldLocationSummary(
4682 instruction, instruction->GetFieldType(), calling_convention);
4683}
4684
4685void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4686 HUnresolvedStaticFieldGet* instruction) {
4687 FieldAccessCallingConventionARM calling_convention;
4688 codegen_->GenerateUnresolvedFieldAccess(instruction,
4689 instruction->GetFieldType(),
4690 instruction->GetFieldIndex(),
4691 instruction->GetDexPc(),
4692 calling_convention);
4693}
4694
4695void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4696 HUnresolvedStaticFieldSet* instruction) {
4697 FieldAccessCallingConventionARM calling_convention;
4698 codegen_->CreateUnresolvedFieldLocationSummary(
4699 instruction, instruction->GetFieldType(), calling_convention);
4700}
4701
4702void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4703 HUnresolvedStaticFieldSet* instruction) {
4704 FieldAccessCallingConventionARM calling_convention;
4705 codegen_->GenerateUnresolvedFieldAccess(instruction,
4706 instruction->GetFieldType(),
4707 instruction->GetFieldIndex(),
4708 instruction->GetDexPc(),
4709 calling_convention);
4710}
4711
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004712void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004713 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4714 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004715}
4716
Calin Juravle2ae48182016-03-16 14:05:09 +00004717void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4718 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004719 return;
4720 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004721 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004722
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004723 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004724 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004725}
4726
Calin Juravle2ae48182016-03-16 14:05:09 +00004727void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004728 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004729 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004730
4731 LocationSummary* locations = instruction->GetLocations();
4732 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004733
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004734 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004735}
4736
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004737void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004738 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004739}
4740
Artem Serov6c916792016-07-11 14:02:34 +01004741static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4742 switch (type) {
4743 case Primitive::kPrimNot:
4744 return kLoadWord;
4745 case Primitive::kPrimBoolean:
4746 return kLoadUnsignedByte;
4747 case Primitive::kPrimByte:
4748 return kLoadSignedByte;
4749 case Primitive::kPrimChar:
4750 return kLoadUnsignedHalfword;
4751 case Primitive::kPrimShort:
4752 return kLoadSignedHalfword;
4753 case Primitive::kPrimInt:
4754 return kLoadWord;
4755 case Primitive::kPrimLong:
4756 return kLoadWordPair;
4757 case Primitive::kPrimFloat:
4758 return kLoadSWord;
4759 case Primitive::kPrimDouble:
4760 return kLoadDWord;
4761 default:
4762 LOG(FATAL) << "Unreachable type " << type;
4763 UNREACHABLE();
4764 }
4765}
4766
4767static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4768 switch (type) {
4769 case Primitive::kPrimNot:
4770 return kStoreWord;
4771 case Primitive::kPrimBoolean:
4772 case Primitive::kPrimByte:
4773 return kStoreByte;
4774 case Primitive::kPrimChar:
4775 case Primitive::kPrimShort:
4776 return kStoreHalfword;
4777 case Primitive::kPrimInt:
4778 return kStoreWord;
4779 case Primitive::kPrimLong:
4780 return kStoreWordPair;
4781 case Primitive::kPrimFloat:
4782 return kStoreSWord;
4783 case Primitive::kPrimDouble:
4784 return kStoreDWord;
4785 default:
4786 LOG(FATAL) << "Unreachable type " << type;
4787 UNREACHABLE();
4788 }
4789}
4790
4791void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4792 Location out_loc,
4793 Register base,
4794 Register reg_offset,
4795 Condition cond) {
4796 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4797 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4798
4799 switch (type) {
4800 case Primitive::kPrimByte:
4801 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4802 break;
4803 case Primitive::kPrimBoolean:
4804 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4805 break;
4806 case Primitive::kPrimShort:
4807 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4808 break;
4809 case Primitive::kPrimChar:
4810 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4811 break;
4812 case Primitive::kPrimNot:
4813 case Primitive::kPrimInt:
4814 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4815 break;
4816 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4817 case Primitive::kPrimLong:
4818 case Primitive::kPrimFloat:
4819 case Primitive::kPrimDouble:
4820 default:
4821 LOG(FATAL) << "Unreachable type " << type;
4822 UNREACHABLE();
4823 }
4824}
4825
4826void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4827 Location loc,
4828 Register base,
4829 Register reg_offset,
4830 Condition cond) {
4831 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4832 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4833
4834 switch (type) {
4835 case Primitive::kPrimByte:
4836 case Primitive::kPrimBoolean:
4837 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4838 break;
4839 case Primitive::kPrimShort:
4840 case Primitive::kPrimChar:
4841 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4842 break;
4843 case Primitive::kPrimNot:
4844 case Primitive::kPrimInt:
4845 __ str(loc.AsRegister<Register>(), mem_address, cond);
4846 break;
4847 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4848 case Primitive::kPrimLong:
4849 case Primitive::kPrimFloat:
4850 case Primitive::kPrimDouble:
4851 default:
4852 LOG(FATAL) << "Unreachable type " << type;
4853 UNREACHABLE();
4854 }
4855}
4856
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004857void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004858 bool object_array_get_with_read_barrier =
4859 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004860 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004861 new (GetGraph()->GetArena()) LocationSummary(instruction,
4862 object_array_get_with_read_barrier ?
4863 LocationSummary::kCallOnSlowPath :
4864 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004865 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004866 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004867 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004868 locations->SetInAt(0, Location::RequiresRegister());
4869 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004870 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4871 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4872 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004873 // The output overlaps in the case of an object array get with
4874 // read barriers enabled: we do not want the move to overwrite the
4875 // array's location, as we need it to emit the read barrier.
4876 locations->SetOut(
4877 Location::RequiresRegister(),
4878 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004879 }
Roland Levillainc9285912015-12-18 10:38:42 +00004880 // We need a temporary register for the read barrier marking slow
4881 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07004882 // Also need for String compression feature.
4883 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4884 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00004885 locations->AddTemp(Location::RequiresRegister());
4886 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004887}
4888
4889void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4890 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004891 Location obj_loc = locations->InAt(0);
4892 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004893 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004894 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004895 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004896 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07004897 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4898 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01004899 HInstruction* array_instr = instruction->GetArray();
4900 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01004901
Roland Levillain4d027112015-07-01 15:41:14 +01004902 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004903 case Primitive::kPrimBoolean:
4904 case Primitive::kPrimByte:
4905 case Primitive::kPrimShort:
4906 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004907 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004908 Register length;
4909 if (maybe_compressed_char_at) {
4910 length = locations->GetTemp(0).AsRegister<Register>();
4911 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4912 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4913 codegen_->MaybeRecordImplicitNullCheck(instruction);
4914 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004915 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004916 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07004917 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07004918 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004919 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4920 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4921 "Expecting 0=compressed, 1=uncompressed");
4922 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004923 __ LoadFromOffset(kLoadUnsignedByte,
4924 out_loc.AsRegister<Register>(),
4925 obj,
4926 data_offset + const_index);
4927 __ b(&done);
4928 __ Bind(&uncompressed_load);
4929 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4930 out_loc.AsRegister<Register>(),
4931 obj,
4932 data_offset + (const_index << 1));
4933 __ Bind(&done);
4934 } else {
4935 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01004936
jessicahandojo05765752016-09-09 19:01:32 -07004937 LoadOperandType load_type = GetLoadOperandType(type);
4938 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4939 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004940 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004941 Register temp = IP;
4942
4943 if (has_intermediate_address) {
4944 // We do not need to compute the intermediate address from the array: the
4945 // input instruction has done it already. See the comment in
4946 // `TryExtractArrayAccessAddress()`.
4947 if (kIsDebugBuild) {
4948 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4949 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4950 }
4951 temp = obj;
4952 } else {
4953 __ add(temp, obj, ShifterOperand(data_offset));
4954 }
jessicahandojo05765752016-09-09 19:01:32 -07004955 if (maybe_compressed_char_at) {
4956 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004957 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4958 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4959 "Expecting 0=compressed, 1=uncompressed");
4960 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004961 __ ldrb(out_loc.AsRegister<Register>(),
4962 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
4963 __ b(&done);
4964 __ Bind(&uncompressed_load);
4965 __ ldrh(out_loc.AsRegister<Register>(),
4966 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
4967 __ Bind(&done);
4968 } else {
4969 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4970 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004971 }
4972 break;
4973 }
4974
Roland Levillainc9285912015-12-18 10:38:42 +00004975 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00004976 // The read barrier instrumentation of object ArrayGet
4977 // instructions does not support the HIntermediateAddress
4978 // instruction.
4979 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4980
Roland Levillainc9285912015-12-18 10:38:42 +00004981 static_assert(
4982 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4983 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004984 // /* HeapReference<Object> */ out =
4985 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4986 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4987 Location temp = locations->GetTemp(0);
4988 // Note that a potential implicit null check is handled in this
4989 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4990 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4991 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4992 } else {
4993 Register out = out_loc.AsRegister<Register>();
4994 if (index.IsConstant()) {
4995 size_t offset =
4996 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4997 __ LoadFromOffset(kLoadWord, out, obj, offset);
4998 codegen_->MaybeRecordImplicitNullCheck(instruction);
4999 // If read barriers are enabled, emit read barriers other than
5000 // Baker's using a slow path (and also unpoison the loaded
5001 // reference, if heap poisoning is enabled).
5002 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5003 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005004 Register temp = IP;
5005
5006 if (has_intermediate_address) {
5007 // We do not need to compute the intermediate address from the array: the
5008 // input instruction has done it already. See the comment in
5009 // `TryExtractArrayAccessAddress()`.
5010 if (kIsDebugBuild) {
5011 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5012 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5013 }
5014 temp = obj;
5015 } else {
5016 __ add(temp, obj, ShifterOperand(data_offset));
5017 }
5018 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005019
Roland Levillainc9285912015-12-18 10:38:42 +00005020 codegen_->MaybeRecordImplicitNullCheck(instruction);
5021 // If read barriers are enabled, emit read barriers other than
5022 // Baker's using a slow path (and also unpoison the loaded
5023 // reference, if heap poisoning is enabled).
5024 codegen_->MaybeGenerateReadBarrierSlow(
5025 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5026 }
5027 }
5028 break;
5029 }
5030
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005031 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005032 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005033 size_t offset =
5034 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005035 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005036 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005037 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005038 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005039 }
5040 break;
5041 }
5042
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005043 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005044 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005045 if (index.IsConstant()) {
5046 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005047 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005048 } else {
5049 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005050 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005051 }
5052 break;
5053 }
5054
5055 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005056 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005057 if (index.IsConstant()) {
5058 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005059 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005060 } else {
5061 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005062 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005063 }
5064 break;
5065 }
5066
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005067 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005068 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005069 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005070 }
Roland Levillain4d027112015-07-01 15:41:14 +01005071
5072 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005073 // Potential implicit null checks, in the case of reference
5074 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005075 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005076 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005077 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005078}
5079
5080void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005081 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005082
5083 bool needs_write_barrier =
5084 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005085 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005086
Nicolas Geoffray39468442014-09-02 15:17:15 +01005087 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005088 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005089 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005090 LocationSummary::kCallOnSlowPath :
5091 LocationSummary::kNoCall);
5092
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005093 locations->SetInAt(0, Location::RequiresRegister());
5094 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5095 if (Primitive::IsFloatingPointType(value_type)) {
5096 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005097 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005098 locations->SetInAt(2, Location::RequiresRegister());
5099 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005100 if (needs_write_barrier) {
5101 // Temporary registers for the write barrier.
5102 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005103 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105}
5106
5107void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5108 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005109 Location array_loc = locations->InAt(0);
5110 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005111 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005112 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005113 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005114 bool needs_write_barrier =
5115 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005116 uint32_t data_offset =
5117 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5118 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005119 HInstruction* array_instr = instruction->GetArray();
5120 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005121
5122 switch (value_type) {
5123 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005124 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005125 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005126 case Primitive::kPrimChar:
5127 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005128 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005129 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5130 uint32_t full_offset =
5131 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5132 StoreOperandType store_type = GetStoreOperandType(value_type);
5133 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005134 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005135 Register temp = IP;
5136
5137 if (has_intermediate_address) {
5138 // We do not need to compute the intermediate address from the array: the
5139 // input instruction has done it already. See the comment in
5140 // `TryExtractArrayAccessAddress()`.
5141 if (kIsDebugBuild) {
5142 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5143 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5144 }
5145 temp = array;
5146 } else {
5147 __ add(temp, array, ShifterOperand(data_offset));
5148 }
Artem Serov6c916792016-07-11 14:02:34 +01005149 codegen_->StoreToShiftedRegOffset(value_type,
5150 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005151 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005152 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153 }
5154 break;
5155 }
5156
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005157 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005158 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005159 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5160 // See the comment in instruction_simplifier_shared.cc.
5161 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005162
5163 if (instruction->InputAt(2)->IsNullConstant()) {
5164 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005165 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005166 size_t offset =
5167 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005168 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005169 } else {
5170 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005171 __ add(IP, array, ShifterOperand(data_offset));
5172 codegen_->StoreToShiftedRegOffset(value_type,
5173 value_loc,
5174 IP,
5175 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005176 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005177 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005178 DCHECK(!needs_write_barrier);
5179 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005180 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005181 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005182
5183 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005184 Location temp1_loc = locations->GetTemp(0);
5185 Register temp1 = temp1_loc.AsRegister<Register>();
5186 Location temp2_loc = locations->GetTemp(1);
5187 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005188 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5189 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5190 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5191 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005192 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005193
Roland Levillain3b359c72015-11-17 19:35:12 +00005194 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005195 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5196 codegen_->AddSlowPath(slow_path);
5197 if (instruction->GetValueCanBeNull()) {
5198 Label non_zero;
5199 __ CompareAndBranchIfNonZero(value, &non_zero);
5200 if (index.IsConstant()) {
5201 size_t offset =
5202 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5203 __ StoreToOffset(kStoreWord, value, array, offset);
5204 } else {
5205 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005206 __ add(IP, array, ShifterOperand(data_offset));
5207 codegen_->StoreToShiftedRegOffset(value_type,
5208 value_loc,
5209 IP,
5210 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005211 }
5212 codegen_->MaybeRecordImplicitNullCheck(instruction);
5213 __ b(&done);
5214 __ Bind(&non_zero);
5215 }
5216
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005217 // Note that when read barriers are enabled, the type checks
5218 // are performed without read barriers. This is fine, even in
5219 // the case where a class object is in the from-space after
5220 // the flip, as a comparison involving such a type would not
5221 // produce a false positive; it may of course produce a false
5222 // negative, in which case we would take the ArraySet slow
5223 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005224
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005225 // /* HeapReference<Class> */ temp1 = array->klass_
5226 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5227 codegen_->MaybeRecordImplicitNullCheck(instruction);
5228 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005229
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005230 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5231 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5232 // /* HeapReference<Class> */ temp2 = value->klass_
5233 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5234 // If heap poisoning is enabled, no need to unpoison `temp1`
5235 // nor `temp2`, as we are comparing two poisoned references.
5236 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005237
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005238 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5239 Label do_put;
5240 __ b(&do_put, EQ);
5241 // If heap poisoning is enabled, the `temp1` reference has
5242 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005243 __ MaybeUnpoisonHeapReference(temp1);
5244
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005245 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5246 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5247 // If heap poisoning is enabled, no need to unpoison
5248 // `temp1`, as we are comparing against null below.
5249 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5250 __ Bind(&do_put);
5251 } else {
5252 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005253 }
5254 }
5255
Artem Serov6c916792016-07-11 14:02:34 +01005256 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005257 if (kPoisonHeapReferences) {
5258 // Note that in the case where `value` is a null reference,
5259 // we do not enter this block, as a null reference does not
5260 // need poisoning.
5261 DCHECK_EQ(value_type, Primitive::kPrimNot);
5262 __ Mov(temp1, value);
5263 __ PoisonHeapReference(temp1);
5264 source = temp1;
5265 }
5266
5267 if (index.IsConstant()) {
5268 size_t offset =
5269 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5270 __ StoreToOffset(kStoreWord, source, array, offset);
5271 } else {
5272 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005273
5274 __ add(IP, array, ShifterOperand(data_offset));
5275 codegen_->StoreToShiftedRegOffset(value_type,
5276 Location::RegisterLocation(source),
5277 IP,
5278 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005279 }
5280
Roland Levillain3b359c72015-11-17 19:35:12 +00005281 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005282 codegen_->MaybeRecordImplicitNullCheck(instruction);
5283 }
5284
5285 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5286
5287 if (done.IsLinked()) {
5288 __ Bind(&done);
5289 }
5290
5291 if (slow_path != nullptr) {
5292 __ Bind(slow_path->GetExitLabel());
5293 }
5294
5295 break;
5296 }
5297
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005299 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005301 size_t offset =
5302 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005303 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005304 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005305 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005306 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005307 }
5308 break;
5309 }
5310
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005311 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005312 Location value = locations->InAt(2);
5313 DCHECK(value.IsFpuRegister());
5314 if (index.IsConstant()) {
5315 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005316 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005317 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005318 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005319 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5320 }
5321 break;
5322 }
5323
5324 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005325 Location value = locations->InAt(2);
5326 DCHECK(value.IsFpuRegisterPair());
5327 if (index.IsConstant()) {
5328 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005329 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005330 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005331 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005332 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5333 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005334
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005335 break;
5336 }
5337
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005338 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005339 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005340 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005341 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005342
Roland Levillain80e67092016-01-08 16:04:55 +00005343 // Objects are handled in the switch.
5344 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005345 codegen_->MaybeRecordImplicitNullCheck(instruction);
5346 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005347}
5348
5349void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005350 LocationSummary* locations =
5351 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005352 locations->SetInAt(0, Location::RequiresRegister());
5353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005354}
5355
5356void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5357 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005358 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005359 Register obj = locations->InAt(0).AsRegister<Register>();
5360 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005362 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005363 // Mask out compression flag from String's array length.
5364 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005365 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07005366 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005367}
5368
Artem Serov328429f2016-07-06 16:23:04 +01005369void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005370 LocationSummary* locations =
5371 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5372
5373 locations->SetInAt(0, Location::RequiresRegister());
5374 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5375 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5376}
5377
5378void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5379 LocationSummary* locations = instruction->GetLocations();
5380 Location out = locations->Out();
5381 Location first = locations->InAt(0);
5382 Location second = locations->InAt(1);
5383
Artem Serov328429f2016-07-06 16:23:04 +01005384 if (second.IsRegister()) {
5385 __ add(out.AsRegister<Register>(),
5386 first.AsRegister<Register>(),
5387 ShifterOperand(second.AsRegister<Register>()));
5388 } else {
5389 __ AddConstant(out.AsRegister<Register>(),
5390 first.AsRegister<Register>(),
5391 second.GetConstant()->AsIntConstant()->GetValue());
5392 }
5393}
5394
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005395void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005396 RegisterSet caller_saves = RegisterSet::Empty();
5397 InvokeRuntimeCallingConvention calling_convention;
5398 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5399 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5400 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005401 locations->SetInAt(0, Location::RequiresRegister());
5402 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005403}
5404
5405void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5406 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005407 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005408 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005409 codegen_->AddSlowPath(slow_path);
5410
Roland Levillain271ab9c2014-11-27 15:23:57 +00005411 Register index = locations->InAt(0).AsRegister<Register>();
5412 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005413
5414 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005415 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005416}
5417
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005418void CodeGeneratorARM::MarkGCCard(Register temp,
5419 Register card,
5420 Register object,
5421 Register value,
5422 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005423 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005424 if (can_be_null) {
5425 __ CompareAndBranchIfZero(value, &is_null);
5426 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005427 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005428 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5429 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005430 if (can_be_null) {
5431 __ Bind(&is_null);
5432 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005433}
5434
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005435void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005436 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005437}
5438
5439void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005440 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5441}
5442
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005443void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005444 LocationSummary* locations =
5445 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005446 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005447}
5448
5449void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005450 HBasicBlock* block = instruction->GetBlock();
5451 if (block->GetLoopInformation() != nullptr) {
5452 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5453 // The back edge will generate the suspend check.
5454 return;
5455 }
5456 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5457 // The goto will generate the suspend check.
5458 return;
5459 }
5460 GenerateSuspendCheck(instruction, nullptr);
5461}
5462
5463void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5464 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005465 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005466 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5467 if (slow_path == nullptr) {
5468 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5469 instruction->SetSlowPath(slow_path);
5470 codegen_->AddSlowPath(slow_path);
5471 if (successor != nullptr) {
5472 DCHECK(successor->IsLoopHeader());
5473 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5474 }
5475 } else {
5476 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5477 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005478
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005479 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005480 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005481 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005482 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005483 __ Bind(slow_path->GetReturnLabel());
5484 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005485 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005486 __ b(slow_path->GetEntryLabel());
5487 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005488}
5489
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005490ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5491 return codegen_->GetAssembler();
5492}
5493
5494void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005495 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005496 Location source = move->GetSource();
5497 Location destination = move->GetDestination();
5498
5499 if (source.IsRegister()) {
5500 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005501 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005502 } else if (destination.IsFpuRegister()) {
5503 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005504 } else {
5505 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005506 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005507 SP, destination.GetStackIndex());
5508 }
5509 } else if (source.IsStackSlot()) {
5510 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005511 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005512 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005513 } else if (destination.IsFpuRegister()) {
5514 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005515 } else {
5516 DCHECK(destination.IsStackSlot());
5517 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5518 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5519 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005520 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005521 if (destination.IsRegister()) {
5522 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5523 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005524 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005525 } else {
5526 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005527 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5528 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005529 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005530 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005531 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5532 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005533 } else if (destination.IsRegisterPair()) {
5534 DCHECK(ExpectedPairLayout(destination));
5535 __ LoadFromOffset(
5536 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5537 } else {
5538 DCHECK(destination.IsFpuRegisterPair()) << destination;
5539 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5540 SP,
5541 source.GetStackIndex());
5542 }
5543 } else if (source.IsRegisterPair()) {
5544 if (destination.IsRegisterPair()) {
5545 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5546 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005547 } else if (destination.IsFpuRegisterPair()) {
5548 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5549 source.AsRegisterPairLow<Register>(),
5550 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005551 } else {
5552 DCHECK(destination.IsDoubleStackSlot()) << destination;
5553 DCHECK(ExpectedPairLayout(source));
5554 __ StoreToOffset(
5555 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5556 }
5557 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005558 if (destination.IsRegisterPair()) {
5559 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5560 destination.AsRegisterPairHigh<Register>(),
5561 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5562 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005563 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5564 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5565 } else {
5566 DCHECK(destination.IsDoubleStackSlot()) << destination;
5567 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5568 SP,
5569 destination.GetStackIndex());
5570 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005571 } else {
5572 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005573 HConstant* constant = source.GetConstant();
5574 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5575 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005576 if (destination.IsRegister()) {
5577 __ LoadImmediate(destination.AsRegister<Register>(), value);
5578 } else {
5579 DCHECK(destination.IsStackSlot());
5580 __ LoadImmediate(IP, value);
5581 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5582 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005583 } else if (constant->IsLongConstant()) {
5584 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005585 if (destination.IsRegisterPair()) {
5586 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5587 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005588 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005589 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005590 __ LoadImmediate(IP, Low32Bits(value));
5591 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5592 __ LoadImmediate(IP, High32Bits(value));
5593 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5594 }
5595 } else if (constant->IsDoubleConstant()) {
5596 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005597 if (destination.IsFpuRegisterPair()) {
5598 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005599 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005600 DCHECK(destination.IsDoubleStackSlot()) << destination;
5601 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005602 __ LoadImmediate(IP, Low32Bits(int_value));
5603 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5604 __ LoadImmediate(IP, High32Bits(int_value));
5605 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5606 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005607 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005608 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005609 float value = constant->AsFloatConstant()->GetValue();
5610 if (destination.IsFpuRegister()) {
5611 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5612 } else {
5613 DCHECK(destination.IsStackSlot());
5614 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5615 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5616 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005617 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005618 }
5619}
5620
5621void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5622 __ Mov(IP, reg);
5623 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5624 __ StoreToOffset(kStoreWord, IP, SP, mem);
5625}
5626
5627void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5628 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5629 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5630 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5631 SP, mem1 + stack_offset);
5632 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5633 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5634 SP, mem2 + stack_offset);
5635 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5636}
5637
5638void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005639 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005640 Location source = move->GetSource();
5641 Location destination = move->GetDestination();
5642
5643 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005644 DCHECK_NE(source.AsRegister<Register>(), IP);
5645 DCHECK_NE(destination.AsRegister<Register>(), IP);
5646 __ Mov(IP, source.AsRegister<Register>());
5647 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5648 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005649 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005650 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005651 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005652 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005653 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5654 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005655 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005656 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005657 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005658 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005659 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005660 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005661 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005662 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005663 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5664 destination.AsRegisterPairHigh<Register>(),
5665 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005666 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005667 Register low_reg = source.IsRegisterPair()
5668 ? source.AsRegisterPairLow<Register>()
5669 : destination.AsRegisterPairLow<Register>();
5670 int mem = source.IsRegisterPair()
5671 ? destination.GetStackIndex()
5672 : source.GetStackIndex();
5673 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005674 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005675 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005676 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005677 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005678 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5679 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005680 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005681 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005682 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005683 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5684 DRegister reg = source.IsFpuRegisterPair()
5685 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5686 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5687 int mem = source.IsFpuRegisterPair()
5688 ? destination.GetStackIndex()
5689 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005690 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005691 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005692 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005693 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5694 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5695 : destination.AsFpuRegister<SRegister>();
5696 int mem = source.IsFpuRegister()
5697 ? destination.GetStackIndex()
5698 : source.GetStackIndex();
5699
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005700 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005701 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005702 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005703 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005704 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5705 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005706 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005707 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005708 }
5709}
5710
5711void ParallelMoveResolverARM::SpillScratch(int reg) {
5712 __ Push(static_cast<Register>(reg));
5713}
5714
5715void ParallelMoveResolverARM::RestoreScratch(int reg) {
5716 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005717}
5718
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005719HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5720 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005721 switch (desired_class_load_kind) {
5722 case HLoadClass::LoadKind::kReferrersClass:
5723 break;
5724 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5725 DCHECK(!GetCompilerOptions().GetCompilePic());
5726 break;
5727 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5728 DCHECK(GetCompilerOptions().GetCompilePic());
5729 break;
5730 case HLoadClass::LoadKind::kBootImageAddress:
5731 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005732 case HLoadClass::LoadKind::kBssEntry:
5733 DCHECK(!Runtime::Current()->UseJitCompilation());
5734 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005735 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005736 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005737 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005738 case HLoadClass::LoadKind::kDexCacheViaMethod:
5739 break;
5740 }
5741 return desired_class_load_kind;
5742}
5743
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005744void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005745 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5746 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005747 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005748 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005749 cls,
5750 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005751 Location::RegisterLocation(R0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005752 return;
5753 }
Vladimir Marko41559982017-01-06 14:04:23 +00005754 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005755
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005756 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5757 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005758 ? LocationSummary::kCallOnSlowPath
5759 : LocationSummary::kNoCall;
5760 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005761 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005762 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005763 }
5764
Vladimir Marko41559982017-01-06 14:04:23 +00005765 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005766 locations->SetInAt(0, Location::RequiresRegister());
5767 }
5768 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005769}
5770
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005771// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5772// move.
5773void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005774 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5775 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5776 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005777 return;
5778 }
Vladimir Marko41559982017-01-06 14:04:23 +00005779 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005780
Vladimir Marko41559982017-01-06 14:04:23 +00005781 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005782 Location out_loc = locations->Out();
5783 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005784
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005785 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5786 ? kWithoutReadBarrier
5787 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005788 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005789 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005790 case HLoadClass::LoadKind::kReferrersClass: {
5791 DCHECK(!cls->CanCallRuntime());
5792 DCHECK(!cls->MustGenerateClinitCheck());
5793 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5794 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005795 GenerateGcRootFieldLoad(cls,
5796 out_loc,
5797 current_method,
5798 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005799 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005800 break;
5801 }
5802 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005803 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005804 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005805 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5806 cls->GetTypeIndex()));
5807 break;
5808 }
5809 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005810 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005811 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005812 CodeGeneratorARM::PcRelativePatchInfo* labels =
5813 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5814 __ BindTrackedLabel(&labels->movw_label);
5815 __ movw(out, /* placeholder */ 0u);
5816 __ BindTrackedLabel(&labels->movt_label);
5817 __ movt(out, /* placeholder */ 0u);
5818 __ BindTrackedLabel(&labels->add_pc_label);
5819 __ add(out, out, ShifterOperand(PC));
5820 break;
5821 }
5822 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005823 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005824 uint32_t address = dchecked_integral_cast<uint32_t>(
5825 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5826 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005827 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5828 break;
5829 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005830 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005831 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005832 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005833 __ BindTrackedLabel(&labels->movw_label);
5834 __ movw(out, /* placeholder */ 0u);
5835 __ BindTrackedLabel(&labels->movt_label);
5836 __ movt(out, /* placeholder */ 0u);
5837 __ BindTrackedLabel(&labels->add_pc_label);
5838 __ add(out, out, ShifterOperand(PC));
5839 GenerateGcRootFieldLoad(cls, out_loc, out, 0, kCompilerReadBarrierOption);
5840 generate_null_check = true;
5841 break;
5842 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005843 case HLoadClass::LoadKind::kJitTableAddress: {
5844 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5845 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005846 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005847 // /* GcRoot<mirror::Class> */ out = *out
5848 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005849 break;
5850 }
Vladimir Marko41559982017-01-06 14:04:23 +00005851 case HLoadClass::LoadKind::kDexCacheViaMethod:
5852 LOG(FATAL) << "UNREACHABLE";
5853 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005854 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005855
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005856 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5857 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005858 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005859 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5860 codegen_->AddSlowPath(slow_path);
5861 if (generate_null_check) {
5862 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5863 }
5864 if (cls->MustGenerateClinitCheck()) {
5865 GenerateClassInitializationCheck(slow_path, out);
5866 } else {
5867 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005868 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005869 }
5870}
5871
5872void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5873 LocationSummary* locations =
5874 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5875 locations->SetInAt(0, Location::RequiresRegister());
5876 if (check->HasUses()) {
5877 locations->SetOut(Location::SameAsFirstInput());
5878 }
5879}
5880
5881void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005882 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005883 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005884 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005885 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005886 GenerateClassInitializationCheck(slow_path,
5887 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005888}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005889
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005890void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005891 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005892 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5893 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5894 __ b(slow_path->GetEntryLabel(), LT);
5895 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5896 // properly. Therefore, we do a memory fence.
5897 __ dmb(ISH);
5898 __ Bind(slow_path->GetExitLabel());
5899}
5900
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005901HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5902 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005903 switch (desired_string_load_kind) {
5904 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5905 DCHECK(!GetCompilerOptions().GetCompilePic());
5906 break;
5907 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5908 DCHECK(GetCompilerOptions().GetCompilePic());
5909 break;
5910 case HLoadString::LoadKind::kBootImageAddress:
5911 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005912 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005913 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005914 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005915 case HLoadString::LoadKind::kJitTableAddress:
5916 DCHECK(Runtime::Current()->UseJitCompilation());
5917 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005918 case HLoadString::LoadKind::kDexCacheViaMethod:
5919 break;
5920 }
5921 return desired_string_load_kind;
5922}
5923
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005924void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005925 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005926 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005927 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005928 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005929 locations->SetOut(Location::RegisterLocation(R0));
5930 } else {
5931 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005932 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5933 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5934 // Rely on the pResolveString and/or marking to save everything, including temps.
5935 // Note that IP may theoretically be clobbered by saving/restoring the live register
5936 // (only one thanks to the custom calling convention), so we request a different temp.
5937 locations->AddTemp(Location::RequiresRegister());
5938 RegisterSet caller_saves = RegisterSet::Empty();
5939 InvokeRuntimeCallingConvention calling_convention;
5940 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5941 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5942 // that the the kPrimNot result register is the same as the first argument register.
5943 locations->SetCustomSlowPathCallerSaves(caller_saves);
5944 } else {
5945 // For non-Baker read barrier we have a temp-clobbering call.
5946 }
5947 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005948 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005949}
5950
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005951// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5952// move.
5953void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005954 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005955 Location out_loc = locations->Out();
5956 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005957 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005958
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005959 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005960 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005961 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005962 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5963 load->GetStringIndex()));
5964 return; // No dex cache slow path.
5965 }
5966 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005967 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005968 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005969 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005970 __ BindTrackedLabel(&labels->movw_label);
5971 __ movw(out, /* placeholder */ 0u);
5972 __ BindTrackedLabel(&labels->movt_label);
5973 __ movt(out, /* placeholder */ 0u);
5974 __ BindTrackedLabel(&labels->add_pc_label);
5975 __ add(out, out, ShifterOperand(PC));
5976 return; // No dex cache slow path.
5977 }
5978 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005979 uint32_t address = dchecked_integral_cast<uint32_t>(
5980 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5981 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005982 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5983 return; // No dex cache slow path.
5984 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005985 case HLoadString::LoadKind::kBssEntry: {
5986 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005987 Register temp = locations->GetTemp(0).AsRegister<Register>();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005988 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005989 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005990 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005991 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005992 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005993 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005994 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005995 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005996 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005997 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5998 codegen_->AddSlowPath(slow_path);
5999 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6000 __ Bind(slow_path->GetExitLabel());
6001 return;
6002 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006003 case HLoadString::LoadKind::kJitTableAddress: {
6004 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006005 load->GetStringIndex(),
6006 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006007 // /* GcRoot<mirror::String> */ out = *out
6008 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6009 return;
6010 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006011 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006012 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006013 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006014
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006015 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
6016 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6017 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006018 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006019 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006020 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6021 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006022}
6023
David Brazdilcb1c0552015-08-04 16:22:25 +01006024static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006025 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006026}
6027
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006028void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6029 LocationSummary* locations =
6030 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6031 locations->SetOut(Location::RequiresRegister());
6032}
6033
6034void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006035 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006036 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6037}
6038
6039void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6040 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6041}
6042
6043void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006044 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006045 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006046}
6047
6048void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6049 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006050 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006051 InvokeRuntimeCallingConvention calling_convention;
6052 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6053}
6054
6055void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006056 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006057 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006058}
6059
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006060// Temp is used for read barrier.
6061static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6062 if (kEmitCompilerReadBarrier &&
6063 (kUseBakerReadBarrier ||
6064 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6065 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6066 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6067 return 1;
6068 }
6069 return 0;
6070}
6071
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006072// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006073// interface pointer, one for loading the current interface.
6074// The other checks have one temp for loading the object's class.
6075static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6076 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6077 return 3;
6078 }
6079 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006080}
6081
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006082void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006083 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006084 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006085 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006086 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006087 case TypeCheckKind::kExactCheck:
6088 case TypeCheckKind::kAbstractClassCheck:
6089 case TypeCheckKind::kClassHierarchyCheck:
6090 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006091 call_kind =
6092 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006093 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006094 break;
6095 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006096 case TypeCheckKind::kUnresolvedCheck:
6097 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006098 call_kind = LocationSummary::kCallOnSlowPath;
6099 break;
6100 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006101
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006102 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006103 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006104 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006105 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006106 locations->SetInAt(0, Location::RequiresRegister());
6107 locations->SetInAt(1, Location::RequiresRegister());
6108 // The "out" register is used as a temporary, so it overlaps with the inputs.
6109 // Note that TypeCheckSlowPathARM uses this register too.
6110 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006111 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006112}
6113
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006114void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006115 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006116 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006117 Location obj_loc = locations->InAt(0);
6118 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006119 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006120 Location out_loc = locations->Out();
6121 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006122 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6123 DCHECK_LE(num_temps, 1u);
6124 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006125 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006126 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6127 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6128 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006129 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006130 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006131
6132 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006133 // avoid null check if we know obj is not null.
6134 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006135 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006136 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006137
Roland Levillainc9285912015-12-18 10:38:42 +00006138 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006139 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006140 // /* HeapReference<Class> */ out = obj->klass_
6141 GenerateReferenceLoadTwoRegisters(instruction,
6142 out_loc,
6143 obj_loc,
6144 class_offset,
6145 maybe_temp_loc,
6146 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006147 __ cmp(out, ShifterOperand(cls));
6148 // Classes must be equal for the instanceof to succeed.
6149 __ b(&zero, NE);
6150 __ LoadImmediate(out, 1);
6151 __ b(&done);
6152 break;
6153 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006154
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006155 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006156 // /* HeapReference<Class> */ out = obj->klass_
6157 GenerateReferenceLoadTwoRegisters(instruction,
6158 out_loc,
6159 obj_loc,
6160 class_offset,
6161 maybe_temp_loc,
6162 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006163 // If the class is abstract, we eagerly fetch the super class of the
6164 // object to avoid doing a comparison we know will fail.
6165 Label loop;
6166 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006167 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006168 GenerateReferenceLoadOneRegister(instruction,
6169 out_loc,
6170 super_offset,
6171 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006172 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006173 // If `out` is null, we use it for the result, and jump to `done`.
6174 __ CompareAndBranchIfZero(out, &done);
6175 __ cmp(out, ShifterOperand(cls));
6176 __ b(&loop, NE);
6177 __ LoadImmediate(out, 1);
6178 if (zero.IsLinked()) {
6179 __ b(&done);
6180 }
6181 break;
6182 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006183
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006184 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006185 // /* HeapReference<Class> */ out = obj->klass_
6186 GenerateReferenceLoadTwoRegisters(instruction,
6187 out_loc,
6188 obj_loc,
6189 class_offset,
6190 maybe_temp_loc,
6191 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006192 // Walk over the class hierarchy to find a match.
6193 Label loop, success;
6194 __ Bind(&loop);
6195 __ cmp(out, ShifterOperand(cls));
6196 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006197 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006198 GenerateReferenceLoadOneRegister(instruction,
6199 out_loc,
6200 super_offset,
6201 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006202 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006203 __ CompareAndBranchIfNonZero(out, &loop);
6204 // If `out` is null, we use it for the result, and jump to `done`.
6205 __ b(&done);
6206 __ Bind(&success);
6207 __ LoadImmediate(out, 1);
6208 if (zero.IsLinked()) {
6209 __ b(&done);
6210 }
6211 break;
6212 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006213
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006214 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006215 // /* HeapReference<Class> */ out = obj->klass_
6216 GenerateReferenceLoadTwoRegisters(instruction,
6217 out_loc,
6218 obj_loc,
6219 class_offset,
6220 maybe_temp_loc,
6221 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006222 // Do an exact check.
6223 Label exact_check;
6224 __ cmp(out, ShifterOperand(cls));
6225 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006226 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006227 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006228 GenerateReferenceLoadOneRegister(instruction,
6229 out_loc,
6230 component_offset,
6231 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006232 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006233 // If `out` is null, we use it for the result, and jump to `done`.
6234 __ CompareAndBranchIfZero(out, &done);
6235 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6236 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6237 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006238 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006239 __ LoadImmediate(out, 1);
6240 __ b(&done);
6241 break;
6242 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006243
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006244 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006245 // No read barrier since the slow path will retry upon failure.
6246 // /* HeapReference<Class> */ out = obj->klass_
6247 GenerateReferenceLoadTwoRegisters(instruction,
6248 out_loc,
6249 obj_loc,
6250 class_offset,
6251 maybe_temp_loc,
6252 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006253 __ cmp(out, ShifterOperand(cls));
6254 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006255 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6256 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006257 codegen_->AddSlowPath(slow_path);
6258 __ b(slow_path->GetEntryLabel(), NE);
6259 __ LoadImmediate(out, 1);
6260 if (zero.IsLinked()) {
6261 __ b(&done);
6262 }
6263 break;
6264 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006265
Calin Juravle98893e12015-10-02 21:05:03 +01006266 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006267 case TypeCheckKind::kInterfaceCheck: {
6268 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006269 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006270 // cases.
6271 //
6272 // We cannot directly call the InstanceofNonTrivial runtime
6273 // entry point without resorting to a type checking slow path
6274 // here (i.e. by calling InvokeRuntime directly), as it would
6275 // require to assign fixed registers for the inputs of this
6276 // HInstanceOf instruction (following the runtime calling
6277 // convention), which might be cluttered by the potential first
6278 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006279 //
6280 // TODO: Introduce a new runtime entry point taking the object
6281 // to test (instead of its class) as argument, and let it deal
6282 // with the read barrier issues. This will let us refactor this
6283 // case of the `switch` code as it was previously (with a direct
6284 // call to the runtime not using a type checking slow path).
6285 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006286 DCHECK(locations->OnlyCallsOnSlowPath());
6287 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6288 /* is_fatal */ false);
6289 codegen_->AddSlowPath(slow_path);
6290 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006291 if (zero.IsLinked()) {
6292 __ b(&done);
6293 }
6294 break;
6295 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006296 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006297
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006298 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006299 __ Bind(&zero);
6300 __ LoadImmediate(out, 0);
6301 }
6302
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006303 if (done.IsLinked()) {
6304 __ Bind(&done);
6305 }
6306
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006307 if (slow_path != nullptr) {
6308 __ Bind(slow_path->GetExitLabel());
6309 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006310}
6311
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006312void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006313 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6314 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6315
Roland Levillain3b359c72015-11-17 19:35:12 +00006316 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6317 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006318 case TypeCheckKind::kExactCheck:
6319 case TypeCheckKind::kAbstractClassCheck:
6320 case TypeCheckKind::kClassHierarchyCheck:
6321 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006322 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6323 LocationSummary::kCallOnSlowPath :
6324 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006325 break;
6326 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006327 case TypeCheckKind::kUnresolvedCheck:
6328 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006329 call_kind = LocationSummary::kCallOnSlowPath;
6330 break;
6331 }
6332
Roland Levillain3b359c72015-11-17 19:35:12 +00006333 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6334 locations->SetInAt(0, Location::RequiresRegister());
6335 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006336 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006337}
6338
6339void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006340 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006341 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006342 Location obj_loc = locations->InAt(0);
6343 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006344 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006345 Location temp_loc = locations->GetTemp(0);
6346 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006347 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6348 DCHECK_LE(num_temps, 3u);
6349 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6350 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6351 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6352 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6353 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6354 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6355 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6356 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6357 const uint32_t object_array_data_offset =
6358 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006359
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006360 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6361 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6362 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006363 bool is_type_check_slow_path_fatal = false;
6364 if (!kEmitCompilerReadBarrier) {
6365 is_type_check_slow_path_fatal =
6366 (type_check_kind == TypeCheckKind::kExactCheck ||
6367 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6368 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6369 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6370 !instruction->CanThrowIntoCatchBlock();
6371 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01006372 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006373 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6374 is_type_check_slow_path_fatal);
6375 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006376
6377 Label done;
6378 // Avoid null check if we know obj is not null.
6379 if (instruction->MustDoNullCheck()) {
6380 __ CompareAndBranchIfZero(obj, &done);
6381 }
6382
Roland Levillain3b359c72015-11-17 19:35:12 +00006383 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006384 case TypeCheckKind::kExactCheck:
6385 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006386 // /* HeapReference<Class> */ temp = obj->klass_
6387 GenerateReferenceLoadTwoRegisters(instruction,
6388 temp_loc,
6389 obj_loc,
6390 class_offset,
6391 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006392 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006393
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006394 __ cmp(temp, ShifterOperand(cls));
6395 // Jump to slow path for throwing the exception or doing a
6396 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006397 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006398 break;
6399 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006400
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006401 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006402 // /* HeapReference<Class> */ temp = obj->klass_
6403 GenerateReferenceLoadTwoRegisters(instruction,
6404 temp_loc,
6405 obj_loc,
6406 class_offset,
6407 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006408 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006409
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006410 // If the class is abstract, we eagerly fetch the super class of the
6411 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006412 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006413 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006414 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006415 GenerateReferenceLoadOneRegister(instruction,
6416 temp_loc,
6417 super_offset,
6418 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006419 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006420
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006421 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6422 // exception.
6423 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00006424
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006425 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006426 __ cmp(temp, ShifterOperand(cls));
6427 __ b(&loop, NE);
6428 break;
6429 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006430
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006431 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006432 // /* HeapReference<Class> */ temp = obj->klass_
6433 GenerateReferenceLoadTwoRegisters(instruction,
6434 temp_loc,
6435 obj_loc,
6436 class_offset,
6437 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006438 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006439
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006440 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006441 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006442 __ Bind(&loop);
6443 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006444 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006445
Roland Levillain3b359c72015-11-17 19:35:12 +00006446 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006447 GenerateReferenceLoadOneRegister(instruction,
6448 temp_loc,
6449 super_offset,
6450 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006451 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006452
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006453 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6454 // exception.
6455 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6456 // Otherwise, jump to the beginning of the loop.
6457 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006458 break;
6459 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006460
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006461 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006462 // /* HeapReference<Class> */ temp = obj->klass_
6463 GenerateReferenceLoadTwoRegisters(instruction,
6464 temp_loc,
6465 obj_loc,
6466 class_offset,
6467 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006468 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006469
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006470 // Do an exact check.
6471 __ cmp(temp, ShifterOperand(cls));
6472 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006473
6474 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006475 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006476 GenerateReferenceLoadOneRegister(instruction,
6477 temp_loc,
6478 component_offset,
6479 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006480 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006481 // If the component type is null, jump to the slow path to throw the exception.
6482 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6483 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6484 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006485 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006486 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006487 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006488 break;
6489 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006490
Calin Juravle98893e12015-10-02 21:05:03 +01006491 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006492 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00006493 // We cannot directly call the CheckCast runtime entry point
6494 // without resorting to a type checking slow path here (i.e. by
6495 // calling InvokeRuntime directly), as it would require to
6496 // assign fixed registers for the inputs of this HInstanceOf
6497 // instruction (following the runtime calling convention), which
6498 // might be cluttered by the potential first read barrier
6499 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006500
Roland Levillain3b359c72015-11-17 19:35:12 +00006501 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006502 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006503
6504 case TypeCheckKind::kInterfaceCheck: {
6505 // Avoid read barriers to improve performance of the fast path. We can not get false
6506 // positives by doing this.
6507 // /* HeapReference<Class> */ temp = obj->klass_
6508 GenerateReferenceLoadTwoRegisters(instruction,
6509 temp_loc,
6510 obj_loc,
6511 class_offset,
6512 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006513 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006514
6515 // /* HeapReference<Class> */ temp = temp->iftable_
6516 GenerateReferenceLoadTwoRegisters(instruction,
6517 temp_loc,
6518 temp_loc,
6519 iftable_offset,
6520 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006521 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006522 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006523 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006524 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006525 Label start_loop;
6526 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006527 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
6528 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006529 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
6530 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006531 // Go to next interface.
6532 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
6533 __ sub(maybe_temp2_loc.AsRegister<Register>(),
6534 maybe_temp2_loc.AsRegister<Register>(),
6535 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006536 // Compare the classes and continue the loop if they do not match.
6537 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
6538 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006539 break;
6540 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006541 }
6542 __ Bind(&done);
6543
Roland Levillain3b359c72015-11-17 19:35:12 +00006544 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006545}
6546
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006547void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6548 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006549 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006550 InvokeRuntimeCallingConvention calling_convention;
6551 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6552}
6553
6554void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006555 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6556 instruction,
6557 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006558 if (instruction->IsEnter()) {
6559 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6560 } else {
6561 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6562 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006563}
6564
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006565void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6566void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6567void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006568
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006569void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006570 LocationSummary* locations =
6571 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6572 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6573 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006574 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006575 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006576 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006577 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006578}
6579
6580void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6581 HandleBitwiseOperation(instruction);
6582}
6583
6584void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6585 HandleBitwiseOperation(instruction);
6586}
6587
6588void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6589 HandleBitwiseOperation(instruction);
6590}
6591
Artem Serov7fc63502016-02-09 17:15:29 +00006592
6593void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6594 LocationSummary* locations =
6595 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6596 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6597 || instruction->GetResultType() == Primitive::kPrimLong);
6598
6599 locations->SetInAt(0, Location::RequiresRegister());
6600 locations->SetInAt(1, Location::RequiresRegister());
6601 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6602}
6603
6604void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6605 LocationSummary* locations = instruction->GetLocations();
6606 Location first = locations->InAt(0);
6607 Location second = locations->InAt(1);
6608 Location out = locations->Out();
6609
6610 if (instruction->GetResultType() == Primitive::kPrimInt) {
6611 Register first_reg = first.AsRegister<Register>();
6612 ShifterOperand second_reg(second.AsRegister<Register>());
6613 Register out_reg = out.AsRegister<Register>();
6614
6615 switch (instruction->GetOpKind()) {
6616 case HInstruction::kAnd:
6617 __ bic(out_reg, first_reg, second_reg);
6618 break;
6619 case HInstruction::kOr:
6620 __ orn(out_reg, first_reg, second_reg);
6621 break;
6622 // There is no EON on arm.
6623 case HInstruction::kXor:
6624 default:
6625 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6626 UNREACHABLE();
6627 }
6628 return;
6629
6630 } else {
6631 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6632 Register first_low = first.AsRegisterPairLow<Register>();
6633 Register first_high = first.AsRegisterPairHigh<Register>();
6634 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6635 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6636 Register out_low = out.AsRegisterPairLow<Register>();
6637 Register out_high = out.AsRegisterPairHigh<Register>();
6638
6639 switch (instruction->GetOpKind()) {
6640 case HInstruction::kAnd:
6641 __ bic(out_low, first_low, second_low);
6642 __ bic(out_high, first_high, second_high);
6643 break;
6644 case HInstruction::kOr:
6645 __ orn(out_low, first_low, second_low);
6646 __ orn(out_high, first_high, second_high);
6647 break;
6648 // There is no EON on arm.
6649 case HInstruction::kXor:
6650 default:
6651 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6652 UNREACHABLE();
6653 }
6654 }
6655}
6656
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006657void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6658 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6659 if (value == 0xffffffffu) {
6660 if (out != first) {
6661 __ mov(out, ShifterOperand(first));
6662 }
6663 return;
6664 }
6665 if (value == 0u) {
6666 __ mov(out, ShifterOperand(0));
6667 return;
6668 }
6669 ShifterOperand so;
6670 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6671 __ and_(out, first, so);
6672 } else {
6673 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6674 __ bic(out, first, ShifterOperand(~value));
6675 }
6676}
6677
6678void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6679 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6680 if (value == 0u) {
6681 if (out != first) {
6682 __ mov(out, ShifterOperand(first));
6683 }
6684 return;
6685 }
6686 if (value == 0xffffffffu) {
6687 __ mvn(out, ShifterOperand(0));
6688 return;
6689 }
6690 ShifterOperand so;
6691 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6692 __ orr(out, first, so);
6693 } else {
6694 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6695 __ orn(out, first, ShifterOperand(~value));
6696 }
6697}
6698
6699void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6700 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6701 if (value == 0u) {
6702 if (out != first) {
6703 __ mov(out, ShifterOperand(first));
6704 }
6705 return;
6706 }
6707 __ eor(out, first, ShifterOperand(value));
6708}
6709
Vladimir Marko59751a72016-08-05 14:37:27 +01006710void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6711 Location first,
6712 uint64_t value) {
6713 Register out_low = out.AsRegisterPairLow<Register>();
6714 Register out_high = out.AsRegisterPairHigh<Register>();
6715 Register first_low = first.AsRegisterPairLow<Register>();
6716 Register first_high = first.AsRegisterPairHigh<Register>();
6717 uint32_t value_low = Low32Bits(value);
6718 uint32_t value_high = High32Bits(value);
6719 if (value_low == 0u) {
6720 if (out_low != first_low) {
6721 __ mov(out_low, ShifterOperand(first_low));
6722 }
6723 __ AddConstant(out_high, first_high, value_high);
6724 return;
6725 }
6726 __ AddConstantSetFlags(out_low, first_low, value_low);
6727 ShifterOperand so;
6728 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6729 __ adc(out_high, first_high, so);
6730 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6731 __ sbc(out_high, first_high, so);
6732 } else {
6733 LOG(FATAL) << "Unexpected constant " << value_high;
6734 UNREACHABLE();
6735 }
6736}
6737
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006738void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6739 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006740 Location first = locations->InAt(0);
6741 Location second = locations->InAt(1);
6742 Location out = locations->Out();
6743
6744 if (second.IsConstant()) {
6745 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6746 uint32_t value_low = Low32Bits(value);
6747 if (instruction->GetResultType() == Primitive::kPrimInt) {
6748 Register first_reg = first.AsRegister<Register>();
6749 Register out_reg = out.AsRegister<Register>();
6750 if (instruction->IsAnd()) {
6751 GenerateAndConst(out_reg, first_reg, value_low);
6752 } else if (instruction->IsOr()) {
6753 GenerateOrrConst(out_reg, first_reg, value_low);
6754 } else {
6755 DCHECK(instruction->IsXor());
6756 GenerateEorConst(out_reg, first_reg, value_low);
6757 }
6758 } else {
6759 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6760 uint32_t value_high = High32Bits(value);
6761 Register first_low = first.AsRegisterPairLow<Register>();
6762 Register first_high = first.AsRegisterPairHigh<Register>();
6763 Register out_low = out.AsRegisterPairLow<Register>();
6764 Register out_high = out.AsRegisterPairHigh<Register>();
6765 if (instruction->IsAnd()) {
6766 GenerateAndConst(out_low, first_low, value_low);
6767 GenerateAndConst(out_high, first_high, value_high);
6768 } else if (instruction->IsOr()) {
6769 GenerateOrrConst(out_low, first_low, value_low);
6770 GenerateOrrConst(out_high, first_high, value_high);
6771 } else {
6772 DCHECK(instruction->IsXor());
6773 GenerateEorConst(out_low, first_low, value_low);
6774 GenerateEorConst(out_high, first_high, value_high);
6775 }
6776 }
6777 return;
6778 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006779
6780 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006781 Register first_reg = first.AsRegister<Register>();
6782 ShifterOperand second_reg(second.AsRegister<Register>());
6783 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006784 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006785 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006786 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006787 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006788 } else {
6789 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006790 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006791 }
6792 } else {
6793 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006794 Register first_low = first.AsRegisterPairLow<Register>();
6795 Register first_high = first.AsRegisterPairHigh<Register>();
6796 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6797 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6798 Register out_low = out.AsRegisterPairLow<Register>();
6799 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006800 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006801 __ and_(out_low, first_low, second_low);
6802 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006803 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006804 __ orr(out_low, first_low, second_low);
6805 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006806 } else {
6807 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006808 __ eor(out_low, first_low, second_low);
6809 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006810 }
6811 }
6812}
6813
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006814void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
6815 HInstruction* instruction,
6816 Location out,
6817 uint32_t offset,
6818 Location maybe_temp,
6819 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006820 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006821 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006822 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006823 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006824 if (kUseBakerReadBarrier) {
6825 // Load with fast path based Baker's read barrier.
6826 // /* HeapReference<Object> */ out = *(out + offset)
6827 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006828 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006829 } else {
6830 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006831 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006832 // in the following move operation, as we will need it for the
6833 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006834 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006835 // /* HeapReference<Object> */ out = *(out + offset)
6836 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006837 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006838 }
6839 } else {
6840 // Plain load with no read barrier.
6841 // /* HeapReference<Object> */ out = *(out + offset)
6842 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6843 __ MaybeUnpoisonHeapReference(out_reg);
6844 }
6845}
6846
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006847void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
6848 HInstruction* instruction,
6849 Location out,
6850 Location obj,
6851 uint32_t offset,
6852 Location maybe_temp,
6853 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006854 Register out_reg = out.AsRegister<Register>();
6855 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006856 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006857 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006858 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006859 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006860 // Load with fast path based Baker's read barrier.
6861 // /* HeapReference<Object> */ out = *(obj + offset)
6862 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006863 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006864 } else {
6865 // Load with slow path based read barrier.
6866 // /* HeapReference<Object> */ out = *(obj + offset)
6867 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6868 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6869 }
6870 } else {
6871 // Plain load with no read barrier.
6872 // /* HeapReference<Object> */ out = *(obj + offset)
6873 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6874 __ MaybeUnpoisonHeapReference(out_reg);
6875 }
6876}
6877
6878void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6879 Location root,
6880 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006881 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006882 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006883 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006884 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006885 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006886 if (kUseBakerReadBarrier) {
6887 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6888 // Baker's read barrier are used:
6889 //
6890 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006891 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6892 // if (temp != null) {
6893 // root = temp(root)
Roland Levillainc9285912015-12-18 10:38:42 +00006894 // }
6895
6896 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6897 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6898 static_assert(
6899 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6900 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6901 "have different sizes.");
6902 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6903 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6904 "have different sizes.");
6905
Vladimir Marko953437b2016-08-24 08:30:46 +00006906 // Slow path marking the GC root `root`.
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006907 Location temp = Location::RegisterLocation(LR);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006908 SlowPathCodeARM* slow_path =
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006909 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
6910 instruction,
6911 root,
6912 /*entrypoint*/ temp);
Roland Levillainc9285912015-12-18 10:38:42 +00006913 codegen_->AddSlowPath(slow_path);
6914
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006915 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6916 const int32_t entry_point_offset =
6917 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
6918 // Loading the entrypoint does not require a load acquire since it is only changed when
6919 // threads are suspended or running a checkpoint.
6920 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6921 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6922 // checking GetIsGcMarking.
6923 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00006924 __ Bind(slow_path->GetExitLabel());
6925 } else {
6926 // GC root loaded through a slow path for read barriers other
6927 // than Baker's.
6928 // /* GcRoot<mirror::Object>* */ root = obj + offset
6929 __ AddConstant(root_reg, obj, offset);
6930 // /* mirror::Object* */ root = root->Read()
6931 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6932 }
6933 } else {
6934 // Plain GC root load with no read barrier.
6935 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6936 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6937 // Note that GC roots are not affected by heap poisoning, thus we
6938 // do not have to unpoison `root_reg` here.
6939 }
6940}
6941
6942void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6943 Location ref,
6944 Register obj,
6945 uint32_t offset,
6946 Location temp,
6947 bool needs_null_check) {
6948 DCHECK(kEmitCompilerReadBarrier);
6949 DCHECK(kUseBakerReadBarrier);
6950
6951 // /* HeapReference<Object> */ ref = *(obj + offset)
6952 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006953 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006954 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006955 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006956}
6957
6958void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6959 Location ref,
6960 Register obj,
6961 uint32_t data_offset,
6962 Location index,
6963 Location temp,
6964 bool needs_null_check) {
6965 DCHECK(kEmitCompilerReadBarrier);
6966 DCHECK(kUseBakerReadBarrier);
6967
Roland Levillainbfea3352016-06-23 13:48:47 +01006968 static_assert(
6969 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6970 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006971 // /* HeapReference<Object> */ ref =
6972 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006973 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006974 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006975 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006976}
6977
6978void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6979 Location ref,
6980 Register obj,
6981 uint32_t offset,
6982 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006983 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006984 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006985 bool needs_null_check,
6986 bool always_update_field,
6987 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00006988 DCHECK(kEmitCompilerReadBarrier);
6989 DCHECK(kUseBakerReadBarrier);
6990
6991 // In slow path based read barriers, the read barrier call is
6992 // inserted after the original load. However, in fast path based
6993 // Baker's read barriers, we need to perform the load of
6994 // mirror::Object::monitor_ *before* the original reference load.
6995 // This load-load ordering is required by the read barrier.
6996 // The fast path/slow path (for Baker's algorithm) should look like:
6997 //
6998 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6999 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7000 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007001 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillainc9285912015-12-18 10:38:42 +00007002 // if (is_gray) {
7003 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7004 // }
7005 //
7006 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007007 // slightly more complex as it performs additional checks that we do
7008 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00007009
7010 Register ref_reg = ref.AsRegister<Register>();
7011 Register temp_reg = temp.AsRegister<Register>();
7012 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7013
7014 // /* int32_t */ monitor = obj->monitor_
7015 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7016 if (needs_null_check) {
7017 MaybeRecordImplicitNullCheck(instruction);
7018 }
7019 // /* LockWord */ lock_word = LockWord(monitor)
7020 static_assert(sizeof(LockWord) == sizeof(int32_t),
7021 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007022
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007023 // Introduce a dependency on the lock_word including the rb_state,
7024 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00007025 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01007026 // `obj` is unchanged by this operation, but its value now depends
7027 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007028 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00007029
7030 // The actual reference load.
7031 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007032 // Load types involving an "index": ArrayGet,
7033 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7034 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01007035 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00007036 if (index.IsConstant()) {
7037 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01007038 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00007039 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7040 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01007041 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007042 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7043 // intrinsics, which use a register pair as index ("long
7044 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01007045 Register index_reg = index.IsRegisterPair()
7046 ? index.AsRegisterPairLow<Register>()
7047 : index.AsRegister<Register>();
7048 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00007049 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
7050 }
7051 } else {
7052 // /* HeapReference<Object> */ ref = *(obj + offset)
7053 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7054 }
7055
7056 // Object* ref = ref_addr->AsMirrorPtr()
7057 __ MaybeUnpoisonHeapReference(ref_reg);
7058
Vladimir Marko953437b2016-08-24 08:30:46 +00007059 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007060 SlowPathCodeARM* slow_path;
7061 if (always_update_field) {
7062 DCHECK(temp2 != nullptr);
7063 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
7064 // of the form `obj + field_offset`, where `obj` is a register and
7065 // `field_offset` is a register pair (of which only the lower half
7066 // is used). Thus `offset` and `scale_factor` above are expected
7067 // to be null in this code path.
7068 DCHECK_EQ(offset, 0u);
7069 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7070 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
7071 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7072 } else {
7073 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
7074 }
Roland Levillainc9285912015-12-18 10:38:42 +00007075 AddSlowPath(slow_path);
7076
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007077 // if (rb_state == ReadBarrier::GrayState())
Roland Levillainc9285912015-12-18 10:38:42 +00007078 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007079 // Given the numeric representation, it's enough to check the low bit of the
7080 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7081 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007082 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7083 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007084 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7085 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00007086 __ Bind(slow_path->GetExitLabel());
7087}
7088
7089void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
7090 Location out,
7091 Location ref,
7092 Location obj,
7093 uint32_t offset,
7094 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007095 DCHECK(kEmitCompilerReadBarrier);
7096
Roland Levillainc9285912015-12-18 10:38:42 +00007097 // Insert a slow path based read barrier *after* the reference load.
7098 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007099 // If heap poisoning is enabled, the unpoisoning of the loaded
7100 // reference will be carried out by the runtime within the slow
7101 // path.
7102 //
7103 // Note that `ref` currently does not get unpoisoned (when heap
7104 // poisoning is enabled), which is alright as the `ref` argument is
7105 // not used by the artReadBarrierSlow entry point.
7106 //
7107 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007108 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00007109 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
7110 AddSlowPath(slow_path);
7111
Roland Levillain3b359c72015-11-17 19:35:12 +00007112 __ b(slow_path->GetEntryLabel());
7113 __ Bind(slow_path->GetExitLabel());
7114}
7115
Roland Levillainc9285912015-12-18 10:38:42 +00007116void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7117 Location out,
7118 Location ref,
7119 Location obj,
7120 uint32_t offset,
7121 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007122 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00007123 // Baker's read barriers shall be handled by the fast path
7124 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
7125 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007126 // If heap poisoning is enabled, unpoisoning will be taken care of
7127 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007128 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00007129 } else if (kPoisonHeapReferences) {
7130 __ UnpoisonHeapReference(out.AsRegister<Register>());
7131 }
7132}
7133
Roland Levillainc9285912015-12-18 10:38:42 +00007134void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7135 Location out,
7136 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007137 DCHECK(kEmitCompilerReadBarrier);
7138
Roland Levillainc9285912015-12-18 10:38:42 +00007139 // Insert a slow path based read barrier *after* the GC root load.
7140 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007141 // Note that GC roots are not affected by heap poisoning, so we do
7142 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007143 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007144 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
7145 AddSlowPath(slow_path);
7146
Roland Levillain3b359c72015-11-17 19:35:12 +00007147 __ b(slow_path->GetEntryLabel());
7148 __ Bind(slow_path->GetExitLabel());
7149}
7150
Vladimir Markodc151b22015-10-15 18:02:30 +01007151HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7152 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007153 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00007154 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007155}
7156
Vladimir Markob4536b72015-11-24 13:45:23 +00007157Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7158 Register temp) {
7159 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7160 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7161 if (!invoke->GetLocations()->Intrinsified()) {
7162 return location.AsRegister<Register>();
7163 }
7164 // For intrinsics we allow any location, so it may be on the stack.
7165 if (!location.IsRegister()) {
7166 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7167 return temp;
7168 }
7169 // For register locations, check if the register was saved. If so, get it from the stack.
7170 // Note: There is a chance that the register was saved but not overwritten, so we could
7171 // save one load. However, since this is just an intrinsic slow path we prefer this
7172 // simple and more robust approach rather that trying to determine if that's the case.
7173 SlowPathCode* slow_path = GetCurrentSlowPath();
7174 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7175 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7176 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7177 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7178 return temp;
7179 }
7180 return location.AsRegister<Register>();
7181}
7182
Nicolas Geoffray38207af2015-06-01 15:46:22 +01007183void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007184 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7185 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007186 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7187 uint32_t offset =
7188 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007189 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007190 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007191 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007192 }
Vladimir Marko58155012015-08-19 12:49:41 +00007193 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007194 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007195 break;
7196 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7197 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7198 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007199 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7200 HArmDexCacheArraysBase* base =
7201 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7202 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7203 temp.AsRegister<Register>());
7204 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7205 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7206 break;
7207 }
Vladimir Marko58155012015-08-19 12:49:41 +00007208 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007209 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007210 Register method_reg;
7211 Register reg = temp.AsRegister<Register>();
7212 if (current_method.IsRegister()) {
7213 method_reg = current_method.AsRegister<Register>();
7214 } else {
7215 DCHECK(invoke->GetLocations()->Intrinsified());
7216 DCHECK(!current_method.IsValid());
7217 method_reg = reg;
7218 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7219 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007220 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7221 __ LoadFromOffset(kLoadWord,
7222 reg,
7223 method_reg,
7224 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007225 // temp = temp[index_in_cache];
7226 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7227 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007228 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7229 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007230 }
Vladimir Marko58155012015-08-19 12:49:41 +00007231 }
7232
7233 switch (invoke->GetCodePtrLocation()) {
7234 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7235 __ bl(GetFrameEntryLabel());
7236 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007237 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7238 // LR = callee_method->entry_point_from_quick_compiled_code_
7239 __ LoadFromOffset(
7240 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007241 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007242 // LR()
7243 __ blx(LR);
7244 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007245 }
7246
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007247 DCHECK(!IsLeafMethod());
7248}
7249
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007250void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7251 Register temp = temp_location.AsRegister<Register>();
7252 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7253 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007254
7255 // Use the calling convention instead of the location of the receiver, as
7256 // intrinsics may have put the receiver in a different register. In the intrinsics
7257 // slow path, the arguments have been moved to the right place, so here we are
7258 // guaranteed that the receiver is the first register of the calling convention.
7259 InvokeDexCallingConvention calling_convention;
7260 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007261 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007262 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007263 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007264 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007265 // Instead of simply (possibly) unpoisoning `temp` here, we should
7266 // emit a read barrier for the previous class reference load.
7267 // However this is not required in practice, as this is an
7268 // intermediate/temporary reference and because the current
7269 // concurrent copying collector keeps the from-space memory
7270 // intact/accessible until the end of the marking phase (the
7271 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007272 __ MaybeUnpoisonHeapReference(temp);
7273 // temp = temp->GetMethodAt(method_offset);
7274 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007275 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007276 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7277 // LR = temp->GetEntryPoint();
7278 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7279 // LR();
7280 __ blx(LR);
7281}
7282
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007283CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007284 const DexFile& dex_file, dex::StringIndex string_index) {
7285 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007286}
7287
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007288CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08007289 const DexFile& dex_file, dex::TypeIndex type_index) {
7290 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007291}
7292
Vladimir Marko1998cd02017-01-13 13:02:58 +00007293CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
7294 const DexFile& dex_file, dex::TypeIndex type_index) {
7295 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
7296}
7297
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007298CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7299 const DexFile& dex_file, uint32_t element_offset) {
7300 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7301}
7302
7303CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7304 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7305 patches->emplace_back(dex_file, offset_or_index);
7306 return &patches->back();
7307}
7308
7309Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007310 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007311 return boot_image_string_patches_.GetOrCreate(
7312 StringReference(&dex_file, string_index),
7313 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7314}
7315
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007316Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007317 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007318 return boot_image_type_patches_.GetOrCreate(
7319 TypeReference(&dex_file, type_index),
7320 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7321}
7322
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007323Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7324 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7325 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7326 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7327}
7328
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007329Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007330 dex::StringIndex string_index,
7331 Handle<mirror::String> handle) {
7332 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7333 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007334 return jit_string_patches_.GetOrCreate(
7335 StringReference(&dex_file, string_index),
7336 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7337}
7338
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007339Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
7340 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007341 Handle<mirror::Class> handle) {
7342 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
7343 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007344 return jit_class_patches_.GetOrCreate(
7345 TypeReference(&dex_file, type_index),
7346 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7347}
7348
Vladimir Markoaad75c62016-10-03 08:46:48 +00007349template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7350inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7351 const ArenaDeque<PcRelativePatchInfo>& infos,
7352 ArenaVector<LinkerPatch>* linker_patches) {
7353 for (const PcRelativePatchInfo& info : infos) {
7354 const DexFile& dex_file = info.target_dex_file;
7355 size_t offset_or_index = info.offset_or_index;
7356 DCHECK(info.add_pc_label.IsBound());
7357 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7358 // Add MOVW patch.
7359 DCHECK(info.movw_label.IsBound());
7360 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7361 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7362 // Add MOVT patch.
7363 DCHECK(info.movt_label.IsBound());
7364 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7365 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7366 }
7367}
7368
Vladimir Marko58155012015-08-19 12:49:41 +00007369void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7370 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007371 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00007372 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007373 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007374 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007375 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007376 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00007377 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007378 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007379 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007380 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7381 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007382 for (const auto& entry : boot_image_string_patches_) {
7383 const StringReference& target_string = entry.first;
7384 Literal* literal = entry.second;
7385 DCHECK(literal->GetLabel()->IsBound());
7386 uint32_t literal_offset = literal->GetLabel()->Position();
7387 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7388 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007389 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007390 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007391 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00007392 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007393 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7394 linker_patches);
7395 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007396 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7397 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007398 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7399 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007400 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007401 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
7402 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007403 for (const auto& entry : boot_image_type_patches_) {
7404 const TypeReference& target_type = entry.first;
7405 Literal* literal = entry.second;
7406 DCHECK(literal->GetLabel()->IsBound());
7407 uint32_t literal_offset = literal->GetLabel()->Position();
7408 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7409 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007410 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007411 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007412 for (const auto& entry : boot_image_address_patches_) {
7413 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7414 Literal* literal = entry.second;
7415 DCHECK(literal->GetLabel()->IsBound());
7416 uint32_t literal_offset = literal->GetLabel()->Position();
7417 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7418 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007419 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007420}
7421
7422Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7423 return map->GetOrCreate(
7424 value,
7425 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007426}
7427
7428Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7429 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007430 return map->GetOrCreate(
7431 target_method,
7432 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007433}
7434
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007435void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7436 LocationSummary* locations =
7437 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7438 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7439 Location::RequiresRegister());
7440 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7441 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7442 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7443}
7444
7445void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7446 LocationSummary* locations = instr->GetLocations();
7447 Register res = locations->Out().AsRegister<Register>();
7448 Register accumulator =
7449 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7450 Register mul_left =
7451 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7452 Register mul_right =
7453 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7454
7455 if (instr->GetOpKind() == HInstruction::kAdd) {
7456 __ mla(res, mul_left, mul_right, accumulator);
7457 } else {
7458 __ mls(res, mul_left, mul_right, accumulator);
7459 }
7460}
7461
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007462void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007463 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007464 LOG(FATAL) << "Unreachable";
7465}
7466
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007467void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007468 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007469 LOG(FATAL) << "Unreachable";
7470}
7471
Mark Mendellfe57faa2015-09-18 09:26:15 -04007472// Simple implementation of packed switch - generate cascaded compare/jumps.
7473void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7474 LocationSummary* locations =
7475 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7476 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007477 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007478 codegen_->GetAssembler()->IsThumb()) {
7479 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7480 if (switch_instr->GetStartValue() != 0) {
7481 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7482 }
7483 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007484}
7485
7486void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7487 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007488 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007489 LocationSummary* locations = switch_instr->GetLocations();
7490 Register value_reg = locations->InAt(0).AsRegister<Register>();
7491 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7492
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007493 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007494 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007495 Register temp_reg = IP;
7496 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7497 // the immediate, because IP is used as the destination register. For the other
7498 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7499 // and they can be encoded in the instruction without making use of IP register.
7500 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7501
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007502 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007503 // Jump to successors[0] if value == lower_bound.
7504 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7505 int32_t last_index = 0;
7506 for (; num_entries - last_index > 2; last_index += 2) {
7507 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7508 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7509 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7510 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7511 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7512 }
7513 if (num_entries - last_index == 2) {
7514 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007515 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007516 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007517 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007518
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007519 // And the default for any other value.
7520 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7521 __ b(codegen_->GetLabelOf(default_block));
7522 }
7523 } else {
7524 // Create a table lookup.
7525 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7526
7527 // Materialize a pointer to the switch table
7528 std::vector<Label*> labels(num_entries);
7529 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7530 for (uint32_t i = 0; i < num_entries; i++) {
7531 labels[i] = codegen_->GetLabelOf(successors[i]);
7532 }
7533 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7534
7535 // Remove the bias.
7536 Register key_reg;
7537 if (lower_bound != 0) {
7538 key_reg = locations->GetTemp(1).AsRegister<Register>();
7539 __ AddConstant(key_reg, value_reg, -lower_bound);
7540 } else {
7541 key_reg = value_reg;
7542 }
7543
7544 // Check whether the value is in the table, jump to default block if not.
7545 __ CmpConstant(key_reg, num_entries - 1);
7546 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7547
7548 // Load the displacement from the table.
7549 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7550
7551 // Dispatch is a direct add to the PC (for Thumb2).
7552 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007553 }
7554}
7555
Vladimir Markob4536b72015-11-24 13:45:23 +00007556void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7557 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7558 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007559}
7560
7561void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7562 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007563 CodeGeneratorARM::PcRelativePatchInfo* labels =
7564 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007565 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007566 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007567 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007568 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007569 __ BindTrackedLabel(&labels->add_pc_label);
7570 __ add(base_reg, base_reg, ShifterOperand(PC));
7571}
7572
Andreas Gampe85b62f22015-09-09 13:15:38 -07007573void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7574 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007575 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007576 return;
7577 }
7578
7579 DCHECK_NE(type, Primitive::kPrimVoid);
7580
7581 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7582 if (return_loc.Equals(trg)) {
7583 return;
7584 }
7585
7586 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7587 // with the last branch.
7588 if (type == Primitive::kPrimLong) {
7589 HParallelMove parallel_move(GetGraph()->GetArena());
7590 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7591 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7592 GetMoveResolver()->EmitNativeCode(&parallel_move);
7593 } else if (type == Primitive::kPrimDouble) {
7594 HParallelMove parallel_move(GetGraph()->GetArena());
7595 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7596 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7597 GetMoveResolver()->EmitNativeCode(&parallel_move);
7598 } else {
7599 // Let the parallel move resolver take care of all of this.
7600 HParallelMove parallel_move(GetGraph()->GetArena());
7601 parallel_move.AddMove(return_loc, trg, type, nullptr);
7602 GetMoveResolver()->EmitNativeCode(&parallel_move);
7603 }
7604}
7605
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007606void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7607 LocationSummary* locations =
7608 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7609 locations->SetInAt(0, Location::RequiresRegister());
7610 locations->SetOut(Location::RequiresRegister());
7611}
7612
7613void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7614 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007615 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007616 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007617 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007618 __ LoadFromOffset(kLoadWord,
7619 locations->Out().AsRegister<Register>(),
7620 locations->InAt(0).AsRegister<Register>(),
7621 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007622 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007623 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007624 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007625 __ LoadFromOffset(kLoadWord,
7626 locations->Out().AsRegister<Register>(),
7627 locations->InAt(0).AsRegister<Register>(),
7628 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7629 __ LoadFromOffset(kLoadWord,
7630 locations->Out().AsRegister<Register>(),
7631 locations->Out().AsRegister<Register>(),
7632 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007633 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007634}
7635
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007636static void PatchJitRootUse(uint8_t* code,
7637 const uint8_t* roots_data,
7638 Literal* literal,
7639 uint64_t index_in_table) {
7640 DCHECK(literal->GetLabel()->IsBound());
7641 uint32_t literal_offset = literal->GetLabel()->Position();
7642 uintptr_t address =
7643 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7644 uint8_t* data = code + literal_offset;
7645 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7646}
7647
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007648void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7649 for (const auto& entry : jit_string_patches_) {
7650 const auto& it = jit_string_roots_.find(entry.first);
7651 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007652 PatchJitRootUse(code, roots_data, entry.second, it->second);
7653 }
7654 for (const auto& entry : jit_class_patches_) {
7655 const auto& it = jit_class_roots_.find(entry.first);
7656 DCHECK(it != jit_class_roots_.end());
7657 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007658 }
7659}
7660
Roland Levillain4d027112015-07-01 15:41:14 +01007661#undef __
7662#undef QUICK_ENTRY_POINT
7663
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007664} // namespace arm
7665} // namespace art