blob: e66408911fe4ec38f44795dec78458e03afca72c [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`).
Roland Levillaind966ce72017-02-09 16:20:14 +0000627//
628// If `entrypoint` is a valid location it is assumed to already be
629// holding the entrypoint. The case where the entrypoint is passed in
630// is for the GcRoot read barrier.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100631class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000632 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800633 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
634 Location ref,
635 Location entrypoint = Location::NoLocation())
636 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillainc9285912015-12-18 10:38:42 +0000637 DCHECK(kEmitCompilerReadBarrier);
638 }
639
640 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
641
642 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
643 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100644 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000645 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100646 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillainc9285912015-12-18 10:38:42 +0000647 DCHECK(instruction_->IsInstanceFieldGet() ||
648 instruction_->IsStaticFieldGet() ||
649 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100650 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000651 instruction_->IsLoadClass() ||
652 instruction_->IsLoadString() ||
653 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100654 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100655 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
656 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000657 << "Unexpected instruction in read barrier marking slow path: "
658 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000659 // The read barrier instrumentation of object ArrayGet
660 // instructions does not support the HIntermediateAddress
661 // instruction.
662 DCHECK(!(instruction_->IsArrayGet() &&
663 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillainc9285912015-12-18 10:38:42 +0000664
665 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100666 // No need to save live registers; it's taken care of by the
667 // entrypoint. Also, there is no need to update the stack mask,
668 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000669 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100670 DCHECK_NE(ref_reg, SP);
671 DCHECK_NE(ref_reg, LR);
672 DCHECK_NE(ref_reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100673 // IP is used internally by the ReadBarrierMarkRegX entry point
674 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100675 DCHECK_NE(ref_reg, IP);
676 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100677 // "Compact" slow path, saving two moves.
678 //
679 // Instead of using the standard runtime calling convention (input
680 // and output in R0):
681 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100682 // R0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100683 // R0 <- ReadBarrierMark(R0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100684 // ref <- R0
Roland Levillain02b75802016-07-13 11:54:35 +0100685 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100686 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100687 // of a dedicated entrypoint:
688 //
689 // rX <- ReadBarrierMarkRegX(rX)
690 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800691 if (entrypoint_.IsValid()) {
692 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
693 __ blx(entrypoint_.AsRegister<Register>());
694 } else {
695 int32_t entry_point_offset =
696 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
697 // This runtime call does not require a stack map.
698 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
699 }
Roland Levillainc9285912015-12-18 10:38:42 +0000700 __ b(GetExitLabel());
701 }
702
703 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100704 // The location (register) of the marked object reference.
705 const Location ref_;
Roland Levillainc9285912015-12-18 10:38:42 +0000706
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800707 // The location of the entrypoint if already loaded.
708 const Location entrypoint_;
709
Roland Levillainc9285912015-12-18 10:38:42 +0000710 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
711};
712
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100713// Slow path marking an object reference `ref` during a read barrier,
714// and if needed, atomically updating the field `obj.field` in the
715// object `obj` holding this reference after marking (contrary to
716// ReadBarrierMarkSlowPathARM above, which never tries to update
717// `obj.field`).
718//
719// This means that after the execution of this slow path, both `ref`
720// and `obj.field` will be up-to-date; i.e., after the flip, both will
721// hold the same to-space reference (unless another thread installed
722// another object reference (different from `ref`) in `obj.field`).
723class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
724 public:
725 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
726 Location ref,
727 Register obj,
728 Location field_offset,
729 Register temp1,
730 Register temp2)
731 : SlowPathCodeARM(instruction),
732 ref_(ref),
733 obj_(obj),
734 field_offset_(field_offset),
735 temp1_(temp1),
736 temp2_(temp2) {
737 DCHECK(kEmitCompilerReadBarrier);
738 }
739
740 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
741
742 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
743 LocationSummary* locations = instruction_->GetLocations();
744 Register ref_reg = ref_.AsRegister<Register>();
745 DCHECK(locations->CanCall());
746 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
747 // This slow path is only used by the UnsafeCASObject intrinsic.
748 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
749 << "Unexpected instruction in read barrier marking and field updating slow path: "
750 << instruction_->DebugName();
751 DCHECK(instruction_->GetLocations()->Intrinsified());
752 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
753 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
754
755 __ Bind(GetEntryLabel());
756
757 // Save the old reference.
758 // Note that we cannot use IP to save the old reference, as IP is
759 // used internally by the ReadBarrierMarkRegX entry point, and we
760 // need the old reference after the call to that entry point.
761 DCHECK_NE(temp1_, IP);
762 __ Mov(temp1_, ref_reg);
763
764 // No need to save live registers; it's taken care of by the
765 // entrypoint. Also, there is no need to update the stack mask,
766 // as this runtime call will not trigger a garbage collection.
767 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
768 DCHECK_NE(ref_reg, SP);
769 DCHECK_NE(ref_reg, LR);
770 DCHECK_NE(ref_reg, PC);
771 // IP is used internally by the ReadBarrierMarkRegX entry point
772 // as a temporary, it cannot be the entry point's input/output.
773 DCHECK_NE(ref_reg, IP);
774 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
775 // "Compact" slow path, saving two moves.
776 //
777 // Instead of using the standard runtime calling convention (input
778 // and output in R0):
779 //
780 // R0 <- ref
781 // R0 <- ReadBarrierMark(R0)
782 // ref <- R0
783 //
784 // we just use rX (the register containing `ref`) as input and output
785 // of a dedicated entrypoint:
786 //
787 // rX <- ReadBarrierMarkRegX(rX)
788 //
789 int32_t entry_point_offset =
790 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
791 // This runtime call does not require a stack map.
792 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
793
794 // If the new reference is different from the old reference,
795 // update the field in the holder (`*(obj_ + field_offset_)`).
796 //
797 // Note that this field could also hold a different object, if
798 // another thread had concurrently changed it. In that case, the
799 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
800 // (CAS) operation below would abort the CAS, leaving the field
801 // as-is.
802 Label done;
803 __ cmp(temp1_, ShifterOperand(ref_reg));
804 __ b(&done, EQ);
805
806 // Update the the holder's field atomically. This may fail if
807 // mutator updates before us, but it's OK. This is achieved
808 // using a strong compare-and-set (CAS) operation with relaxed
809 // memory synchronization ordering, where the expected value is
810 // the old reference and the desired value is the new reference.
811
812 // Convenience aliases.
813 Register base = obj_;
814 // The UnsafeCASObject intrinsic uses a register pair as field
815 // offset ("long offset"), of which only the low part contains
816 // data.
817 Register offset = field_offset_.AsRegisterPairLow<Register>();
818 Register expected = temp1_;
819 Register value = ref_reg;
820 Register tmp_ptr = IP; // Pointer to actual memory.
821 Register tmp = temp2_; // Value in memory.
822
823 __ add(tmp_ptr, base, ShifterOperand(offset));
824
825 if (kPoisonHeapReferences) {
826 __ PoisonHeapReference(expected);
827 if (value == expected) {
828 // Do not poison `value`, as it is the same register as
829 // `expected`, which has just been poisoned.
830 } else {
831 __ PoisonHeapReference(value);
832 }
833 }
834
835 // do {
836 // tmp = [r_ptr] - expected;
837 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
838
Roland Levillain24a4d112016-10-26 13:10:46 +0100839 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100840 __ Bind(&loop_head);
841
842 __ ldrex(tmp, tmp_ptr);
843
844 __ subs(tmp, tmp, ShifterOperand(expected));
845
Roland Levillain24a4d112016-10-26 13:10:46 +0100846 __ it(NE);
847 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100848
Roland Levillain24a4d112016-10-26 13:10:46 +0100849 __ b(&exit_loop, NE);
850
851 __ strex(tmp, value, tmp_ptr);
852 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100853 __ b(&loop_head, EQ);
854
Roland Levillain24a4d112016-10-26 13:10:46 +0100855 __ Bind(&exit_loop);
856
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100857 if (kPoisonHeapReferences) {
858 __ UnpoisonHeapReference(expected);
859 if (value == expected) {
860 // Do not unpoison `value`, as it is the same register as
861 // `expected`, which has just been unpoisoned.
862 } else {
863 __ UnpoisonHeapReference(value);
864 }
865 }
866
867 __ Bind(&done);
868 __ b(GetExitLabel());
869 }
870
871 private:
872 // The location (register) of the marked object reference.
873 const Location ref_;
874 // The register containing the object holding the marked object reference field.
875 const Register obj_;
876 // The location of the offset of the marked reference field within `obj_`.
877 Location field_offset_;
878
879 const Register temp1_;
880 const Register temp2_;
881
882 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
883};
884
Roland Levillain3b359c72015-11-17 19:35:12 +0000885// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100886class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000887 public:
888 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
889 Location out,
890 Location ref,
891 Location obj,
892 uint32_t offset,
893 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100894 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000895 out_(out),
896 ref_(ref),
897 obj_(obj),
898 offset_(offset),
899 index_(index) {
900 DCHECK(kEmitCompilerReadBarrier);
901 // If `obj` is equal to `out` or `ref`, it means the initial object
902 // has been overwritten by (or after) the heap object reference load
903 // to be instrumented, e.g.:
904 //
905 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000906 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000907 //
908 // In that case, we have lost the information about the original
909 // object, and the emitted read barrier cannot work properly.
910 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
911 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
912 }
913
914 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
915 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
916 LocationSummary* locations = instruction_->GetLocations();
917 Register reg_out = out_.AsRegister<Register>();
918 DCHECK(locations->CanCall());
919 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100920 DCHECK(instruction_->IsInstanceFieldGet() ||
921 instruction_->IsStaticFieldGet() ||
922 instruction_->IsArrayGet() ||
923 instruction_->IsInstanceOf() ||
924 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100925 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000926 << "Unexpected instruction in read barrier for heap reference slow path: "
927 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000928 // The read barrier instrumentation of object ArrayGet
929 // instructions does not support the HIntermediateAddress
930 // instruction.
931 DCHECK(!(instruction_->IsArrayGet() &&
932 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +0000933
934 __ Bind(GetEntryLabel());
935 SaveLiveRegisters(codegen, locations);
936
937 // We may have to change the index's value, but as `index_` is a
938 // constant member (like other "inputs" of this slow path),
939 // introduce a copy of it, `index`.
940 Location index = index_;
941 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100942 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000943 if (instruction_->IsArrayGet()) {
944 // Compute the actual memory offset and store it in `index`.
945 Register index_reg = index_.AsRegister<Register>();
946 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
947 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
948 // We are about to change the value of `index_reg` (see the
949 // calls to art::arm::Thumb2Assembler::Lsl and
950 // art::arm::Thumb2Assembler::AddConstant below), but it has
951 // not been saved by the previous call to
952 // art::SlowPathCode::SaveLiveRegisters, as it is a
953 // callee-save register --
954 // art::SlowPathCode::SaveLiveRegisters does not consider
955 // callee-save registers, as it has been designed with the
956 // assumption that callee-save registers are supposed to be
957 // handled by the called function. So, as a callee-save
958 // register, `index_reg` _would_ eventually be saved onto
959 // the stack, but it would be too late: we would have
960 // changed its value earlier. Therefore, we manually save
961 // it here into another freely available register,
962 // `free_reg`, chosen of course among the caller-save
963 // registers (as a callee-save `free_reg` register would
964 // exhibit the same problem).
965 //
966 // Note we could have requested a temporary register from
967 // the register allocator instead; but we prefer not to, as
968 // this is a slow path, and we know we can find a
969 // caller-save register that is available.
970 Register free_reg = FindAvailableCallerSaveRegister(codegen);
971 __ Mov(free_reg, index_reg);
972 index_reg = free_reg;
973 index = Location::RegisterLocation(index_reg);
974 } else {
975 // The initial register stored in `index_` has already been
976 // saved in the call to art::SlowPathCode::SaveLiveRegisters
977 // (as it is not a callee-save register), so we can freely
978 // use it.
979 }
980 // Shifting the index value contained in `index_reg` by the scale
981 // factor (2) cannot overflow in practice, as the runtime is
982 // unable to allocate object arrays with a size larger than
983 // 2^26 - 1 (that is, 2^28 - 4 bytes).
984 __ Lsl(index_reg, index_reg, TIMES_4);
985 static_assert(
986 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
987 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
988 __ AddConstant(index_reg, index_reg, offset_);
989 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100990 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
991 // intrinsics, `index_` is not shifted by a scale factor of 2
992 // (as in the case of ArrayGet), as it is actually an offset
993 // to an object field within an object.
994 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000995 DCHECK(instruction_->GetLocations()->Intrinsified());
996 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
997 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
998 << instruction_->AsInvoke()->GetIntrinsic();
999 DCHECK_EQ(offset_, 0U);
1000 DCHECK(index_.IsRegisterPair());
1001 // UnsafeGet's offset location is a register pair, the low
1002 // part contains the correct offset.
1003 index = index_.ToLow();
1004 }
1005 }
1006
1007 // We're moving two or three locations to locations that could
1008 // overlap, so we need a parallel move resolver.
1009 InvokeRuntimeCallingConvention calling_convention;
1010 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1011 parallel_move.AddMove(ref_,
1012 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1013 Primitive::kPrimNot,
1014 nullptr);
1015 parallel_move.AddMove(obj_,
1016 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1017 Primitive::kPrimNot,
1018 nullptr);
1019 if (index.IsValid()) {
1020 parallel_move.AddMove(index,
1021 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1022 Primitive::kPrimInt,
1023 nullptr);
1024 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1025 } else {
1026 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1027 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1028 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001029 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001030 CheckEntrypointTypes<
1031 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1032 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1033
1034 RestoreLiveRegisters(codegen, locations);
1035 __ b(GetExitLabel());
1036 }
1037
1038 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1039
1040 private:
1041 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1042 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1043 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1044 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1045 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1046 return static_cast<Register>(i);
1047 }
1048 }
1049 // We shall never fail to find a free caller-save register, as
1050 // there are more than two core caller-save registers on ARM
1051 // (meaning it is possible to find one which is different from
1052 // `ref` and `obj`).
1053 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1054 LOG(FATAL) << "Could not find a free caller-save register";
1055 UNREACHABLE();
1056 }
1057
Roland Levillain3b359c72015-11-17 19:35:12 +00001058 const Location out_;
1059 const Location ref_;
1060 const Location obj_;
1061 const uint32_t offset_;
1062 // An additional location containing an index to an array.
1063 // Only used for HArrayGet and the UnsafeGetObject &
1064 // UnsafeGetObjectVolatile intrinsics.
1065 const Location index_;
1066
1067 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1068};
1069
1070// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001071class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001072 public:
1073 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001074 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001075 DCHECK(kEmitCompilerReadBarrier);
1076 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001077
1078 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1079 LocationSummary* locations = instruction_->GetLocations();
1080 Register reg_out = out_.AsRegister<Register>();
1081 DCHECK(locations->CanCall());
1082 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001083 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1084 << "Unexpected instruction in read barrier for GC root slow path: "
1085 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001086
1087 __ Bind(GetEntryLabel());
1088 SaveLiveRegisters(codegen, locations);
1089
1090 InvokeRuntimeCallingConvention calling_convention;
1091 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1092 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001093 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001094 instruction_,
1095 instruction_->GetDexPc(),
1096 this);
1097 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1098 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1099
1100 RestoreLiveRegisters(codegen, locations);
1101 __ b(GetExitLabel());
1102 }
1103
1104 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1105
1106 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001107 const Location out_;
1108 const Location root_;
1109
1110 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1111};
1112
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001113#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001114// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1115#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -07001116
Aart Bike9f37602015-10-09 11:15:55 -07001117inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001118 switch (cond) {
1119 case kCondEQ: return EQ;
1120 case kCondNE: return NE;
1121 case kCondLT: return LT;
1122 case kCondLE: return LE;
1123 case kCondGT: return GT;
1124 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001125 case kCondB: return LO;
1126 case kCondBE: return LS;
1127 case kCondA: return HI;
1128 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001129 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001130 LOG(FATAL) << "Unreachable";
1131 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001132}
1133
Aart Bike9f37602015-10-09 11:15:55 -07001134// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001135inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001136 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001137 case kCondEQ: return EQ;
1138 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001139 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001140 case kCondLT: return LO;
1141 case kCondLE: return LS;
1142 case kCondGT: return HI;
1143 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001144 // Unsigned remain unchanged.
1145 case kCondB: return LO;
1146 case kCondBE: return LS;
1147 case kCondA: return HI;
1148 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001149 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001150 LOG(FATAL) << "Unreachable";
1151 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001152}
1153
Vladimir Markod6e069b2016-01-18 11:11:01 +00001154inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1155 // The ARM condition codes can express all the necessary branches, see the
1156 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1157 // There is no dex instruction or HIR that would need the missing conditions
1158 // "equal or unordered" or "not equal".
1159 switch (cond) {
1160 case kCondEQ: return EQ;
1161 case kCondNE: return NE /* unordered */;
1162 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1163 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1164 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1165 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1166 default:
1167 LOG(FATAL) << "UNREACHABLE";
1168 UNREACHABLE();
1169 }
1170}
1171
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001172void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001173 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001174}
1175
1176void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001177 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001178}
1179
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001180size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1181 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1182 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001183}
1184
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001185size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1186 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1187 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001188}
1189
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001190size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1191 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1192 return kArmWordSize;
1193}
1194
1195size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1196 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1197 return kArmWordSize;
1198}
1199
Calin Juravle34166012014-12-19 17:22:29 +00001200CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001201 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001202 const CompilerOptions& compiler_options,
1203 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001204 : CodeGenerator(graph,
1205 kNumberOfCoreRegisters,
1206 kNumberOfSRegisters,
1207 kNumberOfRegisterPairs,
1208 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1209 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001210 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1211 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001212 compiler_options,
1213 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001214 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001215 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001216 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001217 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001218 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001219 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001220 uint32_literals_(std::less<uint32_t>(),
1221 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001222 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1223 boot_image_string_patches_(StringReferenceValueComparator(),
1224 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1225 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001226 boot_image_type_patches_(TypeReferenceValueComparator(),
1227 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1228 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001229 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001230 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001231 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1232 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001233 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1234 jit_class_patches_(TypeReferenceValueComparator(),
1235 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001236 // Always save the LR register to mimic Quick.
1237 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001238}
1239
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001240void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1241 // Ensure that we fix up branches and literal loads and emit the literal pool.
1242 __ FinalizeCode();
1243
1244 // Adjust native pc offsets in stack maps.
1245 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001246 uint32_t old_position =
1247 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001248 uint32_t new_position = __ GetAdjustedPosition(old_position);
1249 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1250 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001251 // Adjust pc offsets for the disassembly information.
1252 if (disasm_info_ != nullptr) {
1253 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1254 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1255 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1256 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1257 it.second.start = __ GetAdjustedPosition(it.second.start);
1258 it.second.end = __ GetAdjustedPosition(it.second.end);
1259 }
1260 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1261 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1262 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1263 }
1264 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001265
1266 CodeGenerator::Finalize(allocator);
1267}
1268
David Brazdil58282f42016-01-14 12:45:10 +00001269void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001270 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001271 blocked_core_registers_[SP] = true;
1272 blocked_core_registers_[LR] = true;
1273 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001274
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001275 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001276 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001277
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001278 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001279 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001280
David Brazdil58282f42016-01-14 12:45:10 +00001281 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001282 // Stubs do not save callee-save floating point registers. If the graph
1283 // is debuggable, we need to deal with these registers differently. For
1284 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001285 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1286 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1287 }
1288 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001289}
1290
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001291InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001292 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001293 assembler_(codegen->GetAssembler()),
1294 codegen_(codegen) {}
1295
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001296void CodeGeneratorARM::ComputeSpillMask() {
1297 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1298 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001299 // There is no easy instruction to restore just the PC on thumb2. We spill and
1300 // restore another arbitrary register.
1301 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001302 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1303 // We use vpush and vpop for saving and restoring floating point registers, which take
1304 // a SRegister and the number of registers to save/restore after that SRegister. We
1305 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1306 // but in the range.
1307 if (fpu_spill_mask_ != 0) {
1308 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1309 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1310 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1311 fpu_spill_mask_ |= (1 << i);
1312 }
1313 }
1314}
1315
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001316static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001317 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001318}
1319
1320static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001321 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001322}
1323
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001324void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001325 bool skip_overflow_check =
1326 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001327 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001328 __ Bind(&frame_entry_label_);
1329
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001330 if (HasEmptyFrame()) {
1331 return;
1332 }
1333
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001334 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001335 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1336 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1337 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001338 }
1339
Andreas Gampe501fd632015-09-10 16:11:06 -07001340 __ PushList(core_spill_mask_);
1341 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1342 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001343 if (fpu_spill_mask_ != 0) {
1344 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1345 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001346 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001347 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001348 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001349
1350 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1351 // Initialize should_deoptimize flag to 0.
1352 __ mov(IP, ShifterOperand(0));
1353 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
1354 }
1355
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001356 int adjust = GetFrameSize() - FrameEntrySpillSize();
1357 __ AddConstant(SP, -adjust);
1358 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001359
1360 // Save the current method if we need it. Note that we do not
1361 // do this in HCurrentMethod, as the instruction might have been removed
1362 // in the SSA graph.
1363 if (RequiresCurrentMethod()) {
1364 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1365 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001366}
1367
1368void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001369 if (HasEmptyFrame()) {
1370 __ bx(LR);
1371 return;
1372 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001373 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001374 int adjust = GetFrameSize() - FrameEntrySpillSize();
1375 __ AddConstant(SP, adjust);
1376 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001377 if (fpu_spill_mask_ != 0) {
1378 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1379 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001380 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001381 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001382 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001383 // Pop LR into PC to return.
1384 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1385 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1386 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001387 __ cfi().RestoreState();
1388 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001389}
1390
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001391void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001392 Label* label = GetLabelOf(block);
1393 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001394}
1395
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001396Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001397 switch (type) {
1398 case Primitive::kPrimBoolean:
1399 case Primitive::kPrimByte:
1400 case Primitive::kPrimChar:
1401 case Primitive::kPrimShort:
1402 case Primitive::kPrimInt:
1403 case Primitive::kPrimNot: {
1404 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001405 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001406 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001407 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001408 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001409 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001410 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001411 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001412
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001413 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001414 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001415 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001416 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001417 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001418 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001419 if (calling_convention.GetRegisterAt(index) == R1) {
1420 // Skip R1, and use R2_R3 instead.
1421 gp_index_++;
1422 index++;
1423 }
1424 }
1425 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1426 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001427 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001428
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001429 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001430 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001431 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001432 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1433 }
1434 }
1435
1436 case Primitive::kPrimFloat: {
1437 uint32_t stack_index = stack_index_++;
1438 if (float_index_ % 2 == 0) {
1439 float_index_ = std::max(double_index_, float_index_);
1440 }
1441 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1442 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1443 } else {
1444 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1445 }
1446 }
1447
1448 case Primitive::kPrimDouble: {
1449 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1450 uint32_t stack_index = stack_index_;
1451 stack_index_ += 2;
1452 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1453 uint32_t index = double_index_;
1454 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001455 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001456 calling_convention.GetFpuRegisterAt(index),
1457 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001458 DCHECK(ExpectedPairLayout(result));
1459 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001460 } else {
1461 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001462 }
1463 }
1464
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001465 case Primitive::kPrimVoid:
1466 LOG(FATAL) << "Unexpected parameter type " << type;
1467 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001468 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001469 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001470}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001471
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001472Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001473 switch (type) {
1474 case Primitive::kPrimBoolean:
1475 case Primitive::kPrimByte:
1476 case Primitive::kPrimChar:
1477 case Primitive::kPrimShort:
1478 case Primitive::kPrimInt:
1479 case Primitive::kPrimNot: {
1480 return Location::RegisterLocation(R0);
1481 }
1482
1483 case Primitive::kPrimFloat: {
1484 return Location::FpuRegisterLocation(S0);
1485 }
1486
1487 case Primitive::kPrimLong: {
1488 return Location::RegisterPairLocation(R0, R1);
1489 }
1490
1491 case Primitive::kPrimDouble: {
1492 return Location::FpuRegisterPairLocation(S0, S1);
1493 }
1494
1495 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001496 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001497 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001498
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001499 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001500}
1501
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001502Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1503 return Location::RegisterLocation(kMethodRegisterArgument);
1504}
1505
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001506void CodeGeneratorARM::Move32(Location destination, Location source) {
1507 if (source.Equals(destination)) {
1508 return;
1509 }
1510 if (destination.IsRegister()) {
1511 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001512 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001513 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001514 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001515 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001516 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001517 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001518 } else if (destination.IsFpuRegister()) {
1519 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001520 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001521 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001522 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001523 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001524 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001525 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001526 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001527 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001528 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001529 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001530 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001531 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001532 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001533 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001534 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1535 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001536 }
1537 }
1538}
1539
1540void CodeGeneratorARM::Move64(Location destination, Location source) {
1541 if (source.Equals(destination)) {
1542 return;
1543 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001544 if (destination.IsRegisterPair()) {
1545 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001546 EmitParallelMoves(
1547 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1548 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001549 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001550 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001551 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1552 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001553 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001554 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001555 } else if (source.IsFpuRegisterPair()) {
1556 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1557 destination.AsRegisterPairHigh<Register>(),
1558 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001559 } else {
1560 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001561 DCHECK(ExpectedPairLayout(destination));
1562 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1563 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001564 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001565 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001566 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001567 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1568 SP,
1569 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001570 } else if (source.IsRegisterPair()) {
1571 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1572 source.AsRegisterPairLow<Register>(),
1573 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001574 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001575 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001576 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001577 } else {
1578 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001579 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001580 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001581 if (source.AsRegisterPairLow<Register>() == R1) {
1582 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001583 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1584 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001585 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001586 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001587 SP, destination.GetStackIndex());
1588 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001589 } else if (source.IsFpuRegisterPair()) {
1590 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1591 SP,
1592 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001593 } else {
1594 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001595 EmitParallelMoves(
1596 Location::StackSlot(source.GetStackIndex()),
1597 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001598 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001599 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001600 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1601 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001602 }
1603 }
1604}
1605
Calin Juravle175dc732015-08-25 15:42:32 +01001606void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1607 DCHECK(location.IsRegister());
1608 __ LoadImmediate(location.AsRegister<Register>(), value);
1609}
1610
Calin Juravlee460d1d2015-09-29 04:52:17 +01001611void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001612 HParallelMove move(GetGraph()->GetArena());
1613 move.AddMove(src, dst, dst_type, nullptr);
1614 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001615}
1616
1617void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1618 if (location.IsRegister()) {
1619 locations->AddTemp(location);
1620 } else if (location.IsRegisterPair()) {
1621 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1622 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1623 } else {
1624 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1625 }
1626}
1627
Calin Juravle175dc732015-08-25 15:42:32 +01001628void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1629 HInstruction* instruction,
1630 uint32_t dex_pc,
1631 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001632 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001633 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001634 if (EntrypointRequiresStackMap(entrypoint)) {
1635 RecordPcInfo(instruction, dex_pc, slow_path);
1636 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001637}
1638
Roland Levillaindec8f632016-07-22 17:10:06 +01001639void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1640 HInstruction* instruction,
1641 SlowPathCode* slow_path) {
1642 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001643 GenerateInvokeRuntime(entry_point_offset);
1644}
1645
1646void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001647 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1648 __ blx(LR);
1649}
1650
David Brazdilfc6a86a2015-06-26 10:33:45 +00001651void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001652 DCHECK(!successor->IsExitBlock());
1653
1654 HBasicBlock* block = got->GetBlock();
1655 HInstruction* previous = got->GetPrevious();
1656
1657 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001658 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001659 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1660 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1661 return;
1662 }
1663
1664 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1665 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1666 }
1667 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001668 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001669 }
1670}
1671
David Brazdilfc6a86a2015-06-26 10:33:45 +00001672void LocationsBuilderARM::VisitGoto(HGoto* got) {
1673 got->SetLocations(nullptr);
1674}
1675
1676void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1677 HandleGoto(got, got->GetSuccessor());
1678}
1679
1680void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1681 try_boundary->SetLocations(nullptr);
1682}
1683
1684void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1685 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1686 if (!successor->IsExitBlock()) {
1687 HandleGoto(try_boundary, successor);
1688 }
1689}
1690
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001691void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001692 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001693}
1694
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001695void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001696}
1697
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001698void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1699 Primitive::Type type = instruction->InputAt(0)->GetType();
1700 Location lhs_loc = instruction->GetLocations()->InAt(0);
1701 Location rhs_loc = instruction->GetLocations()->InAt(1);
1702 if (rhs_loc.IsConstant()) {
1703 // 0.0 is the only immediate that can be encoded directly in
1704 // a VCMP instruction.
1705 //
1706 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1707 // specify that in a floating-point comparison, positive zero
1708 // and negative zero are considered equal, so we can use the
1709 // literal 0.0 for both cases here.
1710 //
1711 // Note however that some methods (Float.equal, Float.compare,
1712 // Float.compareTo, Double.equal, Double.compare,
1713 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1714 // StrictMath.min) consider 0.0 to be (strictly) greater than
1715 // -0.0. So if we ever translate calls to these methods into a
1716 // HCompare instruction, we must handle the -0.0 case with
1717 // care here.
1718 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1719 if (type == Primitive::kPrimFloat) {
1720 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1721 } else {
1722 DCHECK_EQ(type, Primitive::kPrimDouble);
1723 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1724 }
1725 } else {
1726 if (type == Primitive::kPrimFloat) {
1727 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1728 } else {
1729 DCHECK_EQ(type, Primitive::kPrimDouble);
1730 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1731 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1732 }
1733 }
1734}
1735
Roland Levillain4fa13f62015-07-06 18:11:54 +01001736void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1737 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001738 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001739 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001740 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001741}
1742
1743void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1744 Label* true_label,
1745 Label* false_label) {
1746 LocationSummary* locations = cond->GetLocations();
1747 Location left = locations->InAt(0);
1748 Location right = locations->InAt(1);
1749 IfCondition if_cond = cond->GetCondition();
1750
1751 Register left_high = left.AsRegisterPairHigh<Register>();
1752 Register left_low = left.AsRegisterPairLow<Register>();
1753 IfCondition true_high_cond = if_cond;
1754 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001755 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001756
1757 // Set the conditions for the test, remembering that == needs to be
1758 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001759 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001760 switch (if_cond) {
1761 case kCondEQ:
1762 case kCondNE:
1763 // Nothing to do.
1764 break;
1765 case kCondLT:
1766 false_high_cond = kCondGT;
1767 break;
1768 case kCondLE:
1769 true_high_cond = kCondLT;
1770 break;
1771 case kCondGT:
1772 false_high_cond = kCondLT;
1773 break;
1774 case kCondGE:
1775 true_high_cond = kCondGT;
1776 break;
Aart Bike9f37602015-10-09 11:15:55 -07001777 case kCondB:
1778 false_high_cond = kCondA;
1779 break;
1780 case kCondBE:
1781 true_high_cond = kCondB;
1782 break;
1783 case kCondA:
1784 false_high_cond = kCondB;
1785 break;
1786 case kCondAE:
1787 true_high_cond = kCondA;
1788 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001789 }
1790 if (right.IsConstant()) {
1791 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1792 int32_t val_low = Low32Bits(value);
1793 int32_t val_high = High32Bits(value);
1794
Vladimir Markoac6ac102015-12-17 12:14:00 +00001795 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001796 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001797 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001798 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001799 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001800 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001801 __ b(true_label, ARMCondition(true_high_cond));
1802 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001803 }
1804 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001805 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001806 } else {
1807 Register right_high = right.AsRegisterPairHigh<Register>();
1808 Register right_low = right.AsRegisterPairLow<Register>();
1809
1810 __ cmp(left_high, ShifterOperand(right_high));
1811 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001812 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001813 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001814 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001815 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001816 __ b(true_label, ARMCondition(true_high_cond));
1817 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001818 }
1819 // Must be equal high, so compare the lows.
1820 __ cmp(left_low, ShifterOperand(right_low));
1821 }
1822 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001823 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001824 __ b(true_label, final_condition);
1825}
1826
David Brazdil0debae72015-11-12 18:37:00 +00001827void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1828 Label* true_target_in,
1829 Label* false_target_in) {
1830 // Generated branching requires both targets to be explicit. If either of the
1831 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1832 Label fallthrough_target;
1833 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1834 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1835
Roland Levillain4fa13f62015-07-06 18:11:54 +01001836 Primitive::Type type = condition->InputAt(0)->GetType();
1837 switch (type) {
1838 case Primitive::kPrimLong:
1839 GenerateLongComparesAndJumps(condition, true_target, false_target);
1840 break;
1841 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001842 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001843 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001844 GenerateFPJumps(condition, true_target, false_target);
1845 break;
1846 default:
1847 LOG(FATAL) << "Unexpected compare type " << type;
1848 }
1849
David Brazdil0debae72015-11-12 18:37:00 +00001850 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001851 __ b(false_target);
1852 }
David Brazdil0debae72015-11-12 18:37:00 +00001853
1854 if (fallthrough_target.IsLinked()) {
1855 __ Bind(&fallthrough_target);
1856 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001857}
1858
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001859void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001860 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001861 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001862 Label* false_target) {
1863 HInstruction* cond = instruction->InputAt(condition_input_index);
1864
1865 if (true_target == nullptr && false_target == nullptr) {
1866 // Nothing to do. The code always falls through.
1867 return;
1868 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001869 // Constant condition, statically compared against "true" (integer value 1).
1870 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001871 if (true_target != nullptr) {
1872 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001873 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001874 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001875 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001876 if (false_target != nullptr) {
1877 __ b(false_target);
1878 }
1879 }
1880 return;
1881 }
1882
1883 // The following code generates these patterns:
1884 // (1) true_target == nullptr && false_target != nullptr
1885 // - opposite condition true => branch to false_target
1886 // (2) true_target != nullptr && false_target == nullptr
1887 // - condition true => branch to true_target
1888 // (3) true_target != nullptr && false_target != nullptr
1889 // - condition true => branch to true_target
1890 // - branch to false_target
1891 if (IsBooleanValueOrMaterializedCondition(cond)) {
1892 // Condition has been materialized, compare the output to 0.
1893 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1894 DCHECK(cond_val.IsRegister());
1895 if (true_target == nullptr) {
1896 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1897 } else {
1898 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001899 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001900 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001901 // Condition has not been materialized. Use its inputs as the comparison and
1902 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001903 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001904
1905 // If this is a long or FP comparison that has been folded into
1906 // the HCondition, generate the comparison directly.
1907 Primitive::Type type = condition->InputAt(0)->GetType();
1908 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1909 GenerateCompareTestAndBranch(condition, true_target, false_target);
1910 return;
1911 }
1912
1913 LocationSummary* locations = cond->GetLocations();
1914 DCHECK(locations->InAt(0).IsRegister());
1915 Register left = locations->InAt(0).AsRegister<Register>();
1916 Location right = locations->InAt(1);
1917 if (right.IsRegister()) {
1918 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001919 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001920 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001921 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001922 }
1923 if (true_target == nullptr) {
1924 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1925 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001926 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001927 }
Dave Allison20dfc792014-06-16 20:44:29 -07001928 }
David Brazdil0debae72015-11-12 18:37:00 +00001929
1930 // If neither branch falls through (case 3), the conditional branch to `true_target`
1931 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1932 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001933 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001934 }
1935}
1936
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001937void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001938 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1939 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001940 locations->SetInAt(0, Location::RequiresRegister());
1941 }
1942}
1943
1944void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001945 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1946 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1947 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1948 nullptr : codegen_->GetLabelOf(true_successor);
1949 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1950 nullptr : codegen_->GetLabelOf(false_successor);
1951 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001952}
1953
1954void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1955 LocationSummary* locations = new (GetGraph()->GetArena())
1956 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001957 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001958 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001959 locations->SetInAt(0, Location::RequiresRegister());
1960 }
1961}
1962
1963void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001964 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001965 GenerateTestAndBranch(deoptimize,
1966 /* condition_input_index */ 0,
1967 slow_path->GetEntryLabel(),
1968 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001969}
Dave Allison20dfc792014-06-16 20:44:29 -07001970
Mingyao Yang063fc772016-08-02 11:02:54 -07001971void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1972 LocationSummary* locations = new (GetGraph()->GetArena())
1973 LocationSummary(flag, LocationSummary::kNoCall);
1974 locations->SetOut(Location::RequiresRegister());
1975}
1976
1977void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1978 __ LoadFromOffset(kLoadWord,
1979 flag->GetLocations()->Out().AsRegister<Register>(),
1980 SP,
1981 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
1982}
1983
David Brazdil74eb1b22015-12-14 11:44:01 +00001984void LocationsBuilderARM::VisitSelect(HSelect* select) {
1985 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1986 if (Primitive::IsFloatingPointType(select->GetType())) {
1987 locations->SetInAt(0, Location::RequiresFpuRegister());
1988 locations->SetInAt(1, Location::RequiresFpuRegister());
1989 } else {
1990 locations->SetInAt(0, Location::RequiresRegister());
1991 locations->SetInAt(1, Location::RequiresRegister());
1992 }
1993 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1994 locations->SetInAt(2, Location::RequiresRegister());
1995 }
1996 locations->SetOut(Location::SameAsFirstInput());
1997}
1998
1999void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
2000 LocationSummary* locations = select->GetLocations();
2001 Label false_target;
2002 GenerateTestAndBranch(select,
2003 /* condition_input_index */ 2,
2004 /* true_target */ nullptr,
2005 &false_target);
2006 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2007 __ Bind(&false_target);
2008}
2009
David Srbecky0cf44932015-12-09 14:09:59 +00002010void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2011 new (GetGraph()->GetArena()) LocationSummary(info);
2012}
2013
David Srbeckyd28f4a02016-03-14 17:14:24 +00002014void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2015 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002016}
2017
2018void CodeGeneratorARM::GenerateNop() {
2019 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002020}
2021
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002022void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002023 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002024 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002025 // Handle the long/FP comparisons made in instruction simplification.
2026 switch (cond->InputAt(0)->GetType()) {
2027 case Primitive::kPrimLong:
2028 locations->SetInAt(0, Location::RequiresRegister());
2029 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002030 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002031 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2032 }
2033 break;
2034
2035 case Primitive::kPrimFloat:
2036 case Primitive::kPrimDouble:
2037 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002038 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002039 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002040 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2041 }
2042 break;
2043
2044 default:
2045 locations->SetInAt(0, Location::RequiresRegister());
2046 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002047 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002048 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2049 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002050 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002051}
2052
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002053void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002054 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002055 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002056 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002057
2058 LocationSummary* locations = cond->GetLocations();
2059 Location left = locations->InAt(0);
2060 Location right = locations->InAt(1);
2061 Register out = locations->Out().AsRegister<Register>();
2062 Label true_label, false_label;
2063
2064 switch (cond->InputAt(0)->GetType()) {
2065 default: {
2066 // Integer case.
2067 if (right.IsRegister()) {
2068 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2069 } else {
2070 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002071 __ CmpConstant(left.AsRegister<Register>(),
2072 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002073 }
Aart Bike9f37602015-10-09 11:15:55 -07002074 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002075 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002076 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002077 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002078 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002079 return;
2080 }
2081 case Primitive::kPrimLong:
2082 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2083 break;
2084 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002085 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002086 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002087 GenerateFPJumps(cond, &true_label, &false_label);
2088 break;
2089 }
2090
2091 // Convert the jumps into the result.
2092 Label done_label;
2093
2094 // False case: result = 0.
2095 __ Bind(&false_label);
2096 __ LoadImmediate(out, 0);
2097 __ b(&done_label);
2098
2099 // True case: result = 1.
2100 __ Bind(&true_label);
2101 __ LoadImmediate(out, 1);
2102 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002103}
2104
2105void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002106 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002107}
2108
2109void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002110 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002111}
2112
2113void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002114 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002115}
2116
2117void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002118 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002119}
2120
2121void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002122 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002123}
2124
2125void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002126 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002127}
2128
2129void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002130 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002131}
2132
2133void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002134 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002135}
2136
2137void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002138 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002139}
2140
2141void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002142 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002143}
2144
2145void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002146 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002147}
2148
2149void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002150 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002151}
2152
Aart Bike9f37602015-10-09 11:15:55 -07002153void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002154 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002155}
2156
2157void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002158 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002159}
2160
2161void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002162 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002163}
2164
2165void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002166 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002167}
2168
2169void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002170 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002171}
2172
2173void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002174 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002175}
2176
2177void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002178 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002179}
2180
2181void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002182 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002183}
2184
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002185void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002186 LocationSummary* locations =
2187 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002188 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002189}
2190
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002191void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002192 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002193}
2194
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002195void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2196 LocationSummary* locations =
2197 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2198 locations->SetOut(Location::ConstantLocation(constant));
2199}
2200
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002201void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002202 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002203}
2204
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002205void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002206 LocationSummary* locations =
2207 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002208 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002209}
2210
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002211void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002212 // Will be generated at use site.
2213}
2214
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002215void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2216 LocationSummary* locations =
2217 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2218 locations->SetOut(Location::ConstantLocation(constant));
2219}
2220
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002221void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002222 // Will be generated at use site.
2223}
2224
2225void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2226 LocationSummary* locations =
2227 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2228 locations->SetOut(Location::ConstantLocation(constant));
2229}
2230
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002231void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002232 // Will be generated at use site.
2233}
2234
Calin Juravle27df7582015-04-17 19:12:31 +01002235void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2236 memory_barrier->SetLocations(nullptr);
2237}
2238
2239void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002240 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002241}
2242
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002243void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002244 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002245}
2246
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002247void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002248 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002249}
2250
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002251void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002252 LocationSummary* locations =
2253 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002254 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002255}
2256
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002257void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002258 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002259}
2260
Calin Juravle175dc732015-08-25 15:42:32 +01002261void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2262 // The trampoline uses the same calling convention as dex calling conventions,
2263 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2264 // the method_idx.
2265 HandleInvoke(invoke);
2266}
2267
2268void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2269 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2270}
2271
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002272void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002273 // Explicit clinit checks triggered by static invokes must have been pruned by
2274 // art::PrepareForRegisterAllocation.
2275 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002276
Vladimir Marko68c981f2016-08-26 13:13:33 +01002277 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002278 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002279 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2280 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2281 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002282 return;
2283 }
2284
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002285 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002286
2287 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2288 if (invoke->HasPcRelativeDexCache()) {
2289 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2290 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002291}
2292
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002293static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2294 if (invoke->GetLocations()->Intrinsified()) {
2295 IntrinsicCodeGeneratorARM intrinsic(codegen);
2296 intrinsic.Dispatch(invoke);
2297 return true;
2298 }
2299 return false;
2300}
2301
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002302void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002303 // Explicit clinit checks triggered by static invokes must have been pruned by
2304 // art::PrepareForRegisterAllocation.
2305 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002306
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002307 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2308 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002309 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002310
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002311 LocationSummary* locations = invoke->GetLocations();
2312 codegen_->GenerateStaticOrDirectCall(
2313 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002314 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002315}
2316
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002317void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002318 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002319 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002320}
2321
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002322void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002323 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002324 if (intrinsic.TryDispatch(invoke)) {
2325 return;
2326 }
2327
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002328 HandleInvoke(invoke);
2329}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002330
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002331void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002332 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2333 return;
2334 }
2335
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002336 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002337 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002338 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002339}
2340
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002341void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2342 HandleInvoke(invoke);
2343 // Add the hidden argument.
2344 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2345}
2346
2347void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2348 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002349 LocationSummary* locations = invoke->GetLocations();
2350 Register temp = locations->GetTemp(0).AsRegister<Register>();
2351 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002352 Location receiver = locations->InAt(0);
2353 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2354
Roland Levillain3b359c72015-11-17 19:35:12 +00002355 // Set the hidden argument. This is safe to do this here, as R12
2356 // won't be modified thereafter, before the `blx` (call) instruction.
2357 DCHECK_EQ(R12, hidden_reg);
2358 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002359
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002360 if (receiver.IsStackSlot()) {
2361 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002362 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002363 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2364 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002365 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002366 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002367 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002368 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002369 // Instead of simply (possibly) unpoisoning `temp` here, we should
2370 // emit a read barrier for the previous class reference load.
2371 // However this is not required in practice, as this is an
2372 // intermediate/temporary reference and because the current
2373 // concurrent copying collector keeps the from-space memory
2374 // intact/accessible until the end of the marking phase (the
2375 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002376 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002377 __ LoadFromOffset(kLoadWord, temp, temp,
2378 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2379 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002380 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002381 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002382 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002383 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002384 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002385 // LR = temp->GetEntryPoint();
2386 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2387 // LR();
2388 __ blx(LR);
2389 DCHECK(!codegen_->IsLeafMethod());
2390 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2391}
2392
Orion Hodsonac141392017-01-13 11:53:47 +00002393void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2394 HandleInvoke(invoke);
2395}
2396
2397void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2398 codegen_->GenerateInvokePolymorphicCall(invoke);
2399}
2400
Roland Levillain88cb1752014-10-20 16:36:47 +01002401void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2402 LocationSummary* locations =
2403 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2404 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002405 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002406 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002407 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2408 break;
2409 }
2410 case Primitive::kPrimLong: {
2411 locations->SetInAt(0, Location::RequiresRegister());
2412 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002413 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002414 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002415
Roland Levillain88cb1752014-10-20 16:36:47 +01002416 case Primitive::kPrimFloat:
2417 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002418 locations->SetInAt(0, Location::RequiresFpuRegister());
2419 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002420 break;
2421
2422 default:
2423 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2424 }
2425}
2426
2427void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2428 LocationSummary* locations = neg->GetLocations();
2429 Location out = locations->Out();
2430 Location in = locations->InAt(0);
2431 switch (neg->GetResultType()) {
2432 case Primitive::kPrimInt:
2433 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002434 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002435 break;
2436
2437 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002438 DCHECK(in.IsRegisterPair());
2439 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2440 __ rsbs(out.AsRegisterPairLow<Register>(),
2441 in.AsRegisterPairLow<Register>(),
2442 ShifterOperand(0));
2443 // We cannot emit an RSC (Reverse Subtract with Carry)
2444 // instruction here, as it does not exist in the Thumb-2
2445 // instruction set. We use the following approach
2446 // using SBC and SUB instead.
2447 //
2448 // out.hi = -C
2449 __ sbc(out.AsRegisterPairHigh<Register>(),
2450 out.AsRegisterPairHigh<Register>(),
2451 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2452 // out.hi = out.hi - in.hi
2453 __ sub(out.AsRegisterPairHigh<Register>(),
2454 out.AsRegisterPairHigh<Register>(),
2455 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2456 break;
2457
Roland Levillain88cb1752014-10-20 16:36:47 +01002458 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002459 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002460 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002461 break;
2462
Roland Levillain88cb1752014-10-20 16:36:47 +01002463 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002464 DCHECK(in.IsFpuRegisterPair());
2465 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2466 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002467 break;
2468
2469 default:
2470 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2471 }
2472}
2473
Roland Levillaindff1f282014-11-05 14:15:05 +00002474void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002475 Primitive::Type result_type = conversion->GetResultType();
2476 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002477 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002478
Roland Levillain5b3ee562015-04-14 16:02:41 +01002479 // The float-to-long, double-to-long and long-to-float type conversions
2480 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002481 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002482 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2483 && result_type == Primitive::kPrimLong)
2484 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002485 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002486 : LocationSummary::kNoCall;
2487 LocationSummary* locations =
2488 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2489
David Brazdilb2bd1c52015-03-25 11:17:37 +00002490 // The Java language does not allow treating boolean as an integral type but
2491 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002492
Roland Levillaindff1f282014-11-05 14:15:05 +00002493 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002494 case Primitive::kPrimByte:
2495 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002496 case Primitive::kPrimLong:
2497 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002498 case Primitive::kPrimBoolean:
2499 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002500 case Primitive::kPrimShort:
2501 case Primitive::kPrimInt:
2502 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002503 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002504 locations->SetInAt(0, Location::RequiresRegister());
2505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2506 break;
2507
2508 default:
2509 LOG(FATAL) << "Unexpected type conversion from " << input_type
2510 << " to " << result_type;
2511 }
2512 break;
2513
Roland Levillain01a8d712014-11-14 16:27:39 +00002514 case Primitive::kPrimShort:
2515 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002516 case Primitive::kPrimLong:
2517 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002518 case Primitive::kPrimBoolean:
2519 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002520 case Primitive::kPrimByte:
2521 case Primitive::kPrimInt:
2522 case Primitive::kPrimChar:
2523 // Processing a Dex `int-to-short' instruction.
2524 locations->SetInAt(0, Location::RequiresRegister());
2525 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2526 break;
2527
2528 default:
2529 LOG(FATAL) << "Unexpected type conversion from " << input_type
2530 << " to " << result_type;
2531 }
2532 break;
2533
Roland Levillain946e1432014-11-11 17:35:19 +00002534 case Primitive::kPrimInt:
2535 switch (input_type) {
2536 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002537 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002538 locations->SetInAt(0, Location::Any());
2539 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2540 break;
2541
2542 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002543 // Processing a Dex `float-to-int' instruction.
2544 locations->SetInAt(0, Location::RequiresFpuRegister());
2545 locations->SetOut(Location::RequiresRegister());
2546 locations->AddTemp(Location::RequiresFpuRegister());
2547 break;
2548
Roland Levillain946e1432014-11-11 17:35:19 +00002549 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002550 // Processing a Dex `double-to-int' instruction.
2551 locations->SetInAt(0, Location::RequiresFpuRegister());
2552 locations->SetOut(Location::RequiresRegister());
2553 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002554 break;
2555
2556 default:
2557 LOG(FATAL) << "Unexpected type conversion from " << input_type
2558 << " to " << result_type;
2559 }
2560 break;
2561
Roland Levillaindff1f282014-11-05 14:15:05 +00002562 case Primitive::kPrimLong:
2563 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002564 case Primitive::kPrimBoolean:
2565 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002566 case Primitive::kPrimByte:
2567 case Primitive::kPrimShort:
2568 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002569 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002570 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002571 locations->SetInAt(0, Location::RequiresRegister());
2572 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2573 break;
2574
Roland Levillain624279f2014-12-04 11:54:28 +00002575 case Primitive::kPrimFloat: {
2576 // Processing a Dex `float-to-long' instruction.
2577 InvokeRuntimeCallingConvention calling_convention;
2578 locations->SetInAt(0, Location::FpuRegisterLocation(
2579 calling_convention.GetFpuRegisterAt(0)));
2580 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2581 break;
2582 }
2583
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002584 case Primitive::kPrimDouble: {
2585 // Processing a Dex `double-to-long' instruction.
2586 InvokeRuntimeCallingConvention calling_convention;
2587 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2588 calling_convention.GetFpuRegisterAt(0),
2589 calling_convention.GetFpuRegisterAt(1)));
2590 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002591 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002592 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002593
2594 default:
2595 LOG(FATAL) << "Unexpected type conversion from " << input_type
2596 << " to " << result_type;
2597 }
2598 break;
2599
Roland Levillain981e4542014-11-14 11:47:14 +00002600 case Primitive::kPrimChar:
2601 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002602 case Primitive::kPrimLong:
2603 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002604 case Primitive::kPrimBoolean:
2605 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002606 case Primitive::kPrimByte:
2607 case Primitive::kPrimShort:
2608 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002609 // Processing a Dex `int-to-char' instruction.
2610 locations->SetInAt(0, Location::RequiresRegister());
2611 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2612 break;
2613
2614 default:
2615 LOG(FATAL) << "Unexpected type conversion from " << input_type
2616 << " to " << result_type;
2617 }
2618 break;
2619
Roland Levillaindff1f282014-11-05 14:15:05 +00002620 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002621 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002622 case Primitive::kPrimBoolean:
2623 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002624 case Primitive::kPrimByte:
2625 case Primitive::kPrimShort:
2626 case Primitive::kPrimInt:
2627 case Primitive::kPrimChar:
2628 // Processing a Dex `int-to-float' instruction.
2629 locations->SetInAt(0, Location::RequiresRegister());
2630 locations->SetOut(Location::RequiresFpuRegister());
2631 break;
2632
Roland Levillain5b3ee562015-04-14 16:02:41 +01002633 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002634 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002635 InvokeRuntimeCallingConvention calling_convention;
2636 locations->SetInAt(0, Location::RegisterPairLocation(
2637 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2638 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002639 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002640 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002641
Roland Levillaincff13742014-11-17 14:32:17 +00002642 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002643 // Processing a Dex `double-to-float' instruction.
2644 locations->SetInAt(0, Location::RequiresFpuRegister());
2645 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002646 break;
2647
2648 default:
2649 LOG(FATAL) << "Unexpected type conversion from " << input_type
2650 << " to " << result_type;
2651 };
2652 break;
2653
Roland Levillaindff1f282014-11-05 14:15:05 +00002654 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002655 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002656 case Primitive::kPrimBoolean:
2657 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002658 case Primitive::kPrimByte:
2659 case Primitive::kPrimShort:
2660 case Primitive::kPrimInt:
2661 case Primitive::kPrimChar:
2662 // Processing a Dex `int-to-double' instruction.
2663 locations->SetInAt(0, Location::RequiresRegister());
2664 locations->SetOut(Location::RequiresFpuRegister());
2665 break;
2666
2667 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002668 // Processing a Dex `long-to-double' instruction.
2669 locations->SetInAt(0, Location::RequiresRegister());
2670 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002671 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002672 locations->AddTemp(Location::RequiresFpuRegister());
2673 break;
2674
Roland Levillaincff13742014-11-17 14:32:17 +00002675 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002676 // Processing a Dex `float-to-double' instruction.
2677 locations->SetInAt(0, Location::RequiresFpuRegister());
2678 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002679 break;
2680
2681 default:
2682 LOG(FATAL) << "Unexpected type conversion from " << input_type
2683 << " to " << result_type;
2684 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002685 break;
2686
2687 default:
2688 LOG(FATAL) << "Unexpected type conversion from " << input_type
2689 << " to " << result_type;
2690 }
2691}
2692
2693void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2694 LocationSummary* locations = conversion->GetLocations();
2695 Location out = locations->Out();
2696 Location in = locations->InAt(0);
2697 Primitive::Type result_type = conversion->GetResultType();
2698 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002699 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002700 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002701 case Primitive::kPrimByte:
2702 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002703 case Primitive::kPrimLong:
2704 // Type conversion from long to byte is a result of code transformations.
2705 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2706 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002707 case Primitive::kPrimBoolean:
2708 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002709 case Primitive::kPrimShort:
2710 case Primitive::kPrimInt:
2711 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002712 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002713 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002714 break;
2715
2716 default:
2717 LOG(FATAL) << "Unexpected type conversion from " << input_type
2718 << " to " << result_type;
2719 }
2720 break;
2721
Roland Levillain01a8d712014-11-14 16:27:39 +00002722 case Primitive::kPrimShort:
2723 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002724 case Primitive::kPrimLong:
2725 // Type conversion from long to short is a result of code transformations.
2726 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2727 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002728 case Primitive::kPrimBoolean:
2729 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002730 case Primitive::kPrimByte:
2731 case Primitive::kPrimInt:
2732 case Primitive::kPrimChar:
2733 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002734 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002735 break;
2736
2737 default:
2738 LOG(FATAL) << "Unexpected type conversion from " << input_type
2739 << " to " << result_type;
2740 }
2741 break;
2742
Roland Levillain946e1432014-11-11 17:35:19 +00002743 case Primitive::kPrimInt:
2744 switch (input_type) {
2745 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002746 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002747 DCHECK(out.IsRegister());
2748 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002749 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002750 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002751 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002752 } else {
2753 DCHECK(in.IsConstant());
2754 DCHECK(in.GetConstant()->IsLongConstant());
2755 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002756 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002757 }
2758 break;
2759
Roland Levillain3f8f9362014-12-02 17:45:01 +00002760 case Primitive::kPrimFloat: {
2761 // Processing a Dex `float-to-int' instruction.
2762 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002763 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002764 __ vmovrs(out.AsRegister<Register>(), temp);
2765 break;
2766 }
2767
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002768 case Primitive::kPrimDouble: {
2769 // Processing a Dex `double-to-int' instruction.
2770 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002771 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002772 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002773 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002774 }
Roland Levillain946e1432014-11-11 17:35:19 +00002775
2776 default:
2777 LOG(FATAL) << "Unexpected type conversion from " << input_type
2778 << " to " << result_type;
2779 }
2780 break;
2781
Roland Levillaindff1f282014-11-05 14:15:05 +00002782 case Primitive::kPrimLong:
2783 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002784 case Primitive::kPrimBoolean:
2785 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002786 case Primitive::kPrimByte:
2787 case Primitive::kPrimShort:
2788 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002789 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002790 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002791 DCHECK(out.IsRegisterPair());
2792 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002793 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002794 // Sign extension.
2795 __ Asr(out.AsRegisterPairHigh<Register>(),
2796 out.AsRegisterPairLow<Register>(),
2797 31);
2798 break;
2799
2800 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002801 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002802 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002803 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002804 break;
2805
Roland Levillaindff1f282014-11-05 14:15:05 +00002806 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002807 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002808 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002809 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002810 break;
2811
2812 default:
2813 LOG(FATAL) << "Unexpected type conversion from " << input_type
2814 << " to " << result_type;
2815 }
2816 break;
2817
Roland Levillain981e4542014-11-14 11:47:14 +00002818 case Primitive::kPrimChar:
2819 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002820 case Primitive::kPrimLong:
2821 // Type conversion from long to char is a result of code transformations.
2822 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2823 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002824 case Primitive::kPrimBoolean:
2825 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002826 case Primitive::kPrimByte:
2827 case Primitive::kPrimShort:
2828 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002829 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002830 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002831 break;
2832
2833 default:
2834 LOG(FATAL) << "Unexpected type conversion from " << input_type
2835 << " to " << result_type;
2836 }
2837 break;
2838
Roland Levillaindff1f282014-11-05 14:15:05 +00002839 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002840 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002841 case Primitive::kPrimBoolean:
2842 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002843 case Primitive::kPrimByte:
2844 case Primitive::kPrimShort:
2845 case Primitive::kPrimInt:
2846 case Primitive::kPrimChar: {
2847 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002848 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2849 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002850 break;
2851 }
2852
Roland Levillain5b3ee562015-04-14 16:02:41 +01002853 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002854 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002855 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002856 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002857 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002858
Roland Levillaincff13742014-11-17 14:32:17 +00002859 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002860 // Processing a Dex `double-to-float' instruction.
2861 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2862 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002863 break;
2864
2865 default:
2866 LOG(FATAL) << "Unexpected type conversion from " << input_type
2867 << " to " << result_type;
2868 };
2869 break;
2870
Roland Levillaindff1f282014-11-05 14:15:05 +00002871 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002872 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002873 case Primitive::kPrimBoolean:
2874 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002875 case Primitive::kPrimByte:
2876 case Primitive::kPrimShort:
2877 case Primitive::kPrimInt:
2878 case Primitive::kPrimChar: {
2879 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002880 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002881 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2882 out.AsFpuRegisterPairLow<SRegister>());
2883 break;
2884 }
2885
Roland Levillain647b9ed2014-11-27 12:06:00 +00002886 case Primitive::kPrimLong: {
2887 // Processing a Dex `long-to-double' instruction.
2888 Register low = in.AsRegisterPairLow<Register>();
2889 Register high = in.AsRegisterPairHigh<Register>();
2890 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2891 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002892 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002893 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002894 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2895 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002896
Roland Levillain682393c2015-04-14 15:57:52 +01002897 // temp_d = int-to-double(high)
2898 __ vmovsr(temp_s, high);
2899 __ vcvtdi(temp_d, temp_s);
2900 // constant_d = k2Pow32EncodingForDouble
2901 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2902 // out_d = unsigned-to-double(low)
2903 __ vmovsr(out_s, low);
2904 __ vcvtdu(out_d, out_s);
2905 // out_d += temp_d * constant_d
2906 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002907 break;
2908 }
2909
Roland Levillaincff13742014-11-17 14:32:17 +00002910 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002911 // Processing a Dex `float-to-double' instruction.
2912 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2913 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002914 break;
2915
2916 default:
2917 LOG(FATAL) << "Unexpected type conversion from " << input_type
2918 << " to " << result_type;
2919 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002920 break;
2921
2922 default:
2923 LOG(FATAL) << "Unexpected type conversion from " << input_type
2924 << " to " << result_type;
2925 }
2926}
2927
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002928void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002929 LocationSummary* locations =
2930 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002931 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002932 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002933 locations->SetInAt(0, Location::RequiresRegister());
2934 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002935 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2936 break;
2937 }
2938
2939 case Primitive::kPrimLong: {
2940 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002941 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002942 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002943 break;
2944 }
2945
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002946 case Primitive::kPrimFloat:
2947 case Primitive::kPrimDouble: {
2948 locations->SetInAt(0, Location::RequiresFpuRegister());
2949 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002950 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002951 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002952 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002953
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002954 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002955 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002956 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002957}
2958
2959void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2960 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002961 Location out = locations->Out();
2962 Location first = locations->InAt(0);
2963 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002964 switch (add->GetResultType()) {
2965 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002966 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002967 __ add(out.AsRegister<Register>(),
2968 first.AsRegister<Register>(),
2969 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002970 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002971 __ AddConstant(out.AsRegister<Register>(),
2972 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002973 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002974 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002975 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002976
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002977 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002978 if (second.IsConstant()) {
2979 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2980 GenerateAddLongConst(out, first, value);
2981 } else {
2982 DCHECK(second.IsRegisterPair());
2983 __ adds(out.AsRegisterPairLow<Register>(),
2984 first.AsRegisterPairLow<Register>(),
2985 ShifterOperand(second.AsRegisterPairLow<Register>()));
2986 __ adc(out.AsRegisterPairHigh<Register>(),
2987 first.AsRegisterPairHigh<Register>(),
2988 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2989 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002990 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002991 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002992
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002993 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002994 __ vadds(out.AsFpuRegister<SRegister>(),
2995 first.AsFpuRegister<SRegister>(),
2996 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002997 break;
2998
2999 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003000 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3001 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3002 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003003 break;
3004
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003005 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003006 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003007 }
3008}
3009
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003010void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003011 LocationSummary* locations =
3012 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003013 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003014 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003015 locations->SetInAt(0, Location::RequiresRegister());
3016 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003017 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3018 break;
3019 }
3020
3021 case Primitive::kPrimLong: {
3022 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003023 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003024 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003025 break;
3026 }
Calin Juravle11351682014-10-23 15:38:15 +01003027 case Primitive::kPrimFloat:
3028 case Primitive::kPrimDouble: {
3029 locations->SetInAt(0, Location::RequiresFpuRegister());
3030 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003031 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003032 break;
Calin Juravle11351682014-10-23 15:38:15 +01003033 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003034 default:
Calin Juravle11351682014-10-23 15:38:15 +01003035 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003036 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003037}
3038
3039void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3040 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003041 Location out = locations->Out();
3042 Location first = locations->InAt(0);
3043 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003044 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003045 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003046 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003047 __ sub(out.AsRegister<Register>(),
3048 first.AsRegister<Register>(),
3049 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003050 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003051 __ AddConstant(out.AsRegister<Register>(),
3052 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003053 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003054 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003055 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003056 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003057
Calin Juravle11351682014-10-23 15:38:15 +01003058 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003059 if (second.IsConstant()) {
3060 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3061 GenerateAddLongConst(out, first, -value);
3062 } else {
3063 DCHECK(second.IsRegisterPair());
3064 __ subs(out.AsRegisterPairLow<Register>(),
3065 first.AsRegisterPairLow<Register>(),
3066 ShifterOperand(second.AsRegisterPairLow<Register>()));
3067 __ sbc(out.AsRegisterPairHigh<Register>(),
3068 first.AsRegisterPairHigh<Register>(),
3069 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3070 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003071 break;
Calin Juravle11351682014-10-23 15:38:15 +01003072 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003073
Calin Juravle11351682014-10-23 15:38:15 +01003074 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003075 __ vsubs(out.AsFpuRegister<SRegister>(),
3076 first.AsFpuRegister<SRegister>(),
3077 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003078 break;
Calin Juravle11351682014-10-23 15:38:15 +01003079 }
3080
3081 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003082 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3083 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3084 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003085 break;
3086 }
3087
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003088
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003089 default:
Calin Juravle11351682014-10-23 15:38:15 +01003090 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003091 }
3092}
3093
Calin Juravle34bacdf2014-10-07 20:23:36 +01003094void LocationsBuilderARM::VisitMul(HMul* mul) {
3095 LocationSummary* locations =
3096 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3097 switch (mul->GetResultType()) {
3098 case Primitive::kPrimInt:
3099 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003100 locations->SetInAt(0, Location::RequiresRegister());
3101 locations->SetInAt(1, Location::RequiresRegister());
3102 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003103 break;
3104 }
3105
Calin Juravleb5bfa962014-10-21 18:02:24 +01003106 case Primitive::kPrimFloat:
3107 case Primitive::kPrimDouble: {
3108 locations->SetInAt(0, Location::RequiresFpuRegister());
3109 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003110 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003111 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003112 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003113
3114 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003115 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003116 }
3117}
3118
3119void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3120 LocationSummary* locations = mul->GetLocations();
3121 Location out = locations->Out();
3122 Location first = locations->InAt(0);
3123 Location second = locations->InAt(1);
3124 switch (mul->GetResultType()) {
3125 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003126 __ mul(out.AsRegister<Register>(),
3127 first.AsRegister<Register>(),
3128 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003129 break;
3130 }
3131 case Primitive::kPrimLong: {
3132 Register out_hi = out.AsRegisterPairHigh<Register>();
3133 Register out_lo = out.AsRegisterPairLow<Register>();
3134 Register in1_hi = first.AsRegisterPairHigh<Register>();
3135 Register in1_lo = first.AsRegisterPairLow<Register>();
3136 Register in2_hi = second.AsRegisterPairHigh<Register>();
3137 Register in2_lo = second.AsRegisterPairLow<Register>();
3138
3139 // Extra checks to protect caused by the existence of R1_R2.
3140 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3141 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3142 DCHECK_NE(out_hi, in1_lo);
3143 DCHECK_NE(out_hi, in2_lo);
3144
3145 // input: in1 - 64 bits, in2 - 64 bits
3146 // output: out
3147 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3148 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3149 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3150
3151 // IP <- in1.lo * in2.hi
3152 __ mul(IP, in1_lo, in2_hi);
3153 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3154 __ mla(out_hi, in1_hi, in2_lo, IP);
3155 // out.lo <- (in1.lo * in2.lo)[31:0];
3156 __ umull(out_lo, IP, in1_lo, in2_lo);
3157 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3158 __ add(out_hi, out_hi, ShifterOperand(IP));
3159 break;
3160 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003161
3162 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003163 __ vmuls(out.AsFpuRegister<SRegister>(),
3164 first.AsFpuRegister<SRegister>(),
3165 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003166 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003167 }
3168
3169 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003170 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3171 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3172 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003173 break;
3174 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003175
3176 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003177 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003178 }
3179}
3180
Zheng Xuc6667102015-05-15 16:08:45 +08003181void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3182 DCHECK(instruction->IsDiv() || instruction->IsRem());
3183 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3184
3185 LocationSummary* locations = instruction->GetLocations();
3186 Location second = locations->InAt(1);
3187 DCHECK(second.IsConstant());
3188
3189 Register out = locations->Out().AsRegister<Register>();
3190 Register dividend = locations->InAt(0).AsRegister<Register>();
3191 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3192 DCHECK(imm == 1 || imm == -1);
3193
3194 if (instruction->IsRem()) {
3195 __ LoadImmediate(out, 0);
3196 } else {
3197 if (imm == 1) {
3198 __ Mov(out, dividend);
3199 } else {
3200 __ rsb(out, dividend, ShifterOperand(0));
3201 }
3202 }
3203}
3204
3205void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3206 DCHECK(instruction->IsDiv() || instruction->IsRem());
3207 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3208
3209 LocationSummary* locations = instruction->GetLocations();
3210 Location second = locations->InAt(1);
3211 DCHECK(second.IsConstant());
3212
3213 Register out = locations->Out().AsRegister<Register>();
3214 Register dividend = locations->InAt(0).AsRegister<Register>();
3215 Register temp = locations->GetTemp(0).AsRegister<Register>();
3216 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003217 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003218 int ctz_imm = CTZ(abs_imm);
3219
3220 if (ctz_imm == 1) {
3221 __ Lsr(temp, dividend, 32 - ctz_imm);
3222 } else {
3223 __ Asr(temp, dividend, 31);
3224 __ Lsr(temp, temp, 32 - ctz_imm);
3225 }
3226 __ add(out, temp, ShifterOperand(dividend));
3227
3228 if (instruction->IsDiv()) {
3229 __ Asr(out, out, ctz_imm);
3230 if (imm < 0) {
3231 __ rsb(out, out, ShifterOperand(0));
3232 }
3233 } else {
3234 __ ubfx(out, out, 0, ctz_imm);
3235 __ sub(out, out, ShifterOperand(temp));
3236 }
3237}
3238
3239void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3240 DCHECK(instruction->IsDiv() || instruction->IsRem());
3241 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3242
3243 LocationSummary* locations = instruction->GetLocations();
3244 Location second = locations->InAt(1);
3245 DCHECK(second.IsConstant());
3246
3247 Register out = locations->Out().AsRegister<Register>();
3248 Register dividend = locations->InAt(0).AsRegister<Register>();
3249 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3250 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3251 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3252
3253 int64_t magic;
3254 int shift;
3255 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3256
3257 __ LoadImmediate(temp1, magic);
3258 __ smull(temp2, temp1, dividend, temp1);
3259
3260 if (imm > 0 && magic < 0) {
3261 __ add(temp1, temp1, ShifterOperand(dividend));
3262 } else if (imm < 0 && magic > 0) {
3263 __ sub(temp1, temp1, ShifterOperand(dividend));
3264 }
3265
3266 if (shift != 0) {
3267 __ Asr(temp1, temp1, shift);
3268 }
3269
3270 if (instruction->IsDiv()) {
3271 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3272 } else {
3273 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3274 // TODO: Strength reduction for mls.
3275 __ LoadImmediate(temp2, imm);
3276 __ mls(out, temp1, temp2, dividend);
3277 }
3278}
3279
3280void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3281 DCHECK(instruction->IsDiv() || instruction->IsRem());
3282 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3283
3284 LocationSummary* locations = instruction->GetLocations();
3285 Location second = locations->InAt(1);
3286 DCHECK(second.IsConstant());
3287
3288 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3289 if (imm == 0) {
3290 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3291 } else if (imm == 1 || imm == -1) {
3292 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003293 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003294 DivRemByPowerOfTwo(instruction);
3295 } else {
3296 DCHECK(imm <= -2 || imm >= 2);
3297 GenerateDivRemWithAnyConstant(instruction);
3298 }
3299}
3300
Calin Juravle7c4954d2014-10-28 16:57:40 +00003301void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003302 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3303 if (div->GetResultType() == Primitive::kPrimLong) {
3304 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003305 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003306 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3307 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003308 } else if (div->GetResultType() == Primitive::kPrimInt &&
3309 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3310 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003311 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003312 }
3313
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003314 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3315
Calin Juravle7c4954d2014-10-28 16:57:40 +00003316 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003317 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003318 if (div->InputAt(1)->IsConstant()) {
3319 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003320 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003321 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003322 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3323 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003324 // No temp register required.
3325 } else {
3326 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003327 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003328 locations->AddTemp(Location::RequiresRegister());
3329 }
3330 }
3331 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003332 locations->SetInAt(0, Location::RequiresRegister());
3333 locations->SetInAt(1, Location::RequiresRegister());
3334 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3335 } else {
3336 InvokeRuntimeCallingConvention calling_convention;
3337 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3338 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003339 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003340 // we only need the former.
3341 locations->SetOut(Location::RegisterLocation(R0));
3342 }
Calin Juravled0d48522014-11-04 16:40:20 +00003343 break;
3344 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003345 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003346 InvokeRuntimeCallingConvention calling_convention;
3347 locations->SetInAt(0, Location::RegisterPairLocation(
3348 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3349 locations->SetInAt(1, Location::RegisterPairLocation(
3350 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003351 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003352 break;
3353 }
3354 case Primitive::kPrimFloat:
3355 case Primitive::kPrimDouble: {
3356 locations->SetInAt(0, Location::RequiresFpuRegister());
3357 locations->SetInAt(1, Location::RequiresFpuRegister());
3358 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3359 break;
3360 }
3361
3362 default:
3363 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3364 }
3365}
3366
3367void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3368 LocationSummary* locations = div->GetLocations();
3369 Location out = locations->Out();
3370 Location first = locations->InAt(0);
3371 Location second = locations->InAt(1);
3372
3373 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003374 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003375 if (second.IsConstant()) {
3376 GenerateDivRemConstantIntegral(div);
3377 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003378 __ sdiv(out.AsRegister<Register>(),
3379 first.AsRegister<Register>(),
3380 second.AsRegister<Register>());
3381 } else {
3382 InvokeRuntimeCallingConvention calling_convention;
3383 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3384 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3385 DCHECK_EQ(R0, out.AsRegister<Register>());
3386
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003387 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003388 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003389 }
Calin Juravled0d48522014-11-04 16:40:20 +00003390 break;
3391 }
3392
Calin Juravle7c4954d2014-10-28 16:57:40 +00003393 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003394 InvokeRuntimeCallingConvention calling_convention;
3395 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3396 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3397 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3398 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3399 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003400 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003401
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003402 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003403 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003404 break;
3405 }
3406
3407 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003408 __ vdivs(out.AsFpuRegister<SRegister>(),
3409 first.AsFpuRegister<SRegister>(),
3410 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003411 break;
3412 }
3413
3414 case Primitive::kPrimDouble: {
3415 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3416 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3417 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3418 break;
3419 }
3420
3421 default:
3422 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3423 }
3424}
3425
Calin Juravlebacfec32014-11-14 15:54:36 +00003426void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003427 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003428
3429 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003430 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003431 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3432 // sdiv will be replaced by other instruction sequence.
3433 call_kind = LocationSummary::kNoCall;
3434 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3435 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003436 // Have hardware divide instruction for int, do it with three instructions.
3437 call_kind = LocationSummary::kNoCall;
3438 }
3439
Calin Juravlebacfec32014-11-14 15:54:36 +00003440 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3441
Calin Juravled2ec87d2014-12-08 14:24:46 +00003442 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003443 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003444 if (rem->InputAt(1)->IsConstant()) {
3445 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003446 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003447 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003448 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3449 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003450 // No temp register required.
3451 } else {
3452 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003453 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003454 locations->AddTemp(Location::RequiresRegister());
3455 }
3456 }
3457 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003458 locations->SetInAt(0, Location::RequiresRegister());
3459 locations->SetInAt(1, Location::RequiresRegister());
3460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3461 locations->AddTemp(Location::RequiresRegister());
3462 } else {
3463 InvokeRuntimeCallingConvention calling_convention;
3464 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3465 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003466 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003467 // we only need the latter.
3468 locations->SetOut(Location::RegisterLocation(R1));
3469 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003470 break;
3471 }
3472 case Primitive::kPrimLong: {
3473 InvokeRuntimeCallingConvention calling_convention;
3474 locations->SetInAt(0, Location::RegisterPairLocation(
3475 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3476 locations->SetInAt(1, Location::RegisterPairLocation(
3477 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3478 // The runtime helper puts the output in R2,R3.
3479 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3480 break;
3481 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003482 case Primitive::kPrimFloat: {
3483 InvokeRuntimeCallingConvention calling_convention;
3484 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3485 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3486 locations->SetOut(Location::FpuRegisterLocation(S0));
3487 break;
3488 }
3489
Calin Juravlebacfec32014-11-14 15:54:36 +00003490 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003491 InvokeRuntimeCallingConvention calling_convention;
3492 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3493 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3494 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3495 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3496 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003497 break;
3498 }
3499
3500 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003501 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003502 }
3503}
3504
3505void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3506 LocationSummary* locations = rem->GetLocations();
3507 Location out = locations->Out();
3508 Location first = locations->InAt(0);
3509 Location second = locations->InAt(1);
3510
Calin Juravled2ec87d2014-12-08 14:24:46 +00003511 Primitive::Type type = rem->GetResultType();
3512 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003513 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003514 if (second.IsConstant()) {
3515 GenerateDivRemConstantIntegral(rem);
3516 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003517 Register reg1 = first.AsRegister<Register>();
3518 Register reg2 = second.AsRegister<Register>();
3519 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003520
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003521 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003522 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003523 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003524 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003525 } else {
3526 InvokeRuntimeCallingConvention calling_convention;
3527 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3528 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3529 DCHECK_EQ(R1, out.AsRegister<Register>());
3530
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003531 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003532 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003533 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003534 break;
3535 }
3536
3537 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003538 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003539 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003540 break;
3541 }
3542
Calin Juravled2ec87d2014-12-08 14:24:46 +00003543 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003544 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003545 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003546 break;
3547 }
3548
Calin Juravlebacfec32014-11-14 15:54:36 +00003549 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003550 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003551 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003552 break;
3553 }
3554
3555 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003556 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003557 }
3558}
3559
Calin Juravled0d48522014-11-04 16:40:20 +00003560void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003561 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003562 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003563}
3564
3565void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003566 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003567 codegen_->AddSlowPath(slow_path);
3568
3569 LocationSummary* locations = instruction->GetLocations();
3570 Location value = locations->InAt(0);
3571
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003572 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003573 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003574 case Primitive::kPrimByte:
3575 case Primitive::kPrimChar:
3576 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003577 case Primitive::kPrimInt: {
3578 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003579 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003580 } else {
3581 DCHECK(value.IsConstant()) << value;
3582 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3583 __ b(slow_path->GetEntryLabel());
3584 }
3585 }
3586 break;
3587 }
3588 case Primitive::kPrimLong: {
3589 if (value.IsRegisterPair()) {
3590 __ orrs(IP,
3591 value.AsRegisterPairLow<Register>(),
3592 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3593 __ b(slow_path->GetEntryLabel(), EQ);
3594 } else {
3595 DCHECK(value.IsConstant()) << value;
3596 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3597 __ b(slow_path->GetEntryLabel());
3598 }
3599 }
3600 break;
3601 default:
3602 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3603 }
3604 }
Calin Juravled0d48522014-11-04 16:40:20 +00003605}
3606
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003607void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3608 Register in = locations->InAt(0).AsRegister<Register>();
3609 Location rhs = locations->InAt(1);
3610 Register out = locations->Out().AsRegister<Register>();
3611
3612 if (rhs.IsConstant()) {
3613 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3614 // so map all rotations to a +ve. equivalent in that range.
3615 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3616 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3617 if (rot) {
3618 // Rotate, mapping left rotations to right equivalents if necessary.
3619 // (e.g. left by 2 bits == right by 30.)
3620 __ Ror(out, in, rot);
3621 } else if (out != in) {
3622 __ Mov(out, in);
3623 }
3624 } else {
3625 __ Ror(out, in, rhs.AsRegister<Register>());
3626 }
3627}
3628
3629// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3630// rotates by swapping input regs (effectively rotating by the first 32-bits of
3631// a larger rotation) or flipping direction (thus treating larger right/left
3632// rotations as sub-word sized rotations in the other direction) as appropriate.
3633void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3634 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3635 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3636 Location rhs = locations->InAt(1);
3637 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3638 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3639
3640 if (rhs.IsConstant()) {
3641 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3642 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003643 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003644 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3645 // logic below to a simple pair of binary orr.
3646 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3647 if (rot >= kArmBitsPerWord) {
3648 rot -= kArmBitsPerWord;
3649 std::swap(in_reg_hi, in_reg_lo);
3650 }
3651 // Rotate, or mov to out for zero or word size rotations.
3652 if (rot != 0u) {
3653 __ Lsr(out_reg_hi, in_reg_hi, rot);
3654 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3655 __ Lsr(out_reg_lo, in_reg_lo, rot);
3656 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3657 } else {
3658 __ Mov(out_reg_lo, in_reg_lo);
3659 __ Mov(out_reg_hi, in_reg_hi);
3660 }
3661 } else {
3662 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3663 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3664 Label end;
3665 Label shift_by_32_plus_shift_right;
3666
3667 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3668 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3669 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3670 __ b(&shift_by_32_plus_shift_right, CC);
3671
3672 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3673 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3674 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3675 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3676 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3677 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3678 __ Lsr(shift_left, in_reg_hi, shift_right);
3679 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3680 __ b(&end);
3681
3682 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3683 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3684 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3685 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3686 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3687 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3688 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3689 __ Lsl(shift_right, in_reg_hi, shift_left);
3690 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3691
3692 __ Bind(&end);
3693 }
3694}
Roland Levillain22c49222016-03-18 14:04:28 +00003695
3696void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003697 LocationSummary* locations =
3698 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3699 switch (ror->GetResultType()) {
3700 case Primitive::kPrimInt: {
3701 locations->SetInAt(0, Location::RequiresRegister());
3702 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3703 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3704 break;
3705 }
3706 case Primitive::kPrimLong: {
3707 locations->SetInAt(0, Location::RequiresRegister());
3708 if (ror->InputAt(1)->IsConstant()) {
3709 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3710 } else {
3711 locations->SetInAt(1, Location::RequiresRegister());
3712 locations->AddTemp(Location::RequiresRegister());
3713 locations->AddTemp(Location::RequiresRegister());
3714 }
3715 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3716 break;
3717 }
3718 default:
3719 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3720 }
3721}
3722
Roland Levillain22c49222016-03-18 14:04:28 +00003723void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003724 LocationSummary* locations = ror->GetLocations();
3725 Primitive::Type type = ror->GetResultType();
3726 switch (type) {
3727 case Primitive::kPrimInt: {
3728 HandleIntegerRotate(locations);
3729 break;
3730 }
3731 case Primitive::kPrimLong: {
3732 HandleLongRotate(locations);
3733 break;
3734 }
3735 default:
3736 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003737 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003738 }
3739}
3740
Calin Juravle9aec02f2014-11-18 23:06:35 +00003741void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3742 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3743
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003744 LocationSummary* locations =
3745 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003746
3747 switch (op->GetResultType()) {
3748 case Primitive::kPrimInt: {
3749 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003750 if (op->InputAt(1)->IsConstant()) {
3751 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3752 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3753 } else {
3754 locations->SetInAt(1, Location::RequiresRegister());
3755 // Make the output overlap, as it will be used to hold the masked
3756 // second input.
3757 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3758 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003759 break;
3760 }
3761 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003762 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003763 if (op->InputAt(1)->IsConstant()) {
3764 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3765 // For simplicity, use kOutputOverlap even though we only require that low registers
3766 // don't clash with high registers which the register allocator currently guarantees.
3767 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3768 } else {
3769 locations->SetInAt(1, Location::RequiresRegister());
3770 locations->AddTemp(Location::RequiresRegister());
3771 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3772 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003773 break;
3774 }
3775 default:
3776 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3777 }
3778}
3779
3780void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3781 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3782
3783 LocationSummary* locations = op->GetLocations();
3784 Location out = locations->Out();
3785 Location first = locations->InAt(0);
3786 Location second = locations->InAt(1);
3787
3788 Primitive::Type type = op->GetResultType();
3789 switch (type) {
3790 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003791 Register out_reg = out.AsRegister<Register>();
3792 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003793 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003794 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003795 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003796 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003797 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003798 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003799 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003800 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003801 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003802 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003803 }
3804 } else {
3805 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003806 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003807 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003808 __ Mov(out_reg, first_reg);
3809 } else if (op->IsShl()) {
3810 __ Lsl(out_reg, first_reg, shift_value);
3811 } else if (op->IsShr()) {
3812 __ Asr(out_reg, first_reg, shift_value);
3813 } else {
3814 __ Lsr(out_reg, first_reg, shift_value);
3815 }
3816 }
3817 break;
3818 }
3819 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003820 Register o_h = out.AsRegisterPairHigh<Register>();
3821 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003822
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003823 Register high = first.AsRegisterPairHigh<Register>();
3824 Register low = first.AsRegisterPairLow<Register>();
3825
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003826 if (second.IsRegister()) {
3827 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003828
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003829 Register second_reg = second.AsRegister<Register>();
3830
3831 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003832 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003833 // Shift the high part
3834 __ Lsl(o_h, high, o_l);
3835 // Shift the low part and `or` what overflew on the high part
3836 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3837 __ Lsr(temp, low, temp);
3838 __ orr(o_h, o_h, ShifterOperand(temp));
3839 // If the shift is > 32 bits, override the high part
3840 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3841 __ it(PL);
3842 __ Lsl(o_h, low, temp, PL);
3843 // Shift the low part
3844 __ Lsl(o_l, low, o_l);
3845 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003846 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003847 // Shift the low part
3848 __ Lsr(o_l, low, o_h);
3849 // Shift the high part and `or` what underflew on the low part
3850 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3851 __ Lsl(temp, high, temp);
3852 __ orr(o_l, o_l, ShifterOperand(temp));
3853 // If the shift is > 32 bits, override the low part
3854 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3855 __ it(PL);
3856 __ Asr(o_l, high, temp, PL);
3857 // Shift the high part
3858 __ Asr(o_h, high, o_h);
3859 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003860 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003861 // same as Shr except we use `Lsr`s and not `Asr`s
3862 __ Lsr(o_l, low, o_h);
3863 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3864 __ Lsl(temp, high, temp);
3865 __ orr(o_l, o_l, ShifterOperand(temp));
3866 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3867 __ it(PL);
3868 __ Lsr(o_l, high, temp, PL);
3869 __ Lsr(o_h, high, o_h);
3870 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003871 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003872 // Register allocator doesn't create partial overlap.
3873 DCHECK_NE(o_l, high);
3874 DCHECK_NE(o_h, low);
3875 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003876 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003877 if (shift_value > 32) {
3878 if (op->IsShl()) {
3879 __ Lsl(o_h, low, shift_value - 32);
3880 __ LoadImmediate(o_l, 0);
3881 } else if (op->IsShr()) {
3882 __ Asr(o_l, high, shift_value - 32);
3883 __ Asr(o_h, high, 31);
3884 } else {
3885 __ Lsr(o_l, high, shift_value - 32);
3886 __ LoadImmediate(o_h, 0);
3887 }
3888 } else if (shift_value == 32) {
3889 if (op->IsShl()) {
3890 __ mov(o_h, ShifterOperand(low));
3891 __ LoadImmediate(o_l, 0);
3892 } else if (op->IsShr()) {
3893 __ mov(o_l, ShifterOperand(high));
3894 __ Asr(o_h, high, 31);
3895 } else {
3896 __ mov(o_l, ShifterOperand(high));
3897 __ LoadImmediate(o_h, 0);
3898 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003899 } else if (shift_value == 1) {
3900 if (op->IsShl()) {
3901 __ Lsls(o_l, low, 1);
3902 __ adc(o_h, high, ShifterOperand(high));
3903 } else if (op->IsShr()) {
3904 __ Asrs(o_h, high, 1);
3905 __ Rrx(o_l, low);
3906 } else {
3907 __ Lsrs(o_h, high, 1);
3908 __ Rrx(o_l, low);
3909 }
3910 } else {
3911 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003912 if (op->IsShl()) {
3913 __ Lsl(o_h, high, shift_value);
3914 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3915 __ Lsl(o_l, low, shift_value);
3916 } else if (op->IsShr()) {
3917 __ Lsr(o_l, low, shift_value);
3918 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3919 __ Asr(o_h, high, shift_value);
3920 } else {
3921 __ Lsr(o_l, low, shift_value);
3922 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3923 __ Lsr(o_h, high, shift_value);
3924 }
3925 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003926 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003927 break;
3928 }
3929 default:
3930 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003931 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003932 }
3933}
3934
3935void LocationsBuilderARM::VisitShl(HShl* shl) {
3936 HandleShift(shl);
3937}
3938
3939void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3940 HandleShift(shl);
3941}
3942
3943void LocationsBuilderARM::VisitShr(HShr* shr) {
3944 HandleShift(shr);
3945}
3946
3947void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3948 HandleShift(shr);
3949}
3950
3951void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3952 HandleShift(ushr);
3953}
3954
3955void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3956 HandleShift(ushr);
3957}
3958
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003959void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003960 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003961 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003962 if (instruction->IsStringAlloc()) {
3963 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3964 } else {
3965 InvokeRuntimeCallingConvention calling_convention;
3966 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00003967 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003968 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003969}
3970
3971void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003972 // Note: if heap poisoning is enabled, the entry point takes cares
3973 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003974 if (instruction->IsStringAlloc()) {
3975 // String is allocated through StringFactory. Call NewEmptyString entry point.
3976 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003977 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003978 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3979 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3980 __ blx(LR);
3981 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3982 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003983 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00003984 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00003985 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003986}
3987
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003988void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3989 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003990 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003991 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003992 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00003993 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3994 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003995}
3996
3997void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003998 // Note: if heap poisoning is enabled, the entry point takes cares
3999 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004000 QuickEntrypointEnum entrypoint =
4001 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4002 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004003 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004004 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004005}
4006
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004007void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004008 LocationSummary* locations =
4009 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004010 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4011 if (location.IsStackSlot()) {
4012 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4013 } else if (location.IsDoubleStackSlot()) {
4014 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004015 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004016 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004017}
4018
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004019void InstructionCodeGeneratorARM::VisitParameterValue(
4020 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004021 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004022}
4023
4024void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4025 LocationSummary* locations =
4026 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4027 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4028}
4029
4030void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4031 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004032}
4033
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004034void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004035 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004036 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004037 locations->SetInAt(0, Location::RequiresRegister());
4038 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004039}
4040
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004041void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4042 LocationSummary* locations = not_->GetLocations();
4043 Location out = locations->Out();
4044 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004045 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004046 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004047 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004048 break;
4049
4050 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004051 __ mvn(out.AsRegisterPairLow<Register>(),
4052 ShifterOperand(in.AsRegisterPairLow<Register>()));
4053 __ mvn(out.AsRegisterPairHigh<Register>(),
4054 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004055 break;
4056
4057 default:
4058 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4059 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004060}
4061
David Brazdil66d126e2015-04-03 16:02:44 +01004062void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4063 LocationSummary* locations =
4064 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4065 locations->SetInAt(0, Location::RequiresRegister());
4066 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4067}
4068
4069void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004070 LocationSummary* locations = bool_not->GetLocations();
4071 Location out = locations->Out();
4072 Location in = locations->InAt(0);
4073 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4074}
4075
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004076void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004077 LocationSummary* locations =
4078 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004079 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004080 case Primitive::kPrimBoolean:
4081 case Primitive::kPrimByte:
4082 case Primitive::kPrimShort:
4083 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004084 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004085 case Primitive::kPrimLong: {
4086 locations->SetInAt(0, Location::RequiresRegister());
4087 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004088 // Output overlaps because it is written before doing the low comparison.
4089 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004090 break;
4091 }
4092 case Primitive::kPrimFloat:
4093 case Primitive::kPrimDouble: {
4094 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004095 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004096 locations->SetOut(Location::RequiresRegister());
4097 break;
4098 }
4099 default:
4100 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4101 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004102}
4103
4104void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004105 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004106 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004107 Location left = locations->InAt(0);
4108 Location right = locations->InAt(1);
4109
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004110 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004111 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004112 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004113 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004114 case Primitive::kPrimBoolean:
4115 case Primitive::kPrimByte:
4116 case Primitive::kPrimShort:
4117 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004118 case Primitive::kPrimInt: {
4119 __ LoadImmediate(out, 0);
4120 __ cmp(left.AsRegister<Register>(),
4121 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4122 less_cond = LT;
4123 break;
4124 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004125 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004126 __ cmp(left.AsRegisterPairHigh<Register>(),
4127 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004128 __ b(&less, LT);
4129 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004130 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004131 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004132 __ cmp(left.AsRegisterPairLow<Register>(),
4133 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004134 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004135 break;
4136 }
4137 case Primitive::kPrimFloat:
4138 case Primitive::kPrimDouble: {
4139 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004140 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004141 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004142 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004143 break;
4144 }
4145 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004146 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004147 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004148 }
Aart Bika19616e2016-02-01 18:57:58 -08004149
Calin Juravleddb7df22014-11-25 20:56:51 +00004150 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004151 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004152
4153 __ Bind(&greater);
4154 __ LoadImmediate(out, 1);
4155 __ b(&done);
4156
4157 __ Bind(&less);
4158 __ LoadImmediate(out, -1);
4159
4160 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004161}
4162
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004163void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004164 LocationSummary* locations =
4165 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004166 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004167 locations->SetInAt(i, Location::Any());
4168 }
4169 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004170}
4171
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004172void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004173 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004174}
4175
Roland Levillainc9285912015-12-18 10:38:42 +00004176void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4177 // TODO (ported from quick): revisit ARM barrier kinds.
4178 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004179 switch (kind) {
4180 case MemBarrierKind::kAnyStore:
4181 case MemBarrierKind::kLoadAny:
4182 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004183 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004184 break;
4185 }
4186 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004187 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004188 break;
4189 }
4190 default:
4191 LOG(FATAL) << "Unexpected memory barrier " << kind;
4192 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004193 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004194}
4195
4196void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4197 uint32_t offset,
4198 Register out_lo,
4199 Register out_hi) {
4200 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004201 // Ensure `out_lo` is different from `addr`, so that loading
4202 // `offset` into `out_lo` does not clutter `addr`.
4203 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004204 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004205 __ add(IP, addr, ShifterOperand(out_lo));
4206 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004207 }
4208 __ ldrexd(out_lo, out_hi, addr);
4209}
4210
4211void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4212 uint32_t offset,
4213 Register value_lo,
4214 Register value_hi,
4215 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004216 Register temp2,
4217 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004218 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004219 if (offset != 0) {
4220 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004221 __ add(IP, addr, ShifterOperand(temp1));
4222 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004223 }
4224 __ Bind(&fail);
4225 // We need a load followed by store. (The address used in a STREX instruction must
4226 // be the same as the address in the most recently executed LDREX instruction.)
4227 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004228 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004229 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004230 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004231}
4232
4233void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4234 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4235
Nicolas Geoffray39468442014-09-02 15:17:15 +01004236 LocationSummary* locations =
4237 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004238 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004239
Calin Juravle52c48962014-12-16 17:02:57 +00004240 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004241 if (Primitive::IsFloatingPointType(field_type)) {
4242 locations->SetInAt(1, Location::RequiresFpuRegister());
4243 } else {
4244 locations->SetInAt(1, Location::RequiresRegister());
4245 }
4246
Calin Juravle52c48962014-12-16 17:02:57 +00004247 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004248 bool generate_volatile = field_info.IsVolatile()
4249 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004250 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004251 bool needs_write_barrier =
4252 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004253 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004254 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004255 if (needs_write_barrier) {
4256 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004257 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004258 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004259 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004260 // - registers need to be consecutive
4261 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004262 // We don't test for ARM yet, and the assertion makes sure that we
4263 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004264 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4265
4266 locations->AddTemp(Location::RequiresRegister());
4267 locations->AddTemp(Location::RequiresRegister());
4268 if (field_type == Primitive::kPrimDouble) {
4269 // For doubles we need two more registers to copy the value.
4270 locations->AddTemp(Location::RegisterLocation(R2));
4271 locations->AddTemp(Location::RegisterLocation(R3));
4272 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004273 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004274}
4275
Calin Juravle52c48962014-12-16 17:02:57 +00004276void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004277 const FieldInfo& field_info,
4278 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004279 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4280
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004281 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004282 Register base = locations->InAt(0).AsRegister<Register>();
4283 Location value = locations->InAt(1);
4284
4285 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004286 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004287 Primitive::Type field_type = field_info.GetFieldType();
4288 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004289 bool needs_write_barrier =
4290 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004291
4292 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004293 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004294 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004295
4296 switch (field_type) {
4297 case Primitive::kPrimBoolean:
4298 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004299 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004300 break;
4301 }
4302
4303 case Primitive::kPrimShort:
4304 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004305 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004306 break;
4307 }
4308
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004309 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004310 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004311 if (kPoisonHeapReferences && needs_write_barrier) {
4312 // Note that in the case where `value` is a null reference,
4313 // we do not enter this block, as a null reference does not
4314 // need poisoning.
4315 DCHECK_EQ(field_type, Primitive::kPrimNot);
4316 Register temp = locations->GetTemp(0).AsRegister<Register>();
4317 __ Mov(temp, value.AsRegister<Register>());
4318 __ PoisonHeapReference(temp);
4319 __ StoreToOffset(kStoreWord, temp, base, offset);
4320 } else {
4321 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4322 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004323 break;
4324 }
4325
4326 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004327 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004328 GenerateWideAtomicStore(base, offset,
4329 value.AsRegisterPairLow<Register>(),
4330 value.AsRegisterPairHigh<Register>(),
4331 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004332 locations->GetTemp(1).AsRegister<Register>(),
4333 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004334 } else {
4335 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004336 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004337 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004338 break;
4339 }
4340
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004341 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004342 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004343 break;
4344 }
4345
4346 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004347 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004348 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004349 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4350 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4351
4352 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4353
4354 GenerateWideAtomicStore(base, offset,
4355 value_reg_lo,
4356 value_reg_hi,
4357 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004358 locations->GetTemp(3).AsRegister<Register>(),
4359 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004360 } else {
4361 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004362 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004363 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004364 break;
4365 }
4366
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004367 case Primitive::kPrimVoid:
4368 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004369 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004370 }
Calin Juravle52c48962014-12-16 17:02:57 +00004371
Calin Juravle77520bc2015-01-12 18:45:46 +00004372 // Longs and doubles are handled in the switch.
4373 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4374 codegen_->MaybeRecordImplicitNullCheck(instruction);
4375 }
4376
4377 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4378 Register temp = locations->GetTemp(0).AsRegister<Register>();
4379 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004380 codegen_->MarkGCCard(
4381 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004382 }
4383
Calin Juravle52c48962014-12-16 17:02:57 +00004384 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004385 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004386 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004387}
4388
Calin Juravle52c48962014-12-16 17:02:57 +00004389void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4390 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004391
4392 bool object_field_get_with_read_barrier =
4393 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004394 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004395 new (GetGraph()->GetArena()) LocationSummary(instruction,
4396 object_field_get_with_read_barrier ?
4397 LocationSummary::kCallOnSlowPath :
4398 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004399 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004400 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004401 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004402 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004403
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004404 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004405 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004406 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004407 // The output overlaps in case of volatile long: we don't want the
4408 // code generated by GenerateWideAtomicLoad to overwrite the
4409 // object's location. Likewise, in the case of an object field get
4410 // with read barriers enabled, we do not want the load to overwrite
4411 // the object's location, as we need it to emit the read barrier.
4412 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4413 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004414
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004415 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4416 locations->SetOut(Location::RequiresFpuRegister());
4417 } else {
4418 locations->SetOut(Location::RequiresRegister(),
4419 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4420 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004421 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004422 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004423 // - registers need to be consecutive
4424 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004425 // We don't test for ARM yet, and the assertion makes sure that we
4426 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004427 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4428 locations->AddTemp(Location::RequiresRegister());
4429 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004430 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4431 // We need a temporary register for the read barrier marking slow
4432 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4433 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004434 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004435}
4436
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004437Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4438 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4439 << input->GetType();
4440 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4441 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4442 return Location::ConstantLocation(input->AsConstant());
4443 } else {
4444 return Location::RequiresFpuRegister();
4445 }
4446}
4447
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004448Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4449 Opcode opcode) {
4450 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4451 if (constant->IsConstant() &&
4452 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4453 return Location::ConstantLocation(constant->AsConstant());
4454 }
4455 return Location::RequiresRegister();
4456}
4457
4458bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4459 Opcode opcode) {
4460 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4461 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004462 Opcode high_opcode = opcode;
4463 SetCc low_set_cc = kCcDontCare;
4464 switch (opcode) {
4465 case SUB:
4466 // Flip the operation to an ADD.
4467 value = -value;
4468 opcode = ADD;
4469 FALLTHROUGH_INTENDED;
4470 case ADD:
4471 if (Low32Bits(value) == 0u) {
4472 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4473 }
4474 high_opcode = ADC;
4475 low_set_cc = kCcSet;
4476 break;
4477 default:
4478 break;
4479 }
4480 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4481 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004482 } else {
4483 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4484 }
4485}
4486
Vladimir Marko59751a72016-08-05 14:37:27 +01004487bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4488 Opcode opcode,
4489 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004490 ShifterOperand so;
4491 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004492 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004493 return true;
4494 }
4495 Opcode neg_opcode = kNoOperand;
4496 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004497 case AND: neg_opcode = BIC; value = ~value; break;
4498 case ORR: neg_opcode = ORN; value = ~value; break;
4499 case ADD: neg_opcode = SUB; value = -value; break;
4500 case ADC: neg_opcode = SBC; value = ~value; break;
4501 case SUB: neg_opcode = ADD; value = -value; break;
4502 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004503 default:
4504 return false;
4505 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004506 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004507}
4508
Calin Juravle52c48962014-12-16 17:02:57 +00004509void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4510 const FieldInfo& field_info) {
4511 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004512
Calin Juravle52c48962014-12-16 17:02:57 +00004513 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004514 Location base_loc = locations->InAt(0);
4515 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004516 Location out = locations->Out();
4517 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004518 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004519 Primitive::Type field_type = field_info.GetFieldType();
4520 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4521
4522 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004523 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004524 __ LoadFromOffset(kLoadUnsignedByte, 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::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004528 __ LoadFromOffset(kLoadSignedByte, 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::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004532 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004533 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004534
Roland Levillainc9285912015-12-18 10:38:42 +00004535 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004536 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004537 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004538
4539 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004540 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004541 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004542
4543 case Primitive::kPrimNot: {
4544 // /* HeapReference<Object> */ out = *(base + offset)
4545 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4546 Location temp_loc = locations->GetTemp(0);
4547 // Note that a potential implicit null check is handled in this
4548 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4549 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4550 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4551 if (is_volatile) {
4552 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4553 }
4554 } else {
4555 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4556 codegen_->MaybeRecordImplicitNullCheck(instruction);
4557 if (is_volatile) {
4558 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4559 }
4560 // If read barriers are enabled, emit read barriers other than
4561 // Baker's using a slow path (and also unpoison the loaded
4562 // reference, if heap poisoning is enabled).
4563 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4564 }
4565 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004566 }
4567
Roland Levillainc9285912015-12-18 10:38:42 +00004568 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004569 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004570 GenerateWideAtomicLoad(base, offset,
4571 out.AsRegisterPairLow<Register>(),
4572 out.AsRegisterPairHigh<Register>());
4573 } else {
4574 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4575 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004576 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004577
Roland Levillainc9285912015-12-18 10:38:42 +00004578 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004579 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004580 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004581
4582 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004583 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004584 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004585 Register lo = locations->GetTemp(0).AsRegister<Register>();
4586 Register hi = locations->GetTemp(1).AsRegister<Register>();
4587 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004588 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004589 __ vmovdrr(out_reg, lo, hi);
4590 } else {
4591 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004592 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004593 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004594 break;
4595 }
4596
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004597 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004598 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004599 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004600 }
Calin Juravle52c48962014-12-16 17:02:57 +00004601
Roland Levillainc9285912015-12-18 10:38:42 +00004602 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4603 // Potential implicit null checks, in the case of reference or
4604 // double fields, are handled in the previous switch statement.
4605 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004606 codegen_->MaybeRecordImplicitNullCheck(instruction);
4607 }
4608
Calin Juravle52c48962014-12-16 17:02:57 +00004609 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004610 if (field_type == Primitive::kPrimNot) {
4611 // Memory barriers, in the case of references, are also handled
4612 // in the previous switch statement.
4613 } else {
4614 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4615 }
Roland Levillain4d027112015-07-01 15:41:14 +01004616 }
Calin Juravle52c48962014-12-16 17:02:57 +00004617}
4618
4619void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4620 HandleFieldSet(instruction, instruction->GetFieldInfo());
4621}
4622
4623void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004624 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004625}
4626
4627void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4628 HandleFieldGet(instruction, instruction->GetFieldInfo());
4629}
4630
4631void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4632 HandleFieldGet(instruction, instruction->GetFieldInfo());
4633}
4634
4635void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4636 HandleFieldGet(instruction, instruction->GetFieldInfo());
4637}
4638
4639void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4640 HandleFieldGet(instruction, instruction->GetFieldInfo());
4641}
4642
4643void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4644 HandleFieldSet(instruction, instruction->GetFieldInfo());
4645}
4646
4647void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004648 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004649}
4650
Calin Juravlee460d1d2015-09-29 04:52:17 +01004651void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4652 HUnresolvedInstanceFieldGet* instruction) {
4653 FieldAccessCallingConventionARM calling_convention;
4654 codegen_->CreateUnresolvedFieldLocationSummary(
4655 instruction, instruction->GetFieldType(), calling_convention);
4656}
4657
4658void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4659 HUnresolvedInstanceFieldGet* instruction) {
4660 FieldAccessCallingConventionARM calling_convention;
4661 codegen_->GenerateUnresolvedFieldAccess(instruction,
4662 instruction->GetFieldType(),
4663 instruction->GetFieldIndex(),
4664 instruction->GetDexPc(),
4665 calling_convention);
4666}
4667
4668void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4669 HUnresolvedInstanceFieldSet* instruction) {
4670 FieldAccessCallingConventionARM calling_convention;
4671 codegen_->CreateUnresolvedFieldLocationSummary(
4672 instruction, instruction->GetFieldType(), calling_convention);
4673}
4674
4675void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4676 HUnresolvedInstanceFieldSet* instruction) {
4677 FieldAccessCallingConventionARM calling_convention;
4678 codegen_->GenerateUnresolvedFieldAccess(instruction,
4679 instruction->GetFieldType(),
4680 instruction->GetFieldIndex(),
4681 instruction->GetDexPc(),
4682 calling_convention);
4683}
4684
4685void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4686 HUnresolvedStaticFieldGet* instruction) {
4687 FieldAccessCallingConventionARM calling_convention;
4688 codegen_->CreateUnresolvedFieldLocationSummary(
4689 instruction, instruction->GetFieldType(), calling_convention);
4690}
4691
4692void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4693 HUnresolvedStaticFieldGet* instruction) {
4694 FieldAccessCallingConventionARM calling_convention;
4695 codegen_->GenerateUnresolvedFieldAccess(instruction,
4696 instruction->GetFieldType(),
4697 instruction->GetFieldIndex(),
4698 instruction->GetDexPc(),
4699 calling_convention);
4700}
4701
4702void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4703 HUnresolvedStaticFieldSet* instruction) {
4704 FieldAccessCallingConventionARM calling_convention;
4705 codegen_->CreateUnresolvedFieldLocationSummary(
4706 instruction, instruction->GetFieldType(), calling_convention);
4707}
4708
4709void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4710 HUnresolvedStaticFieldSet* instruction) {
4711 FieldAccessCallingConventionARM calling_convention;
4712 codegen_->GenerateUnresolvedFieldAccess(instruction,
4713 instruction->GetFieldType(),
4714 instruction->GetFieldIndex(),
4715 instruction->GetDexPc(),
4716 calling_convention);
4717}
4718
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004719void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004720 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4721 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004722}
4723
Calin Juravle2ae48182016-03-16 14:05:09 +00004724void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4725 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004726 return;
4727 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004728 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004729
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004730 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004731 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004732}
4733
Calin Juravle2ae48182016-03-16 14:05:09 +00004734void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004735 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004736 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004737
4738 LocationSummary* locations = instruction->GetLocations();
4739 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004740
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004741 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004742}
4743
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004744void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004745 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004746}
4747
Artem Serov6c916792016-07-11 14:02:34 +01004748static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4749 switch (type) {
4750 case Primitive::kPrimNot:
4751 return kLoadWord;
4752 case Primitive::kPrimBoolean:
4753 return kLoadUnsignedByte;
4754 case Primitive::kPrimByte:
4755 return kLoadSignedByte;
4756 case Primitive::kPrimChar:
4757 return kLoadUnsignedHalfword;
4758 case Primitive::kPrimShort:
4759 return kLoadSignedHalfword;
4760 case Primitive::kPrimInt:
4761 return kLoadWord;
4762 case Primitive::kPrimLong:
4763 return kLoadWordPair;
4764 case Primitive::kPrimFloat:
4765 return kLoadSWord;
4766 case Primitive::kPrimDouble:
4767 return kLoadDWord;
4768 default:
4769 LOG(FATAL) << "Unreachable type " << type;
4770 UNREACHABLE();
4771 }
4772}
4773
4774static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4775 switch (type) {
4776 case Primitive::kPrimNot:
4777 return kStoreWord;
4778 case Primitive::kPrimBoolean:
4779 case Primitive::kPrimByte:
4780 return kStoreByte;
4781 case Primitive::kPrimChar:
4782 case Primitive::kPrimShort:
4783 return kStoreHalfword;
4784 case Primitive::kPrimInt:
4785 return kStoreWord;
4786 case Primitive::kPrimLong:
4787 return kStoreWordPair;
4788 case Primitive::kPrimFloat:
4789 return kStoreSWord;
4790 case Primitive::kPrimDouble:
4791 return kStoreDWord;
4792 default:
4793 LOG(FATAL) << "Unreachable type " << type;
4794 UNREACHABLE();
4795 }
4796}
4797
4798void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4799 Location out_loc,
4800 Register base,
4801 Register reg_offset,
4802 Condition cond) {
4803 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4804 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4805
4806 switch (type) {
4807 case Primitive::kPrimByte:
4808 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4809 break;
4810 case Primitive::kPrimBoolean:
4811 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4812 break;
4813 case Primitive::kPrimShort:
4814 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4815 break;
4816 case Primitive::kPrimChar:
4817 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4818 break;
4819 case Primitive::kPrimNot:
4820 case Primitive::kPrimInt:
4821 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4822 break;
4823 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4824 case Primitive::kPrimLong:
4825 case Primitive::kPrimFloat:
4826 case Primitive::kPrimDouble:
4827 default:
4828 LOG(FATAL) << "Unreachable type " << type;
4829 UNREACHABLE();
4830 }
4831}
4832
4833void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4834 Location loc,
4835 Register base,
4836 Register reg_offset,
4837 Condition cond) {
4838 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4839 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4840
4841 switch (type) {
4842 case Primitive::kPrimByte:
4843 case Primitive::kPrimBoolean:
4844 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4845 break;
4846 case Primitive::kPrimShort:
4847 case Primitive::kPrimChar:
4848 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4849 break;
4850 case Primitive::kPrimNot:
4851 case Primitive::kPrimInt:
4852 __ str(loc.AsRegister<Register>(), mem_address, cond);
4853 break;
4854 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4855 case Primitive::kPrimLong:
4856 case Primitive::kPrimFloat:
4857 case Primitive::kPrimDouble:
4858 default:
4859 LOG(FATAL) << "Unreachable type " << type;
4860 UNREACHABLE();
4861 }
4862}
4863
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004864void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004865 bool object_array_get_with_read_barrier =
4866 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004867 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004868 new (GetGraph()->GetArena()) LocationSummary(instruction,
4869 object_array_get_with_read_barrier ?
4870 LocationSummary::kCallOnSlowPath :
4871 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004872 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004873 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004874 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004875 locations->SetInAt(0, Location::RequiresRegister());
4876 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004877 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4878 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4879 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004880 // The output overlaps in the case of an object array get with
4881 // read barriers enabled: we do not want the move to overwrite the
4882 // array's location, as we need it to emit the read barrier.
4883 locations->SetOut(
4884 Location::RequiresRegister(),
4885 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004886 }
Roland Levillainc9285912015-12-18 10:38:42 +00004887 // We need a temporary register for the read barrier marking slow
4888 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07004889 // Also need for String compression feature.
4890 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4891 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00004892 locations->AddTemp(Location::RequiresRegister());
4893 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004894}
4895
4896void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4897 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004898 Location obj_loc = locations->InAt(0);
4899 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004900 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004901 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004902 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004903 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07004904 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4905 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01004906 HInstruction* array_instr = instruction->GetArray();
4907 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01004908
Roland Levillain4d027112015-07-01 15:41:14 +01004909 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004910 case Primitive::kPrimBoolean:
4911 case Primitive::kPrimByte:
4912 case Primitive::kPrimShort:
4913 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004914 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004915 Register length;
4916 if (maybe_compressed_char_at) {
4917 length = locations->GetTemp(0).AsRegister<Register>();
4918 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4919 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4920 codegen_->MaybeRecordImplicitNullCheck(instruction);
4921 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004922 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004923 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07004924 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07004925 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004926 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4927 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4928 "Expecting 0=compressed, 1=uncompressed");
4929 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004930 __ LoadFromOffset(kLoadUnsignedByte,
4931 out_loc.AsRegister<Register>(),
4932 obj,
4933 data_offset + const_index);
4934 __ b(&done);
4935 __ Bind(&uncompressed_load);
4936 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4937 out_loc.AsRegister<Register>(),
4938 obj,
4939 data_offset + (const_index << 1));
4940 __ Bind(&done);
4941 } else {
4942 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01004943
jessicahandojo05765752016-09-09 19:01:32 -07004944 LoadOperandType load_type = GetLoadOperandType(type);
4945 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4946 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004947 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004948 Register temp = IP;
4949
4950 if (has_intermediate_address) {
4951 // We do not need to compute the intermediate address from the array: the
4952 // input instruction has done it already. See the comment in
4953 // `TryExtractArrayAccessAddress()`.
4954 if (kIsDebugBuild) {
4955 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4956 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4957 }
4958 temp = obj;
4959 } else {
4960 __ add(temp, obj, ShifterOperand(data_offset));
4961 }
jessicahandojo05765752016-09-09 19:01:32 -07004962 if (maybe_compressed_char_at) {
4963 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004964 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4965 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4966 "Expecting 0=compressed, 1=uncompressed");
4967 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004968 __ ldrb(out_loc.AsRegister<Register>(),
4969 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
4970 __ b(&done);
4971 __ Bind(&uncompressed_load);
4972 __ ldrh(out_loc.AsRegister<Register>(),
4973 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
4974 __ Bind(&done);
4975 } else {
4976 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4977 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004978 }
4979 break;
4980 }
4981
Roland Levillainc9285912015-12-18 10:38:42 +00004982 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00004983 // The read barrier instrumentation of object ArrayGet
4984 // instructions does not support the HIntermediateAddress
4985 // instruction.
4986 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4987
Roland Levillainc9285912015-12-18 10:38:42 +00004988 static_assert(
4989 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4990 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004991 // /* HeapReference<Object> */ out =
4992 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4993 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4994 Location temp = locations->GetTemp(0);
4995 // Note that a potential implicit null check is handled in this
4996 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4997 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4998 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4999 } else {
5000 Register out = out_loc.AsRegister<Register>();
5001 if (index.IsConstant()) {
5002 size_t offset =
5003 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5004 __ LoadFromOffset(kLoadWord, out, obj, offset);
5005 codegen_->MaybeRecordImplicitNullCheck(instruction);
5006 // If read barriers are enabled, emit read barriers other than
5007 // Baker's using a slow path (and also unpoison the loaded
5008 // reference, if heap poisoning is enabled).
5009 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5010 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005011 Register temp = IP;
5012
5013 if (has_intermediate_address) {
5014 // We do not need to compute the intermediate address from the array: the
5015 // input instruction has done it already. See the comment in
5016 // `TryExtractArrayAccessAddress()`.
5017 if (kIsDebugBuild) {
5018 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5019 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5020 }
5021 temp = obj;
5022 } else {
5023 __ add(temp, obj, ShifterOperand(data_offset));
5024 }
5025 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005026
Roland Levillainc9285912015-12-18 10:38:42 +00005027 codegen_->MaybeRecordImplicitNullCheck(instruction);
5028 // If read barriers are enabled, emit read barriers other than
5029 // Baker's using a slow path (and also unpoison the loaded
5030 // reference, if heap poisoning is enabled).
5031 codegen_->MaybeGenerateReadBarrierSlow(
5032 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5033 }
5034 }
5035 break;
5036 }
5037
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005038 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005039 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005040 size_t offset =
5041 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005042 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005043 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005044 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005045 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005046 }
5047 break;
5048 }
5049
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005050 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005051 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005052 if (index.IsConstant()) {
5053 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005054 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005055 } else {
5056 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005057 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005058 }
5059 break;
5060 }
5061
5062 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005063 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005064 if (index.IsConstant()) {
5065 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005066 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005067 } else {
5068 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005069 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005070 }
5071 break;
5072 }
5073
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005075 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005076 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005077 }
Roland Levillain4d027112015-07-01 15:41:14 +01005078
5079 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005080 // Potential implicit null checks, in the case of reference
5081 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005082 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005083 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005084 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005085}
5086
5087void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005088 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005089
5090 bool needs_write_barrier =
5091 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005092 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005093
Nicolas Geoffray39468442014-09-02 15:17:15 +01005094 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005095 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005096 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005097 LocationSummary::kCallOnSlowPath :
5098 LocationSummary::kNoCall);
5099
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005100 locations->SetInAt(0, Location::RequiresRegister());
5101 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5102 if (Primitive::IsFloatingPointType(value_type)) {
5103 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005105 locations->SetInAt(2, Location::RequiresRegister());
5106 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005107 if (needs_write_barrier) {
5108 // Temporary registers for the write barrier.
5109 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005110 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005111 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112}
5113
5114void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5115 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005116 Location array_loc = locations->InAt(0);
5117 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005118 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005119 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005120 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005121 bool needs_write_barrier =
5122 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005123 uint32_t data_offset =
5124 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5125 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005126 HInstruction* array_instr = instruction->GetArray();
5127 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005128
5129 switch (value_type) {
5130 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005131 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005133 case Primitive::kPrimChar:
5134 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005135 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005136 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5137 uint32_t full_offset =
5138 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5139 StoreOperandType store_type = GetStoreOperandType(value_type);
5140 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005141 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005142 Register temp = IP;
5143
5144 if (has_intermediate_address) {
5145 // We do not need to compute the intermediate address from the array: the
5146 // input instruction has done it already. See the comment in
5147 // `TryExtractArrayAccessAddress()`.
5148 if (kIsDebugBuild) {
5149 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5150 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5151 }
5152 temp = array;
5153 } else {
5154 __ add(temp, array, ShifterOperand(data_offset));
5155 }
Artem Serov6c916792016-07-11 14:02:34 +01005156 codegen_->StoreToShiftedRegOffset(value_type,
5157 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005158 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005159 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160 }
5161 break;
5162 }
5163
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005164 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005165 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005166 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5167 // See the comment in instruction_simplifier_shared.cc.
5168 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005169
5170 if (instruction->InputAt(2)->IsNullConstant()) {
5171 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005172 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005173 size_t offset =
5174 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005175 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005176 } else {
5177 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005178 __ add(IP, array, ShifterOperand(data_offset));
5179 codegen_->StoreToShiftedRegOffset(value_type,
5180 value_loc,
5181 IP,
5182 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005183 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005184 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005185 DCHECK(!needs_write_barrier);
5186 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005187 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005188 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005189
5190 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005191 Location temp1_loc = locations->GetTemp(0);
5192 Register temp1 = temp1_loc.AsRegister<Register>();
5193 Location temp2_loc = locations->GetTemp(1);
5194 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005195 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5196 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5197 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5198 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005199 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005200
Roland Levillain3b359c72015-11-17 19:35:12 +00005201 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005202 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5203 codegen_->AddSlowPath(slow_path);
5204 if (instruction->GetValueCanBeNull()) {
5205 Label non_zero;
5206 __ CompareAndBranchIfNonZero(value, &non_zero);
5207 if (index.IsConstant()) {
5208 size_t offset =
5209 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5210 __ StoreToOffset(kStoreWord, value, array, offset);
5211 } else {
5212 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005213 __ add(IP, array, ShifterOperand(data_offset));
5214 codegen_->StoreToShiftedRegOffset(value_type,
5215 value_loc,
5216 IP,
5217 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005218 }
5219 codegen_->MaybeRecordImplicitNullCheck(instruction);
5220 __ b(&done);
5221 __ Bind(&non_zero);
5222 }
5223
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005224 // Note that when read barriers are enabled, the type checks
5225 // are performed without read barriers. This is fine, even in
5226 // the case where a class object is in the from-space after
5227 // the flip, as a comparison involving such a type would not
5228 // produce a false positive; it may of course produce a false
5229 // negative, in which case we would take the ArraySet slow
5230 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005231
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005232 // /* HeapReference<Class> */ temp1 = array->klass_
5233 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5234 codegen_->MaybeRecordImplicitNullCheck(instruction);
5235 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005236
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005237 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5238 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5239 // /* HeapReference<Class> */ temp2 = value->klass_
5240 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5241 // If heap poisoning is enabled, no need to unpoison `temp1`
5242 // nor `temp2`, as we are comparing two poisoned references.
5243 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005244
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005245 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5246 Label do_put;
5247 __ b(&do_put, EQ);
5248 // If heap poisoning is enabled, the `temp1` reference has
5249 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005250 __ MaybeUnpoisonHeapReference(temp1);
5251
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005252 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5253 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5254 // If heap poisoning is enabled, no need to unpoison
5255 // `temp1`, as we are comparing against null below.
5256 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5257 __ Bind(&do_put);
5258 } else {
5259 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005260 }
5261 }
5262
Artem Serov6c916792016-07-11 14:02:34 +01005263 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005264 if (kPoisonHeapReferences) {
5265 // Note that in the case where `value` is a null reference,
5266 // we do not enter this block, as a null reference does not
5267 // need poisoning.
5268 DCHECK_EQ(value_type, Primitive::kPrimNot);
5269 __ Mov(temp1, value);
5270 __ PoisonHeapReference(temp1);
5271 source = temp1;
5272 }
5273
5274 if (index.IsConstant()) {
5275 size_t offset =
5276 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5277 __ StoreToOffset(kStoreWord, source, array, offset);
5278 } else {
5279 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005280
5281 __ add(IP, array, ShifterOperand(data_offset));
5282 codegen_->StoreToShiftedRegOffset(value_type,
5283 Location::RegisterLocation(source),
5284 IP,
5285 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005286 }
5287
Roland Levillain3b359c72015-11-17 19:35:12 +00005288 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005289 codegen_->MaybeRecordImplicitNullCheck(instruction);
5290 }
5291
5292 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5293
5294 if (done.IsLinked()) {
5295 __ Bind(&done);
5296 }
5297
5298 if (slow_path != nullptr) {
5299 __ Bind(slow_path->GetExitLabel());
5300 }
5301
5302 break;
5303 }
5304
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005306 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005307 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005308 size_t offset =
5309 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005310 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005311 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005312 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005313 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005314 }
5315 break;
5316 }
5317
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005318 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005319 Location value = locations->InAt(2);
5320 DCHECK(value.IsFpuRegister());
5321 if (index.IsConstant()) {
5322 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005323 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005324 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005325 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005326 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5327 }
5328 break;
5329 }
5330
5331 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005332 Location value = locations->InAt(2);
5333 DCHECK(value.IsFpuRegisterPair());
5334 if (index.IsConstant()) {
5335 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005336 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005337 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005338 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005339 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5340 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005341
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005342 break;
5343 }
5344
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005345 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005346 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005347 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005348 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005349
Roland Levillain80e67092016-01-08 16:04:55 +00005350 // Objects are handled in the switch.
5351 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005352 codegen_->MaybeRecordImplicitNullCheck(instruction);
5353 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005354}
5355
5356void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005357 LocationSummary* locations =
5358 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005359 locations->SetInAt(0, Location::RequiresRegister());
5360 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361}
5362
5363void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5364 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005365 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005366 Register obj = locations->InAt(0).AsRegister<Register>();
5367 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005369 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005370 // Mask out compression flag from String's array length.
5371 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005372 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07005373 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374}
5375
Artem Serov328429f2016-07-06 16:23:04 +01005376void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005377 LocationSummary* locations =
5378 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5379
5380 locations->SetInAt(0, Location::RequiresRegister());
5381 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5383}
5384
5385void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5386 LocationSummary* locations = instruction->GetLocations();
5387 Location out = locations->Out();
5388 Location first = locations->InAt(0);
5389 Location second = locations->InAt(1);
5390
Artem Serov328429f2016-07-06 16:23:04 +01005391 if (second.IsRegister()) {
5392 __ add(out.AsRegister<Register>(),
5393 first.AsRegister<Register>(),
5394 ShifterOperand(second.AsRegister<Register>()));
5395 } else {
5396 __ AddConstant(out.AsRegister<Register>(),
5397 first.AsRegister<Register>(),
5398 second.GetConstant()->AsIntConstant()->GetValue());
5399 }
5400}
5401
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005403 RegisterSet caller_saves = RegisterSet::Empty();
5404 InvokeRuntimeCallingConvention calling_convention;
5405 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5406 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5407 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005408 locations->SetInAt(0, Location::RequiresRegister());
5409 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005410}
5411
5412void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5413 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005414 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005415 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005416 codegen_->AddSlowPath(slow_path);
5417
Roland Levillain271ab9c2014-11-27 15:23:57 +00005418 Register index = locations->InAt(0).AsRegister<Register>();
5419 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005420
5421 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005422 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005423}
5424
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005425void CodeGeneratorARM::MarkGCCard(Register temp,
5426 Register card,
5427 Register object,
5428 Register value,
5429 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005430 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005431 if (can_be_null) {
5432 __ CompareAndBranchIfZero(value, &is_null);
5433 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005434 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5436 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005437 if (can_be_null) {
5438 __ Bind(&is_null);
5439 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005440}
5441
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005442void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005443 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005444}
5445
5446void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005447 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5448}
5449
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005450void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005451 LocationSummary* locations =
5452 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005453 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005454}
5455
5456void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005457 HBasicBlock* block = instruction->GetBlock();
5458 if (block->GetLoopInformation() != nullptr) {
5459 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5460 // The back edge will generate the suspend check.
5461 return;
5462 }
5463 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5464 // The goto will generate the suspend check.
5465 return;
5466 }
5467 GenerateSuspendCheck(instruction, nullptr);
5468}
5469
5470void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5471 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005472 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005473 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5474 if (slow_path == nullptr) {
5475 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5476 instruction->SetSlowPath(slow_path);
5477 codegen_->AddSlowPath(slow_path);
5478 if (successor != nullptr) {
5479 DCHECK(successor->IsLoopHeader());
5480 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5481 }
5482 } else {
5483 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5484 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005485
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005486 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005487 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005488 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005489 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005490 __ Bind(slow_path->GetReturnLabel());
5491 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005492 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005493 __ b(slow_path->GetEntryLabel());
5494 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005495}
5496
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005497ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5498 return codegen_->GetAssembler();
5499}
5500
5501void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005502 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005503 Location source = move->GetSource();
5504 Location destination = move->GetDestination();
5505
5506 if (source.IsRegister()) {
5507 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005508 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005509 } else if (destination.IsFpuRegister()) {
5510 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005511 } else {
5512 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005513 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005514 SP, destination.GetStackIndex());
5515 }
5516 } else if (source.IsStackSlot()) {
5517 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005518 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005519 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005520 } else if (destination.IsFpuRegister()) {
5521 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005522 } else {
5523 DCHECK(destination.IsStackSlot());
5524 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5525 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5526 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005527 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005528 if (destination.IsRegister()) {
5529 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5530 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005531 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005532 } else {
5533 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005534 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5535 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005536 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005537 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005538 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5539 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005540 } else if (destination.IsRegisterPair()) {
5541 DCHECK(ExpectedPairLayout(destination));
5542 __ LoadFromOffset(
5543 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5544 } else {
5545 DCHECK(destination.IsFpuRegisterPair()) << destination;
5546 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5547 SP,
5548 source.GetStackIndex());
5549 }
5550 } else if (source.IsRegisterPair()) {
5551 if (destination.IsRegisterPair()) {
5552 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5553 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005554 } else if (destination.IsFpuRegisterPair()) {
5555 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5556 source.AsRegisterPairLow<Register>(),
5557 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005558 } else {
5559 DCHECK(destination.IsDoubleStackSlot()) << destination;
5560 DCHECK(ExpectedPairLayout(source));
5561 __ StoreToOffset(
5562 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5563 }
5564 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005565 if (destination.IsRegisterPair()) {
5566 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5567 destination.AsRegisterPairHigh<Register>(),
5568 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5569 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005570 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5571 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5572 } else {
5573 DCHECK(destination.IsDoubleStackSlot()) << destination;
5574 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5575 SP,
5576 destination.GetStackIndex());
5577 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005578 } else {
5579 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005580 HConstant* constant = source.GetConstant();
5581 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5582 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005583 if (destination.IsRegister()) {
5584 __ LoadImmediate(destination.AsRegister<Register>(), value);
5585 } else {
5586 DCHECK(destination.IsStackSlot());
5587 __ LoadImmediate(IP, value);
5588 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5589 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005590 } else if (constant->IsLongConstant()) {
5591 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005592 if (destination.IsRegisterPair()) {
5593 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5594 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005595 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005596 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005597 __ LoadImmediate(IP, Low32Bits(value));
5598 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5599 __ LoadImmediate(IP, High32Bits(value));
5600 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5601 }
5602 } else if (constant->IsDoubleConstant()) {
5603 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005604 if (destination.IsFpuRegisterPair()) {
5605 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005606 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005607 DCHECK(destination.IsDoubleStackSlot()) << destination;
5608 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005609 __ LoadImmediate(IP, Low32Bits(int_value));
5610 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5611 __ LoadImmediate(IP, High32Bits(int_value));
5612 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5613 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005614 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005615 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005616 float value = constant->AsFloatConstant()->GetValue();
5617 if (destination.IsFpuRegister()) {
5618 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5619 } else {
5620 DCHECK(destination.IsStackSlot());
5621 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5622 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5623 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005624 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005625 }
5626}
5627
5628void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5629 __ Mov(IP, reg);
5630 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5631 __ StoreToOffset(kStoreWord, IP, SP, mem);
5632}
5633
5634void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5635 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5636 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5637 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5638 SP, mem1 + stack_offset);
5639 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5640 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5641 SP, mem2 + stack_offset);
5642 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5643}
5644
5645void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005646 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005647 Location source = move->GetSource();
5648 Location destination = move->GetDestination();
5649
5650 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005651 DCHECK_NE(source.AsRegister<Register>(), IP);
5652 DCHECK_NE(destination.AsRegister<Register>(), IP);
5653 __ Mov(IP, source.AsRegister<Register>());
5654 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5655 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005656 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005657 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005658 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005659 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005660 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5661 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005662 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005663 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005664 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005665 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005666 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005667 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005668 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005669 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005670 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5671 destination.AsRegisterPairHigh<Register>(),
5672 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005673 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005674 Register low_reg = source.IsRegisterPair()
5675 ? source.AsRegisterPairLow<Register>()
5676 : destination.AsRegisterPairLow<Register>();
5677 int mem = source.IsRegisterPair()
5678 ? destination.GetStackIndex()
5679 : source.GetStackIndex();
5680 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005681 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005682 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005683 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005684 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005685 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5686 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005687 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005688 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005689 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005690 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5691 DRegister reg = source.IsFpuRegisterPair()
5692 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5693 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5694 int mem = source.IsFpuRegisterPair()
5695 ? destination.GetStackIndex()
5696 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005697 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005698 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005699 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005700 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5701 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5702 : destination.AsFpuRegister<SRegister>();
5703 int mem = source.IsFpuRegister()
5704 ? destination.GetStackIndex()
5705 : source.GetStackIndex();
5706
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005707 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005708 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005709 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005710 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005711 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5712 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005713 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005714 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005715 }
5716}
5717
5718void ParallelMoveResolverARM::SpillScratch(int reg) {
5719 __ Push(static_cast<Register>(reg));
5720}
5721
5722void ParallelMoveResolverARM::RestoreScratch(int reg) {
5723 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005724}
5725
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005726HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5727 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005728 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005729 case HLoadClass::LoadKind::kInvalid:
5730 LOG(FATAL) << "UNREACHABLE";
5731 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005732 case HLoadClass::LoadKind::kReferrersClass:
5733 break;
5734 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5735 DCHECK(!GetCompilerOptions().GetCompilePic());
5736 break;
5737 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5738 DCHECK(GetCompilerOptions().GetCompilePic());
5739 break;
5740 case HLoadClass::LoadKind::kBootImageAddress:
5741 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005742 case HLoadClass::LoadKind::kBssEntry:
5743 DCHECK(!Runtime::Current()->UseJitCompilation());
5744 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005745 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005746 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005747 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005748 case HLoadClass::LoadKind::kDexCacheViaMethod:
5749 break;
5750 }
5751 return desired_class_load_kind;
5752}
5753
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005754void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005755 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5756 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005757 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005758 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005759 cls,
5760 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005761 Location::RegisterLocation(R0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005762 return;
5763 }
Vladimir Marko41559982017-01-06 14:04:23 +00005764 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005765
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005766 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5767 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005768 ? LocationSummary::kCallOnSlowPath
5769 : LocationSummary::kNoCall;
5770 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005771 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005772 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005773 }
5774
Vladimir Marko41559982017-01-06 14:04:23 +00005775 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005776 locations->SetInAt(0, Location::RequiresRegister());
5777 }
5778 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005779}
5780
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005781// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5782// move.
5783void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005784 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5785 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5786 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005787 return;
5788 }
Vladimir Marko41559982017-01-06 14:04:23 +00005789 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005790
Vladimir Marko41559982017-01-06 14:04:23 +00005791 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005792 Location out_loc = locations->Out();
5793 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005794
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005795 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5796 ? kWithoutReadBarrier
5797 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005798 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005799 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005800 case HLoadClass::LoadKind::kReferrersClass: {
5801 DCHECK(!cls->CanCallRuntime());
5802 DCHECK(!cls->MustGenerateClinitCheck());
5803 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5804 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005805 GenerateGcRootFieldLoad(cls,
5806 out_loc,
5807 current_method,
5808 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005809 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005810 break;
5811 }
5812 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
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 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5816 cls->GetTypeIndex()));
5817 break;
5818 }
5819 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005820 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005821 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005822 CodeGeneratorARM::PcRelativePatchInfo* labels =
5823 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5824 __ BindTrackedLabel(&labels->movw_label);
5825 __ movw(out, /* placeholder */ 0u);
5826 __ BindTrackedLabel(&labels->movt_label);
5827 __ movt(out, /* placeholder */ 0u);
5828 __ BindTrackedLabel(&labels->add_pc_label);
5829 __ add(out, out, ShifterOperand(PC));
5830 break;
5831 }
5832 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005833 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005834 uint32_t address = dchecked_integral_cast<uint32_t>(
5835 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5836 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005837 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5838 break;
5839 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005840 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005841 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005842 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005843 __ BindTrackedLabel(&labels->movw_label);
5844 __ movw(out, /* placeholder */ 0u);
5845 __ BindTrackedLabel(&labels->movt_label);
5846 __ movt(out, /* placeholder */ 0u);
5847 __ BindTrackedLabel(&labels->add_pc_label);
5848 __ add(out, out, ShifterOperand(PC));
5849 GenerateGcRootFieldLoad(cls, out_loc, out, 0, kCompilerReadBarrierOption);
5850 generate_null_check = true;
5851 break;
5852 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005853 case HLoadClass::LoadKind::kJitTableAddress: {
5854 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5855 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005856 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005857 // /* GcRoot<mirror::Class> */ out = *out
5858 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005859 break;
5860 }
Vladimir Marko41559982017-01-06 14:04:23 +00005861 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005862 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005863 LOG(FATAL) << "UNREACHABLE";
5864 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005865 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005866
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005867 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5868 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005869 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005870 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5871 codegen_->AddSlowPath(slow_path);
5872 if (generate_null_check) {
5873 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5874 }
5875 if (cls->MustGenerateClinitCheck()) {
5876 GenerateClassInitializationCheck(slow_path, out);
5877 } else {
5878 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005879 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005880 }
5881}
5882
5883void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5884 LocationSummary* locations =
5885 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5886 locations->SetInAt(0, Location::RequiresRegister());
5887 if (check->HasUses()) {
5888 locations->SetOut(Location::SameAsFirstInput());
5889 }
5890}
5891
5892void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005893 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005894 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005895 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005896 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005897 GenerateClassInitializationCheck(slow_path,
5898 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005899}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005900
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005901void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005902 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005903 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5904 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5905 __ b(slow_path->GetEntryLabel(), LT);
5906 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5907 // properly. Therefore, we do a memory fence.
5908 __ dmb(ISH);
5909 __ Bind(slow_path->GetExitLabel());
5910}
5911
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005912HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5913 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005914 switch (desired_string_load_kind) {
5915 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5916 DCHECK(!GetCompilerOptions().GetCompilePic());
5917 break;
5918 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5919 DCHECK(GetCompilerOptions().GetCompilePic());
5920 break;
5921 case HLoadString::LoadKind::kBootImageAddress:
5922 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005923 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005924 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005925 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005926 case HLoadString::LoadKind::kJitTableAddress:
5927 DCHECK(Runtime::Current()->UseJitCompilation());
5928 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005929 case HLoadString::LoadKind::kDexCacheViaMethod:
5930 break;
5931 }
5932 return desired_string_load_kind;
5933}
5934
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005935void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005936 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005937 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005938 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005939 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005940 locations->SetOut(Location::RegisterLocation(R0));
5941 } else {
5942 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005943 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5944 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5945 // Rely on the pResolveString and/or marking to save everything, including temps.
5946 // Note that IP may theoretically be clobbered by saving/restoring the live register
5947 // (only one thanks to the custom calling convention), so we request a different temp.
5948 locations->AddTemp(Location::RequiresRegister());
5949 RegisterSet caller_saves = RegisterSet::Empty();
5950 InvokeRuntimeCallingConvention calling_convention;
5951 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5952 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5953 // that the the kPrimNot result register is the same as the first argument register.
5954 locations->SetCustomSlowPathCallerSaves(caller_saves);
5955 } else {
5956 // For non-Baker read barrier we have a temp-clobbering call.
5957 }
5958 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005959 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005960}
5961
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005962// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5963// move.
5964void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005965 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005966 Location out_loc = locations->Out();
5967 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005968 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005969
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005970 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005971 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005972 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005973 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5974 load->GetStringIndex()));
5975 return; // No dex cache slow path.
5976 }
5977 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005978 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005979 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005980 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005981 __ BindTrackedLabel(&labels->movw_label);
5982 __ movw(out, /* placeholder */ 0u);
5983 __ BindTrackedLabel(&labels->movt_label);
5984 __ movt(out, /* placeholder */ 0u);
5985 __ BindTrackedLabel(&labels->add_pc_label);
5986 __ add(out, out, ShifterOperand(PC));
5987 return; // No dex cache slow path.
5988 }
5989 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005990 uint32_t address = dchecked_integral_cast<uint32_t>(
5991 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5992 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005993 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5994 return; // No dex cache slow path.
5995 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005996 case HLoadString::LoadKind::kBssEntry: {
5997 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005998 Register temp = locations->GetTemp(0).AsRegister<Register>();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005999 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006000 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006001 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006002 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006003 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006004 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006005 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006006 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006007 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006008 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
6009 codegen_->AddSlowPath(slow_path);
6010 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6011 __ Bind(slow_path->GetExitLabel());
6012 return;
6013 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006014 case HLoadString::LoadKind::kJitTableAddress: {
6015 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006016 load->GetStringIndex(),
6017 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006018 // /* GcRoot<mirror::String> */ out = *out
6019 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6020 return;
6021 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006022 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006023 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006024 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006025
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006026 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
6027 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6028 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006029 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006030 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006031 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6032 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006033}
6034
David Brazdilcb1c0552015-08-04 16:22:25 +01006035static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006036 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006037}
6038
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006039void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6040 LocationSummary* locations =
6041 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6042 locations->SetOut(Location::RequiresRegister());
6043}
6044
6045void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006046 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006047 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6048}
6049
6050void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6051 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6052}
6053
6054void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006055 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006056 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006057}
6058
6059void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6060 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006061 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006062 InvokeRuntimeCallingConvention calling_convention;
6063 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6064}
6065
6066void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006067 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006068 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006069}
6070
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006071// Temp is used for read barrier.
6072static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6073 if (kEmitCompilerReadBarrier &&
6074 (kUseBakerReadBarrier ||
6075 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6076 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6077 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6078 return 1;
6079 }
6080 return 0;
6081}
6082
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006083// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006084// interface pointer, one for loading the current interface.
6085// The other checks have one temp for loading the object's class.
6086static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6087 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6088 return 3;
6089 }
6090 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006091}
6092
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006093void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006094 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006095 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006096 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006097 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006098 case TypeCheckKind::kExactCheck:
6099 case TypeCheckKind::kAbstractClassCheck:
6100 case TypeCheckKind::kClassHierarchyCheck:
6101 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006102 call_kind =
6103 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006104 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006105 break;
6106 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006107 case TypeCheckKind::kUnresolvedCheck:
6108 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006109 call_kind = LocationSummary::kCallOnSlowPath;
6110 break;
6111 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006112
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006113 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006114 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006115 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006116 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006117 locations->SetInAt(0, Location::RequiresRegister());
6118 locations->SetInAt(1, Location::RequiresRegister());
6119 // The "out" register is used as a temporary, so it overlaps with the inputs.
6120 // Note that TypeCheckSlowPathARM uses this register too.
6121 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006122 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006123}
6124
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006125void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006126 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006127 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006128 Location obj_loc = locations->InAt(0);
6129 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006130 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006131 Location out_loc = locations->Out();
6132 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006133 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6134 DCHECK_LE(num_temps, 1u);
6135 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006136 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006137 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6138 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6139 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006140 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006141 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006142
6143 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006144 // avoid null check if we know obj is not null.
6145 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006146 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006147 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006148
Roland Levillainc9285912015-12-18 10:38:42 +00006149 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006150 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006151 // /* HeapReference<Class> */ out = obj->klass_
6152 GenerateReferenceLoadTwoRegisters(instruction,
6153 out_loc,
6154 obj_loc,
6155 class_offset,
6156 maybe_temp_loc,
6157 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006158 __ cmp(out, ShifterOperand(cls));
6159 // Classes must be equal for the instanceof to succeed.
6160 __ b(&zero, NE);
6161 __ LoadImmediate(out, 1);
6162 __ b(&done);
6163 break;
6164 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006165
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006167 // /* HeapReference<Class> */ out = obj->klass_
6168 GenerateReferenceLoadTwoRegisters(instruction,
6169 out_loc,
6170 obj_loc,
6171 class_offset,
6172 maybe_temp_loc,
6173 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006174 // If the class is abstract, we eagerly fetch the super class of the
6175 // object to avoid doing a comparison we know will fail.
6176 Label loop;
6177 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006178 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006179 GenerateReferenceLoadOneRegister(instruction,
6180 out_loc,
6181 super_offset,
6182 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006183 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006184 // If `out` is null, we use it for the result, and jump to `done`.
6185 __ CompareAndBranchIfZero(out, &done);
6186 __ cmp(out, ShifterOperand(cls));
6187 __ b(&loop, NE);
6188 __ LoadImmediate(out, 1);
6189 if (zero.IsLinked()) {
6190 __ b(&done);
6191 }
6192 break;
6193 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006194
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006195 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006196 // /* HeapReference<Class> */ out = obj->klass_
6197 GenerateReferenceLoadTwoRegisters(instruction,
6198 out_loc,
6199 obj_loc,
6200 class_offset,
6201 maybe_temp_loc,
6202 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006203 // Walk over the class hierarchy to find a match.
6204 Label loop, success;
6205 __ Bind(&loop);
6206 __ cmp(out, ShifterOperand(cls));
6207 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006208 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006209 GenerateReferenceLoadOneRegister(instruction,
6210 out_loc,
6211 super_offset,
6212 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006213 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006214 __ CompareAndBranchIfNonZero(out, &loop);
6215 // If `out` is null, we use it for the result, and jump to `done`.
6216 __ b(&done);
6217 __ Bind(&success);
6218 __ LoadImmediate(out, 1);
6219 if (zero.IsLinked()) {
6220 __ b(&done);
6221 }
6222 break;
6223 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006224
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006225 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006226 // /* HeapReference<Class> */ out = obj->klass_
6227 GenerateReferenceLoadTwoRegisters(instruction,
6228 out_loc,
6229 obj_loc,
6230 class_offset,
6231 maybe_temp_loc,
6232 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006233 // Do an exact check.
6234 Label exact_check;
6235 __ cmp(out, ShifterOperand(cls));
6236 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006237 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006238 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006239 GenerateReferenceLoadOneRegister(instruction,
6240 out_loc,
6241 component_offset,
6242 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006243 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006244 // If `out` is null, we use it for the result, and jump to `done`.
6245 __ CompareAndBranchIfZero(out, &done);
6246 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6247 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6248 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006249 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006250 __ LoadImmediate(out, 1);
6251 __ b(&done);
6252 break;
6253 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006254
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006255 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006256 // No read barrier since the slow path will retry upon failure.
6257 // /* HeapReference<Class> */ out = obj->klass_
6258 GenerateReferenceLoadTwoRegisters(instruction,
6259 out_loc,
6260 obj_loc,
6261 class_offset,
6262 maybe_temp_loc,
6263 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006264 __ cmp(out, ShifterOperand(cls));
6265 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006266 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6267 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006268 codegen_->AddSlowPath(slow_path);
6269 __ b(slow_path->GetEntryLabel(), NE);
6270 __ LoadImmediate(out, 1);
6271 if (zero.IsLinked()) {
6272 __ b(&done);
6273 }
6274 break;
6275 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006276
Calin Juravle98893e12015-10-02 21:05:03 +01006277 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006278 case TypeCheckKind::kInterfaceCheck: {
6279 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006280 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006281 // cases.
6282 //
6283 // We cannot directly call the InstanceofNonTrivial runtime
6284 // entry point without resorting to a type checking slow path
6285 // here (i.e. by calling InvokeRuntime directly), as it would
6286 // require to assign fixed registers for the inputs of this
6287 // HInstanceOf instruction (following the runtime calling
6288 // convention), which might be cluttered by the potential first
6289 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006290 //
6291 // TODO: Introduce a new runtime entry point taking the object
6292 // to test (instead of its class) as argument, and let it deal
6293 // with the read barrier issues. This will let us refactor this
6294 // case of the `switch` code as it was previously (with a direct
6295 // call to the runtime not using a type checking slow path).
6296 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006297 DCHECK(locations->OnlyCallsOnSlowPath());
6298 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6299 /* is_fatal */ false);
6300 codegen_->AddSlowPath(slow_path);
6301 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006302 if (zero.IsLinked()) {
6303 __ b(&done);
6304 }
6305 break;
6306 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006307 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006308
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006309 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006310 __ Bind(&zero);
6311 __ LoadImmediate(out, 0);
6312 }
6313
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006314 if (done.IsLinked()) {
6315 __ Bind(&done);
6316 }
6317
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006318 if (slow_path != nullptr) {
6319 __ Bind(slow_path->GetExitLabel());
6320 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006321}
6322
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006323void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006324 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6325 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6326
Roland Levillain3b359c72015-11-17 19:35:12 +00006327 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6328 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006329 case TypeCheckKind::kExactCheck:
6330 case TypeCheckKind::kAbstractClassCheck:
6331 case TypeCheckKind::kClassHierarchyCheck:
6332 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006333 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6334 LocationSummary::kCallOnSlowPath :
6335 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006336 break;
6337 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006338 case TypeCheckKind::kUnresolvedCheck:
6339 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006340 call_kind = LocationSummary::kCallOnSlowPath;
6341 break;
6342 }
6343
Roland Levillain3b359c72015-11-17 19:35:12 +00006344 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6345 locations->SetInAt(0, Location::RequiresRegister());
6346 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006347 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006348}
6349
6350void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006351 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006352 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006353 Location obj_loc = locations->InAt(0);
6354 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006355 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006356 Location temp_loc = locations->GetTemp(0);
6357 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006358 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6359 DCHECK_LE(num_temps, 3u);
6360 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6361 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6362 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6363 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6364 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6365 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6366 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6367 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6368 const uint32_t object_array_data_offset =
6369 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006370
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006371 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6372 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6373 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006374 bool is_type_check_slow_path_fatal = false;
6375 if (!kEmitCompilerReadBarrier) {
6376 is_type_check_slow_path_fatal =
6377 (type_check_kind == TypeCheckKind::kExactCheck ||
6378 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6379 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6380 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6381 !instruction->CanThrowIntoCatchBlock();
6382 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01006383 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006384 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6385 is_type_check_slow_path_fatal);
6386 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006387
6388 Label done;
6389 // Avoid null check if we know obj is not null.
6390 if (instruction->MustDoNullCheck()) {
6391 __ CompareAndBranchIfZero(obj, &done);
6392 }
6393
Roland Levillain3b359c72015-11-17 19:35:12 +00006394 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006395 case TypeCheckKind::kExactCheck:
6396 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006397 // /* HeapReference<Class> */ temp = obj->klass_
6398 GenerateReferenceLoadTwoRegisters(instruction,
6399 temp_loc,
6400 obj_loc,
6401 class_offset,
6402 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006403 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006404
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006405 __ cmp(temp, ShifterOperand(cls));
6406 // Jump to slow path for throwing the exception or doing a
6407 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006408 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006409 break;
6410 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006411
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006412 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006413 // /* HeapReference<Class> */ temp = obj->klass_
6414 GenerateReferenceLoadTwoRegisters(instruction,
6415 temp_loc,
6416 obj_loc,
6417 class_offset,
6418 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006419 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006420
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006421 // If the class is abstract, we eagerly fetch the super class of the
6422 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006423 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006424 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006425 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006426 GenerateReferenceLoadOneRegister(instruction,
6427 temp_loc,
6428 super_offset,
6429 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006430 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006431
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006432 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6433 // exception.
6434 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00006435
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006436 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006437 __ cmp(temp, ShifterOperand(cls));
6438 __ b(&loop, NE);
6439 break;
6440 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006441
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006442 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006443 // /* HeapReference<Class> */ temp = obj->klass_
6444 GenerateReferenceLoadTwoRegisters(instruction,
6445 temp_loc,
6446 obj_loc,
6447 class_offset,
6448 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006449 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006450
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006451 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006452 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006453 __ Bind(&loop);
6454 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006455 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006456
Roland Levillain3b359c72015-11-17 19:35:12 +00006457 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006458 GenerateReferenceLoadOneRegister(instruction,
6459 temp_loc,
6460 super_offset,
6461 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006462 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006463
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006464 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6465 // exception.
6466 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6467 // Otherwise, jump to the beginning of the loop.
6468 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006469 break;
6470 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006471
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006472 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006473 // /* HeapReference<Class> */ temp = obj->klass_
6474 GenerateReferenceLoadTwoRegisters(instruction,
6475 temp_loc,
6476 obj_loc,
6477 class_offset,
6478 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006479 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006480
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006481 // Do an exact check.
6482 __ cmp(temp, ShifterOperand(cls));
6483 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006484
6485 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006486 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006487 GenerateReferenceLoadOneRegister(instruction,
6488 temp_loc,
6489 component_offset,
6490 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006491 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006492 // If the component type is null, jump to the slow path to throw the exception.
6493 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6494 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6495 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006496 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006497 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006498 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006499 break;
6500 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006501
Calin Juravle98893e12015-10-02 21:05:03 +01006502 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006503 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00006504 // We cannot directly call the CheckCast runtime entry point
6505 // without resorting to a type checking slow path here (i.e. by
6506 // calling InvokeRuntime directly), as it would require to
6507 // assign fixed registers for the inputs of this HInstanceOf
6508 // instruction (following the runtime calling convention), which
6509 // might be cluttered by the potential first read barrier
6510 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006511
Roland Levillain3b359c72015-11-17 19:35:12 +00006512 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006513 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006514
6515 case TypeCheckKind::kInterfaceCheck: {
6516 // Avoid read barriers to improve performance of the fast path. We can not get false
6517 // positives by doing this.
6518 // /* HeapReference<Class> */ temp = obj->klass_
6519 GenerateReferenceLoadTwoRegisters(instruction,
6520 temp_loc,
6521 obj_loc,
6522 class_offset,
6523 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006524 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006525
6526 // /* HeapReference<Class> */ temp = temp->iftable_
6527 GenerateReferenceLoadTwoRegisters(instruction,
6528 temp_loc,
6529 temp_loc,
6530 iftable_offset,
6531 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006532 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006533 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006534 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006535 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006536 Label start_loop;
6537 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006538 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
6539 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006540 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
6541 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006542 // Go to next interface.
6543 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
6544 __ sub(maybe_temp2_loc.AsRegister<Register>(),
6545 maybe_temp2_loc.AsRegister<Register>(),
6546 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006547 // Compare the classes and continue the loop if they do not match.
6548 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
6549 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006550 break;
6551 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006552 }
6553 __ Bind(&done);
6554
Roland Levillain3b359c72015-11-17 19:35:12 +00006555 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006556}
6557
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006558void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6559 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006560 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006561 InvokeRuntimeCallingConvention calling_convention;
6562 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6563}
6564
6565void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006566 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6567 instruction,
6568 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006569 if (instruction->IsEnter()) {
6570 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6571 } else {
6572 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6573 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006574}
6575
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006576void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6577void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6578void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006579
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006580void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006581 LocationSummary* locations =
6582 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6583 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6584 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006585 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006586 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006587 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006589}
6590
6591void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6592 HandleBitwiseOperation(instruction);
6593}
6594
6595void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6596 HandleBitwiseOperation(instruction);
6597}
6598
6599void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6600 HandleBitwiseOperation(instruction);
6601}
6602
Artem Serov7fc63502016-02-09 17:15:29 +00006603
6604void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6605 LocationSummary* locations =
6606 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6607 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6608 || instruction->GetResultType() == Primitive::kPrimLong);
6609
6610 locations->SetInAt(0, Location::RequiresRegister());
6611 locations->SetInAt(1, Location::RequiresRegister());
6612 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6613}
6614
6615void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6616 LocationSummary* locations = instruction->GetLocations();
6617 Location first = locations->InAt(0);
6618 Location second = locations->InAt(1);
6619 Location out = locations->Out();
6620
6621 if (instruction->GetResultType() == Primitive::kPrimInt) {
6622 Register first_reg = first.AsRegister<Register>();
6623 ShifterOperand second_reg(second.AsRegister<Register>());
6624 Register out_reg = out.AsRegister<Register>();
6625
6626 switch (instruction->GetOpKind()) {
6627 case HInstruction::kAnd:
6628 __ bic(out_reg, first_reg, second_reg);
6629 break;
6630 case HInstruction::kOr:
6631 __ orn(out_reg, first_reg, second_reg);
6632 break;
6633 // There is no EON on arm.
6634 case HInstruction::kXor:
6635 default:
6636 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6637 UNREACHABLE();
6638 }
6639 return;
6640
6641 } else {
6642 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6643 Register first_low = first.AsRegisterPairLow<Register>();
6644 Register first_high = first.AsRegisterPairHigh<Register>();
6645 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6646 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6647 Register out_low = out.AsRegisterPairLow<Register>();
6648 Register out_high = out.AsRegisterPairHigh<Register>();
6649
6650 switch (instruction->GetOpKind()) {
6651 case HInstruction::kAnd:
6652 __ bic(out_low, first_low, second_low);
6653 __ bic(out_high, first_high, second_high);
6654 break;
6655 case HInstruction::kOr:
6656 __ orn(out_low, first_low, second_low);
6657 __ orn(out_high, first_high, second_high);
6658 break;
6659 // There is no EON on arm.
6660 case HInstruction::kXor:
6661 default:
6662 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6663 UNREACHABLE();
6664 }
6665 }
6666}
6667
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006668void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6669 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6670 if (value == 0xffffffffu) {
6671 if (out != first) {
6672 __ mov(out, ShifterOperand(first));
6673 }
6674 return;
6675 }
6676 if (value == 0u) {
6677 __ mov(out, ShifterOperand(0));
6678 return;
6679 }
6680 ShifterOperand so;
6681 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6682 __ and_(out, first, so);
6683 } else {
6684 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6685 __ bic(out, first, ShifterOperand(~value));
6686 }
6687}
6688
6689void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6690 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6691 if (value == 0u) {
6692 if (out != first) {
6693 __ mov(out, ShifterOperand(first));
6694 }
6695 return;
6696 }
6697 if (value == 0xffffffffu) {
6698 __ mvn(out, ShifterOperand(0));
6699 return;
6700 }
6701 ShifterOperand so;
6702 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6703 __ orr(out, first, so);
6704 } else {
6705 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6706 __ orn(out, first, ShifterOperand(~value));
6707 }
6708}
6709
6710void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6711 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6712 if (value == 0u) {
6713 if (out != first) {
6714 __ mov(out, ShifterOperand(first));
6715 }
6716 return;
6717 }
6718 __ eor(out, first, ShifterOperand(value));
6719}
6720
Vladimir Marko59751a72016-08-05 14:37:27 +01006721void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6722 Location first,
6723 uint64_t value) {
6724 Register out_low = out.AsRegisterPairLow<Register>();
6725 Register out_high = out.AsRegisterPairHigh<Register>();
6726 Register first_low = first.AsRegisterPairLow<Register>();
6727 Register first_high = first.AsRegisterPairHigh<Register>();
6728 uint32_t value_low = Low32Bits(value);
6729 uint32_t value_high = High32Bits(value);
6730 if (value_low == 0u) {
6731 if (out_low != first_low) {
6732 __ mov(out_low, ShifterOperand(first_low));
6733 }
6734 __ AddConstant(out_high, first_high, value_high);
6735 return;
6736 }
6737 __ AddConstantSetFlags(out_low, first_low, value_low);
6738 ShifterOperand so;
6739 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6740 __ adc(out_high, first_high, so);
6741 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6742 __ sbc(out_high, first_high, so);
6743 } else {
6744 LOG(FATAL) << "Unexpected constant " << value_high;
6745 UNREACHABLE();
6746 }
6747}
6748
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006749void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6750 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006751 Location first = locations->InAt(0);
6752 Location second = locations->InAt(1);
6753 Location out = locations->Out();
6754
6755 if (second.IsConstant()) {
6756 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6757 uint32_t value_low = Low32Bits(value);
6758 if (instruction->GetResultType() == Primitive::kPrimInt) {
6759 Register first_reg = first.AsRegister<Register>();
6760 Register out_reg = out.AsRegister<Register>();
6761 if (instruction->IsAnd()) {
6762 GenerateAndConst(out_reg, first_reg, value_low);
6763 } else if (instruction->IsOr()) {
6764 GenerateOrrConst(out_reg, first_reg, value_low);
6765 } else {
6766 DCHECK(instruction->IsXor());
6767 GenerateEorConst(out_reg, first_reg, value_low);
6768 }
6769 } else {
6770 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6771 uint32_t value_high = High32Bits(value);
6772 Register first_low = first.AsRegisterPairLow<Register>();
6773 Register first_high = first.AsRegisterPairHigh<Register>();
6774 Register out_low = out.AsRegisterPairLow<Register>();
6775 Register out_high = out.AsRegisterPairHigh<Register>();
6776 if (instruction->IsAnd()) {
6777 GenerateAndConst(out_low, first_low, value_low);
6778 GenerateAndConst(out_high, first_high, value_high);
6779 } else if (instruction->IsOr()) {
6780 GenerateOrrConst(out_low, first_low, value_low);
6781 GenerateOrrConst(out_high, first_high, value_high);
6782 } else {
6783 DCHECK(instruction->IsXor());
6784 GenerateEorConst(out_low, first_low, value_low);
6785 GenerateEorConst(out_high, first_high, value_high);
6786 }
6787 }
6788 return;
6789 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006790
6791 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006792 Register first_reg = first.AsRegister<Register>();
6793 ShifterOperand second_reg(second.AsRegister<Register>());
6794 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006795 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006796 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006797 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006798 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006799 } else {
6800 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006801 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006802 }
6803 } else {
6804 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006805 Register first_low = first.AsRegisterPairLow<Register>();
6806 Register first_high = first.AsRegisterPairHigh<Register>();
6807 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6808 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6809 Register out_low = out.AsRegisterPairLow<Register>();
6810 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006811 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006812 __ and_(out_low, first_low, second_low);
6813 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006814 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006815 __ orr(out_low, first_low, second_low);
6816 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006817 } else {
6818 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006819 __ eor(out_low, first_low, second_low);
6820 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006821 }
6822 }
6823}
6824
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006825void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
6826 HInstruction* instruction,
6827 Location out,
6828 uint32_t offset,
6829 Location maybe_temp,
6830 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006831 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006832 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006833 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006834 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006835 if (kUseBakerReadBarrier) {
6836 // Load with fast path based Baker's read barrier.
6837 // /* HeapReference<Object> */ out = *(out + offset)
6838 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006839 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006840 } else {
6841 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006842 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006843 // in the following move operation, as we will need it for the
6844 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006845 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006846 // /* HeapReference<Object> */ out = *(out + offset)
6847 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006848 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006849 }
6850 } else {
6851 // Plain load with no read barrier.
6852 // /* HeapReference<Object> */ out = *(out + offset)
6853 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6854 __ MaybeUnpoisonHeapReference(out_reg);
6855 }
6856}
6857
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006858void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
6859 HInstruction* instruction,
6860 Location out,
6861 Location obj,
6862 uint32_t offset,
6863 Location maybe_temp,
6864 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006865 Register out_reg = out.AsRegister<Register>();
6866 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006867 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006868 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006869 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006870 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006871 // Load with fast path based Baker's read barrier.
6872 // /* HeapReference<Object> */ out = *(obj + offset)
6873 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006874 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006875 } else {
6876 // Load with slow path based read barrier.
6877 // /* HeapReference<Object> */ out = *(obj + offset)
6878 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6879 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6880 }
6881 } else {
6882 // Plain load with no read barrier.
6883 // /* HeapReference<Object> */ out = *(obj + offset)
6884 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6885 __ MaybeUnpoisonHeapReference(out_reg);
6886 }
6887}
6888
6889void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6890 Location root,
6891 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006892 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006893 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006894 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006895 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006896 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006897 if (kUseBakerReadBarrier) {
6898 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6899 // Baker's read barrier are used:
6900 //
6901 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006902 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6903 // if (temp != null) {
6904 // root = temp(root)
Roland Levillainc9285912015-12-18 10:38:42 +00006905 // }
6906
6907 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6908 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6909 static_assert(
6910 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6911 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6912 "have different sizes.");
6913 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6914 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6915 "have different sizes.");
6916
Vladimir Marko953437b2016-08-24 08:30:46 +00006917 // Slow path marking the GC root `root`.
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006918 Location temp = Location::RegisterLocation(LR);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006919 SlowPathCodeARM* slow_path =
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006920 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
6921 instruction,
6922 root,
6923 /*entrypoint*/ temp);
Roland Levillainc9285912015-12-18 10:38:42 +00006924 codegen_->AddSlowPath(slow_path);
6925
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006926 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6927 const int32_t entry_point_offset =
6928 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
6929 // Loading the entrypoint does not require a load acquire since it is only changed when
6930 // threads are suspended or running a checkpoint.
6931 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6932 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6933 // checking GetIsGcMarking.
6934 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00006935 __ Bind(slow_path->GetExitLabel());
6936 } else {
6937 // GC root loaded through a slow path for read barriers other
6938 // than Baker's.
6939 // /* GcRoot<mirror::Object>* */ root = obj + offset
6940 __ AddConstant(root_reg, obj, offset);
6941 // /* mirror::Object* */ root = root->Read()
6942 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6943 }
6944 } else {
6945 // Plain GC root load with no read barrier.
6946 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6947 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6948 // Note that GC roots are not affected by heap poisoning, thus we
6949 // do not have to unpoison `root_reg` here.
6950 }
6951}
6952
6953void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6954 Location ref,
6955 Register obj,
6956 uint32_t offset,
6957 Location temp,
6958 bool needs_null_check) {
6959 DCHECK(kEmitCompilerReadBarrier);
6960 DCHECK(kUseBakerReadBarrier);
6961
6962 // /* HeapReference<Object> */ ref = *(obj + offset)
6963 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006964 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006965 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006966 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006967}
6968
6969void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6970 Location ref,
6971 Register obj,
6972 uint32_t data_offset,
6973 Location index,
6974 Location temp,
6975 bool needs_null_check) {
6976 DCHECK(kEmitCompilerReadBarrier);
6977 DCHECK(kUseBakerReadBarrier);
6978
Roland Levillainbfea3352016-06-23 13:48:47 +01006979 static_assert(
6980 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6981 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006982 // /* HeapReference<Object> */ ref =
6983 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006984 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006985 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006986 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006987}
6988
6989void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6990 Location ref,
6991 Register obj,
6992 uint32_t offset,
6993 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006994 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006995 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006996 bool needs_null_check,
6997 bool always_update_field,
6998 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00006999 DCHECK(kEmitCompilerReadBarrier);
7000 DCHECK(kUseBakerReadBarrier);
7001
7002 // In slow path based read barriers, the read barrier call is
7003 // inserted after the original load. However, in fast path based
7004 // Baker's read barriers, we need to perform the load of
7005 // mirror::Object::monitor_ *before* the original reference load.
7006 // This load-load ordering is required by the read barrier.
7007 // The fast path/slow path (for Baker's algorithm) should look like:
7008 //
7009 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7010 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7011 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007012 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillainc9285912015-12-18 10:38:42 +00007013 // if (is_gray) {
7014 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7015 // }
7016 //
7017 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007018 // slightly more complex as it performs additional checks that we do
7019 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00007020
7021 Register ref_reg = ref.AsRegister<Register>();
7022 Register temp_reg = temp.AsRegister<Register>();
7023 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7024
7025 // /* int32_t */ monitor = obj->monitor_
7026 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7027 if (needs_null_check) {
7028 MaybeRecordImplicitNullCheck(instruction);
7029 }
7030 // /* LockWord */ lock_word = LockWord(monitor)
7031 static_assert(sizeof(LockWord) == sizeof(int32_t),
7032 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007033
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007034 // Introduce a dependency on the lock_word including the rb_state,
7035 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00007036 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01007037 // `obj` is unchanged by this operation, but its value now depends
7038 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007039 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00007040
7041 // The actual reference load.
7042 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007043 // Load types involving an "index": ArrayGet,
7044 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7045 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01007046 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00007047 if (index.IsConstant()) {
7048 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01007049 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00007050 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7051 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01007052 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007053 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7054 // intrinsics, which use a register pair as index ("long
7055 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01007056 Register index_reg = index.IsRegisterPair()
7057 ? index.AsRegisterPairLow<Register>()
7058 : index.AsRegister<Register>();
7059 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00007060 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
7061 }
7062 } else {
7063 // /* HeapReference<Object> */ ref = *(obj + offset)
7064 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7065 }
7066
7067 // Object* ref = ref_addr->AsMirrorPtr()
7068 __ MaybeUnpoisonHeapReference(ref_reg);
7069
Vladimir Marko953437b2016-08-24 08:30:46 +00007070 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007071 SlowPathCodeARM* slow_path;
7072 if (always_update_field) {
7073 DCHECK(temp2 != nullptr);
7074 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
7075 // of the form `obj + field_offset`, where `obj` is a register and
7076 // `field_offset` is a register pair (of which only the lower half
7077 // is used). Thus `offset` and `scale_factor` above are expected
7078 // to be null in this code path.
7079 DCHECK_EQ(offset, 0u);
7080 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7081 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
7082 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7083 } else {
7084 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
7085 }
Roland Levillainc9285912015-12-18 10:38:42 +00007086 AddSlowPath(slow_path);
7087
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007088 // if (rb_state == ReadBarrier::GrayState())
Roland Levillainc9285912015-12-18 10:38:42 +00007089 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007090 // Given the numeric representation, it's enough to check the low bit of the
7091 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7092 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007093 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7094 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007095 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7096 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00007097 __ Bind(slow_path->GetExitLabel());
7098}
7099
7100void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
7101 Location out,
7102 Location ref,
7103 Location obj,
7104 uint32_t offset,
7105 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007106 DCHECK(kEmitCompilerReadBarrier);
7107
Roland Levillainc9285912015-12-18 10:38:42 +00007108 // Insert a slow path based read barrier *after* the reference load.
7109 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007110 // If heap poisoning is enabled, the unpoisoning of the loaded
7111 // reference will be carried out by the runtime within the slow
7112 // path.
7113 //
7114 // Note that `ref` currently does not get unpoisoned (when heap
7115 // poisoning is enabled), which is alright as the `ref` argument is
7116 // not used by the artReadBarrierSlow entry point.
7117 //
7118 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007119 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00007120 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
7121 AddSlowPath(slow_path);
7122
Roland Levillain3b359c72015-11-17 19:35:12 +00007123 __ b(slow_path->GetEntryLabel());
7124 __ Bind(slow_path->GetExitLabel());
7125}
7126
Roland Levillainc9285912015-12-18 10:38:42 +00007127void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7128 Location out,
7129 Location ref,
7130 Location obj,
7131 uint32_t offset,
7132 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007133 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00007134 // Baker's read barriers shall be handled by the fast path
7135 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
7136 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007137 // If heap poisoning is enabled, unpoisoning will be taken care of
7138 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007139 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00007140 } else if (kPoisonHeapReferences) {
7141 __ UnpoisonHeapReference(out.AsRegister<Register>());
7142 }
7143}
7144
Roland Levillainc9285912015-12-18 10:38:42 +00007145void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7146 Location out,
7147 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007148 DCHECK(kEmitCompilerReadBarrier);
7149
Roland Levillainc9285912015-12-18 10:38:42 +00007150 // Insert a slow path based read barrier *after* the GC root load.
7151 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007152 // Note that GC roots are not affected by heap poisoning, so we do
7153 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007154 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007155 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
7156 AddSlowPath(slow_path);
7157
Roland Levillain3b359c72015-11-17 19:35:12 +00007158 __ b(slow_path->GetEntryLabel());
7159 __ Bind(slow_path->GetExitLabel());
7160}
7161
Vladimir Markodc151b22015-10-15 18:02:30 +01007162HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7163 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007164 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00007165 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007166}
7167
Vladimir Markob4536b72015-11-24 13:45:23 +00007168Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7169 Register temp) {
7170 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7171 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7172 if (!invoke->GetLocations()->Intrinsified()) {
7173 return location.AsRegister<Register>();
7174 }
7175 // For intrinsics we allow any location, so it may be on the stack.
7176 if (!location.IsRegister()) {
7177 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7178 return temp;
7179 }
7180 // For register locations, check if the register was saved. If so, get it from the stack.
7181 // Note: There is a chance that the register was saved but not overwritten, so we could
7182 // save one load. However, since this is just an intrinsic slow path we prefer this
7183 // simple and more robust approach rather that trying to determine if that's the case.
7184 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08007185 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00007186 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7187 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7188 return temp;
7189 }
7190 return location.AsRegister<Register>();
7191}
7192
TatWai Chongd8c052a2016-11-02 16:12:48 +08007193Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
7194 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007195 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7196 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007197 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7198 uint32_t offset =
7199 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007200 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007201 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007202 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007203 }
Vladimir Marko58155012015-08-19 12:49:41 +00007204 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007205 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007206 break;
7207 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7208 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7209 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007210 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7211 HArmDexCacheArraysBase* base =
7212 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7213 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7214 temp.AsRegister<Register>());
7215 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7216 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7217 break;
7218 }
Vladimir Marko58155012015-08-19 12:49:41 +00007219 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007220 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007221 Register method_reg;
7222 Register reg = temp.AsRegister<Register>();
7223 if (current_method.IsRegister()) {
7224 method_reg = current_method.AsRegister<Register>();
7225 } else {
7226 DCHECK(invoke->GetLocations()->Intrinsified());
7227 DCHECK(!current_method.IsValid());
7228 method_reg = reg;
7229 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7230 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007231 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7232 __ LoadFromOffset(kLoadWord,
7233 reg,
7234 method_reg,
7235 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007236 // temp = temp[index_in_cache];
7237 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7238 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007239 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7240 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007241 }
Vladimir Marko58155012015-08-19 12:49:41 +00007242 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08007243 return callee_method;
7244}
7245
7246void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
7247 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00007248
7249 switch (invoke->GetCodePtrLocation()) {
7250 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7251 __ bl(GetFrameEntryLabel());
7252 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007253 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7254 // LR = callee_method->entry_point_from_quick_compiled_code_
7255 __ LoadFromOffset(
7256 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007257 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007258 // LR()
7259 __ blx(LR);
7260 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007261 }
7262
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007263 DCHECK(!IsLeafMethod());
7264}
7265
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007266void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7267 Register temp = temp_location.AsRegister<Register>();
7268 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7269 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007270
7271 // Use the calling convention instead of the location of the receiver, as
7272 // intrinsics may have put the receiver in a different register. In the intrinsics
7273 // slow path, the arguments have been moved to the right place, so here we are
7274 // guaranteed that the receiver is the first register of the calling convention.
7275 InvokeDexCallingConvention calling_convention;
7276 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007277 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007278 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007279 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007280 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007281 // Instead of simply (possibly) unpoisoning `temp` here, we should
7282 // emit a read barrier for the previous class reference load.
7283 // However this is not required in practice, as this is an
7284 // intermediate/temporary reference and because the current
7285 // concurrent copying collector keeps the from-space memory
7286 // intact/accessible until the end of the marking phase (the
7287 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007288 __ MaybeUnpoisonHeapReference(temp);
7289 // temp = temp->GetMethodAt(method_offset);
7290 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007291 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007292 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7293 // LR = temp->GetEntryPoint();
7294 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7295 // LR();
7296 __ blx(LR);
7297}
7298
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007299CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007300 const DexFile& dex_file, dex::StringIndex string_index) {
7301 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007302}
7303
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007304CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08007305 const DexFile& dex_file, dex::TypeIndex type_index) {
7306 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007307}
7308
Vladimir Marko1998cd02017-01-13 13:02:58 +00007309CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
7310 const DexFile& dex_file, dex::TypeIndex type_index) {
7311 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
7312}
7313
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007314CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7315 const DexFile& dex_file, uint32_t element_offset) {
7316 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7317}
7318
7319CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7320 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7321 patches->emplace_back(dex_file, offset_or_index);
7322 return &patches->back();
7323}
7324
7325Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007326 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007327 return boot_image_string_patches_.GetOrCreate(
7328 StringReference(&dex_file, string_index),
7329 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7330}
7331
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007332Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007333 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007334 return boot_image_type_patches_.GetOrCreate(
7335 TypeReference(&dex_file, type_index),
7336 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7337}
7338
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007339Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7340 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7341 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7342 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7343}
7344
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007345Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007346 dex::StringIndex string_index,
7347 Handle<mirror::String> handle) {
7348 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7349 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007350 return jit_string_patches_.GetOrCreate(
7351 StringReference(&dex_file, string_index),
7352 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7353}
7354
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007355Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
7356 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007357 Handle<mirror::Class> handle) {
7358 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
7359 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007360 return jit_class_patches_.GetOrCreate(
7361 TypeReference(&dex_file, type_index),
7362 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7363}
7364
Vladimir Markoaad75c62016-10-03 08:46:48 +00007365template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7366inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7367 const ArenaDeque<PcRelativePatchInfo>& infos,
7368 ArenaVector<LinkerPatch>* linker_patches) {
7369 for (const PcRelativePatchInfo& info : infos) {
7370 const DexFile& dex_file = info.target_dex_file;
7371 size_t offset_or_index = info.offset_or_index;
7372 DCHECK(info.add_pc_label.IsBound());
7373 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7374 // Add MOVW patch.
7375 DCHECK(info.movw_label.IsBound());
7376 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7377 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7378 // Add MOVT patch.
7379 DCHECK(info.movt_label.IsBound());
7380 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7381 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7382 }
7383}
7384
Vladimir Marko58155012015-08-19 12:49:41 +00007385void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7386 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007387 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00007388 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007389 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007390 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007391 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007392 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00007393 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007394 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007395 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007396 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7397 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007398 for (const auto& entry : boot_image_string_patches_) {
7399 const StringReference& target_string = entry.first;
7400 Literal* literal = entry.second;
7401 DCHECK(literal->GetLabel()->IsBound());
7402 uint32_t literal_offset = literal->GetLabel()->Position();
7403 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7404 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007405 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007406 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007407 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00007408 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007409 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7410 linker_patches);
7411 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007412 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7413 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007414 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7415 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007416 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007417 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
7418 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007419 for (const auto& entry : boot_image_type_patches_) {
7420 const TypeReference& target_type = entry.first;
7421 Literal* literal = entry.second;
7422 DCHECK(literal->GetLabel()->IsBound());
7423 uint32_t literal_offset = literal->GetLabel()->Position();
7424 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7425 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007426 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007427 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007428 for (const auto& entry : boot_image_address_patches_) {
7429 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7430 Literal* literal = entry.second;
7431 DCHECK(literal->GetLabel()->IsBound());
7432 uint32_t literal_offset = literal->GetLabel()->Position();
7433 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7434 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007435 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007436}
7437
7438Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7439 return map->GetOrCreate(
7440 value,
7441 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007442}
7443
7444Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7445 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007446 return map->GetOrCreate(
7447 target_method,
7448 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007449}
7450
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007451void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7452 LocationSummary* locations =
7453 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7454 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7455 Location::RequiresRegister());
7456 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7457 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7458 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7459}
7460
7461void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7462 LocationSummary* locations = instr->GetLocations();
7463 Register res = locations->Out().AsRegister<Register>();
7464 Register accumulator =
7465 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7466 Register mul_left =
7467 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7468 Register mul_right =
7469 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7470
7471 if (instr->GetOpKind() == HInstruction::kAdd) {
7472 __ mla(res, mul_left, mul_right, accumulator);
7473 } else {
7474 __ mls(res, mul_left, mul_right, accumulator);
7475 }
7476}
7477
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007478void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007479 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007480 LOG(FATAL) << "Unreachable";
7481}
7482
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007483void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007484 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007485 LOG(FATAL) << "Unreachable";
7486}
7487
Mark Mendellfe57faa2015-09-18 09:26:15 -04007488// Simple implementation of packed switch - generate cascaded compare/jumps.
7489void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7490 LocationSummary* locations =
7491 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7492 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007493 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007494 codegen_->GetAssembler()->IsThumb()) {
7495 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7496 if (switch_instr->GetStartValue() != 0) {
7497 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7498 }
7499 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007500}
7501
7502void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7503 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007504 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007505 LocationSummary* locations = switch_instr->GetLocations();
7506 Register value_reg = locations->InAt(0).AsRegister<Register>();
7507 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7508
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007509 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007510 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007511 Register temp_reg = IP;
7512 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7513 // the immediate, because IP is used as the destination register. For the other
7514 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7515 // and they can be encoded in the instruction without making use of IP register.
7516 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7517
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007518 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007519 // Jump to successors[0] if value == lower_bound.
7520 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7521 int32_t last_index = 0;
7522 for (; num_entries - last_index > 2; last_index += 2) {
7523 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7524 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7525 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7526 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7527 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7528 }
7529 if (num_entries - last_index == 2) {
7530 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007531 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007532 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007533 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007534
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007535 // And the default for any other value.
7536 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7537 __ b(codegen_->GetLabelOf(default_block));
7538 }
7539 } else {
7540 // Create a table lookup.
7541 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7542
7543 // Materialize a pointer to the switch table
7544 std::vector<Label*> labels(num_entries);
7545 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7546 for (uint32_t i = 0; i < num_entries; i++) {
7547 labels[i] = codegen_->GetLabelOf(successors[i]);
7548 }
7549 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7550
7551 // Remove the bias.
7552 Register key_reg;
7553 if (lower_bound != 0) {
7554 key_reg = locations->GetTemp(1).AsRegister<Register>();
7555 __ AddConstant(key_reg, value_reg, -lower_bound);
7556 } else {
7557 key_reg = value_reg;
7558 }
7559
7560 // Check whether the value is in the table, jump to default block if not.
7561 __ CmpConstant(key_reg, num_entries - 1);
7562 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7563
7564 // Load the displacement from the table.
7565 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7566
7567 // Dispatch is a direct add to the PC (for Thumb2).
7568 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007569 }
7570}
7571
Vladimir Markob4536b72015-11-24 13:45:23 +00007572void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7573 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7574 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007575}
7576
7577void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7578 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007579 CodeGeneratorARM::PcRelativePatchInfo* labels =
7580 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007581 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007582 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007583 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007584 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007585 __ BindTrackedLabel(&labels->add_pc_label);
7586 __ add(base_reg, base_reg, ShifterOperand(PC));
7587}
7588
Andreas Gampe85b62f22015-09-09 13:15:38 -07007589void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7590 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007591 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007592 return;
7593 }
7594
7595 DCHECK_NE(type, Primitive::kPrimVoid);
7596
7597 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7598 if (return_loc.Equals(trg)) {
7599 return;
7600 }
7601
7602 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7603 // with the last branch.
7604 if (type == Primitive::kPrimLong) {
7605 HParallelMove parallel_move(GetGraph()->GetArena());
7606 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7607 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7608 GetMoveResolver()->EmitNativeCode(&parallel_move);
7609 } else if (type == Primitive::kPrimDouble) {
7610 HParallelMove parallel_move(GetGraph()->GetArena());
7611 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7612 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7613 GetMoveResolver()->EmitNativeCode(&parallel_move);
7614 } else {
7615 // Let the parallel move resolver take care of all of this.
7616 HParallelMove parallel_move(GetGraph()->GetArena());
7617 parallel_move.AddMove(return_loc, trg, type, nullptr);
7618 GetMoveResolver()->EmitNativeCode(&parallel_move);
7619 }
7620}
7621
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007622void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7623 LocationSummary* locations =
7624 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7625 locations->SetInAt(0, Location::RequiresRegister());
7626 locations->SetOut(Location::RequiresRegister());
7627}
7628
7629void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7630 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007631 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007632 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007633 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007634 __ LoadFromOffset(kLoadWord,
7635 locations->Out().AsRegister<Register>(),
7636 locations->InAt(0).AsRegister<Register>(),
7637 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007638 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007639 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007640 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007641 __ LoadFromOffset(kLoadWord,
7642 locations->Out().AsRegister<Register>(),
7643 locations->InAt(0).AsRegister<Register>(),
7644 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7645 __ LoadFromOffset(kLoadWord,
7646 locations->Out().AsRegister<Register>(),
7647 locations->Out().AsRegister<Register>(),
7648 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007649 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007650}
7651
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007652static void PatchJitRootUse(uint8_t* code,
7653 const uint8_t* roots_data,
7654 Literal* literal,
7655 uint64_t index_in_table) {
7656 DCHECK(literal->GetLabel()->IsBound());
7657 uint32_t literal_offset = literal->GetLabel()->Position();
7658 uintptr_t address =
7659 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7660 uint8_t* data = code + literal_offset;
7661 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7662}
7663
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007664void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7665 for (const auto& entry : jit_string_patches_) {
7666 const auto& it = jit_string_roots_.find(entry.first);
7667 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007668 PatchJitRootUse(code, roots_data, entry.second, it->second);
7669 }
7670 for (const auto& entry : jit_class_patches_) {
7671 const auto& it = jit_class_roots_.find(entry.first);
7672 DCHECK(it != jit_class_roots_.end());
7673 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007674 }
7675}
7676
Roland Levillain4d027112015-07-01 15:41:14 +01007677#undef __
7678#undef QUICK_ENTRY_POINT
7679
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007680} // namespace arm
7681} // namespace art