blob: 06e164feaddaefe7049151ecaa96fb1dd194cd57 [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`).
Artem Serovf4d6aee2016-07-11 10:41:45 +0100649class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000650 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800651 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
652 Location ref,
653 Location entrypoint = Location::NoLocation())
654 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillainc9285912015-12-18 10:38:42 +0000655 DCHECK(kEmitCompilerReadBarrier);
656 }
657
658 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
659
660 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
661 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100662 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000663 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100664 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillainc9285912015-12-18 10:38:42 +0000665 DCHECK(instruction_->IsInstanceFieldGet() ||
666 instruction_->IsStaticFieldGet() ||
667 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100668 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000669 instruction_->IsLoadClass() ||
670 instruction_->IsLoadString() ||
671 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100672 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100673 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
674 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000675 << "Unexpected instruction in read barrier marking slow path: "
676 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000677 // The read barrier instrumentation of object ArrayGet
678 // instructions does not support the HIntermediateAddress
679 // instruction.
680 DCHECK(!(instruction_->IsArrayGet() &&
681 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillainc9285912015-12-18 10:38:42 +0000682
683 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100684 // No need to save live registers; it's taken care of by the
685 // entrypoint. Also, there is no need to update the stack mask,
686 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000687 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100688 DCHECK_NE(ref_reg, SP);
689 DCHECK_NE(ref_reg, LR);
690 DCHECK_NE(ref_reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100691 // IP is used internally by the ReadBarrierMarkRegX entry point
692 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100693 DCHECK_NE(ref_reg, IP);
694 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100695 // "Compact" slow path, saving two moves.
696 //
697 // Instead of using the standard runtime calling convention (input
698 // and output in R0):
699 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100700 // R0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100701 // R0 <- ReadBarrierMark(R0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100702 // ref <- R0
Roland Levillain02b75802016-07-13 11:54:35 +0100703 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100704 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100705 // of a dedicated entrypoint:
706 //
707 // rX <- ReadBarrierMarkRegX(rX)
708 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800709 if (entrypoint_.IsValid()) {
710 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
711 __ blx(entrypoint_.AsRegister<Register>());
712 } else {
713 int32_t entry_point_offset =
714 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
715 // This runtime call does not require a stack map.
716 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
717 }
Roland Levillainc9285912015-12-18 10:38:42 +0000718 __ b(GetExitLabel());
719 }
720
721 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100722 // The location (register) of the marked object reference.
723 const Location ref_;
Roland Levillainc9285912015-12-18 10:38:42 +0000724
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800725 // The location of the entrypoint if already loaded.
726 const Location entrypoint_;
727
Roland Levillainc9285912015-12-18 10:38:42 +0000728 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
729};
730
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100731// Slow path marking an object reference `ref` during a read barrier,
732// and if needed, atomically updating the field `obj.field` in the
733// object `obj` holding this reference after marking (contrary to
734// ReadBarrierMarkSlowPathARM above, which never tries to update
735// `obj.field`).
736//
737// This means that after the execution of this slow path, both `ref`
738// and `obj.field` will be up-to-date; i.e., after the flip, both will
739// hold the same to-space reference (unless another thread installed
740// another object reference (different from `ref`) in `obj.field`).
741class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
742 public:
743 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
744 Location ref,
745 Register obj,
746 Location field_offset,
747 Register temp1,
748 Register temp2)
749 : SlowPathCodeARM(instruction),
750 ref_(ref),
751 obj_(obj),
752 field_offset_(field_offset),
753 temp1_(temp1),
754 temp2_(temp2) {
755 DCHECK(kEmitCompilerReadBarrier);
756 }
757
758 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
759
760 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
761 LocationSummary* locations = instruction_->GetLocations();
762 Register ref_reg = ref_.AsRegister<Register>();
763 DCHECK(locations->CanCall());
764 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
765 // This slow path is only used by the UnsafeCASObject intrinsic.
766 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
767 << "Unexpected instruction in read barrier marking and field updating slow path: "
768 << instruction_->DebugName();
769 DCHECK(instruction_->GetLocations()->Intrinsified());
770 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
771 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
772
773 __ Bind(GetEntryLabel());
774
775 // Save the old reference.
776 // Note that we cannot use IP to save the old reference, as IP is
777 // used internally by the ReadBarrierMarkRegX entry point, and we
778 // need the old reference after the call to that entry point.
779 DCHECK_NE(temp1_, IP);
780 __ Mov(temp1_, ref_reg);
781
782 // No need to save live registers; it's taken care of by the
783 // entrypoint. Also, there is no need to update the stack mask,
784 // as this runtime call will not trigger a garbage collection.
785 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
786 DCHECK_NE(ref_reg, SP);
787 DCHECK_NE(ref_reg, LR);
788 DCHECK_NE(ref_reg, PC);
789 // IP is used internally by the ReadBarrierMarkRegX entry point
790 // as a temporary, it cannot be the entry point's input/output.
791 DCHECK_NE(ref_reg, IP);
792 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
793 // "Compact" slow path, saving two moves.
794 //
795 // Instead of using the standard runtime calling convention (input
796 // and output in R0):
797 //
798 // R0 <- ref
799 // R0 <- ReadBarrierMark(R0)
800 // ref <- R0
801 //
802 // we just use rX (the register containing `ref`) as input and output
803 // of a dedicated entrypoint:
804 //
805 // rX <- ReadBarrierMarkRegX(rX)
806 //
807 int32_t entry_point_offset =
808 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
809 // This runtime call does not require a stack map.
810 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
811
812 // If the new reference is different from the old reference,
813 // update the field in the holder (`*(obj_ + field_offset_)`).
814 //
815 // Note that this field could also hold a different object, if
816 // another thread had concurrently changed it. In that case, the
817 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
818 // (CAS) operation below would abort the CAS, leaving the field
819 // as-is.
820 Label done;
821 __ cmp(temp1_, ShifterOperand(ref_reg));
822 __ b(&done, EQ);
823
824 // Update the the holder's field atomically. This may fail if
825 // mutator updates before us, but it's OK. This is achieved
826 // using a strong compare-and-set (CAS) operation with relaxed
827 // memory synchronization ordering, where the expected value is
828 // the old reference and the desired value is the new reference.
829
830 // Convenience aliases.
831 Register base = obj_;
832 // The UnsafeCASObject intrinsic uses a register pair as field
833 // offset ("long offset"), of which only the low part contains
834 // data.
835 Register offset = field_offset_.AsRegisterPairLow<Register>();
836 Register expected = temp1_;
837 Register value = ref_reg;
838 Register tmp_ptr = IP; // Pointer to actual memory.
839 Register tmp = temp2_; // Value in memory.
840
841 __ add(tmp_ptr, base, ShifterOperand(offset));
842
843 if (kPoisonHeapReferences) {
844 __ PoisonHeapReference(expected);
845 if (value == expected) {
846 // Do not poison `value`, as it is the same register as
847 // `expected`, which has just been poisoned.
848 } else {
849 __ PoisonHeapReference(value);
850 }
851 }
852
853 // do {
854 // tmp = [r_ptr] - expected;
855 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
856
Roland Levillain24a4d112016-10-26 13:10:46 +0100857 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100858 __ Bind(&loop_head);
859
860 __ ldrex(tmp, tmp_ptr);
861
862 __ subs(tmp, tmp, ShifterOperand(expected));
863
Roland Levillain24a4d112016-10-26 13:10:46 +0100864 __ it(NE);
865 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100866
Roland Levillain24a4d112016-10-26 13:10:46 +0100867 __ b(&exit_loop, NE);
868
869 __ strex(tmp, value, tmp_ptr);
870 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100871 __ b(&loop_head, EQ);
872
Roland Levillain24a4d112016-10-26 13:10:46 +0100873 __ Bind(&exit_loop);
874
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100875 if (kPoisonHeapReferences) {
876 __ UnpoisonHeapReference(expected);
877 if (value == expected) {
878 // Do not unpoison `value`, as it is the same register as
879 // `expected`, which has just been unpoisoned.
880 } else {
881 __ UnpoisonHeapReference(value);
882 }
883 }
884
885 __ Bind(&done);
886 __ b(GetExitLabel());
887 }
888
889 private:
890 // The location (register) of the marked object reference.
891 const Location ref_;
892 // The register containing the object holding the marked object reference field.
893 const Register obj_;
894 // The location of the offset of the marked reference field within `obj_`.
895 Location field_offset_;
896
897 const Register temp1_;
898 const Register temp2_;
899
900 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
901};
902
Roland Levillain3b359c72015-11-17 19:35:12 +0000903// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100904class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000905 public:
906 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
907 Location out,
908 Location ref,
909 Location obj,
910 uint32_t offset,
911 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100912 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000913 out_(out),
914 ref_(ref),
915 obj_(obj),
916 offset_(offset),
917 index_(index) {
918 DCHECK(kEmitCompilerReadBarrier);
919 // If `obj` is equal to `out` or `ref`, it means the initial object
920 // has been overwritten by (or after) the heap object reference load
921 // to be instrumented, e.g.:
922 //
923 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000924 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000925 //
926 // In that case, we have lost the information about the original
927 // object, and the emitted read barrier cannot work properly.
928 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
929 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
930 }
931
932 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
933 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
934 LocationSummary* locations = instruction_->GetLocations();
935 Register reg_out = out_.AsRegister<Register>();
936 DCHECK(locations->CanCall());
937 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100938 DCHECK(instruction_->IsInstanceFieldGet() ||
939 instruction_->IsStaticFieldGet() ||
940 instruction_->IsArrayGet() ||
941 instruction_->IsInstanceOf() ||
942 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100943 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000944 << "Unexpected instruction in read barrier for heap reference slow path: "
945 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000946 // The read barrier instrumentation of object ArrayGet
947 // instructions does not support the HIntermediateAddress
948 // instruction.
949 DCHECK(!(instruction_->IsArrayGet() &&
950 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +0000951
952 __ Bind(GetEntryLabel());
953 SaveLiveRegisters(codegen, locations);
954
955 // We may have to change the index's value, but as `index_` is a
956 // constant member (like other "inputs" of this slow path),
957 // introduce a copy of it, `index`.
958 Location index = index_;
959 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100960 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000961 if (instruction_->IsArrayGet()) {
962 // Compute the actual memory offset and store it in `index`.
963 Register index_reg = index_.AsRegister<Register>();
964 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
965 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
966 // We are about to change the value of `index_reg` (see the
967 // calls to art::arm::Thumb2Assembler::Lsl and
968 // art::arm::Thumb2Assembler::AddConstant below), but it has
969 // not been saved by the previous call to
970 // art::SlowPathCode::SaveLiveRegisters, as it is a
971 // callee-save register --
972 // art::SlowPathCode::SaveLiveRegisters does not consider
973 // callee-save registers, as it has been designed with the
974 // assumption that callee-save registers are supposed to be
975 // handled by the called function. So, as a callee-save
976 // register, `index_reg` _would_ eventually be saved onto
977 // the stack, but it would be too late: we would have
978 // changed its value earlier. Therefore, we manually save
979 // it here into another freely available register,
980 // `free_reg`, chosen of course among the caller-save
981 // registers (as a callee-save `free_reg` register would
982 // exhibit the same problem).
983 //
984 // Note we could have requested a temporary register from
985 // the register allocator instead; but we prefer not to, as
986 // this is a slow path, and we know we can find a
987 // caller-save register that is available.
988 Register free_reg = FindAvailableCallerSaveRegister(codegen);
989 __ Mov(free_reg, index_reg);
990 index_reg = free_reg;
991 index = Location::RegisterLocation(index_reg);
992 } else {
993 // The initial register stored in `index_` has already been
994 // saved in the call to art::SlowPathCode::SaveLiveRegisters
995 // (as it is not a callee-save register), so we can freely
996 // use it.
997 }
998 // Shifting the index value contained in `index_reg` by the scale
999 // factor (2) cannot overflow in practice, as the runtime is
1000 // unable to allocate object arrays with a size larger than
1001 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1002 __ Lsl(index_reg, index_reg, TIMES_4);
1003 static_assert(
1004 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1005 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1006 __ AddConstant(index_reg, index_reg, offset_);
1007 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001008 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1009 // intrinsics, `index_` is not shifted by a scale factor of 2
1010 // (as in the case of ArrayGet), as it is actually an offset
1011 // to an object field within an object.
1012 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001013 DCHECK(instruction_->GetLocations()->Intrinsified());
1014 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1015 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1016 << instruction_->AsInvoke()->GetIntrinsic();
1017 DCHECK_EQ(offset_, 0U);
1018 DCHECK(index_.IsRegisterPair());
1019 // UnsafeGet's offset location is a register pair, the low
1020 // part contains the correct offset.
1021 index = index_.ToLow();
1022 }
1023 }
1024
1025 // We're moving two or three locations to locations that could
1026 // overlap, so we need a parallel move resolver.
1027 InvokeRuntimeCallingConvention calling_convention;
1028 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1029 parallel_move.AddMove(ref_,
1030 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1031 Primitive::kPrimNot,
1032 nullptr);
1033 parallel_move.AddMove(obj_,
1034 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1035 Primitive::kPrimNot,
1036 nullptr);
1037 if (index.IsValid()) {
1038 parallel_move.AddMove(index,
1039 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1040 Primitive::kPrimInt,
1041 nullptr);
1042 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1043 } else {
1044 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1045 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1046 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001047 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001048 CheckEntrypointTypes<
1049 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1050 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1051
1052 RestoreLiveRegisters(codegen, locations);
1053 __ b(GetExitLabel());
1054 }
1055
1056 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1057
1058 private:
1059 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1060 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1061 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1062 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1063 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1064 return static_cast<Register>(i);
1065 }
1066 }
1067 // We shall never fail to find a free caller-save register, as
1068 // there are more than two core caller-save registers on ARM
1069 // (meaning it is possible to find one which is different from
1070 // `ref` and `obj`).
1071 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1072 LOG(FATAL) << "Could not find a free caller-save register";
1073 UNREACHABLE();
1074 }
1075
Roland Levillain3b359c72015-11-17 19:35:12 +00001076 const Location out_;
1077 const Location ref_;
1078 const Location obj_;
1079 const uint32_t offset_;
1080 // An additional location containing an index to an array.
1081 // Only used for HArrayGet and the UnsafeGetObject &
1082 // UnsafeGetObjectVolatile intrinsics.
1083 const Location index_;
1084
1085 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1086};
1087
1088// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001089class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001090 public:
1091 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001092 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001093 DCHECK(kEmitCompilerReadBarrier);
1094 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001095
1096 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1097 LocationSummary* locations = instruction_->GetLocations();
1098 Register reg_out = out_.AsRegister<Register>();
1099 DCHECK(locations->CanCall());
1100 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001101 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1102 << "Unexpected instruction in read barrier for GC root slow path: "
1103 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001104
1105 __ Bind(GetEntryLabel());
1106 SaveLiveRegisters(codegen, locations);
1107
1108 InvokeRuntimeCallingConvention calling_convention;
1109 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1110 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001111 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001112 instruction_,
1113 instruction_->GetDexPc(),
1114 this);
1115 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1116 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1117
1118 RestoreLiveRegisters(codegen, locations);
1119 __ b(GetExitLabel());
1120 }
1121
1122 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1123
1124 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001125 const Location out_;
1126 const Location root_;
1127
1128 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1129};
1130
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001131#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001132// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1133#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -07001134
Aart Bike9f37602015-10-09 11:15:55 -07001135inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001136 switch (cond) {
1137 case kCondEQ: return EQ;
1138 case kCondNE: return NE;
1139 case kCondLT: return LT;
1140 case kCondLE: return LE;
1141 case kCondGT: return GT;
1142 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001143 case kCondB: return LO;
1144 case kCondBE: return LS;
1145 case kCondA: return HI;
1146 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001147 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001148 LOG(FATAL) << "Unreachable";
1149 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001150}
1151
Aart Bike9f37602015-10-09 11:15:55 -07001152// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001153inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001154 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001155 case kCondEQ: return EQ;
1156 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001157 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001158 case kCondLT: return LO;
1159 case kCondLE: return LS;
1160 case kCondGT: return HI;
1161 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001162 // Unsigned remain unchanged.
1163 case kCondB: return LO;
1164 case kCondBE: return LS;
1165 case kCondA: return HI;
1166 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001167 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001168 LOG(FATAL) << "Unreachable";
1169 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001170}
1171
Vladimir Markod6e069b2016-01-18 11:11:01 +00001172inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1173 // The ARM condition codes can express all the necessary branches, see the
1174 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1175 // There is no dex instruction or HIR that would need the missing conditions
1176 // "equal or unordered" or "not equal".
1177 switch (cond) {
1178 case kCondEQ: return EQ;
1179 case kCondNE: return NE /* unordered */;
1180 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1181 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1182 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1183 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1184 default:
1185 LOG(FATAL) << "UNREACHABLE";
1186 UNREACHABLE();
1187 }
1188}
1189
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001190void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001191 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001192}
1193
1194void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001195 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001196}
1197
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001198size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1199 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1200 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001201}
1202
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001203size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1204 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1205 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001206}
1207
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001208size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1209 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1210 return kArmWordSize;
1211}
1212
1213size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1214 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1215 return kArmWordSize;
1216}
1217
Calin Juravle34166012014-12-19 17:22:29 +00001218CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001219 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001220 const CompilerOptions& compiler_options,
1221 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001222 : CodeGenerator(graph,
1223 kNumberOfCoreRegisters,
1224 kNumberOfSRegisters,
1225 kNumberOfRegisterPairs,
1226 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1227 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001228 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1229 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001230 compiler_options,
1231 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001232 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001233 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001234 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001235 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001236 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001237 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001238 uint32_literals_(std::less<uint32_t>(),
1239 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001240 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1241 boot_image_string_patches_(StringReferenceValueComparator(),
1242 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1243 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001244 boot_image_type_patches_(TypeReferenceValueComparator(),
1245 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1246 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001247 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001248 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001249 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1250 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001251 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1252 jit_class_patches_(TypeReferenceValueComparator(),
1253 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001254 // Always save the LR register to mimic Quick.
1255 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001256}
1257
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001258void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1259 // Ensure that we fix up branches and literal loads and emit the literal pool.
1260 __ FinalizeCode();
1261
1262 // Adjust native pc offsets in stack maps.
1263 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001264 uint32_t old_position =
1265 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001266 uint32_t new_position = __ GetAdjustedPosition(old_position);
1267 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1268 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001269 // Adjust pc offsets for the disassembly information.
1270 if (disasm_info_ != nullptr) {
1271 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1272 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1273 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1274 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1275 it.second.start = __ GetAdjustedPosition(it.second.start);
1276 it.second.end = __ GetAdjustedPosition(it.second.end);
1277 }
1278 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1279 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1280 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1281 }
1282 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001283
1284 CodeGenerator::Finalize(allocator);
1285}
1286
David Brazdil58282f42016-01-14 12:45:10 +00001287void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001288 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001289 blocked_core_registers_[SP] = true;
1290 blocked_core_registers_[LR] = true;
1291 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001292
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001293 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001294 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001295
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001296 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001297 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001298
David Brazdil58282f42016-01-14 12:45:10 +00001299 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001300 // Stubs do not save callee-save floating point registers. If the graph
1301 // is debuggable, we need to deal with these registers differently. For
1302 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001303 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1304 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1305 }
1306 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001307}
1308
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001309InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001310 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001311 assembler_(codegen->GetAssembler()),
1312 codegen_(codegen) {}
1313
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001314void CodeGeneratorARM::ComputeSpillMask() {
1315 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1316 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001317 // There is no easy instruction to restore just the PC on thumb2. We spill and
1318 // restore another arbitrary register.
1319 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001320 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1321 // We use vpush and vpop for saving and restoring floating point registers, which take
1322 // a SRegister and the number of registers to save/restore after that SRegister. We
1323 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1324 // but in the range.
1325 if (fpu_spill_mask_ != 0) {
1326 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1327 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1328 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1329 fpu_spill_mask_ |= (1 << i);
1330 }
1331 }
1332}
1333
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001334static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001335 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001336}
1337
1338static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001339 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001340}
1341
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001342void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001343 bool skip_overflow_check =
1344 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001345 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001346 __ Bind(&frame_entry_label_);
1347
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001348 if (HasEmptyFrame()) {
1349 return;
1350 }
1351
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001352 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001353 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1354 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1355 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001356 }
1357
Andreas Gampe501fd632015-09-10 16:11:06 -07001358 __ PushList(core_spill_mask_);
1359 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1360 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001361 if (fpu_spill_mask_ != 0) {
1362 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1363 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001364 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001365 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001366 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001367
1368 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1369 // Initialize should_deoptimize flag to 0.
1370 __ mov(IP, ShifterOperand(0));
1371 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
1372 }
1373
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001374 int adjust = GetFrameSize() - FrameEntrySpillSize();
1375 __ AddConstant(SP, -adjust);
1376 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001377
1378 // Save the current method if we need it. Note that we do not
1379 // do this in HCurrentMethod, as the instruction might have been removed
1380 // in the SSA graph.
1381 if (RequiresCurrentMethod()) {
1382 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1383 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001384}
1385
1386void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001387 if (HasEmptyFrame()) {
1388 __ bx(LR);
1389 return;
1390 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001391 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001392 int adjust = GetFrameSize() - FrameEntrySpillSize();
1393 __ AddConstant(SP, adjust);
1394 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001395 if (fpu_spill_mask_ != 0) {
1396 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1397 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001398 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001399 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001400 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001401 // Pop LR into PC to return.
1402 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1403 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1404 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001405 __ cfi().RestoreState();
1406 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001407}
1408
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001409void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001410 Label* label = GetLabelOf(block);
1411 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001412}
1413
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001414Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001415 switch (type) {
1416 case Primitive::kPrimBoolean:
1417 case Primitive::kPrimByte:
1418 case Primitive::kPrimChar:
1419 case Primitive::kPrimShort:
1420 case Primitive::kPrimInt:
1421 case Primitive::kPrimNot: {
1422 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001423 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001424 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001425 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001426 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001427 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001428 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001429 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001430
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001431 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001432 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001433 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001434 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001435 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001436 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001437 if (calling_convention.GetRegisterAt(index) == R1) {
1438 // Skip R1, and use R2_R3 instead.
1439 gp_index_++;
1440 index++;
1441 }
1442 }
1443 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1444 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001445 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001446
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001447 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001448 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001449 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001450 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1451 }
1452 }
1453
1454 case Primitive::kPrimFloat: {
1455 uint32_t stack_index = stack_index_++;
1456 if (float_index_ % 2 == 0) {
1457 float_index_ = std::max(double_index_, float_index_);
1458 }
1459 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1460 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1461 } else {
1462 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1463 }
1464 }
1465
1466 case Primitive::kPrimDouble: {
1467 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1468 uint32_t stack_index = stack_index_;
1469 stack_index_ += 2;
1470 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1471 uint32_t index = double_index_;
1472 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001473 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001474 calling_convention.GetFpuRegisterAt(index),
1475 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001476 DCHECK(ExpectedPairLayout(result));
1477 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001478 } else {
1479 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001480 }
1481 }
1482
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001483 case Primitive::kPrimVoid:
1484 LOG(FATAL) << "Unexpected parameter type " << type;
1485 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001486 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001487 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001488}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001489
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001490Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001491 switch (type) {
1492 case Primitive::kPrimBoolean:
1493 case Primitive::kPrimByte:
1494 case Primitive::kPrimChar:
1495 case Primitive::kPrimShort:
1496 case Primitive::kPrimInt:
1497 case Primitive::kPrimNot: {
1498 return Location::RegisterLocation(R0);
1499 }
1500
1501 case Primitive::kPrimFloat: {
1502 return Location::FpuRegisterLocation(S0);
1503 }
1504
1505 case Primitive::kPrimLong: {
1506 return Location::RegisterPairLocation(R0, R1);
1507 }
1508
1509 case Primitive::kPrimDouble: {
1510 return Location::FpuRegisterPairLocation(S0, S1);
1511 }
1512
1513 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001514 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001515 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001516
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001517 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001518}
1519
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001520Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1521 return Location::RegisterLocation(kMethodRegisterArgument);
1522}
1523
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001524void CodeGeneratorARM::Move32(Location destination, Location source) {
1525 if (source.Equals(destination)) {
1526 return;
1527 }
1528 if (destination.IsRegister()) {
1529 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001530 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001531 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001532 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001533 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001534 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001535 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001536 } else if (destination.IsFpuRegister()) {
1537 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001538 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001539 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001540 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001541 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001542 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001543 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001544 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001545 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001546 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001547 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001548 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001549 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001550 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001551 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001552 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1553 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001554 }
1555 }
1556}
1557
1558void CodeGeneratorARM::Move64(Location destination, Location source) {
1559 if (source.Equals(destination)) {
1560 return;
1561 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001562 if (destination.IsRegisterPair()) {
1563 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001564 EmitParallelMoves(
1565 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1566 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001567 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001568 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001569 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1570 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001571 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001572 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001573 } else if (source.IsFpuRegisterPair()) {
1574 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1575 destination.AsRegisterPairHigh<Register>(),
1576 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001577 } else {
1578 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001579 DCHECK(ExpectedPairLayout(destination));
1580 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1581 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001582 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001583 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001584 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001585 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1586 SP,
1587 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001588 } else if (source.IsRegisterPair()) {
1589 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1590 source.AsRegisterPairLow<Register>(),
1591 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001592 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001593 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001594 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001595 } else {
1596 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001597 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001598 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001599 if (source.AsRegisterPairLow<Register>() == R1) {
1600 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001601 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1602 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001603 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001604 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001605 SP, destination.GetStackIndex());
1606 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001607 } else if (source.IsFpuRegisterPair()) {
1608 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1609 SP,
1610 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001611 } else {
1612 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001613 EmitParallelMoves(
1614 Location::StackSlot(source.GetStackIndex()),
1615 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001616 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001617 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001618 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1619 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001620 }
1621 }
1622}
1623
Calin Juravle175dc732015-08-25 15:42:32 +01001624void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1625 DCHECK(location.IsRegister());
1626 __ LoadImmediate(location.AsRegister<Register>(), value);
1627}
1628
Calin Juravlee460d1d2015-09-29 04:52:17 +01001629void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001630 HParallelMove move(GetGraph()->GetArena());
1631 move.AddMove(src, dst, dst_type, nullptr);
1632 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001633}
1634
1635void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1636 if (location.IsRegister()) {
1637 locations->AddTemp(location);
1638 } else if (location.IsRegisterPair()) {
1639 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1640 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1641 } else {
1642 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1643 }
1644}
1645
Calin Juravle175dc732015-08-25 15:42:32 +01001646void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1647 HInstruction* instruction,
1648 uint32_t dex_pc,
1649 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001650 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001651 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001652 if (EntrypointRequiresStackMap(entrypoint)) {
1653 RecordPcInfo(instruction, dex_pc, slow_path);
1654 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001655}
1656
Roland Levillaindec8f632016-07-22 17:10:06 +01001657void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1658 HInstruction* instruction,
1659 SlowPathCode* slow_path) {
1660 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001661 GenerateInvokeRuntime(entry_point_offset);
1662}
1663
1664void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001665 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1666 __ blx(LR);
1667}
1668
David Brazdilfc6a86a2015-06-26 10:33:45 +00001669void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001670 DCHECK(!successor->IsExitBlock());
1671
1672 HBasicBlock* block = got->GetBlock();
1673 HInstruction* previous = got->GetPrevious();
1674
1675 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001676 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001677 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1678 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1679 return;
1680 }
1681
1682 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1683 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1684 }
1685 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001686 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001687 }
1688}
1689
David Brazdilfc6a86a2015-06-26 10:33:45 +00001690void LocationsBuilderARM::VisitGoto(HGoto* got) {
1691 got->SetLocations(nullptr);
1692}
1693
1694void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1695 HandleGoto(got, got->GetSuccessor());
1696}
1697
1698void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1699 try_boundary->SetLocations(nullptr);
1700}
1701
1702void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1703 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1704 if (!successor->IsExitBlock()) {
1705 HandleGoto(try_boundary, successor);
1706 }
1707}
1708
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001709void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001710 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001711}
1712
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001713void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001714}
1715
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001716void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1717 Primitive::Type type = instruction->InputAt(0)->GetType();
1718 Location lhs_loc = instruction->GetLocations()->InAt(0);
1719 Location rhs_loc = instruction->GetLocations()->InAt(1);
1720 if (rhs_loc.IsConstant()) {
1721 // 0.0 is the only immediate that can be encoded directly in
1722 // a VCMP instruction.
1723 //
1724 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1725 // specify that in a floating-point comparison, positive zero
1726 // and negative zero are considered equal, so we can use the
1727 // literal 0.0 for both cases here.
1728 //
1729 // Note however that some methods (Float.equal, Float.compare,
1730 // Float.compareTo, Double.equal, Double.compare,
1731 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1732 // StrictMath.min) consider 0.0 to be (strictly) greater than
1733 // -0.0. So if we ever translate calls to these methods into a
1734 // HCompare instruction, we must handle the -0.0 case with
1735 // care here.
1736 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1737 if (type == Primitive::kPrimFloat) {
1738 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1739 } else {
1740 DCHECK_EQ(type, Primitive::kPrimDouble);
1741 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1742 }
1743 } else {
1744 if (type == Primitive::kPrimFloat) {
1745 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1746 } else {
1747 DCHECK_EQ(type, Primitive::kPrimDouble);
1748 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1749 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1750 }
1751 }
1752}
1753
Roland Levillain4fa13f62015-07-06 18:11:54 +01001754void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1755 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001756 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001757 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001758 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001759}
1760
1761void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1762 Label* true_label,
1763 Label* false_label) {
1764 LocationSummary* locations = cond->GetLocations();
1765 Location left = locations->InAt(0);
1766 Location right = locations->InAt(1);
1767 IfCondition if_cond = cond->GetCondition();
1768
1769 Register left_high = left.AsRegisterPairHigh<Register>();
1770 Register left_low = left.AsRegisterPairLow<Register>();
1771 IfCondition true_high_cond = if_cond;
1772 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001773 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001774
1775 // Set the conditions for the test, remembering that == needs to be
1776 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001777 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001778 switch (if_cond) {
1779 case kCondEQ:
1780 case kCondNE:
1781 // Nothing to do.
1782 break;
1783 case kCondLT:
1784 false_high_cond = kCondGT;
1785 break;
1786 case kCondLE:
1787 true_high_cond = kCondLT;
1788 break;
1789 case kCondGT:
1790 false_high_cond = kCondLT;
1791 break;
1792 case kCondGE:
1793 true_high_cond = kCondGT;
1794 break;
Aart Bike9f37602015-10-09 11:15:55 -07001795 case kCondB:
1796 false_high_cond = kCondA;
1797 break;
1798 case kCondBE:
1799 true_high_cond = kCondB;
1800 break;
1801 case kCondA:
1802 false_high_cond = kCondB;
1803 break;
1804 case kCondAE:
1805 true_high_cond = kCondA;
1806 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001807 }
1808 if (right.IsConstant()) {
1809 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1810 int32_t val_low = Low32Bits(value);
1811 int32_t val_high = High32Bits(value);
1812
Vladimir Markoac6ac102015-12-17 12:14:00 +00001813 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001814 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001815 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001816 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001817 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001818 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001819 __ b(true_label, ARMCondition(true_high_cond));
1820 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001821 }
1822 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001823 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001824 } else {
1825 Register right_high = right.AsRegisterPairHigh<Register>();
1826 Register right_low = right.AsRegisterPairLow<Register>();
1827
1828 __ cmp(left_high, ShifterOperand(right_high));
1829 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001830 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001831 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001832 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001833 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001834 __ b(true_label, ARMCondition(true_high_cond));
1835 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001836 }
1837 // Must be equal high, so compare the lows.
1838 __ cmp(left_low, ShifterOperand(right_low));
1839 }
1840 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001841 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001842 __ b(true_label, final_condition);
1843}
1844
David Brazdil0debae72015-11-12 18:37:00 +00001845void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1846 Label* true_target_in,
1847 Label* false_target_in) {
1848 // Generated branching requires both targets to be explicit. If either of the
1849 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1850 Label fallthrough_target;
1851 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1852 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1853
Roland Levillain4fa13f62015-07-06 18:11:54 +01001854 Primitive::Type type = condition->InputAt(0)->GetType();
1855 switch (type) {
1856 case Primitive::kPrimLong:
1857 GenerateLongComparesAndJumps(condition, true_target, false_target);
1858 break;
1859 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001860 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001861 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001862 GenerateFPJumps(condition, true_target, false_target);
1863 break;
1864 default:
1865 LOG(FATAL) << "Unexpected compare type " << type;
1866 }
1867
David Brazdil0debae72015-11-12 18:37:00 +00001868 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001869 __ b(false_target);
1870 }
David Brazdil0debae72015-11-12 18:37:00 +00001871
1872 if (fallthrough_target.IsLinked()) {
1873 __ Bind(&fallthrough_target);
1874 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001875}
1876
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001877void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001878 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001879 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001880 Label* false_target) {
1881 HInstruction* cond = instruction->InputAt(condition_input_index);
1882
1883 if (true_target == nullptr && false_target == nullptr) {
1884 // Nothing to do. The code always falls through.
1885 return;
1886 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001887 // Constant condition, statically compared against "true" (integer value 1).
1888 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001889 if (true_target != nullptr) {
1890 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001891 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001892 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001893 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001894 if (false_target != nullptr) {
1895 __ b(false_target);
1896 }
1897 }
1898 return;
1899 }
1900
1901 // The following code generates these patterns:
1902 // (1) true_target == nullptr && false_target != nullptr
1903 // - opposite condition true => branch to false_target
1904 // (2) true_target != nullptr && false_target == nullptr
1905 // - condition true => branch to true_target
1906 // (3) true_target != nullptr && false_target != nullptr
1907 // - condition true => branch to true_target
1908 // - branch to false_target
1909 if (IsBooleanValueOrMaterializedCondition(cond)) {
1910 // Condition has been materialized, compare the output to 0.
1911 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1912 DCHECK(cond_val.IsRegister());
1913 if (true_target == nullptr) {
1914 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1915 } else {
1916 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001917 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001918 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001919 // Condition has not been materialized. Use its inputs as the comparison and
1920 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001921 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001922
1923 // If this is a long or FP comparison that has been folded into
1924 // the HCondition, generate the comparison directly.
1925 Primitive::Type type = condition->InputAt(0)->GetType();
1926 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1927 GenerateCompareTestAndBranch(condition, true_target, false_target);
1928 return;
1929 }
1930
1931 LocationSummary* locations = cond->GetLocations();
1932 DCHECK(locations->InAt(0).IsRegister());
1933 Register left = locations->InAt(0).AsRegister<Register>();
1934 Location right = locations->InAt(1);
1935 if (right.IsRegister()) {
1936 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001937 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001938 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001939 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001940 }
1941 if (true_target == nullptr) {
1942 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1943 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001944 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001945 }
Dave Allison20dfc792014-06-16 20:44:29 -07001946 }
David Brazdil0debae72015-11-12 18:37:00 +00001947
1948 // If neither branch falls through (case 3), the conditional branch to `true_target`
1949 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1950 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001951 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001952 }
1953}
1954
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001955void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001956 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1957 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001958 locations->SetInAt(0, Location::RequiresRegister());
1959 }
1960}
1961
1962void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001963 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1964 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1965 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1966 nullptr : codegen_->GetLabelOf(true_successor);
1967 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1968 nullptr : codegen_->GetLabelOf(false_successor);
1969 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001970}
1971
1972void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1973 LocationSummary* locations = new (GetGraph()->GetArena())
1974 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001975 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001976 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001977 locations->SetInAt(0, Location::RequiresRegister());
1978 }
1979}
1980
1981void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001982 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001983 GenerateTestAndBranch(deoptimize,
1984 /* condition_input_index */ 0,
1985 slow_path->GetEntryLabel(),
1986 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001987}
Dave Allison20dfc792014-06-16 20:44:29 -07001988
Mingyao Yang063fc772016-08-02 11:02:54 -07001989void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1990 LocationSummary* locations = new (GetGraph()->GetArena())
1991 LocationSummary(flag, LocationSummary::kNoCall);
1992 locations->SetOut(Location::RequiresRegister());
1993}
1994
1995void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1996 __ LoadFromOffset(kLoadWord,
1997 flag->GetLocations()->Out().AsRegister<Register>(),
1998 SP,
1999 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2000}
2001
David Brazdil74eb1b22015-12-14 11:44:01 +00002002void LocationsBuilderARM::VisitSelect(HSelect* select) {
2003 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2004 if (Primitive::IsFloatingPointType(select->GetType())) {
2005 locations->SetInAt(0, Location::RequiresFpuRegister());
2006 locations->SetInAt(1, Location::RequiresFpuRegister());
2007 } else {
2008 locations->SetInAt(0, Location::RequiresRegister());
2009 locations->SetInAt(1, Location::RequiresRegister());
2010 }
2011 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2012 locations->SetInAt(2, Location::RequiresRegister());
2013 }
2014 locations->SetOut(Location::SameAsFirstInput());
2015}
2016
2017void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
2018 LocationSummary* locations = select->GetLocations();
2019 Label false_target;
2020 GenerateTestAndBranch(select,
2021 /* condition_input_index */ 2,
2022 /* true_target */ nullptr,
2023 &false_target);
2024 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2025 __ Bind(&false_target);
2026}
2027
David Srbecky0cf44932015-12-09 14:09:59 +00002028void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2029 new (GetGraph()->GetArena()) LocationSummary(info);
2030}
2031
David Srbeckyd28f4a02016-03-14 17:14:24 +00002032void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2033 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002034}
2035
2036void CodeGeneratorARM::GenerateNop() {
2037 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002038}
2039
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002040void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002041 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002042 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002043 // Handle the long/FP comparisons made in instruction simplification.
2044 switch (cond->InputAt(0)->GetType()) {
2045 case Primitive::kPrimLong:
2046 locations->SetInAt(0, Location::RequiresRegister());
2047 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002048 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002049 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2050 }
2051 break;
2052
2053 case Primitive::kPrimFloat:
2054 case Primitive::kPrimDouble:
2055 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002056 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002057 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002058 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2059 }
2060 break;
2061
2062 default:
2063 locations->SetInAt(0, Location::RequiresRegister());
2064 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002065 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002066 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2067 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002068 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002069}
2070
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002071void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002072 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002073 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002074 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002075
2076 LocationSummary* locations = cond->GetLocations();
2077 Location left = locations->InAt(0);
2078 Location right = locations->InAt(1);
2079 Register out = locations->Out().AsRegister<Register>();
2080 Label true_label, false_label;
2081
2082 switch (cond->InputAt(0)->GetType()) {
2083 default: {
2084 // Integer case.
2085 if (right.IsRegister()) {
2086 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2087 } else {
2088 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002089 __ CmpConstant(left.AsRegister<Register>(),
2090 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002091 }
Aart Bike9f37602015-10-09 11:15:55 -07002092 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002093 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002094 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002095 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002096 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002097 return;
2098 }
2099 case Primitive::kPrimLong:
2100 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2101 break;
2102 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002103 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002104 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002105 GenerateFPJumps(cond, &true_label, &false_label);
2106 break;
2107 }
2108
2109 // Convert the jumps into the result.
2110 Label done_label;
2111
2112 // False case: result = 0.
2113 __ Bind(&false_label);
2114 __ LoadImmediate(out, 0);
2115 __ b(&done_label);
2116
2117 // True case: result = 1.
2118 __ Bind(&true_label);
2119 __ LoadImmediate(out, 1);
2120 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002121}
2122
2123void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002124 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002125}
2126
2127void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002128 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002129}
2130
2131void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002132 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002133}
2134
2135void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002136 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002137}
2138
2139void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002140 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002141}
2142
2143void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002144 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002145}
2146
2147void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002148 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002149}
2150
2151void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002152 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002153}
2154
2155void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002156 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002157}
2158
2159void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002160 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002161}
2162
2163void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002164 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002165}
2166
2167void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002168 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002169}
2170
Aart Bike9f37602015-10-09 11:15:55 -07002171void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002172 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002173}
2174
2175void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002176 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002177}
2178
2179void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002180 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002181}
2182
2183void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002184 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002185}
2186
2187void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002188 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002189}
2190
2191void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002192 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002193}
2194
2195void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002196 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002197}
2198
2199void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002200 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002201}
2202
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002203void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002204 LocationSummary* locations =
2205 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002206 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002207}
2208
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002209void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002210 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002211}
2212
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002213void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2214 LocationSummary* locations =
2215 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2216 locations->SetOut(Location::ConstantLocation(constant));
2217}
2218
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002219void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002220 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002221}
2222
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002223void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002224 LocationSummary* locations =
2225 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002226 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002227}
2228
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002229void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002230 // Will be generated at use site.
2231}
2232
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002233void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2234 LocationSummary* locations =
2235 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2236 locations->SetOut(Location::ConstantLocation(constant));
2237}
2238
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002239void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002240 // Will be generated at use site.
2241}
2242
2243void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2244 LocationSummary* locations =
2245 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2246 locations->SetOut(Location::ConstantLocation(constant));
2247}
2248
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002249void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002250 // Will be generated at use site.
2251}
2252
Calin Juravle27df7582015-04-17 19:12:31 +01002253void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2254 memory_barrier->SetLocations(nullptr);
2255}
2256
2257void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002258 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002259}
2260
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002261void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002262 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002263}
2264
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002265void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002266 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002267}
2268
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002269void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002270 LocationSummary* locations =
2271 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002272 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002273}
2274
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002275void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002276 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002277}
2278
Calin Juravle175dc732015-08-25 15:42:32 +01002279void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2280 // The trampoline uses the same calling convention as dex calling conventions,
2281 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2282 // the method_idx.
2283 HandleInvoke(invoke);
2284}
2285
2286void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2287 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2288}
2289
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002290void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002291 // Explicit clinit checks triggered by static invokes must have been pruned by
2292 // art::PrepareForRegisterAllocation.
2293 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002294
Vladimir Marko68c981f2016-08-26 13:13:33 +01002295 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002296 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002297 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2298 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2299 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002300 return;
2301 }
2302
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002303 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002304
2305 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2306 if (invoke->HasPcRelativeDexCache()) {
2307 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2308 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002309}
2310
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002311static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2312 if (invoke->GetLocations()->Intrinsified()) {
2313 IntrinsicCodeGeneratorARM intrinsic(codegen);
2314 intrinsic.Dispatch(invoke);
2315 return true;
2316 }
2317 return false;
2318}
2319
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002320void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002321 // Explicit clinit checks triggered by static invokes must have been pruned by
2322 // art::PrepareForRegisterAllocation.
2323 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002324
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002325 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2326 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002327 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002328
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002329 LocationSummary* locations = invoke->GetLocations();
2330 codegen_->GenerateStaticOrDirectCall(
2331 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002332 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002333}
2334
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002335void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002336 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002337 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002338}
2339
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002340void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002341 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002342 if (intrinsic.TryDispatch(invoke)) {
2343 return;
2344 }
2345
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002346 HandleInvoke(invoke);
2347}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002348
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002349void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002350 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2351 return;
2352 }
2353
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002354 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002355 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002356 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002357}
2358
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002359void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2360 HandleInvoke(invoke);
2361 // Add the hidden argument.
2362 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2363}
2364
2365void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2366 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002367 LocationSummary* locations = invoke->GetLocations();
2368 Register temp = locations->GetTemp(0).AsRegister<Register>();
2369 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002370 Location receiver = locations->InAt(0);
2371 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2372
Roland Levillain3b359c72015-11-17 19:35:12 +00002373 // Set the hidden argument. This is safe to do this here, as R12
2374 // won't be modified thereafter, before the `blx` (call) instruction.
2375 DCHECK_EQ(R12, hidden_reg);
2376 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002377
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002378 if (receiver.IsStackSlot()) {
2379 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002380 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002381 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2382 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002383 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002384 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002385 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002386 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002387 // Instead of simply (possibly) unpoisoning `temp` here, we should
2388 // emit a read barrier for the previous class reference load.
2389 // However this is not required in practice, as this is an
2390 // intermediate/temporary reference and because the current
2391 // concurrent copying collector keeps the from-space memory
2392 // intact/accessible until the end of the marking phase (the
2393 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002394 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002395 __ LoadFromOffset(kLoadWord, temp, temp,
2396 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2397 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002398 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002399 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002400 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002401 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002402 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002403 // LR = temp->GetEntryPoint();
2404 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2405 // LR();
2406 __ blx(LR);
2407 DCHECK(!codegen_->IsLeafMethod());
2408 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2409}
2410
Orion Hodsonac141392017-01-13 11:53:47 +00002411void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2412 HandleInvoke(invoke);
2413}
2414
2415void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2416 codegen_->GenerateInvokePolymorphicCall(invoke);
2417}
2418
Roland Levillain88cb1752014-10-20 16:36:47 +01002419void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2420 LocationSummary* locations =
2421 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2422 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002423 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002424 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002425 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2426 break;
2427 }
2428 case Primitive::kPrimLong: {
2429 locations->SetInAt(0, Location::RequiresRegister());
2430 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002431 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002432 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002433
Roland Levillain88cb1752014-10-20 16:36:47 +01002434 case Primitive::kPrimFloat:
2435 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002436 locations->SetInAt(0, Location::RequiresFpuRegister());
2437 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002438 break;
2439
2440 default:
2441 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2442 }
2443}
2444
2445void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2446 LocationSummary* locations = neg->GetLocations();
2447 Location out = locations->Out();
2448 Location in = locations->InAt(0);
2449 switch (neg->GetResultType()) {
2450 case Primitive::kPrimInt:
2451 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002452 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002453 break;
2454
2455 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002456 DCHECK(in.IsRegisterPair());
2457 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2458 __ rsbs(out.AsRegisterPairLow<Register>(),
2459 in.AsRegisterPairLow<Register>(),
2460 ShifterOperand(0));
2461 // We cannot emit an RSC (Reverse Subtract with Carry)
2462 // instruction here, as it does not exist in the Thumb-2
2463 // instruction set. We use the following approach
2464 // using SBC and SUB instead.
2465 //
2466 // out.hi = -C
2467 __ sbc(out.AsRegisterPairHigh<Register>(),
2468 out.AsRegisterPairHigh<Register>(),
2469 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2470 // out.hi = out.hi - in.hi
2471 __ sub(out.AsRegisterPairHigh<Register>(),
2472 out.AsRegisterPairHigh<Register>(),
2473 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2474 break;
2475
Roland Levillain88cb1752014-10-20 16:36:47 +01002476 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002477 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002478 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002479 break;
2480
Roland Levillain88cb1752014-10-20 16:36:47 +01002481 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002482 DCHECK(in.IsFpuRegisterPair());
2483 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2484 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002485 break;
2486
2487 default:
2488 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2489 }
2490}
2491
Roland Levillaindff1f282014-11-05 14:15:05 +00002492void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002493 Primitive::Type result_type = conversion->GetResultType();
2494 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002495 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002496
Roland Levillain5b3ee562015-04-14 16:02:41 +01002497 // The float-to-long, double-to-long and long-to-float type conversions
2498 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002499 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002500 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2501 && result_type == Primitive::kPrimLong)
2502 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002503 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002504 : LocationSummary::kNoCall;
2505 LocationSummary* locations =
2506 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2507
David Brazdilb2bd1c52015-03-25 11:17:37 +00002508 // The Java language does not allow treating boolean as an integral type but
2509 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002510
Roland Levillaindff1f282014-11-05 14:15:05 +00002511 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002512 case Primitive::kPrimByte:
2513 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002514 case Primitive::kPrimLong:
2515 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002516 case Primitive::kPrimBoolean:
2517 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002518 case Primitive::kPrimShort:
2519 case Primitive::kPrimInt:
2520 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002521 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002522 locations->SetInAt(0, Location::RequiresRegister());
2523 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2524 break;
2525
2526 default:
2527 LOG(FATAL) << "Unexpected type conversion from " << input_type
2528 << " to " << result_type;
2529 }
2530 break;
2531
Roland Levillain01a8d712014-11-14 16:27:39 +00002532 case Primitive::kPrimShort:
2533 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002534 case Primitive::kPrimLong:
2535 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002536 case Primitive::kPrimBoolean:
2537 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002538 case Primitive::kPrimByte:
2539 case Primitive::kPrimInt:
2540 case Primitive::kPrimChar:
2541 // Processing a Dex `int-to-short' instruction.
2542 locations->SetInAt(0, Location::RequiresRegister());
2543 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2544 break;
2545
2546 default:
2547 LOG(FATAL) << "Unexpected type conversion from " << input_type
2548 << " to " << result_type;
2549 }
2550 break;
2551
Roland Levillain946e1432014-11-11 17:35:19 +00002552 case Primitive::kPrimInt:
2553 switch (input_type) {
2554 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002555 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002556 locations->SetInAt(0, Location::Any());
2557 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2558 break;
2559
2560 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002561 // Processing a Dex `float-to-int' instruction.
2562 locations->SetInAt(0, Location::RequiresFpuRegister());
2563 locations->SetOut(Location::RequiresRegister());
2564 locations->AddTemp(Location::RequiresFpuRegister());
2565 break;
2566
Roland Levillain946e1432014-11-11 17:35:19 +00002567 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002568 // Processing a Dex `double-to-int' instruction.
2569 locations->SetInAt(0, Location::RequiresFpuRegister());
2570 locations->SetOut(Location::RequiresRegister());
2571 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002572 break;
2573
2574 default:
2575 LOG(FATAL) << "Unexpected type conversion from " << input_type
2576 << " to " << result_type;
2577 }
2578 break;
2579
Roland Levillaindff1f282014-11-05 14:15:05 +00002580 case Primitive::kPrimLong:
2581 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002582 case Primitive::kPrimBoolean:
2583 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002584 case Primitive::kPrimByte:
2585 case Primitive::kPrimShort:
2586 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002587 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002588 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002589 locations->SetInAt(0, Location::RequiresRegister());
2590 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2591 break;
2592
Roland Levillain624279f2014-12-04 11:54:28 +00002593 case Primitive::kPrimFloat: {
2594 // Processing a Dex `float-to-long' instruction.
2595 InvokeRuntimeCallingConvention calling_convention;
2596 locations->SetInAt(0, Location::FpuRegisterLocation(
2597 calling_convention.GetFpuRegisterAt(0)));
2598 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2599 break;
2600 }
2601
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002602 case Primitive::kPrimDouble: {
2603 // Processing a Dex `double-to-long' instruction.
2604 InvokeRuntimeCallingConvention calling_convention;
2605 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2606 calling_convention.GetFpuRegisterAt(0),
2607 calling_convention.GetFpuRegisterAt(1)));
2608 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002609 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002610 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002611
2612 default:
2613 LOG(FATAL) << "Unexpected type conversion from " << input_type
2614 << " to " << result_type;
2615 }
2616 break;
2617
Roland Levillain981e4542014-11-14 11:47:14 +00002618 case Primitive::kPrimChar:
2619 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002620 case Primitive::kPrimLong:
2621 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002622 case Primitive::kPrimBoolean:
2623 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002624 case Primitive::kPrimByte:
2625 case Primitive::kPrimShort:
2626 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002627 // Processing a Dex `int-to-char' instruction.
2628 locations->SetInAt(0, Location::RequiresRegister());
2629 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2630 break;
2631
2632 default:
2633 LOG(FATAL) << "Unexpected type conversion from " << input_type
2634 << " to " << result_type;
2635 }
2636 break;
2637
Roland Levillaindff1f282014-11-05 14:15:05 +00002638 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002639 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002640 case Primitive::kPrimBoolean:
2641 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002642 case Primitive::kPrimByte:
2643 case Primitive::kPrimShort:
2644 case Primitive::kPrimInt:
2645 case Primitive::kPrimChar:
2646 // Processing a Dex `int-to-float' instruction.
2647 locations->SetInAt(0, Location::RequiresRegister());
2648 locations->SetOut(Location::RequiresFpuRegister());
2649 break;
2650
Roland Levillain5b3ee562015-04-14 16:02:41 +01002651 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002652 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002653 InvokeRuntimeCallingConvention calling_convention;
2654 locations->SetInAt(0, Location::RegisterPairLocation(
2655 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2656 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002657 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002658 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002659
Roland Levillaincff13742014-11-17 14:32:17 +00002660 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002661 // Processing a Dex `double-to-float' instruction.
2662 locations->SetInAt(0, Location::RequiresFpuRegister());
2663 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002664 break;
2665
2666 default:
2667 LOG(FATAL) << "Unexpected type conversion from " << input_type
2668 << " to " << result_type;
2669 };
2670 break;
2671
Roland Levillaindff1f282014-11-05 14:15:05 +00002672 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002673 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002674 case Primitive::kPrimBoolean:
2675 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002676 case Primitive::kPrimByte:
2677 case Primitive::kPrimShort:
2678 case Primitive::kPrimInt:
2679 case Primitive::kPrimChar:
2680 // Processing a Dex `int-to-double' instruction.
2681 locations->SetInAt(0, Location::RequiresRegister());
2682 locations->SetOut(Location::RequiresFpuRegister());
2683 break;
2684
2685 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002686 // Processing a Dex `long-to-double' instruction.
2687 locations->SetInAt(0, Location::RequiresRegister());
2688 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002689 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002690 locations->AddTemp(Location::RequiresFpuRegister());
2691 break;
2692
Roland Levillaincff13742014-11-17 14:32:17 +00002693 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002694 // Processing a Dex `float-to-double' instruction.
2695 locations->SetInAt(0, Location::RequiresFpuRegister());
2696 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002697 break;
2698
2699 default:
2700 LOG(FATAL) << "Unexpected type conversion from " << input_type
2701 << " to " << result_type;
2702 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002703 break;
2704
2705 default:
2706 LOG(FATAL) << "Unexpected type conversion from " << input_type
2707 << " to " << result_type;
2708 }
2709}
2710
2711void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2712 LocationSummary* locations = conversion->GetLocations();
2713 Location out = locations->Out();
2714 Location in = locations->InAt(0);
2715 Primitive::Type result_type = conversion->GetResultType();
2716 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002717 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002718 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002719 case Primitive::kPrimByte:
2720 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002721 case Primitive::kPrimLong:
2722 // Type conversion from long to byte is a result of code transformations.
2723 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2724 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002725 case Primitive::kPrimBoolean:
2726 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002727 case Primitive::kPrimShort:
2728 case Primitive::kPrimInt:
2729 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002730 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002731 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002732 break;
2733
2734 default:
2735 LOG(FATAL) << "Unexpected type conversion from " << input_type
2736 << " to " << result_type;
2737 }
2738 break;
2739
Roland Levillain01a8d712014-11-14 16:27:39 +00002740 case Primitive::kPrimShort:
2741 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002742 case Primitive::kPrimLong:
2743 // Type conversion from long to short is a result of code transformations.
2744 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2745 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002746 case Primitive::kPrimBoolean:
2747 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002748 case Primitive::kPrimByte:
2749 case Primitive::kPrimInt:
2750 case Primitive::kPrimChar:
2751 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002752 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002753 break;
2754
2755 default:
2756 LOG(FATAL) << "Unexpected type conversion from " << input_type
2757 << " to " << result_type;
2758 }
2759 break;
2760
Roland Levillain946e1432014-11-11 17:35:19 +00002761 case Primitive::kPrimInt:
2762 switch (input_type) {
2763 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002764 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002765 DCHECK(out.IsRegister());
2766 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002767 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002768 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002769 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002770 } else {
2771 DCHECK(in.IsConstant());
2772 DCHECK(in.GetConstant()->IsLongConstant());
2773 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002774 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002775 }
2776 break;
2777
Roland Levillain3f8f9362014-12-02 17:45:01 +00002778 case Primitive::kPrimFloat: {
2779 // Processing a Dex `float-to-int' instruction.
2780 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002781 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002782 __ vmovrs(out.AsRegister<Register>(), temp);
2783 break;
2784 }
2785
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002786 case Primitive::kPrimDouble: {
2787 // Processing a Dex `double-to-int' instruction.
2788 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002789 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002790 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002791 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002792 }
Roland Levillain946e1432014-11-11 17:35:19 +00002793
2794 default:
2795 LOG(FATAL) << "Unexpected type conversion from " << input_type
2796 << " to " << result_type;
2797 }
2798 break;
2799
Roland Levillaindff1f282014-11-05 14:15:05 +00002800 case Primitive::kPrimLong:
2801 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002802 case Primitive::kPrimBoolean:
2803 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002804 case Primitive::kPrimByte:
2805 case Primitive::kPrimShort:
2806 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002807 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002808 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002809 DCHECK(out.IsRegisterPair());
2810 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002811 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002812 // Sign extension.
2813 __ Asr(out.AsRegisterPairHigh<Register>(),
2814 out.AsRegisterPairLow<Register>(),
2815 31);
2816 break;
2817
2818 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002819 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002820 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002821 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002822 break;
2823
Roland Levillaindff1f282014-11-05 14:15:05 +00002824 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002825 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002826 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002827 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002828 break;
2829
2830 default:
2831 LOG(FATAL) << "Unexpected type conversion from " << input_type
2832 << " to " << result_type;
2833 }
2834 break;
2835
Roland Levillain981e4542014-11-14 11:47:14 +00002836 case Primitive::kPrimChar:
2837 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002838 case Primitive::kPrimLong:
2839 // Type conversion from long to char is a result of code transformations.
2840 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2841 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002842 case Primitive::kPrimBoolean:
2843 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002844 case Primitive::kPrimByte:
2845 case Primitive::kPrimShort:
2846 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002847 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002848 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002849 break;
2850
2851 default:
2852 LOG(FATAL) << "Unexpected type conversion from " << input_type
2853 << " to " << result_type;
2854 }
2855 break;
2856
Roland Levillaindff1f282014-11-05 14:15:05 +00002857 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002858 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002859 case Primitive::kPrimBoolean:
2860 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002861 case Primitive::kPrimByte:
2862 case Primitive::kPrimShort:
2863 case Primitive::kPrimInt:
2864 case Primitive::kPrimChar: {
2865 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002866 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2867 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002868 break;
2869 }
2870
Roland Levillain5b3ee562015-04-14 16:02:41 +01002871 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002872 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002873 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002874 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002875 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002876
Roland Levillaincff13742014-11-17 14:32:17 +00002877 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002878 // Processing a Dex `double-to-float' instruction.
2879 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2880 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002881 break;
2882
2883 default:
2884 LOG(FATAL) << "Unexpected type conversion from " << input_type
2885 << " to " << result_type;
2886 };
2887 break;
2888
Roland Levillaindff1f282014-11-05 14:15:05 +00002889 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002890 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002891 case Primitive::kPrimBoolean:
2892 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002893 case Primitive::kPrimByte:
2894 case Primitive::kPrimShort:
2895 case Primitive::kPrimInt:
2896 case Primitive::kPrimChar: {
2897 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002898 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002899 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2900 out.AsFpuRegisterPairLow<SRegister>());
2901 break;
2902 }
2903
Roland Levillain647b9ed2014-11-27 12:06:00 +00002904 case Primitive::kPrimLong: {
2905 // Processing a Dex `long-to-double' instruction.
2906 Register low = in.AsRegisterPairLow<Register>();
2907 Register high = in.AsRegisterPairHigh<Register>();
2908 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2909 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002910 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002911 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002912 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2913 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002914
Roland Levillain682393c2015-04-14 15:57:52 +01002915 // temp_d = int-to-double(high)
2916 __ vmovsr(temp_s, high);
2917 __ vcvtdi(temp_d, temp_s);
2918 // constant_d = k2Pow32EncodingForDouble
2919 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2920 // out_d = unsigned-to-double(low)
2921 __ vmovsr(out_s, low);
2922 __ vcvtdu(out_d, out_s);
2923 // out_d += temp_d * constant_d
2924 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002925 break;
2926 }
2927
Roland Levillaincff13742014-11-17 14:32:17 +00002928 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002929 // Processing a Dex `float-to-double' instruction.
2930 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2931 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002932 break;
2933
2934 default:
2935 LOG(FATAL) << "Unexpected type conversion from " << input_type
2936 << " to " << result_type;
2937 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002938 break;
2939
2940 default:
2941 LOG(FATAL) << "Unexpected type conversion from " << input_type
2942 << " to " << result_type;
2943 }
2944}
2945
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002946void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002947 LocationSummary* locations =
2948 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002949 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002950 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002951 locations->SetInAt(0, Location::RequiresRegister());
2952 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002953 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2954 break;
2955 }
2956
2957 case Primitive::kPrimLong: {
2958 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002959 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002960 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002961 break;
2962 }
2963
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002964 case Primitive::kPrimFloat:
2965 case Primitive::kPrimDouble: {
2966 locations->SetInAt(0, Location::RequiresFpuRegister());
2967 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002968 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002969 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002970 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002971
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002972 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002973 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002974 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002975}
2976
2977void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2978 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002979 Location out = locations->Out();
2980 Location first = locations->InAt(0);
2981 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002982 switch (add->GetResultType()) {
2983 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002984 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002985 __ add(out.AsRegister<Register>(),
2986 first.AsRegister<Register>(),
2987 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002988 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002989 __ AddConstant(out.AsRegister<Register>(),
2990 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002991 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002992 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002993 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002994
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002995 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002996 if (second.IsConstant()) {
2997 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2998 GenerateAddLongConst(out, first, value);
2999 } else {
3000 DCHECK(second.IsRegisterPair());
3001 __ adds(out.AsRegisterPairLow<Register>(),
3002 first.AsRegisterPairLow<Register>(),
3003 ShifterOperand(second.AsRegisterPairLow<Register>()));
3004 __ adc(out.AsRegisterPairHigh<Register>(),
3005 first.AsRegisterPairHigh<Register>(),
3006 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3007 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003008 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003009 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003010
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003011 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00003012 __ vadds(out.AsFpuRegister<SRegister>(),
3013 first.AsFpuRegister<SRegister>(),
3014 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003015 break;
3016
3017 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003018 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3019 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3020 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003021 break;
3022
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003023 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003024 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003025 }
3026}
3027
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003028void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003029 LocationSummary* locations =
3030 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003031 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003032 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003033 locations->SetInAt(0, Location::RequiresRegister());
3034 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003035 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3036 break;
3037 }
3038
3039 case Primitive::kPrimLong: {
3040 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003041 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003042 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003043 break;
3044 }
Calin Juravle11351682014-10-23 15:38:15 +01003045 case Primitive::kPrimFloat:
3046 case Primitive::kPrimDouble: {
3047 locations->SetInAt(0, Location::RequiresFpuRegister());
3048 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003049 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003050 break;
Calin Juravle11351682014-10-23 15:38:15 +01003051 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003052 default:
Calin Juravle11351682014-10-23 15:38:15 +01003053 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003054 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003055}
3056
3057void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3058 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003059 Location out = locations->Out();
3060 Location first = locations->InAt(0);
3061 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003062 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003063 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003064 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003065 __ sub(out.AsRegister<Register>(),
3066 first.AsRegister<Register>(),
3067 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003068 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003069 __ AddConstant(out.AsRegister<Register>(),
3070 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003071 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003072 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003073 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003074 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003075
Calin Juravle11351682014-10-23 15:38:15 +01003076 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003077 if (second.IsConstant()) {
3078 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3079 GenerateAddLongConst(out, first, -value);
3080 } else {
3081 DCHECK(second.IsRegisterPair());
3082 __ subs(out.AsRegisterPairLow<Register>(),
3083 first.AsRegisterPairLow<Register>(),
3084 ShifterOperand(second.AsRegisterPairLow<Register>()));
3085 __ sbc(out.AsRegisterPairHigh<Register>(),
3086 first.AsRegisterPairHigh<Register>(),
3087 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3088 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003089 break;
Calin Juravle11351682014-10-23 15:38:15 +01003090 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003091
Calin Juravle11351682014-10-23 15:38:15 +01003092 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003093 __ vsubs(out.AsFpuRegister<SRegister>(),
3094 first.AsFpuRegister<SRegister>(),
3095 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003096 break;
Calin Juravle11351682014-10-23 15:38:15 +01003097 }
3098
3099 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003100 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3101 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3102 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003103 break;
3104 }
3105
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003106
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003107 default:
Calin Juravle11351682014-10-23 15:38:15 +01003108 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003109 }
3110}
3111
Calin Juravle34bacdf2014-10-07 20:23:36 +01003112void LocationsBuilderARM::VisitMul(HMul* mul) {
3113 LocationSummary* locations =
3114 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3115 switch (mul->GetResultType()) {
3116 case Primitive::kPrimInt:
3117 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003118 locations->SetInAt(0, Location::RequiresRegister());
3119 locations->SetInAt(1, Location::RequiresRegister());
3120 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003121 break;
3122 }
3123
Calin Juravleb5bfa962014-10-21 18:02:24 +01003124 case Primitive::kPrimFloat:
3125 case Primitive::kPrimDouble: {
3126 locations->SetInAt(0, Location::RequiresFpuRegister());
3127 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003128 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003129 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003130 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003131
3132 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003133 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003134 }
3135}
3136
3137void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3138 LocationSummary* locations = mul->GetLocations();
3139 Location out = locations->Out();
3140 Location first = locations->InAt(0);
3141 Location second = locations->InAt(1);
3142 switch (mul->GetResultType()) {
3143 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003144 __ mul(out.AsRegister<Register>(),
3145 first.AsRegister<Register>(),
3146 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003147 break;
3148 }
3149 case Primitive::kPrimLong: {
3150 Register out_hi = out.AsRegisterPairHigh<Register>();
3151 Register out_lo = out.AsRegisterPairLow<Register>();
3152 Register in1_hi = first.AsRegisterPairHigh<Register>();
3153 Register in1_lo = first.AsRegisterPairLow<Register>();
3154 Register in2_hi = second.AsRegisterPairHigh<Register>();
3155 Register in2_lo = second.AsRegisterPairLow<Register>();
3156
3157 // Extra checks to protect caused by the existence of R1_R2.
3158 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3159 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3160 DCHECK_NE(out_hi, in1_lo);
3161 DCHECK_NE(out_hi, in2_lo);
3162
3163 // input: in1 - 64 bits, in2 - 64 bits
3164 // output: out
3165 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3166 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3167 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3168
3169 // IP <- in1.lo * in2.hi
3170 __ mul(IP, in1_lo, in2_hi);
3171 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3172 __ mla(out_hi, in1_hi, in2_lo, IP);
3173 // out.lo <- (in1.lo * in2.lo)[31:0];
3174 __ umull(out_lo, IP, in1_lo, in2_lo);
3175 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3176 __ add(out_hi, out_hi, ShifterOperand(IP));
3177 break;
3178 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003179
3180 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003181 __ vmuls(out.AsFpuRegister<SRegister>(),
3182 first.AsFpuRegister<SRegister>(),
3183 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003184 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003185 }
3186
3187 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003188 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3189 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3190 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003191 break;
3192 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003193
3194 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003195 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003196 }
3197}
3198
Zheng Xuc6667102015-05-15 16:08:45 +08003199void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3200 DCHECK(instruction->IsDiv() || instruction->IsRem());
3201 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3202
3203 LocationSummary* locations = instruction->GetLocations();
3204 Location second = locations->InAt(1);
3205 DCHECK(second.IsConstant());
3206
3207 Register out = locations->Out().AsRegister<Register>();
3208 Register dividend = locations->InAt(0).AsRegister<Register>();
3209 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3210 DCHECK(imm == 1 || imm == -1);
3211
3212 if (instruction->IsRem()) {
3213 __ LoadImmediate(out, 0);
3214 } else {
3215 if (imm == 1) {
3216 __ Mov(out, dividend);
3217 } else {
3218 __ rsb(out, dividend, ShifterOperand(0));
3219 }
3220 }
3221}
3222
3223void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3224 DCHECK(instruction->IsDiv() || instruction->IsRem());
3225 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3226
3227 LocationSummary* locations = instruction->GetLocations();
3228 Location second = locations->InAt(1);
3229 DCHECK(second.IsConstant());
3230
3231 Register out = locations->Out().AsRegister<Register>();
3232 Register dividend = locations->InAt(0).AsRegister<Register>();
3233 Register temp = locations->GetTemp(0).AsRegister<Register>();
3234 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003235 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003236 int ctz_imm = CTZ(abs_imm);
3237
3238 if (ctz_imm == 1) {
3239 __ Lsr(temp, dividend, 32 - ctz_imm);
3240 } else {
3241 __ Asr(temp, dividend, 31);
3242 __ Lsr(temp, temp, 32 - ctz_imm);
3243 }
3244 __ add(out, temp, ShifterOperand(dividend));
3245
3246 if (instruction->IsDiv()) {
3247 __ Asr(out, out, ctz_imm);
3248 if (imm < 0) {
3249 __ rsb(out, out, ShifterOperand(0));
3250 }
3251 } else {
3252 __ ubfx(out, out, 0, ctz_imm);
3253 __ sub(out, out, ShifterOperand(temp));
3254 }
3255}
3256
3257void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3258 DCHECK(instruction->IsDiv() || instruction->IsRem());
3259 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3260
3261 LocationSummary* locations = instruction->GetLocations();
3262 Location second = locations->InAt(1);
3263 DCHECK(second.IsConstant());
3264
3265 Register out = locations->Out().AsRegister<Register>();
3266 Register dividend = locations->InAt(0).AsRegister<Register>();
3267 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3268 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3269 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3270
3271 int64_t magic;
3272 int shift;
3273 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3274
3275 __ LoadImmediate(temp1, magic);
3276 __ smull(temp2, temp1, dividend, temp1);
3277
3278 if (imm > 0 && magic < 0) {
3279 __ add(temp1, temp1, ShifterOperand(dividend));
3280 } else if (imm < 0 && magic > 0) {
3281 __ sub(temp1, temp1, ShifterOperand(dividend));
3282 }
3283
3284 if (shift != 0) {
3285 __ Asr(temp1, temp1, shift);
3286 }
3287
3288 if (instruction->IsDiv()) {
3289 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3290 } else {
3291 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3292 // TODO: Strength reduction for mls.
3293 __ LoadImmediate(temp2, imm);
3294 __ mls(out, temp1, temp2, dividend);
3295 }
3296}
3297
3298void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3299 DCHECK(instruction->IsDiv() || instruction->IsRem());
3300 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3301
3302 LocationSummary* locations = instruction->GetLocations();
3303 Location second = locations->InAt(1);
3304 DCHECK(second.IsConstant());
3305
3306 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3307 if (imm == 0) {
3308 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3309 } else if (imm == 1 || imm == -1) {
3310 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003311 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003312 DivRemByPowerOfTwo(instruction);
3313 } else {
3314 DCHECK(imm <= -2 || imm >= 2);
3315 GenerateDivRemWithAnyConstant(instruction);
3316 }
3317}
3318
Calin Juravle7c4954d2014-10-28 16:57:40 +00003319void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003320 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3321 if (div->GetResultType() == Primitive::kPrimLong) {
3322 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003323 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003324 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3325 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003326 } else if (div->GetResultType() == Primitive::kPrimInt &&
3327 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3328 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003329 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003330 }
3331
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003332 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3333
Calin Juravle7c4954d2014-10-28 16:57:40 +00003334 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003335 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003336 if (div->InputAt(1)->IsConstant()) {
3337 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003338 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003339 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003340 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3341 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003342 // No temp register required.
3343 } else {
3344 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003345 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003346 locations->AddTemp(Location::RequiresRegister());
3347 }
3348 }
3349 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003350 locations->SetInAt(0, Location::RequiresRegister());
3351 locations->SetInAt(1, Location::RequiresRegister());
3352 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3353 } else {
3354 InvokeRuntimeCallingConvention calling_convention;
3355 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3356 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003357 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003358 // we only need the former.
3359 locations->SetOut(Location::RegisterLocation(R0));
3360 }
Calin Juravled0d48522014-11-04 16:40:20 +00003361 break;
3362 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003363 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003364 InvokeRuntimeCallingConvention calling_convention;
3365 locations->SetInAt(0, Location::RegisterPairLocation(
3366 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3367 locations->SetInAt(1, Location::RegisterPairLocation(
3368 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003369 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003370 break;
3371 }
3372 case Primitive::kPrimFloat:
3373 case Primitive::kPrimDouble: {
3374 locations->SetInAt(0, Location::RequiresFpuRegister());
3375 locations->SetInAt(1, Location::RequiresFpuRegister());
3376 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3377 break;
3378 }
3379
3380 default:
3381 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3382 }
3383}
3384
3385void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3386 LocationSummary* locations = div->GetLocations();
3387 Location out = locations->Out();
3388 Location first = locations->InAt(0);
3389 Location second = locations->InAt(1);
3390
3391 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003392 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003393 if (second.IsConstant()) {
3394 GenerateDivRemConstantIntegral(div);
3395 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003396 __ sdiv(out.AsRegister<Register>(),
3397 first.AsRegister<Register>(),
3398 second.AsRegister<Register>());
3399 } else {
3400 InvokeRuntimeCallingConvention calling_convention;
3401 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3402 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3403 DCHECK_EQ(R0, out.AsRegister<Register>());
3404
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003405 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003406 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003407 }
Calin Juravled0d48522014-11-04 16:40:20 +00003408 break;
3409 }
3410
Calin Juravle7c4954d2014-10-28 16:57:40 +00003411 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003412 InvokeRuntimeCallingConvention calling_convention;
3413 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3414 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3415 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3416 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3417 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003418 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003419
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003420 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003421 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003422 break;
3423 }
3424
3425 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003426 __ vdivs(out.AsFpuRegister<SRegister>(),
3427 first.AsFpuRegister<SRegister>(),
3428 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003429 break;
3430 }
3431
3432 case Primitive::kPrimDouble: {
3433 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3434 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3435 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3436 break;
3437 }
3438
3439 default:
3440 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3441 }
3442}
3443
Calin Juravlebacfec32014-11-14 15:54:36 +00003444void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003445 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003446
3447 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003448 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003449 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3450 // sdiv will be replaced by other instruction sequence.
3451 call_kind = LocationSummary::kNoCall;
3452 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3453 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003454 // Have hardware divide instruction for int, do it with three instructions.
3455 call_kind = LocationSummary::kNoCall;
3456 }
3457
Calin Juravlebacfec32014-11-14 15:54:36 +00003458 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3459
Calin Juravled2ec87d2014-12-08 14:24:46 +00003460 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003461 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003462 if (rem->InputAt(1)->IsConstant()) {
3463 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003464 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003465 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003466 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3467 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003468 // No temp register required.
3469 } else {
3470 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003471 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003472 locations->AddTemp(Location::RequiresRegister());
3473 }
3474 }
3475 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003476 locations->SetInAt(0, Location::RequiresRegister());
3477 locations->SetInAt(1, Location::RequiresRegister());
3478 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3479 locations->AddTemp(Location::RequiresRegister());
3480 } else {
3481 InvokeRuntimeCallingConvention calling_convention;
3482 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3483 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003484 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003485 // we only need the latter.
3486 locations->SetOut(Location::RegisterLocation(R1));
3487 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003488 break;
3489 }
3490 case Primitive::kPrimLong: {
3491 InvokeRuntimeCallingConvention calling_convention;
3492 locations->SetInAt(0, Location::RegisterPairLocation(
3493 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3494 locations->SetInAt(1, Location::RegisterPairLocation(
3495 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3496 // The runtime helper puts the output in R2,R3.
3497 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3498 break;
3499 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003500 case Primitive::kPrimFloat: {
3501 InvokeRuntimeCallingConvention calling_convention;
3502 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3503 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3504 locations->SetOut(Location::FpuRegisterLocation(S0));
3505 break;
3506 }
3507
Calin Juravlebacfec32014-11-14 15:54:36 +00003508 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003509 InvokeRuntimeCallingConvention calling_convention;
3510 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3511 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3512 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3513 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3514 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003515 break;
3516 }
3517
3518 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003519 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003520 }
3521}
3522
3523void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3524 LocationSummary* locations = rem->GetLocations();
3525 Location out = locations->Out();
3526 Location first = locations->InAt(0);
3527 Location second = locations->InAt(1);
3528
Calin Juravled2ec87d2014-12-08 14:24:46 +00003529 Primitive::Type type = rem->GetResultType();
3530 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003531 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003532 if (second.IsConstant()) {
3533 GenerateDivRemConstantIntegral(rem);
3534 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003535 Register reg1 = first.AsRegister<Register>();
3536 Register reg2 = second.AsRegister<Register>();
3537 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003538
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003539 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003540 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003541 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003542 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003543 } else {
3544 InvokeRuntimeCallingConvention calling_convention;
3545 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3546 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3547 DCHECK_EQ(R1, out.AsRegister<Register>());
3548
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003549 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003550 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003551 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003552 break;
3553 }
3554
3555 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003556 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003557 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003558 break;
3559 }
3560
Calin Juravled2ec87d2014-12-08 14:24:46 +00003561 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003562 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003563 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003564 break;
3565 }
3566
Calin Juravlebacfec32014-11-14 15:54:36 +00003567 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003568 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003569 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003570 break;
3571 }
3572
3573 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003574 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003575 }
3576}
3577
Calin Juravled0d48522014-11-04 16:40:20 +00003578void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003579 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003580 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003581}
3582
3583void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003584 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003585 codegen_->AddSlowPath(slow_path);
3586
3587 LocationSummary* locations = instruction->GetLocations();
3588 Location value = locations->InAt(0);
3589
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003590 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003591 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003592 case Primitive::kPrimByte:
3593 case Primitive::kPrimChar:
3594 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003595 case Primitive::kPrimInt: {
3596 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003597 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003598 } else {
3599 DCHECK(value.IsConstant()) << value;
3600 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3601 __ b(slow_path->GetEntryLabel());
3602 }
3603 }
3604 break;
3605 }
3606 case Primitive::kPrimLong: {
3607 if (value.IsRegisterPair()) {
3608 __ orrs(IP,
3609 value.AsRegisterPairLow<Register>(),
3610 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3611 __ b(slow_path->GetEntryLabel(), EQ);
3612 } else {
3613 DCHECK(value.IsConstant()) << value;
3614 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3615 __ b(slow_path->GetEntryLabel());
3616 }
3617 }
3618 break;
3619 default:
3620 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3621 }
3622 }
Calin Juravled0d48522014-11-04 16:40:20 +00003623}
3624
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003625void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3626 Register in = locations->InAt(0).AsRegister<Register>();
3627 Location rhs = locations->InAt(1);
3628 Register out = locations->Out().AsRegister<Register>();
3629
3630 if (rhs.IsConstant()) {
3631 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3632 // so map all rotations to a +ve. equivalent in that range.
3633 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3634 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3635 if (rot) {
3636 // Rotate, mapping left rotations to right equivalents if necessary.
3637 // (e.g. left by 2 bits == right by 30.)
3638 __ Ror(out, in, rot);
3639 } else if (out != in) {
3640 __ Mov(out, in);
3641 }
3642 } else {
3643 __ Ror(out, in, rhs.AsRegister<Register>());
3644 }
3645}
3646
3647// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3648// rotates by swapping input regs (effectively rotating by the first 32-bits of
3649// a larger rotation) or flipping direction (thus treating larger right/left
3650// rotations as sub-word sized rotations in the other direction) as appropriate.
3651void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3652 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3653 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3654 Location rhs = locations->InAt(1);
3655 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3656 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3657
3658 if (rhs.IsConstant()) {
3659 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3660 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003661 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003662 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3663 // logic below to a simple pair of binary orr.
3664 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3665 if (rot >= kArmBitsPerWord) {
3666 rot -= kArmBitsPerWord;
3667 std::swap(in_reg_hi, in_reg_lo);
3668 }
3669 // Rotate, or mov to out for zero or word size rotations.
3670 if (rot != 0u) {
3671 __ Lsr(out_reg_hi, in_reg_hi, rot);
3672 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3673 __ Lsr(out_reg_lo, in_reg_lo, rot);
3674 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3675 } else {
3676 __ Mov(out_reg_lo, in_reg_lo);
3677 __ Mov(out_reg_hi, in_reg_hi);
3678 }
3679 } else {
3680 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3681 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3682 Label end;
3683 Label shift_by_32_plus_shift_right;
3684
3685 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3686 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3687 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3688 __ b(&shift_by_32_plus_shift_right, CC);
3689
3690 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3691 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3692 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3693 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3694 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3695 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3696 __ Lsr(shift_left, in_reg_hi, shift_right);
3697 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3698 __ b(&end);
3699
3700 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3701 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3702 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3703 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3704 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3705 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3706 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3707 __ Lsl(shift_right, in_reg_hi, shift_left);
3708 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3709
3710 __ Bind(&end);
3711 }
3712}
Roland Levillain22c49222016-03-18 14:04:28 +00003713
3714void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003715 LocationSummary* locations =
3716 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3717 switch (ror->GetResultType()) {
3718 case Primitive::kPrimInt: {
3719 locations->SetInAt(0, Location::RequiresRegister());
3720 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3721 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3722 break;
3723 }
3724 case Primitive::kPrimLong: {
3725 locations->SetInAt(0, Location::RequiresRegister());
3726 if (ror->InputAt(1)->IsConstant()) {
3727 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3728 } else {
3729 locations->SetInAt(1, Location::RequiresRegister());
3730 locations->AddTemp(Location::RequiresRegister());
3731 locations->AddTemp(Location::RequiresRegister());
3732 }
3733 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3734 break;
3735 }
3736 default:
3737 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3738 }
3739}
3740
Roland Levillain22c49222016-03-18 14:04:28 +00003741void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003742 LocationSummary* locations = ror->GetLocations();
3743 Primitive::Type type = ror->GetResultType();
3744 switch (type) {
3745 case Primitive::kPrimInt: {
3746 HandleIntegerRotate(locations);
3747 break;
3748 }
3749 case Primitive::kPrimLong: {
3750 HandleLongRotate(locations);
3751 break;
3752 }
3753 default:
3754 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003755 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003756 }
3757}
3758
Calin Juravle9aec02f2014-11-18 23:06:35 +00003759void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3760 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3761
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003762 LocationSummary* locations =
3763 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003764
3765 switch (op->GetResultType()) {
3766 case Primitive::kPrimInt: {
3767 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003768 if (op->InputAt(1)->IsConstant()) {
3769 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3770 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3771 } else {
3772 locations->SetInAt(1, Location::RequiresRegister());
3773 // Make the output overlap, as it will be used to hold the masked
3774 // second input.
3775 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3776 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003777 break;
3778 }
3779 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003780 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003781 if (op->InputAt(1)->IsConstant()) {
3782 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3783 // For simplicity, use kOutputOverlap even though we only require that low registers
3784 // don't clash with high registers which the register allocator currently guarantees.
3785 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3786 } else {
3787 locations->SetInAt(1, Location::RequiresRegister());
3788 locations->AddTemp(Location::RequiresRegister());
3789 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3790 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003791 break;
3792 }
3793 default:
3794 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3795 }
3796}
3797
3798void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3799 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3800
3801 LocationSummary* locations = op->GetLocations();
3802 Location out = locations->Out();
3803 Location first = locations->InAt(0);
3804 Location second = locations->InAt(1);
3805
3806 Primitive::Type type = op->GetResultType();
3807 switch (type) {
3808 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003809 Register out_reg = out.AsRegister<Register>();
3810 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003811 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003812 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003813 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003814 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003815 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003816 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003817 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003818 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003819 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003820 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003821 }
3822 } else {
3823 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003824 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003825 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003826 __ Mov(out_reg, first_reg);
3827 } else if (op->IsShl()) {
3828 __ Lsl(out_reg, first_reg, shift_value);
3829 } else if (op->IsShr()) {
3830 __ Asr(out_reg, first_reg, shift_value);
3831 } else {
3832 __ Lsr(out_reg, first_reg, shift_value);
3833 }
3834 }
3835 break;
3836 }
3837 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003838 Register o_h = out.AsRegisterPairHigh<Register>();
3839 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003840
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003841 Register high = first.AsRegisterPairHigh<Register>();
3842 Register low = first.AsRegisterPairLow<Register>();
3843
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003844 if (second.IsRegister()) {
3845 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003846
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003847 Register second_reg = second.AsRegister<Register>();
3848
3849 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003850 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003851 // Shift the high part
3852 __ Lsl(o_h, high, o_l);
3853 // Shift the low part and `or` what overflew on the high part
3854 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3855 __ Lsr(temp, low, temp);
3856 __ orr(o_h, o_h, ShifterOperand(temp));
3857 // If the shift is > 32 bits, override the high part
3858 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3859 __ it(PL);
3860 __ Lsl(o_h, low, temp, PL);
3861 // Shift the low part
3862 __ Lsl(o_l, low, o_l);
3863 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003864 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003865 // Shift the low part
3866 __ Lsr(o_l, low, o_h);
3867 // Shift the high part and `or` what underflew on the low part
3868 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3869 __ Lsl(temp, high, temp);
3870 __ orr(o_l, o_l, ShifterOperand(temp));
3871 // If the shift is > 32 bits, override the low part
3872 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3873 __ it(PL);
3874 __ Asr(o_l, high, temp, PL);
3875 // Shift the high part
3876 __ Asr(o_h, high, o_h);
3877 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003878 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003879 // same as Shr except we use `Lsr`s and not `Asr`s
3880 __ Lsr(o_l, low, o_h);
3881 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3882 __ Lsl(temp, high, temp);
3883 __ orr(o_l, o_l, ShifterOperand(temp));
3884 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3885 __ it(PL);
3886 __ Lsr(o_l, high, temp, PL);
3887 __ Lsr(o_h, high, o_h);
3888 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003889 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003890 // Register allocator doesn't create partial overlap.
3891 DCHECK_NE(o_l, high);
3892 DCHECK_NE(o_h, low);
3893 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003894 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003895 if (shift_value > 32) {
3896 if (op->IsShl()) {
3897 __ Lsl(o_h, low, shift_value - 32);
3898 __ LoadImmediate(o_l, 0);
3899 } else if (op->IsShr()) {
3900 __ Asr(o_l, high, shift_value - 32);
3901 __ Asr(o_h, high, 31);
3902 } else {
3903 __ Lsr(o_l, high, shift_value - 32);
3904 __ LoadImmediate(o_h, 0);
3905 }
3906 } else if (shift_value == 32) {
3907 if (op->IsShl()) {
3908 __ mov(o_h, ShifterOperand(low));
3909 __ LoadImmediate(o_l, 0);
3910 } else if (op->IsShr()) {
3911 __ mov(o_l, ShifterOperand(high));
3912 __ Asr(o_h, high, 31);
3913 } else {
3914 __ mov(o_l, ShifterOperand(high));
3915 __ LoadImmediate(o_h, 0);
3916 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003917 } else if (shift_value == 1) {
3918 if (op->IsShl()) {
3919 __ Lsls(o_l, low, 1);
3920 __ adc(o_h, high, ShifterOperand(high));
3921 } else if (op->IsShr()) {
3922 __ Asrs(o_h, high, 1);
3923 __ Rrx(o_l, low);
3924 } else {
3925 __ Lsrs(o_h, high, 1);
3926 __ Rrx(o_l, low);
3927 }
3928 } else {
3929 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003930 if (op->IsShl()) {
3931 __ Lsl(o_h, high, shift_value);
3932 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3933 __ Lsl(o_l, low, shift_value);
3934 } else if (op->IsShr()) {
3935 __ Lsr(o_l, low, shift_value);
3936 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3937 __ Asr(o_h, high, shift_value);
3938 } else {
3939 __ Lsr(o_l, low, shift_value);
3940 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3941 __ Lsr(o_h, high, shift_value);
3942 }
3943 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003944 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003945 break;
3946 }
3947 default:
3948 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003949 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003950 }
3951}
3952
3953void LocationsBuilderARM::VisitShl(HShl* shl) {
3954 HandleShift(shl);
3955}
3956
3957void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3958 HandleShift(shl);
3959}
3960
3961void LocationsBuilderARM::VisitShr(HShr* shr) {
3962 HandleShift(shr);
3963}
3964
3965void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3966 HandleShift(shr);
3967}
3968
3969void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3970 HandleShift(ushr);
3971}
3972
3973void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3974 HandleShift(ushr);
3975}
3976
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003977void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003978 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003979 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003980 if (instruction->IsStringAlloc()) {
3981 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3982 } else {
3983 InvokeRuntimeCallingConvention calling_convention;
3984 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00003985 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003986 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003987}
3988
3989void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003990 // Note: if heap poisoning is enabled, the entry point takes cares
3991 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003992 if (instruction->IsStringAlloc()) {
3993 // String is allocated through StringFactory. Call NewEmptyString entry point.
3994 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003995 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003996 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3997 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3998 __ blx(LR);
3999 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4000 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004001 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004002 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004003 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004004}
4005
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004006void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
4007 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004008 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004009 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004010 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004011 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4012 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004013}
4014
4015void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004016 // Note: if heap poisoning is enabled, the entry point takes cares
4017 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004018 QuickEntrypointEnum entrypoint =
4019 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4020 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004021 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004022 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004023}
4024
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004025void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004026 LocationSummary* locations =
4027 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004028 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4029 if (location.IsStackSlot()) {
4030 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4031 } else if (location.IsDoubleStackSlot()) {
4032 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004033 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004034 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004035}
4036
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004037void InstructionCodeGeneratorARM::VisitParameterValue(
4038 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004039 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004040}
4041
4042void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4043 LocationSummary* locations =
4044 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4045 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4046}
4047
4048void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4049 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004050}
4051
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004052void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004053 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004054 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004055 locations->SetInAt(0, Location::RequiresRegister());
4056 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004057}
4058
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004059void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4060 LocationSummary* locations = not_->GetLocations();
4061 Location out = locations->Out();
4062 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004063 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004064 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004065 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004066 break;
4067
4068 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004069 __ mvn(out.AsRegisterPairLow<Register>(),
4070 ShifterOperand(in.AsRegisterPairLow<Register>()));
4071 __ mvn(out.AsRegisterPairHigh<Register>(),
4072 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004073 break;
4074
4075 default:
4076 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4077 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004078}
4079
David Brazdil66d126e2015-04-03 16:02:44 +01004080void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4081 LocationSummary* locations =
4082 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4083 locations->SetInAt(0, Location::RequiresRegister());
4084 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4085}
4086
4087void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004088 LocationSummary* locations = bool_not->GetLocations();
4089 Location out = locations->Out();
4090 Location in = locations->InAt(0);
4091 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4092}
4093
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004094void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004095 LocationSummary* locations =
4096 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004097 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004098 case Primitive::kPrimBoolean:
4099 case Primitive::kPrimByte:
4100 case Primitive::kPrimShort:
4101 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004102 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004103 case Primitive::kPrimLong: {
4104 locations->SetInAt(0, Location::RequiresRegister());
4105 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004106 // Output overlaps because it is written before doing the low comparison.
4107 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004108 break;
4109 }
4110 case Primitive::kPrimFloat:
4111 case Primitive::kPrimDouble: {
4112 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004113 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004114 locations->SetOut(Location::RequiresRegister());
4115 break;
4116 }
4117 default:
4118 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4119 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004120}
4121
4122void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004123 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004124 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004125 Location left = locations->InAt(0);
4126 Location right = locations->InAt(1);
4127
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004128 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004129 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004130 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004131 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004132 case Primitive::kPrimBoolean:
4133 case Primitive::kPrimByte:
4134 case Primitive::kPrimShort:
4135 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004136 case Primitive::kPrimInt: {
4137 __ LoadImmediate(out, 0);
4138 __ cmp(left.AsRegister<Register>(),
4139 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4140 less_cond = LT;
4141 break;
4142 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004143 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004144 __ cmp(left.AsRegisterPairHigh<Register>(),
4145 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004146 __ b(&less, LT);
4147 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004148 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004149 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004150 __ cmp(left.AsRegisterPairLow<Register>(),
4151 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004152 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004153 break;
4154 }
4155 case Primitive::kPrimFloat:
4156 case Primitive::kPrimDouble: {
4157 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004158 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004159 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004160 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004161 break;
4162 }
4163 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004164 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004165 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004166 }
Aart Bika19616e2016-02-01 18:57:58 -08004167
Calin Juravleddb7df22014-11-25 20:56:51 +00004168 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004169 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004170
4171 __ Bind(&greater);
4172 __ LoadImmediate(out, 1);
4173 __ b(&done);
4174
4175 __ Bind(&less);
4176 __ LoadImmediate(out, -1);
4177
4178 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004179}
4180
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004181void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004182 LocationSummary* locations =
4183 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004184 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004185 locations->SetInAt(i, Location::Any());
4186 }
4187 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004188}
4189
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004190void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004191 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004192}
4193
Roland Levillainc9285912015-12-18 10:38:42 +00004194void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4195 // TODO (ported from quick): revisit ARM barrier kinds.
4196 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004197 switch (kind) {
4198 case MemBarrierKind::kAnyStore:
4199 case MemBarrierKind::kLoadAny:
4200 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004201 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004202 break;
4203 }
4204 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004205 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004206 break;
4207 }
4208 default:
4209 LOG(FATAL) << "Unexpected memory barrier " << kind;
4210 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004211 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004212}
4213
4214void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4215 uint32_t offset,
4216 Register out_lo,
4217 Register out_hi) {
4218 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004219 // Ensure `out_lo` is different from `addr`, so that loading
4220 // `offset` into `out_lo` does not clutter `addr`.
4221 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004222 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004223 __ add(IP, addr, ShifterOperand(out_lo));
4224 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004225 }
4226 __ ldrexd(out_lo, out_hi, addr);
4227}
4228
4229void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4230 uint32_t offset,
4231 Register value_lo,
4232 Register value_hi,
4233 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004234 Register temp2,
4235 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004236 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004237 if (offset != 0) {
4238 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004239 __ add(IP, addr, ShifterOperand(temp1));
4240 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004241 }
4242 __ Bind(&fail);
4243 // We need a load followed by store. (The address used in a STREX instruction must
4244 // be the same as the address in the most recently executed LDREX instruction.)
4245 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004246 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004247 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004248 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004249}
4250
4251void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4252 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4253
Nicolas Geoffray39468442014-09-02 15:17:15 +01004254 LocationSummary* locations =
4255 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004256 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004257
Calin Juravle52c48962014-12-16 17:02:57 +00004258 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004259 if (Primitive::IsFloatingPointType(field_type)) {
4260 locations->SetInAt(1, Location::RequiresFpuRegister());
4261 } else {
4262 locations->SetInAt(1, Location::RequiresRegister());
4263 }
4264
Calin Juravle52c48962014-12-16 17:02:57 +00004265 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004266 bool generate_volatile = field_info.IsVolatile()
4267 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004268 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004269 bool needs_write_barrier =
4270 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004271 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004272 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004273 if (needs_write_barrier) {
4274 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004275 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004276 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004277 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004278 // - registers need to be consecutive
4279 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004280 // We don't test for ARM yet, and the assertion makes sure that we
4281 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004282 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4283
4284 locations->AddTemp(Location::RequiresRegister());
4285 locations->AddTemp(Location::RequiresRegister());
4286 if (field_type == Primitive::kPrimDouble) {
4287 // For doubles we need two more registers to copy the value.
4288 locations->AddTemp(Location::RegisterLocation(R2));
4289 locations->AddTemp(Location::RegisterLocation(R3));
4290 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004291 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004292}
4293
Calin Juravle52c48962014-12-16 17:02:57 +00004294void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004295 const FieldInfo& field_info,
4296 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004297 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4298
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004299 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004300 Register base = locations->InAt(0).AsRegister<Register>();
4301 Location value = locations->InAt(1);
4302
4303 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004304 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004305 Primitive::Type field_type = field_info.GetFieldType();
4306 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004307 bool needs_write_barrier =
4308 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004309
4310 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004311 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004312 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004313
4314 switch (field_type) {
4315 case Primitive::kPrimBoolean:
4316 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004317 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004318 break;
4319 }
4320
4321 case Primitive::kPrimShort:
4322 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004323 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004324 break;
4325 }
4326
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004327 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004328 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004329 if (kPoisonHeapReferences && needs_write_barrier) {
4330 // Note that in the case where `value` is a null reference,
4331 // we do not enter this block, as a null reference does not
4332 // need poisoning.
4333 DCHECK_EQ(field_type, Primitive::kPrimNot);
4334 Register temp = locations->GetTemp(0).AsRegister<Register>();
4335 __ Mov(temp, value.AsRegister<Register>());
4336 __ PoisonHeapReference(temp);
4337 __ StoreToOffset(kStoreWord, temp, base, offset);
4338 } else {
4339 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4340 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004341 break;
4342 }
4343
4344 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004345 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004346 GenerateWideAtomicStore(base, offset,
4347 value.AsRegisterPairLow<Register>(),
4348 value.AsRegisterPairHigh<Register>(),
4349 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004350 locations->GetTemp(1).AsRegister<Register>(),
4351 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004352 } else {
4353 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004354 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004355 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004356 break;
4357 }
4358
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004359 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004360 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004361 break;
4362 }
4363
4364 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004365 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004366 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004367 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4368 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4369
4370 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4371
4372 GenerateWideAtomicStore(base, offset,
4373 value_reg_lo,
4374 value_reg_hi,
4375 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004376 locations->GetTemp(3).AsRegister<Register>(),
4377 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004378 } else {
4379 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004380 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004381 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004382 break;
4383 }
4384
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004385 case Primitive::kPrimVoid:
4386 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004387 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004388 }
Calin Juravle52c48962014-12-16 17:02:57 +00004389
Calin Juravle77520bc2015-01-12 18:45:46 +00004390 // Longs and doubles are handled in the switch.
4391 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4392 codegen_->MaybeRecordImplicitNullCheck(instruction);
4393 }
4394
4395 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4396 Register temp = locations->GetTemp(0).AsRegister<Register>();
4397 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004398 codegen_->MarkGCCard(
4399 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004400 }
4401
Calin Juravle52c48962014-12-16 17:02:57 +00004402 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004403 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004404 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004405}
4406
Calin Juravle52c48962014-12-16 17:02:57 +00004407void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4408 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004409
4410 bool object_field_get_with_read_barrier =
4411 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004412 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004413 new (GetGraph()->GetArena()) LocationSummary(instruction,
4414 object_field_get_with_read_barrier ?
4415 LocationSummary::kCallOnSlowPath :
4416 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004417 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004418 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004419 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004420 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004421
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004422 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004423 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004424 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004425 // The output overlaps in case of volatile long: we don't want the
4426 // code generated by GenerateWideAtomicLoad to overwrite the
4427 // object's location. Likewise, in the case of an object field get
4428 // with read barriers enabled, we do not want the load to overwrite
4429 // the object's location, as we need it to emit the read barrier.
4430 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4431 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004432
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004433 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4434 locations->SetOut(Location::RequiresFpuRegister());
4435 } else {
4436 locations->SetOut(Location::RequiresRegister(),
4437 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4438 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004439 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004440 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004441 // - registers need to be consecutive
4442 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004443 // We don't test for ARM yet, and the assertion makes sure that we
4444 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004445 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4446 locations->AddTemp(Location::RequiresRegister());
4447 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004448 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4449 // We need a temporary register for the read barrier marking slow
4450 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4451 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004452 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004453}
4454
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004455Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4456 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4457 << input->GetType();
4458 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4459 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4460 return Location::ConstantLocation(input->AsConstant());
4461 } else {
4462 return Location::RequiresFpuRegister();
4463 }
4464}
4465
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004466Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4467 Opcode opcode) {
4468 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4469 if (constant->IsConstant() &&
4470 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4471 return Location::ConstantLocation(constant->AsConstant());
4472 }
4473 return Location::RequiresRegister();
4474}
4475
4476bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4477 Opcode opcode) {
4478 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4479 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004480 Opcode high_opcode = opcode;
4481 SetCc low_set_cc = kCcDontCare;
4482 switch (opcode) {
4483 case SUB:
4484 // Flip the operation to an ADD.
4485 value = -value;
4486 opcode = ADD;
4487 FALLTHROUGH_INTENDED;
4488 case ADD:
4489 if (Low32Bits(value) == 0u) {
4490 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4491 }
4492 high_opcode = ADC;
4493 low_set_cc = kCcSet;
4494 break;
4495 default:
4496 break;
4497 }
4498 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4499 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004500 } else {
4501 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4502 }
4503}
4504
Vladimir Marko59751a72016-08-05 14:37:27 +01004505bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4506 Opcode opcode,
4507 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004508 ShifterOperand so;
4509 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004510 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004511 return true;
4512 }
4513 Opcode neg_opcode = kNoOperand;
4514 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004515 case AND: neg_opcode = BIC; value = ~value; break;
4516 case ORR: neg_opcode = ORN; value = ~value; break;
4517 case ADD: neg_opcode = SUB; value = -value; break;
4518 case ADC: neg_opcode = SBC; value = ~value; break;
4519 case SUB: neg_opcode = ADD; value = -value; break;
4520 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004521 default:
4522 return false;
4523 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004524 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004525}
4526
Calin Juravle52c48962014-12-16 17:02:57 +00004527void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4528 const FieldInfo& field_info) {
4529 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004530
Calin Juravle52c48962014-12-16 17:02:57 +00004531 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004532 Location base_loc = locations->InAt(0);
4533 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004534 Location out = locations->Out();
4535 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004536 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004537 Primitive::Type field_type = field_info.GetFieldType();
4538 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4539
4540 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004541 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004542 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004543 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004544
Roland Levillainc9285912015-12-18 10:38:42 +00004545 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004546 __ LoadFromOffset(kLoadSignedByte, 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::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004550 __ LoadFromOffset(kLoadSignedHalfword, 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::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004554 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004555 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004556
4557 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004558 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004559 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004560
4561 case Primitive::kPrimNot: {
4562 // /* HeapReference<Object> */ out = *(base + offset)
4563 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4564 Location temp_loc = locations->GetTemp(0);
4565 // Note that a potential implicit null check is handled in this
4566 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4567 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4568 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4569 if (is_volatile) {
4570 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4571 }
4572 } else {
4573 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4574 codegen_->MaybeRecordImplicitNullCheck(instruction);
4575 if (is_volatile) {
4576 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4577 }
4578 // If read barriers are enabled, emit read barriers other than
4579 // Baker's using a slow path (and also unpoison the loaded
4580 // reference, if heap poisoning is enabled).
4581 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4582 }
4583 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004584 }
4585
Roland Levillainc9285912015-12-18 10:38:42 +00004586 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004587 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004588 GenerateWideAtomicLoad(base, offset,
4589 out.AsRegisterPairLow<Register>(),
4590 out.AsRegisterPairHigh<Register>());
4591 } else {
4592 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4593 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004594 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004595
Roland Levillainc9285912015-12-18 10:38:42 +00004596 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004597 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004598 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004599
4600 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004601 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004602 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004603 Register lo = locations->GetTemp(0).AsRegister<Register>();
4604 Register hi = locations->GetTemp(1).AsRegister<Register>();
4605 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004606 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004607 __ vmovdrr(out_reg, lo, hi);
4608 } else {
4609 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004610 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004611 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004612 break;
4613 }
4614
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004615 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004616 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004617 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004618 }
Calin Juravle52c48962014-12-16 17:02:57 +00004619
Roland Levillainc9285912015-12-18 10:38:42 +00004620 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4621 // Potential implicit null checks, in the case of reference or
4622 // double fields, are handled in the previous switch statement.
4623 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004624 codegen_->MaybeRecordImplicitNullCheck(instruction);
4625 }
4626
Calin Juravle52c48962014-12-16 17:02:57 +00004627 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004628 if (field_type == Primitive::kPrimNot) {
4629 // Memory barriers, in the case of references, are also handled
4630 // in the previous switch statement.
4631 } else {
4632 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4633 }
Roland Levillain4d027112015-07-01 15:41:14 +01004634 }
Calin Juravle52c48962014-12-16 17:02:57 +00004635}
4636
4637void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4638 HandleFieldSet(instruction, instruction->GetFieldInfo());
4639}
4640
4641void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004642 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004643}
4644
4645void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4646 HandleFieldGet(instruction, instruction->GetFieldInfo());
4647}
4648
4649void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4650 HandleFieldGet(instruction, instruction->GetFieldInfo());
4651}
4652
4653void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4654 HandleFieldGet(instruction, instruction->GetFieldInfo());
4655}
4656
4657void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4658 HandleFieldGet(instruction, instruction->GetFieldInfo());
4659}
4660
4661void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4662 HandleFieldSet(instruction, instruction->GetFieldInfo());
4663}
4664
4665void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004666 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004667}
4668
Calin Juravlee460d1d2015-09-29 04:52:17 +01004669void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4670 HUnresolvedInstanceFieldGet* instruction) {
4671 FieldAccessCallingConventionARM calling_convention;
4672 codegen_->CreateUnresolvedFieldLocationSummary(
4673 instruction, instruction->GetFieldType(), calling_convention);
4674}
4675
4676void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4677 HUnresolvedInstanceFieldGet* instruction) {
4678 FieldAccessCallingConventionARM calling_convention;
4679 codegen_->GenerateUnresolvedFieldAccess(instruction,
4680 instruction->GetFieldType(),
4681 instruction->GetFieldIndex(),
4682 instruction->GetDexPc(),
4683 calling_convention);
4684}
4685
4686void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4687 HUnresolvedInstanceFieldSet* instruction) {
4688 FieldAccessCallingConventionARM calling_convention;
4689 codegen_->CreateUnresolvedFieldLocationSummary(
4690 instruction, instruction->GetFieldType(), calling_convention);
4691}
4692
4693void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4694 HUnresolvedInstanceFieldSet* instruction) {
4695 FieldAccessCallingConventionARM calling_convention;
4696 codegen_->GenerateUnresolvedFieldAccess(instruction,
4697 instruction->GetFieldType(),
4698 instruction->GetFieldIndex(),
4699 instruction->GetDexPc(),
4700 calling_convention);
4701}
4702
4703void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4704 HUnresolvedStaticFieldGet* instruction) {
4705 FieldAccessCallingConventionARM calling_convention;
4706 codegen_->CreateUnresolvedFieldLocationSummary(
4707 instruction, instruction->GetFieldType(), calling_convention);
4708}
4709
4710void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4711 HUnresolvedStaticFieldGet* instruction) {
4712 FieldAccessCallingConventionARM calling_convention;
4713 codegen_->GenerateUnresolvedFieldAccess(instruction,
4714 instruction->GetFieldType(),
4715 instruction->GetFieldIndex(),
4716 instruction->GetDexPc(),
4717 calling_convention);
4718}
4719
4720void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4721 HUnresolvedStaticFieldSet* instruction) {
4722 FieldAccessCallingConventionARM calling_convention;
4723 codegen_->CreateUnresolvedFieldLocationSummary(
4724 instruction, instruction->GetFieldType(), calling_convention);
4725}
4726
4727void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4728 HUnresolvedStaticFieldSet* instruction) {
4729 FieldAccessCallingConventionARM calling_convention;
4730 codegen_->GenerateUnresolvedFieldAccess(instruction,
4731 instruction->GetFieldType(),
4732 instruction->GetFieldIndex(),
4733 instruction->GetDexPc(),
4734 calling_convention);
4735}
4736
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004737void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004738 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4739 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004740}
4741
Calin Juravle2ae48182016-03-16 14:05:09 +00004742void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4743 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004744 return;
4745 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004746 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004747
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004748 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004749 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004750}
4751
Calin Juravle2ae48182016-03-16 14:05:09 +00004752void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004753 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004754 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004755
4756 LocationSummary* locations = instruction->GetLocations();
4757 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004758
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004759 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004760}
4761
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004762void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004763 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004764}
4765
Artem Serov6c916792016-07-11 14:02:34 +01004766static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4767 switch (type) {
4768 case Primitive::kPrimNot:
4769 return kLoadWord;
4770 case Primitive::kPrimBoolean:
4771 return kLoadUnsignedByte;
4772 case Primitive::kPrimByte:
4773 return kLoadSignedByte;
4774 case Primitive::kPrimChar:
4775 return kLoadUnsignedHalfword;
4776 case Primitive::kPrimShort:
4777 return kLoadSignedHalfword;
4778 case Primitive::kPrimInt:
4779 return kLoadWord;
4780 case Primitive::kPrimLong:
4781 return kLoadWordPair;
4782 case Primitive::kPrimFloat:
4783 return kLoadSWord;
4784 case Primitive::kPrimDouble:
4785 return kLoadDWord;
4786 default:
4787 LOG(FATAL) << "Unreachable type " << type;
4788 UNREACHABLE();
4789 }
4790}
4791
4792static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4793 switch (type) {
4794 case Primitive::kPrimNot:
4795 return kStoreWord;
4796 case Primitive::kPrimBoolean:
4797 case Primitive::kPrimByte:
4798 return kStoreByte;
4799 case Primitive::kPrimChar:
4800 case Primitive::kPrimShort:
4801 return kStoreHalfword;
4802 case Primitive::kPrimInt:
4803 return kStoreWord;
4804 case Primitive::kPrimLong:
4805 return kStoreWordPair;
4806 case Primitive::kPrimFloat:
4807 return kStoreSWord;
4808 case Primitive::kPrimDouble:
4809 return kStoreDWord;
4810 default:
4811 LOG(FATAL) << "Unreachable type " << type;
4812 UNREACHABLE();
4813 }
4814}
4815
4816void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4817 Location out_loc,
4818 Register base,
4819 Register reg_offset,
4820 Condition cond) {
4821 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4822 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4823
4824 switch (type) {
4825 case Primitive::kPrimByte:
4826 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4827 break;
4828 case Primitive::kPrimBoolean:
4829 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4830 break;
4831 case Primitive::kPrimShort:
4832 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4833 break;
4834 case Primitive::kPrimChar:
4835 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4836 break;
4837 case Primitive::kPrimNot:
4838 case Primitive::kPrimInt:
4839 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4840 break;
4841 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4842 case Primitive::kPrimLong:
4843 case Primitive::kPrimFloat:
4844 case Primitive::kPrimDouble:
4845 default:
4846 LOG(FATAL) << "Unreachable type " << type;
4847 UNREACHABLE();
4848 }
4849}
4850
4851void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4852 Location loc,
4853 Register base,
4854 Register reg_offset,
4855 Condition cond) {
4856 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4857 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4858
4859 switch (type) {
4860 case Primitive::kPrimByte:
4861 case Primitive::kPrimBoolean:
4862 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4863 break;
4864 case Primitive::kPrimShort:
4865 case Primitive::kPrimChar:
4866 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4867 break;
4868 case Primitive::kPrimNot:
4869 case Primitive::kPrimInt:
4870 __ str(loc.AsRegister<Register>(), mem_address, cond);
4871 break;
4872 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4873 case Primitive::kPrimLong:
4874 case Primitive::kPrimFloat:
4875 case Primitive::kPrimDouble:
4876 default:
4877 LOG(FATAL) << "Unreachable type " << type;
4878 UNREACHABLE();
4879 }
4880}
4881
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004882void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004883 bool object_array_get_with_read_barrier =
4884 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004885 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004886 new (GetGraph()->GetArena()) LocationSummary(instruction,
4887 object_array_get_with_read_barrier ?
4888 LocationSummary::kCallOnSlowPath :
4889 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004890 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004891 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004892 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004893 locations->SetInAt(0, Location::RequiresRegister());
4894 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004895 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4896 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4897 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004898 // The output overlaps in the case of an object array get with
4899 // read barriers enabled: we do not want the move to overwrite the
4900 // array's location, as we need it to emit the read barrier.
4901 locations->SetOut(
4902 Location::RequiresRegister(),
4903 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004904 }
Roland Levillainc9285912015-12-18 10:38:42 +00004905 // We need a temporary register for the read barrier marking slow
4906 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07004907 // Also need for String compression feature.
4908 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4909 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00004910 locations->AddTemp(Location::RequiresRegister());
4911 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004912}
4913
4914void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4915 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004916 Location obj_loc = locations->InAt(0);
4917 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004918 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004919 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004920 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004921 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07004922 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4923 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01004924 HInstruction* array_instr = instruction->GetArray();
4925 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01004926
Roland Levillain4d027112015-07-01 15:41:14 +01004927 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004928 case Primitive::kPrimBoolean:
4929 case Primitive::kPrimByte:
4930 case Primitive::kPrimShort:
4931 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004932 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004933 Register length;
4934 if (maybe_compressed_char_at) {
4935 length = locations->GetTemp(0).AsRegister<Register>();
4936 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4937 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4938 codegen_->MaybeRecordImplicitNullCheck(instruction);
4939 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004940 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004941 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07004942 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07004943 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004944 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4945 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4946 "Expecting 0=compressed, 1=uncompressed");
4947 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004948 __ LoadFromOffset(kLoadUnsignedByte,
4949 out_loc.AsRegister<Register>(),
4950 obj,
4951 data_offset + const_index);
4952 __ b(&done);
4953 __ Bind(&uncompressed_load);
4954 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4955 out_loc.AsRegister<Register>(),
4956 obj,
4957 data_offset + (const_index << 1));
4958 __ Bind(&done);
4959 } else {
4960 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01004961
jessicahandojo05765752016-09-09 19:01:32 -07004962 LoadOperandType load_type = GetLoadOperandType(type);
4963 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4964 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004965 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004966 Register temp = IP;
4967
4968 if (has_intermediate_address) {
4969 // We do not need to compute the intermediate address from the array: the
4970 // input instruction has done it already. See the comment in
4971 // `TryExtractArrayAccessAddress()`.
4972 if (kIsDebugBuild) {
4973 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4974 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4975 }
4976 temp = obj;
4977 } else {
4978 __ add(temp, obj, ShifterOperand(data_offset));
4979 }
jessicahandojo05765752016-09-09 19:01:32 -07004980 if (maybe_compressed_char_at) {
4981 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004982 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4983 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4984 "Expecting 0=compressed, 1=uncompressed");
4985 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004986 __ ldrb(out_loc.AsRegister<Register>(),
4987 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
4988 __ b(&done);
4989 __ Bind(&uncompressed_load);
4990 __ ldrh(out_loc.AsRegister<Register>(),
4991 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
4992 __ Bind(&done);
4993 } else {
4994 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4995 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004996 }
4997 break;
4998 }
4999
Roland Levillainc9285912015-12-18 10:38:42 +00005000 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00005001 // The read barrier instrumentation of object ArrayGet
5002 // instructions does not support the HIntermediateAddress
5003 // instruction.
5004 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5005
Roland Levillainc9285912015-12-18 10:38:42 +00005006 static_assert(
5007 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5008 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00005009 // /* HeapReference<Object> */ out =
5010 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5011 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5012 Location temp = locations->GetTemp(0);
5013 // Note that a potential implicit null check is handled in this
5014 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
5015 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5016 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5017 } else {
5018 Register out = out_loc.AsRegister<Register>();
5019 if (index.IsConstant()) {
5020 size_t offset =
5021 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5022 __ LoadFromOffset(kLoadWord, out, obj, offset);
5023 codegen_->MaybeRecordImplicitNullCheck(instruction);
5024 // If read barriers are enabled, emit read barriers other than
5025 // Baker's using a slow path (and also unpoison the loaded
5026 // reference, if heap poisoning is enabled).
5027 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5028 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005029 Register temp = IP;
5030
5031 if (has_intermediate_address) {
5032 // We do not need to compute the intermediate address from the array: the
5033 // input instruction has done it already. See the comment in
5034 // `TryExtractArrayAccessAddress()`.
5035 if (kIsDebugBuild) {
5036 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5037 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5038 }
5039 temp = obj;
5040 } else {
5041 __ add(temp, obj, ShifterOperand(data_offset));
5042 }
5043 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005044
Roland Levillainc9285912015-12-18 10:38:42 +00005045 codegen_->MaybeRecordImplicitNullCheck(instruction);
5046 // If read barriers are enabled, emit read barriers other than
5047 // Baker's using a slow path (and also unpoison the loaded
5048 // reference, if heap poisoning is enabled).
5049 codegen_->MaybeGenerateReadBarrierSlow(
5050 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5051 }
5052 }
5053 break;
5054 }
5055
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005056 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005058 size_t offset =
5059 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005060 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005061 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005062 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005063 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 }
5065 break;
5066 }
5067
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005068 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005069 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005070 if (index.IsConstant()) {
5071 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005072 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005073 } else {
5074 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005075 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005076 }
5077 break;
5078 }
5079
5080 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005081 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005082 if (index.IsConstant()) {
5083 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005084 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005085 } else {
5086 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005087 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005088 }
5089 break;
5090 }
5091
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005092 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005093 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005094 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005095 }
Roland Levillain4d027112015-07-01 15:41:14 +01005096
5097 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005098 // Potential implicit null checks, in the case of reference
5099 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005100 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005101 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005102 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005103}
5104
5105void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005106 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005107
5108 bool needs_write_barrier =
5109 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005110 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005111
Nicolas Geoffray39468442014-09-02 15:17:15 +01005112 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005113 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005114 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005115 LocationSummary::kCallOnSlowPath :
5116 LocationSummary::kNoCall);
5117
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005118 locations->SetInAt(0, Location::RequiresRegister());
5119 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5120 if (Primitive::IsFloatingPointType(value_type)) {
5121 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005123 locations->SetInAt(2, Location::RequiresRegister());
5124 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005125 if (needs_write_barrier) {
5126 // Temporary registers for the write barrier.
5127 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005128 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005129 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005130}
5131
5132void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5133 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005134 Location array_loc = locations->InAt(0);
5135 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005136 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005137 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005138 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005139 bool needs_write_barrier =
5140 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005141 uint32_t data_offset =
5142 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5143 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005144 HInstruction* array_instr = instruction->GetArray();
5145 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005146
5147 switch (value_type) {
5148 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005149 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005150 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005151 case Primitive::kPrimChar:
5152 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005154 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5155 uint32_t full_offset =
5156 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5157 StoreOperandType store_type = GetStoreOperandType(value_type);
5158 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005159 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005160 Register temp = IP;
5161
5162 if (has_intermediate_address) {
5163 // We do not need to compute the intermediate address from the array: the
5164 // input instruction has done it already. See the comment in
5165 // `TryExtractArrayAccessAddress()`.
5166 if (kIsDebugBuild) {
5167 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5168 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5169 }
5170 temp = array;
5171 } else {
5172 __ add(temp, array, ShifterOperand(data_offset));
5173 }
Artem Serov6c916792016-07-11 14:02:34 +01005174 codegen_->StoreToShiftedRegOffset(value_type,
5175 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005176 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005177 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005178 }
5179 break;
5180 }
5181
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005182 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005183 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005184 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5185 // See the comment in instruction_simplifier_shared.cc.
5186 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005187
5188 if (instruction->InputAt(2)->IsNullConstant()) {
5189 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005190 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005191 size_t offset =
5192 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005193 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005194 } else {
5195 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005196 __ add(IP, array, ShifterOperand(data_offset));
5197 codegen_->StoreToShiftedRegOffset(value_type,
5198 value_loc,
5199 IP,
5200 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005201 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005202 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005203 DCHECK(!needs_write_barrier);
5204 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005205 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005206 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005207
5208 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005209 Location temp1_loc = locations->GetTemp(0);
5210 Register temp1 = temp1_loc.AsRegister<Register>();
5211 Location temp2_loc = locations->GetTemp(1);
5212 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005213 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5214 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5215 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5216 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005217 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005218
Roland Levillain3b359c72015-11-17 19:35:12 +00005219 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005220 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5221 codegen_->AddSlowPath(slow_path);
5222 if (instruction->GetValueCanBeNull()) {
5223 Label non_zero;
5224 __ CompareAndBranchIfNonZero(value, &non_zero);
5225 if (index.IsConstant()) {
5226 size_t offset =
5227 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5228 __ StoreToOffset(kStoreWord, value, array, offset);
5229 } else {
5230 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005231 __ add(IP, array, ShifterOperand(data_offset));
5232 codegen_->StoreToShiftedRegOffset(value_type,
5233 value_loc,
5234 IP,
5235 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005236 }
5237 codegen_->MaybeRecordImplicitNullCheck(instruction);
5238 __ b(&done);
5239 __ Bind(&non_zero);
5240 }
5241
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005242 // Note that when read barriers are enabled, the type checks
5243 // are performed without read barriers. This is fine, even in
5244 // the case where a class object is in the from-space after
5245 // the flip, as a comparison involving such a type would not
5246 // produce a false positive; it may of course produce a false
5247 // negative, in which case we would take the ArraySet slow
5248 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005249
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005250 // /* HeapReference<Class> */ temp1 = array->klass_
5251 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5252 codegen_->MaybeRecordImplicitNullCheck(instruction);
5253 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005254
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005255 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5256 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5257 // /* HeapReference<Class> */ temp2 = value->klass_
5258 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5259 // If heap poisoning is enabled, no need to unpoison `temp1`
5260 // nor `temp2`, as we are comparing two poisoned references.
5261 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005262
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005263 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5264 Label do_put;
5265 __ b(&do_put, EQ);
5266 // If heap poisoning is enabled, the `temp1` reference has
5267 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005268 __ MaybeUnpoisonHeapReference(temp1);
5269
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005270 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5271 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5272 // If heap poisoning is enabled, no need to unpoison
5273 // `temp1`, as we are comparing against null below.
5274 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5275 __ Bind(&do_put);
5276 } else {
5277 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005278 }
5279 }
5280
Artem Serov6c916792016-07-11 14:02:34 +01005281 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005282 if (kPoisonHeapReferences) {
5283 // Note that in the case where `value` is a null reference,
5284 // we do not enter this block, as a null reference does not
5285 // need poisoning.
5286 DCHECK_EQ(value_type, Primitive::kPrimNot);
5287 __ Mov(temp1, value);
5288 __ PoisonHeapReference(temp1);
5289 source = temp1;
5290 }
5291
5292 if (index.IsConstant()) {
5293 size_t offset =
5294 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5295 __ StoreToOffset(kStoreWord, source, array, offset);
5296 } else {
5297 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005298
5299 __ add(IP, array, ShifterOperand(data_offset));
5300 codegen_->StoreToShiftedRegOffset(value_type,
5301 Location::RegisterLocation(source),
5302 IP,
5303 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005304 }
5305
Roland Levillain3b359c72015-11-17 19:35:12 +00005306 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005307 codegen_->MaybeRecordImplicitNullCheck(instruction);
5308 }
5309
5310 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5311
5312 if (done.IsLinked()) {
5313 __ Bind(&done);
5314 }
5315
5316 if (slow_path != nullptr) {
5317 __ Bind(slow_path->GetExitLabel());
5318 }
5319
5320 break;
5321 }
5322
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005323 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005324 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005325 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005326 size_t offset =
5327 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005328 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005329 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005330 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005331 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005332 }
5333 break;
5334 }
5335
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005336 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005337 Location value = locations->InAt(2);
5338 DCHECK(value.IsFpuRegister());
5339 if (index.IsConstant()) {
5340 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005341 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005342 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005343 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005344 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5345 }
5346 break;
5347 }
5348
5349 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005350 Location value = locations->InAt(2);
5351 DCHECK(value.IsFpuRegisterPair());
5352 if (index.IsConstant()) {
5353 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005354 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005355 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005356 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005357 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5358 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005359
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005360 break;
5361 }
5362
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005363 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005364 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005365 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005366 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005367
Roland Levillain80e67092016-01-08 16:04:55 +00005368 // Objects are handled in the switch.
5369 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005370 codegen_->MaybeRecordImplicitNullCheck(instruction);
5371 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005372}
5373
5374void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005375 LocationSummary* locations =
5376 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005377 locations->SetInAt(0, Location::RequiresRegister());
5378 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005379}
5380
5381void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5382 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005383 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005384 Register obj = locations->InAt(0).AsRegister<Register>();
5385 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005387 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005388 // Mask out compression flag from String's array length.
5389 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005390 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07005391 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005392}
5393
Artem Serov328429f2016-07-06 16:23:04 +01005394void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005395 LocationSummary* locations =
5396 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5397
5398 locations->SetInAt(0, Location::RequiresRegister());
5399 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5400 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5401}
5402
5403void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5404 LocationSummary* locations = instruction->GetLocations();
5405 Location out = locations->Out();
5406 Location first = locations->InAt(0);
5407 Location second = locations->InAt(1);
5408
Artem Serov328429f2016-07-06 16:23:04 +01005409 if (second.IsRegister()) {
5410 __ add(out.AsRegister<Register>(),
5411 first.AsRegister<Register>(),
5412 ShifterOperand(second.AsRegister<Register>()));
5413 } else {
5414 __ AddConstant(out.AsRegister<Register>(),
5415 first.AsRegister<Register>(),
5416 second.GetConstant()->AsIntConstant()->GetValue());
5417 }
5418}
5419
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005420void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005421 RegisterSet caller_saves = RegisterSet::Empty();
5422 InvokeRuntimeCallingConvention calling_convention;
5423 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5424 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5425 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005426 locations->SetInAt(0, Location::RequiresRegister());
5427 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005428}
5429
5430void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5431 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005432 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005433 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005434 codegen_->AddSlowPath(slow_path);
5435
Roland Levillain271ab9c2014-11-27 15:23:57 +00005436 Register index = locations->InAt(0).AsRegister<Register>();
5437 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005438
5439 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005440 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005441}
5442
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005443void CodeGeneratorARM::MarkGCCard(Register temp,
5444 Register card,
5445 Register object,
5446 Register value,
5447 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005448 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005449 if (can_be_null) {
5450 __ CompareAndBranchIfZero(value, &is_null);
5451 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005452 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005453 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5454 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005455 if (can_be_null) {
5456 __ Bind(&is_null);
5457 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005458}
5459
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005460void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005461 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005462}
5463
5464void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005465 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5466}
5467
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005468void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005469 LocationSummary* locations =
5470 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005471 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005472}
5473
5474void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005475 HBasicBlock* block = instruction->GetBlock();
5476 if (block->GetLoopInformation() != nullptr) {
5477 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5478 // The back edge will generate the suspend check.
5479 return;
5480 }
5481 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5482 // The goto will generate the suspend check.
5483 return;
5484 }
5485 GenerateSuspendCheck(instruction, nullptr);
5486}
5487
5488void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5489 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005490 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005491 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5492 if (slow_path == nullptr) {
5493 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5494 instruction->SetSlowPath(slow_path);
5495 codegen_->AddSlowPath(slow_path);
5496 if (successor != nullptr) {
5497 DCHECK(successor->IsLoopHeader());
5498 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5499 }
5500 } else {
5501 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5502 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005503
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005504 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005505 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005506 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005507 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005508 __ Bind(slow_path->GetReturnLabel());
5509 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005510 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005511 __ b(slow_path->GetEntryLabel());
5512 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005513}
5514
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005515ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5516 return codegen_->GetAssembler();
5517}
5518
5519void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005520 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005521 Location source = move->GetSource();
5522 Location destination = move->GetDestination();
5523
5524 if (source.IsRegister()) {
5525 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005526 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005527 } else if (destination.IsFpuRegister()) {
5528 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005529 } else {
5530 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005531 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005532 SP, destination.GetStackIndex());
5533 }
5534 } else if (source.IsStackSlot()) {
5535 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005536 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005537 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005538 } else if (destination.IsFpuRegister()) {
5539 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005540 } else {
5541 DCHECK(destination.IsStackSlot());
5542 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5543 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5544 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005545 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005546 if (destination.IsRegister()) {
5547 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5548 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005549 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005550 } else {
5551 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005552 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5553 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005554 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005555 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005556 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5557 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005558 } else if (destination.IsRegisterPair()) {
5559 DCHECK(ExpectedPairLayout(destination));
5560 __ LoadFromOffset(
5561 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5562 } else {
5563 DCHECK(destination.IsFpuRegisterPair()) << destination;
5564 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5565 SP,
5566 source.GetStackIndex());
5567 }
5568 } else if (source.IsRegisterPair()) {
5569 if (destination.IsRegisterPair()) {
5570 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5571 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005572 } else if (destination.IsFpuRegisterPair()) {
5573 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5574 source.AsRegisterPairLow<Register>(),
5575 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005576 } else {
5577 DCHECK(destination.IsDoubleStackSlot()) << destination;
5578 DCHECK(ExpectedPairLayout(source));
5579 __ StoreToOffset(
5580 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5581 }
5582 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005583 if (destination.IsRegisterPair()) {
5584 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5585 destination.AsRegisterPairHigh<Register>(),
5586 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5587 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005588 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5589 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5590 } else {
5591 DCHECK(destination.IsDoubleStackSlot()) << destination;
5592 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5593 SP,
5594 destination.GetStackIndex());
5595 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005596 } else {
5597 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005598 HConstant* constant = source.GetConstant();
5599 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5600 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005601 if (destination.IsRegister()) {
5602 __ LoadImmediate(destination.AsRegister<Register>(), value);
5603 } else {
5604 DCHECK(destination.IsStackSlot());
5605 __ LoadImmediate(IP, value);
5606 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5607 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005608 } else if (constant->IsLongConstant()) {
5609 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005610 if (destination.IsRegisterPair()) {
5611 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5612 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005613 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005614 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005615 __ LoadImmediate(IP, Low32Bits(value));
5616 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5617 __ LoadImmediate(IP, High32Bits(value));
5618 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5619 }
5620 } else if (constant->IsDoubleConstant()) {
5621 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005622 if (destination.IsFpuRegisterPair()) {
5623 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005624 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005625 DCHECK(destination.IsDoubleStackSlot()) << destination;
5626 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005627 __ LoadImmediate(IP, Low32Bits(int_value));
5628 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5629 __ LoadImmediate(IP, High32Bits(int_value));
5630 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5631 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005632 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005633 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005634 float value = constant->AsFloatConstant()->GetValue();
5635 if (destination.IsFpuRegister()) {
5636 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5637 } else {
5638 DCHECK(destination.IsStackSlot());
5639 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5640 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5641 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005642 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005643 }
5644}
5645
5646void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5647 __ Mov(IP, reg);
5648 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5649 __ StoreToOffset(kStoreWord, IP, SP, mem);
5650}
5651
5652void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5653 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5654 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5655 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5656 SP, mem1 + stack_offset);
5657 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5658 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5659 SP, mem2 + stack_offset);
5660 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5661}
5662
5663void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005664 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005665 Location source = move->GetSource();
5666 Location destination = move->GetDestination();
5667
5668 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005669 DCHECK_NE(source.AsRegister<Register>(), IP);
5670 DCHECK_NE(destination.AsRegister<Register>(), IP);
5671 __ Mov(IP, source.AsRegister<Register>());
5672 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5673 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005674 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005675 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005676 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005677 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005678 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5679 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005680 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005681 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005682 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005683 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005684 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005685 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005686 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005687 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005688 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5689 destination.AsRegisterPairHigh<Register>(),
5690 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005691 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005692 Register low_reg = source.IsRegisterPair()
5693 ? source.AsRegisterPairLow<Register>()
5694 : destination.AsRegisterPairLow<Register>();
5695 int mem = source.IsRegisterPair()
5696 ? destination.GetStackIndex()
5697 : source.GetStackIndex();
5698 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005699 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005700 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005701 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005702 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005703 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5704 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005705 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005706 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005707 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005708 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5709 DRegister reg = source.IsFpuRegisterPair()
5710 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5711 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5712 int mem = source.IsFpuRegisterPair()
5713 ? destination.GetStackIndex()
5714 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005715 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005716 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005717 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005718 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5719 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5720 : destination.AsFpuRegister<SRegister>();
5721 int mem = source.IsFpuRegister()
5722 ? destination.GetStackIndex()
5723 : source.GetStackIndex();
5724
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005725 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005726 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005727 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005728 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005729 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5730 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005731 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005732 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005733 }
5734}
5735
5736void ParallelMoveResolverARM::SpillScratch(int reg) {
5737 __ Push(static_cast<Register>(reg));
5738}
5739
5740void ParallelMoveResolverARM::RestoreScratch(int reg) {
5741 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005742}
5743
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005744HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5745 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005746 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005747 case HLoadClass::LoadKind::kInvalid:
5748 LOG(FATAL) << "UNREACHABLE";
5749 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005750 case HLoadClass::LoadKind::kReferrersClass:
5751 break;
5752 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5753 DCHECK(!GetCompilerOptions().GetCompilePic());
5754 break;
5755 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5756 DCHECK(GetCompilerOptions().GetCompilePic());
5757 break;
5758 case HLoadClass::LoadKind::kBootImageAddress:
5759 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005760 case HLoadClass::LoadKind::kBssEntry:
5761 DCHECK(!Runtime::Current()->UseJitCompilation());
5762 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005763 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005764 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005765 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005766 case HLoadClass::LoadKind::kDexCacheViaMethod:
5767 break;
5768 }
5769 return desired_class_load_kind;
5770}
5771
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005772void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005773 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5774 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005775 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005776 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005777 cls,
5778 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005779 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005780 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005781 return;
5782 }
Vladimir Marko41559982017-01-06 14:04:23 +00005783 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005784
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005785 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5786 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005787 ? LocationSummary::kCallOnSlowPath
5788 : LocationSummary::kNoCall;
5789 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005790 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005791 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005792 }
5793
Vladimir Marko41559982017-01-06 14:04:23 +00005794 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005795 locations->SetInAt(0, Location::RequiresRegister());
5796 }
5797 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005798 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5799 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5800 // Rely on the type resolution or initialization and marking to save everything we need.
5801 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
5802 // to the custom calling convention) or by marking, so we request a different temp.
5803 locations->AddTemp(Location::RequiresRegister());
5804 RegisterSet caller_saves = RegisterSet::Empty();
5805 InvokeRuntimeCallingConvention calling_convention;
5806 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5807 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5808 // that the the kPrimNot result register is the same as the first argument register.
5809 locations->SetCustomSlowPathCallerSaves(caller_saves);
5810 } else {
5811 // For non-Baker read barrier we have a temp-clobbering call.
5812 }
5813 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005814}
5815
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005816// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5817// move.
5818void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00005819 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5820 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5821 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005822 return;
5823 }
Vladimir Marko41559982017-01-06 14:04:23 +00005824 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005825
Vladimir Marko41559982017-01-06 14:04:23 +00005826 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005827 Location out_loc = locations->Out();
5828 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005829
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005830 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5831 ? kWithoutReadBarrier
5832 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005833 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005834 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005835 case HLoadClass::LoadKind::kReferrersClass: {
5836 DCHECK(!cls->CanCallRuntime());
5837 DCHECK(!cls->MustGenerateClinitCheck());
5838 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5839 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005840 GenerateGcRootFieldLoad(cls,
5841 out_loc,
5842 current_method,
5843 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005844 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005845 break;
5846 }
5847 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005848 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005849 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005850 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5851 cls->GetTypeIndex()));
5852 break;
5853 }
5854 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005855 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005856 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005857 CodeGeneratorARM::PcRelativePatchInfo* labels =
5858 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5859 __ BindTrackedLabel(&labels->movw_label);
5860 __ movw(out, /* placeholder */ 0u);
5861 __ BindTrackedLabel(&labels->movt_label);
5862 __ movt(out, /* placeholder */ 0u);
5863 __ BindTrackedLabel(&labels->add_pc_label);
5864 __ add(out, out, ShifterOperand(PC));
5865 break;
5866 }
5867 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005868 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005869 uint32_t address = dchecked_integral_cast<uint32_t>(
5870 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
5871 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005872 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5873 break;
5874 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005875 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005876 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
5877 ? locations->GetTemp(0).AsRegister<Register>()
5878 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005879 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00005880 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005881 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00005882 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005883 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00005884 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005885 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00005886 __ add(temp, temp, ShifterOperand(PC));
5887 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005888 generate_null_check = true;
5889 break;
5890 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005891 case HLoadClass::LoadKind::kJitTableAddress: {
5892 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5893 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00005894 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005895 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00005896 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005897 break;
5898 }
Vladimir Marko41559982017-01-06 14:04:23 +00005899 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005900 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00005901 LOG(FATAL) << "UNREACHABLE";
5902 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005903 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005904
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005905 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5906 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005907 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005908 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5909 codegen_->AddSlowPath(slow_path);
5910 if (generate_null_check) {
5911 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5912 }
5913 if (cls->MustGenerateClinitCheck()) {
5914 GenerateClassInitializationCheck(slow_path, out);
5915 } else {
5916 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005917 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005918 }
5919}
5920
5921void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5922 LocationSummary* locations =
5923 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5924 locations->SetInAt(0, Location::RequiresRegister());
5925 if (check->HasUses()) {
5926 locations->SetOut(Location::SameAsFirstInput());
5927 }
5928}
5929
5930void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005931 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005932 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005933 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005934 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005935 GenerateClassInitializationCheck(slow_path,
5936 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005937}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005938
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005939void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005940 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005941 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5942 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5943 __ b(slow_path->GetEntryLabel(), LT);
5944 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5945 // properly. Therefore, we do a memory fence.
5946 __ dmb(ISH);
5947 __ Bind(slow_path->GetExitLabel());
5948}
5949
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005950HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5951 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005952 switch (desired_string_load_kind) {
5953 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5954 DCHECK(!GetCompilerOptions().GetCompilePic());
5955 break;
5956 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5957 DCHECK(GetCompilerOptions().GetCompilePic());
5958 break;
5959 case HLoadString::LoadKind::kBootImageAddress:
5960 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005961 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005962 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005963 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005964 case HLoadString::LoadKind::kJitTableAddress:
5965 DCHECK(Runtime::Current()->UseJitCompilation());
5966 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005967 case HLoadString::LoadKind::kDexCacheViaMethod:
5968 break;
5969 }
5970 return desired_string_load_kind;
5971}
5972
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005973void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005974 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005975 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005976 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005977 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005978 locations->SetOut(Location::RegisterLocation(R0));
5979 } else {
5980 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005981 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5982 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00005983 // Rely on the pResolveString and marking to save everything we need, including temps.
5984 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
5985 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005986 locations->AddTemp(Location::RequiresRegister());
5987 RegisterSet caller_saves = RegisterSet::Empty();
5988 InvokeRuntimeCallingConvention calling_convention;
5989 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5990 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5991 // that the the kPrimNot result register is the same as the first argument register.
5992 locations->SetCustomSlowPathCallerSaves(caller_saves);
5993 } else {
5994 // For non-Baker read barrier we have a temp-clobbering call.
5995 }
5996 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005997 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005998}
5999
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006000// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6001// move.
6002void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006003 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006004 Location out_loc = locations->Out();
6005 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006006 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00006007
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006008 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006009 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006010 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006011 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6012 load->GetStringIndex()));
6013 return; // No dex cache slow path.
6014 }
6015 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006016 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006017 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006018 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006019 __ BindTrackedLabel(&labels->movw_label);
6020 __ movw(out, /* placeholder */ 0u);
6021 __ BindTrackedLabel(&labels->movt_label);
6022 __ movt(out, /* placeholder */ 0u);
6023 __ BindTrackedLabel(&labels->add_pc_label);
6024 __ add(out, out, ShifterOperand(PC));
6025 return; // No dex cache slow path.
6026 }
6027 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006028 uint32_t address = dchecked_integral_cast<uint32_t>(
6029 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6030 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006031 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6032 return; // No dex cache slow path.
6033 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006034 case HLoadString::LoadKind::kBssEntry: {
6035 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006036 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6037 ? locations->GetTemp(0).AsRegister<Register>()
6038 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006039 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006040 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006041 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006042 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006043 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006044 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006045 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006046 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006047 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006048 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
6049 codegen_->AddSlowPath(slow_path);
6050 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6051 __ Bind(slow_path->GetExitLabel());
6052 return;
6053 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006054 case HLoadString::LoadKind::kJitTableAddress: {
6055 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006056 load->GetStringIndex(),
6057 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006058 // /* GcRoot<mirror::String> */ out = *out
6059 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6060 return;
6061 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006062 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006063 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006064 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006065
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006066 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
6067 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6068 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006069 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006070 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006071 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6072 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006073}
6074
David Brazdilcb1c0552015-08-04 16:22:25 +01006075static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006076 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006077}
6078
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006079void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6080 LocationSummary* locations =
6081 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6082 locations->SetOut(Location::RequiresRegister());
6083}
6084
6085void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006086 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006087 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6088}
6089
6090void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6091 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6092}
6093
6094void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006095 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006096 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006097}
6098
6099void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6100 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006101 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006102 InvokeRuntimeCallingConvention calling_convention;
6103 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6104}
6105
6106void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006107 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006108 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006109}
6110
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006111// Temp is used for read barrier.
6112static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6113 if (kEmitCompilerReadBarrier &&
6114 (kUseBakerReadBarrier ||
6115 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6116 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6117 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6118 return 1;
6119 }
6120 return 0;
6121}
6122
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006123// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006124// interface pointer, one for loading the current interface.
6125// The other checks have one temp for loading the object's class.
6126static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6127 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6128 return 3;
6129 }
6130 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006131}
6132
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006133void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006134 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006135 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006136 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006137 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006138 case TypeCheckKind::kExactCheck:
6139 case TypeCheckKind::kAbstractClassCheck:
6140 case TypeCheckKind::kClassHierarchyCheck:
6141 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006142 call_kind =
6143 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006144 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006145 break;
6146 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006147 case TypeCheckKind::kUnresolvedCheck:
6148 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006149 call_kind = LocationSummary::kCallOnSlowPath;
6150 break;
6151 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006152
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006153 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006154 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006155 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006156 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006157 locations->SetInAt(0, Location::RequiresRegister());
6158 locations->SetInAt(1, Location::RequiresRegister());
6159 // The "out" register is used as a temporary, so it overlaps with the inputs.
6160 // Note that TypeCheckSlowPathARM uses this register too.
6161 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006162 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006163}
6164
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006165void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006166 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006167 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006168 Location obj_loc = locations->InAt(0);
6169 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006170 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006171 Location out_loc = locations->Out();
6172 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006173 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6174 DCHECK_LE(num_temps, 1u);
6175 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006176 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006177 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6178 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6179 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006180 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006181 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006182
6183 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006184 // avoid null check if we know obj is not null.
6185 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006186 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006187 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006188
Roland Levillainc9285912015-12-18 10:38:42 +00006189 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006190 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006191 // /* HeapReference<Class> */ out = obj->klass_
6192 GenerateReferenceLoadTwoRegisters(instruction,
6193 out_loc,
6194 obj_loc,
6195 class_offset,
6196 maybe_temp_loc,
6197 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006198 __ cmp(out, ShifterOperand(cls));
6199 // Classes must be equal for the instanceof to succeed.
6200 __ b(&zero, NE);
6201 __ LoadImmediate(out, 1);
6202 __ b(&done);
6203 break;
6204 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006205
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006206 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006207 // /* HeapReference<Class> */ out = obj->klass_
6208 GenerateReferenceLoadTwoRegisters(instruction,
6209 out_loc,
6210 obj_loc,
6211 class_offset,
6212 maybe_temp_loc,
6213 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006214 // If the class is abstract, we eagerly fetch the super class of the
6215 // object to avoid doing a comparison we know will fail.
6216 Label loop;
6217 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006218 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006219 GenerateReferenceLoadOneRegister(instruction,
6220 out_loc,
6221 super_offset,
6222 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006223 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006224 // If `out` is null, we use it for the result, and jump to `done`.
6225 __ CompareAndBranchIfZero(out, &done);
6226 __ cmp(out, ShifterOperand(cls));
6227 __ b(&loop, NE);
6228 __ LoadImmediate(out, 1);
6229 if (zero.IsLinked()) {
6230 __ b(&done);
6231 }
6232 break;
6233 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006234
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006235 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006236 // /* HeapReference<Class> */ out = obj->klass_
6237 GenerateReferenceLoadTwoRegisters(instruction,
6238 out_loc,
6239 obj_loc,
6240 class_offset,
6241 maybe_temp_loc,
6242 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006243 // Walk over the class hierarchy to find a match.
6244 Label loop, success;
6245 __ Bind(&loop);
6246 __ cmp(out, ShifterOperand(cls));
6247 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006248 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006249 GenerateReferenceLoadOneRegister(instruction,
6250 out_loc,
6251 super_offset,
6252 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006253 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006254 __ CompareAndBranchIfNonZero(out, &loop);
6255 // If `out` is null, we use it for the result, and jump to `done`.
6256 __ b(&done);
6257 __ Bind(&success);
6258 __ LoadImmediate(out, 1);
6259 if (zero.IsLinked()) {
6260 __ b(&done);
6261 }
6262 break;
6263 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006264
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006265 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006266 // /* HeapReference<Class> */ out = obj->klass_
6267 GenerateReferenceLoadTwoRegisters(instruction,
6268 out_loc,
6269 obj_loc,
6270 class_offset,
6271 maybe_temp_loc,
6272 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006273 // Do an exact check.
6274 Label exact_check;
6275 __ cmp(out, ShifterOperand(cls));
6276 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006277 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006278 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006279 GenerateReferenceLoadOneRegister(instruction,
6280 out_loc,
6281 component_offset,
6282 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006283 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006284 // If `out` is null, we use it for the result, and jump to `done`.
6285 __ CompareAndBranchIfZero(out, &done);
6286 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6287 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6288 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006289 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006290 __ LoadImmediate(out, 1);
6291 __ b(&done);
6292 break;
6293 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006294
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006295 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006296 // No read barrier since the slow path will retry upon failure.
6297 // /* HeapReference<Class> */ out = obj->klass_
6298 GenerateReferenceLoadTwoRegisters(instruction,
6299 out_loc,
6300 obj_loc,
6301 class_offset,
6302 maybe_temp_loc,
6303 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006304 __ cmp(out, ShifterOperand(cls));
6305 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006306 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6307 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006308 codegen_->AddSlowPath(slow_path);
6309 __ b(slow_path->GetEntryLabel(), NE);
6310 __ LoadImmediate(out, 1);
6311 if (zero.IsLinked()) {
6312 __ b(&done);
6313 }
6314 break;
6315 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006316
Calin Juravle98893e12015-10-02 21:05:03 +01006317 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006318 case TypeCheckKind::kInterfaceCheck: {
6319 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006320 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006321 // cases.
6322 //
6323 // We cannot directly call the InstanceofNonTrivial runtime
6324 // entry point without resorting to a type checking slow path
6325 // here (i.e. by calling InvokeRuntime directly), as it would
6326 // require to assign fixed registers for the inputs of this
6327 // HInstanceOf instruction (following the runtime calling
6328 // convention), which might be cluttered by the potential first
6329 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006330 //
6331 // TODO: Introduce a new runtime entry point taking the object
6332 // to test (instead of its class) as argument, and let it deal
6333 // with the read barrier issues. This will let us refactor this
6334 // case of the `switch` code as it was previously (with a direct
6335 // call to the runtime not using a type checking slow path).
6336 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006337 DCHECK(locations->OnlyCallsOnSlowPath());
6338 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6339 /* is_fatal */ false);
6340 codegen_->AddSlowPath(slow_path);
6341 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006342 if (zero.IsLinked()) {
6343 __ b(&done);
6344 }
6345 break;
6346 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006347 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006348
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006349 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006350 __ Bind(&zero);
6351 __ LoadImmediate(out, 0);
6352 }
6353
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006354 if (done.IsLinked()) {
6355 __ Bind(&done);
6356 }
6357
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006358 if (slow_path != nullptr) {
6359 __ Bind(slow_path->GetExitLabel());
6360 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006361}
6362
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006363void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006364 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6365 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6366
Roland Levillain3b359c72015-11-17 19:35:12 +00006367 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6368 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006369 case TypeCheckKind::kExactCheck:
6370 case TypeCheckKind::kAbstractClassCheck:
6371 case TypeCheckKind::kClassHierarchyCheck:
6372 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006373 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6374 LocationSummary::kCallOnSlowPath :
6375 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006376 break;
6377 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006378 case TypeCheckKind::kUnresolvedCheck:
6379 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006380 call_kind = LocationSummary::kCallOnSlowPath;
6381 break;
6382 }
6383
Roland Levillain3b359c72015-11-17 19:35:12 +00006384 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6385 locations->SetInAt(0, Location::RequiresRegister());
6386 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006387 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006388}
6389
6390void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006391 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006392 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006393 Location obj_loc = locations->InAt(0);
6394 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006395 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006396 Location temp_loc = locations->GetTemp(0);
6397 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006398 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6399 DCHECK_LE(num_temps, 3u);
6400 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6401 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6402 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6403 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6404 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6405 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6406 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6407 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6408 const uint32_t object_array_data_offset =
6409 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006410
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006411 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6412 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6413 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006414 bool is_type_check_slow_path_fatal = false;
6415 if (!kEmitCompilerReadBarrier) {
6416 is_type_check_slow_path_fatal =
6417 (type_check_kind == TypeCheckKind::kExactCheck ||
6418 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6419 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6420 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6421 !instruction->CanThrowIntoCatchBlock();
6422 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01006423 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006424 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6425 is_type_check_slow_path_fatal);
6426 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006427
6428 Label done;
6429 // Avoid null check if we know obj is not null.
6430 if (instruction->MustDoNullCheck()) {
6431 __ CompareAndBranchIfZero(obj, &done);
6432 }
6433
Roland Levillain3b359c72015-11-17 19:35:12 +00006434 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006435 case TypeCheckKind::kExactCheck:
6436 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006437 // /* HeapReference<Class> */ temp = obj->klass_
6438 GenerateReferenceLoadTwoRegisters(instruction,
6439 temp_loc,
6440 obj_loc,
6441 class_offset,
6442 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006443 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006444
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006445 __ cmp(temp, ShifterOperand(cls));
6446 // Jump to slow path for throwing the exception or doing a
6447 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006448 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006449 break;
6450 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006451
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006452 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006453 // /* HeapReference<Class> */ temp = obj->klass_
6454 GenerateReferenceLoadTwoRegisters(instruction,
6455 temp_loc,
6456 obj_loc,
6457 class_offset,
6458 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006459 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006460
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006461 // If the class is abstract, we eagerly fetch the super class of the
6462 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006463 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006464 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006465 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006466 GenerateReferenceLoadOneRegister(instruction,
6467 temp_loc,
6468 super_offset,
6469 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006470 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006471
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006472 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6473 // exception.
6474 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00006475
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006476 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006477 __ cmp(temp, ShifterOperand(cls));
6478 __ b(&loop, NE);
6479 break;
6480 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006481
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006482 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006483 // /* HeapReference<Class> */ temp = obj->klass_
6484 GenerateReferenceLoadTwoRegisters(instruction,
6485 temp_loc,
6486 obj_loc,
6487 class_offset,
6488 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006489 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006490
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006491 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006492 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006493 __ Bind(&loop);
6494 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006495 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006496
Roland Levillain3b359c72015-11-17 19:35:12 +00006497 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006498 GenerateReferenceLoadOneRegister(instruction,
6499 temp_loc,
6500 super_offset,
6501 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006502 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006503
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006504 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6505 // exception.
6506 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6507 // Otherwise, jump to the beginning of the loop.
6508 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006509 break;
6510 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006511
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006512 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006513 // /* HeapReference<Class> */ temp = obj->klass_
6514 GenerateReferenceLoadTwoRegisters(instruction,
6515 temp_loc,
6516 obj_loc,
6517 class_offset,
6518 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006519 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006520
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006521 // Do an exact check.
6522 __ cmp(temp, ShifterOperand(cls));
6523 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006524
6525 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006526 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006527 GenerateReferenceLoadOneRegister(instruction,
6528 temp_loc,
6529 component_offset,
6530 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006531 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006532 // If the component type is null, jump to the slow path to throw the exception.
6533 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6534 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6535 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006536 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006537 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006538 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006539 break;
6540 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006541
Calin Juravle98893e12015-10-02 21:05:03 +01006542 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006543 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00006544 // We cannot directly call the CheckCast runtime entry point
6545 // without resorting to a type checking slow path here (i.e. by
6546 // calling InvokeRuntime directly), as it would require to
6547 // assign fixed registers for the inputs of this HInstanceOf
6548 // instruction (following the runtime calling convention), which
6549 // might be cluttered by the potential first read barrier
6550 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006551
Roland Levillain3b359c72015-11-17 19:35:12 +00006552 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006553 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006554
6555 case TypeCheckKind::kInterfaceCheck: {
6556 // Avoid read barriers to improve performance of the fast path. We can not get false
6557 // positives by doing this.
6558 // /* HeapReference<Class> */ temp = obj->klass_
6559 GenerateReferenceLoadTwoRegisters(instruction,
6560 temp_loc,
6561 obj_loc,
6562 class_offset,
6563 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006564 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006565
6566 // /* HeapReference<Class> */ temp = temp->iftable_
6567 GenerateReferenceLoadTwoRegisters(instruction,
6568 temp_loc,
6569 temp_loc,
6570 iftable_offset,
6571 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006572 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006573 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006574 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006575 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006576 Label start_loop;
6577 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006578 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
6579 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006580 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
6581 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006582 // Go to next interface.
6583 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
6584 __ sub(maybe_temp2_loc.AsRegister<Register>(),
6585 maybe_temp2_loc.AsRegister<Register>(),
6586 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006587 // Compare the classes and continue the loop if they do not match.
6588 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
6589 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006590 break;
6591 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006592 }
6593 __ Bind(&done);
6594
Roland Levillain3b359c72015-11-17 19:35:12 +00006595 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006596}
6597
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006598void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6599 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006600 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006601 InvokeRuntimeCallingConvention calling_convention;
6602 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6603}
6604
6605void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006606 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6607 instruction,
6608 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006609 if (instruction->IsEnter()) {
6610 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6611 } else {
6612 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6613 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006614}
6615
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006616void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6617void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6618void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006619
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006620void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006621 LocationSummary* locations =
6622 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6623 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6624 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006625 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006626 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006627 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006629}
6630
6631void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6632 HandleBitwiseOperation(instruction);
6633}
6634
6635void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6636 HandleBitwiseOperation(instruction);
6637}
6638
6639void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6640 HandleBitwiseOperation(instruction);
6641}
6642
Artem Serov7fc63502016-02-09 17:15:29 +00006643
6644void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6645 LocationSummary* locations =
6646 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6647 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6648 || instruction->GetResultType() == Primitive::kPrimLong);
6649
6650 locations->SetInAt(0, Location::RequiresRegister());
6651 locations->SetInAt(1, Location::RequiresRegister());
6652 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6653}
6654
6655void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6656 LocationSummary* locations = instruction->GetLocations();
6657 Location first = locations->InAt(0);
6658 Location second = locations->InAt(1);
6659 Location out = locations->Out();
6660
6661 if (instruction->GetResultType() == Primitive::kPrimInt) {
6662 Register first_reg = first.AsRegister<Register>();
6663 ShifterOperand second_reg(second.AsRegister<Register>());
6664 Register out_reg = out.AsRegister<Register>();
6665
6666 switch (instruction->GetOpKind()) {
6667 case HInstruction::kAnd:
6668 __ bic(out_reg, first_reg, second_reg);
6669 break;
6670 case HInstruction::kOr:
6671 __ orn(out_reg, first_reg, second_reg);
6672 break;
6673 // There is no EON on arm.
6674 case HInstruction::kXor:
6675 default:
6676 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6677 UNREACHABLE();
6678 }
6679 return;
6680
6681 } else {
6682 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6683 Register first_low = first.AsRegisterPairLow<Register>();
6684 Register first_high = first.AsRegisterPairHigh<Register>();
6685 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6686 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6687 Register out_low = out.AsRegisterPairLow<Register>();
6688 Register out_high = out.AsRegisterPairHigh<Register>();
6689
6690 switch (instruction->GetOpKind()) {
6691 case HInstruction::kAnd:
6692 __ bic(out_low, first_low, second_low);
6693 __ bic(out_high, first_high, second_high);
6694 break;
6695 case HInstruction::kOr:
6696 __ orn(out_low, first_low, second_low);
6697 __ orn(out_high, first_high, second_high);
6698 break;
6699 // There is no EON on arm.
6700 case HInstruction::kXor:
6701 default:
6702 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6703 UNREACHABLE();
6704 }
6705 }
6706}
6707
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006708void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6709 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6710 if (value == 0xffffffffu) {
6711 if (out != first) {
6712 __ mov(out, ShifterOperand(first));
6713 }
6714 return;
6715 }
6716 if (value == 0u) {
6717 __ mov(out, ShifterOperand(0));
6718 return;
6719 }
6720 ShifterOperand so;
6721 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6722 __ and_(out, first, so);
6723 } else {
6724 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6725 __ bic(out, first, ShifterOperand(~value));
6726 }
6727}
6728
6729void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6730 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6731 if (value == 0u) {
6732 if (out != first) {
6733 __ mov(out, ShifterOperand(first));
6734 }
6735 return;
6736 }
6737 if (value == 0xffffffffu) {
6738 __ mvn(out, ShifterOperand(0));
6739 return;
6740 }
6741 ShifterOperand so;
6742 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6743 __ orr(out, first, so);
6744 } else {
6745 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6746 __ orn(out, first, ShifterOperand(~value));
6747 }
6748}
6749
6750void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6751 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6752 if (value == 0u) {
6753 if (out != first) {
6754 __ mov(out, ShifterOperand(first));
6755 }
6756 return;
6757 }
6758 __ eor(out, first, ShifterOperand(value));
6759}
6760
Vladimir Marko59751a72016-08-05 14:37:27 +01006761void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6762 Location first,
6763 uint64_t value) {
6764 Register out_low = out.AsRegisterPairLow<Register>();
6765 Register out_high = out.AsRegisterPairHigh<Register>();
6766 Register first_low = first.AsRegisterPairLow<Register>();
6767 Register first_high = first.AsRegisterPairHigh<Register>();
6768 uint32_t value_low = Low32Bits(value);
6769 uint32_t value_high = High32Bits(value);
6770 if (value_low == 0u) {
6771 if (out_low != first_low) {
6772 __ mov(out_low, ShifterOperand(first_low));
6773 }
6774 __ AddConstant(out_high, first_high, value_high);
6775 return;
6776 }
6777 __ AddConstantSetFlags(out_low, first_low, value_low);
6778 ShifterOperand so;
6779 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6780 __ adc(out_high, first_high, so);
6781 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6782 __ sbc(out_high, first_high, so);
6783 } else {
6784 LOG(FATAL) << "Unexpected constant " << value_high;
6785 UNREACHABLE();
6786 }
6787}
6788
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006789void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6790 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006791 Location first = locations->InAt(0);
6792 Location second = locations->InAt(1);
6793 Location out = locations->Out();
6794
6795 if (second.IsConstant()) {
6796 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6797 uint32_t value_low = Low32Bits(value);
6798 if (instruction->GetResultType() == Primitive::kPrimInt) {
6799 Register first_reg = first.AsRegister<Register>();
6800 Register out_reg = out.AsRegister<Register>();
6801 if (instruction->IsAnd()) {
6802 GenerateAndConst(out_reg, first_reg, value_low);
6803 } else if (instruction->IsOr()) {
6804 GenerateOrrConst(out_reg, first_reg, value_low);
6805 } else {
6806 DCHECK(instruction->IsXor());
6807 GenerateEorConst(out_reg, first_reg, value_low);
6808 }
6809 } else {
6810 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6811 uint32_t value_high = High32Bits(value);
6812 Register first_low = first.AsRegisterPairLow<Register>();
6813 Register first_high = first.AsRegisterPairHigh<Register>();
6814 Register out_low = out.AsRegisterPairLow<Register>();
6815 Register out_high = out.AsRegisterPairHigh<Register>();
6816 if (instruction->IsAnd()) {
6817 GenerateAndConst(out_low, first_low, value_low);
6818 GenerateAndConst(out_high, first_high, value_high);
6819 } else if (instruction->IsOr()) {
6820 GenerateOrrConst(out_low, first_low, value_low);
6821 GenerateOrrConst(out_high, first_high, value_high);
6822 } else {
6823 DCHECK(instruction->IsXor());
6824 GenerateEorConst(out_low, first_low, value_low);
6825 GenerateEorConst(out_high, first_high, value_high);
6826 }
6827 }
6828 return;
6829 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006830
6831 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006832 Register first_reg = first.AsRegister<Register>();
6833 ShifterOperand second_reg(second.AsRegister<Register>());
6834 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006835 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006836 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006837 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006838 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006839 } else {
6840 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006841 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006842 }
6843 } else {
6844 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006845 Register first_low = first.AsRegisterPairLow<Register>();
6846 Register first_high = first.AsRegisterPairHigh<Register>();
6847 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6848 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6849 Register out_low = out.AsRegisterPairLow<Register>();
6850 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006851 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006852 __ and_(out_low, first_low, second_low);
6853 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006854 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006855 __ orr(out_low, first_low, second_low);
6856 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006857 } else {
6858 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006859 __ eor(out_low, first_low, second_low);
6860 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006861 }
6862 }
6863}
6864
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006865void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
6866 HInstruction* instruction,
6867 Location out,
6868 uint32_t offset,
6869 Location maybe_temp,
6870 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006871 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006872 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006873 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006874 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006875 if (kUseBakerReadBarrier) {
6876 // Load with fast path based Baker's read barrier.
6877 // /* HeapReference<Object> */ out = *(out + offset)
6878 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006879 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006880 } else {
6881 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006882 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006883 // in the following move operation, as we will need it for the
6884 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006885 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006886 // /* HeapReference<Object> */ out = *(out + offset)
6887 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006888 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006889 }
6890 } else {
6891 // Plain load with no read barrier.
6892 // /* HeapReference<Object> */ out = *(out + offset)
6893 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6894 __ MaybeUnpoisonHeapReference(out_reg);
6895 }
6896}
6897
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006898void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
6899 HInstruction* instruction,
6900 Location out,
6901 Location obj,
6902 uint32_t offset,
6903 Location maybe_temp,
6904 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006905 Register out_reg = out.AsRegister<Register>();
6906 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006907 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006908 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006909 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006910 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006911 // Load with fast path based Baker's read barrier.
6912 // /* HeapReference<Object> */ out = *(obj + offset)
6913 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006914 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006915 } else {
6916 // Load with slow path based read barrier.
6917 // /* HeapReference<Object> */ out = *(obj + offset)
6918 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6919 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6920 }
6921 } else {
6922 // Plain load with no read barrier.
6923 // /* HeapReference<Object> */ out = *(obj + offset)
6924 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6925 __ MaybeUnpoisonHeapReference(out_reg);
6926 }
6927}
6928
6929void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6930 Location root,
6931 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006932 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006933 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006934 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006935 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006936 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006937 if (kUseBakerReadBarrier) {
6938 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6939 // Baker's read barrier are used:
6940 //
6941 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006942 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6943 // if (temp != null) {
6944 // root = temp(root)
Roland Levillainc9285912015-12-18 10:38:42 +00006945 // }
6946
6947 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6948 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6949 static_assert(
6950 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6951 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6952 "have different sizes.");
6953 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6954 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6955 "have different sizes.");
6956
Vladimir Marko953437b2016-08-24 08:30:46 +00006957 // Slow path marking the GC root `root`.
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006958 Location temp = Location::RegisterLocation(LR);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006959 SlowPathCodeARM* slow_path =
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006960 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
6961 instruction,
6962 root,
6963 /*entrypoint*/ temp);
Roland Levillainc9285912015-12-18 10:38:42 +00006964 codegen_->AddSlowPath(slow_path);
6965
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006966 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6967 const int32_t entry_point_offset =
6968 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
6969 // Loading the entrypoint does not require a load acquire since it is only changed when
6970 // threads are suspended or running a checkpoint.
6971 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6972 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6973 // checking GetIsGcMarking.
6974 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00006975 __ Bind(slow_path->GetExitLabel());
6976 } else {
6977 // GC root loaded through a slow path for read barriers other
6978 // than Baker's.
6979 // /* GcRoot<mirror::Object>* */ root = obj + offset
6980 __ AddConstant(root_reg, obj, offset);
6981 // /* mirror::Object* */ root = root->Read()
6982 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6983 }
6984 } else {
6985 // Plain GC root load with no read barrier.
6986 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6987 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6988 // Note that GC roots are not affected by heap poisoning, thus we
6989 // do not have to unpoison `root_reg` here.
6990 }
6991}
6992
6993void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6994 Location ref,
6995 Register obj,
6996 uint32_t offset,
6997 Location temp,
6998 bool needs_null_check) {
6999 DCHECK(kEmitCompilerReadBarrier);
7000 DCHECK(kUseBakerReadBarrier);
7001
7002 // /* HeapReference<Object> */ ref = *(obj + offset)
7003 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01007004 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00007005 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007006 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007007}
7008
7009void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7010 Location ref,
7011 Register obj,
7012 uint32_t data_offset,
7013 Location index,
7014 Location temp,
7015 bool needs_null_check) {
7016 DCHECK(kEmitCompilerReadBarrier);
7017 DCHECK(kUseBakerReadBarrier);
7018
Roland Levillainbfea3352016-06-23 13:48:47 +01007019 static_assert(
7020 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7021 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007022 // /* HeapReference<Object> */ ref =
7023 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01007024 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00007025 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007026 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007027}
7028
7029void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7030 Location ref,
7031 Register obj,
7032 uint32_t offset,
7033 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01007034 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00007035 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007036 bool needs_null_check,
7037 bool always_update_field,
7038 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00007039 DCHECK(kEmitCompilerReadBarrier);
7040 DCHECK(kUseBakerReadBarrier);
7041
7042 // In slow path based read barriers, the read barrier call is
7043 // inserted after the original load. However, in fast path based
7044 // Baker's read barriers, we need to perform the load of
7045 // mirror::Object::monitor_ *before* the original reference load.
7046 // This load-load ordering is required by the read barrier.
7047 // The fast path/slow path (for Baker's algorithm) should look like:
7048 //
7049 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7050 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7051 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007052 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillainc9285912015-12-18 10:38:42 +00007053 // if (is_gray) {
7054 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7055 // }
7056 //
7057 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007058 // slightly more complex as it performs additional checks that we do
7059 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00007060
7061 Register ref_reg = ref.AsRegister<Register>();
7062 Register temp_reg = temp.AsRegister<Register>();
7063 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7064
7065 // /* int32_t */ monitor = obj->monitor_
7066 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7067 if (needs_null_check) {
7068 MaybeRecordImplicitNullCheck(instruction);
7069 }
7070 // /* LockWord */ lock_word = LockWord(monitor)
7071 static_assert(sizeof(LockWord) == sizeof(int32_t),
7072 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007073
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007074 // Introduce a dependency on the lock_word including the rb_state,
7075 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00007076 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01007077 // `obj` is unchanged by this operation, but its value now depends
7078 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007079 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00007080
7081 // The actual reference load.
7082 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007083 // Load types involving an "index": ArrayGet,
7084 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7085 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01007086 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00007087 if (index.IsConstant()) {
7088 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01007089 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00007090 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7091 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01007092 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007093 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7094 // intrinsics, which use a register pair as index ("long
7095 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01007096 Register index_reg = index.IsRegisterPair()
7097 ? index.AsRegisterPairLow<Register>()
7098 : index.AsRegister<Register>();
7099 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00007100 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
7101 }
7102 } else {
7103 // /* HeapReference<Object> */ ref = *(obj + offset)
7104 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7105 }
7106
7107 // Object* ref = ref_addr->AsMirrorPtr()
7108 __ MaybeUnpoisonHeapReference(ref_reg);
7109
Vladimir Marko953437b2016-08-24 08:30:46 +00007110 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007111 SlowPathCodeARM* slow_path;
7112 if (always_update_field) {
7113 DCHECK(temp2 != nullptr);
7114 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
7115 // of the form `obj + field_offset`, where `obj` is a register and
7116 // `field_offset` is a register pair (of which only the lower half
7117 // is used). Thus `offset` and `scale_factor` above are expected
7118 // to be null in this code path.
7119 DCHECK_EQ(offset, 0u);
7120 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7121 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
7122 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7123 } else {
7124 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
7125 }
Roland Levillainc9285912015-12-18 10:38:42 +00007126 AddSlowPath(slow_path);
7127
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007128 // if (rb_state == ReadBarrier::GrayState())
Roland Levillainc9285912015-12-18 10:38:42 +00007129 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007130 // Given the numeric representation, it's enough to check the low bit of the
7131 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7132 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007133 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7134 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007135 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7136 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00007137 __ Bind(slow_path->GetExitLabel());
7138}
7139
7140void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
7141 Location out,
7142 Location ref,
7143 Location obj,
7144 uint32_t offset,
7145 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007146 DCHECK(kEmitCompilerReadBarrier);
7147
Roland Levillainc9285912015-12-18 10:38:42 +00007148 // Insert a slow path based read barrier *after* the reference load.
7149 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007150 // If heap poisoning is enabled, the unpoisoning of the loaded
7151 // reference will be carried out by the runtime within the slow
7152 // path.
7153 //
7154 // Note that `ref` currently does not get unpoisoned (when heap
7155 // poisoning is enabled), which is alright as the `ref` argument is
7156 // not used by the artReadBarrierSlow entry point.
7157 //
7158 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007159 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00007160 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
7161 AddSlowPath(slow_path);
7162
Roland Levillain3b359c72015-11-17 19:35:12 +00007163 __ b(slow_path->GetEntryLabel());
7164 __ Bind(slow_path->GetExitLabel());
7165}
7166
Roland Levillainc9285912015-12-18 10:38:42 +00007167void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7168 Location out,
7169 Location ref,
7170 Location obj,
7171 uint32_t offset,
7172 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007173 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00007174 // Baker's read barriers shall be handled by the fast path
7175 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
7176 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007177 // If heap poisoning is enabled, unpoisoning will be taken care of
7178 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007179 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00007180 } else if (kPoisonHeapReferences) {
7181 __ UnpoisonHeapReference(out.AsRegister<Register>());
7182 }
7183}
7184
Roland Levillainc9285912015-12-18 10:38:42 +00007185void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7186 Location out,
7187 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007188 DCHECK(kEmitCompilerReadBarrier);
7189
Roland Levillainc9285912015-12-18 10:38:42 +00007190 // Insert a slow path based read barrier *after* the GC root load.
7191 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007192 // Note that GC roots are not affected by heap poisoning, so we do
7193 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007194 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007195 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
7196 AddSlowPath(slow_path);
7197
Roland Levillain3b359c72015-11-17 19:35:12 +00007198 __ b(slow_path->GetEntryLabel());
7199 __ Bind(slow_path->GetExitLabel());
7200}
7201
Vladimir Markodc151b22015-10-15 18:02:30 +01007202HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7203 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007204 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00007205 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007206}
7207
Vladimir Markob4536b72015-11-24 13:45:23 +00007208Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7209 Register temp) {
7210 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7211 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7212 if (!invoke->GetLocations()->Intrinsified()) {
7213 return location.AsRegister<Register>();
7214 }
7215 // For intrinsics we allow any location, so it may be on the stack.
7216 if (!location.IsRegister()) {
7217 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7218 return temp;
7219 }
7220 // For register locations, check if the register was saved. If so, get it from the stack.
7221 // Note: There is a chance that the register was saved but not overwritten, so we could
7222 // save one load. However, since this is just an intrinsic slow path we prefer this
7223 // simple and more robust approach rather that trying to determine if that's the case.
7224 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08007225 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00007226 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7227 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7228 return temp;
7229 }
7230 return location.AsRegister<Register>();
7231}
7232
TatWai Chongd8c052a2016-11-02 16:12:48 +08007233Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
7234 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007235 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7236 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007237 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7238 uint32_t offset =
7239 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007240 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007241 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007242 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007243 }
Vladimir Marko58155012015-08-19 12:49:41 +00007244 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007245 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007246 break;
7247 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7248 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7249 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007250 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7251 HArmDexCacheArraysBase* base =
7252 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7253 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7254 temp.AsRegister<Register>());
7255 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7256 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7257 break;
7258 }
Vladimir Marko58155012015-08-19 12:49:41 +00007259 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007260 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007261 Register method_reg;
7262 Register reg = temp.AsRegister<Register>();
7263 if (current_method.IsRegister()) {
7264 method_reg = current_method.AsRegister<Register>();
7265 } else {
7266 DCHECK(invoke->GetLocations()->Intrinsified());
7267 DCHECK(!current_method.IsValid());
7268 method_reg = reg;
7269 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7270 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007271 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7272 __ LoadFromOffset(kLoadWord,
7273 reg,
7274 method_reg,
7275 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007276 // temp = temp[index_in_cache];
7277 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7278 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007279 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7280 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007281 }
Vladimir Marko58155012015-08-19 12:49:41 +00007282 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08007283 return callee_method;
7284}
7285
7286void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
7287 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00007288
7289 switch (invoke->GetCodePtrLocation()) {
7290 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7291 __ bl(GetFrameEntryLabel());
7292 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007293 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7294 // LR = callee_method->entry_point_from_quick_compiled_code_
7295 __ LoadFromOffset(
7296 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007297 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007298 // LR()
7299 __ blx(LR);
7300 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007301 }
7302
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007303 DCHECK(!IsLeafMethod());
7304}
7305
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007306void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7307 Register temp = temp_location.AsRegister<Register>();
7308 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7309 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007310
7311 // Use the calling convention instead of the location of the receiver, as
7312 // intrinsics may have put the receiver in a different register. In the intrinsics
7313 // slow path, the arguments have been moved to the right place, so here we are
7314 // guaranteed that the receiver is the first register of the calling convention.
7315 InvokeDexCallingConvention calling_convention;
7316 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007317 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007318 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007319 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007320 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007321 // Instead of simply (possibly) unpoisoning `temp` here, we should
7322 // emit a read barrier for the previous class reference load.
7323 // However this is not required in practice, as this is an
7324 // intermediate/temporary reference and because the current
7325 // concurrent copying collector keeps the from-space memory
7326 // intact/accessible until the end of the marking phase (the
7327 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007328 __ MaybeUnpoisonHeapReference(temp);
7329 // temp = temp->GetMethodAt(method_offset);
7330 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007331 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007332 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7333 // LR = temp->GetEntryPoint();
7334 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7335 // LR();
7336 __ blx(LR);
7337}
7338
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007339CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007340 const DexFile& dex_file, dex::StringIndex string_index) {
7341 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007342}
7343
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007344CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08007345 const DexFile& dex_file, dex::TypeIndex type_index) {
7346 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007347}
7348
Vladimir Marko1998cd02017-01-13 13:02:58 +00007349CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
7350 const DexFile& dex_file, dex::TypeIndex type_index) {
7351 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
7352}
7353
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007354CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7355 const DexFile& dex_file, uint32_t element_offset) {
7356 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7357}
7358
7359CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7360 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7361 patches->emplace_back(dex_file, offset_or_index);
7362 return &patches->back();
7363}
7364
7365Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007366 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007367 return boot_image_string_patches_.GetOrCreate(
7368 StringReference(&dex_file, string_index),
7369 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7370}
7371
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007372Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007373 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007374 return boot_image_type_patches_.GetOrCreate(
7375 TypeReference(&dex_file, type_index),
7376 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7377}
7378
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007379Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7380 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7381 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7382 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7383}
7384
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007385Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007386 dex::StringIndex string_index,
7387 Handle<mirror::String> handle) {
7388 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7389 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007390 return jit_string_patches_.GetOrCreate(
7391 StringReference(&dex_file, string_index),
7392 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7393}
7394
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007395Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
7396 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007397 Handle<mirror::Class> handle) {
7398 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
7399 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007400 return jit_class_patches_.GetOrCreate(
7401 TypeReference(&dex_file, type_index),
7402 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7403}
7404
Vladimir Markoaad75c62016-10-03 08:46:48 +00007405template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7406inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7407 const ArenaDeque<PcRelativePatchInfo>& infos,
7408 ArenaVector<LinkerPatch>* linker_patches) {
7409 for (const PcRelativePatchInfo& info : infos) {
7410 const DexFile& dex_file = info.target_dex_file;
7411 size_t offset_or_index = info.offset_or_index;
7412 DCHECK(info.add_pc_label.IsBound());
7413 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7414 // Add MOVW patch.
7415 DCHECK(info.movw_label.IsBound());
7416 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7417 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7418 // Add MOVT patch.
7419 DCHECK(info.movt_label.IsBound());
7420 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7421 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7422 }
7423}
7424
Vladimir Marko58155012015-08-19 12:49:41 +00007425void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7426 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007427 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00007428 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007429 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007430 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007431 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007432 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00007433 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007434 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007435 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007436 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7437 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007438 for (const auto& entry : boot_image_string_patches_) {
7439 const StringReference& target_string = entry.first;
7440 Literal* literal = entry.second;
7441 DCHECK(literal->GetLabel()->IsBound());
7442 uint32_t literal_offset = literal->GetLabel()->Position();
7443 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7444 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007445 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007446 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007447 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00007448 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007449 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7450 linker_patches);
7451 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007452 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7453 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007454 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7455 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007456 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007457 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
7458 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007459 for (const auto& entry : boot_image_type_patches_) {
7460 const TypeReference& target_type = entry.first;
7461 Literal* literal = entry.second;
7462 DCHECK(literal->GetLabel()->IsBound());
7463 uint32_t literal_offset = literal->GetLabel()->Position();
7464 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7465 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007466 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007467 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007468 for (const auto& entry : boot_image_address_patches_) {
7469 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7470 Literal* literal = entry.second;
7471 DCHECK(literal->GetLabel()->IsBound());
7472 uint32_t literal_offset = literal->GetLabel()->Position();
7473 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7474 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007475 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007476}
7477
7478Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7479 return map->GetOrCreate(
7480 value,
7481 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007482}
7483
7484Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7485 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007486 return map->GetOrCreate(
7487 target_method,
7488 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007489}
7490
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007491void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7492 LocationSummary* locations =
7493 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7494 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7495 Location::RequiresRegister());
7496 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7497 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7498 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7499}
7500
7501void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7502 LocationSummary* locations = instr->GetLocations();
7503 Register res = locations->Out().AsRegister<Register>();
7504 Register accumulator =
7505 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7506 Register mul_left =
7507 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7508 Register mul_right =
7509 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7510
7511 if (instr->GetOpKind() == HInstruction::kAdd) {
7512 __ mla(res, mul_left, mul_right, accumulator);
7513 } else {
7514 __ mls(res, mul_left, mul_right, accumulator);
7515 }
7516}
7517
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007518void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007519 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007520 LOG(FATAL) << "Unreachable";
7521}
7522
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007523void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007524 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007525 LOG(FATAL) << "Unreachable";
7526}
7527
Mark Mendellfe57faa2015-09-18 09:26:15 -04007528// Simple implementation of packed switch - generate cascaded compare/jumps.
7529void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7530 LocationSummary* locations =
7531 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7532 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007533 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007534 codegen_->GetAssembler()->IsThumb()) {
7535 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7536 if (switch_instr->GetStartValue() != 0) {
7537 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7538 }
7539 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007540}
7541
7542void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7543 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007544 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007545 LocationSummary* locations = switch_instr->GetLocations();
7546 Register value_reg = locations->InAt(0).AsRegister<Register>();
7547 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7548
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007549 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007550 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007551 Register temp_reg = IP;
7552 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7553 // the immediate, because IP is used as the destination register. For the other
7554 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7555 // and they can be encoded in the instruction without making use of IP register.
7556 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7557
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007558 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007559 // Jump to successors[0] if value == lower_bound.
7560 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7561 int32_t last_index = 0;
7562 for (; num_entries - last_index > 2; last_index += 2) {
7563 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7564 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7565 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7566 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7567 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7568 }
7569 if (num_entries - last_index == 2) {
7570 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007571 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007572 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007573 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007574
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007575 // And the default for any other value.
7576 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7577 __ b(codegen_->GetLabelOf(default_block));
7578 }
7579 } else {
7580 // Create a table lookup.
7581 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7582
7583 // Materialize a pointer to the switch table
7584 std::vector<Label*> labels(num_entries);
7585 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7586 for (uint32_t i = 0; i < num_entries; i++) {
7587 labels[i] = codegen_->GetLabelOf(successors[i]);
7588 }
7589 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7590
7591 // Remove the bias.
7592 Register key_reg;
7593 if (lower_bound != 0) {
7594 key_reg = locations->GetTemp(1).AsRegister<Register>();
7595 __ AddConstant(key_reg, value_reg, -lower_bound);
7596 } else {
7597 key_reg = value_reg;
7598 }
7599
7600 // Check whether the value is in the table, jump to default block if not.
7601 __ CmpConstant(key_reg, num_entries - 1);
7602 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7603
7604 // Load the displacement from the table.
7605 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7606
7607 // Dispatch is a direct add to the PC (for Thumb2).
7608 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007609 }
7610}
7611
Vladimir Markob4536b72015-11-24 13:45:23 +00007612void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7613 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7614 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007615}
7616
7617void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7618 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007619 CodeGeneratorARM::PcRelativePatchInfo* labels =
7620 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007621 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007622 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007623 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007624 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007625 __ BindTrackedLabel(&labels->add_pc_label);
7626 __ add(base_reg, base_reg, ShifterOperand(PC));
7627}
7628
Andreas Gampe85b62f22015-09-09 13:15:38 -07007629void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7630 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007631 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007632 return;
7633 }
7634
7635 DCHECK_NE(type, Primitive::kPrimVoid);
7636
7637 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7638 if (return_loc.Equals(trg)) {
7639 return;
7640 }
7641
7642 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7643 // with the last branch.
7644 if (type == Primitive::kPrimLong) {
7645 HParallelMove parallel_move(GetGraph()->GetArena());
7646 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7647 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7648 GetMoveResolver()->EmitNativeCode(&parallel_move);
7649 } else if (type == Primitive::kPrimDouble) {
7650 HParallelMove parallel_move(GetGraph()->GetArena());
7651 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7652 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7653 GetMoveResolver()->EmitNativeCode(&parallel_move);
7654 } else {
7655 // Let the parallel move resolver take care of all of this.
7656 HParallelMove parallel_move(GetGraph()->GetArena());
7657 parallel_move.AddMove(return_loc, trg, type, nullptr);
7658 GetMoveResolver()->EmitNativeCode(&parallel_move);
7659 }
7660}
7661
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007662void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7663 LocationSummary* locations =
7664 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7665 locations->SetInAt(0, Location::RequiresRegister());
7666 locations->SetOut(Location::RequiresRegister());
7667}
7668
7669void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7670 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007671 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007672 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007673 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007674 __ LoadFromOffset(kLoadWord,
7675 locations->Out().AsRegister<Register>(),
7676 locations->InAt(0).AsRegister<Register>(),
7677 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007678 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007679 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007680 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007681 __ LoadFromOffset(kLoadWord,
7682 locations->Out().AsRegister<Register>(),
7683 locations->InAt(0).AsRegister<Register>(),
7684 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7685 __ LoadFromOffset(kLoadWord,
7686 locations->Out().AsRegister<Register>(),
7687 locations->Out().AsRegister<Register>(),
7688 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007689 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007690}
7691
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007692static void PatchJitRootUse(uint8_t* code,
7693 const uint8_t* roots_data,
7694 Literal* literal,
7695 uint64_t index_in_table) {
7696 DCHECK(literal->GetLabel()->IsBound());
7697 uint32_t literal_offset = literal->GetLabel()->Position();
7698 uintptr_t address =
7699 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7700 uint8_t* data = code + literal_offset;
7701 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7702}
7703
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007704void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7705 for (const auto& entry : jit_string_patches_) {
7706 const auto& it = jit_string_roots_.find(entry.first);
7707 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007708 PatchJitRootUse(code, roots_data, entry.second, it->second);
7709 }
7710 for (const auto& entry : jit_class_patches_) {
7711 const auto& it = jit_class_roots_.find(entry.first);
7712 DCHECK(it != jit_class_roots_.end());
7713 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007714 }
7715}
7716
Roland Levillain4d027112015-07-01 15:41:14 +01007717#undef __
7718#undef QUICK_ENTRY_POINT
7719
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007720} // namespace arm
7721} // namespace art