blob: 1893029cad6d9b863af65e5a46db89e8c5883d03 [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)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003335 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003336 // 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)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003462 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003463 // 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 Geoffrayd0958442017-01-30 14:57:16 +00003996 QuickEntrypointEnum entrypoint =
3997 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
3998 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003999 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004000 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004001}
4002
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004003void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004004 LocationSummary* locations =
4005 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004006 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4007 if (location.IsStackSlot()) {
4008 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4009 } else if (location.IsDoubleStackSlot()) {
4010 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004011 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004012 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004013}
4014
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004015void InstructionCodeGeneratorARM::VisitParameterValue(
4016 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004017 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004018}
4019
4020void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4021 LocationSummary* locations =
4022 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4023 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4024}
4025
4026void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4027 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004028}
4029
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004030void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004031 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004032 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004033 locations->SetInAt(0, Location::RequiresRegister());
4034 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004035}
4036
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004037void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4038 LocationSummary* locations = not_->GetLocations();
4039 Location out = locations->Out();
4040 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004041 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004042 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004043 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004044 break;
4045
4046 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004047 __ mvn(out.AsRegisterPairLow<Register>(),
4048 ShifterOperand(in.AsRegisterPairLow<Register>()));
4049 __ mvn(out.AsRegisterPairHigh<Register>(),
4050 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004051 break;
4052
4053 default:
4054 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4055 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004056}
4057
David Brazdil66d126e2015-04-03 16:02:44 +01004058void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4059 LocationSummary* locations =
4060 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4061 locations->SetInAt(0, Location::RequiresRegister());
4062 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4063}
4064
4065void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004066 LocationSummary* locations = bool_not->GetLocations();
4067 Location out = locations->Out();
4068 Location in = locations->InAt(0);
4069 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4070}
4071
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004072void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004073 LocationSummary* locations =
4074 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004075 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004076 case Primitive::kPrimBoolean:
4077 case Primitive::kPrimByte:
4078 case Primitive::kPrimShort:
4079 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004080 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004081 case Primitive::kPrimLong: {
4082 locations->SetInAt(0, Location::RequiresRegister());
4083 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004084 // Output overlaps because it is written before doing the low comparison.
4085 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004086 break;
4087 }
4088 case Primitive::kPrimFloat:
4089 case Primitive::kPrimDouble: {
4090 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004091 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004092 locations->SetOut(Location::RequiresRegister());
4093 break;
4094 }
4095 default:
4096 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4097 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004098}
4099
4100void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004101 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004102 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004103 Location left = locations->InAt(0);
4104 Location right = locations->InAt(1);
4105
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004106 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004107 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004108 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004109 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004110 case Primitive::kPrimBoolean:
4111 case Primitive::kPrimByte:
4112 case Primitive::kPrimShort:
4113 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004114 case Primitive::kPrimInt: {
4115 __ LoadImmediate(out, 0);
4116 __ cmp(left.AsRegister<Register>(),
4117 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4118 less_cond = LT;
4119 break;
4120 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004121 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004122 __ cmp(left.AsRegisterPairHigh<Register>(),
4123 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004124 __ b(&less, LT);
4125 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004126 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004127 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004128 __ cmp(left.AsRegisterPairLow<Register>(),
4129 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004130 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004131 break;
4132 }
4133 case Primitive::kPrimFloat:
4134 case Primitive::kPrimDouble: {
4135 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004136 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004137 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004138 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004139 break;
4140 }
4141 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004142 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004143 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004144 }
Aart Bika19616e2016-02-01 18:57:58 -08004145
Calin Juravleddb7df22014-11-25 20:56:51 +00004146 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004147 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004148
4149 __ Bind(&greater);
4150 __ LoadImmediate(out, 1);
4151 __ b(&done);
4152
4153 __ Bind(&less);
4154 __ LoadImmediate(out, -1);
4155
4156 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004157}
4158
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004159void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004160 LocationSummary* locations =
4161 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004162 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004163 locations->SetInAt(i, Location::Any());
4164 }
4165 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004166}
4167
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004168void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004169 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004170}
4171
Roland Levillainc9285912015-12-18 10:38:42 +00004172void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4173 // TODO (ported from quick): revisit ARM barrier kinds.
4174 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004175 switch (kind) {
4176 case MemBarrierKind::kAnyStore:
4177 case MemBarrierKind::kLoadAny:
4178 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004179 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004180 break;
4181 }
4182 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004183 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004184 break;
4185 }
4186 default:
4187 LOG(FATAL) << "Unexpected memory barrier " << kind;
4188 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004189 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004190}
4191
4192void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4193 uint32_t offset,
4194 Register out_lo,
4195 Register out_hi) {
4196 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004197 // Ensure `out_lo` is different from `addr`, so that loading
4198 // `offset` into `out_lo` does not clutter `addr`.
4199 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004200 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004201 __ add(IP, addr, ShifterOperand(out_lo));
4202 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004203 }
4204 __ ldrexd(out_lo, out_hi, addr);
4205}
4206
4207void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4208 uint32_t offset,
4209 Register value_lo,
4210 Register value_hi,
4211 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004212 Register temp2,
4213 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004214 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004215 if (offset != 0) {
4216 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004217 __ add(IP, addr, ShifterOperand(temp1));
4218 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004219 }
4220 __ Bind(&fail);
4221 // We need a load followed by store. (The address used in a STREX instruction must
4222 // be the same as the address in the most recently executed LDREX instruction.)
4223 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004224 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004225 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004226 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004227}
4228
4229void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4230 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4231
Nicolas Geoffray39468442014-09-02 15:17:15 +01004232 LocationSummary* locations =
4233 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004234 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004235
Calin Juravle52c48962014-12-16 17:02:57 +00004236 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004237 if (Primitive::IsFloatingPointType(field_type)) {
4238 locations->SetInAt(1, Location::RequiresFpuRegister());
4239 } else {
4240 locations->SetInAt(1, Location::RequiresRegister());
4241 }
4242
Calin Juravle52c48962014-12-16 17:02:57 +00004243 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004244 bool generate_volatile = field_info.IsVolatile()
4245 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004246 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004247 bool needs_write_barrier =
4248 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004249 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004250 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004251 if (needs_write_barrier) {
4252 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004253 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004254 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004255 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004256 // - registers need to be consecutive
4257 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004258 // We don't test for ARM yet, and the assertion makes sure that we
4259 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004260 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4261
4262 locations->AddTemp(Location::RequiresRegister());
4263 locations->AddTemp(Location::RequiresRegister());
4264 if (field_type == Primitive::kPrimDouble) {
4265 // For doubles we need two more registers to copy the value.
4266 locations->AddTemp(Location::RegisterLocation(R2));
4267 locations->AddTemp(Location::RegisterLocation(R3));
4268 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004269 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004270}
4271
Calin Juravle52c48962014-12-16 17:02:57 +00004272void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004273 const FieldInfo& field_info,
4274 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004275 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4276
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004277 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004278 Register base = locations->InAt(0).AsRegister<Register>();
4279 Location value = locations->InAt(1);
4280
4281 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004282 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004283 Primitive::Type field_type = field_info.GetFieldType();
4284 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004285 bool needs_write_barrier =
4286 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004287
4288 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004289 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004290 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004291
4292 switch (field_type) {
4293 case Primitive::kPrimBoolean:
4294 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004295 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004296 break;
4297 }
4298
4299 case Primitive::kPrimShort:
4300 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004301 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004302 break;
4303 }
4304
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004305 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004306 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004307 if (kPoisonHeapReferences && needs_write_barrier) {
4308 // Note that in the case where `value` is a null reference,
4309 // we do not enter this block, as a null reference does not
4310 // need poisoning.
4311 DCHECK_EQ(field_type, Primitive::kPrimNot);
4312 Register temp = locations->GetTemp(0).AsRegister<Register>();
4313 __ Mov(temp, value.AsRegister<Register>());
4314 __ PoisonHeapReference(temp);
4315 __ StoreToOffset(kStoreWord, temp, base, offset);
4316 } else {
4317 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4318 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004319 break;
4320 }
4321
4322 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004323 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004324 GenerateWideAtomicStore(base, offset,
4325 value.AsRegisterPairLow<Register>(),
4326 value.AsRegisterPairHigh<Register>(),
4327 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004328 locations->GetTemp(1).AsRegister<Register>(),
4329 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004330 } else {
4331 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004332 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004333 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004334 break;
4335 }
4336
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004337 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004338 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004339 break;
4340 }
4341
4342 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004343 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004344 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004345 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4346 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4347
4348 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4349
4350 GenerateWideAtomicStore(base, offset,
4351 value_reg_lo,
4352 value_reg_hi,
4353 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004354 locations->GetTemp(3).AsRegister<Register>(),
4355 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004356 } else {
4357 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004358 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004359 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004360 break;
4361 }
4362
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004363 case Primitive::kPrimVoid:
4364 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004365 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004366 }
Calin Juravle52c48962014-12-16 17:02:57 +00004367
Calin Juravle77520bc2015-01-12 18:45:46 +00004368 // Longs and doubles are handled in the switch.
4369 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4370 codegen_->MaybeRecordImplicitNullCheck(instruction);
4371 }
4372
4373 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4374 Register temp = locations->GetTemp(0).AsRegister<Register>();
4375 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004376 codegen_->MarkGCCard(
4377 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004378 }
4379
Calin Juravle52c48962014-12-16 17:02:57 +00004380 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004381 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004382 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004383}
4384
Calin Juravle52c48962014-12-16 17:02:57 +00004385void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4386 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004387
4388 bool object_field_get_with_read_barrier =
4389 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004390 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004391 new (GetGraph()->GetArena()) LocationSummary(instruction,
4392 object_field_get_with_read_barrier ?
4393 LocationSummary::kCallOnSlowPath :
4394 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004395 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004396 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004397 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004398 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004399
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004400 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004401 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004402 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004403 // The output overlaps in case of volatile long: we don't want the
4404 // code generated by GenerateWideAtomicLoad to overwrite the
4405 // object's location. Likewise, in the case of an object field get
4406 // with read barriers enabled, we do not want the load to overwrite
4407 // the object's location, as we need it to emit the read barrier.
4408 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4409 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004410
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004411 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4412 locations->SetOut(Location::RequiresFpuRegister());
4413 } else {
4414 locations->SetOut(Location::RequiresRegister(),
4415 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4416 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004417 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004418 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004419 // - registers need to be consecutive
4420 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004421 // We don't test for ARM yet, and the assertion makes sure that we
4422 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004423 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4424 locations->AddTemp(Location::RequiresRegister());
4425 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004426 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4427 // We need a temporary register for the read barrier marking slow
4428 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4429 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004430 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004431}
4432
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004433Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4434 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4435 << input->GetType();
4436 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4437 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4438 return Location::ConstantLocation(input->AsConstant());
4439 } else {
4440 return Location::RequiresFpuRegister();
4441 }
4442}
4443
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004444Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4445 Opcode opcode) {
4446 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4447 if (constant->IsConstant() &&
4448 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4449 return Location::ConstantLocation(constant->AsConstant());
4450 }
4451 return Location::RequiresRegister();
4452}
4453
4454bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4455 Opcode opcode) {
4456 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4457 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004458 Opcode high_opcode = opcode;
4459 SetCc low_set_cc = kCcDontCare;
4460 switch (opcode) {
4461 case SUB:
4462 // Flip the operation to an ADD.
4463 value = -value;
4464 opcode = ADD;
4465 FALLTHROUGH_INTENDED;
4466 case ADD:
4467 if (Low32Bits(value) == 0u) {
4468 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4469 }
4470 high_opcode = ADC;
4471 low_set_cc = kCcSet;
4472 break;
4473 default:
4474 break;
4475 }
4476 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4477 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004478 } else {
4479 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4480 }
4481}
4482
Vladimir Marko59751a72016-08-05 14:37:27 +01004483bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4484 Opcode opcode,
4485 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004486 ShifterOperand so;
4487 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004488 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004489 return true;
4490 }
4491 Opcode neg_opcode = kNoOperand;
4492 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004493 case AND: neg_opcode = BIC; value = ~value; break;
4494 case ORR: neg_opcode = ORN; value = ~value; break;
4495 case ADD: neg_opcode = SUB; value = -value; break;
4496 case ADC: neg_opcode = SBC; value = ~value; break;
4497 case SUB: neg_opcode = ADD; value = -value; break;
4498 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004499 default:
4500 return false;
4501 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004502 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004503}
4504
Calin Juravle52c48962014-12-16 17:02:57 +00004505void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4506 const FieldInfo& field_info) {
4507 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004508
Calin Juravle52c48962014-12-16 17:02:57 +00004509 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004510 Location base_loc = locations->InAt(0);
4511 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004512 Location out = locations->Out();
4513 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004514 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004515 Primitive::Type field_type = field_info.GetFieldType();
4516 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4517
4518 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004519 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004520 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004521 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004522
Roland Levillainc9285912015-12-18 10:38:42 +00004523 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004524 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004525 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004526
Roland Levillainc9285912015-12-18 10:38:42 +00004527 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004528 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004529 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004530
Roland Levillainc9285912015-12-18 10:38:42 +00004531 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004532 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004533 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004534
4535 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004536 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004537 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004538
4539 case Primitive::kPrimNot: {
4540 // /* HeapReference<Object> */ out = *(base + offset)
4541 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4542 Location temp_loc = locations->GetTemp(0);
4543 // Note that a potential implicit null check is handled in this
4544 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4545 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4546 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4547 if (is_volatile) {
4548 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4549 }
4550 } else {
4551 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4552 codegen_->MaybeRecordImplicitNullCheck(instruction);
4553 if (is_volatile) {
4554 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4555 }
4556 // If read barriers are enabled, emit read barriers other than
4557 // Baker's using a slow path (and also unpoison the loaded
4558 // reference, if heap poisoning is enabled).
4559 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4560 }
4561 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004562 }
4563
Roland Levillainc9285912015-12-18 10:38:42 +00004564 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004565 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004566 GenerateWideAtomicLoad(base, offset,
4567 out.AsRegisterPairLow<Register>(),
4568 out.AsRegisterPairHigh<Register>());
4569 } else {
4570 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4571 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004572 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004573
Roland Levillainc9285912015-12-18 10:38:42 +00004574 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004575 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004576 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004577
4578 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004579 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004580 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004581 Register lo = locations->GetTemp(0).AsRegister<Register>();
4582 Register hi = locations->GetTemp(1).AsRegister<Register>();
4583 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004584 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004585 __ vmovdrr(out_reg, lo, hi);
4586 } else {
4587 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004588 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004589 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004590 break;
4591 }
4592
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004593 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004594 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004595 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004596 }
Calin Juravle52c48962014-12-16 17:02:57 +00004597
Roland Levillainc9285912015-12-18 10:38:42 +00004598 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4599 // Potential implicit null checks, in the case of reference or
4600 // double fields, are handled in the previous switch statement.
4601 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004602 codegen_->MaybeRecordImplicitNullCheck(instruction);
4603 }
4604
Calin Juravle52c48962014-12-16 17:02:57 +00004605 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004606 if (field_type == Primitive::kPrimNot) {
4607 // Memory barriers, in the case of references, are also handled
4608 // in the previous switch statement.
4609 } else {
4610 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4611 }
Roland Levillain4d027112015-07-01 15:41:14 +01004612 }
Calin Juravle52c48962014-12-16 17:02:57 +00004613}
4614
4615void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4616 HandleFieldSet(instruction, instruction->GetFieldInfo());
4617}
4618
4619void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004620 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004621}
4622
4623void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4624 HandleFieldGet(instruction, instruction->GetFieldInfo());
4625}
4626
4627void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4628 HandleFieldGet(instruction, instruction->GetFieldInfo());
4629}
4630
4631void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4632 HandleFieldGet(instruction, instruction->GetFieldInfo());
4633}
4634
4635void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4636 HandleFieldGet(instruction, instruction->GetFieldInfo());
4637}
4638
4639void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4640 HandleFieldSet(instruction, instruction->GetFieldInfo());
4641}
4642
4643void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004644 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004645}
4646
Calin Juravlee460d1d2015-09-29 04:52:17 +01004647void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4648 HUnresolvedInstanceFieldGet* instruction) {
4649 FieldAccessCallingConventionARM calling_convention;
4650 codegen_->CreateUnresolvedFieldLocationSummary(
4651 instruction, instruction->GetFieldType(), calling_convention);
4652}
4653
4654void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4655 HUnresolvedInstanceFieldGet* instruction) {
4656 FieldAccessCallingConventionARM calling_convention;
4657 codegen_->GenerateUnresolvedFieldAccess(instruction,
4658 instruction->GetFieldType(),
4659 instruction->GetFieldIndex(),
4660 instruction->GetDexPc(),
4661 calling_convention);
4662}
4663
4664void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4665 HUnresolvedInstanceFieldSet* instruction) {
4666 FieldAccessCallingConventionARM calling_convention;
4667 codegen_->CreateUnresolvedFieldLocationSummary(
4668 instruction, instruction->GetFieldType(), calling_convention);
4669}
4670
4671void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4672 HUnresolvedInstanceFieldSet* instruction) {
4673 FieldAccessCallingConventionARM calling_convention;
4674 codegen_->GenerateUnresolvedFieldAccess(instruction,
4675 instruction->GetFieldType(),
4676 instruction->GetFieldIndex(),
4677 instruction->GetDexPc(),
4678 calling_convention);
4679}
4680
4681void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4682 HUnresolvedStaticFieldGet* instruction) {
4683 FieldAccessCallingConventionARM calling_convention;
4684 codegen_->CreateUnresolvedFieldLocationSummary(
4685 instruction, instruction->GetFieldType(), calling_convention);
4686}
4687
4688void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4689 HUnresolvedStaticFieldGet* instruction) {
4690 FieldAccessCallingConventionARM calling_convention;
4691 codegen_->GenerateUnresolvedFieldAccess(instruction,
4692 instruction->GetFieldType(),
4693 instruction->GetFieldIndex(),
4694 instruction->GetDexPc(),
4695 calling_convention);
4696}
4697
4698void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4699 HUnresolvedStaticFieldSet* instruction) {
4700 FieldAccessCallingConventionARM calling_convention;
4701 codegen_->CreateUnresolvedFieldLocationSummary(
4702 instruction, instruction->GetFieldType(), calling_convention);
4703}
4704
4705void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4706 HUnresolvedStaticFieldSet* instruction) {
4707 FieldAccessCallingConventionARM calling_convention;
4708 codegen_->GenerateUnresolvedFieldAccess(instruction,
4709 instruction->GetFieldType(),
4710 instruction->GetFieldIndex(),
4711 instruction->GetDexPc(),
4712 calling_convention);
4713}
4714
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004715void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004716 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4717 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004718}
4719
Calin Juravle2ae48182016-03-16 14:05:09 +00004720void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4721 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004722 return;
4723 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004724 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004725
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004726 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004727 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004728}
4729
Calin Juravle2ae48182016-03-16 14:05:09 +00004730void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004731 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004732 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004733
4734 LocationSummary* locations = instruction->GetLocations();
4735 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004736
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004737 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004738}
4739
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004740void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004741 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004742}
4743
Artem Serov6c916792016-07-11 14:02:34 +01004744static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4745 switch (type) {
4746 case Primitive::kPrimNot:
4747 return kLoadWord;
4748 case Primitive::kPrimBoolean:
4749 return kLoadUnsignedByte;
4750 case Primitive::kPrimByte:
4751 return kLoadSignedByte;
4752 case Primitive::kPrimChar:
4753 return kLoadUnsignedHalfword;
4754 case Primitive::kPrimShort:
4755 return kLoadSignedHalfword;
4756 case Primitive::kPrimInt:
4757 return kLoadWord;
4758 case Primitive::kPrimLong:
4759 return kLoadWordPair;
4760 case Primitive::kPrimFloat:
4761 return kLoadSWord;
4762 case Primitive::kPrimDouble:
4763 return kLoadDWord;
4764 default:
4765 LOG(FATAL) << "Unreachable type " << type;
4766 UNREACHABLE();
4767 }
4768}
4769
4770static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4771 switch (type) {
4772 case Primitive::kPrimNot:
4773 return kStoreWord;
4774 case Primitive::kPrimBoolean:
4775 case Primitive::kPrimByte:
4776 return kStoreByte;
4777 case Primitive::kPrimChar:
4778 case Primitive::kPrimShort:
4779 return kStoreHalfword;
4780 case Primitive::kPrimInt:
4781 return kStoreWord;
4782 case Primitive::kPrimLong:
4783 return kStoreWordPair;
4784 case Primitive::kPrimFloat:
4785 return kStoreSWord;
4786 case Primitive::kPrimDouble:
4787 return kStoreDWord;
4788 default:
4789 LOG(FATAL) << "Unreachable type " << type;
4790 UNREACHABLE();
4791 }
4792}
4793
4794void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4795 Location out_loc,
4796 Register base,
4797 Register reg_offset,
4798 Condition cond) {
4799 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4800 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4801
4802 switch (type) {
4803 case Primitive::kPrimByte:
4804 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4805 break;
4806 case Primitive::kPrimBoolean:
4807 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4808 break;
4809 case Primitive::kPrimShort:
4810 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4811 break;
4812 case Primitive::kPrimChar:
4813 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4814 break;
4815 case Primitive::kPrimNot:
4816 case Primitive::kPrimInt:
4817 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4818 break;
4819 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4820 case Primitive::kPrimLong:
4821 case Primitive::kPrimFloat:
4822 case Primitive::kPrimDouble:
4823 default:
4824 LOG(FATAL) << "Unreachable type " << type;
4825 UNREACHABLE();
4826 }
4827}
4828
4829void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4830 Location loc,
4831 Register base,
4832 Register reg_offset,
4833 Condition cond) {
4834 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4835 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4836
4837 switch (type) {
4838 case Primitive::kPrimByte:
4839 case Primitive::kPrimBoolean:
4840 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4841 break;
4842 case Primitive::kPrimShort:
4843 case Primitive::kPrimChar:
4844 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4845 break;
4846 case Primitive::kPrimNot:
4847 case Primitive::kPrimInt:
4848 __ str(loc.AsRegister<Register>(), mem_address, cond);
4849 break;
4850 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4851 case Primitive::kPrimLong:
4852 case Primitive::kPrimFloat:
4853 case Primitive::kPrimDouble:
4854 default:
4855 LOG(FATAL) << "Unreachable type " << type;
4856 UNREACHABLE();
4857 }
4858}
4859
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004860void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004861 bool object_array_get_with_read_barrier =
4862 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004863 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004864 new (GetGraph()->GetArena()) LocationSummary(instruction,
4865 object_array_get_with_read_barrier ?
4866 LocationSummary::kCallOnSlowPath :
4867 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004868 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004869 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004870 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004871 locations->SetInAt(0, Location::RequiresRegister());
4872 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004873 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4874 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4875 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004876 // The output overlaps in the case of an object array get with
4877 // read barriers enabled: we do not want the move to overwrite the
4878 // array's location, as we need it to emit the read barrier.
4879 locations->SetOut(
4880 Location::RequiresRegister(),
4881 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004882 }
Roland Levillainc9285912015-12-18 10:38:42 +00004883 // We need a temporary register for the read barrier marking slow
4884 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07004885 // Also need for String compression feature.
4886 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4887 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00004888 locations->AddTemp(Location::RequiresRegister());
4889 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004890}
4891
4892void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4893 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004894 Location obj_loc = locations->InAt(0);
4895 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004896 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004897 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004898 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004899 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07004900 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4901 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01004902 HInstruction* array_instr = instruction->GetArray();
4903 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01004904
Roland Levillain4d027112015-07-01 15:41:14 +01004905 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004906 case Primitive::kPrimBoolean:
4907 case Primitive::kPrimByte:
4908 case Primitive::kPrimShort:
4909 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004910 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004911 Register length;
4912 if (maybe_compressed_char_at) {
4913 length = locations->GetTemp(0).AsRegister<Register>();
4914 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4915 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4916 codegen_->MaybeRecordImplicitNullCheck(instruction);
4917 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004918 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004919 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07004920 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07004921 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004922 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4923 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4924 "Expecting 0=compressed, 1=uncompressed");
4925 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004926 __ LoadFromOffset(kLoadUnsignedByte,
4927 out_loc.AsRegister<Register>(),
4928 obj,
4929 data_offset + const_index);
4930 __ b(&done);
4931 __ Bind(&uncompressed_load);
4932 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4933 out_loc.AsRegister<Register>(),
4934 obj,
4935 data_offset + (const_index << 1));
4936 __ Bind(&done);
4937 } else {
4938 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01004939
jessicahandojo05765752016-09-09 19:01:32 -07004940 LoadOperandType load_type = GetLoadOperandType(type);
4941 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4942 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004943 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004944 Register temp = IP;
4945
4946 if (has_intermediate_address) {
4947 // We do not need to compute the intermediate address from the array: the
4948 // input instruction has done it already. See the comment in
4949 // `TryExtractArrayAccessAddress()`.
4950 if (kIsDebugBuild) {
4951 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4952 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4953 }
4954 temp = obj;
4955 } else {
4956 __ add(temp, obj, ShifterOperand(data_offset));
4957 }
jessicahandojo05765752016-09-09 19:01:32 -07004958 if (maybe_compressed_char_at) {
4959 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004960 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4961 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4962 "Expecting 0=compressed, 1=uncompressed");
4963 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004964 __ ldrb(out_loc.AsRegister<Register>(),
4965 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
4966 __ b(&done);
4967 __ Bind(&uncompressed_load);
4968 __ ldrh(out_loc.AsRegister<Register>(),
4969 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
4970 __ Bind(&done);
4971 } else {
4972 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4973 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004974 }
4975 break;
4976 }
4977
Roland Levillainc9285912015-12-18 10:38:42 +00004978 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00004979 // The read barrier instrumentation of object ArrayGet
4980 // instructions does not support the HIntermediateAddress
4981 // instruction.
4982 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4983
Roland Levillainc9285912015-12-18 10:38:42 +00004984 static_assert(
4985 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4986 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004987 // /* HeapReference<Object> */ out =
4988 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4989 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4990 Location temp = locations->GetTemp(0);
4991 // Note that a potential implicit null check is handled in this
4992 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4993 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4994 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4995 } else {
4996 Register out = out_loc.AsRegister<Register>();
4997 if (index.IsConstant()) {
4998 size_t offset =
4999 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5000 __ LoadFromOffset(kLoadWord, out, obj, offset);
5001 codegen_->MaybeRecordImplicitNullCheck(instruction);
5002 // If read barriers are enabled, emit read barriers other than
5003 // Baker's using a slow path (and also unpoison the loaded
5004 // reference, if heap poisoning is enabled).
5005 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5006 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005007 Register temp = IP;
5008
5009 if (has_intermediate_address) {
5010 // We do not need to compute the intermediate address from the array: the
5011 // input instruction has done it already. See the comment in
5012 // `TryExtractArrayAccessAddress()`.
5013 if (kIsDebugBuild) {
5014 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5015 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5016 }
5017 temp = obj;
5018 } else {
5019 __ add(temp, obj, ShifterOperand(data_offset));
5020 }
5021 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005022
Roland Levillainc9285912015-12-18 10:38:42 +00005023 codegen_->MaybeRecordImplicitNullCheck(instruction);
5024 // If read barriers are enabled, emit read barriers other than
5025 // Baker's using a slow path (and also unpoison the loaded
5026 // reference, if heap poisoning is enabled).
5027 codegen_->MaybeGenerateReadBarrierSlow(
5028 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5029 }
5030 }
5031 break;
5032 }
5033
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005034 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005035 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005036 size_t offset =
5037 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005038 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005039 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005040 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005041 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005042 }
5043 break;
5044 }
5045
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005046 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005047 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005048 if (index.IsConstant()) {
5049 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005050 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005051 } else {
5052 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005053 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005054 }
5055 break;
5056 }
5057
5058 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005059 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005060 if (index.IsConstant()) {
5061 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005062 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005063 } else {
5064 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005065 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005066 }
5067 break;
5068 }
5069
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005070 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005071 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005072 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005073 }
Roland Levillain4d027112015-07-01 15:41:14 +01005074
5075 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005076 // Potential implicit null checks, in the case of reference
5077 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005078 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005079 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005080 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081}
5082
5083void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005084 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005085
5086 bool needs_write_barrier =
5087 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005088 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005089
Nicolas Geoffray39468442014-09-02 15:17:15 +01005090 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005091 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005092 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005093 LocationSummary::kCallOnSlowPath :
5094 LocationSummary::kNoCall);
5095
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005096 locations->SetInAt(0, Location::RequiresRegister());
5097 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5098 if (Primitive::IsFloatingPointType(value_type)) {
5099 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005100 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005101 locations->SetInAt(2, Location::RequiresRegister());
5102 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005103 if (needs_write_barrier) {
5104 // Temporary registers for the write barrier.
5105 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005106 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005107 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108}
5109
5110void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5111 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005112 Location array_loc = locations->InAt(0);
5113 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005114 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005115 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005116 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005117 bool needs_write_barrier =
5118 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005119 uint32_t data_offset =
5120 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5121 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005122 HInstruction* array_instr = instruction->GetArray();
5123 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124
5125 switch (value_type) {
5126 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005127 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005128 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005129 case Primitive::kPrimChar:
5130 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005131 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005132 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5133 uint32_t full_offset =
5134 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5135 StoreOperandType store_type = GetStoreOperandType(value_type);
5136 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005138 Register temp = IP;
5139
5140 if (has_intermediate_address) {
5141 // We do not need to compute the intermediate address from the array: the
5142 // input instruction has done it already. See the comment in
5143 // `TryExtractArrayAccessAddress()`.
5144 if (kIsDebugBuild) {
5145 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5146 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5147 }
5148 temp = array;
5149 } else {
5150 __ add(temp, array, ShifterOperand(data_offset));
5151 }
Artem Serov6c916792016-07-11 14:02:34 +01005152 codegen_->StoreToShiftedRegOffset(value_type,
5153 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005154 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005155 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005156 }
5157 break;
5158 }
5159
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005161 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005162 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5163 // See the comment in instruction_simplifier_shared.cc.
5164 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005165
5166 if (instruction->InputAt(2)->IsNullConstant()) {
5167 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005168 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005169 size_t offset =
5170 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005171 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005172 } else {
5173 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005174 __ add(IP, array, ShifterOperand(data_offset));
5175 codegen_->StoreToShiftedRegOffset(value_type,
5176 value_loc,
5177 IP,
5178 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005179 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005180 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005181 DCHECK(!needs_write_barrier);
5182 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005183 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005184 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005185
5186 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005187 Location temp1_loc = locations->GetTemp(0);
5188 Register temp1 = temp1_loc.AsRegister<Register>();
5189 Location temp2_loc = locations->GetTemp(1);
5190 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005191 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5192 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5193 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5194 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005195 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005196
Roland Levillain3b359c72015-11-17 19:35:12 +00005197 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005198 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5199 codegen_->AddSlowPath(slow_path);
5200 if (instruction->GetValueCanBeNull()) {
5201 Label non_zero;
5202 __ CompareAndBranchIfNonZero(value, &non_zero);
5203 if (index.IsConstant()) {
5204 size_t offset =
5205 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5206 __ StoreToOffset(kStoreWord, value, array, offset);
5207 } else {
5208 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005209 __ add(IP, array, ShifterOperand(data_offset));
5210 codegen_->StoreToShiftedRegOffset(value_type,
5211 value_loc,
5212 IP,
5213 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005214 }
5215 codegen_->MaybeRecordImplicitNullCheck(instruction);
5216 __ b(&done);
5217 __ Bind(&non_zero);
5218 }
5219
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005220 // Note that when read barriers are enabled, the type checks
5221 // are performed without read barriers. This is fine, even in
5222 // the case where a class object is in the from-space after
5223 // the flip, as a comparison involving such a type would not
5224 // produce a false positive; it may of course produce a false
5225 // negative, in which case we would take the ArraySet slow
5226 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005227
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005228 // /* HeapReference<Class> */ temp1 = array->klass_
5229 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5230 codegen_->MaybeRecordImplicitNullCheck(instruction);
5231 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005232
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005233 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5234 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5235 // /* HeapReference<Class> */ temp2 = value->klass_
5236 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5237 // If heap poisoning is enabled, no need to unpoison `temp1`
5238 // nor `temp2`, as we are comparing two poisoned references.
5239 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005240
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005241 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5242 Label do_put;
5243 __ b(&do_put, EQ);
5244 // If heap poisoning is enabled, the `temp1` reference has
5245 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005246 __ MaybeUnpoisonHeapReference(temp1);
5247
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005248 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5249 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5250 // If heap poisoning is enabled, no need to unpoison
5251 // `temp1`, as we are comparing against null below.
5252 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5253 __ Bind(&do_put);
5254 } else {
5255 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005256 }
5257 }
5258
Artem Serov6c916792016-07-11 14:02:34 +01005259 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005260 if (kPoisonHeapReferences) {
5261 // Note that in the case where `value` is a null reference,
5262 // we do not enter this block, as a null reference does not
5263 // need poisoning.
5264 DCHECK_EQ(value_type, Primitive::kPrimNot);
5265 __ Mov(temp1, value);
5266 __ PoisonHeapReference(temp1);
5267 source = temp1;
5268 }
5269
5270 if (index.IsConstant()) {
5271 size_t offset =
5272 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5273 __ StoreToOffset(kStoreWord, source, array, offset);
5274 } else {
5275 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005276
5277 __ add(IP, array, ShifterOperand(data_offset));
5278 codegen_->StoreToShiftedRegOffset(value_type,
5279 Location::RegisterLocation(source),
5280 IP,
5281 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005282 }
5283
Roland Levillain3b359c72015-11-17 19:35:12 +00005284 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005285 codegen_->MaybeRecordImplicitNullCheck(instruction);
5286 }
5287
5288 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5289
5290 if (done.IsLinked()) {
5291 __ Bind(&done);
5292 }
5293
5294 if (slow_path != nullptr) {
5295 __ Bind(slow_path->GetExitLabel());
5296 }
5297
5298 break;
5299 }
5300
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005301 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005302 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005303 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005304 size_t offset =
5305 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005306 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005307 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005308 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005309 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005310 }
5311 break;
5312 }
5313
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005314 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005315 Location value = locations->InAt(2);
5316 DCHECK(value.IsFpuRegister());
5317 if (index.IsConstant()) {
5318 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005319 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005320 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005321 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005322 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5323 }
5324 break;
5325 }
5326
5327 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005328 Location value = locations->InAt(2);
5329 DCHECK(value.IsFpuRegisterPair());
5330 if (index.IsConstant()) {
5331 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005332 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005333 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005334 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005335 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5336 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005337
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005338 break;
5339 }
5340
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005341 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005342 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005343 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005344 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005345
Roland Levillain80e67092016-01-08 16:04:55 +00005346 // Objects are handled in the switch.
5347 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005348 codegen_->MaybeRecordImplicitNullCheck(instruction);
5349 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005350}
5351
5352void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005353 LocationSummary* locations =
5354 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005355 locations->SetInAt(0, Location::RequiresRegister());
5356 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005357}
5358
5359void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5360 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005361 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005362 Register obj = locations->InAt(0).AsRegister<Register>();
5363 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005364 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005365 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005366 // Mask out compression flag from String's array length.
5367 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005368 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07005369 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005370}
5371
Artem Serov328429f2016-07-06 16:23:04 +01005372void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005373 LocationSummary* locations =
5374 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5375
5376 locations->SetInAt(0, Location::RequiresRegister());
5377 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5378 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5379}
5380
5381void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5382 LocationSummary* locations = instruction->GetLocations();
5383 Location out = locations->Out();
5384 Location first = locations->InAt(0);
5385 Location second = locations->InAt(1);
5386
Artem Serov328429f2016-07-06 16:23:04 +01005387 if (second.IsRegister()) {
5388 __ add(out.AsRegister<Register>(),
5389 first.AsRegister<Register>(),
5390 ShifterOperand(second.AsRegister<Register>()));
5391 } else {
5392 __ AddConstant(out.AsRegister<Register>(),
5393 first.AsRegister<Register>(),
5394 second.GetConstant()->AsIntConstant()->GetValue());
5395 }
5396}
5397
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005398void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005399 RegisterSet caller_saves = RegisterSet::Empty();
5400 InvokeRuntimeCallingConvention calling_convention;
5401 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5402 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5403 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005404 locations->SetInAt(0, Location::RequiresRegister());
5405 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406}
5407
5408void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5409 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005410 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005411 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005412 codegen_->AddSlowPath(slow_path);
5413
Roland Levillain271ab9c2014-11-27 15:23:57 +00005414 Register index = locations->InAt(0).AsRegister<Register>();
5415 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005416
5417 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005418 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005419}
5420
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005421void CodeGeneratorARM::MarkGCCard(Register temp,
5422 Register card,
5423 Register object,
5424 Register value,
5425 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005426 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005427 if (can_be_null) {
5428 __ CompareAndBranchIfZero(value, &is_null);
5429 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005430 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005431 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5432 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005433 if (can_be_null) {
5434 __ Bind(&is_null);
5435 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005436}
5437
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005438void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005439 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005440}
5441
5442void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005443 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5444}
5445
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005446void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005447 LocationSummary* locations =
5448 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005449 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005450}
5451
5452void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005453 HBasicBlock* block = instruction->GetBlock();
5454 if (block->GetLoopInformation() != nullptr) {
5455 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5456 // The back edge will generate the suspend check.
5457 return;
5458 }
5459 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5460 // The goto will generate the suspend check.
5461 return;
5462 }
5463 GenerateSuspendCheck(instruction, nullptr);
5464}
5465
5466void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5467 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005468 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005469 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5470 if (slow_path == nullptr) {
5471 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5472 instruction->SetSlowPath(slow_path);
5473 codegen_->AddSlowPath(slow_path);
5474 if (successor != nullptr) {
5475 DCHECK(successor->IsLoopHeader());
5476 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5477 }
5478 } else {
5479 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5480 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005481
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005482 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005483 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005484 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005485 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005486 __ Bind(slow_path->GetReturnLabel());
5487 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005488 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005489 __ b(slow_path->GetEntryLabel());
5490 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005491}
5492
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005493ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5494 return codegen_->GetAssembler();
5495}
5496
5497void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005498 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005499 Location source = move->GetSource();
5500 Location destination = move->GetDestination();
5501
5502 if (source.IsRegister()) {
5503 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005504 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005505 } else if (destination.IsFpuRegister()) {
5506 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005507 } else {
5508 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005509 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005510 SP, destination.GetStackIndex());
5511 }
5512 } else if (source.IsStackSlot()) {
5513 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005514 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005515 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005516 } else if (destination.IsFpuRegister()) {
5517 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005518 } else {
5519 DCHECK(destination.IsStackSlot());
5520 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5521 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5522 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005523 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005524 if (destination.IsRegister()) {
5525 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5526 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005527 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005528 } else {
5529 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005530 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5531 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005532 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005533 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005534 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5535 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005536 } else if (destination.IsRegisterPair()) {
5537 DCHECK(ExpectedPairLayout(destination));
5538 __ LoadFromOffset(
5539 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5540 } else {
5541 DCHECK(destination.IsFpuRegisterPair()) << destination;
5542 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5543 SP,
5544 source.GetStackIndex());
5545 }
5546 } else if (source.IsRegisterPair()) {
5547 if (destination.IsRegisterPair()) {
5548 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5549 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005550 } else if (destination.IsFpuRegisterPair()) {
5551 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5552 source.AsRegisterPairLow<Register>(),
5553 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005554 } else {
5555 DCHECK(destination.IsDoubleStackSlot()) << destination;
5556 DCHECK(ExpectedPairLayout(source));
5557 __ StoreToOffset(
5558 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5559 }
5560 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005561 if (destination.IsRegisterPair()) {
5562 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5563 destination.AsRegisterPairHigh<Register>(),
5564 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5565 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005566 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5567 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5568 } else {
5569 DCHECK(destination.IsDoubleStackSlot()) << destination;
5570 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5571 SP,
5572 destination.GetStackIndex());
5573 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005574 } else {
5575 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005576 HConstant* constant = source.GetConstant();
5577 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5578 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005579 if (destination.IsRegister()) {
5580 __ LoadImmediate(destination.AsRegister<Register>(), value);
5581 } else {
5582 DCHECK(destination.IsStackSlot());
5583 __ LoadImmediate(IP, value);
5584 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5585 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005586 } else if (constant->IsLongConstant()) {
5587 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005588 if (destination.IsRegisterPair()) {
5589 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5590 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005591 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005592 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005593 __ LoadImmediate(IP, Low32Bits(value));
5594 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5595 __ LoadImmediate(IP, High32Bits(value));
5596 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5597 }
5598 } else if (constant->IsDoubleConstant()) {
5599 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005600 if (destination.IsFpuRegisterPair()) {
5601 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005602 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005603 DCHECK(destination.IsDoubleStackSlot()) << destination;
5604 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005605 __ LoadImmediate(IP, Low32Bits(int_value));
5606 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5607 __ LoadImmediate(IP, High32Bits(int_value));
5608 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5609 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005610 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005611 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005612 float value = constant->AsFloatConstant()->GetValue();
5613 if (destination.IsFpuRegister()) {
5614 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5615 } else {
5616 DCHECK(destination.IsStackSlot());
5617 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5618 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5619 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005620 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005621 }
5622}
5623
5624void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5625 __ Mov(IP, reg);
5626 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5627 __ StoreToOffset(kStoreWord, IP, SP, mem);
5628}
5629
5630void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5631 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5632 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5633 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5634 SP, mem1 + stack_offset);
5635 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5636 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5637 SP, mem2 + stack_offset);
5638 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5639}
5640
5641void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005642 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005643 Location source = move->GetSource();
5644 Location destination = move->GetDestination();
5645
5646 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005647 DCHECK_NE(source.AsRegister<Register>(), IP);
5648 DCHECK_NE(destination.AsRegister<Register>(), IP);
5649 __ Mov(IP, source.AsRegister<Register>());
5650 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5651 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005652 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005653 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005654 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005655 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005656 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5657 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005658 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005659 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005660 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005661 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005662 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005663 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005664 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005665 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005666 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5667 destination.AsRegisterPairHigh<Register>(),
5668 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005669 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005670 Register low_reg = source.IsRegisterPair()
5671 ? source.AsRegisterPairLow<Register>()
5672 : destination.AsRegisterPairLow<Register>();
5673 int mem = source.IsRegisterPair()
5674 ? destination.GetStackIndex()
5675 : source.GetStackIndex();
5676 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005677 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005678 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005679 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005680 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005681 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5682 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005683 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005684 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005685 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005686 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5687 DRegister reg = source.IsFpuRegisterPair()
5688 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5689 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5690 int mem = source.IsFpuRegisterPair()
5691 ? destination.GetStackIndex()
5692 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005693 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005694 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005695 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005696 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5697 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5698 : destination.AsFpuRegister<SRegister>();
5699 int mem = source.IsFpuRegister()
5700 ? destination.GetStackIndex()
5701 : source.GetStackIndex();
5702
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005703 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005704 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005705 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005706 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005707 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5708 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005709 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005710 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005711 }
5712}
5713
5714void ParallelMoveResolverARM::SpillScratch(int reg) {
5715 __ Push(static_cast<Register>(reg));
5716}
5717
5718void ParallelMoveResolverARM::RestoreScratch(int reg) {
5719 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005720}
5721
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005722HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5723 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005724 switch (desired_class_load_kind) {
5725 case HLoadClass::LoadKind::kReferrersClass:
5726 break;
5727 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5728 DCHECK(!GetCompilerOptions().GetCompilePic());
5729 break;
5730 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5731 DCHECK(GetCompilerOptions().GetCompilePic());
5732 break;
5733 case HLoadClass::LoadKind::kBootImageAddress:
5734 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005735 case HLoadClass::LoadKind::kBssEntry:
5736 DCHECK(!Runtime::Current()->UseJitCompilation());
5737 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005738 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005739 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005740 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005741 case HLoadClass::LoadKind::kDexCacheViaMethod:
5742 break;
5743 }
5744 return desired_class_load_kind;
5745}
5746
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005747void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005748 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5749 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005750 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005751 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005752 cls,
5753 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005754 Location::RegisterLocation(R0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005755 return;
5756 }
Vladimir Marko41559982017-01-06 14:04:23 +00005757 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005758
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005759 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5760 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005761 ? LocationSummary::kCallOnSlowPath
5762 : LocationSummary::kNoCall;
5763 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005764 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005765 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005766 }
5767
Vladimir Marko41559982017-01-06 14:04:23 +00005768 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005769 locations->SetInAt(0, Location::RequiresRegister());
5770 }
5771 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005772}
5773
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005774// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5775// move.
5776void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005777 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5778 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5779 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005780 return;
5781 }
Vladimir Marko41559982017-01-06 14:04:23 +00005782 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005783
Vladimir Marko41559982017-01-06 14:04:23 +00005784 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005785 Location out_loc = locations->Out();
5786 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005787
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005788 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5789 ? kWithoutReadBarrier
5790 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005791 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005792 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005793 case HLoadClass::LoadKind::kReferrersClass: {
5794 DCHECK(!cls->CanCallRuntime());
5795 DCHECK(!cls->MustGenerateClinitCheck());
5796 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5797 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005798 GenerateGcRootFieldLoad(cls,
5799 out_loc,
5800 current_method,
5801 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005802 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005803 break;
5804 }
5805 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005806 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005807 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005808 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5809 cls->GetTypeIndex()));
5810 break;
5811 }
5812 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005813 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005814 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005815 CodeGeneratorARM::PcRelativePatchInfo* labels =
5816 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5817 __ BindTrackedLabel(&labels->movw_label);
5818 __ movw(out, /* placeholder */ 0u);
5819 __ BindTrackedLabel(&labels->movt_label);
5820 __ movt(out, /* placeholder */ 0u);
5821 __ BindTrackedLabel(&labels->add_pc_label);
5822 __ add(out, out, ShifterOperand(PC));
5823 break;
5824 }
5825 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005826 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005827 uint32_t address = dchecked_integral_cast<uint32_t>(
5828 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5829 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005830 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5831 break;
5832 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005833 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005834 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005835 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005836 __ BindTrackedLabel(&labels->movw_label);
5837 __ movw(out, /* placeholder */ 0u);
5838 __ BindTrackedLabel(&labels->movt_label);
5839 __ movt(out, /* placeholder */ 0u);
5840 __ BindTrackedLabel(&labels->add_pc_label);
5841 __ add(out, out, ShifterOperand(PC));
5842 GenerateGcRootFieldLoad(cls, out_loc, out, 0, kCompilerReadBarrierOption);
5843 generate_null_check = true;
5844 break;
5845 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005846 case HLoadClass::LoadKind::kJitTableAddress: {
5847 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5848 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005849 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005850 // /* GcRoot<mirror::Class> */ out = *out
5851 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005852 break;
5853 }
Vladimir Marko41559982017-01-06 14:04:23 +00005854 case HLoadClass::LoadKind::kDexCacheViaMethod:
5855 LOG(FATAL) << "UNREACHABLE";
5856 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005857 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005858
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005859 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5860 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005861 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005862 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5863 codegen_->AddSlowPath(slow_path);
5864 if (generate_null_check) {
5865 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5866 }
5867 if (cls->MustGenerateClinitCheck()) {
5868 GenerateClassInitializationCheck(slow_path, out);
5869 } else {
5870 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005871 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005872 }
5873}
5874
5875void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5876 LocationSummary* locations =
5877 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5878 locations->SetInAt(0, Location::RequiresRegister());
5879 if (check->HasUses()) {
5880 locations->SetOut(Location::SameAsFirstInput());
5881 }
5882}
5883
5884void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005885 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005886 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005887 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005888 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005889 GenerateClassInitializationCheck(slow_path,
5890 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005891}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005892
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005893void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005894 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005895 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5896 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5897 __ b(slow_path->GetEntryLabel(), LT);
5898 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5899 // properly. Therefore, we do a memory fence.
5900 __ dmb(ISH);
5901 __ Bind(slow_path->GetExitLabel());
5902}
5903
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005904HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5905 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005906 switch (desired_string_load_kind) {
5907 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5908 DCHECK(!GetCompilerOptions().GetCompilePic());
5909 break;
5910 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5911 DCHECK(GetCompilerOptions().GetCompilePic());
5912 break;
5913 case HLoadString::LoadKind::kBootImageAddress:
5914 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005915 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005916 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005917 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005918 case HLoadString::LoadKind::kJitTableAddress:
5919 DCHECK(Runtime::Current()->UseJitCompilation());
5920 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005921 case HLoadString::LoadKind::kDexCacheViaMethod:
5922 break;
5923 }
5924 return desired_string_load_kind;
5925}
5926
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005927void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005928 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005929 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005930 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005931 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005932 locations->SetOut(Location::RegisterLocation(R0));
5933 } else {
5934 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005935 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5936 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5937 // Rely on the pResolveString and/or marking to save everything, including temps.
5938 // Note that IP may theoretically be clobbered by saving/restoring the live register
5939 // (only one thanks to the custom calling convention), so we request a different temp.
5940 locations->AddTemp(Location::RequiresRegister());
5941 RegisterSet caller_saves = RegisterSet::Empty();
5942 InvokeRuntimeCallingConvention calling_convention;
5943 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5944 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5945 // that the the kPrimNot result register is the same as the first argument register.
5946 locations->SetCustomSlowPathCallerSaves(caller_saves);
5947 } else {
5948 // For non-Baker read barrier we have a temp-clobbering call.
5949 }
5950 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005951 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005952}
5953
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005954// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5955// move.
5956void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005957 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005958 Location out_loc = locations->Out();
5959 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005960 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005961
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005962 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005963 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005964 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005965 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5966 load->GetStringIndex()));
5967 return; // No dex cache slow path.
5968 }
5969 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005970 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005971 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005972 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005973 __ BindTrackedLabel(&labels->movw_label);
5974 __ movw(out, /* placeholder */ 0u);
5975 __ BindTrackedLabel(&labels->movt_label);
5976 __ movt(out, /* placeholder */ 0u);
5977 __ BindTrackedLabel(&labels->add_pc_label);
5978 __ add(out, out, ShifterOperand(PC));
5979 return; // No dex cache slow path.
5980 }
5981 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005982 uint32_t address = dchecked_integral_cast<uint32_t>(
5983 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5984 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005985 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5986 return; // No dex cache slow path.
5987 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005988 case HLoadString::LoadKind::kBssEntry: {
5989 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005990 Register temp = locations->GetTemp(0).AsRegister<Register>();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005991 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005992 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005993 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005994 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005995 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005996 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005997 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005998 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005999 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006000 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
6001 codegen_->AddSlowPath(slow_path);
6002 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6003 __ Bind(slow_path->GetExitLabel());
6004 return;
6005 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006006 case HLoadString::LoadKind::kJitTableAddress: {
6007 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006008 load->GetStringIndex(),
6009 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006010 // /* GcRoot<mirror::String> */ out = *out
6011 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6012 return;
6013 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006014 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006015 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006016 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006017
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006018 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
6019 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6020 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006021 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006022 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006023 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6024 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006025}
6026
David Brazdilcb1c0552015-08-04 16:22:25 +01006027static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006028 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006029}
6030
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006031void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6032 LocationSummary* locations =
6033 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6034 locations->SetOut(Location::RequiresRegister());
6035}
6036
6037void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006038 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006039 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6040}
6041
6042void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6043 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6044}
6045
6046void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006047 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006048 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006049}
6050
6051void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6052 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006053 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006054 InvokeRuntimeCallingConvention calling_convention;
6055 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6056}
6057
6058void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006059 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006060 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006061}
6062
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006063// Temp is used for read barrier.
6064static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6065 if (kEmitCompilerReadBarrier &&
6066 (kUseBakerReadBarrier ||
6067 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6068 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6069 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6070 return 1;
6071 }
6072 return 0;
6073}
6074
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006075// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006076// interface pointer, one for loading the current interface.
6077// The other checks have one temp for loading the object's class.
6078static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6079 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6080 return 3;
6081 }
6082 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006083}
6084
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006085void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006086 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006087 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006088 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006089 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006090 case TypeCheckKind::kExactCheck:
6091 case TypeCheckKind::kAbstractClassCheck:
6092 case TypeCheckKind::kClassHierarchyCheck:
6093 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006094 call_kind =
6095 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006096 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006097 break;
6098 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006099 case TypeCheckKind::kUnresolvedCheck:
6100 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006101 call_kind = LocationSummary::kCallOnSlowPath;
6102 break;
6103 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006104
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006105 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006106 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006107 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006108 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006109 locations->SetInAt(0, Location::RequiresRegister());
6110 locations->SetInAt(1, Location::RequiresRegister());
6111 // The "out" register is used as a temporary, so it overlaps with the inputs.
6112 // Note that TypeCheckSlowPathARM uses this register too.
6113 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006114 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006115}
6116
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006117void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006118 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006119 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006120 Location obj_loc = locations->InAt(0);
6121 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006122 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006123 Location out_loc = locations->Out();
6124 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006125 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6126 DCHECK_LE(num_temps, 1u);
6127 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006128 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006129 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6130 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6131 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006132 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006133 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006134
6135 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006136 // avoid null check if we know obj is not null.
6137 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006138 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006139 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006140
Roland Levillainc9285912015-12-18 10:38:42 +00006141 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006143 // /* HeapReference<Class> */ out = obj->klass_
6144 GenerateReferenceLoadTwoRegisters(instruction,
6145 out_loc,
6146 obj_loc,
6147 class_offset,
6148 maybe_temp_loc,
6149 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006150 __ cmp(out, ShifterOperand(cls));
6151 // Classes must be equal for the instanceof to succeed.
6152 __ b(&zero, NE);
6153 __ LoadImmediate(out, 1);
6154 __ b(&done);
6155 break;
6156 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006157
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006158 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006159 // /* HeapReference<Class> */ out = obj->klass_
6160 GenerateReferenceLoadTwoRegisters(instruction,
6161 out_loc,
6162 obj_loc,
6163 class_offset,
6164 maybe_temp_loc,
6165 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166 // If the class is abstract, we eagerly fetch the super class of the
6167 // object to avoid doing a comparison we know will fail.
6168 Label loop;
6169 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006170 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006171 GenerateReferenceLoadOneRegister(instruction,
6172 out_loc,
6173 super_offset,
6174 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006175 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006176 // If `out` is null, we use it for the result, and jump to `done`.
6177 __ CompareAndBranchIfZero(out, &done);
6178 __ cmp(out, ShifterOperand(cls));
6179 __ b(&loop, NE);
6180 __ LoadImmediate(out, 1);
6181 if (zero.IsLinked()) {
6182 __ b(&done);
6183 }
6184 break;
6185 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006186
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006187 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006188 // /* HeapReference<Class> */ out = obj->klass_
6189 GenerateReferenceLoadTwoRegisters(instruction,
6190 out_loc,
6191 obj_loc,
6192 class_offset,
6193 maybe_temp_loc,
6194 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006195 // Walk over the class hierarchy to find a match.
6196 Label loop, success;
6197 __ Bind(&loop);
6198 __ cmp(out, ShifterOperand(cls));
6199 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006200 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006201 GenerateReferenceLoadOneRegister(instruction,
6202 out_loc,
6203 super_offset,
6204 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006205 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006206 __ CompareAndBranchIfNonZero(out, &loop);
6207 // If `out` is null, we use it for the result, and jump to `done`.
6208 __ b(&done);
6209 __ Bind(&success);
6210 __ LoadImmediate(out, 1);
6211 if (zero.IsLinked()) {
6212 __ b(&done);
6213 }
6214 break;
6215 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006216
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006217 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006218 // /* HeapReference<Class> */ out = obj->klass_
6219 GenerateReferenceLoadTwoRegisters(instruction,
6220 out_loc,
6221 obj_loc,
6222 class_offset,
6223 maybe_temp_loc,
6224 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006225 // Do an exact check.
6226 Label exact_check;
6227 __ cmp(out, ShifterOperand(cls));
6228 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006229 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006230 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006231 GenerateReferenceLoadOneRegister(instruction,
6232 out_loc,
6233 component_offset,
6234 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006235 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006236 // If `out` is null, we use it for the result, and jump to `done`.
6237 __ CompareAndBranchIfZero(out, &done);
6238 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6239 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6240 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006241 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006242 __ LoadImmediate(out, 1);
6243 __ b(&done);
6244 break;
6245 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006246
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006247 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006248 // No read barrier since the slow path will retry upon failure.
6249 // /* HeapReference<Class> */ out = obj->klass_
6250 GenerateReferenceLoadTwoRegisters(instruction,
6251 out_loc,
6252 obj_loc,
6253 class_offset,
6254 maybe_temp_loc,
6255 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006256 __ cmp(out, ShifterOperand(cls));
6257 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006258 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6259 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006260 codegen_->AddSlowPath(slow_path);
6261 __ b(slow_path->GetEntryLabel(), NE);
6262 __ LoadImmediate(out, 1);
6263 if (zero.IsLinked()) {
6264 __ b(&done);
6265 }
6266 break;
6267 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006268
Calin Juravle98893e12015-10-02 21:05:03 +01006269 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006270 case TypeCheckKind::kInterfaceCheck: {
6271 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006272 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006273 // cases.
6274 //
6275 // We cannot directly call the InstanceofNonTrivial runtime
6276 // entry point without resorting to a type checking slow path
6277 // here (i.e. by calling InvokeRuntime directly), as it would
6278 // require to assign fixed registers for the inputs of this
6279 // HInstanceOf instruction (following the runtime calling
6280 // convention), which might be cluttered by the potential first
6281 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006282 //
6283 // TODO: Introduce a new runtime entry point taking the object
6284 // to test (instead of its class) as argument, and let it deal
6285 // with the read barrier issues. This will let us refactor this
6286 // case of the `switch` code as it was previously (with a direct
6287 // call to the runtime not using a type checking slow path).
6288 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006289 DCHECK(locations->OnlyCallsOnSlowPath());
6290 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6291 /* is_fatal */ false);
6292 codegen_->AddSlowPath(slow_path);
6293 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006294 if (zero.IsLinked()) {
6295 __ b(&done);
6296 }
6297 break;
6298 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006299 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006300
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006301 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006302 __ Bind(&zero);
6303 __ LoadImmediate(out, 0);
6304 }
6305
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006306 if (done.IsLinked()) {
6307 __ Bind(&done);
6308 }
6309
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006310 if (slow_path != nullptr) {
6311 __ Bind(slow_path->GetExitLabel());
6312 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006313}
6314
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006315void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6317 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6318
Roland Levillain3b359c72015-11-17 19:35:12 +00006319 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6320 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006321 case TypeCheckKind::kExactCheck:
6322 case TypeCheckKind::kAbstractClassCheck:
6323 case TypeCheckKind::kClassHierarchyCheck:
6324 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006325 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6326 LocationSummary::kCallOnSlowPath :
6327 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 break;
6329 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006330 case TypeCheckKind::kUnresolvedCheck:
6331 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006332 call_kind = LocationSummary::kCallOnSlowPath;
6333 break;
6334 }
6335
Roland Levillain3b359c72015-11-17 19:35:12 +00006336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6337 locations->SetInAt(0, Location::RequiresRegister());
6338 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006339 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006340}
6341
6342void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006343 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006344 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006345 Location obj_loc = locations->InAt(0);
6346 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006347 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006348 Location temp_loc = locations->GetTemp(0);
6349 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006350 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6351 DCHECK_LE(num_temps, 3u);
6352 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6353 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6354 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6355 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6356 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6357 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6358 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6359 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6360 const uint32_t object_array_data_offset =
6361 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006362
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006363 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6364 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6365 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006366 bool is_type_check_slow_path_fatal = false;
6367 if (!kEmitCompilerReadBarrier) {
6368 is_type_check_slow_path_fatal =
6369 (type_check_kind == TypeCheckKind::kExactCheck ||
6370 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6371 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6372 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6373 !instruction->CanThrowIntoCatchBlock();
6374 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01006375 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006376 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6377 is_type_check_slow_path_fatal);
6378 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006379
6380 Label done;
6381 // Avoid null check if we know obj is not null.
6382 if (instruction->MustDoNullCheck()) {
6383 __ CompareAndBranchIfZero(obj, &done);
6384 }
6385
Roland Levillain3b359c72015-11-17 19:35:12 +00006386 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006387 case TypeCheckKind::kExactCheck:
6388 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006389 // /* HeapReference<Class> */ temp = obj->klass_
6390 GenerateReferenceLoadTwoRegisters(instruction,
6391 temp_loc,
6392 obj_loc,
6393 class_offset,
6394 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006395 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006396
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006397 __ cmp(temp, ShifterOperand(cls));
6398 // Jump to slow path for throwing the exception or doing a
6399 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006400 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006401 break;
6402 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006403
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006404 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006405 // /* HeapReference<Class> */ temp = obj->klass_
6406 GenerateReferenceLoadTwoRegisters(instruction,
6407 temp_loc,
6408 obj_loc,
6409 class_offset,
6410 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006411 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006412
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006413 // If the class is abstract, we eagerly fetch the super class of the
6414 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006415 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006416 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006417 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006418 GenerateReferenceLoadOneRegister(instruction,
6419 temp_loc,
6420 super_offset,
6421 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006422 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006423
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006424 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6425 // exception.
6426 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00006427
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006428 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006429 __ cmp(temp, ShifterOperand(cls));
6430 __ b(&loop, NE);
6431 break;
6432 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006433
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006434 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006435 // /* HeapReference<Class> */ temp = obj->klass_
6436 GenerateReferenceLoadTwoRegisters(instruction,
6437 temp_loc,
6438 obj_loc,
6439 class_offset,
6440 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006441 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006442
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006443 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006444 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006445 __ Bind(&loop);
6446 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006447 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006448
Roland Levillain3b359c72015-11-17 19:35:12 +00006449 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006450 GenerateReferenceLoadOneRegister(instruction,
6451 temp_loc,
6452 super_offset,
6453 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006454 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006455
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006456 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6457 // exception.
6458 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6459 // Otherwise, jump to the beginning of the loop.
6460 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006461 break;
6462 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006463
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006464 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006465 // /* HeapReference<Class> */ temp = obj->klass_
6466 GenerateReferenceLoadTwoRegisters(instruction,
6467 temp_loc,
6468 obj_loc,
6469 class_offset,
6470 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006471 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006472
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006473 // Do an exact check.
6474 __ cmp(temp, ShifterOperand(cls));
6475 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006476
6477 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006478 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006479 GenerateReferenceLoadOneRegister(instruction,
6480 temp_loc,
6481 component_offset,
6482 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006483 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006484 // If the component type is null, jump to the slow path to throw the exception.
6485 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6486 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6487 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006488 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006489 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006490 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006491 break;
6492 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006493
Calin Juravle98893e12015-10-02 21:05:03 +01006494 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006495 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00006496 // We cannot directly call the CheckCast runtime entry point
6497 // without resorting to a type checking slow path here (i.e. by
6498 // calling InvokeRuntime directly), as it would require to
6499 // assign fixed registers for the inputs of this HInstanceOf
6500 // instruction (following the runtime calling convention), which
6501 // might be cluttered by the potential first read barrier
6502 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006503
Roland Levillain3b359c72015-11-17 19:35:12 +00006504 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006505 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006506
6507 case TypeCheckKind::kInterfaceCheck: {
6508 // Avoid read barriers to improve performance of the fast path. We can not get false
6509 // positives by doing this.
6510 // /* HeapReference<Class> */ temp = obj->klass_
6511 GenerateReferenceLoadTwoRegisters(instruction,
6512 temp_loc,
6513 obj_loc,
6514 class_offset,
6515 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006516 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006517
6518 // /* HeapReference<Class> */ temp = temp->iftable_
6519 GenerateReferenceLoadTwoRegisters(instruction,
6520 temp_loc,
6521 temp_loc,
6522 iftable_offset,
6523 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006524 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006525 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006526 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006527 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006528 Label start_loop;
6529 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006530 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
6531 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006532 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
6533 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006534 // Go to next interface.
6535 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
6536 __ sub(maybe_temp2_loc.AsRegister<Register>(),
6537 maybe_temp2_loc.AsRegister<Register>(),
6538 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006539 // Compare the classes and continue the loop if they do not match.
6540 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
6541 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006542 break;
6543 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006544 }
6545 __ Bind(&done);
6546
Roland Levillain3b359c72015-11-17 19:35:12 +00006547 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006548}
6549
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006550void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6551 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006552 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006553 InvokeRuntimeCallingConvention calling_convention;
6554 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6555}
6556
6557void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006558 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6559 instruction,
6560 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006561 if (instruction->IsEnter()) {
6562 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6563 } else {
6564 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6565 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006566}
6567
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006568void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6569void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6570void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006571
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006572void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006573 LocationSummary* locations =
6574 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6575 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6576 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006577 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006578 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006579 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006580 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006581}
6582
6583void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6584 HandleBitwiseOperation(instruction);
6585}
6586
6587void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6588 HandleBitwiseOperation(instruction);
6589}
6590
6591void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6592 HandleBitwiseOperation(instruction);
6593}
6594
Artem Serov7fc63502016-02-09 17:15:29 +00006595
6596void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6597 LocationSummary* locations =
6598 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6599 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6600 || instruction->GetResultType() == Primitive::kPrimLong);
6601
6602 locations->SetInAt(0, Location::RequiresRegister());
6603 locations->SetInAt(1, Location::RequiresRegister());
6604 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6605}
6606
6607void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6608 LocationSummary* locations = instruction->GetLocations();
6609 Location first = locations->InAt(0);
6610 Location second = locations->InAt(1);
6611 Location out = locations->Out();
6612
6613 if (instruction->GetResultType() == Primitive::kPrimInt) {
6614 Register first_reg = first.AsRegister<Register>();
6615 ShifterOperand second_reg(second.AsRegister<Register>());
6616 Register out_reg = out.AsRegister<Register>();
6617
6618 switch (instruction->GetOpKind()) {
6619 case HInstruction::kAnd:
6620 __ bic(out_reg, first_reg, second_reg);
6621 break;
6622 case HInstruction::kOr:
6623 __ orn(out_reg, first_reg, second_reg);
6624 break;
6625 // There is no EON on arm.
6626 case HInstruction::kXor:
6627 default:
6628 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6629 UNREACHABLE();
6630 }
6631 return;
6632
6633 } else {
6634 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6635 Register first_low = first.AsRegisterPairLow<Register>();
6636 Register first_high = first.AsRegisterPairHigh<Register>();
6637 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6638 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6639 Register out_low = out.AsRegisterPairLow<Register>();
6640 Register out_high = out.AsRegisterPairHigh<Register>();
6641
6642 switch (instruction->GetOpKind()) {
6643 case HInstruction::kAnd:
6644 __ bic(out_low, first_low, second_low);
6645 __ bic(out_high, first_high, second_high);
6646 break;
6647 case HInstruction::kOr:
6648 __ orn(out_low, first_low, second_low);
6649 __ orn(out_high, first_high, second_high);
6650 break;
6651 // There is no EON on arm.
6652 case HInstruction::kXor:
6653 default:
6654 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6655 UNREACHABLE();
6656 }
6657 }
6658}
6659
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006660void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6661 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6662 if (value == 0xffffffffu) {
6663 if (out != first) {
6664 __ mov(out, ShifterOperand(first));
6665 }
6666 return;
6667 }
6668 if (value == 0u) {
6669 __ mov(out, ShifterOperand(0));
6670 return;
6671 }
6672 ShifterOperand so;
6673 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6674 __ and_(out, first, so);
6675 } else {
6676 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6677 __ bic(out, first, ShifterOperand(~value));
6678 }
6679}
6680
6681void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6682 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6683 if (value == 0u) {
6684 if (out != first) {
6685 __ mov(out, ShifterOperand(first));
6686 }
6687 return;
6688 }
6689 if (value == 0xffffffffu) {
6690 __ mvn(out, ShifterOperand(0));
6691 return;
6692 }
6693 ShifterOperand so;
6694 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6695 __ orr(out, first, so);
6696 } else {
6697 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6698 __ orn(out, first, ShifterOperand(~value));
6699 }
6700}
6701
6702void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6703 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6704 if (value == 0u) {
6705 if (out != first) {
6706 __ mov(out, ShifterOperand(first));
6707 }
6708 return;
6709 }
6710 __ eor(out, first, ShifterOperand(value));
6711}
6712
Vladimir Marko59751a72016-08-05 14:37:27 +01006713void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6714 Location first,
6715 uint64_t value) {
6716 Register out_low = out.AsRegisterPairLow<Register>();
6717 Register out_high = out.AsRegisterPairHigh<Register>();
6718 Register first_low = first.AsRegisterPairLow<Register>();
6719 Register first_high = first.AsRegisterPairHigh<Register>();
6720 uint32_t value_low = Low32Bits(value);
6721 uint32_t value_high = High32Bits(value);
6722 if (value_low == 0u) {
6723 if (out_low != first_low) {
6724 __ mov(out_low, ShifterOperand(first_low));
6725 }
6726 __ AddConstant(out_high, first_high, value_high);
6727 return;
6728 }
6729 __ AddConstantSetFlags(out_low, first_low, value_low);
6730 ShifterOperand so;
6731 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6732 __ adc(out_high, first_high, so);
6733 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6734 __ sbc(out_high, first_high, so);
6735 } else {
6736 LOG(FATAL) << "Unexpected constant " << value_high;
6737 UNREACHABLE();
6738 }
6739}
6740
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006741void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6742 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006743 Location first = locations->InAt(0);
6744 Location second = locations->InAt(1);
6745 Location out = locations->Out();
6746
6747 if (second.IsConstant()) {
6748 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6749 uint32_t value_low = Low32Bits(value);
6750 if (instruction->GetResultType() == Primitive::kPrimInt) {
6751 Register first_reg = first.AsRegister<Register>();
6752 Register out_reg = out.AsRegister<Register>();
6753 if (instruction->IsAnd()) {
6754 GenerateAndConst(out_reg, first_reg, value_low);
6755 } else if (instruction->IsOr()) {
6756 GenerateOrrConst(out_reg, first_reg, value_low);
6757 } else {
6758 DCHECK(instruction->IsXor());
6759 GenerateEorConst(out_reg, first_reg, value_low);
6760 }
6761 } else {
6762 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6763 uint32_t value_high = High32Bits(value);
6764 Register first_low = first.AsRegisterPairLow<Register>();
6765 Register first_high = first.AsRegisterPairHigh<Register>();
6766 Register out_low = out.AsRegisterPairLow<Register>();
6767 Register out_high = out.AsRegisterPairHigh<Register>();
6768 if (instruction->IsAnd()) {
6769 GenerateAndConst(out_low, first_low, value_low);
6770 GenerateAndConst(out_high, first_high, value_high);
6771 } else if (instruction->IsOr()) {
6772 GenerateOrrConst(out_low, first_low, value_low);
6773 GenerateOrrConst(out_high, first_high, value_high);
6774 } else {
6775 DCHECK(instruction->IsXor());
6776 GenerateEorConst(out_low, first_low, value_low);
6777 GenerateEorConst(out_high, first_high, value_high);
6778 }
6779 }
6780 return;
6781 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006782
6783 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006784 Register first_reg = first.AsRegister<Register>();
6785 ShifterOperand second_reg(second.AsRegister<Register>());
6786 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006787 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006788 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006789 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006790 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006791 } else {
6792 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006793 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006794 }
6795 } else {
6796 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006797 Register first_low = first.AsRegisterPairLow<Register>();
6798 Register first_high = first.AsRegisterPairHigh<Register>();
6799 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6800 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6801 Register out_low = out.AsRegisterPairLow<Register>();
6802 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006803 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006804 __ and_(out_low, first_low, second_low);
6805 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006806 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006807 __ orr(out_low, first_low, second_low);
6808 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006809 } else {
6810 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006811 __ eor(out_low, first_low, second_low);
6812 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006813 }
6814 }
6815}
6816
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006817void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
6818 HInstruction* instruction,
6819 Location out,
6820 uint32_t offset,
6821 Location maybe_temp,
6822 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006823 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006824 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006825 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006826 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006827 if (kUseBakerReadBarrier) {
6828 // Load with fast path based Baker's read barrier.
6829 // /* HeapReference<Object> */ out = *(out + offset)
6830 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006831 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006832 } else {
6833 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006834 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006835 // in the following move operation, as we will need it for the
6836 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006837 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006838 // /* HeapReference<Object> */ out = *(out + offset)
6839 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006840 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006841 }
6842 } else {
6843 // Plain load with no read barrier.
6844 // /* HeapReference<Object> */ out = *(out + offset)
6845 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6846 __ MaybeUnpoisonHeapReference(out_reg);
6847 }
6848}
6849
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006850void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
6851 HInstruction* instruction,
6852 Location out,
6853 Location obj,
6854 uint32_t offset,
6855 Location maybe_temp,
6856 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006857 Register out_reg = out.AsRegister<Register>();
6858 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006859 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006860 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006861 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006862 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006863 // Load with fast path based Baker's read barrier.
6864 // /* HeapReference<Object> */ out = *(obj + offset)
6865 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006866 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006867 } else {
6868 // Load with slow path based read barrier.
6869 // /* HeapReference<Object> */ out = *(obj + offset)
6870 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6871 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6872 }
6873 } else {
6874 // Plain load with no read barrier.
6875 // /* HeapReference<Object> */ out = *(obj + offset)
6876 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6877 __ MaybeUnpoisonHeapReference(out_reg);
6878 }
6879}
6880
6881void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6882 Location root,
6883 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006884 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006885 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006886 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006887 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006888 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006889 if (kUseBakerReadBarrier) {
6890 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6891 // Baker's read barrier are used:
6892 //
6893 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006894 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6895 // if (temp != null) {
6896 // root = temp(root)
Roland Levillainc9285912015-12-18 10:38:42 +00006897 // }
6898
6899 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6900 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6901 static_assert(
6902 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6903 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6904 "have different sizes.");
6905 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6906 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6907 "have different sizes.");
6908
Vladimir Marko953437b2016-08-24 08:30:46 +00006909 // Slow path marking the GC root `root`.
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006910 Location temp = Location::RegisterLocation(LR);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006911 SlowPathCodeARM* slow_path =
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006912 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
6913 instruction,
6914 root,
6915 /*entrypoint*/ temp);
Roland Levillainc9285912015-12-18 10:38:42 +00006916 codegen_->AddSlowPath(slow_path);
6917
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006918 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6919 const int32_t entry_point_offset =
6920 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
6921 // Loading the entrypoint does not require a load acquire since it is only changed when
6922 // threads are suspended or running a checkpoint.
6923 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6924 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6925 // checking GetIsGcMarking.
6926 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00006927 __ Bind(slow_path->GetExitLabel());
6928 } else {
6929 // GC root loaded through a slow path for read barriers other
6930 // than Baker's.
6931 // /* GcRoot<mirror::Object>* */ root = obj + offset
6932 __ AddConstant(root_reg, obj, offset);
6933 // /* mirror::Object* */ root = root->Read()
6934 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6935 }
6936 } else {
6937 // Plain GC root load with no read barrier.
6938 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6939 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6940 // Note that GC roots are not affected by heap poisoning, thus we
6941 // do not have to unpoison `root_reg` here.
6942 }
6943}
6944
6945void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6946 Location ref,
6947 Register obj,
6948 uint32_t offset,
6949 Location temp,
6950 bool needs_null_check) {
6951 DCHECK(kEmitCompilerReadBarrier);
6952 DCHECK(kUseBakerReadBarrier);
6953
6954 // /* HeapReference<Object> */ ref = *(obj + offset)
6955 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006956 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006957 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006958 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006959}
6960
6961void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6962 Location ref,
6963 Register obj,
6964 uint32_t data_offset,
6965 Location index,
6966 Location temp,
6967 bool needs_null_check) {
6968 DCHECK(kEmitCompilerReadBarrier);
6969 DCHECK(kUseBakerReadBarrier);
6970
Roland Levillainbfea3352016-06-23 13:48:47 +01006971 static_assert(
6972 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6973 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006974 // /* HeapReference<Object> */ ref =
6975 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006976 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006977 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006978 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006979}
6980
6981void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6982 Location ref,
6983 Register obj,
6984 uint32_t offset,
6985 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006986 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006987 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006988 bool needs_null_check,
6989 bool always_update_field,
6990 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00006991 DCHECK(kEmitCompilerReadBarrier);
6992 DCHECK(kUseBakerReadBarrier);
6993
6994 // In slow path based read barriers, the read barrier call is
6995 // inserted after the original load. However, in fast path based
6996 // Baker's read barriers, we need to perform the load of
6997 // mirror::Object::monitor_ *before* the original reference load.
6998 // This load-load ordering is required by the read barrier.
6999 // The fast path/slow path (for Baker's algorithm) should look like:
7000 //
7001 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7002 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7003 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007004 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillainc9285912015-12-18 10:38:42 +00007005 // if (is_gray) {
7006 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7007 // }
7008 //
7009 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007010 // slightly more complex as it performs additional checks that we do
7011 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00007012
7013 Register ref_reg = ref.AsRegister<Register>();
7014 Register temp_reg = temp.AsRegister<Register>();
7015 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7016
7017 // /* int32_t */ monitor = obj->monitor_
7018 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7019 if (needs_null_check) {
7020 MaybeRecordImplicitNullCheck(instruction);
7021 }
7022 // /* LockWord */ lock_word = LockWord(monitor)
7023 static_assert(sizeof(LockWord) == sizeof(int32_t),
7024 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007025
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007026 // Introduce a dependency on the lock_word including the rb_state,
7027 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00007028 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01007029 // `obj` is unchanged by this operation, but its value now depends
7030 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007031 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00007032
7033 // The actual reference load.
7034 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007035 // Load types involving an "index": ArrayGet,
7036 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7037 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01007038 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00007039 if (index.IsConstant()) {
7040 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01007041 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00007042 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7043 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01007044 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007045 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7046 // intrinsics, which use a register pair as index ("long
7047 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01007048 Register index_reg = index.IsRegisterPair()
7049 ? index.AsRegisterPairLow<Register>()
7050 : index.AsRegister<Register>();
7051 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00007052 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
7053 }
7054 } else {
7055 // /* HeapReference<Object> */ ref = *(obj + offset)
7056 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7057 }
7058
7059 // Object* ref = ref_addr->AsMirrorPtr()
7060 __ MaybeUnpoisonHeapReference(ref_reg);
7061
Vladimir Marko953437b2016-08-24 08:30:46 +00007062 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007063 SlowPathCodeARM* slow_path;
7064 if (always_update_field) {
7065 DCHECK(temp2 != nullptr);
7066 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
7067 // of the form `obj + field_offset`, where `obj` is a register and
7068 // `field_offset` is a register pair (of which only the lower half
7069 // is used). Thus `offset` and `scale_factor` above are expected
7070 // to be null in this code path.
7071 DCHECK_EQ(offset, 0u);
7072 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7073 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
7074 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7075 } else {
7076 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
7077 }
Roland Levillainc9285912015-12-18 10:38:42 +00007078 AddSlowPath(slow_path);
7079
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007080 // if (rb_state == ReadBarrier::GrayState())
Roland Levillainc9285912015-12-18 10:38:42 +00007081 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007082 // Given the numeric representation, it's enough to check the low bit of the
7083 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7084 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007085 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7086 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007087 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7088 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00007089 __ Bind(slow_path->GetExitLabel());
7090}
7091
7092void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
7093 Location out,
7094 Location ref,
7095 Location obj,
7096 uint32_t offset,
7097 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007098 DCHECK(kEmitCompilerReadBarrier);
7099
Roland Levillainc9285912015-12-18 10:38:42 +00007100 // Insert a slow path based read barrier *after* the reference load.
7101 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007102 // If heap poisoning is enabled, the unpoisoning of the loaded
7103 // reference will be carried out by the runtime within the slow
7104 // path.
7105 //
7106 // Note that `ref` currently does not get unpoisoned (when heap
7107 // poisoning is enabled), which is alright as the `ref` argument is
7108 // not used by the artReadBarrierSlow entry point.
7109 //
7110 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007111 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00007112 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
7113 AddSlowPath(slow_path);
7114
Roland Levillain3b359c72015-11-17 19:35:12 +00007115 __ b(slow_path->GetEntryLabel());
7116 __ Bind(slow_path->GetExitLabel());
7117}
7118
Roland Levillainc9285912015-12-18 10:38:42 +00007119void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7120 Location out,
7121 Location ref,
7122 Location obj,
7123 uint32_t offset,
7124 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007125 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00007126 // Baker's read barriers shall be handled by the fast path
7127 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
7128 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007129 // If heap poisoning is enabled, unpoisoning will be taken care of
7130 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007131 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00007132 } else if (kPoisonHeapReferences) {
7133 __ UnpoisonHeapReference(out.AsRegister<Register>());
7134 }
7135}
7136
Roland Levillainc9285912015-12-18 10:38:42 +00007137void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7138 Location out,
7139 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007140 DCHECK(kEmitCompilerReadBarrier);
7141
Roland Levillainc9285912015-12-18 10:38:42 +00007142 // Insert a slow path based read barrier *after* the GC root load.
7143 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007144 // Note that GC roots are not affected by heap poisoning, so we do
7145 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007146 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007147 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
7148 AddSlowPath(slow_path);
7149
Roland Levillain3b359c72015-11-17 19:35:12 +00007150 __ b(slow_path->GetEntryLabel());
7151 __ Bind(slow_path->GetExitLabel());
7152}
7153
Vladimir Markodc151b22015-10-15 18:02:30 +01007154HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7155 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007156 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00007157 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007158}
7159
Vladimir Markob4536b72015-11-24 13:45:23 +00007160Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7161 Register temp) {
7162 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7163 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7164 if (!invoke->GetLocations()->Intrinsified()) {
7165 return location.AsRegister<Register>();
7166 }
7167 // For intrinsics we allow any location, so it may be on the stack.
7168 if (!location.IsRegister()) {
7169 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7170 return temp;
7171 }
7172 // For register locations, check if the register was saved. If so, get it from the stack.
7173 // Note: There is a chance that the register was saved but not overwritten, so we could
7174 // save one load. However, since this is just an intrinsic slow path we prefer this
7175 // simple and more robust approach rather that trying to determine if that's the case.
7176 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08007177 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00007178 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7179 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7180 return temp;
7181 }
7182 return location.AsRegister<Register>();
7183}
7184
TatWai Chongd8c052a2016-11-02 16:12:48 +08007185Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
7186 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007187 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7188 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007189 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7190 uint32_t offset =
7191 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007192 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007193 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007194 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007195 }
Vladimir Marko58155012015-08-19 12:49:41 +00007196 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007197 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007198 break;
7199 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7200 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7201 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007202 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7203 HArmDexCacheArraysBase* base =
7204 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7205 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7206 temp.AsRegister<Register>());
7207 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7208 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7209 break;
7210 }
Vladimir Marko58155012015-08-19 12:49:41 +00007211 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007212 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007213 Register method_reg;
7214 Register reg = temp.AsRegister<Register>();
7215 if (current_method.IsRegister()) {
7216 method_reg = current_method.AsRegister<Register>();
7217 } else {
7218 DCHECK(invoke->GetLocations()->Intrinsified());
7219 DCHECK(!current_method.IsValid());
7220 method_reg = reg;
7221 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7222 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007223 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7224 __ LoadFromOffset(kLoadWord,
7225 reg,
7226 method_reg,
7227 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007228 // temp = temp[index_in_cache];
7229 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7230 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007231 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7232 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007233 }
Vladimir Marko58155012015-08-19 12:49:41 +00007234 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08007235 return callee_method;
7236}
7237
7238void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
7239 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00007240
7241 switch (invoke->GetCodePtrLocation()) {
7242 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7243 __ bl(GetFrameEntryLabel());
7244 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007245 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7246 // LR = callee_method->entry_point_from_quick_compiled_code_
7247 __ LoadFromOffset(
7248 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007249 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007250 // LR()
7251 __ blx(LR);
7252 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007253 }
7254
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007255 DCHECK(!IsLeafMethod());
7256}
7257
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007258void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7259 Register temp = temp_location.AsRegister<Register>();
7260 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7261 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007262
7263 // Use the calling convention instead of the location of the receiver, as
7264 // intrinsics may have put the receiver in a different register. In the intrinsics
7265 // slow path, the arguments have been moved to the right place, so here we are
7266 // guaranteed that the receiver is the first register of the calling convention.
7267 InvokeDexCallingConvention calling_convention;
7268 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007269 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007270 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007271 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007272 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007273 // Instead of simply (possibly) unpoisoning `temp` here, we should
7274 // emit a read barrier for the previous class reference load.
7275 // However this is not required in practice, as this is an
7276 // intermediate/temporary reference and because the current
7277 // concurrent copying collector keeps the from-space memory
7278 // intact/accessible until the end of the marking phase (the
7279 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007280 __ MaybeUnpoisonHeapReference(temp);
7281 // temp = temp->GetMethodAt(method_offset);
7282 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007283 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007284 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7285 // LR = temp->GetEntryPoint();
7286 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7287 // LR();
7288 __ blx(LR);
7289}
7290
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007291CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007292 const DexFile& dex_file, dex::StringIndex string_index) {
7293 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007294}
7295
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007296CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08007297 const DexFile& dex_file, dex::TypeIndex type_index) {
7298 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007299}
7300
Vladimir Marko1998cd02017-01-13 13:02:58 +00007301CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
7302 const DexFile& dex_file, dex::TypeIndex type_index) {
7303 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
7304}
7305
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007306CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7307 const DexFile& dex_file, uint32_t element_offset) {
7308 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7309}
7310
7311CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7312 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7313 patches->emplace_back(dex_file, offset_or_index);
7314 return &patches->back();
7315}
7316
7317Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007318 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007319 return boot_image_string_patches_.GetOrCreate(
7320 StringReference(&dex_file, string_index),
7321 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7322}
7323
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007324Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007325 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007326 return boot_image_type_patches_.GetOrCreate(
7327 TypeReference(&dex_file, type_index),
7328 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7329}
7330
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007331Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7332 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7333 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7334 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7335}
7336
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007337Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007338 dex::StringIndex string_index,
7339 Handle<mirror::String> handle) {
7340 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7341 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007342 return jit_string_patches_.GetOrCreate(
7343 StringReference(&dex_file, string_index),
7344 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7345}
7346
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007347Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
7348 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007349 Handle<mirror::Class> handle) {
7350 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
7351 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007352 return jit_class_patches_.GetOrCreate(
7353 TypeReference(&dex_file, type_index),
7354 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7355}
7356
Vladimir Markoaad75c62016-10-03 08:46:48 +00007357template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7358inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7359 const ArenaDeque<PcRelativePatchInfo>& infos,
7360 ArenaVector<LinkerPatch>* linker_patches) {
7361 for (const PcRelativePatchInfo& info : infos) {
7362 const DexFile& dex_file = info.target_dex_file;
7363 size_t offset_or_index = info.offset_or_index;
7364 DCHECK(info.add_pc_label.IsBound());
7365 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7366 // Add MOVW patch.
7367 DCHECK(info.movw_label.IsBound());
7368 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7369 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7370 // Add MOVT patch.
7371 DCHECK(info.movt_label.IsBound());
7372 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7373 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7374 }
7375}
7376
Vladimir Marko58155012015-08-19 12:49:41 +00007377void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7378 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007379 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00007380 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007381 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007382 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007383 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007384 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00007385 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007386 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007387 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007388 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7389 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007390 for (const auto& entry : boot_image_string_patches_) {
7391 const StringReference& target_string = entry.first;
7392 Literal* literal = entry.second;
7393 DCHECK(literal->GetLabel()->IsBound());
7394 uint32_t literal_offset = literal->GetLabel()->Position();
7395 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7396 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007397 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007398 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007399 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00007400 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007401 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7402 linker_patches);
7403 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007404 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7405 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007406 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7407 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007408 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007409 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
7410 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007411 for (const auto& entry : boot_image_type_patches_) {
7412 const TypeReference& target_type = entry.first;
7413 Literal* literal = entry.second;
7414 DCHECK(literal->GetLabel()->IsBound());
7415 uint32_t literal_offset = literal->GetLabel()->Position();
7416 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7417 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007418 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007419 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007420 for (const auto& entry : boot_image_address_patches_) {
7421 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7422 Literal* literal = entry.second;
7423 DCHECK(literal->GetLabel()->IsBound());
7424 uint32_t literal_offset = literal->GetLabel()->Position();
7425 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7426 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007427 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007428}
7429
7430Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7431 return map->GetOrCreate(
7432 value,
7433 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007434}
7435
7436Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7437 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007438 return map->GetOrCreate(
7439 target_method,
7440 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007441}
7442
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007443void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7444 LocationSummary* locations =
7445 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7446 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7447 Location::RequiresRegister());
7448 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7449 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7450 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7451}
7452
7453void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7454 LocationSummary* locations = instr->GetLocations();
7455 Register res = locations->Out().AsRegister<Register>();
7456 Register accumulator =
7457 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7458 Register mul_left =
7459 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7460 Register mul_right =
7461 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7462
7463 if (instr->GetOpKind() == HInstruction::kAdd) {
7464 __ mla(res, mul_left, mul_right, accumulator);
7465 } else {
7466 __ mls(res, mul_left, mul_right, accumulator);
7467 }
7468}
7469
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007470void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007471 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007472 LOG(FATAL) << "Unreachable";
7473}
7474
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007475void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007476 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007477 LOG(FATAL) << "Unreachable";
7478}
7479
Mark Mendellfe57faa2015-09-18 09:26:15 -04007480// Simple implementation of packed switch - generate cascaded compare/jumps.
7481void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7482 LocationSummary* locations =
7483 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7484 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007485 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007486 codegen_->GetAssembler()->IsThumb()) {
7487 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7488 if (switch_instr->GetStartValue() != 0) {
7489 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7490 }
7491 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007492}
7493
7494void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7495 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007496 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007497 LocationSummary* locations = switch_instr->GetLocations();
7498 Register value_reg = locations->InAt(0).AsRegister<Register>();
7499 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7500
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007501 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007502 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007503 Register temp_reg = IP;
7504 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7505 // the immediate, because IP is used as the destination register. For the other
7506 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7507 // and they can be encoded in the instruction without making use of IP register.
7508 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7509
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007510 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007511 // Jump to successors[0] if value == lower_bound.
7512 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7513 int32_t last_index = 0;
7514 for (; num_entries - last_index > 2; last_index += 2) {
7515 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7516 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7517 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7518 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7519 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7520 }
7521 if (num_entries - last_index == 2) {
7522 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007523 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007524 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007525 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007526
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007527 // And the default for any other value.
7528 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7529 __ b(codegen_->GetLabelOf(default_block));
7530 }
7531 } else {
7532 // Create a table lookup.
7533 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7534
7535 // Materialize a pointer to the switch table
7536 std::vector<Label*> labels(num_entries);
7537 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7538 for (uint32_t i = 0; i < num_entries; i++) {
7539 labels[i] = codegen_->GetLabelOf(successors[i]);
7540 }
7541 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7542
7543 // Remove the bias.
7544 Register key_reg;
7545 if (lower_bound != 0) {
7546 key_reg = locations->GetTemp(1).AsRegister<Register>();
7547 __ AddConstant(key_reg, value_reg, -lower_bound);
7548 } else {
7549 key_reg = value_reg;
7550 }
7551
7552 // Check whether the value is in the table, jump to default block if not.
7553 __ CmpConstant(key_reg, num_entries - 1);
7554 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7555
7556 // Load the displacement from the table.
7557 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7558
7559 // Dispatch is a direct add to the PC (for Thumb2).
7560 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007561 }
7562}
7563
Vladimir Markob4536b72015-11-24 13:45:23 +00007564void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7565 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7566 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007567}
7568
7569void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7570 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007571 CodeGeneratorARM::PcRelativePatchInfo* labels =
7572 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007573 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007574 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007575 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007576 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007577 __ BindTrackedLabel(&labels->add_pc_label);
7578 __ add(base_reg, base_reg, ShifterOperand(PC));
7579}
7580
Andreas Gampe85b62f22015-09-09 13:15:38 -07007581void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7582 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007583 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007584 return;
7585 }
7586
7587 DCHECK_NE(type, Primitive::kPrimVoid);
7588
7589 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7590 if (return_loc.Equals(trg)) {
7591 return;
7592 }
7593
7594 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7595 // with the last branch.
7596 if (type == Primitive::kPrimLong) {
7597 HParallelMove parallel_move(GetGraph()->GetArena());
7598 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7599 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7600 GetMoveResolver()->EmitNativeCode(&parallel_move);
7601 } else if (type == Primitive::kPrimDouble) {
7602 HParallelMove parallel_move(GetGraph()->GetArena());
7603 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7604 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7605 GetMoveResolver()->EmitNativeCode(&parallel_move);
7606 } else {
7607 // Let the parallel move resolver take care of all of this.
7608 HParallelMove parallel_move(GetGraph()->GetArena());
7609 parallel_move.AddMove(return_loc, trg, type, nullptr);
7610 GetMoveResolver()->EmitNativeCode(&parallel_move);
7611 }
7612}
7613
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007614void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7615 LocationSummary* locations =
7616 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7617 locations->SetInAt(0, Location::RequiresRegister());
7618 locations->SetOut(Location::RequiresRegister());
7619}
7620
7621void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7622 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007623 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007624 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007625 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007626 __ LoadFromOffset(kLoadWord,
7627 locations->Out().AsRegister<Register>(),
7628 locations->InAt(0).AsRegister<Register>(),
7629 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007630 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007631 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007632 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007633 __ LoadFromOffset(kLoadWord,
7634 locations->Out().AsRegister<Register>(),
7635 locations->InAt(0).AsRegister<Register>(),
7636 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7637 __ LoadFromOffset(kLoadWord,
7638 locations->Out().AsRegister<Register>(),
7639 locations->Out().AsRegister<Register>(),
7640 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007641 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007642}
7643
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007644static void PatchJitRootUse(uint8_t* code,
7645 const uint8_t* roots_data,
7646 Literal* literal,
7647 uint64_t index_in_table) {
7648 DCHECK(literal->GetLabel()->IsBound());
7649 uint32_t literal_offset = literal->GetLabel()->Position();
7650 uintptr_t address =
7651 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7652 uint8_t* data = code + literal_offset;
7653 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7654}
7655
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007656void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7657 for (const auto& entry : jit_string_patches_) {
7658 const auto& it = jit_string_roots_.find(entry.first);
7659 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007660 PatchJitRootUse(code, roots_data, entry.second, it->second);
7661 }
7662 for (const auto& entry : jit_class_patches_) {
7663 const auto& it = jit_class_roots_.find(entry.first);
7664 DCHECK(it != jit_class_roots_.end());
7665 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007666 }
7667}
7668
Roland Levillain4d027112015-07-01 15:41:14 +01007669#undef __
7670#undef QUICK_ENTRY_POINT
7671
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007672} // namespace arm
7673} // namespace art