blob: 759a951d6be63e86df368ab3c1d792d17e404ed6 [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:
Vladimir Markoea4c1262017-02-06 19:59:33 +0000370 LoadClassSlowPathARM(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000371 : SlowPathCodeARM(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000372 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
373 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100374
Alexandre Rames67555f72014-11-18 10:55:16 +0000375 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000376 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000377 Location out = locations->Out();
378 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000379
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100380 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
381 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000382 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100383
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100384 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000385 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
386 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
387 bool is_load_class_bss_entry =
388 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
389 Register entry_address = kNoRegister;
390 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
391 Register temp = locations->GetTemp(0).AsRegister<Register>();
392 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
393 // the kSaveEverything call.
394 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
395 entry_address = temp_is_r0 ? out.AsRegister<Register>() : temp;
396 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
397 if (temp_is_r0) {
398 __ mov(entry_address, ShifterOperand(temp));
399 }
400 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000401 dex::TypeIndex type_index = cls_->GetTypeIndex();
402 __ LoadImmediate(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100403 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
404 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000405 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000406 if (do_clinit_) {
407 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
408 } else {
409 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
410 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000411
Vladimir Markoea4c1262017-02-06 19:59:33 +0000412 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
413 if (is_load_class_bss_entry) {
414 if (call_saves_everything_except_r0) {
415 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
416 __ str(R0, Address(entry_address));
417 } else {
418 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
419 Register temp = IP;
420 CodeGeneratorARM::PcRelativePatchInfo* labels =
421 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
422 __ BindTrackedLabel(&labels->movw_label);
423 __ movw(temp, /* placeholder */ 0u);
424 __ BindTrackedLabel(&labels->movt_label);
425 __ movt(temp, /* placeholder */ 0u);
426 __ BindTrackedLabel(&labels->add_pc_label);
427 __ add(temp, temp, ShifterOperand(PC));
428 __ str(R0, Address(temp));
429 }
430 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000431 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000432 if (out.IsValid()) {
433 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000434 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
435 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000436 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100437 __ b(GetExitLabel());
438 }
439
Alexandre Rames9931f312015-06-19 14:47:01 +0100440 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
441
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100442 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000443 // The class this slow path will load.
444 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100445
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000446 // The dex PC of `at_`.
447 const uint32_t dex_pc_;
448
449 // Whether to initialize the class.
450 const bool do_clinit_;
451
452 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100453};
454
Vladimir Markoaad75c62016-10-03 08:46:48 +0000455class LoadStringSlowPathARM : public SlowPathCodeARM {
456 public:
457 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
458
459 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000460 DCHECK(instruction_->IsLoadString());
461 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000462 LocationSummary* locations = instruction_->GetLocations();
463 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100464 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000465 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100466 Register out = locations->Out().AsRegister<Register>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100467 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000468
469 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
470 __ Bind(GetEntryLabel());
471 SaveLiveRegisters(codegen, locations);
472
473 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100474 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000475 // the kSaveEverything call.
476 Register entry_address = kNoRegister;
477 if (call_saves_everything_except_r0) {
478 Register temp = locations->GetTemp(0).AsRegister<Register>();
479 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
480 entry_address = temp_is_r0 ? out : temp;
481 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
482 if (temp_is_r0) {
483 __ mov(entry_address, ShifterOperand(temp));
484 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100485 }
486
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000487 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000488 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
489 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100490
491 // Store the resolved String to the .bss entry.
492 if (call_saves_everything_except_r0) {
493 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
494 __ str(R0, Address(entry_address));
495 } else {
496 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000497 Register temp = IP;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100498 CodeGeneratorARM::PcRelativePatchInfo* labels =
499 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
500 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000501 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100502 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000503 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100504 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000505 __ add(temp, temp, ShifterOperand(PC));
506 __ str(R0, Address(temp));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100507 }
508
Vladimir Markoaad75c62016-10-03 08:46:48 +0000509 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000510 RestoreLiveRegisters(codegen, locations);
511
Vladimir Markoaad75c62016-10-03 08:46:48 +0000512 __ b(GetExitLabel());
513 }
514
515 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
516
517 private:
518 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
519};
520
Artem Serovf4d6aee2016-07-11 10:41:45 +0100521class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000522 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000523 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100524 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000525
Alexandre Rames67555f72014-11-18 10:55:16 +0000526 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000527 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000528 DCHECK(instruction_->IsCheckCast()
529 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000530
531 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
532 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000533
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000534 if (!is_fatal_) {
535 SaveLiveRegisters(codegen, locations);
536 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000537
538 // We're moving two locations to locations that could overlap, so we need a parallel
539 // move resolver.
540 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800541 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800542 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
543 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800544 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800545 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
546 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000547 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100548 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100549 instruction_,
550 instruction_->GetDexPc(),
551 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800552 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000553 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
554 } else {
555 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800556 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
557 instruction_,
558 instruction_->GetDexPc(),
559 this);
560 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000561 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000562
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000563 if (!is_fatal_) {
564 RestoreLiveRegisters(codegen, locations);
565 __ b(GetExitLabel());
566 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000567 }
568
Alexandre Rames9931f312015-06-19 14:47:01 +0100569 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
570
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000571 bool IsFatal() const OVERRIDE { return is_fatal_; }
572
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000573 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000574 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000575
576 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
577};
578
Artem Serovf4d6aee2016-07-11 10:41:45 +0100579class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700580 public:
Aart Bik42249c32016-01-07 15:33:50 -0800581 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100582 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700583
584 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800585 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700586 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100587 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000588 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700589 }
590
Alexandre Rames9931f312015-06-19 14:47:01 +0100591 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
592
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700593 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700594 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
595};
596
Artem Serovf4d6aee2016-07-11 10:41:45 +0100597class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100598 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100599 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100600
601 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
602 LocationSummary* locations = instruction_->GetLocations();
603 __ Bind(GetEntryLabel());
604 SaveLiveRegisters(codegen, locations);
605
606 InvokeRuntimeCallingConvention calling_convention;
607 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
608 parallel_move.AddMove(
609 locations->InAt(0),
610 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
611 Primitive::kPrimNot,
612 nullptr);
613 parallel_move.AddMove(
614 locations->InAt(1),
615 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
616 Primitive::kPrimInt,
617 nullptr);
618 parallel_move.AddMove(
619 locations->InAt(2),
620 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
621 Primitive::kPrimNot,
622 nullptr);
623 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
624
625 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100626 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000627 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100628 RestoreLiveRegisters(codegen, locations);
629 __ b(GetExitLabel());
630 }
631
632 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
633
634 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100635 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
636};
637
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100638// Slow path marking an object reference `ref` during a read
639// barrier. The field `obj.field` in the object `obj` holding this
640// reference does not get updated by this slow path after marking (see
641// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
642//
643// This means that after the execution of this slow path, `ref` will
644// always be up-to-date, but `obj.field` may not; i.e., after the
645// flip, `ref` will be a to-space reference, but `obj.field` will
646// probably still be a from-space reference (unless it gets updated by
647// another thread, or if another thread installed another object
648// reference (different from `ref`) in `obj.field`).
Roland Levillaind966ce72017-02-09 16:20:14 +0000649//
650// If `entrypoint` is a valid location it is assumed to already be
651// holding the entrypoint. The case where the entrypoint is passed in
652// is for the GcRoot read barrier.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100653class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000654 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800655 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
656 Location ref,
657 Location entrypoint = Location::NoLocation())
658 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillainc9285912015-12-18 10:38:42 +0000659 DCHECK(kEmitCompilerReadBarrier);
660 }
661
662 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
663
664 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
665 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100666 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000667 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100668 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillainc9285912015-12-18 10:38:42 +0000669 DCHECK(instruction_->IsInstanceFieldGet() ||
670 instruction_->IsStaticFieldGet() ||
671 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100672 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000673 instruction_->IsLoadClass() ||
674 instruction_->IsLoadString() ||
675 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100676 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100677 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
678 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000679 << "Unexpected instruction in read barrier marking slow path: "
680 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000681 // The read barrier instrumentation of object ArrayGet
682 // instructions does not support the HIntermediateAddress
683 // instruction.
684 DCHECK(!(instruction_->IsArrayGet() &&
685 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillainc9285912015-12-18 10:38:42 +0000686
687 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100688 // No need to save live registers; it's taken care of by the
689 // entrypoint. Also, there is no need to update the stack mask,
690 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000691 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100692 DCHECK_NE(ref_reg, SP);
693 DCHECK_NE(ref_reg, LR);
694 DCHECK_NE(ref_reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100695 // IP is used internally by the ReadBarrierMarkRegX entry point
696 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100697 DCHECK_NE(ref_reg, IP);
698 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100699 // "Compact" slow path, saving two moves.
700 //
701 // Instead of using the standard runtime calling convention (input
702 // and output in R0):
703 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100704 // R0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100705 // R0 <- ReadBarrierMark(R0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100706 // ref <- R0
Roland Levillain02b75802016-07-13 11:54:35 +0100707 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100708 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100709 // of a dedicated entrypoint:
710 //
711 // rX <- ReadBarrierMarkRegX(rX)
712 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800713 if (entrypoint_.IsValid()) {
714 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
715 __ blx(entrypoint_.AsRegister<Register>());
716 } else {
717 int32_t entry_point_offset =
718 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
719 // This runtime call does not require a stack map.
720 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
721 }
Roland Levillainc9285912015-12-18 10:38:42 +0000722 __ b(GetExitLabel());
723 }
724
725 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100726 // The location (register) of the marked object reference.
727 const Location ref_;
Roland Levillainc9285912015-12-18 10:38:42 +0000728
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800729 // The location of the entrypoint if already loaded.
730 const Location entrypoint_;
731
Roland Levillainc9285912015-12-18 10:38:42 +0000732 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
733};
734
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100735// Slow path marking an object reference `ref` during a read barrier,
736// and if needed, atomically updating the field `obj.field` in the
737// object `obj` holding this reference after marking (contrary to
738// ReadBarrierMarkSlowPathARM above, which never tries to update
739// `obj.field`).
740//
741// This means that after the execution of this slow path, both `ref`
742// and `obj.field` will be up-to-date; i.e., after the flip, both will
743// hold the same to-space reference (unless another thread installed
744// another object reference (different from `ref`) in `obj.field`).
745class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
746 public:
747 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
748 Location ref,
749 Register obj,
750 Location field_offset,
751 Register temp1,
752 Register temp2)
753 : SlowPathCodeARM(instruction),
754 ref_(ref),
755 obj_(obj),
756 field_offset_(field_offset),
757 temp1_(temp1),
758 temp2_(temp2) {
759 DCHECK(kEmitCompilerReadBarrier);
760 }
761
762 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
763
764 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
765 LocationSummary* locations = instruction_->GetLocations();
766 Register ref_reg = ref_.AsRegister<Register>();
767 DCHECK(locations->CanCall());
768 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
769 // This slow path is only used by the UnsafeCASObject intrinsic.
770 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
771 << "Unexpected instruction in read barrier marking and field updating slow path: "
772 << instruction_->DebugName();
773 DCHECK(instruction_->GetLocations()->Intrinsified());
774 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
775 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
776
777 __ Bind(GetEntryLabel());
778
779 // Save the old reference.
780 // Note that we cannot use IP to save the old reference, as IP is
781 // used internally by the ReadBarrierMarkRegX entry point, and we
782 // need the old reference after the call to that entry point.
783 DCHECK_NE(temp1_, IP);
784 __ Mov(temp1_, ref_reg);
785
786 // No need to save live registers; it's taken care of by the
787 // entrypoint. Also, there is no need to update the stack mask,
788 // as this runtime call will not trigger a garbage collection.
789 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
790 DCHECK_NE(ref_reg, SP);
791 DCHECK_NE(ref_reg, LR);
792 DCHECK_NE(ref_reg, PC);
793 // IP is used internally by the ReadBarrierMarkRegX entry point
794 // as a temporary, it cannot be the entry point's input/output.
795 DCHECK_NE(ref_reg, IP);
796 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
797 // "Compact" slow path, saving two moves.
798 //
799 // Instead of using the standard runtime calling convention (input
800 // and output in R0):
801 //
802 // R0 <- ref
803 // R0 <- ReadBarrierMark(R0)
804 // ref <- R0
805 //
806 // we just use rX (the register containing `ref`) as input and output
807 // of a dedicated entrypoint:
808 //
809 // rX <- ReadBarrierMarkRegX(rX)
810 //
811 int32_t entry_point_offset =
812 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
813 // This runtime call does not require a stack map.
814 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
815
816 // If the new reference is different from the old reference,
817 // update the field in the holder (`*(obj_ + field_offset_)`).
818 //
819 // Note that this field could also hold a different object, if
820 // another thread had concurrently changed it. In that case, the
821 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
822 // (CAS) operation below would abort the CAS, leaving the field
823 // as-is.
824 Label done;
825 __ cmp(temp1_, ShifterOperand(ref_reg));
826 __ b(&done, EQ);
827
828 // Update the the holder's field atomically. This may fail if
829 // mutator updates before us, but it's OK. This is achieved
830 // using a strong compare-and-set (CAS) operation with relaxed
831 // memory synchronization ordering, where the expected value is
832 // the old reference and the desired value is the new reference.
833
834 // Convenience aliases.
835 Register base = obj_;
836 // The UnsafeCASObject intrinsic uses a register pair as field
837 // offset ("long offset"), of which only the low part contains
838 // data.
839 Register offset = field_offset_.AsRegisterPairLow<Register>();
840 Register expected = temp1_;
841 Register value = ref_reg;
842 Register tmp_ptr = IP; // Pointer to actual memory.
843 Register tmp = temp2_; // Value in memory.
844
845 __ add(tmp_ptr, base, ShifterOperand(offset));
846
847 if (kPoisonHeapReferences) {
848 __ PoisonHeapReference(expected);
849 if (value == expected) {
850 // Do not poison `value`, as it is the same register as
851 // `expected`, which has just been poisoned.
852 } else {
853 __ PoisonHeapReference(value);
854 }
855 }
856
857 // do {
858 // tmp = [r_ptr] - expected;
859 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
860
Roland Levillain24a4d112016-10-26 13:10:46 +0100861 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100862 __ Bind(&loop_head);
863
864 __ ldrex(tmp, tmp_ptr);
865
866 __ subs(tmp, tmp, ShifterOperand(expected));
867
Roland Levillain24a4d112016-10-26 13:10:46 +0100868 __ it(NE);
869 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100870
Roland Levillain24a4d112016-10-26 13:10:46 +0100871 __ b(&exit_loop, NE);
872
873 __ strex(tmp, value, tmp_ptr);
874 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100875 __ b(&loop_head, EQ);
876
Roland Levillain24a4d112016-10-26 13:10:46 +0100877 __ Bind(&exit_loop);
878
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100879 if (kPoisonHeapReferences) {
880 __ UnpoisonHeapReference(expected);
881 if (value == expected) {
882 // Do not unpoison `value`, as it is the same register as
883 // `expected`, which has just been unpoisoned.
884 } else {
885 __ UnpoisonHeapReference(value);
886 }
887 }
888
889 __ Bind(&done);
890 __ b(GetExitLabel());
891 }
892
893 private:
894 // The location (register) of the marked object reference.
895 const Location ref_;
896 // The register containing the object holding the marked object reference field.
897 const Register obj_;
898 // The location of the offset of the marked reference field within `obj_`.
899 Location field_offset_;
900
901 const Register temp1_;
902 const Register temp2_;
903
904 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
905};
906
Roland Levillain3b359c72015-11-17 19:35:12 +0000907// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100908class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000909 public:
910 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
911 Location out,
912 Location ref,
913 Location obj,
914 uint32_t offset,
915 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100916 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000917 out_(out),
918 ref_(ref),
919 obj_(obj),
920 offset_(offset),
921 index_(index) {
922 DCHECK(kEmitCompilerReadBarrier);
923 // If `obj` is equal to `out` or `ref`, it means the initial object
924 // has been overwritten by (or after) the heap object reference load
925 // to be instrumented, e.g.:
926 //
927 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000928 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000929 //
930 // In that case, we have lost the information about the original
931 // object, and the emitted read barrier cannot work properly.
932 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
933 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
934 }
935
936 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
937 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
938 LocationSummary* locations = instruction_->GetLocations();
939 Register reg_out = out_.AsRegister<Register>();
940 DCHECK(locations->CanCall());
941 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100942 DCHECK(instruction_->IsInstanceFieldGet() ||
943 instruction_->IsStaticFieldGet() ||
944 instruction_->IsArrayGet() ||
945 instruction_->IsInstanceOf() ||
946 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100947 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000948 << "Unexpected instruction in read barrier for heap reference slow path: "
949 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000950 // The read barrier instrumentation of object ArrayGet
951 // instructions does not support the HIntermediateAddress
952 // instruction.
953 DCHECK(!(instruction_->IsArrayGet() &&
954 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +0000955
956 __ Bind(GetEntryLabel());
957 SaveLiveRegisters(codegen, locations);
958
959 // We may have to change the index's value, but as `index_` is a
960 // constant member (like other "inputs" of this slow path),
961 // introduce a copy of it, `index`.
962 Location index = index_;
963 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100964 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000965 if (instruction_->IsArrayGet()) {
966 // Compute the actual memory offset and store it in `index`.
967 Register index_reg = index_.AsRegister<Register>();
968 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
969 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
970 // We are about to change the value of `index_reg` (see the
971 // calls to art::arm::Thumb2Assembler::Lsl and
972 // art::arm::Thumb2Assembler::AddConstant below), but it has
973 // not been saved by the previous call to
974 // art::SlowPathCode::SaveLiveRegisters, as it is a
975 // callee-save register --
976 // art::SlowPathCode::SaveLiveRegisters does not consider
977 // callee-save registers, as it has been designed with the
978 // assumption that callee-save registers are supposed to be
979 // handled by the called function. So, as a callee-save
980 // register, `index_reg` _would_ eventually be saved onto
981 // the stack, but it would be too late: we would have
982 // changed its value earlier. Therefore, we manually save
983 // it here into another freely available register,
984 // `free_reg`, chosen of course among the caller-save
985 // registers (as a callee-save `free_reg` register would
986 // exhibit the same problem).
987 //
988 // Note we could have requested a temporary register from
989 // the register allocator instead; but we prefer not to, as
990 // this is a slow path, and we know we can find a
991 // caller-save register that is available.
992 Register free_reg = FindAvailableCallerSaveRegister(codegen);
993 __ Mov(free_reg, index_reg);
994 index_reg = free_reg;
995 index = Location::RegisterLocation(index_reg);
996 } else {
997 // The initial register stored in `index_` has already been
998 // saved in the call to art::SlowPathCode::SaveLiveRegisters
999 // (as it is not a callee-save register), so we can freely
1000 // use it.
1001 }
1002 // Shifting the index value contained in `index_reg` by the scale
1003 // factor (2) cannot overflow in practice, as the runtime is
1004 // unable to allocate object arrays with a size larger than
1005 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1006 __ Lsl(index_reg, index_reg, TIMES_4);
1007 static_assert(
1008 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1009 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1010 __ AddConstant(index_reg, index_reg, offset_);
1011 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001012 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1013 // intrinsics, `index_` is not shifted by a scale factor of 2
1014 // (as in the case of ArrayGet), as it is actually an offset
1015 // to an object field within an object.
1016 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001017 DCHECK(instruction_->GetLocations()->Intrinsified());
1018 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1019 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1020 << instruction_->AsInvoke()->GetIntrinsic();
1021 DCHECK_EQ(offset_, 0U);
1022 DCHECK(index_.IsRegisterPair());
1023 // UnsafeGet's offset location is a register pair, the low
1024 // part contains the correct offset.
1025 index = index_.ToLow();
1026 }
1027 }
1028
1029 // We're moving two or three locations to locations that could
1030 // overlap, so we need a parallel move resolver.
1031 InvokeRuntimeCallingConvention calling_convention;
1032 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1033 parallel_move.AddMove(ref_,
1034 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1035 Primitive::kPrimNot,
1036 nullptr);
1037 parallel_move.AddMove(obj_,
1038 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1039 Primitive::kPrimNot,
1040 nullptr);
1041 if (index.IsValid()) {
1042 parallel_move.AddMove(index,
1043 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1044 Primitive::kPrimInt,
1045 nullptr);
1046 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1047 } else {
1048 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1049 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1050 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001051 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001052 CheckEntrypointTypes<
1053 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1054 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1055
1056 RestoreLiveRegisters(codegen, locations);
1057 __ b(GetExitLabel());
1058 }
1059
1060 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1061
1062 private:
1063 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1064 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1065 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1066 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1067 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1068 return static_cast<Register>(i);
1069 }
1070 }
1071 // We shall never fail to find a free caller-save register, as
1072 // there are more than two core caller-save registers on ARM
1073 // (meaning it is possible to find one which is different from
1074 // `ref` and `obj`).
1075 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1076 LOG(FATAL) << "Could not find a free caller-save register";
1077 UNREACHABLE();
1078 }
1079
Roland Levillain3b359c72015-11-17 19:35:12 +00001080 const Location out_;
1081 const Location ref_;
1082 const Location obj_;
1083 const uint32_t offset_;
1084 // An additional location containing an index to an array.
1085 // Only used for HArrayGet and the UnsafeGetObject &
1086 // UnsafeGetObjectVolatile intrinsics.
1087 const Location index_;
1088
1089 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1090};
1091
1092// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001093class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001094 public:
1095 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001096 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001097 DCHECK(kEmitCompilerReadBarrier);
1098 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001099
1100 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1101 LocationSummary* locations = instruction_->GetLocations();
1102 Register reg_out = out_.AsRegister<Register>();
1103 DCHECK(locations->CanCall());
1104 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001105 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1106 << "Unexpected instruction in read barrier for GC root slow path: "
1107 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001108
1109 __ Bind(GetEntryLabel());
1110 SaveLiveRegisters(codegen, locations);
1111
1112 InvokeRuntimeCallingConvention calling_convention;
1113 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1114 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001115 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001116 instruction_,
1117 instruction_->GetDexPc(),
1118 this);
1119 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1120 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1121
1122 RestoreLiveRegisters(codegen, locations);
1123 __ b(GetExitLabel());
1124 }
1125
1126 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1127
1128 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001129 const Location out_;
1130 const Location root_;
1131
1132 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1133};
1134
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001135#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001136// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1137#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -07001138
Aart Bike9f37602015-10-09 11:15:55 -07001139inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001140 switch (cond) {
1141 case kCondEQ: return EQ;
1142 case kCondNE: return NE;
1143 case kCondLT: return LT;
1144 case kCondLE: return LE;
1145 case kCondGT: return GT;
1146 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001147 case kCondB: return LO;
1148 case kCondBE: return LS;
1149 case kCondA: return HI;
1150 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001151 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001152 LOG(FATAL) << "Unreachable";
1153 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001154}
1155
Aart Bike9f37602015-10-09 11:15:55 -07001156// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001157inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001158 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001159 case kCondEQ: return EQ;
1160 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001161 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001162 case kCondLT: return LO;
1163 case kCondLE: return LS;
1164 case kCondGT: return HI;
1165 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001166 // Unsigned remain unchanged.
1167 case kCondB: return LO;
1168 case kCondBE: return LS;
1169 case kCondA: return HI;
1170 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001171 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001172 LOG(FATAL) << "Unreachable";
1173 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001174}
1175
Vladimir Markod6e069b2016-01-18 11:11:01 +00001176inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1177 // The ARM condition codes can express all the necessary branches, see the
1178 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1179 // There is no dex instruction or HIR that would need the missing conditions
1180 // "equal or unordered" or "not equal".
1181 switch (cond) {
1182 case kCondEQ: return EQ;
1183 case kCondNE: return NE /* unordered */;
1184 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1185 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1186 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1187 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1188 default:
1189 LOG(FATAL) << "UNREACHABLE";
1190 UNREACHABLE();
1191 }
1192}
1193
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001194void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001195 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001196}
1197
1198void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001199 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001200}
1201
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001202size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1203 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1204 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001205}
1206
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001207size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1208 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1209 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001210}
1211
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001212size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1213 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1214 return kArmWordSize;
1215}
1216
1217size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1218 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1219 return kArmWordSize;
1220}
1221
Calin Juravle34166012014-12-19 17:22:29 +00001222CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001223 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001224 const CompilerOptions& compiler_options,
1225 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001226 : CodeGenerator(graph,
1227 kNumberOfCoreRegisters,
1228 kNumberOfSRegisters,
1229 kNumberOfRegisterPairs,
1230 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1231 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001232 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1233 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001234 compiler_options,
1235 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001236 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001237 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001238 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001239 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001240 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001241 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001242 uint32_literals_(std::less<uint32_t>(),
1243 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001244 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1245 boot_image_string_patches_(StringReferenceValueComparator(),
1246 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1247 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001248 boot_image_type_patches_(TypeReferenceValueComparator(),
1249 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1250 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001251 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001252 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001253 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1254 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001255 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1256 jit_class_patches_(TypeReferenceValueComparator(),
1257 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001258 // Always save the LR register to mimic Quick.
1259 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001260}
1261
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001262void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1263 // Ensure that we fix up branches and literal loads and emit the literal pool.
1264 __ FinalizeCode();
1265
1266 // Adjust native pc offsets in stack maps.
1267 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001268 uint32_t old_position =
1269 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001270 uint32_t new_position = __ GetAdjustedPosition(old_position);
1271 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1272 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001273 // Adjust pc offsets for the disassembly information.
1274 if (disasm_info_ != nullptr) {
1275 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1276 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1277 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1278 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1279 it.second.start = __ GetAdjustedPosition(it.second.start);
1280 it.second.end = __ GetAdjustedPosition(it.second.end);
1281 }
1282 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1283 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1284 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1285 }
1286 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001287
1288 CodeGenerator::Finalize(allocator);
1289}
1290
David Brazdil58282f42016-01-14 12:45:10 +00001291void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001292 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001293 blocked_core_registers_[SP] = true;
1294 blocked_core_registers_[LR] = true;
1295 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001296
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001297 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001298 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001299
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001300 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001301 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001302
David Brazdil58282f42016-01-14 12:45:10 +00001303 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001304 // Stubs do not save callee-save floating point registers. If the graph
1305 // is debuggable, we need to deal with these registers differently. For
1306 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001307 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1308 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1309 }
1310 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001311}
1312
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001313InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001314 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001315 assembler_(codegen->GetAssembler()),
1316 codegen_(codegen) {}
1317
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001318void CodeGeneratorARM::ComputeSpillMask() {
1319 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1320 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001321 // There is no easy instruction to restore just the PC on thumb2. We spill and
1322 // restore another arbitrary register.
1323 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001324 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1325 // We use vpush and vpop for saving and restoring floating point registers, which take
1326 // a SRegister and the number of registers to save/restore after that SRegister. We
1327 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1328 // but in the range.
1329 if (fpu_spill_mask_ != 0) {
1330 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1331 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1332 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1333 fpu_spill_mask_ |= (1 << i);
1334 }
1335 }
1336}
1337
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001338static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001339 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001340}
1341
1342static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001343 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001344}
1345
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001346void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001347 bool skip_overflow_check =
1348 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001349 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001350 __ Bind(&frame_entry_label_);
1351
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001352 if (HasEmptyFrame()) {
1353 return;
1354 }
1355
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001356 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001357 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1358 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1359 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001360 }
1361
Andreas Gampe501fd632015-09-10 16:11:06 -07001362 __ PushList(core_spill_mask_);
1363 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1364 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001365 if (fpu_spill_mask_ != 0) {
1366 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1367 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001368 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001369 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001370 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001371
1372 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1373 // Initialize should_deoptimize flag to 0.
1374 __ mov(IP, ShifterOperand(0));
1375 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
1376 }
1377
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001378 int adjust = GetFrameSize() - FrameEntrySpillSize();
1379 __ AddConstant(SP, -adjust);
1380 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001381
1382 // Save the current method if we need it. Note that we do not
1383 // do this in HCurrentMethod, as the instruction might have been removed
1384 // in the SSA graph.
1385 if (RequiresCurrentMethod()) {
1386 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1387 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001388}
1389
1390void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001391 if (HasEmptyFrame()) {
1392 __ bx(LR);
1393 return;
1394 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001395 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001396 int adjust = GetFrameSize() - FrameEntrySpillSize();
1397 __ AddConstant(SP, adjust);
1398 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001399 if (fpu_spill_mask_ != 0) {
1400 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1401 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001402 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001403 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001404 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001405 // Pop LR into PC to return.
1406 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1407 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1408 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001409 __ cfi().RestoreState();
1410 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001411}
1412
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001413void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001414 Label* label = GetLabelOf(block);
1415 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001416}
1417
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001418Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001419 switch (type) {
1420 case Primitive::kPrimBoolean:
1421 case Primitive::kPrimByte:
1422 case Primitive::kPrimChar:
1423 case Primitive::kPrimShort:
1424 case Primitive::kPrimInt:
1425 case Primitive::kPrimNot: {
1426 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001427 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001428 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001429 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001430 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001431 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001432 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001433 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001434
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001435 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001436 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001437 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001438 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001439 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001440 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001441 if (calling_convention.GetRegisterAt(index) == R1) {
1442 // Skip R1, and use R2_R3 instead.
1443 gp_index_++;
1444 index++;
1445 }
1446 }
1447 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1448 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001449 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001450
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001451 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001452 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001453 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001454 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1455 }
1456 }
1457
1458 case Primitive::kPrimFloat: {
1459 uint32_t stack_index = stack_index_++;
1460 if (float_index_ % 2 == 0) {
1461 float_index_ = std::max(double_index_, float_index_);
1462 }
1463 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1464 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1465 } else {
1466 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1467 }
1468 }
1469
1470 case Primitive::kPrimDouble: {
1471 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1472 uint32_t stack_index = stack_index_;
1473 stack_index_ += 2;
1474 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1475 uint32_t index = double_index_;
1476 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001477 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001478 calling_convention.GetFpuRegisterAt(index),
1479 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001480 DCHECK(ExpectedPairLayout(result));
1481 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001482 } else {
1483 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001484 }
1485 }
1486
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001487 case Primitive::kPrimVoid:
1488 LOG(FATAL) << "Unexpected parameter type " << type;
1489 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001490 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001491 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001492}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001493
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001494Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001495 switch (type) {
1496 case Primitive::kPrimBoolean:
1497 case Primitive::kPrimByte:
1498 case Primitive::kPrimChar:
1499 case Primitive::kPrimShort:
1500 case Primitive::kPrimInt:
1501 case Primitive::kPrimNot: {
1502 return Location::RegisterLocation(R0);
1503 }
1504
1505 case Primitive::kPrimFloat: {
1506 return Location::FpuRegisterLocation(S0);
1507 }
1508
1509 case Primitive::kPrimLong: {
1510 return Location::RegisterPairLocation(R0, R1);
1511 }
1512
1513 case Primitive::kPrimDouble: {
1514 return Location::FpuRegisterPairLocation(S0, S1);
1515 }
1516
1517 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001518 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001519 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001520
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001521 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001522}
1523
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001524Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1525 return Location::RegisterLocation(kMethodRegisterArgument);
1526}
1527
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001528void CodeGeneratorARM::Move32(Location destination, Location source) {
1529 if (source.Equals(destination)) {
1530 return;
1531 }
1532 if (destination.IsRegister()) {
1533 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001534 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001535 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001536 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001537 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001538 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001539 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001540 } else if (destination.IsFpuRegister()) {
1541 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001542 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001543 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001544 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001545 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001546 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001547 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001548 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001549 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001550 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001551 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001552 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001553 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001554 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001555 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001556 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1557 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001558 }
1559 }
1560}
1561
1562void CodeGeneratorARM::Move64(Location destination, Location source) {
1563 if (source.Equals(destination)) {
1564 return;
1565 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001566 if (destination.IsRegisterPair()) {
1567 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001568 EmitParallelMoves(
1569 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1570 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001571 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001572 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001573 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1574 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001575 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001576 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001577 } else if (source.IsFpuRegisterPair()) {
1578 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1579 destination.AsRegisterPairHigh<Register>(),
1580 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001581 } else {
1582 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001583 DCHECK(ExpectedPairLayout(destination));
1584 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1585 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001586 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001587 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001588 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001589 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1590 SP,
1591 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001592 } else if (source.IsRegisterPair()) {
1593 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1594 source.AsRegisterPairLow<Register>(),
1595 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001596 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001597 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001598 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001599 } else {
1600 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001601 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001602 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001603 if (source.AsRegisterPairLow<Register>() == R1) {
1604 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001605 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1606 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001607 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001608 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001609 SP, destination.GetStackIndex());
1610 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001611 } else if (source.IsFpuRegisterPair()) {
1612 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1613 SP,
1614 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001615 } else {
1616 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001617 EmitParallelMoves(
1618 Location::StackSlot(source.GetStackIndex()),
1619 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001620 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001621 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001622 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1623 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001624 }
1625 }
1626}
1627
Calin Juravle175dc732015-08-25 15:42:32 +01001628void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1629 DCHECK(location.IsRegister());
1630 __ LoadImmediate(location.AsRegister<Register>(), value);
1631}
1632
Calin Juravlee460d1d2015-09-29 04:52:17 +01001633void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001634 HParallelMove move(GetGraph()->GetArena());
1635 move.AddMove(src, dst, dst_type, nullptr);
1636 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001637}
1638
1639void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1640 if (location.IsRegister()) {
1641 locations->AddTemp(location);
1642 } else if (location.IsRegisterPair()) {
1643 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1644 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1645 } else {
1646 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1647 }
1648}
1649
Calin Juravle175dc732015-08-25 15:42:32 +01001650void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1651 HInstruction* instruction,
1652 uint32_t dex_pc,
1653 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001654 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001655 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001656 if (EntrypointRequiresStackMap(entrypoint)) {
1657 RecordPcInfo(instruction, dex_pc, slow_path);
1658 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001659}
1660
Roland Levillaindec8f632016-07-22 17:10:06 +01001661void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1662 HInstruction* instruction,
1663 SlowPathCode* slow_path) {
1664 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001665 GenerateInvokeRuntime(entry_point_offset);
1666}
1667
1668void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001669 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1670 __ blx(LR);
1671}
1672
David Brazdilfc6a86a2015-06-26 10:33:45 +00001673void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001674 DCHECK(!successor->IsExitBlock());
1675
1676 HBasicBlock* block = got->GetBlock();
1677 HInstruction* previous = got->GetPrevious();
1678
1679 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001680 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001681 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1682 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1683 return;
1684 }
1685
1686 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1687 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1688 }
1689 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001690 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001691 }
1692}
1693
David Brazdilfc6a86a2015-06-26 10:33:45 +00001694void LocationsBuilderARM::VisitGoto(HGoto* got) {
1695 got->SetLocations(nullptr);
1696}
1697
1698void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1699 HandleGoto(got, got->GetSuccessor());
1700}
1701
1702void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1703 try_boundary->SetLocations(nullptr);
1704}
1705
1706void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1707 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1708 if (!successor->IsExitBlock()) {
1709 HandleGoto(try_boundary, successor);
1710 }
1711}
1712
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001713void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001714 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001715}
1716
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001717void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001718}
1719
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001720void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1721 Primitive::Type type = instruction->InputAt(0)->GetType();
1722 Location lhs_loc = instruction->GetLocations()->InAt(0);
1723 Location rhs_loc = instruction->GetLocations()->InAt(1);
1724 if (rhs_loc.IsConstant()) {
1725 // 0.0 is the only immediate that can be encoded directly in
1726 // a VCMP instruction.
1727 //
1728 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1729 // specify that in a floating-point comparison, positive zero
1730 // and negative zero are considered equal, so we can use the
1731 // literal 0.0 for both cases here.
1732 //
1733 // Note however that some methods (Float.equal, Float.compare,
1734 // Float.compareTo, Double.equal, Double.compare,
1735 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1736 // StrictMath.min) consider 0.0 to be (strictly) greater than
1737 // -0.0. So if we ever translate calls to these methods into a
1738 // HCompare instruction, we must handle the -0.0 case with
1739 // care here.
1740 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1741 if (type == Primitive::kPrimFloat) {
1742 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1743 } else {
1744 DCHECK_EQ(type, Primitive::kPrimDouble);
1745 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1746 }
1747 } else {
1748 if (type == Primitive::kPrimFloat) {
1749 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1750 } else {
1751 DCHECK_EQ(type, Primitive::kPrimDouble);
1752 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1753 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1754 }
1755 }
1756}
1757
Roland Levillain4fa13f62015-07-06 18:11:54 +01001758void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1759 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001760 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001761 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001762 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001763}
1764
1765void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1766 Label* true_label,
1767 Label* false_label) {
1768 LocationSummary* locations = cond->GetLocations();
1769 Location left = locations->InAt(0);
1770 Location right = locations->InAt(1);
1771 IfCondition if_cond = cond->GetCondition();
1772
1773 Register left_high = left.AsRegisterPairHigh<Register>();
1774 Register left_low = left.AsRegisterPairLow<Register>();
1775 IfCondition true_high_cond = if_cond;
1776 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001777 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001778
1779 // Set the conditions for the test, remembering that == needs to be
1780 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001781 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001782 switch (if_cond) {
1783 case kCondEQ:
1784 case kCondNE:
1785 // Nothing to do.
1786 break;
1787 case kCondLT:
1788 false_high_cond = kCondGT;
1789 break;
1790 case kCondLE:
1791 true_high_cond = kCondLT;
1792 break;
1793 case kCondGT:
1794 false_high_cond = kCondLT;
1795 break;
1796 case kCondGE:
1797 true_high_cond = kCondGT;
1798 break;
Aart Bike9f37602015-10-09 11:15:55 -07001799 case kCondB:
1800 false_high_cond = kCondA;
1801 break;
1802 case kCondBE:
1803 true_high_cond = kCondB;
1804 break;
1805 case kCondA:
1806 false_high_cond = kCondB;
1807 break;
1808 case kCondAE:
1809 true_high_cond = kCondA;
1810 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001811 }
1812 if (right.IsConstant()) {
1813 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1814 int32_t val_low = Low32Bits(value);
1815 int32_t val_high = High32Bits(value);
1816
Vladimir Markoac6ac102015-12-17 12:14:00 +00001817 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001818 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001819 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001820 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001821 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001822 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001823 __ b(true_label, ARMCondition(true_high_cond));
1824 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001825 }
1826 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001827 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001828 } else {
1829 Register right_high = right.AsRegisterPairHigh<Register>();
1830 Register right_low = right.AsRegisterPairLow<Register>();
1831
1832 __ cmp(left_high, ShifterOperand(right_high));
1833 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001834 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001835 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001836 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001837 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001838 __ b(true_label, ARMCondition(true_high_cond));
1839 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001840 }
1841 // Must be equal high, so compare the lows.
1842 __ cmp(left_low, ShifterOperand(right_low));
1843 }
1844 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001845 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001846 __ b(true_label, final_condition);
1847}
1848
David Brazdil0debae72015-11-12 18:37:00 +00001849void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1850 Label* true_target_in,
1851 Label* false_target_in) {
1852 // Generated branching requires both targets to be explicit. If either of the
1853 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1854 Label fallthrough_target;
1855 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1856 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1857
Roland Levillain4fa13f62015-07-06 18:11:54 +01001858 Primitive::Type type = condition->InputAt(0)->GetType();
1859 switch (type) {
1860 case Primitive::kPrimLong:
1861 GenerateLongComparesAndJumps(condition, true_target, false_target);
1862 break;
1863 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001864 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001865 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001866 GenerateFPJumps(condition, true_target, false_target);
1867 break;
1868 default:
1869 LOG(FATAL) << "Unexpected compare type " << type;
1870 }
1871
David Brazdil0debae72015-11-12 18:37:00 +00001872 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001873 __ b(false_target);
1874 }
David Brazdil0debae72015-11-12 18:37:00 +00001875
1876 if (fallthrough_target.IsLinked()) {
1877 __ Bind(&fallthrough_target);
1878 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001879}
1880
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001881void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001882 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001883 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001884 Label* false_target) {
1885 HInstruction* cond = instruction->InputAt(condition_input_index);
1886
1887 if (true_target == nullptr && false_target == nullptr) {
1888 // Nothing to do. The code always falls through.
1889 return;
1890 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001891 // Constant condition, statically compared against "true" (integer value 1).
1892 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001893 if (true_target != nullptr) {
1894 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001895 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001896 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001897 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001898 if (false_target != nullptr) {
1899 __ b(false_target);
1900 }
1901 }
1902 return;
1903 }
1904
1905 // The following code generates these patterns:
1906 // (1) true_target == nullptr && false_target != nullptr
1907 // - opposite condition true => branch to false_target
1908 // (2) true_target != nullptr && false_target == nullptr
1909 // - condition true => branch to true_target
1910 // (3) true_target != nullptr && false_target != nullptr
1911 // - condition true => branch to true_target
1912 // - branch to false_target
1913 if (IsBooleanValueOrMaterializedCondition(cond)) {
1914 // Condition has been materialized, compare the output to 0.
1915 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1916 DCHECK(cond_val.IsRegister());
1917 if (true_target == nullptr) {
1918 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1919 } else {
1920 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001921 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001922 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001923 // Condition has not been materialized. Use its inputs as the comparison and
1924 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001925 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001926
1927 // If this is a long or FP comparison that has been folded into
1928 // the HCondition, generate the comparison directly.
1929 Primitive::Type type = condition->InputAt(0)->GetType();
1930 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1931 GenerateCompareTestAndBranch(condition, true_target, false_target);
1932 return;
1933 }
1934
1935 LocationSummary* locations = cond->GetLocations();
1936 DCHECK(locations->InAt(0).IsRegister());
1937 Register left = locations->InAt(0).AsRegister<Register>();
1938 Location right = locations->InAt(1);
1939 if (right.IsRegister()) {
1940 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001941 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001942 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001943 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001944 }
1945 if (true_target == nullptr) {
1946 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1947 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001948 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001949 }
Dave Allison20dfc792014-06-16 20:44:29 -07001950 }
David Brazdil0debae72015-11-12 18:37:00 +00001951
1952 // If neither branch falls through (case 3), the conditional branch to `true_target`
1953 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1954 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001955 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001956 }
1957}
1958
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001959void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001960 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1961 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001962 locations->SetInAt(0, Location::RequiresRegister());
1963 }
1964}
1965
1966void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001967 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1968 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1969 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1970 nullptr : codegen_->GetLabelOf(true_successor);
1971 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1972 nullptr : codegen_->GetLabelOf(false_successor);
1973 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001974}
1975
1976void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1977 LocationSummary* locations = new (GetGraph()->GetArena())
1978 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001979 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001980 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001981 locations->SetInAt(0, Location::RequiresRegister());
1982 }
1983}
1984
1985void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001986 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001987 GenerateTestAndBranch(deoptimize,
1988 /* condition_input_index */ 0,
1989 slow_path->GetEntryLabel(),
1990 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001991}
Dave Allison20dfc792014-06-16 20:44:29 -07001992
Mingyao Yang063fc772016-08-02 11:02:54 -07001993void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1994 LocationSummary* locations = new (GetGraph()->GetArena())
1995 LocationSummary(flag, LocationSummary::kNoCall);
1996 locations->SetOut(Location::RequiresRegister());
1997}
1998
1999void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2000 __ LoadFromOffset(kLoadWord,
2001 flag->GetLocations()->Out().AsRegister<Register>(),
2002 SP,
2003 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2004}
2005
David Brazdil74eb1b22015-12-14 11:44:01 +00002006void LocationsBuilderARM::VisitSelect(HSelect* select) {
2007 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2008 if (Primitive::IsFloatingPointType(select->GetType())) {
2009 locations->SetInAt(0, Location::RequiresFpuRegister());
2010 locations->SetInAt(1, Location::RequiresFpuRegister());
2011 } else {
2012 locations->SetInAt(0, Location::RequiresRegister());
2013 locations->SetInAt(1, Location::RequiresRegister());
2014 }
2015 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2016 locations->SetInAt(2, Location::RequiresRegister());
2017 }
2018 locations->SetOut(Location::SameAsFirstInput());
2019}
2020
2021void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
2022 LocationSummary* locations = select->GetLocations();
2023 Label false_target;
2024 GenerateTestAndBranch(select,
2025 /* condition_input_index */ 2,
2026 /* true_target */ nullptr,
2027 &false_target);
2028 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2029 __ Bind(&false_target);
2030}
2031
David Srbecky0cf44932015-12-09 14:09:59 +00002032void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2033 new (GetGraph()->GetArena()) LocationSummary(info);
2034}
2035
David Srbeckyd28f4a02016-03-14 17:14:24 +00002036void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2037 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002038}
2039
2040void CodeGeneratorARM::GenerateNop() {
2041 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002042}
2043
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002044void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002045 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002046 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002047 // Handle the long/FP comparisons made in instruction simplification.
2048 switch (cond->InputAt(0)->GetType()) {
2049 case Primitive::kPrimLong:
2050 locations->SetInAt(0, Location::RequiresRegister());
2051 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002052 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002053 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2054 }
2055 break;
2056
2057 case Primitive::kPrimFloat:
2058 case Primitive::kPrimDouble:
2059 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002060 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002061 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002062 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2063 }
2064 break;
2065
2066 default:
2067 locations->SetInAt(0, Location::RequiresRegister());
2068 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002069 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002070 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2071 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002072 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002073}
2074
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002075void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002076 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002077 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002078 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002079
2080 LocationSummary* locations = cond->GetLocations();
2081 Location left = locations->InAt(0);
2082 Location right = locations->InAt(1);
2083 Register out = locations->Out().AsRegister<Register>();
2084 Label true_label, false_label;
2085
2086 switch (cond->InputAt(0)->GetType()) {
2087 default: {
2088 // Integer case.
2089 if (right.IsRegister()) {
2090 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2091 } else {
2092 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002093 __ CmpConstant(left.AsRegister<Register>(),
2094 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002095 }
Aart Bike9f37602015-10-09 11:15:55 -07002096 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002097 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002098 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002099 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002100 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002101 return;
2102 }
2103 case Primitive::kPrimLong:
2104 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2105 break;
2106 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002107 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002108 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002109 GenerateFPJumps(cond, &true_label, &false_label);
2110 break;
2111 }
2112
2113 // Convert the jumps into the result.
2114 Label done_label;
2115
2116 // False case: result = 0.
2117 __ Bind(&false_label);
2118 __ LoadImmediate(out, 0);
2119 __ b(&done_label);
2120
2121 // True case: result = 1.
2122 __ Bind(&true_label);
2123 __ LoadImmediate(out, 1);
2124 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002125}
2126
2127void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002128 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002129}
2130
2131void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002132 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002133}
2134
2135void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002136 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002137}
2138
2139void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002140 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002141}
2142
2143void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002144 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002145}
2146
2147void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002148 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002149}
2150
2151void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002152 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002153}
2154
2155void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002156 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002157}
2158
2159void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002160 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002161}
2162
2163void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002164 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002165}
2166
2167void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002168 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002169}
2170
2171void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002172 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002173}
2174
Aart Bike9f37602015-10-09 11:15:55 -07002175void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002176 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002177}
2178
2179void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002180 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002181}
2182
2183void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002184 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002185}
2186
2187void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002188 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002189}
2190
2191void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002192 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002193}
2194
2195void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002196 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002197}
2198
2199void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002200 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002201}
2202
2203void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002204 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002205}
2206
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002207void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002208 LocationSummary* locations =
2209 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002210 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002211}
2212
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002213void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002214 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002215}
2216
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002217void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2218 LocationSummary* locations =
2219 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2220 locations->SetOut(Location::ConstantLocation(constant));
2221}
2222
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002223void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002224 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002225}
2226
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002227void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002228 LocationSummary* locations =
2229 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002230 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002231}
2232
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002233void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002234 // Will be generated at use site.
2235}
2236
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002237void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2238 LocationSummary* locations =
2239 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2240 locations->SetOut(Location::ConstantLocation(constant));
2241}
2242
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002243void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002244 // Will be generated at use site.
2245}
2246
2247void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2248 LocationSummary* locations =
2249 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2250 locations->SetOut(Location::ConstantLocation(constant));
2251}
2252
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002253void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002254 // Will be generated at use site.
2255}
2256
Calin Juravle27df7582015-04-17 19:12:31 +01002257void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2258 memory_barrier->SetLocations(nullptr);
2259}
2260
2261void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002262 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002263}
2264
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002265void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002266 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002267}
2268
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002269void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002270 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002271}
2272
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002273void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002274 LocationSummary* locations =
2275 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002276 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002277}
2278
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002279void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002280 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002281}
2282
Calin Juravle175dc732015-08-25 15:42:32 +01002283void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2284 // The trampoline uses the same calling convention as dex calling conventions,
2285 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2286 // the method_idx.
2287 HandleInvoke(invoke);
2288}
2289
2290void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2291 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2292}
2293
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002294void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002295 // Explicit clinit checks triggered by static invokes must have been pruned by
2296 // art::PrepareForRegisterAllocation.
2297 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002298
Vladimir Marko68c981f2016-08-26 13:13:33 +01002299 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002300 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002301 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2302 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2303 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002304 return;
2305 }
2306
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002307 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002308
2309 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2310 if (invoke->HasPcRelativeDexCache()) {
2311 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2312 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002313}
2314
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002315static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2316 if (invoke->GetLocations()->Intrinsified()) {
2317 IntrinsicCodeGeneratorARM intrinsic(codegen);
2318 intrinsic.Dispatch(invoke);
2319 return true;
2320 }
2321 return false;
2322}
2323
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002324void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002325 // Explicit clinit checks triggered by static invokes must have been pruned by
2326 // art::PrepareForRegisterAllocation.
2327 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002328
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002329 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2330 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002331 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002332
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002333 LocationSummary* locations = invoke->GetLocations();
2334 codegen_->GenerateStaticOrDirectCall(
2335 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002336 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002337}
2338
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002339void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002340 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002341 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002342}
2343
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002344void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002345 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002346 if (intrinsic.TryDispatch(invoke)) {
2347 return;
2348 }
2349
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002350 HandleInvoke(invoke);
2351}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002352
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002353void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002354 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2355 return;
2356 }
2357
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002358 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002359 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002360 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002361}
2362
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002363void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2364 HandleInvoke(invoke);
2365 // Add the hidden argument.
2366 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2367}
2368
2369void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2370 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002371 LocationSummary* locations = invoke->GetLocations();
2372 Register temp = locations->GetTemp(0).AsRegister<Register>();
2373 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002374 Location receiver = locations->InAt(0);
2375 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2376
Roland Levillain3b359c72015-11-17 19:35:12 +00002377 // Set the hidden argument. This is safe to do this here, as R12
2378 // won't be modified thereafter, before the `blx` (call) instruction.
2379 DCHECK_EQ(R12, hidden_reg);
2380 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002381
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002382 if (receiver.IsStackSlot()) {
2383 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002384 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002385 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2386 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002387 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002388 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002389 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002390 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002391 // Instead of simply (possibly) unpoisoning `temp` here, we should
2392 // emit a read barrier for the previous class reference load.
2393 // However this is not required in practice, as this is an
2394 // intermediate/temporary reference and because the current
2395 // concurrent copying collector keeps the from-space memory
2396 // intact/accessible until the end of the marking phase (the
2397 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002398 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002399 __ LoadFromOffset(kLoadWord, temp, temp,
2400 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2401 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002402 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002403 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002404 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002405 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002406 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002407 // LR = temp->GetEntryPoint();
2408 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2409 // LR();
2410 __ blx(LR);
2411 DCHECK(!codegen_->IsLeafMethod());
2412 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2413}
2414
Orion Hodsonac141392017-01-13 11:53:47 +00002415void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2416 HandleInvoke(invoke);
2417}
2418
2419void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2420 codegen_->GenerateInvokePolymorphicCall(invoke);
2421}
2422
Roland Levillain88cb1752014-10-20 16:36:47 +01002423void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2424 LocationSummary* locations =
2425 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2426 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002427 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002428 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002429 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2430 break;
2431 }
2432 case Primitive::kPrimLong: {
2433 locations->SetInAt(0, Location::RequiresRegister());
2434 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002435 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002436 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002437
Roland Levillain88cb1752014-10-20 16:36:47 +01002438 case Primitive::kPrimFloat:
2439 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002440 locations->SetInAt(0, Location::RequiresFpuRegister());
2441 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002442 break;
2443
2444 default:
2445 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2446 }
2447}
2448
2449void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2450 LocationSummary* locations = neg->GetLocations();
2451 Location out = locations->Out();
2452 Location in = locations->InAt(0);
2453 switch (neg->GetResultType()) {
2454 case Primitive::kPrimInt:
2455 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002456 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002457 break;
2458
2459 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002460 DCHECK(in.IsRegisterPair());
2461 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2462 __ rsbs(out.AsRegisterPairLow<Register>(),
2463 in.AsRegisterPairLow<Register>(),
2464 ShifterOperand(0));
2465 // We cannot emit an RSC (Reverse Subtract with Carry)
2466 // instruction here, as it does not exist in the Thumb-2
2467 // instruction set. We use the following approach
2468 // using SBC and SUB instead.
2469 //
2470 // out.hi = -C
2471 __ sbc(out.AsRegisterPairHigh<Register>(),
2472 out.AsRegisterPairHigh<Register>(),
2473 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2474 // out.hi = out.hi - in.hi
2475 __ sub(out.AsRegisterPairHigh<Register>(),
2476 out.AsRegisterPairHigh<Register>(),
2477 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2478 break;
2479
Roland Levillain88cb1752014-10-20 16:36:47 +01002480 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002481 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002482 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002483 break;
2484
Roland Levillain88cb1752014-10-20 16:36:47 +01002485 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002486 DCHECK(in.IsFpuRegisterPair());
2487 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2488 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002489 break;
2490
2491 default:
2492 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2493 }
2494}
2495
Roland Levillaindff1f282014-11-05 14:15:05 +00002496void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002497 Primitive::Type result_type = conversion->GetResultType();
2498 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002499 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002500
Roland Levillain5b3ee562015-04-14 16:02:41 +01002501 // The float-to-long, double-to-long and long-to-float type conversions
2502 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002503 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002504 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2505 && result_type == Primitive::kPrimLong)
2506 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002507 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002508 : LocationSummary::kNoCall;
2509 LocationSummary* locations =
2510 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2511
David Brazdilb2bd1c52015-03-25 11:17:37 +00002512 // The Java language does not allow treating boolean as an integral type but
2513 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002514
Roland Levillaindff1f282014-11-05 14:15:05 +00002515 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002516 case Primitive::kPrimByte:
2517 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002518 case Primitive::kPrimLong:
2519 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002520 case Primitive::kPrimBoolean:
2521 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002522 case Primitive::kPrimShort:
2523 case Primitive::kPrimInt:
2524 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002525 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002526 locations->SetInAt(0, Location::RequiresRegister());
2527 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2528 break;
2529
2530 default:
2531 LOG(FATAL) << "Unexpected type conversion from " << input_type
2532 << " to " << result_type;
2533 }
2534 break;
2535
Roland Levillain01a8d712014-11-14 16:27:39 +00002536 case Primitive::kPrimShort:
2537 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002538 case Primitive::kPrimLong:
2539 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002540 case Primitive::kPrimBoolean:
2541 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002542 case Primitive::kPrimByte:
2543 case Primitive::kPrimInt:
2544 case Primitive::kPrimChar:
2545 // Processing a Dex `int-to-short' instruction.
2546 locations->SetInAt(0, Location::RequiresRegister());
2547 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2548 break;
2549
2550 default:
2551 LOG(FATAL) << "Unexpected type conversion from " << input_type
2552 << " to " << result_type;
2553 }
2554 break;
2555
Roland Levillain946e1432014-11-11 17:35:19 +00002556 case Primitive::kPrimInt:
2557 switch (input_type) {
2558 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002559 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002560 locations->SetInAt(0, Location::Any());
2561 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2562 break;
2563
2564 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002565 // Processing a Dex `float-to-int' instruction.
2566 locations->SetInAt(0, Location::RequiresFpuRegister());
2567 locations->SetOut(Location::RequiresRegister());
2568 locations->AddTemp(Location::RequiresFpuRegister());
2569 break;
2570
Roland Levillain946e1432014-11-11 17:35:19 +00002571 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002572 // Processing a Dex `double-to-int' instruction.
2573 locations->SetInAt(0, Location::RequiresFpuRegister());
2574 locations->SetOut(Location::RequiresRegister());
2575 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002576 break;
2577
2578 default:
2579 LOG(FATAL) << "Unexpected type conversion from " << input_type
2580 << " to " << result_type;
2581 }
2582 break;
2583
Roland Levillaindff1f282014-11-05 14:15:05 +00002584 case Primitive::kPrimLong:
2585 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002586 case Primitive::kPrimBoolean:
2587 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002588 case Primitive::kPrimByte:
2589 case Primitive::kPrimShort:
2590 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002591 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002592 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002593 locations->SetInAt(0, Location::RequiresRegister());
2594 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2595 break;
2596
Roland Levillain624279f2014-12-04 11:54:28 +00002597 case Primitive::kPrimFloat: {
2598 // Processing a Dex `float-to-long' instruction.
2599 InvokeRuntimeCallingConvention calling_convention;
2600 locations->SetInAt(0, Location::FpuRegisterLocation(
2601 calling_convention.GetFpuRegisterAt(0)));
2602 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2603 break;
2604 }
2605
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002606 case Primitive::kPrimDouble: {
2607 // Processing a Dex `double-to-long' instruction.
2608 InvokeRuntimeCallingConvention calling_convention;
2609 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2610 calling_convention.GetFpuRegisterAt(0),
2611 calling_convention.GetFpuRegisterAt(1)));
2612 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002613 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002614 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002615
2616 default:
2617 LOG(FATAL) << "Unexpected type conversion from " << input_type
2618 << " to " << result_type;
2619 }
2620 break;
2621
Roland Levillain981e4542014-11-14 11:47:14 +00002622 case Primitive::kPrimChar:
2623 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002624 case Primitive::kPrimLong:
2625 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002626 case Primitive::kPrimBoolean:
2627 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002628 case Primitive::kPrimByte:
2629 case Primitive::kPrimShort:
2630 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002631 // Processing a Dex `int-to-char' instruction.
2632 locations->SetInAt(0, Location::RequiresRegister());
2633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2634 break;
2635
2636 default:
2637 LOG(FATAL) << "Unexpected type conversion from " << input_type
2638 << " to " << result_type;
2639 }
2640 break;
2641
Roland Levillaindff1f282014-11-05 14:15:05 +00002642 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002643 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002644 case Primitive::kPrimBoolean:
2645 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002646 case Primitive::kPrimByte:
2647 case Primitive::kPrimShort:
2648 case Primitive::kPrimInt:
2649 case Primitive::kPrimChar:
2650 // Processing a Dex `int-to-float' instruction.
2651 locations->SetInAt(0, Location::RequiresRegister());
2652 locations->SetOut(Location::RequiresFpuRegister());
2653 break;
2654
Roland Levillain5b3ee562015-04-14 16:02:41 +01002655 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002656 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002657 InvokeRuntimeCallingConvention calling_convention;
2658 locations->SetInAt(0, Location::RegisterPairLocation(
2659 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2660 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002661 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002662 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002663
Roland Levillaincff13742014-11-17 14:32:17 +00002664 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002665 // Processing a Dex `double-to-float' instruction.
2666 locations->SetInAt(0, Location::RequiresFpuRegister());
2667 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002668 break;
2669
2670 default:
2671 LOG(FATAL) << "Unexpected type conversion from " << input_type
2672 << " to " << result_type;
2673 };
2674 break;
2675
Roland Levillaindff1f282014-11-05 14:15:05 +00002676 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002677 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002678 case Primitive::kPrimBoolean:
2679 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002680 case Primitive::kPrimByte:
2681 case Primitive::kPrimShort:
2682 case Primitive::kPrimInt:
2683 case Primitive::kPrimChar:
2684 // Processing a Dex `int-to-double' instruction.
2685 locations->SetInAt(0, Location::RequiresRegister());
2686 locations->SetOut(Location::RequiresFpuRegister());
2687 break;
2688
2689 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002690 // Processing a Dex `long-to-double' instruction.
2691 locations->SetInAt(0, Location::RequiresRegister());
2692 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002693 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002694 locations->AddTemp(Location::RequiresFpuRegister());
2695 break;
2696
Roland Levillaincff13742014-11-17 14:32:17 +00002697 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002698 // Processing a Dex `float-to-double' instruction.
2699 locations->SetInAt(0, Location::RequiresFpuRegister());
2700 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002701 break;
2702
2703 default:
2704 LOG(FATAL) << "Unexpected type conversion from " << input_type
2705 << " to " << result_type;
2706 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002707 break;
2708
2709 default:
2710 LOG(FATAL) << "Unexpected type conversion from " << input_type
2711 << " to " << result_type;
2712 }
2713}
2714
2715void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2716 LocationSummary* locations = conversion->GetLocations();
2717 Location out = locations->Out();
2718 Location in = locations->InAt(0);
2719 Primitive::Type result_type = conversion->GetResultType();
2720 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002721 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002722 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002723 case Primitive::kPrimByte:
2724 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002725 case Primitive::kPrimLong:
2726 // Type conversion from long to byte is a result of code transformations.
2727 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2728 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002729 case Primitive::kPrimBoolean:
2730 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002731 case Primitive::kPrimShort:
2732 case Primitive::kPrimInt:
2733 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002734 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002735 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002736 break;
2737
2738 default:
2739 LOG(FATAL) << "Unexpected type conversion from " << input_type
2740 << " to " << result_type;
2741 }
2742 break;
2743
Roland Levillain01a8d712014-11-14 16:27:39 +00002744 case Primitive::kPrimShort:
2745 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002746 case Primitive::kPrimLong:
2747 // Type conversion from long to short is a result of code transformations.
2748 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2749 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002750 case Primitive::kPrimBoolean:
2751 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002752 case Primitive::kPrimByte:
2753 case Primitive::kPrimInt:
2754 case Primitive::kPrimChar:
2755 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002756 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002757 break;
2758
2759 default:
2760 LOG(FATAL) << "Unexpected type conversion from " << input_type
2761 << " to " << result_type;
2762 }
2763 break;
2764
Roland Levillain946e1432014-11-11 17:35:19 +00002765 case Primitive::kPrimInt:
2766 switch (input_type) {
2767 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002768 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002769 DCHECK(out.IsRegister());
2770 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002771 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002772 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002773 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002774 } else {
2775 DCHECK(in.IsConstant());
2776 DCHECK(in.GetConstant()->IsLongConstant());
2777 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002778 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002779 }
2780 break;
2781
Roland Levillain3f8f9362014-12-02 17:45:01 +00002782 case Primitive::kPrimFloat: {
2783 // Processing a Dex `float-to-int' instruction.
2784 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002785 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002786 __ vmovrs(out.AsRegister<Register>(), temp);
2787 break;
2788 }
2789
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002790 case Primitive::kPrimDouble: {
2791 // Processing a Dex `double-to-int' instruction.
2792 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002793 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002794 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002795 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002796 }
Roland Levillain946e1432014-11-11 17:35:19 +00002797
2798 default:
2799 LOG(FATAL) << "Unexpected type conversion from " << input_type
2800 << " to " << result_type;
2801 }
2802 break;
2803
Roland Levillaindff1f282014-11-05 14:15:05 +00002804 case Primitive::kPrimLong:
2805 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002806 case Primitive::kPrimBoolean:
2807 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002808 case Primitive::kPrimByte:
2809 case Primitive::kPrimShort:
2810 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002811 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002812 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002813 DCHECK(out.IsRegisterPair());
2814 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002815 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002816 // Sign extension.
2817 __ Asr(out.AsRegisterPairHigh<Register>(),
2818 out.AsRegisterPairLow<Register>(),
2819 31);
2820 break;
2821
2822 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002823 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002824 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002825 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002826 break;
2827
Roland Levillaindff1f282014-11-05 14:15:05 +00002828 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002829 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002830 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002831 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002832 break;
2833
2834 default:
2835 LOG(FATAL) << "Unexpected type conversion from " << input_type
2836 << " to " << result_type;
2837 }
2838 break;
2839
Roland Levillain981e4542014-11-14 11:47:14 +00002840 case Primitive::kPrimChar:
2841 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002842 case Primitive::kPrimLong:
2843 // Type conversion from long to char is a result of code transformations.
2844 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2845 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002846 case Primitive::kPrimBoolean:
2847 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002848 case Primitive::kPrimByte:
2849 case Primitive::kPrimShort:
2850 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002851 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002852 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002853 break;
2854
2855 default:
2856 LOG(FATAL) << "Unexpected type conversion from " << input_type
2857 << " to " << result_type;
2858 }
2859 break;
2860
Roland Levillaindff1f282014-11-05 14:15:05 +00002861 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002862 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002863 case Primitive::kPrimBoolean:
2864 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002865 case Primitive::kPrimByte:
2866 case Primitive::kPrimShort:
2867 case Primitive::kPrimInt:
2868 case Primitive::kPrimChar: {
2869 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002870 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2871 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002872 break;
2873 }
2874
Roland Levillain5b3ee562015-04-14 16:02:41 +01002875 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002876 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002877 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002878 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002879 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002880
Roland Levillaincff13742014-11-17 14:32:17 +00002881 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002882 // Processing a Dex `double-to-float' instruction.
2883 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2884 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002885 break;
2886
2887 default:
2888 LOG(FATAL) << "Unexpected type conversion from " << input_type
2889 << " to " << result_type;
2890 };
2891 break;
2892
Roland Levillaindff1f282014-11-05 14:15:05 +00002893 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002894 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002895 case Primitive::kPrimBoolean:
2896 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002897 case Primitive::kPrimByte:
2898 case Primitive::kPrimShort:
2899 case Primitive::kPrimInt:
2900 case Primitive::kPrimChar: {
2901 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002902 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002903 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2904 out.AsFpuRegisterPairLow<SRegister>());
2905 break;
2906 }
2907
Roland Levillain647b9ed2014-11-27 12:06:00 +00002908 case Primitive::kPrimLong: {
2909 // Processing a Dex `long-to-double' instruction.
2910 Register low = in.AsRegisterPairLow<Register>();
2911 Register high = in.AsRegisterPairHigh<Register>();
2912 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2913 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002914 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002915 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002916 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2917 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002918
Roland Levillain682393c2015-04-14 15:57:52 +01002919 // temp_d = int-to-double(high)
2920 __ vmovsr(temp_s, high);
2921 __ vcvtdi(temp_d, temp_s);
2922 // constant_d = k2Pow32EncodingForDouble
2923 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2924 // out_d = unsigned-to-double(low)
2925 __ vmovsr(out_s, low);
2926 __ vcvtdu(out_d, out_s);
2927 // out_d += temp_d * constant_d
2928 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002929 break;
2930 }
2931
Roland Levillaincff13742014-11-17 14:32:17 +00002932 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002933 // Processing a Dex `float-to-double' instruction.
2934 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2935 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002936 break;
2937
2938 default:
2939 LOG(FATAL) << "Unexpected type conversion from " << input_type
2940 << " to " << result_type;
2941 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002942 break;
2943
2944 default:
2945 LOG(FATAL) << "Unexpected type conversion from " << input_type
2946 << " to " << result_type;
2947 }
2948}
2949
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002950void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002951 LocationSummary* locations =
2952 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002953 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002954 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002955 locations->SetInAt(0, Location::RequiresRegister());
2956 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002957 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2958 break;
2959 }
2960
2961 case Primitive::kPrimLong: {
2962 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002963 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002965 break;
2966 }
2967
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002968 case Primitive::kPrimFloat:
2969 case Primitive::kPrimDouble: {
2970 locations->SetInAt(0, Location::RequiresFpuRegister());
2971 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002972 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002973 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002974 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002975
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002976 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002977 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002978 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002979}
2980
2981void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2982 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002983 Location out = locations->Out();
2984 Location first = locations->InAt(0);
2985 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002986 switch (add->GetResultType()) {
2987 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002988 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002989 __ add(out.AsRegister<Register>(),
2990 first.AsRegister<Register>(),
2991 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002992 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002993 __ AddConstant(out.AsRegister<Register>(),
2994 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002995 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002996 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002997 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002998
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002999 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003000 if (second.IsConstant()) {
3001 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3002 GenerateAddLongConst(out, first, value);
3003 } else {
3004 DCHECK(second.IsRegisterPair());
3005 __ adds(out.AsRegisterPairLow<Register>(),
3006 first.AsRegisterPairLow<Register>(),
3007 ShifterOperand(second.AsRegisterPairLow<Register>()));
3008 __ adc(out.AsRegisterPairHigh<Register>(),
3009 first.AsRegisterPairHigh<Register>(),
3010 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3011 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003012 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003013 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003014
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003015 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00003016 __ vadds(out.AsFpuRegister<SRegister>(),
3017 first.AsFpuRegister<SRegister>(),
3018 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003019 break;
3020
3021 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003022 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3023 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3024 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003025 break;
3026
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003027 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003028 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003029 }
3030}
3031
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003032void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003033 LocationSummary* locations =
3034 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003035 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003036 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003037 locations->SetInAt(0, Location::RequiresRegister());
3038 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003039 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3040 break;
3041 }
3042
3043 case Primitive::kPrimLong: {
3044 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003045 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003046 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003047 break;
3048 }
Calin Juravle11351682014-10-23 15:38:15 +01003049 case Primitive::kPrimFloat:
3050 case Primitive::kPrimDouble: {
3051 locations->SetInAt(0, Location::RequiresFpuRegister());
3052 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003053 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003054 break;
Calin Juravle11351682014-10-23 15:38:15 +01003055 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003056 default:
Calin Juravle11351682014-10-23 15:38:15 +01003057 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003058 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003059}
3060
3061void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3062 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003063 Location out = locations->Out();
3064 Location first = locations->InAt(0);
3065 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003066 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003067 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003068 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003069 __ sub(out.AsRegister<Register>(),
3070 first.AsRegister<Register>(),
3071 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003072 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003073 __ AddConstant(out.AsRegister<Register>(),
3074 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003075 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003076 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003077 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003078 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003079
Calin Juravle11351682014-10-23 15:38:15 +01003080 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003081 if (second.IsConstant()) {
3082 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3083 GenerateAddLongConst(out, first, -value);
3084 } else {
3085 DCHECK(second.IsRegisterPair());
3086 __ subs(out.AsRegisterPairLow<Register>(),
3087 first.AsRegisterPairLow<Register>(),
3088 ShifterOperand(second.AsRegisterPairLow<Register>()));
3089 __ sbc(out.AsRegisterPairHigh<Register>(),
3090 first.AsRegisterPairHigh<Register>(),
3091 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3092 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003093 break;
Calin Juravle11351682014-10-23 15:38:15 +01003094 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003095
Calin Juravle11351682014-10-23 15:38:15 +01003096 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003097 __ vsubs(out.AsFpuRegister<SRegister>(),
3098 first.AsFpuRegister<SRegister>(),
3099 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003100 break;
Calin Juravle11351682014-10-23 15:38:15 +01003101 }
3102
3103 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003104 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3105 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3106 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003107 break;
3108 }
3109
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003110
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003111 default:
Calin Juravle11351682014-10-23 15:38:15 +01003112 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003113 }
3114}
3115
Calin Juravle34bacdf2014-10-07 20:23:36 +01003116void LocationsBuilderARM::VisitMul(HMul* mul) {
3117 LocationSummary* locations =
3118 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3119 switch (mul->GetResultType()) {
3120 case Primitive::kPrimInt:
3121 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003122 locations->SetInAt(0, Location::RequiresRegister());
3123 locations->SetInAt(1, Location::RequiresRegister());
3124 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003125 break;
3126 }
3127
Calin Juravleb5bfa962014-10-21 18:02:24 +01003128 case Primitive::kPrimFloat:
3129 case Primitive::kPrimDouble: {
3130 locations->SetInAt(0, Location::RequiresFpuRegister());
3131 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003132 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003133 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003134 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003135
3136 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003137 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003138 }
3139}
3140
3141void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3142 LocationSummary* locations = mul->GetLocations();
3143 Location out = locations->Out();
3144 Location first = locations->InAt(0);
3145 Location second = locations->InAt(1);
3146 switch (mul->GetResultType()) {
3147 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003148 __ mul(out.AsRegister<Register>(),
3149 first.AsRegister<Register>(),
3150 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003151 break;
3152 }
3153 case Primitive::kPrimLong: {
3154 Register out_hi = out.AsRegisterPairHigh<Register>();
3155 Register out_lo = out.AsRegisterPairLow<Register>();
3156 Register in1_hi = first.AsRegisterPairHigh<Register>();
3157 Register in1_lo = first.AsRegisterPairLow<Register>();
3158 Register in2_hi = second.AsRegisterPairHigh<Register>();
3159 Register in2_lo = second.AsRegisterPairLow<Register>();
3160
3161 // Extra checks to protect caused by the existence of R1_R2.
3162 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3163 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3164 DCHECK_NE(out_hi, in1_lo);
3165 DCHECK_NE(out_hi, in2_lo);
3166
3167 // input: in1 - 64 bits, in2 - 64 bits
3168 // output: out
3169 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3170 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3171 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3172
3173 // IP <- in1.lo * in2.hi
3174 __ mul(IP, in1_lo, in2_hi);
3175 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3176 __ mla(out_hi, in1_hi, in2_lo, IP);
3177 // out.lo <- (in1.lo * in2.lo)[31:0];
3178 __ umull(out_lo, IP, in1_lo, in2_lo);
3179 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3180 __ add(out_hi, out_hi, ShifterOperand(IP));
3181 break;
3182 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003183
3184 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003185 __ vmuls(out.AsFpuRegister<SRegister>(),
3186 first.AsFpuRegister<SRegister>(),
3187 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003188 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003189 }
3190
3191 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003192 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3193 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3194 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003195 break;
3196 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003197
3198 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003199 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003200 }
3201}
3202
Zheng Xuc6667102015-05-15 16:08:45 +08003203void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3204 DCHECK(instruction->IsDiv() || instruction->IsRem());
3205 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3206
3207 LocationSummary* locations = instruction->GetLocations();
3208 Location second = locations->InAt(1);
3209 DCHECK(second.IsConstant());
3210
3211 Register out = locations->Out().AsRegister<Register>();
3212 Register dividend = locations->InAt(0).AsRegister<Register>();
3213 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3214 DCHECK(imm == 1 || imm == -1);
3215
3216 if (instruction->IsRem()) {
3217 __ LoadImmediate(out, 0);
3218 } else {
3219 if (imm == 1) {
3220 __ Mov(out, dividend);
3221 } else {
3222 __ rsb(out, dividend, ShifterOperand(0));
3223 }
3224 }
3225}
3226
3227void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3228 DCHECK(instruction->IsDiv() || instruction->IsRem());
3229 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3230
3231 LocationSummary* locations = instruction->GetLocations();
3232 Location second = locations->InAt(1);
3233 DCHECK(second.IsConstant());
3234
3235 Register out = locations->Out().AsRegister<Register>();
3236 Register dividend = locations->InAt(0).AsRegister<Register>();
3237 Register temp = locations->GetTemp(0).AsRegister<Register>();
3238 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003239 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003240 int ctz_imm = CTZ(abs_imm);
3241
3242 if (ctz_imm == 1) {
3243 __ Lsr(temp, dividend, 32 - ctz_imm);
3244 } else {
3245 __ Asr(temp, dividend, 31);
3246 __ Lsr(temp, temp, 32 - ctz_imm);
3247 }
3248 __ add(out, temp, ShifterOperand(dividend));
3249
3250 if (instruction->IsDiv()) {
3251 __ Asr(out, out, ctz_imm);
3252 if (imm < 0) {
3253 __ rsb(out, out, ShifterOperand(0));
3254 }
3255 } else {
3256 __ ubfx(out, out, 0, ctz_imm);
3257 __ sub(out, out, ShifterOperand(temp));
3258 }
3259}
3260
3261void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3262 DCHECK(instruction->IsDiv() || instruction->IsRem());
3263 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3264
3265 LocationSummary* locations = instruction->GetLocations();
3266 Location second = locations->InAt(1);
3267 DCHECK(second.IsConstant());
3268
3269 Register out = locations->Out().AsRegister<Register>();
3270 Register dividend = locations->InAt(0).AsRegister<Register>();
3271 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3272 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3273 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3274
3275 int64_t magic;
3276 int shift;
3277 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3278
3279 __ LoadImmediate(temp1, magic);
3280 __ smull(temp2, temp1, dividend, temp1);
3281
3282 if (imm > 0 && magic < 0) {
3283 __ add(temp1, temp1, ShifterOperand(dividend));
3284 } else if (imm < 0 && magic > 0) {
3285 __ sub(temp1, temp1, ShifterOperand(dividend));
3286 }
3287
3288 if (shift != 0) {
3289 __ Asr(temp1, temp1, shift);
3290 }
3291
3292 if (instruction->IsDiv()) {
3293 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3294 } else {
3295 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3296 // TODO: Strength reduction for mls.
3297 __ LoadImmediate(temp2, imm);
3298 __ mls(out, temp1, temp2, dividend);
3299 }
3300}
3301
3302void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3303 DCHECK(instruction->IsDiv() || instruction->IsRem());
3304 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3305
3306 LocationSummary* locations = instruction->GetLocations();
3307 Location second = locations->InAt(1);
3308 DCHECK(second.IsConstant());
3309
3310 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3311 if (imm == 0) {
3312 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3313 } else if (imm == 1 || imm == -1) {
3314 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003315 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003316 DivRemByPowerOfTwo(instruction);
3317 } else {
3318 DCHECK(imm <= -2 || imm >= 2);
3319 GenerateDivRemWithAnyConstant(instruction);
3320 }
3321}
3322
Calin Juravle7c4954d2014-10-28 16:57:40 +00003323void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003324 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3325 if (div->GetResultType() == Primitive::kPrimLong) {
3326 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003327 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003328 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3329 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003330 } else if (div->GetResultType() == Primitive::kPrimInt &&
3331 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3332 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003333 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003334 }
3335
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003336 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3337
Calin Juravle7c4954d2014-10-28 16:57:40 +00003338 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003339 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003340 if (div->InputAt(1)->IsConstant()) {
3341 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003342 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003343 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003344 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3345 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003346 // No temp register required.
3347 } else {
3348 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003349 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003350 locations->AddTemp(Location::RequiresRegister());
3351 }
3352 }
3353 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003354 locations->SetInAt(0, Location::RequiresRegister());
3355 locations->SetInAt(1, Location::RequiresRegister());
3356 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3357 } else {
3358 InvokeRuntimeCallingConvention calling_convention;
3359 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3360 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003361 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003362 // we only need the former.
3363 locations->SetOut(Location::RegisterLocation(R0));
3364 }
Calin Juravled0d48522014-11-04 16:40:20 +00003365 break;
3366 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003367 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003368 InvokeRuntimeCallingConvention calling_convention;
3369 locations->SetInAt(0, Location::RegisterPairLocation(
3370 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3371 locations->SetInAt(1, Location::RegisterPairLocation(
3372 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003373 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003374 break;
3375 }
3376 case Primitive::kPrimFloat:
3377 case Primitive::kPrimDouble: {
3378 locations->SetInAt(0, Location::RequiresFpuRegister());
3379 locations->SetInAt(1, Location::RequiresFpuRegister());
3380 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3381 break;
3382 }
3383
3384 default:
3385 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3386 }
3387}
3388
3389void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3390 LocationSummary* locations = div->GetLocations();
3391 Location out = locations->Out();
3392 Location first = locations->InAt(0);
3393 Location second = locations->InAt(1);
3394
3395 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003396 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003397 if (second.IsConstant()) {
3398 GenerateDivRemConstantIntegral(div);
3399 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003400 __ sdiv(out.AsRegister<Register>(),
3401 first.AsRegister<Register>(),
3402 second.AsRegister<Register>());
3403 } else {
3404 InvokeRuntimeCallingConvention calling_convention;
3405 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3406 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3407 DCHECK_EQ(R0, out.AsRegister<Register>());
3408
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003409 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003410 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003411 }
Calin Juravled0d48522014-11-04 16:40:20 +00003412 break;
3413 }
3414
Calin Juravle7c4954d2014-10-28 16:57:40 +00003415 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003416 InvokeRuntimeCallingConvention calling_convention;
3417 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3418 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3419 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3420 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3421 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003422 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003423
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003424 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003425 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003426 break;
3427 }
3428
3429 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003430 __ vdivs(out.AsFpuRegister<SRegister>(),
3431 first.AsFpuRegister<SRegister>(),
3432 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003433 break;
3434 }
3435
3436 case Primitive::kPrimDouble: {
3437 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3438 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3439 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3440 break;
3441 }
3442
3443 default:
3444 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3445 }
3446}
3447
Calin Juravlebacfec32014-11-14 15:54:36 +00003448void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003449 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003450
3451 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003452 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003453 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3454 // sdiv will be replaced by other instruction sequence.
3455 call_kind = LocationSummary::kNoCall;
3456 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3457 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003458 // Have hardware divide instruction for int, do it with three instructions.
3459 call_kind = LocationSummary::kNoCall;
3460 }
3461
Calin Juravlebacfec32014-11-14 15:54:36 +00003462 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3463
Calin Juravled2ec87d2014-12-08 14:24:46 +00003464 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003465 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003466 if (rem->InputAt(1)->IsConstant()) {
3467 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003468 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003469 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003470 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3471 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003472 // No temp register required.
3473 } else {
3474 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003475 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003476 locations->AddTemp(Location::RequiresRegister());
3477 }
3478 }
3479 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003480 locations->SetInAt(0, Location::RequiresRegister());
3481 locations->SetInAt(1, Location::RequiresRegister());
3482 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3483 locations->AddTemp(Location::RequiresRegister());
3484 } else {
3485 InvokeRuntimeCallingConvention calling_convention;
3486 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3487 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003488 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003489 // we only need the latter.
3490 locations->SetOut(Location::RegisterLocation(R1));
3491 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003492 break;
3493 }
3494 case Primitive::kPrimLong: {
3495 InvokeRuntimeCallingConvention calling_convention;
3496 locations->SetInAt(0, Location::RegisterPairLocation(
3497 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3498 locations->SetInAt(1, Location::RegisterPairLocation(
3499 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3500 // The runtime helper puts the output in R2,R3.
3501 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3502 break;
3503 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003504 case Primitive::kPrimFloat: {
3505 InvokeRuntimeCallingConvention calling_convention;
3506 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3507 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3508 locations->SetOut(Location::FpuRegisterLocation(S0));
3509 break;
3510 }
3511
Calin Juravlebacfec32014-11-14 15:54:36 +00003512 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003513 InvokeRuntimeCallingConvention calling_convention;
3514 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3515 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3516 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3517 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3518 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003519 break;
3520 }
3521
3522 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003523 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003524 }
3525}
3526
3527void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3528 LocationSummary* locations = rem->GetLocations();
3529 Location out = locations->Out();
3530 Location first = locations->InAt(0);
3531 Location second = locations->InAt(1);
3532
Calin Juravled2ec87d2014-12-08 14:24:46 +00003533 Primitive::Type type = rem->GetResultType();
3534 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003535 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003536 if (second.IsConstant()) {
3537 GenerateDivRemConstantIntegral(rem);
3538 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003539 Register reg1 = first.AsRegister<Register>();
3540 Register reg2 = second.AsRegister<Register>();
3541 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003542
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003543 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003544 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003545 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003546 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003547 } else {
3548 InvokeRuntimeCallingConvention calling_convention;
3549 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3550 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3551 DCHECK_EQ(R1, out.AsRegister<Register>());
3552
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003553 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003554 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003555 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003556 break;
3557 }
3558
3559 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003560 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003561 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003562 break;
3563 }
3564
Calin Juravled2ec87d2014-12-08 14:24:46 +00003565 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003566 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003567 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003568 break;
3569 }
3570
Calin Juravlebacfec32014-11-14 15:54:36 +00003571 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003572 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003573 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003574 break;
3575 }
3576
3577 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003578 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003579 }
3580}
3581
Calin Juravled0d48522014-11-04 16:40:20 +00003582void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003583 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003584 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003585}
3586
3587void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003588 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003589 codegen_->AddSlowPath(slow_path);
3590
3591 LocationSummary* locations = instruction->GetLocations();
3592 Location value = locations->InAt(0);
3593
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003594 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003595 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003596 case Primitive::kPrimByte:
3597 case Primitive::kPrimChar:
3598 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003599 case Primitive::kPrimInt: {
3600 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003601 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003602 } else {
3603 DCHECK(value.IsConstant()) << value;
3604 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3605 __ b(slow_path->GetEntryLabel());
3606 }
3607 }
3608 break;
3609 }
3610 case Primitive::kPrimLong: {
3611 if (value.IsRegisterPair()) {
3612 __ orrs(IP,
3613 value.AsRegisterPairLow<Register>(),
3614 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3615 __ b(slow_path->GetEntryLabel(), EQ);
3616 } else {
3617 DCHECK(value.IsConstant()) << value;
3618 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3619 __ b(slow_path->GetEntryLabel());
3620 }
3621 }
3622 break;
3623 default:
3624 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3625 }
3626 }
Calin Juravled0d48522014-11-04 16:40:20 +00003627}
3628
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003629void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3630 Register in = locations->InAt(0).AsRegister<Register>();
3631 Location rhs = locations->InAt(1);
3632 Register out = locations->Out().AsRegister<Register>();
3633
3634 if (rhs.IsConstant()) {
3635 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3636 // so map all rotations to a +ve. equivalent in that range.
3637 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3638 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3639 if (rot) {
3640 // Rotate, mapping left rotations to right equivalents if necessary.
3641 // (e.g. left by 2 bits == right by 30.)
3642 __ Ror(out, in, rot);
3643 } else if (out != in) {
3644 __ Mov(out, in);
3645 }
3646 } else {
3647 __ Ror(out, in, rhs.AsRegister<Register>());
3648 }
3649}
3650
3651// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3652// rotates by swapping input regs (effectively rotating by the first 32-bits of
3653// a larger rotation) or flipping direction (thus treating larger right/left
3654// rotations as sub-word sized rotations in the other direction) as appropriate.
3655void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3656 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3657 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3658 Location rhs = locations->InAt(1);
3659 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3660 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3661
3662 if (rhs.IsConstant()) {
3663 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3664 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003665 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003666 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3667 // logic below to a simple pair of binary orr.
3668 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3669 if (rot >= kArmBitsPerWord) {
3670 rot -= kArmBitsPerWord;
3671 std::swap(in_reg_hi, in_reg_lo);
3672 }
3673 // Rotate, or mov to out for zero or word size rotations.
3674 if (rot != 0u) {
3675 __ Lsr(out_reg_hi, in_reg_hi, rot);
3676 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3677 __ Lsr(out_reg_lo, in_reg_lo, rot);
3678 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3679 } else {
3680 __ Mov(out_reg_lo, in_reg_lo);
3681 __ Mov(out_reg_hi, in_reg_hi);
3682 }
3683 } else {
3684 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3685 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3686 Label end;
3687 Label shift_by_32_plus_shift_right;
3688
3689 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3690 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3691 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3692 __ b(&shift_by_32_plus_shift_right, CC);
3693
3694 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3695 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3696 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3697 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3698 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3699 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3700 __ Lsr(shift_left, in_reg_hi, shift_right);
3701 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3702 __ b(&end);
3703
3704 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3705 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3706 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3707 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3708 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3709 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3710 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3711 __ Lsl(shift_right, in_reg_hi, shift_left);
3712 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3713
3714 __ Bind(&end);
3715 }
3716}
Roland Levillain22c49222016-03-18 14:04:28 +00003717
3718void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003719 LocationSummary* locations =
3720 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3721 switch (ror->GetResultType()) {
3722 case Primitive::kPrimInt: {
3723 locations->SetInAt(0, Location::RequiresRegister());
3724 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3725 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3726 break;
3727 }
3728 case Primitive::kPrimLong: {
3729 locations->SetInAt(0, Location::RequiresRegister());
3730 if (ror->InputAt(1)->IsConstant()) {
3731 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3732 } else {
3733 locations->SetInAt(1, Location::RequiresRegister());
3734 locations->AddTemp(Location::RequiresRegister());
3735 locations->AddTemp(Location::RequiresRegister());
3736 }
3737 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3738 break;
3739 }
3740 default:
3741 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3742 }
3743}
3744
Roland Levillain22c49222016-03-18 14:04:28 +00003745void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003746 LocationSummary* locations = ror->GetLocations();
3747 Primitive::Type type = ror->GetResultType();
3748 switch (type) {
3749 case Primitive::kPrimInt: {
3750 HandleIntegerRotate(locations);
3751 break;
3752 }
3753 case Primitive::kPrimLong: {
3754 HandleLongRotate(locations);
3755 break;
3756 }
3757 default:
3758 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003759 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003760 }
3761}
3762
Calin Juravle9aec02f2014-11-18 23:06:35 +00003763void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3764 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3765
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003766 LocationSummary* locations =
3767 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003768
3769 switch (op->GetResultType()) {
3770 case Primitive::kPrimInt: {
3771 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003772 if (op->InputAt(1)->IsConstant()) {
3773 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3774 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3775 } else {
3776 locations->SetInAt(1, Location::RequiresRegister());
3777 // Make the output overlap, as it will be used to hold the masked
3778 // second input.
3779 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3780 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003781 break;
3782 }
3783 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003784 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003785 if (op->InputAt(1)->IsConstant()) {
3786 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3787 // For simplicity, use kOutputOverlap even though we only require that low registers
3788 // don't clash with high registers which the register allocator currently guarantees.
3789 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3790 } else {
3791 locations->SetInAt(1, Location::RequiresRegister());
3792 locations->AddTemp(Location::RequiresRegister());
3793 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3794 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003795 break;
3796 }
3797 default:
3798 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3799 }
3800}
3801
3802void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3803 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3804
3805 LocationSummary* locations = op->GetLocations();
3806 Location out = locations->Out();
3807 Location first = locations->InAt(0);
3808 Location second = locations->InAt(1);
3809
3810 Primitive::Type type = op->GetResultType();
3811 switch (type) {
3812 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003813 Register out_reg = out.AsRegister<Register>();
3814 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003815 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003816 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003817 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003818 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003819 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003820 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003821 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003822 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003823 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003824 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003825 }
3826 } else {
3827 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003828 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003829 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003830 __ Mov(out_reg, first_reg);
3831 } else if (op->IsShl()) {
3832 __ Lsl(out_reg, first_reg, shift_value);
3833 } else if (op->IsShr()) {
3834 __ Asr(out_reg, first_reg, shift_value);
3835 } else {
3836 __ Lsr(out_reg, first_reg, shift_value);
3837 }
3838 }
3839 break;
3840 }
3841 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003842 Register o_h = out.AsRegisterPairHigh<Register>();
3843 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003844
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003845 Register high = first.AsRegisterPairHigh<Register>();
3846 Register low = first.AsRegisterPairLow<Register>();
3847
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003848 if (second.IsRegister()) {
3849 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003850
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003851 Register second_reg = second.AsRegister<Register>();
3852
3853 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003854 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003855 // Shift the high part
3856 __ Lsl(o_h, high, o_l);
3857 // Shift the low part and `or` what overflew on the high part
3858 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3859 __ Lsr(temp, low, temp);
3860 __ orr(o_h, o_h, ShifterOperand(temp));
3861 // If the shift is > 32 bits, override the high part
3862 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3863 __ it(PL);
3864 __ Lsl(o_h, low, temp, PL);
3865 // Shift the low part
3866 __ Lsl(o_l, low, o_l);
3867 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003868 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003869 // Shift the low part
3870 __ Lsr(o_l, low, o_h);
3871 // Shift the high part and `or` what underflew on the low part
3872 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3873 __ Lsl(temp, high, temp);
3874 __ orr(o_l, o_l, ShifterOperand(temp));
3875 // If the shift is > 32 bits, override the low part
3876 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3877 __ it(PL);
3878 __ Asr(o_l, high, temp, PL);
3879 // Shift the high part
3880 __ Asr(o_h, high, o_h);
3881 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003882 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003883 // same as Shr except we use `Lsr`s and not `Asr`s
3884 __ Lsr(o_l, low, o_h);
3885 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3886 __ Lsl(temp, high, temp);
3887 __ orr(o_l, o_l, ShifterOperand(temp));
3888 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3889 __ it(PL);
3890 __ Lsr(o_l, high, temp, PL);
3891 __ Lsr(o_h, high, o_h);
3892 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003893 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003894 // Register allocator doesn't create partial overlap.
3895 DCHECK_NE(o_l, high);
3896 DCHECK_NE(o_h, low);
3897 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003898 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003899 if (shift_value > 32) {
3900 if (op->IsShl()) {
3901 __ Lsl(o_h, low, shift_value - 32);
3902 __ LoadImmediate(o_l, 0);
3903 } else if (op->IsShr()) {
3904 __ Asr(o_l, high, shift_value - 32);
3905 __ Asr(o_h, high, 31);
3906 } else {
3907 __ Lsr(o_l, high, shift_value - 32);
3908 __ LoadImmediate(o_h, 0);
3909 }
3910 } else if (shift_value == 32) {
3911 if (op->IsShl()) {
3912 __ mov(o_h, ShifterOperand(low));
3913 __ LoadImmediate(o_l, 0);
3914 } else if (op->IsShr()) {
3915 __ mov(o_l, ShifterOperand(high));
3916 __ Asr(o_h, high, 31);
3917 } else {
3918 __ mov(o_l, ShifterOperand(high));
3919 __ LoadImmediate(o_h, 0);
3920 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003921 } else if (shift_value == 1) {
3922 if (op->IsShl()) {
3923 __ Lsls(o_l, low, 1);
3924 __ adc(o_h, high, ShifterOperand(high));
3925 } else if (op->IsShr()) {
3926 __ Asrs(o_h, high, 1);
3927 __ Rrx(o_l, low);
3928 } else {
3929 __ Lsrs(o_h, high, 1);
3930 __ Rrx(o_l, low);
3931 }
3932 } else {
3933 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003934 if (op->IsShl()) {
3935 __ Lsl(o_h, high, shift_value);
3936 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3937 __ Lsl(o_l, low, shift_value);
3938 } else if (op->IsShr()) {
3939 __ Lsr(o_l, low, shift_value);
3940 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3941 __ Asr(o_h, high, shift_value);
3942 } else {
3943 __ Lsr(o_l, low, shift_value);
3944 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3945 __ Lsr(o_h, high, shift_value);
3946 }
3947 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003948 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003949 break;
3950 }
3951 default:
3952 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003953 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003954 }
3955}
3956
3957void LocationsBuilderARM::VisitShl(HShl* shl) {
3958 HandleShift(shl);
3959}
3960
3961void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3962 HandleShift(shl);
3963}
3964
3965void LocationsBuilderARM::VisitShr(HShr* shr) {
3966 HandleShift(shr);
3967}
3968
3969void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3970 HandleShift(shr);
3971}
3972
3973void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3974 HandleShift(ushr);
3975}
3976
3977void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3978 HandleShift(ushr);
3979}
3980
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003981void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003982 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003984 if (instruction->IsStringAlloc()) {
3985 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3986 } else {
3987 InvokeRuntimeCallingConvention calling_convention;
3988 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00003989 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003990 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003991}
3992
3993void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003994 // Note: if heap poisoning is enabled, the entry point takes cares
3995 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003996 if (instruction->IsStringAlloc()) {
3997 // String is allocated through StringFactory. Call NewEmptyString entry point.
3998 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003999 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004000 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4001 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
4002 __ blx(LR);
4003 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4004 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004005 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004006 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004007 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004008}
4009
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004010void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
4011 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004012 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004013 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004014 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004015 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4016 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004017}
4018
4019void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004020 // Note: if heap poisoning is enabled, the entry point takes cares
4021 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004022 QuickEntrypointEnum entrypoint =
4023 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4024 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004025 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004026 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004027}
4028
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004029void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004030 LocationSummary* locations =
4031 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004032 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4033 if (location.IsStackSlot()) {
4034 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4035 } else if (location.IsDoubleStackSlot()) {
4036 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004037 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004038 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004039}
4040
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004041void InstructionCodeGeneratorARM::VisitParameterValue(
4042 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004043 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004044}
4045
4046void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4047 LocationSummary* locations =
4048 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4049 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4050}
4051
4052void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4053 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004054}
4055
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004056void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004057 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004058 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004059 locations->SetInAt(0, Location::RequiresRegister());
4060 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004061}
4062
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004063void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4064 LocationSummary* locations = not_->GetLocations();
4065 Location out = locations->Out();
4066 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004067 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004068 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004069 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004070 break;
4071
4072 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004073 __ mvn(out.AsRegisterPairLow<Register>(),
4074 ShifterOperand(in.AsRegisterPairLow<Register>()));
4075 __ mvn(out.AsRegisterPairHigh<Register>(),
4076 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004077 break;
4078
4079 default:
4080 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4081 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004082}
4083
David Brazdil66d126e2015-04-03 16:02:44 +01004084void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4085 LocationSummary* locations =
4086 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4087 locations->SetInAt(0, Location::RequiresRegister());
4088 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4089}
4090
4091void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004092 LocationSummary* locations = bool_not->GetLocations();
4093 Location out = locations->Out();
4094 Location in = locations->InAt(0);
4095 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4096}
4097
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004098void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004099 LocationSummary* locations =
4100 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004101 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004102 case Primitive::kPrimBoolean:
4103 case Primitive::kPrimByte:
4104 case Primitive::kPrimShort:
4105 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004106 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004107 case Primitive::kPrimLong: {
4108 locations->SetInAt(0, Location::RequiresRegister());
4109 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004110 // Output overlaps because it is written before doing the low comparison.
4111 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004112 break;
4113 }
4114 case Primitive::kPrimFloat:
4115 case Primitive::kPrimDouble: {
4116 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004117 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004118 locations->SetOut(Location::RequiresRegister());
4119 break;
4120 }
4121 default:
4122 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4123 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004124}
4125
4126void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004127 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004128 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004129 Location left = locations->InAt(0);
4130 Location right = locations->InAt(1);
4131
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004132 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004133 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004134 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004135 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004136 case Primitive::kPrimBoolean:
4137 case Primitive::kPrimByte:
4138 case Primitive::kPrimShort:
4139 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004140 case Primitive::kPrimInt: {
4141 __ LoadImmediate(out, 0);
4142 __ cmp(left.AsRegister<Register>(),
4143 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4144 less_cond = LT;
4145 break;
4146 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004147 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004148 __ cmp(left.AsRegisterPairHigh<Register>(),
4149 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004150 __ b(&less, LT);
4151 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004152 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004153 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004154 __ cmp(left.AsRegisterPairLow<Register>(),
4155 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004156 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004157 break;
4158 }
4159 case Primitive::kPrimFloat:
4160 case Primitive::kPrimDouble: {
4161 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004162 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004163 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004164 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004165 break;
4166 }
4167 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004168 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004169 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004170 }
Aart Bika19616e2016-02-01 18:57:58 -08004171
Calin Juravleddb7df22014-11-25 20:56:51 +00004172 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004173 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004174
4175 __ Bind(&greater);
4176 __ LoadImmediate(out, 1);
4177 __ b(&done);
4178
4179 __ Bind(&less);
4180 __ LoadImmediate(out, -1);
4181
4182 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004183}
4184
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004185void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004186 LocationSummary* locations =
4187 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004188 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004189 locations->SetInAt(i, Location::Any());
4190 }
4191 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004192}
4193
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004194void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004195 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004196}
4197
Roland Levillainc9285912015-12-18 10:38:42 +00004198void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4199 // TODO (ported from quick): revisit ARM barrier kinds.
4200 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004201 switch (kind) {
4202 case MemBarrierKind::kAnyStore:
4203 case MemBarrierKind::kLoadAny:
4204 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004205 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004206 break;
4207 }
4208 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004209 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004210 break;
4211 }
4212 default:
4213 LOG(FATAL) << "Unexpected memory barrier " << kind;
4214 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004215 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004216}
4217
4218void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4219 uint32_t offset,
4220 Register out_lo,
4221 Register out_hi) {
4222 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004223 // Ensure `out_lo` is different from `addr`, so that loading
4224 // `offset` into `out_lo` does not clutter `addr`.
4225 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004226 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004227 __ add(IP, addr, ShifterOperand(out_lo));
4228 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004229 }
4230 __ ldrexd(out_lo, out_hi, addr);
4231}
4232
4233void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4234 uint32_t offset,
4235 Register value_lo,
4236 Register value_hi,
4237 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004238 Register temp2,
4239 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004240 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004241 if (offset != 0) {
4242 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004243 __ add(IP, addr, ShifterOperand(temp1));
4244 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004245 }
4246 __ Bind(&fail);
4247 // We need a load followed by store. (The address used in a STREX instruction must
4248 // be the same as the address in the most recently executed LDREX instruction.)
4249 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004250 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004251 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004252 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004253}
4254
4255void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4256 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4257
Nicolas Geoffray39468442014-09-02 15:17:15 +01004258 LocationSummary* locations =
4259 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004260 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004261
Calin Juravle52c48962014-12-16 17:02:57 +00004262 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004263 if (Primitive::IsFloatingPointType(field_type)) {
4264 locations->SetInAt(1, Location::RequiresFpuRegister());
4265 } else {
4266 locations->SetInAt(1, Location::RequiresRegister());
4267 }
4268
Calin Juravle52c48962014-12-16 17:02:57 +00004269 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004270 bool generate_volatile = field_info.IsVolatile()
4271 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004272 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004273 bool needs_write_barrier =
4274 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004275 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004276 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004277 if (needs_write_barrier) {
4278 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004279 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004280 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004281 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004282 // - registers need to be consecutive
4283 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004284 // We don't test for ARM yet, and the assertion makes sure that we
4285 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004286 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4287
4288 locations->AddTemp(Location::RequiresRegister());
4289 locations->AddTemp(Location::RequiresRegister());
4290 if (field_type == Primitive::kPrimDouble) {
4291 // For doubles we need two more registers to copy the value.
4292 locations->AddTemp(Location::RegisterLocation(R2));
4293 locations->AddTemp(Location::RegisterLocation(R3));
4294 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004295 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004296}
4297
Calin Juravle52c48962014-12-16 17:02:57 +00004298void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004299 const FieldInfo& field_info,
4300 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004301 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4302
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004303 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004304 Register base = locations->InAt(0).AsRegister<Register>();
4305 Location value = locations->InAt(1);
4306
4307 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004308 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004309 Primitive::Type field_type = field_info.GetFieldType();
4310 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004311 bool needs_write_barrier =
4312 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004313
4314 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004315 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004316 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004317
4318 switch (field_type) {
4319 case Primitive::kPrimBoolean:
4320 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004321 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004322 break;
4323 }
4324
4325 case Primitive::kPrimShort:
4326 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004327 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004328 break;
4329 }
4330
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004331 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004332 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004333 if (kPoisonHeapReferences && needs_write_barrier) {
4334 // Note that in the case where `value` is a null reference,
4335 // we do not enter this block, as a null reference does not
4336 // need poisoning.
4337 DCHECK_EQ(field_type, Primitive::kPrimNot);
4338 Register temp = locations->GetTemp(0).AsRegister<Register>();
4339 __ Mov(temp, value.AsRegister<Register>());
4340 __ PoisonHeapReference(temp);
4341 __ StoreToOffset(kStoreWord, temp, base, offset);
4342 } else {
4343 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4344 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004345 break;
4346 }
4347
4348 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004349 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004350 GenerateWideAtomicStore(base, offset,
4351 value.AsRegisterPairLow<Register>(),
4352 value.AsRegisterPairHigh<Register>(),
4353 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004354 locations->GetTemp(1).AsRegister<Register>(),
4355 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004356 } else {
4357 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004358 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004359 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004360 break;
4361 }
4362
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004363 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004364 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004365 break;
4366 }
4367
4368 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004369 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004370 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004371 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4372 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4373
4374 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4375
4376 GenerateWideAtomicStore(base, offset,
4377 value_reg_lo,
4378 value_reg_hi,
4379 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004380 locations->GetTemp(3).AsRegister<Register>(),
4381 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004382 } else {
4383 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004384 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004385 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004386 break;
4387 }
4388
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004389 case Primitive::kPrimVoid:
4390 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004391 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004392 }
Calin Juravle52c48962014-12-16 17:02:57 +00004393
Calin Juravle77520bc2015-01-12 18:45:46 +00004394 // Longs and doubles are handled in the switch.
4395 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4396 codegen_->MaybeRecordImplicitNullCheck(instruction);
4397 }
4398
4399 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4400 Register temp = locations->GetTemp(0).AsRegister<Register>();
4401 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004402 codegen_->MarkGCCard(
4403 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004404 }
4405
Calin Juravle52c48962014-12-16 17:02:57 +00004406 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004407 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004408 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004409}
4410
Calin Juravle52c48962014-12-16 17:02:57 +00004411void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4412 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004413
4414 bool object_field_get_with_read_barrier =
4415 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004416 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004417 new (GetGraph()->GetArena()) LocationSummary(instruction,
4418 object_field_get_with_read_barrier ?
4419 LocationSummary::kCallOnSlowPath :
4420 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004421 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004422 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004423 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004424 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004425
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004426 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004427 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004428 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004429 // The output overlaps in case of volatile long: we don't want the
4430 // code generated by GenerateWideAtomicLoad to overwrite the
4431 // object's location. Likewise, in the case of an object field get
4432 // with read barriers enabled, we do not want the load to overwrite
4433 // the object's location, as we need it to emit the read barrier.
4434 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4435 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004436
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004437 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4438 locations->SetOut(Location::RequiresFpuRegister());
4439 } else {
4440 locations->SetOut(Location::RequiresRegister(),
4441 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4442 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004443 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004444 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004445 // - registers need to be consecutive
4446 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004447 // We don't test for ARM yet, and the assertion makes sure that we
4448 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004449 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4450 locations->AddTemp(Location::RequiresRegister());
4451 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004452 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4453 // We need a temporary register for the read barrier marking slow
4454 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4455 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004456 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004457}
4458
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004459Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4460 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4461 << input->GetType();
4462 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4463 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4464 return Location::ConstantLocation(input->AsConstant());
4465 } else {
4466 return Location::RequiresFpuRegister();
4467 }
4468}
4469
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004470Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4471 Opcode opcode) {
4472 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4473 if (constant->IsConstant() &&
4474 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4475 return Location::ConstantLocation(constant->AsConstant());
4476 }
4477 return Location::RequiresRegister();
4478}
4479
4480bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4481 Opcode opcode) {
4482 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4483 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004484 Opcode high_opcode = opcode;
4485 SetCc low_set_cc = kCcDontCare;
4486 switch (opcode) {
4487 case SUB:
4488 // Flip the operation to an ADD.
4489 value = -value;
4490 opcode = ADD;
4491 FALLTHROUGH_INTENDED;
4492 case ADD:
4493 if (Low32Bits(value) == 0u) {
4494 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4495 }
4496 high_opcode = ADC;
4497 low_set_cc = kCcSet;
4498 break;
4499 default:
4500 break;
4501 }
4502 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4503 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004504 } else {
4505 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4506 }
4507}
4508
Vladimir Marko59751a72016-08-05 14:37:27 +01004509bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4510 Opcode opcode,
4511 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004512 ShifterOperand so;
4513 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004514 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004515 return true;
4516 }
4517 Opcode neg_opcode = kNoOperand;
4518 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004519 case AND: neg_opcode = BIC; value = ~value; break;
4520 case ORR: neg_opcode = ORN; value = ~value; break;
4521 case ADD: neg_opcode = SUB; value = -value; break;
4522 case ADC: neg_opcode = SBC; value = ~value; break;
4523 case SUB: neg_opcode = ADD; value = -value; break;
4524 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004525 default:
4526 return false;
4527 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004528 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004529}
4530
Calin Juravle52c48962014-12-16 17:02:57 +00004531void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4532 const FieldInfo& field_info) {
4533 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004534
Calin Juravle52c48962014-12-16 17:02:57 +00004535 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004536 Location base_loc = locations->InAt(0);
4537 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004538 Location out = locations->Out();
4539 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004540 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004541 Primitive::Type field_type = field_info.GetFieldType();
4542 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4543
4544 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004545 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004546 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004547 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004548
Roland Levillainc9285912015-12-18 10:38:42 +00004549 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004550 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004551 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004552
Roland Levillainc9285912015-12-18 10:38:42 +00004553 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004554 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004555 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004556
Roland Levillainc9285912015-12-18 10:38:42 +00004557 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004558 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004559 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004560
4561 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004562 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004563 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004564
4565 case Primitive::kPrimNot: {
4566 // /* HeapReference<Object> */ out = *(base + offset)
4567 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4568 Location temp_loc = locations->GetTemp(0);
4569 // Note that a potential implicit null check is handled in this
4570 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4571 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4572 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4573 if (is_volatile) {
4574 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4575 }
4576 } else {
4577 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4578 codegen_->MaybeRecordImplicitNullCheck(instruction);
4579 if (is_volatile) {
4580 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4581 }
4582 // If read barriers are enabled, emit read barriers other than
4583 // Baker's using a slow path (and also unpoison the loaded
4584 // reference, if heap poisoning is enabled).
4585 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4586 }
4587 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004588 }
4589
Roland Levillainc9285912015-12-18 10:38:42 +00004590 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004591 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004592 GenerateWideAtomicLoad(base, offset,
4593 out.AsRegisterPairLow<Register>(),
4594 out.AsRegisterPairHigh<Register>());
4595 } else {
4596 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4597 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004598 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004599
Roland Levillainc9285912015-12-18 10:38:42 +00004600 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004601 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004602 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004603
4604 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004605 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004606 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004607 Register lo = locations->GetTemp(0).AsRegister<Register>();
4608 Register hi = locations->GetTemp(1).AsRegister<Register>();
4609 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004610 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004611 __ vmovdrr(out_reg, lo, hi);
4612 } else {
4613 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004614 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004615 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004616 break;
4617 }
4618
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004619 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004620 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004621 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004622 }
Calin Juravle52c48962014-12-16 17:02:57 +00004623
Roland Levillainc9285912015-12-18 10:38:42 +00004624 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4625 // Potential implicit null checks, in the case of reference or
4626 // double fields, are handled in the previous switch statement.
4627 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004628 codegen_->MaybeRecordImplicitNullCheck(instruction);
4629 }
4630
Calin Juravle52c48962014-12-16 17:02:57 +00004631 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004632 if (field_type == Primitive::kPrimNot) {
4633 // Memory barriers, in the case of references, are also handled
4634 // in the previous switch statement.
4635 } else {
4636 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4637 }
Roland Levillain4d027112015-07-01 15:41:14 +01004638 }
Calin Juravle52c48962014-12-16 17:02:57 +00004639}
4640
4641void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4642 HandleFieldSet(instruction, instruction->GetFieldInfo());
4643}
4644
4645void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004646 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004647}
4648
4649void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4650 HandleFieldGet(instruction, instruction->GetFieldInfo());
4651}
4652
4653void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4654 HandleFieldGet(instruction, instruction->GetFieldInfo());
4655}
4656
4657void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4658 HandleFieldGet(instruction, instruction->GetFieldInfo());
4659}
4660
4661void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4662 HandleFieldGet(instruction, instruction->GetFieldInfo());
4663}
4664
4665void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4666 HandleFieldSet(instruction, instruction->GetFieldInfo());
4667}
4668
4669void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004670 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004671}
4672
Calin Juravlee460d1d2015-09-29 04:52:17 +01004673void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4674 HUnresolvedInstanceFieldGet* instruction) {
4675 FieldAccessCallingConventionARM calling_convention;
4676 codegen_->CreateUnresolvedFieldLocationSummary(
4677 instruction, instruction->GetFieldType(), calling_convention);
4678}
4679
4680void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4681 HUnresolvedInstanceFieldGet* instruction) {
4682 FieldAccessCallingConventionARM calling_convention;
4683 codegen_->GenerateUnresolvedFieldAccess(instruction,
4684 instruction->GetFieldType(),
4685 instruction->GetFieldIndex(),
4686 instruction->GetDexPc(),
4687 calling_convention);
4688}
4689
4690void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4691 HUnresolvedInstanceFieldSet* instruction) {
4692 FieldAccessCallingConventionARM calling_convention;
4693 codegen_->CreateUnresolvedFieldLocationSummary(
4694 instruction, instruction->GetFieldType(), calling_convention);
4695}
4696
4697void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4698 HUnresolvedInstanceFieldSet* instruction) {
4699 FieldAccessCallingConventionARM calling_convention;
4700 codegen_->GenerateUnresolvedFieldAccess(instruction,
4701 instruction->GetFieldType(),
4702 instruction->GetFieldIndex(),
4703 instruction->GetDexPc(),
4704 calling_convention);
4705}
4706
4707void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4708 HUnresolvedStaticFieldGet* instruction) {
4709 FieldAccessCallingConventionARM calling_convention;
4710 codegen_->CreateUnresolvedFieldLocationSummary(
4711 instruction, instruction->GetFieldType(), calling_convention);
4712}
4713
4714void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4715 HUnresolvedStaticFieldGet* instruction) {
4716 FieldAccessCallingConventionARM calling_convention;
4717 codegen_->GenerateUnresolvedFieldAccess(instruction,
4718 instruction->GetFieldType(),
4719 instruction->GetFieldIndex(),
4720 instruction->GetDexPc(),
4721 calling_convention);
4722}
4723
4724void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4725 HUnresolvedStaticFieldSet* instruction) {
4726 FieldAccessCallingConventionARM calling_convention;
4727 codegen_->CreateUnresolvedFieldLocationSummary(
4728 instruction, instruction->GetFieldType(), calling_convention);
4729}
4730
4731void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4732 HUnresolvedStaticFieldSet* instruction) {
4733 FieldAccessCallingConventionARM calling_convention;
4734 codegen_->GenerateUnresolvedFieldAccess(instruction,
4735 instruction->GetFieldType(),
4736 instruction->GetFieldIndex(),
4737 instruction->GetDexPc(),
4738 calling_convention);
4739}
4740
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004741void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004742 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4743 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004744}
4745
Calin Juravle2ae48182016-03-16 14:05:09 +00004746void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4747 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004748 return;
4749 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004750 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004751
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004752 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004753 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004754}
4755
Calin Juravle2ae48182016-03-16 14:05:09 +00004756void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004757 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004758 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004759
4760 LocationSummary* locations = instruction->GetLocations();
4761 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004762
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004763 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004764}
4765
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004766void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004767 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004768}
4769
Artem Serov6c916792016-07-11 14:02:34 +01004770static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4771 switch (type) {
4772 case Primitive::kPrimNot:
4773 return kLoadWord;
4774 case Primitive::kPrimBoolean:
4775 return kLoadUnsignedByte;
4776 case Primitive::kPrimByte:
4777 return kLoadSignedByte;
4778 case Primitive::kPrimChar:
4779 return kLoadUnsignedHalfword;
4780 case Primitive::kPrimShort:
4781 return kLoadSignedHalfword;
4782 case Primitive::kPrimInt:
4783 return kLoadWord;
4784 case Primitive::kPrimLong:
4785 return kLoadWordPair;
4786 case Primitive::kPrimFloat:
4787 return kLoadSWord;
4788 case Primitive::kPrimDouble:
4789 return kLoadDWord;
4790 default:
4791 LOG(FATAL) << "Unreachable type " << type;
4792 UNREACHABLE();
4793 }
4794}
4795
4796static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4797 switch (type) {
4798 case Primitive::kPrimNot:
4799 return kStoreWord;
4800 case Primitive::kPrimBoolean:
4801 case Primitive::kPrimByte:
4802 return kStoreByte;
4803 case Primitive::kPrimChar:
4804 case Primitive::kPrimShort:
4805 return kStoreHalfword;
4806 case Primitive::kPrimInt:
4807 return kStoreWord;
4808 case Primitive::kPrimLong:
4809 return kStoreWordPair;
4810 case Primitive::kPrimFloat:
4811 return kStoreSWord;
4812 case Primitive::kPrimDouble:
4813 return kStoreDWord;
4814 default:
4815 LOG(FATAL) << "Unreachable type " << type;
4816 UNREACHABLE();
4817 }
4818}
4819
4820void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4821 Location out_loc,
4822 Register base,
4823 Register reg_offset,
4824 Condition cond) {
4825 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4826 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4827
4828 switch (type) {
4829 case Primitive::kPrimByte:
4830 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4831 break;
4832 case Primitive::kPrimBoolean:
4833 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4834 break;
4835 case Primitive::kPrimShort:
4836 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4837 break;
4838 case Primitive::kPrimChar:
4839 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4840 break;
4841 case Primitive::kPrimNot:
4842 case Primitive::kPrimInt:
4843 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4844 break;
4845 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4846 case Primitive::kPrimLong:
4847 case Primitive::kPrimFloat:
4848 case Primitive::kPrimDouble:
4849 default:
4850 LOG(FATAL) << "Unreachable type " << type;
4851 UNREACHABLE();
4852 }
4853}
4854
4855void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4856 Location loc,
4857 Register base,
4858 Register reg_offset,
4859 Condition cond) {
4860 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4861 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4862
4863 switch (type) {
4864 case Primitive::kPrimByte:
4865 case Primitive::kPrimBoolean:
4866 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4867 break;
4868 case Primitive::kPrimShort:
4869 case Primitive::kPrimChar:
4870 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4871 break;
4872 case Primitive::kPrimNot:
4873 case Primitive::kPrimInt:
4874 __ str(loc.AsRegister<Register>(), mem_address, cond);
4875 break;
4876 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4877 case Primitive::kPrimLong:
4878 case Primitive::kPrimFloat:
4879 case Primitive::kPrimDouble:
4880 default:
4881 LOG(FATAL) << "Unreachable type " << type;
4882 UNREACHABLE();
4883 }
4884}
4885
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004886void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004887 bool object_array_get_with_read_barrier =
4888 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004889 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004890 new (GetGraph()->GetArena()) LocationSummary(instruction,
4891 object_array_get_with_read_barrier ?
4892 LocationSummary::kCallOnSlowPath :
4893 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004894 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004895 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004896 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004897 locations->SetInAt(0, Location::RequiresRegister());
4898 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004899 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4900 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4901 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004902 // The output overlaps in the case of an object array get with
4903 // read barriers enabled: we do not want the move to overwrite the
4904 // array's location, as we need it to emit the read barrier.
4905 locations->SetOut(
4906 Location::RequiresRegister(),
4907 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004908 }
Roland Levillainc9285912015-12-18 10:38:42 +00004909 // We need a temporary register for the read barrier marking slow
4910 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07004911 // Also need for String compression feature.
4912 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4913 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00004914 locations->AddTemp(Location::RequiresRegister());
4915 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004916}
4917
4918void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4919 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004920 Location obj_loc = locations->InAt(0);
4921 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004922 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004923 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004924 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004925 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07004926 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4927 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01004928 HInstruction* array_instr = instruction->GetArray();
4929 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01004930
Roland Levillain4d027112015-07-01 15:41:14 +01004931 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004932 case Primitive::kPrimBoolean:
4933 case Primitive::kPrimByte:
4934 case Primitive::kPrimShort:
4935 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004936 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004937 Register length;
4938 if (maybe_compressed_char_at) {
4939 length = locations->GetTemp(0).AsRegister<Register>();
4940 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4941 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4942 codegen_->MaybeRecordImplicitNullCheck(instruction);
4943 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004944 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004945 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07004946 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07004947 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004948 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4949 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4950 "Expecting 0=compressed, 1=uncompressed");
4951 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004952 __ LoadFromOffset(kLoadUnsignedByte,
4953 out_loc.AsRegister<Register>(),
4954 obj,
4955 data_offset + const_index);
4956 __ b(&done);
4957 __ Bind(&uncompressed_load);
4958 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4959 out_loc.AsRegister<Register>(),
4960 obj,
4961 data_offset + (const_index << 1));
4962 __ Bind(&done);
4963 } else {
4964 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01004965
jessicahandojo05765752016-09-09 19:01:32 -07004966 LoadOperandType load_type = GetLoadOperandType(type);
4967 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4968 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004969 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004970 Register temp = IP;
4971
4972 if (has_intermediate_address) {
4973 // We do not need to compute the intermediate address from the array: the
4974 // input instruction has done it already. See the comment in
4975 // `TryExtractArrayAccessAddress()`.
4976 if (kIsDebugBuild) {
4977 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4978 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4979 }
4980 temp = obj;
4981 } else {
4982 __ add(temp, obj, ShifterOperand(data_offset));
4983 }
jessicahandojo05765752016-09-09 19:01:32 -07004984 if (maybe_compressed_char_at) {
4985 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004986 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4987 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4988 "Expecting 0=compressed, 1=uncompressed");
4989 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004990 __ ldrb(out_loc.AsRegister<Register>(),
4991 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
4992 __ b(&done);
4993 __ Bind(&uncompressed_load);
4994 __ ldrh(out_loc.AsRegister<Register>(),
4995 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
4996 __ Bind(&done);
4997 } else {
4998 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4999 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005000 }
5001 break;
5002 }
5003
Roland Levillainc9285912015-12-18 10:38:42 +00005004 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00005005 // The read barrier instrumentation of object ArrayGet
5006 // instructions does not support the HIntermediateAddress
5007 // instruction.
5008 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5009
Roland Levillainc9285912015-12-18 10:38:42 +00005010 static_assert(
5011 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5012 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00005013 // /* HeapReference<Object> */ out =
5014 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5015 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5016 Location temp = locations->GetTemp(0);
5017 // Note that a potential implicit null check is handled in this
5018 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
5019 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5020 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5021 } else {
5022 Register out = out_loc.AsRegister<Register>();
5023 if (index.IsConstant()) {
5024 size_t offset =
5025 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5026 __ LoadFromOffset(kLoadWord, out, obj, offset);
5027 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(instruction, out_loc, out_loc, obj_loc, offset);
5032 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005033 Register temp = IP;
5034
5035 if (has_intermediate_address) {
5036 // We do not need to compute the intermediate address from the array: the
5037 // input instruction has done it already. See the comment in
5038 // `TryExtractArrayAccessAddress()`.
5039 if (kIsDebugBuild) {
5040 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5041 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5042 }
5043 temp = obj;
5044 } else {
5045 __ add(temp, obj, ShifterOperand(data_offset));
5046 }
5047 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005048
Roland Levillainc9285912015-12-18 10:38:42 +00005049 codegen_->MaybeRecordImplicitNullCheck(instruction);
5050 // If read barriers are enabled, emit read barriers other than
5051 // Baker's using a slow path (and also unpoison the loaded
5052 // reference, if heap poisoning is enabled).
5053 codegen_->MaybeGenerateReadBarrierSlow(
5054 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5055 }
5056 }
5057 break;
5058 }
5059
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005060 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005061 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005062 size_t offset =
5063 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005064 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005065 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005066 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005067 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005068 }
5069 break;
5070 }
5071
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005072 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005073 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005074 if (index.IsConstant()) {
5075 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005076 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005077 } else {
5078 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005079 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005080 }
5081 break;
5082 }
5083
5084 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005085 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005086 if (index.IsConstant()) {
5087 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005088 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005089 } else {
5090 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005091 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005092 }
5093 break;
5094 }
5095
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005096 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005097 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005098 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005099 }
Roland Levillain4d027112015-07-01 15:41:14 +01005100
5101 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005102 // Potential implicit null checks, in the case of reference
5103 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005104 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005105 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005106 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005107}
5108
5109void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005110 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005111
5112 bool needs_write_barrier =
5113 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005114 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005115
Nicolas Geoffray39468442014-09-02 15:17:15 +01005116 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005117 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005118 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005119 LocationSummary::kCallOnSlowPath :
5120 LocationSummary::kNoCall);
5121
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005122 locations->SetInAt(0, Location::RequiresRegister());
5123 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5124 if (Primitive::IsFloatingPointType(value_type)) {
5125 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005127 locations->SetInAt(2, Location::RequiresRegister());
5128 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005129 if (needs_write_barrier) {
5130 // Temporary registers for the write barrier.
5131 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005132 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005133 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005134}
5135
5136void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5137 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005138 Location array_loc = locations->InAt(0);
5139 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005140 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005141 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005142 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005143 bool needs_write_barrier =
5144 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005145 uint32_t data_offset =
5146 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5147 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005148 HInstruction* array_instr = instruction->GetArray();
5149 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005150
5151 switch (value_type) {
5152 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005153 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005154 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005155 case Primitive::kPrimChar:
5156 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005157 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005158 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5159 uint32_t full_offset =
5160 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5161 StoreOperandType store_type = GetStoreOperandType(value_type);
5162 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005164 Register temp = IP;
5165
5166 if (has_intermediate_address) {
5167 // We do not need to compute the intermediate address from the array: the
5168 // input instruction has done it already. See the comment in
5169 // `TryExtractArrayAccessAddress()`.
5170 if (kIsDebugBuild) {
5171 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5172 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5173 }
5174 temp = array;
5175 } else {
5176 __ add(temp, array, ShifterOperand(data_offset));
5177 }
Artem Serov6c916792016-07-11 14:02:34 +01005178 codegen_->StoreToShiftedRegOffset(value_type,
5179 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005180 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005181 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005182 }
5183 break;
5184 }
5185
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005186 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005187 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005188 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5189 // See the comment in instruction_simplifier_shared.cc.
5190 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005191
5192 if (instruction->InputAt(2)->IsNullConstant()) {
5193 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005194 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005195 size_t offset =
5196 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005197 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005198 } else {
5199 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005200 __ add(IP, array, ShifterOperand(data_offset));
5201 codegen_->StoreToShiftedRegOffset(value_type,
5202 value_loc,
5203 IP,
5204 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005205 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005206 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005207 DCHECK(!needs_write_barrier);
5208 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005209 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005210 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005211
5212 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005213 Location temp1_loc = locations->GetTemp(0);
5214 Register temp1 = temp1_loc.AsRegister<Register>();
5215 Location temp2_loc = locations->GetTemp(1);
5216 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005217 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5218 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5219 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5220 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005221 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005222
Roland Levillain3b359c72015-11-17 19:35:12 +00005223 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005224 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5225 codegen_->AddSlowPath(slow_path);
5226 if (instruction->GetValueCanBeNull()) {
5227 Label non_zero;
5228 __ CompareAndBranchIfNonZero(value, &non_zero);
5229 if (index.IsConstant()) {
5230 size_t offset =
5231 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5232 __ StoreToOffset(kStoreWord, value, array, offset);
5233 } else {
5234 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005235 __ add(IP, array, ShifterOperand(data_offset));
5236 codegen_->StoreToShiftedRegOffset(value_type,
5237 value_loc,
5238 IP,
5239 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005240 }
5241 codegen_->MaybeRecordImplicitNullCheck(instruction);
5242 __ b(&done);
5243 __ Bind(&non_zero);
5244 }
5245
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005246 // Note that when read barriers are enabled, the type checks
5247 // are performed without read barriers. This is fine, even in
5248 // the case where a class object is in the from-space after
5249 // the flip, as a comparison involving such a type would not
5250 // produce a false positive; it may of course produce a false
5251 // negative, in which case we would take the ArraySet slow
5252 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005253
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005254 // /* HeapReference<Class> */ temp1 = array->klass_
5255 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5256 codegen_->MaybeRecordImplicitNullCheck(instruction);
5257 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005258
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005259 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5260 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5261 // /* HeapReference<Class> */ temp2 = value->klass_
5262 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5263 // If heap poisoning is enabled, no need to unpoison `temp1`
5264 // nor `temp2`, as we are comparing two poisoned references.
5265 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005266
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005267 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5268 Label do_put;
5269 __ b(&do_put, EQ);
5270 // If heap poisoning is enabled, the `temp1` reference has
5271 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005272 __ MaybeUnpoisonHeapReference(temp1);
5273
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005274 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5275 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5276 // If heap poisoning is enabled, no need to unpoison
5277 // `temp1`, as we are comparing against null below.
5278 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5279 __ Bind(&do_put);
5280 } else {
5281 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005282 }
5283 }
5284
Artem Serov6c916792016-07-11 14:02:34 +01005285 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005286 if (kPoisonHeapReferences) {
5287 // Note that in the case where `value` is a null reference,
5288 // we do not enter this block, as a null reference does not
5289 // need poisoning.
5290 DCHECK_EQ(value_type, Primitive::kPrimNot);
5291 __ Mov(temp1, value);
5292 __ PoisonHeapReference(temp1);
5293 source = temp1;
5294 }
5295
5296 if (index.IsConstant()) {
5297 size_t offset =
5298 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5299 __ StoreToOffset(kStoreWord, source, array, offset);
5300 } else {
5301 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005302
5303 __ add(IP, array, ShifterOperand(data_offset));
5304 codegen_->StoreToShiftedRegOffset(value_type,
5305 Location::RegisterLocation(source),
5306 IP,
5307 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005308 }
5309
Roland Levillain3b359c72015-11-17 19:35:12 +00005310 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005311 codegen_->MaybeRecordImplicitNullCheck(instruction);
5312 }
5313
5314 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5315
5316 if (done.IsLinked()) {
5317 __ Bind(&done);
5318 }
5319
5320 if (slow_path != nullptr) {
5321 __ Bind(slow_path->GetExitLabel());
5322 }
5323
5324 break;
5325 }
5326
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005327 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005328 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005329 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005330 size_t offset =
5331 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005332 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005334 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005335 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005336 }
5337 break;
5338 }
5339
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005340 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005341 Location value = locations->InAt(2);
5342 DCHECK(value.IsFpuRegister());
5343 if (index.IsConstant()) {
5344 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005345 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005346 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005347 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005348 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5349 }
5350 break;
5351 }
5352
5353 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005354 Location value = locations->InAt(2);
5355 DCHECK(value.IsFpuRegisterPair());
5356 if (index.IsConstant()) {
5357 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005358 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005359 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005360 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005361 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5362 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005363
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005364 break;
5365 }
5366
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005367 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005368 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005369 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005370 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005371
Roland Levillain80e67092016-01-08 16:04:55 +00005372 // Objects are handled in the switch.
5373 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005374 codegen_->MaybeRecordImplicitNullCheck(instruction);
5375 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005376}
5377
5378void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005379 LocationSummary* locations =
5380 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005381 locations->SetInAt(0, Location::RequiresRegister());
5382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005383}
5384
5385void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5386 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005387 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005388 Register obj = locations->InAt(0).AsRegister<Register>();
5389 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005391 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005392 // Mask out compression flag from String's array length.
5393 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005394 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07005395 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005396}
5397
Artem Serov328429f2016-07-06 16:23:04 +01005398void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005399 LocationSummary* locations =
5400 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5401
5402 locations->SetInAt(0, Location::RequiresRegister());
5403 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5404 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5405}
5406
5407void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5408 LocationSummary* locations = instruction->GetLocations();
5409 Location out = locations->Out();
5410 Location first = locations->InAt(0);
5411 Location second = locations->InAt(1);
5412
Artem Serov328429f2016-07-06 16:23:04 +01005413 if (second.IsRegister()) {
5414 __ add(out.AsRegister<Register>(),
5415 first.AsRegister<Register>(),
5416 ShifterOperand(second.AsRegister<Register>()));
5417 } else {
5418 __ AddConstant(out.AsRegister<Register>(),
5419 first.AsRegister<Register>(),
5420 second.GetConstant()->AsIntConstant()->GetValue());
5421 }
5422}
5423
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005424void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005425 RegisterSet caller_saves = RegisterSet::Empty();
5426 InvokeRuntimeCallingConvention calling_convention;
5427 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5428 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5429 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005430 locations->SetInAt(0, Location::RequiresRegister());
5431 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005432}
5433
5434void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5435 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005436 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005437 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005438 codegen_->AddSlowPath(slow_path);
5439
Roland Levillain271ab9c2014-11-27 15:23:57 +00005440 Register index = locations->InAt(0).AsRegister<Register>();
5441 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005442
5443 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005444 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005445}
5446
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005447void CodeGeneratorARM::MarkGCCard(Register temp,
5448 Register card,
5449 Register object,
5450 Register value,
5451 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005452 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005453 if (can_be_null) {
5454 __ CompareAndBranchIfZero(value, &is_null);
5455 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005456 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005457 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5458 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005459 if (can_be_null) {
5460 __ Bind(&is_null);
5461 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005462}
5463
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005464void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005465 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005466}
5467
5468void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005469 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5470}
5471
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005472void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005473 LocationSummary* locations =
5474 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005475 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005476}
5477
5478void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005479 HBasicBlock* block = instruction->GetBlock();
5480 if (block->GetLoopInformation() != nullptr) {
5481 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5482 // The back edge will generate the suspend check.
5483 return;
5484 }
5485 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5486 // The goto will generate the suspend check.
5487 return;
5488 }
5489 GenerateSuspendCheck(instruction, nullptr);
5490}
5491
5492void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5493 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005494 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005495 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5496 if (slow_path == nullptr) {
5497 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5498 instruction->SetSlowPath(slow_path);
5499 codegen_->AddSlowPath(slow_path);
5500 if (successor != nullptr) {
5501 DCHECK(successor->IsLoopHeader());
5502 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5503 }
5504 } else {
5505 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5506 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005507
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005508 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005509 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005510 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005511 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005512 __ Bind(slow_path->GetReturnLabel());
5513 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005514 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005515 __ b(slow_path->GetEntryLabel());
5516 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005517}
5518
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005519ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5520 return codegen_->GetAssembler();
5521}
5522
5523void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005524 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005525 Location source = move->GetSource();
5526 Location destination = move->GetDestination();
5527
5528 if (source.IsRegister()) {
5529 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005530 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005531 } else if (destination.IsFpuRegister()) {
5532 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005533 } else {
5534 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005535 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005536 SP, destination.GetStackIndex());
5537 }
5538 } else if (source.IsStackSlot()) {
5539 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005540 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005541 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005542 } else if (destination.IsFpuRegister()) {
5543 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005544 } else {
5545 DCHECK(destination.IsStackSlot());
5546 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5547 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5548 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005549 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005550 if (destination.IsRegister()) {
5551 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5552 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005553 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005554 } else {
5555 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005556 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5557 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005558 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005559 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005560 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5561 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005562 } else if (destination.IsRegisterPair()) {
5563 DCHECK(ExpectedPairLayout(destination));
5564 __ LoadFromOffset(
5565 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5566 } else {
5567 DCHECK(destination.IsFpuRegisterPair()) << destination;
5568 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5569 SP,
5570 source.GetStackIndex());
5571 }
5572 } else if (source.IsRegisterPair()) {
5573 if (destination.IsRegisterPair()) {
5574 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5575 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005576 } else if (destination.IsFpuRegisterPair()) {
5577 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5578 source.AsRegisterPairLow<Register>(),
5579 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005580 } else {
5581 DCHECK(destination.IsDoubleStackSlot()) << destination;
5582 DCHECK(ExpectedPairLayout(source));
5583 __ StoreToOffset(
5584 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5585 }
5586 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005587 if (destination.IsRegisterPair()) {
5588 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5589 destination.AsRegisterPairHigh<Register>(),
5590 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5591 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005592 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5593 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5594 } else {
5595 DCHECK(destination.IsDoubleStackSlot()) << destination;
5596 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5597 SP,
5598 destination.GetStackIndex());
5599 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005600 } else {
5601 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005602 HConstant* constant = source.GetConstant();
5603 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5604 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005605 if (destination.IsRegister()) {
5606 __ LoadImmediate(destination.AsRegister<Register>(), value);
5607 } else {
5608 DCHECK(destination.IsStackSlot());
5609 __ LoadImmediate(IP, value);
5610 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5611 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005612 } else if (constant->IsLongConstant()) {
5613 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005614 if (destination.IsRegisterPair()) {
5615 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5616 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005617 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005618 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005619 __ LoadImmediate(IP, Low32Bits(value));
5620 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5621 __ LoadImmediate(IP, High32Bits(value));
5622 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5623 }
5624 } else if (constant->IsDoubleConstant()) {
5625 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005626 if (destination.IsFpuRegisterPair()) {
5627 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005628 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005629 DCHECK(destination.IsDoubleStackSlot()) << destination;
5630 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005631 __ LoadImmediate(IP, Low32Bits(int_value));
5632 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5633 __ LoadImmediate(IP, High32Bits(int_value));
5634 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5635 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005636 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005637 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005638 float value = constant->AsFloatConstant()->GetValue();
5639 if (destination.IsFpuRegister()) {
5640 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5641 } else {
5642 DCHECK(destination.IsStackSlot());
5643 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5644 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5645 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005646 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005647 }
5648}
5649
5650void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5651 __ Mov(IP, reg);
5652 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5653 __ StoreToOffset(kStoreWord, IP, SP, mem);
5654}
5655
5656void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5657 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5658 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5659 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5660 SP, mem1 + stack_offset);
5661 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5662 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5663 SP, mem2 + stack_offset);
5664 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5665}
5666
5667void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005668 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005669 Location source = move->GetSource();
5670 Location destination = move->GetDestination();
5671
5672 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005673 DCHECK_NE(source.AsRegister<Register>(), IP);
5674 DCHECK_NE(destination.AsRegister<Register>(), IP);
5675 __ Mov(IP, source.AsRegister<Register>());
5676 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5677 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005678 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005679 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005680 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005681 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005682 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5683 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005684 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005685 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005686 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005687 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005688 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005689 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005690 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005691 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005692 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5693 destination.AsRegisterPairHigh<Register>(),
5694 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005695 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005696 Register low_reg = source.IsRegisterPair()
5697 ? source.AsRegisterPairLow<Register>()
5698 : destination.AsRegisterPairLow<Register>();
5699 int mem = source.IsRegisterPair()
5700 ? destination.GetStackIndex()
5701 : source.GetStackIndex();
5702 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005703 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005704 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005705 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005706 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005707 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5708 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005709 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005710 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005711 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005712 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5713 DRegister reg = source.IsFpuRegisterPair()
5714 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5715 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5716 int mem = source.IsFpuRegisterPair()
5717 ? destination.GetStackIndex()
5718 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005719 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005720 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005721 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005722 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5723 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5724 : destination.AsFpuRegister<SRegister>();
5725 int mem = source.IsFpuRegister()
5726 ? destination.GetStackIndex()
5727 : source.GetStackIndex();
5728
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005729 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005730 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005731 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005732 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005733 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5734 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005735 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005736 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005737 }
5738}
5739
5740void ParallelMoveResolverARM::SpillScratch(int reg) {
5741 __ Push(static_cast<Register>(reg));
5742}
5743
5744void ParallelMoveResolverARM::RestoreScratch(int reg) {
5745 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005746}
5747
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005748HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5749 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005750 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005751 case HLoadClass::LoadKind::kInvalid:
5752 LOG(FATAL) << "UNREACHABLE";
5753 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005754 case HLoadClass::LoadKind::kReferrersClass:
5755 break;
5756 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5757 DCHECK(!GetCompilerOptions().GetCompilePic());
5758 break;
5759 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5760 DCHECK(GetCompilerOptions().GetCompilePic());
5761 break;
5762 case HLoadClass::LoadKind::kBootImageAddress:
5763 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005764 case HLoadClass::LoadKind::kBssEntry:
5765 DCHECK(!Runtime::Current()->UseJitCompilation());
5766 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005767 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005768 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005769 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005770 case HLoadClass::LoadKind::kDexCacheViaMethod:
5771 break;
5772 }
5773 return desired_class_load_kind;
5774}
5775
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005776void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005777 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5778 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005779 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005780 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005781 cls,
5782 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005783 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005784 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005785 return;
5786 }
Vladimir Marko41559982017-01-06 14:04:23 +00005787 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005788
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005789 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5790 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005791 ? LocationSummary::kCallOnSlowPath
5792 : LocationSummary::kNoCall;
5793 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005794 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005795 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005796 }
5797
Vladimir Marko41559982017-01-06 14:04:23 +00005798 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005799 locations->SetInAt(0, Location::RequiresRegister());
5800 }
5801 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005802 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5803 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5804 // Rely on the type resolution or initialization and marking to save everything we need.
5805 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
5806 // to the custom calling convention) or by marking, so we request a different temp.
5807 locations->AddTemp(Location::RequiresRegister());
5808 RegisterSet caller_saves = RegisterSet::Empty();
5809 InvokeRuntimeCallingConvention calling_convention;
5810 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5811 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5812 // that the the kPrimNot result register is the same as the first argument register.
5813 locations->SetCustomSlowPathCallerSaves(caller_saves);
5814 } else {
5815 // For non-Baker read barrier we have a temp-clobbering call.
5816 }
5817 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005818}
5819
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005820// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5821// move.
5822void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005823 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5824 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5825 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005826 return;
5827 }
Vladimir Marko41559982017-01-06 14:04:23 +00005828 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005829
Vladimir Marko41559982017-01-06 14:04:23 +00005830 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005831 Location out_loc = locations->Out();
5832 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005833
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005834 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5835 ? kWithoutReadBarrier
5836 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005837 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005838 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005839 case HLoadClass::LoadKind::kReferrersClass: {
5840 DCHECK(!cls->CanCallRuntime());
5841 DCHECK(!cls->MustGenerateClinitCheck());
5842 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5843 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005844 GenerateGcRootFieldLoad(cls,
5845 out_loc,
5846 current_method,
5847 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005848 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005849 break;
5850 }
5851 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005852 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005853 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005854 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5855 cls->GetTypeIndex()));
5856 break;
5857 }
5858 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005859 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005860 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005861 CodeGeneratorARM::PcRelativePatchInfo* labels =
5862 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5863 __ BindTrackedLabel(&labels->movw_label);
5864 __ movw(out, /* placeholder */ 0u);
5865 __ BindTrackedLabel(&labels->movt_label);
5866 __ movt(out, /* placeholder */ 0u);
5867 __ BindTrackedLabel(&labels->add_pc_label);
5868 __ add(out, out, ShifterOperand(PC));
5869 break;
5870 }
5871 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005872 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005873 uint32_t address = dchecked_integral_cast<uint32_t>(
5874 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5875 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005876 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5877 break;
5878 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005879 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005880 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
5881 ? locations->GetTemp(0).AsRegister<Register>()
5882 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005883 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005884 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005885 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00005886 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005887 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00005888 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005889 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00005890 __ add(temp, temp, ShifterOperand(PC));
5891 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005892 generate_null_check = true;
5893 break;
5894 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005895 case HLoadClass::LoadKind::kJitTableAddress: {
5896 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5897 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005898 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005899 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00005900 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005901 break;
5902 }
Vladimir Marko41559982017-01-06 14:04:23 +00005903 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005904 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005905 LOG(FATAL) << "UNREACHABLE";
5906 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005907 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005908
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005909 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5910 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005911 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005912 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5913 codegen_->AddSlowPath(slow_path);
5914 if (generate_null_check) {
5915 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5916 }
5917 if (cls->MustGenerateClinitCheck()) {
5918 GenerateClassInitializationCheck(slow_path, out);
5919 } else {
5920 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005921 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005922 }
5923}
5924
5925void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5926 LocationSummary* locations =
5927 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5928 locations->SetInAt(0, Location::RequiresRegister());
5929 if (check->HasUses()) {
5930 locations->SetOut(Location::SameAsFirstInput());
5931 }
5932}
5933
5934void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005935 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005936 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005937 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005938 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005939 GenerateClassInitializationCheck(slow_path,
5940 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005941}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005942
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005943void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005944 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005945 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5946 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5947 __ b(slow_path->GetEntryLabel(), LT);
5948 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5949 // properly. Therefore, we do a memory fence.
5950 __ dmb(ISH);
5951 __ Bind(slow_path->GetExitLabel());
5952}
5953
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005954HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5955 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005956 switch (desired_string_load_kind) {
5957 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5958 DCHECK(!GetCompilerOptions().GetCompilePic());
5959 break;
5960 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5961 DCHECK(GetCompilerOptions().GetCompilePic());
5962 break;
5963 case HLoadString::LoadKind::kBootImageAddress:
5964 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005965 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005966 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005967 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005968 case HLoadString::LoadKind::kJitTableAddress:
5969 DCHECK(Runtime::Current()->UseJitCompilation());
5970 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005971 case HLoadString::LoadKind::kDexCacheViaMethod:
5972 break;
5973 }
5974 return desired_string_load_kind;
5975}
5976
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005977void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005978 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005979 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005980 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005981 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005982 locations->SetOut(Location::RegisterLocation(R0));
5983 } else {
5984 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005985 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5986 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005987 // Rely on the pResolveString and marking to save everything we need, including temps.
5988 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
5989 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005990 locations->AddTemp(Location::RequiresRegister());
5991 RegisterSet caller_saves = RegisterSet::Empty();
5992 InvokeRuntimeCallingConvention calling_convention;
5993 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5994 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5995 // that the the kPrimNot result register is the same as the first argument register.
5996 locations->SetCustomSlowPathCallerSaves(caller_saves);
5997 } else {
5998 // For non-Baker read barrier we have a temp-clobbering call.
5999 }
6000 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006001 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006002}
6003
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006004// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6005// move.
6006void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006007 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006008 Location out_loc = locations->Out();
6009 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006010 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00006011
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006012 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006013 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006014 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006015 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6016 load->GetStringIndex()));
6017 return; // No dex cache slow path.
6018 }
6019 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006020 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006021 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006022 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006023 __ BindTrackedLabel(&labels->movw_label);
6024 __ movw(out, /* placeholder */ 0u);
6025 __ BindTrackedLabel(&labels->movt_label);
6026 __ movt(out, /* placeholder */ 0u);
6027 __ BindTrackedLabel(&labels->add_pc_label);
6028 __ add(out, out, ShifterOperand(PC));
6029 return; // No dex cache slow path.
6030 }
6031 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006032 uint32_t address = dchecked_integral_cast<uint32_t>(
6033 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6034 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006035 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6036 return; // No dex cache slow path.
6037 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006038 case HLoadString::LoadKind::kBssEntry: {
6039 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006040 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6041 ? locations->GetTemp(0).AsRegister<Register>()
6042 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006043 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006044 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006045 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006046 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006047 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006048 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006049 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006050 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006051 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006052 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
6053 codegen_->AddSlowPath(slow_path);
6054 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6055 __ Bind(slow_path->GetExitLabel());
6056 return;
6057 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006058 case HLoadString::LoadKind::kJitTableAddress: {
6059 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006060 load->GetStringIndex(),
6061 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006062 // /* GcRoot<mirror::String> */ out = *out
6063 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6064 return;
6065 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006066 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006067 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006068 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006069
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006070 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
6071 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6072 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006073 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006074 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006075 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6076 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006077}
6078
David Brazdilcb1c0552015-08-04 16:22:25 +01006079static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006080 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006081}
6082
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006083void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6084 LocationSummary* locations =
6085 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6086 locations->SetOut(Location::RequiresRegister());
6087}
6088
6089void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006090 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006091 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6092}
6093
6094void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6095 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6096}
6097
6098void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006099 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006100 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006101}
6102
6103void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6104 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006105 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006106 InvokeRuntimeCallingConvention calling_convention;
6107 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6108}
6109
6110void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006111 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006112 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006113}
6114
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006115// Temp is used for read barrier.
6116static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6117 if (kEmitCompilerReadBarrier &&
6118 (kUseBakerReadBarrier ||
6119 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6120 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6121 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6122 return 1;
6123 }
6124 return 0;
6125}
6126
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006127// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006128// interface pointer, one for loading the current interface.
6129// The other checks have one temp for loading the object's class.
6130static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6131 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6132 return 3;
6133 }
6134 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006135}
6136
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006137void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006138 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006139 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006140 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006141 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 case TypeCheckKind::kExactCheck:
6143 case TypeCheckKind::kAbstractClassCheck:
6144 case TypeCheckKind::kClassHierarchyCheck:
6145 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006146 call_kind =
6147 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006148 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006149 break;
6150 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006151 case TypeCheckKind::kUnresolvedCheck:
6152 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006153 call_kind = LocationSummary::kCallOnSlowPath;
6154 break;
6155 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006156
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006157 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006158 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006159 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006160 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006161 locations->SetInAt(0, Location::RequiresRegister());
6162 locations->SetInAt(1, Location::RequiresRegister());
6163 // The "out" register is used as a temporary, so it overlaps with the inputs.
6164 // Note that TypeCheckSlowPathARM uses this register too.
6165 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006166 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006167}
6168
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006169void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006170 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006171 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006172 Location obj_loc = locations->InAt(0);
6173 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006174 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006175 Location out_loc = locations->Out();
6176 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006177 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6178 DCHECK_LE(num_temps, 1u);
6179 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006180 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006181 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6182 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6183 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006184 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006185 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006186
6187 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006188 // avoid null check if we know obj is not null.
6189 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006190 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006191 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006192
Roland Levillainc9285912015-12-18 10:38:42 +00006193 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006194 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006195 // /* HeapReference<Class> */ out = obj->klass_
6196 GenerateReferenceLoadTwoRegisters(instruction,
6197 out_loc,
6198 obj_loc,
6199 class_offset,
6200 maybe_temp_loc,
6201 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006202 __ cmp(out, ShifterOperand(cls));
6203 // Classes must be equal for the instanceof to succeed.
6204 __ b(&zero, NE);
6205 __ LoadImmediate(out, 1);
6206 __ b(&done);
6207 break;
6208 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006209
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006210 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006211 // /* HeapReference<Class> */ out = obj->klass_
6212 GenerateReferenceLoadTwoRegisters(instruction,
6213 out_loc,
6214 obj_loc,
6215 class_offset,
6216 maybe_temp_loc,
6217 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006218 // If the class is abstract, we eagerly fetch the super class of the
6219 // object to avoid doing a comparison we know will fail.
6220 Label loop;
6221 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006222 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006223 GenerateReferenceLoadOneRegister(instruction,
6224 out_loc,
6225 super_offset,
6226 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006227 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006228 // If `out` is null, we use it for the result, and jump to `done`.
6229 __ CompareAndBranchIfZero(out, &done);
6230 __ cmp(out, ShifterOperand(cls));
6231 __ b(&loop, NE);
6232 __ LoadImmediate(out, 1);
6233 if (zero.IsLinked()) {
6234 __ b(&done);
6235 }
6236 break;
6237 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006238
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006239 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006240 // /* HeapReference<Class> */ out = obj->klass_
6241 GenerateReferenceLoadTwoRegisters(instruction,
6242 out_loc,
6243 obj_loc,
6244 class_offset,
6245 maybe_temp_loc,
6246 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006247 // Walk over the class hierarchy to find a match.
6248 Label loop, success;
6249 __ Bind(&loop);
6250 __ cmp(out, ShifterOperand(cls));
6251 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006252 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006253 GenerateReferenceLoadOneRegister(instruction,
6254 out_loc,
6255 super_offset,
6256 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006257 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006258 __ CompareAndBranchIfNonZero(out, &loop);
6259 // If `out` is null, we use it for the result, and jump to `done`.
6260 __ b(&done);
6261 __ Bind(&success);
6262 __ LoadImmediate(out, 1);
6263 if (zero.IsLinked()) {
6264 __ b(&done);
6265 }
6266 break;
6267 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006268
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006269 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006270 // /* HeapReference<Class> */ out = obj->klass_
6271 GenerateReferenceLoadTwoRegisters(instruction,
6272 out_loc,
6273 obj_loc,
6274 class_offset,
6275 maybe_temp_loc,
6276 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006277 // Do an exact check.
6278 Label exact_check;
6279 __ cmp(out, ShifterOperand(cls));
6280 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006281 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006282 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006283 GenerateReferenceLoadOneRegister(instruction,
6284 out_loc,
6285 component_offset,
6286 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006287 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006288 // If `out` is null, we use it for the result, and jump to `done`.
6289 __ CompareAndBranchIfZero(out, &done);
6290 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6291 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6292 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006293 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006294 __ LoadImmediate(out, 1);
6295 __ b(&done);
6296 break;
6297 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006298
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006299 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006300 // No read barrier since the slow path will retry upon failure.
6301 // /* HeapReference<Class> */ out = obj->klass_
6302 GenerateReferenceLoadTwoRegisters(instruction,
6303 out_loc,
6304 obj_loc,
6305 class_offset,
6306 maybe_temp_loc,
6307 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006308 __ cmp(out, ShifterOperand(cls));
6309 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006310 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6311 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312 codegen_->AddSlowPath(slow_path);
6313 __ b(slow_path->GetEntryLabel(), NE);
6314 __ LoadImmediate(out, 1);
6315 if (zero.IsLinked()) {
6316 __ b(&done);
6317 }
6318 break;
6319 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006320
Calin Juravle98893e12015-10-02 21:05:03 +01006321 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006322 case TypeCheckKind::kInterfaceCheck: {
6323 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006324 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006325 // cases.
6326 //
6327 // We cannot directly call the InstanceofNonTrivial runtime
6328 // entry point without resorting to a type checking slow path
6329 // here (i.e. by calling InvokeRuntime directly), as it would
6330 // require to assign fixed registers for the inputs of this
6331 // HInstanceOf instruction (following the runtime calling
6332 // convention), which might be cluttered by the potential first
6333 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006334 //
6335 // TODO: Introduce a new runtime entry point taking the object
6336 // to test (instead of its class) as argument, and let it deal
6337 // with the read barrier issues. This will let us refactor this
6338 // case of the `switch` code as it was previously (with a direct
6339 // call to the runtime not using a type checking slow path).
6340 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006341 DCHECK(locations->OnlyCallsOnSlowPath());
6342 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6343 /* is_fatal */ false);
6344 codegen_->AddSlowPath(slow_path);
6345 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006346 if (zero.IsLinked()) {
6347 __ b(&done);
6348 }
6349 break;
6350 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006351 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006352
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006353 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006354 __ Bind(&zero);
6355 __ LoadImmediate(out, 0);
6356 }
6357
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006358 if (done.IsLinked()) {
6359 __ Bind(&done);
6360 }
6361
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006362 if (slow_path != nullptr) {
6363 __ Bind(slow_path->GetExitLabel());
6364 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006365}
6366
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006367void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006368 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6369 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6370
Roland Levillain3b359c72015-11-17 19:35:12 +00006371 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6372 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006373 case TypeCheckKind::kExactCheck:
6374 case TypeCheckKind::kAbstractClassCheck:
6375 case TypeCheckKind::kClassHierarchyCheck:
6376 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006377 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6378 LocationSummary::kCallOnSlowPath :
6379 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006380 break;
6381 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006382 case TypeCheckKind::kUnresolvedCheck:
6383 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006384 call_kind = LocationSummary::kCallOnSlowPath;
6385 break;
6386 }
6387
Roland Levillain3b359c72015-11-17 19:35:12 +00006388 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6389 locations->SetInAt(0, Location::RequiresRegister());
6390 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006391 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006392}
6393
6394void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006395 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006396 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006397 Location obj_loc = locations->InAt(0);
6398 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006399 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006400 Location temp_loc = locations->GetTemp(0);
6401 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006402 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6403 DCHECK_LE(num_temps, 3u);
6404 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6405 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6406 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6407 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6408 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6409 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6410 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6411 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6412 const uint32_t object_array_data_offset =
6413 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006414
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006415 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6416 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6417 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006418 bool is_type_check_slow_path_fatal = false;
6419 if (!kEmitCompilerReadBarrier) {
6420 is_type_check_slow_path_fatal =
6421 (type_check_kind == TypeCheckKind::kExactCheck ||
6422 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6423 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6424 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6425 !instruction->CanThrowIntoCatchBlock();
6426 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01006427 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006428 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6429 is_type_check_slow_path_fatal);
6430 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006431
6432 Label done;
6433 // Avoid null check if we know obj is not null.
6434 if (instruction->MustDoNullCheck()) {
6435 __ CompareAndBranchIfZero(obj, &done);
6436 }
6437
Roland Levillain3b359c72015-11-17 19:35:12 +00006438 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006439 case TypeCheckKind::kExactCheck:
6440 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006441 // /* HeapReference<Class> */ temp = obj->klass_
6442 GenerateReferenceLoadTwoRegisters(instruction,
6443 temp_loc,
6444 obj_loc,
6445 class_offset,
6446 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006447 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006448
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006449 __ cmp(temp, ShifterOperand(cls));
6450 // Jump to slow path for throwing the exception or doing a
6451 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006452 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006453 break;
6454 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006455
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006456 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006457 // /* HeapReference<Class> */ temp = obj->klass_
6458 GenerateReferenceLoadTwoRegisters(instruction,
6459 temp_loc,
6460 obj_loc,
6461 class_offset,
6462 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006463 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006464
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006465 // If the class is abstract, we eagerly fetch the super class of the
6466 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006467 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006468 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006469 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006470 GenerateReferenceLoadOneRegister(instruction,
6471 temp_loc,
6472 super_offset,
6473 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006474 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006475
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006476 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6477 // exception.
6478 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00006479
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006480 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006481 __ cmp(temp, ShifterOperand(cls));
6482 __ b(&loop, NE);
6483 break;
6484 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006485
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006486 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006487 // /* HeapReference<Class> */ temp = obj->klass_
6488 GenerateReferenceLoadTwoRegisters(instruction,
6489 temp_loc,
6490 obj_loc,
6491 class_offset,
6492 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006493 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006494
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006495 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006496 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006497 __ Bind(&loop);
6498 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006499 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006500
Roland Levillain3b359c72015-11-17 19:35:12 +00006501 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006502 GenerateReferenceLoadOneRegister(instruction,
6503 temp_loc,
6504 super_offset,
6505 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006506 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006507
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006508 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6509 // exception.
6510 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6511 // Otherwise, jump to the beginning of the loop.
6512 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006513 break;
6514 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006515
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006516 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006517 // /* HeapReference<Class> */ temp = obj->klass_
6518 GenerateReferenceLoadTwoRegisters(instruction,
6519 temp_loc,
6520 obj_loc,
6521 class_offset,
6522 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006523 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006524
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006525 // Do an exact check.
6526 __ cmp(temp, ShifterOperand(cls));
6527 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006528
6529 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006530 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006531 GenerateReferenceLoadOneRegister(instruction,
6532 temp_loc,
6533 component_offset,
6534 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006535 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006536 // If the component type is null, jump to the slow path to throw the exception.
6537 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6538 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6539 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006540 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006541 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006542 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006543 break;
6544 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006545
Calin Juravle98893e12015-10-02 21:05:03 +01006546 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006547 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00006548 // We cannot directly call the CheckCast runtime entry point
6549 // without resorting to a type checking slow path here (i.e. by
6550 // calling InvokeRuntime directly), as it would require to
6551 // assign fixed registers for the inputs of this HInstanceOf
6552 // instruction (following the runtime calling convention), which
6553 // might be cluttered by the potential first read barrier
6554 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006555
Roland Levillain3b359c72015-11-17 19:35:12 +00006556 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006557 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006558
6559 case TypeCheckKind::kInterfaceCheck: {
6560 // Avoid read barriers to improve performance of the fast path. We can not get false
6561 // positives by doing this.
6562 // /* HeapReference<Class> */ temp = obj->klass_
6563 GenerateReferenceLoadTwoRegisters(instruction,
6564 temp_loc,
6565 obj_loc,
6566 class_offset,
6567 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006568 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006569
6570 // /* HeapReference<Class> */ temp = temp->iftable_
6571 GenerateReferenceLoadTwoRegisters(instruction,
6572 temp_loc,
6573 temp_loc,
6574 iftable_offset,
6575 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006576 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006577 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006578 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006579 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006580 Label start_loop;
6581 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006582 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
6583 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006584 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
6585 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006586 // Go to next interface.
6587 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
6588 __ sub(maybe_temp2_loc.AsRegister<Register>(),
6589 maybe_temp2_loc.AsRegister<Register>(),
6590 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006591 // Compare the classes and continue the loop if they do not match.
6592 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
6593 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006594 break;
6595 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006596 }
6597 __ Bind(&done);
6598
Roland Levillain3b359c72015-11-17 19:35:12 +00006599 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006600}
6601
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006602void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6603 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006604 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006605 InvokeRuntimeCallingConvention calling_convention;
6606 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6607}
6608
6609void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006610 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6611 instruction,
6612 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006613 if (instruction->IsEnter()) {
6614 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6615 } else {
6616 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6617 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006618}
6619
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006620void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6621void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6622void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006623
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006624void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006625 LocationSummary* locations =
6626 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6627 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6628 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006629 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006630 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006631 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006632 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006633}
6634
6635void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6636 HandleBitwiseOperation(instruction);
6637}
6638
6639void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6640 HandleBitwiseOperation(instruction);
6641}
6642
6643void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6644 HandleBitwiseOperation(instruction);
6645}
6646
Artem Serov7fc63502016-02-09 17:15:29 +00006647
6648void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6649 LocationSummary* locations =
6650 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6651 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6652 || instruction->GetResultType() == Primitive::kPrimLong);
6653
6654 locations->SetInAt(0, Location::RequiresRegister());
6655 locations->SetInAt(1, Location::RequiresRegister());
6656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6657}
6658
6659void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6660 LocationSummary* locations = instruction->GetLocations();
6661 Location first = locations->InAt(0);
6662 Location second = locations->InAt(1);
6663 Location out = locations->Out();
6664
6665 if (instruction->GetResultType() == Primitive::kPrimInt) {
6666 Register first_reg = first.AsRegister<Register>();
6667 ShifterOperand second_reg(second.AsRegister<Register>());
6668 Register out_reg = out.AsRegister<Register>();
6669
6670 switch (instruction->GetOpKind()) {
6671 case HInstruction::kAnd:
6672 __ bic(out_reg, first_reg, second_reg);
6673 break;
6674 case HInstruction::kOr:
6675 __ orn(out_reg, first_reg, second_reg);
6676 break;
6677 // There is no EON on arm.
6678 case HInstruction::kXor:
6679 default:
6680 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6681 UNREACHABLE();
6682 }
6683 return;
6684
6685 } else {
6686 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6687 Register first_low = first.AsRegisterPairLow<Register>();
6688 Register first_high = first.AsRegisterPairHigh<Register>();
6689 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6690 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6691 Register out_low = out.AsRegisterPairLow<Register>();
6692 Register out_high = out.AsRegisterPairHigh<Register>();
6693
6694 switch (instruction->GetOpKind()) {
6695 case HInstruction::kAnd:
6696 __ bic(out_low, first_low, second_low);
6697 __ bic(out_high, first_high, second_high);
6698 break;
6699 case HInstruction::kOr:
6700 __ orn(out_low, first_low, second_low);
6701 __ orn(out_high, first_high, second_high);
6702 break;
6703 // There is no EON on arm.
6704 case HInstruction::kXor:
6705 default:
6706 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6707 UNREACHABLE();
6708 }
6709 }
6710}
6711
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006712void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6713 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6714 if (value == 0xffffffffu) {
6715 if (out != first) {
6716 __ mov(out, ShifterOperand(first));
6717 }
6718 return;
6719 }
6720 if (value == 0u) {
6721 __ mov(out, ShifterOperand(0));
6722 return;
6723 }
6724 ShifterOperand so;
6725 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6726 __ and_(out, first, so);
6727 } else {
6728 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6729 __ bic(out, first, ShifterOperand(~value));
6730 }
6731}
6732
6733void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6734 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6735 if (value == 0u) {
6736 if (out != first) {
6737 __ mov(out, ShifterOperand(first));
6738 }
6739 return;
6740 }
6741 if (value == 0xffffffffu) {
6742 __ mvn(out, ShifterOperand(0));
6743 return;
6744 }
6745 ShifterOperand so;
6746 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6747 __ orr(out, first, so);
6748 } else {
6749 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6750 __ orn(out, first, ShifterOperand(~value));
6751 }
6752}
6753
6754void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6755 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6756 if (value == 0u) {
6757 if (out != first) {
6758 __ mov(out, ShifterOperand(first));
6759 }
6760 return;
6761 }
6762 __ eor(out, first, ShifterOperand(value));
6763}
6764
Vladimir Marko59751a72016-08-05 14:37:27 +01006765void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6766 Location first,
6767 uint64_t value) {
6768 Register out_low = out.AsRegisterPairLow<Register>();
6769 Register out_high = out.AsRegisterPairHigh<Register>();
6770 Register first_low = first.AsRegisterPairLow<Register>();
6771 Register first_high = first.AsRegisterPairHigh<Register>();
6772 uint32_t value_low = Low32Bits(value);
6773 uint32_t value_high = High32Bits(value);
6774 if (value_low == 0u) {
6775 if (out_low != first_low) {
6776 __ mov(out_low, ShifterOperand(first_low));
6777 }
6778 __ AddConstant(out_high, first_high, value_high);
6779 return;
6780 }
6781 __ AddConstantSetFlags(out_low, first_low, value_low);
6782 ShifterOperand so;
6783 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6784 __ adc(out_high, first_high, so);
6785 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6786 __ sbc(out_high, first_high, so);
6787 } else {
6788 LOG(FATAL) << "Unexpected constant " << value_high;
6789 UNREACHABLE();
6790 }
6791}
6792
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006793void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6794 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006795 Location first = locations->InAt(0);
6796 Location second = locations->InAt(1);
6797 Location out = locations->Out();
6798
6799 if (second.IsConstant()) {
6800 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6801 uint32_t value_low = Low32Bits(value);
6802 if (instruction->GetResultType() == Primitive::kPrimInt) {
6803 Register first_reg = first.AsRegister<Register>();
6804 Register out_reg = out.AsRegister<Register>();
6805 if (instruction->IsAnd()) {
6806 GenerateAndConst(out_reg, first_reg, value_low);
6807 } else if (instruction->IsOr()) {
6808 GenerateOrrConst(out_reg, first_reg, value_low);
6809 } else {
6810 DCHECK(instruction->IsXor());
6811 GenerateEorConst(out_reg, first_reg, value_low);
6812 }
6813 } else {
6814 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6815 uint32_t value_high = High32Bits(value);
6816 Register first_low = first.AsRegisterPairLow<Register>();
6817 Register first_high = first.AsRegisterPairHigh<Register>();
6818 Register out_low = out.AsRegisterPairLow<Register>();
6819 Register out_high = out.AsRegisterPairHigh<Register>();
6820 if (instruction->IsAnd()) {
6821 GenerateAndConst(out_low, first_low, value_low);
6822 GenerateAndConst(out_high, first_high, value_high);
6823 } else if (instruction->IsOr()) {
6824 GenerateOrrConst(out_low, first_low, value_low);
6825 GenerateOrrConst(out_high, first_high, value_high);
6826 } else {
6827 DCHECK(instruction->IsXor());
6828 GenerateEorConst(out_low, first_low, value_low);
6829 GenerateEorConst(out_high, first_high, value_high);
6830 }
6831 }
6832 return;
6833 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006834
6835 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006836 Register first_reg = first.AsRegister<Register>();
6837 ShifterOperand second_reg(second.AsRegister<Register>());
6838 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006839 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006840 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006841 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006842 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006843 } else {
6844 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006845 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006846 }
6847 } else {
6848 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006849 Register first_low = first.AsRegisterPairLow<Register>();
6850 Register first_high = first.AsRegisterPairHigh<Register>();
6851 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6852 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6853 Register out_low = out.AsRegisterPairLow<Register>();
6854 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006855 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006856 __ and_(out_low, first_low, second_low);
6857 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006858 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006859 __ orr(out_low, first_low, second_low);
6860 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006861 } else {
6862 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006863 __ eor(out_low, first_low, second_low);
6864 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006865 }
6866 }
6867}
6868
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006869void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
6870 HInstruction* instruction,
6871 Location out,
6872 uint32_t offset,
6873 Location maybe_temp,
6874 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006875 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006876 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006877 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006878 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006879 if (kUseBakerReadBarrier) {
6880 // Load with fast path based Baker's read barrier.
6881 // /* HeapReference<Object> */ out = *(out + offset)
6882 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006883 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006884 } else {
6885 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006886 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006887 // in the following move operation, as we will need it for the
6888 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006889 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006890 // /* HeapReference<Object> */ out = *(out + offset)
6891 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006892 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006893 }
6894 } else {
6895 // Plain load with no read barrier.
6896 // /* HeapReference<Object> */ out = *(out + offset)
6897 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6898 __ MaybeUnpoisonHeapReference(out_reg);
6899 }
6900}
6901
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006902void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
6903 HInstruction* instruction,
6904 Location out,
6905 Location obj,
6906 uint32_t offset,
6907 Location maybe_temp,
6908 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006909 Register out_reg = out.AsRegister<Register>();
6910 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006911 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006912 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006913 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006914 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006915 // Load with fast path based Baker's read barrier.
6916 // /* HeapReference<Object> */ out = *(obj + offset)
6917 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006918 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006919 } else {
6920 // Load with slow path based read barrier.
6921 // /* HeapReference<Object> */ out = *(obj + offset)
6922 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6923 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6924 }
6925 } else {
6926 // Plain load with no read barrier.
6927 // /* HeapReference<Object> */ out = *(obj + offset)
6928 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6929 __ MaybeUnpoisonHeapReference(out_reg);
6930 }
6931}
6932
6933void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6934 Location root,
6935 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006936 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006937 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006938 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006939 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006940 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006941 if (kUseBakerReadBarrier) {
6942 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6943 // Baker's read barrier are used:
6944 //
6945 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006946 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6947 // if (temp != null) {
6948 // root = temp(root)
Roland Levillainc9285912015-12-18 10:38:42 +00006949 // }
6950
6951 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6952 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6953 static_assert(
6954 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6955 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6956 "have different sizes.");
6957 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6958 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6959 "have different sizes.");
6960
Vladimir Marko953437b2016-08-24 08:30:46 +00006961 // Slow path marking the GC root `root`.
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006962 Location temp = Location::RegisterLocation(LR);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006963 SlowPathCodeARM* slow_path =
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006964 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
6965 instruction,
6966 root,
6967 /*entrypoint*/ temp);
Roland Levillainc9285912015-12-18 10:38:42 +00006968 codegen_->AddSlowPath(slow_path);
6969
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006970 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6971 const int32_t entry_point_offset =
6972 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
6973 // Loading the entrypoint does not require a load acquire since it is only changed when
6974 // threads are suspended or running a checkpoint.
6975 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6976 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6977 // checking GetIsGcMarking.
6978 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00006979 __ Bind(slow_path->GetExitLabel());
6980 } else {
6981 // GC root loaded through a slow path for read barriers other
6982 // than Baker's.
6983 // /* GcRoot<mirror::Object>* */ root = obj + offset
6984 __ AddConstant(root_reg, obj, offset);
6985 // /* mirror::Object* */ root = root->Read()
6986 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6987 }
6988 } else {
6989 // Plain GC root load with no read barrier.
6990 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6991 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6992 // Note that GC roots are not affected by heap poisoning, thus we
6993 // do not have to unpoison `root_reg` here.
6994 }
6995}
6996
6997void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6998 Location ref,
6999 Register obj,
7000 uint32_t offset,
7001 Location temp,
7002 bool needs_null_check) {
7003 DCHECK(kEmitCompilerReadBarrier);
7004 DCHECK(kUseBakerReadBarrier);
7005
7006 // /* HeapReference<Object> */ ref = *(obj + offset)
7007 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01007008 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00007009 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007010 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007011}
7012
7013void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7014 Location ref,
7015 Register obj,
7016 uint32_t data_offset,
7017 Location index,
7018 Location temp,
7019 bool needs_null_check) {
7020 DCHECK(kEmitCompilerReadBarrier);
7021 DCHECK(kUseBakerReadBarrier);
7022
Roland Levillainbfea3352016-06-23 13:48:47 +01007023 static_assert(
7024 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7025 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007026 // /* HeapReference<Object> */ ref =
7027 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01007028 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00007029 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007030 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007031}
7032
7033void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7034 Location ref,
7035 Register obj,
7036 uint32_t offset,
7037 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01007038 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00007039 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007040 bool needs_null_check,
7041 bool always_update_field,
7042 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00007043 DCHECK(kEmitCompilerReadBarrier);
7044 DCHECK(kUseBakerReadBarrier);
7045
7046 // In slow path based read barriers, the read barrier call is
7047 // inserted after the original load. However, in fast path based
7048 // Baker's read barriers, we need to perform the load of
7049 // mirror::Object::monitor_ *before* the original reference load.
7050 // This load-load ordering is required by the read barrier.
7051 // The fast path/slow path (for Baker's algorithm) should look like:
7052 //
7053 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7054 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7055 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007056 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillainc9285912015-12-18 10:38:42 +00007057 // if (is_gray) {
7058 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7059 // }
7060 //
7061 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007062 // slightly more complex as it performs additional checks that we do
7063 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00007064
7065 Register ref_reg = ref.AsRegister<Register>();
7066 Register temp_reg = temp.AsRegister<Register>();
7067 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7068
7069 // /* int32_t */ monitor = obj->monitor_
7070 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7071 if (needs_null_check) {
7072 MaybeRecordImplicitNullCheck(instruction);
7073 }
7074 // /* LockWord */ lock_word = LockWord(monitor)
7075 static_assert(sizeof(LockWord) == sizeof(int32_t),
7076 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007077
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007078 // Introduce a dependency on the lock_word including the rb_state,
7079 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00007080 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01007081 // `obj` is unchanged by this operation, but its value now depends
7082 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007083 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00007084
7085 // The actual reference load.
7086 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007087 // Load types involving an "index": ArrayGet,
7088 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7089 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01007090 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00007091 if (index.IsConstant()) {
7092 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01007093 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00007094 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7095 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01007096 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007097 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7098 // intrinsics, which use a register pair as index ("long
7099 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01007100 Register index_reg = index.IsRegisterPair()
7101 ? index.AsRegisterPairLow<Register>()
7102 : index.AsRegister<Register>();
7103 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00007104 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
7105 }
7106 } else {
7107 // /* HeapReference<Object> */ ref = *(obj + offset)
7108 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7109 }
7110
7111 // Object* ref = ref_addr->AsMirrorPtr()
7112 __ MaybeUnpoisonHeapReference(ref_reg);
7113
Vladimir Marko953437b2016-08-24 08:30:46 +00007114 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007115 SlowPathCodeARM* slow_path;
7116 if (always_update_field) {
7117 DCHECK(temp2 != nullptr);
7118 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
7119 // of the form `obj + field_offset`, where `obj` is a register and
7120 // `field_offset` is a register pair (of which only the lower half
7121 // is used). Thus `offset` and `scale_factor` above are expected
7122 // to be null in this code path.
7123 DCHECK_EQ(offset, 0u);
7124 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7125 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
7126 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7127 } else {
7128 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
7129 }
Roland Levillainc9285912015-12-18 10:38:42 +00007130 AddSlowPath(slow_path);
7131
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007132 // if (rb_state == ReadBarrier::GrayState())
Roland Levillainc9285912015-12-18 10:38:42 +00007133 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007134 // Given the numeric representation, it's enough to check the low bit of the
7135 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7136 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007137 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7138 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007139 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7140 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00007141 __ Bind(slow_path->GetExitLabel());
7142}
7143
7144void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
7145 Location out,
7146 Location ref,
7147 Location obj,
7148 uint32_t offset,
7149 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007150 DCHECK(kEmitCompilerReadBarrier);
7151
Roland Levillainc9285912015-12-18 10:38:42 +00007152 // Insert a slow path based read barrier *after* the reference load.
7153 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007154 // If heap poisoning is enabled, the unpoisoning of the loaded
7155 // reference will be carried out by the runtime within the slow
7156 // path.
7157 //
7158 // Note that `ref` currently does not get unpoisoned (when heap
7159 // poisoning is enabled), which is alright as the `ref` argument is
7160 // not used by the artReadBarrierSlow entry point.
7161 //
7162 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007163 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00007164 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
7165 AddSlowPath(slow_path);
7166
Roland Levillain3b359c72015-11-17 19:35:12 +00007167 __ b(slow_path->GetEntryLabel());
7168 __ Bind(slow_path->GetExitLabel());
7169}
7170
Roland Levillainc9285912015-12-18 10:38:42 +00007171void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7172 Location out,
7173 Location ref,
7174 Location obj,
7175 uint32_t offset,
7176 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007177 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00007178 // Baker's read barriers shall be handled by the fast path
7179 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
7180 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007181 // If heap poisoning is enabled, unpoisoning will be taken care of
7182 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007183 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00007184 } else if (kPoisonHeapReferences) {
7185 __ UnpoisonHeapReference(out.AsRegister<Register>());
7186 }
7187}
7188
Roland Levillainc9285912015-12-18 10:38:42 +00007189void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7190 Location out,
7191 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007192 DCHECK(kEmitCompilerReadBarrier);
7193
Roland Levillainc9285912015-12-18 10:38:42 +00007194 // Insert a slow path based read barrier *after* the GC root load.
7195 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007196 // Note that GC roots are not affected by heap poisoning, so we do
7197 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007198 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007199 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
7200 AddSlowPath(slow_path);
7201
Roland Levillain3b359c72015-11-17 19:35:12 +00007202 __ b(slow_path->GetEntryLabel());
7203 __ Bind(slow_path->GetExitLabel());
7204}
7205
Vladimir Markodc151b22015-10-15 18:02:30 +01007206HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7207 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007208 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00007209 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007210}
7211
Vladimir Markob4536b72015-11-24 13:45:23 +00007212Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7213 Register temp) {
7214 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7215 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7216 if (!invoke->GetLocations()->Intrinsified()) {
7217 return location.AsRegister<Register>();
7218 }
7219 // For intrinsics we allow any location, so it may be on the stack.
7220 if (!location.IsRegister()) {
7221 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7222 return temp;
7223 }
7224 // For register locations, check if the register was saved. If so, get it from the stack.
7225 // Note: There is a chance that the register was saved but not overwritten, so we could
7226 // save one load. However, since this is just an intrinsic slow path we prefer this
7227 // simple and more robust approach rather that trying to determine if that's the case.
7228 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08007229 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00007230 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7231 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7232 return temp;
7233 }
7234 return location.AsRegister<Register>();
7235}
7236
TatWai Chongd8c052a2016-11-02 16:12:48 +08007237Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
7238 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007239 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7240 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007241 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7242 uint32_t offset =
7243 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007244 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007245 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007246 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007247 }
Vladimir Marko58155012015-08-19 12:49:41 +00007248 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007249 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007250 break;
7251 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7252 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7253 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007254 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7255 HArmDexCacheArraysBase* base =
7256 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7257 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7258 temp.AsRegister<Register>());
7259 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7260 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7261 break;
7262 }
Vladimir Marko58155012015-08-19 12:49:41 +00007263 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007264 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007265 Register method_reg;
7266 Register reg = temp.AsRegister<Register>();
7267 if (current_method.IsRegister()) {
7268 method_reg = current_method.AsRegister<Register>();
7269 } else {
7270 DCHECK(invoke->GetLocations()->Intrinsified());
7271 DCHECK(!current_method.IsValid());
7272 method_reg = reg;
7273 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7274 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007275 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7276 __ LoadFromOffset(kLoadWord,
7277 reg,
7278 method_reg,
7279 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007280 // temp = temp[index_in_cache];
7281 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7282 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007283 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7284 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007285 }
Vladimir Marko58155012015-08-19 12:49:41 +00007286 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08007287 return callee_method;
7288}
7289
7290void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
7291 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00007292
7293 switch (invoke->GetCodePtrLocation()) {
7294 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7295 __ bl(GetFrameEntryLabel());
7296 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007297 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7298 // LR = callee_method->entry_point_from_quick_compiled_code_
7299 __ LoadFromOffset(
7300 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007301 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007302 // LR()
7303 __ blx(LR);
7304 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007305 }
7306
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007307 DCHECK(!IsLeafMethod());
7308}
7309
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007310void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7311 Register temp = temp_location.AsRegister<Register>();
7312 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7313 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007314
7315 // Use the calling convention instead of the location of the receiver, as
7316 // intrinsics may have put the receiver in a different register. In the intrinsics
7317 // slow path, the arguments have been moved to the right place, so here we are
7318 // guaranteed that the receiver is the first register of the calling convention.
7319 InvokeDexCallingConvention calling_convention;
7320 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007321 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007322 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007323 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007324 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007325 // Instead of simply (possibly) unpoisoning `temp` here, we should
7326 // emit a read barrier for the previous class reference load.
7327 // However this is not required in practice, as this is an
7328 // intermediate/temporary reference and because the current
7329 // concurrent copying collector keeps the from-space memory
7330 // intact/accessible until the end of the marking phase (the
7331 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007332 __ MaybeUnpoisonHeapReference(temp);
7333 // temp = temp->GetMethodAt(method_offset);
7334 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007335 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007336 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7337 // LR = temp->GetEntryPoint();
7338 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7339 // LR();
7340 __ blx(LR);
7341}
7342
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007343CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007344 const DexFile& dex_file, dex::StringIndex string_index) {
7345 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007346}
7347
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007348CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08007349 const DexFile& dex_file, dex::TypeIndex type_index) {
7350 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007351}
7352
Vladimir Marko1998cd02017-01-13 13:02:58 +00007353CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
7354 const DexFile& dex_file, dex::TypeIndex type_index) {
7355 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
7356}
7357
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007358CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7359 const DexFile& dex_file, uint32_t element_offset) {
7360 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7361}
7362
7363CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7364 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7365 patches->emplace_back(dex_file, offset_or_index);
7366 return &patches->back();
7367}
7368
7369Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007370 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007371 return boot_image_string_patches_.GetOrCreate(
7372 StringReference(&dex_file, string_index),
7373 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7374}
7375
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007376Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007377 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007378 return boot_image_type_patches_.GetOrCreate(
7379 TypeReference(&dex_file, type_index),
7380 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7381}
7382
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007383Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7384 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7385 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7386 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7387}
7388
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007389Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007390 dex::StringIndex string_index,
7391 Handle<mirror::String> handle) {
7392 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7393 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007394 return jit_string_patches_.GetOrCreate(
7395 StringReference(&dex_file, string_index),
7396 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7397}
7398
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007399Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
7400 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007401 Handle<mirror::Class> handle) {
7402 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
7403 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007404 return jit_class_patches_.GetOrCreate(
7405 TypeReference(&dex_file, type_index),
7406 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7407}
7408
Vladimir Markoaad75c62016-10-03 08:46:48 +00007409template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7410inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7411 const ArenaDeque<PcRelativePatchInfo>& infos,
7412 ArenaVector<LinkerPatch>* linker_patches) {
7413 for (const PcRelativePatchInfo& info : infos) {
7414 const DexFile& dex_file = info.target_dex_file;
7415 size_t offset_or_index = info.offset_or_index;
7416 DCHECK(info.add_pc_label.IsBound());
7417 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7418 // Add MOVW patch.
7419 DCHECK(info.movw_label.IsBound());
7420 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7421 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7422 // Add MOVT patch.
7423 DCHECK(info.movt_label.IsBound());
7424 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7425 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7426 }
7427}
7428
Vladimir Marko58155012015-08-19 12:49:41 +00007429void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7430 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007431 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00007432 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007433 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007434 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007435 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007436 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00007437 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007438 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007439 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007440 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7441 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007442 for (const auto& entry : boot_image_string_patches_) {
7443 const StringReference& target_string = entry.first;
7444 Literal* literal = entry.second;
7445 DCHECK(literal->GetLabel()->IsBound());
7446 uint32_t literal_offset = literal->GetLabel()->Position();
7447 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7448 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007449 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007450 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007451 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00007452 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007453 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7454 linker_patches);
7455 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007456 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7457 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007458 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7459 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007460 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007461 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
7462 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007463 for (const auto& entry : boot_image_type_patches_) {
7464 const TypeReference& target_type = entry.first;
7465 Literal* literal = entry.second;
7466 DCHECK(literal->GetLabel()->IsBound());
7467 uint32_t literal_offset = literal->GetLabel()->Position();
7468 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7469 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007470 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007471 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007472 for (const auto& entry : boot_image_address_patches_) {
7473 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7474 Literal* literal = entry.second;
7475 DCHECK(literal->GetLabel()->IsBound());
7476 uint32_t literal_offset = literal->GetLabel()->Position();
7477 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7478 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007479 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007480}
7481
7482Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7483 return map->GetOrCreate(
7484 value,
7485 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007486}
7487
7488Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7489 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007490 return map->GetOrCreate(
7491 target_method,
7492 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007493}
7494
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007495void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7496 LocationSummary* locations =
7497 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7498 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7499 Location::RequiresRegister());
7500 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7501 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7502 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7503}
7504
7505void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7506 LocationSummary* locations = instr->GetLocations();
7507 Register res = locations->Out().AsRegister<Register>();
7508 Register accumulator =
7509 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7510 Register mul_left =
7511 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7512 Register mul_right =
7513 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7514
7515 if (instr->GetOpKind() == HInstruction::kAdd) {
7516 __ mla(res, mul_left, mul_right, accumulator);
7517 } else {
7518 __ mls(res, mul_left, mul_right, accumulator);
7519 }
7520}
7521
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007522void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007523 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007524 LOG(FATAL) << "Unreachable";
7525}
7526
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007527void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007528 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007529 LOG(FATAL) << "Unreachable";
7530}
7531
Mark Mendellfe57faa2015-09-18 09:26:15 -04007532// Simple implementation of packed switch - generate cascaded compare/jumps.
7533void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7534 LocationSummary* locations =
7535 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7536 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007537 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007538 codegen_->GetAssembler()->IsThumb()) {
7539 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7540 if (switch_instr->GetStartValue() != 0) {
7541 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7542 }
7543 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007544}
7545
7546void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7547 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007548 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007549 LocationSummary* locations = switch_instr->GetLocations();
7550 Register value_reg = locations->InAt(0).AsRegister<Register>();
7551 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7552
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007553 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007554 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007555 Register temp_reg = IP;
7556 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7557 // the immediate, because IP is used as the destination register. For the other
7558 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7559 // and they can be encoded in the instruction without making use of IP register.
7560 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7561
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007562 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007563 // Jump to successors[0] if value == lower_bound.
7564 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7565 int32_t last_index = 0;
7566 for (; num_entries - last_index > 2; last_index += 2) {
7567 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7568 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7569 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7570 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7571 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7572 }
7573 if (num_entries - last_index == 2) {
7574 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007575 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007576 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007577 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007578
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007579 // And the default for any other value.
7580 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7581 __ b(codegen_->GetLabelOf(default_block));
7582 }
7583 } else {
7584 // Create a table lookup.
7585 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7586
7587 // Materialize a pointer to the switch table
7588 std::vector<Label*> labels(num_entries);
7589 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7590 for (uint32_t i = 0; i < num_entries; i++) {
7591 labels[i] = codegen_->GetLabelOf(successors[i]);
7592 }
7593 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7594
7595 // Remove the bias.
7596 Register key_reg;
7597 if (lower_bound != 0) {
7598 key_reg = locations->GetTemp(1).AsRegister<Register>();
7599 __ AddConstant(key_reg, value_reg, -lower_bound);
7600 } else {
7601 key_reg = value_reg;
7602 }
7603
7604 // Check whether the value is in the table, jump to default block if not.
7605 __ CmpConstant(key_reg, num_entries - 1);
7606 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7607
7608 // Load the displacement from the table.
7609 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7610
7611 // Dispatch is a direct add to the PC (for Thumb2).
7612 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007613 }
7614}
7615
Vladimir Markob4536b72015-11-24 13:45:23 +00007616void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7618 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007619}
7620
7621void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7622 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007623 CodeGeneratorARM::PcRelativePatchInfo* labels =
7624 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007625 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007626 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007627 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007628 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007629 __ BindTrackedLabel(&labels->add_pc_label);
7630 __ add(base_reg, base_reg, ShifterOperand(PC));
7631}
7632
Andreas Gampe85b62f22015-09-09 13:15:38 -07007633void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7634 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007635 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007636 return;
7637 }
7638
7639 DCHECK_NE(type, Primitive::kPrimVoid);
7640
7641 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7642 if (return_loc.Equals(trg)) {
7643 return;
7644 }
7645
7646 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7647 // with the last branch.
7648 if (type == Primitive::kPrimLong) {
7649 HParallelMove parallel_move(GetGraph()->GetArena());
7650 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7651 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7652 GetMoveResolver()->EmitNativeCode(&parallel_move);
7653 } else if (type == Primitive::kPrimDouble) {
7654 HParallelMove parallel_move(GetGraph()->GetArena());
7655 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7656 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7657 GetMoveResolver()->EmitNativeCode(&parallel_move);
7658 } else {
7659 // Let the parallel move resolver take care of all of this.
7660 HParallelMove parallel_move(GetGraph()->GetArena());
7661 parallel_move.AddMove(return_loc, trg, type, nullptr);
7662 GetMoveResolver()->EmitNativeCode(&parallel_move);
7663 }
7664}
7665
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007666void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7667 LocationSummary* locations =
7668 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7669 locations->SetInAt(0, Location::RequiresRegister());
7670 locations->SetOut(Location::RequiresRegister());
7671}
7672
7673void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7674 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007675 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007676 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007677 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007678 __ LoadFromOffset(kLoadWord,
7679 locations->Out().AsRegister<Register>(),
7680 locations->InAt(0).AsRegister<Register>(),
7681 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007682 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007683 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007684 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007685 __ LoadFromOffset(kLoadWord,
7686 locations->Out().AsRegister<Register>(),
7687 locations->InAt(0).AsRegister<Register>(),
7688 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7689 __ LoadFromOffset(kLoadWord,
7690 locations->Out().AsRegister<Register>(),
7691 locations->Out().AsRegister<Register>(),
7692 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007693 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007694}
7695
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007696static void PatchJitRootUse(uint8_t* code,
7697 const uint8_t* roots_data,
7698 Literal* literal,
7699 uint64_t index_in_table) {
7700 DCHECK(literal->GetLabel()->IsBound());
7701 uint32_t literal_offset = literal->GetLabel()->Position();
7702 uintptr_t address =
7703 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7704 uint8_t* data = code + literal_offset;
7705 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7706}
7707
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007708void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7709 for (const auto& entry : jit_string_patches_) {
7710 const auto& it = jit_string_roots_.find(entry.first);
7711 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007712 PatchJitRootUse(code, roots_data, entry.second, it->second);
7713 }
7714 for (const auto& entry : jit_class_patches_) {
7715 const auto& it = jit_class_roots_.find(entry.first);
7716 DCHECK(it != jit_class_roots_.end());
7717 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007718 }
7719}
7720
Roland Levillain4d027112015-07-01 15:41:14 +01007721#undef __
7722#undef QUICK_ENTRY_POINT
7723
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007724} // namespace arm
7725} // namespace art