blob: 80ebaa4f5912537a05d311538a71e1cfe1b65d3c [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
David Brazdil58282f42016-01-14 12:45:10 +000050static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000051static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070052 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr SRegister kFpuCalleeSaves[] =
54 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000056// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
Vladimir Markof3e0ee22015-12-17 15:23:13 +000060static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070061
Roland Levillain7cbd27f2016-08-11 23:53:33 +010062// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
63#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070064#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Artem Serovf4d6aee2016-07-11 10:41:45 +010066static constexpr int kRegListThreshold = 4;
67
Artem Serovd300d8f2016-07-15 14:00:56 +010068// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
69// for each live D registers they treat two corresponding S registers as live ones.
70//
71// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
72// from a list of contiguous S registers a list of contiguous D registers (processing first/last
73// S registers corner cases) and save/restore this new list treating them as D registers.
74// - decreasing code size
75// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
76// restored and then used in regular non SlowPath code as D register.
77//
78// For the following example (v means the S register is live):
79// D names: | D0 | D1 | D2 | D4 | ...
80// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
81// Live? | | v | v | v | v | v | v | | ...
82//
83// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
84// as D registers.
85static size_t SaveContiguousSRegisterList(size_t first,
86 size_t last,
87 CodeGenerator* codegen,
88 size_t stack_offset) {
89 DCHECK_LE(first, last);
90 if ((first == last) && (first == 0)) {
91 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first);
92 return stack_offset;
93 }
94 if (first % 2 == 1) {
95 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first++);
96 }
97
98 bool save_last = false;
99 if (last % 2 == 0) {
100 save_last = true;
101 --last;
102 }
103
104 if (first < last) {
105 DRegister d_reg = static_cast<DRegister>(first / 2);
106 DCHECK_EQ((last - first + 1) % 2, 0u);
107 size_t number_of_d_regs = (last - first + 1) / 2;
108
109 if (number_of_d_regs == 1) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100110 __ StoreDToOffset(d_reg, SP, stack_offset);
Artem Serovd300d8f2016-07-15 14:00:56 +0100111 } else if (number_of_d_regs > 1) {
112 __ add(IP, SP, ShifterOperand(stack_offset));
113 __ vstmiad(IP, d_reg, number_of_d_regs);
114 }
115 stack_offset += number_of_d_regs * kArmWordSize * 2;
116 }
117
118 if (save_last) {
119 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, last + 1);
120 }
121
122 return stack_offset;
123}
124
125static size_t RestoreContiguousSRegisterList(size_t first,
126 size_t last,
127 CodeGenerator* codegen,
128 size_t stack_offset) {
129 DCHECK_LE(first, last);
130 if ((first == last) && (first == 0)) {
131 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first);
132 return stack_offset;
133 }
134 if (first % 2 == 1) {
135 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first++);
136 }
137
138 bool restore_last = false;
139 if (last % 2 == 0) {
140 restore_last = true;
141 --last;
142 }
143
144 if (first < last) {
145 DRegister d_reg = static_cast<DRegister>(first / 2);
146 DCHECK_EQ((last - first + 1) % 2, 0u);
147 size_t number_of_d_regs = (last - first + 1) / 2;
148 if (number_of_d_regs == 1) {
149 __ LoadDFromOffset(d_reg, SP, stack_offset);
150 } else if (number_of_d_regs > 1) {
151 __ add(IP, SP, ShifterOperand(stack_offset));
152 __ vldmiad(IP, d_reg, number_of_d_regs);
153 }
154 stack_offset += number_of_d_regs * kArmWordSize * 2;
155 }
156
157 if (restore_last) {
158 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, last + 1);
159 }
160
161 return stack_offset;
162}
163
Artem Serovf4d6aee2016-07-11 10:41:45 +0100164void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
165 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
166 size_t orig_offset = stack_offset;
167
168 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
169 for (uint32_t i : LowToHighBits(core_spills)) {
170 // If the register holds an object, update the stack mask.
171 if (locations->RegisterContainsObject(i)) {
172 locations->SetStackBit(stack_offset / kVRegSize);
173 }
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_core_stack_offsets_[i] = stack_offset;
177 stack_offset += kArmWordSize;
178 }
179
180 int reg_num = POPCOUNT(core_spills);
181 if (reg_num != 0) {
182 if (reg_num > kRegListThreshold) {
183 __ StoreList(RegList(core_spills), orig_offset);
184 } else {
185 stack_offset = orig_offset;
186 for (uint32_t i : LowToHighBits(core_spills)) {
187 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
188 }
189 }
190 }
191
Artem Serovd300d8f2016-07-15 14:00:56 +0100192 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
193 orig_offset = stack_offset;
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 for (uint32_t i : LowToHighBits(fp_spills)) {
Artem Serovf4d6aee2016-07-11 10:41:45 +0100195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serovd300d8f2016-07-15 14:00:56 +0100197 stack_offset += kArmWordSize;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100198 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100199
200 stack_offset = orig_offset;
201 while (fp_spills != 0u) {
202 uint32_t begin = CTZ(fp_spills);
203 uint32_t tmp = fp_spills + (1u << begin);
204 fp_spills &= tmp; // Clear the contiguous range of 1s.
205 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
206 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
207 }
208 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100209}
210
211void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
212 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
213 size_t orig_offset = stack_offset;
214
215 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
216 for (uint32_t i : LowToHighBits(core_spills)) {
217 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
218 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
219 stack_offset += kArmWordSize;
220 }
221
222 int reg_num = POPCOUNT(core_spills);
223 if (reg_num != 0) {
224 if (reg_num > kRegListThreshold) {
225 __ LoadList(RegList(core_spills), orig_offset);
226 } else {
227 stack_offset = orig_offset;
228 for (uint32_t i : LowToHighBits(core_spills)) {
229 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
230 }
231 }
232 }
233
Artem Serovd300d8f2016-07-15 14:00:56 +0100234 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
235 while (fp_spills != 0u) {
236 uint32_t begin = CTZ(fp_spills);
237 uint32_t tmp = fp_spills + (1u << begin);
238 fp_spills &= tmp; // Clear the contiguous range of 1s.
239 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
240 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
Artem Serovf4d6aee2016-07-11 10:41:45 +0100241 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100242 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100243}
244
245class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100246 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100247 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100248
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100250 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100251 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000252 if (instruction_->CanThrowIntoCatchBlock()) {
253 // Live registers will be restored in the catch block if caught.
254 SaveLiveRegisters(codegen, instruction_->GetLocations());
255 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100256 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
257 instruction_,
258 instruction_->GetDexPc(),
259 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000260 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100261 }
262
Alexandre Rames8158f282015-08-07 10:26:17 +0100263 bool IsFatal() const OVERRIDE { return true; }
264
Alexandre Rames9931f312015-06-19 14:47:01 +0100265 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
266
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100267 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100268 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
269};
270
Artem Serovf4d6aee2016-07-11 10:41:45 +0100271class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000272 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100273 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000274
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000276 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
277 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100278 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000279 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000280 }
281
Alexandre Rames8158f282015-08-07 10:26:17 +0100282 bool IsFatal() const OVERRIDE { return true; }
283
Alexandre Rames9931f312015-06-19 14:47:01 +0100284 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
285
Calin Juravled0d48522014-11-04 16:40:20 +0000286 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000287 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
288};
289
Artem Serovf4d6aee2016-07-11 10:41:45 +0100290class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000291 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100293 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000294
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100296 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000297 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100298 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000299 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100300 if (successor_ == nullptr) {
301 __ b(GetReturnLabel());
302 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100303 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100304 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000305 }
306
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100307 Label* GetReturnLabel() {
308 DCHECK(successor_ == nullptr);
309 return &return_label_;
310 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000311
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100312 HBasicBlock* GetSuccessor() const {
313 return successor_;
314 }
315
Alexandre Rames9931f312015-06-19 14:47:01 +0100316 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
317
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000318 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100319 // If not null, the block to branch to after the suspend check.
320 HBasicBlock* const successor_;
321
322 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000323 Label return_label_;
324
325 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
326};
327
Artem Serovf4d6aee2016-07-11 10:41:45 +0100328class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100329 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100330 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100331 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100332
Alexandre Rames67555f72014-11-18 10:55:16 +0000333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100334 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100335 LocationSummary* locations = instruction_->GetLocations();
336
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100337 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000338 if (instruction_->CanThrowIntoCatchBlock()) {
339 // Live registers will be restored in the catch block if caught.
340 SaveLiveRegisters(codegen, instruction_->GetLocations());
341 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000342 // We're moving two locations to locations that could overlap, so we need a parallel
343 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100344 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000345 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100346 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000347 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100348 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100349 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100350 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
351 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100352 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
353 ? kQuickThrowStringBounds
354 : kQuickThrowArrayBounds;
355 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100356 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000357 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100358 }
359
Alexandre Rames8158f282015-08-07 10:26:17 +0100360 bool IsFatal() const OVERRIDE { return true; }
361
Alexandre Rames9931f312015-06-19 14:47:01 +0100362 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
363
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100364 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100365 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
366};
367
Artem Serovf4d6aee2016-07-11 10:41:45 +0100368class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100369 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000370 LoadClassSlowPathARM(HLoadClass* cls,
371 HInstruction* at,
372 uint32_t dex_pc,
373 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000374 : SlowPathCodeARM(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000375 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
376 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100377
Alexandre Rames67555f72014-11-18 10:55:16 +0000378 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000379 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000380
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100381 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
382 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000383 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100385 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000386 dex::TypeIndex type_index = cls_->GetTypeIndex();
387 __ LoadImmediate(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100388 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
389 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000390 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000391 if (do_clinit_) {
392 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
393 } else {
394 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
395 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000396
397 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000398 Location out = locations->Out();
399 if (out.IsValid()) {
400 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000401 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
402 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000403 RestoreLiveRegisters(codegen, locations);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000404 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
405 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
406 if (cls_ == instruction_ && cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry) {
407 DCHECK(out.IsValid());
408 // TODO: Change art_quick_initialize_type/art_quick_initialize_static_storage to
409 // kSaveEverything and use a temporary for the .bss entry address in the fast path,
410 // so that we can avoid another calculation here.
411 CodeGeneratorARM::PcRelativePatchInfo* labels =
412 arm_codegen->NewPcRelativeTypePatch(cls_->GetDexFile(), type_index);
413 __ BindTrackedLabel(&labels->movw_label);
414 __ movw(IP, /* placeholder */ 0u);
415 __ BindTrackedLabel(&labels->movt_label);
416 __ movt(IP, /* placeholder */ 0u);
417 __ BindTrackedLabel(&labels->add_pc_label);
418 __ add(IP, IP, ShifterOperand(PC));
419 __ str(locations->Out().AsRegister<Register>(), Address(IP));
420 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100421 __ b(GetExitLabel());
422 }
423
Alexandre Rames9931f312015-06-19 14:47:01 +0100424 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
425
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100426 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000427 // The class this slow path will load.
428 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100429
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000430 // The dex PC of `at_`.
431 const uint32_t dex_pc_;
432
433 // Whether to initialize the class.
434 const bool do_clinit_;
435
436 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100437};
438
Vladimir Markoaad75c62016-10-03 08:46:48 +0000439class LoadStringSlowPathARM : public SlowPathCodeARM {
440 public:
441 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
442
443 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
444 LocationSummary* locations = instruction_->GetLocations();
445 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100446 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000447 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100448 Register out = locations->Out().AsRegister<Register>();
449 Register temp = locations->GetTemp(0).AsRegister<Register>();
450 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000451
452 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
453 __ Bind(GetEntryLabel());
454 SaveLiveRegisters(codegen, locations);
455
456 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100457 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
458 // the kSaveEverything call (or use `out` for the address after non-kSaveEverything call).
459 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
460 Register entry_address = temp_is_r0 ? out : temp;
461 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
462 if (call_saves_everything_except_r0 && temp_is_r0) {
463 __ mov(entry_address, ShifterOperand(temp));
464 }
465
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000466 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000467 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
468 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100469
470 // Store the resolved String to the .bss entry.
471 if (call_saves_everything_except_r0) {
472 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
473 __ str(R0, Address(entry_address));
474 } else {
475 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
476 CodeGeneratorARM::PcRelativePatchInfo* labels =
477 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
478 __ BindTrackedLabel(&labels->movw_label);
479 __ movw(entry_address, /* placeholder */ 0u);
480 __ BindTrackedLabel(&labels->movt_label);
481 __ movt(entry_address, /* placeholder */ 0u);
482 __ BindTrackedLabel(&labels->add_pc_label);
483 __ add(entry_address, entry_address, ShifterOperand(PC));
484 __ str(R0, Address(entry_address));
485 }
486
Vladimir Markoaad75c62016-10-03 08:46:48 +0000487 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000488 RestoreLiveRegisters(codegen, locations);
489
Vladimir Markoaad75c62016-10-03 08:46:48 +0000490 __ b(GetExitLabel());
491 }
492
493 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
494
495 private:
496 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
497};
498
Artem Serovf4d6aee2016-07-11 10:41:45 +0100499class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000500 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000501 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100502 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000503
Alexandre Rames67555f72014-11-18 10:55:16 +0000504 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000505 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000506 DCHECK(instruction_->IsCheckCast()
507 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000508
509 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
510 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000511
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000512 if (!is_fatal_) {
513 SaveLiveRegisters(codegen, locations);
514 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000515
516 // We're moving two locations to locations that could overlap, so we need a parallel
517 // move resolver.
518 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800519 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800520 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
521 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800522 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800523 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
524 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000525 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100526 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100527 instruction_,
528 instruction_->GetDexPc(),
529 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800530 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000531 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
532 } else {
533 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800534 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
535 instruction_,
536 instruction_->GetDexPc(),
537 this);
538 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000539 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000540
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000541 if (!is_fatal_) {
542 RestoreLiveRegisters(codegen, locations);
543 __ b(GetExitLabel());
544 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000545 }
546
Alexandre Rames9931f312015-06-19 14:47:01 +0100547 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
548
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000549 bool IsFatal() const OVERRIDE { return is_fatal_; }
550
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000551 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000552 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000553
554 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
555};
556
Artem Serovf4d6aee2016-07-11 10:41:45 +0100557class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700558 public:
Aart Bik42249c32016-01-07 15:33:50 -0800559 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100560 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700561
562 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800563 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700564 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100565 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000566 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700567 }
568
Alexandre Rames9931f312015-06-19 14:47:01 +0100569 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
570
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700571 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700572 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
573};
574
Artem Serovf4d6aee2016-07-11 10:41:45 +0100575class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100576 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100577 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100578
579 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
580 LocationSummary* locations = instruction_->GetLocations();
581 __ Bind(GetEntryLabel());
582 SaveLiveRegisters(codegen, locations);
583
584 InvokeRuntimeCallingConvention calling_convention;
585 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
586 parallel_move.AddMove(
587 locations->InAt(0),
588 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
589 Primitive::kPrimNot,
590 nullptr);
591 parallel_move.AddMove(
592 locations->InAt(1),
593 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
594 Primitive::kPrimInt,
595 nullptr);
596 parallel_move.AddMove(
597 locations->InAt(2),
598 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
599 Primitive::kPrimNot,
600 nullptr);
601 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
602
603 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100604 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000605 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100606 RestoreLiveRegisters(codegen, locations);
607 __ b(GetExitLabel());
608 }
609
610 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
611
612 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100613 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
614};
615
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100616// Slow path marking an object reference `ref` during a read
617// barrier. The field `obj.field` in the object `obj` holding this
618// reference does not get updated by this slow path after marking (see
619// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
620//
621// This means that after the execution of this slow path, `ref` will
622// always be up-to-date, but `obj.field` may not; i.e., after the
623// flip, `ref` will be a to-space reference, but `obj.field` will
624// probably still be a from-space reference (unless it gets updated by
625// another thread, or if another thread installed another object
626// reference (different from `ref`) in `obj.field`).
Artem Serovf4d6aee2016-07-11 10:41:45 +0100627class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000628 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800629 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
630 Location ref,
631 Location entrypoint = Location::NoLocation())
632 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillainc9285912015-12-18 10:38:42 +0000633 DCHECK(kEmitCompilerReadBarrier);
634 }
635
636 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
637
638 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
639 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100640 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000641 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100642 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillainc9285912015-12-18 10:38:42 +0000643 DCHECK(instruction_->IsInstanceFieldGet() ||
644 instruction_->IsStaticFieldGet() ||
645 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100646 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000647 instruction_->IsLoadClass() ||
648 instruction_->IsLoadString() ||
649 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100650 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100651 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
652 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000653 << "Unexpected instruction in read barrier marking slow path: "
654 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000655 // The read barrier instrumentation of object ArrayGet
656 // instructions does not support the HIntermediateAddress
657 // instruction.
658 DCHECK(!(instruction_->IsArrayGet() &&
659 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillainc9285912015-12-18 10:38:42 +0000660
661 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100662 // No need to save live registers; it's taken care of by the
663 // entrypoint. Also, there is no need to update the stack mask,
664 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000665 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100666 DCHECK_NE(ref_reg, SP);
667 DCHECK_NE(ref_reg, LR);
668 DCHECK_NE(ref_reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100669 // IP is used internally by the ReadBarrierMarkRegX entry point
670 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100671 DCHECK_NE(ref_reg, IP);
672 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100673 // "Compact" slow path, saving two moves.
674 //
675 // Instead of using the standard runtime calling convention (input
676 // and output in R0):
677 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100678 // R0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100679 // R0 <- ReadBarrierMark(R0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100680 // ref <- R0
Roland Levillain02b75802016-07-13 11:54:35 +0100681 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100682 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100683 // of a dedicated entrypoint:
684 //
685 // rX <- ReadBarrierMarkRegX(rX)
686 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800687 if (entrypoint_.IsValid()) {
688 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
689 __ blx(entrypoint_.AsRegister<Register>());
690 } else {
691 int32_t entry_point_offset =
692 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
693 // This runtime call does not require a stack map.
694 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
695 }
Roland Levillainc9285912015-12-18 10:38:42 +0000696 __ b(GetExitLabel());
697 }
698
699 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100700 // The location (register) of the marked object reference.
701 const Location ref_;
Roland Levillainc9285912015-12-18 10:38:42 +0000702
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800703 // The location of the entrypoint if already loaded.
704 const Location entrypoint_;
705
Roland Levillainc9285912015-12-18 10:38:42 +0000706 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
707};
708
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100709// Slow path marking an object reference `ref` during a read barrier,
710// and if needed, atomically updating the field `obj.field` in the
711// object `obj` holding this reference after marking (contrary to
712// ReadBarrierMarkSlowPathARM above, which never tries to update
713// `obj.field`).
714//
715// This means that after the execution of this slow path, both `ref`
716// and `obj.field` will be up-to-date; i.e., after the flip, both will
717// hold the same to-space reference (unless another thread installed
718// another object reference (different from `ref`) in `obj.field`).
719class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
720 public:
721 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
722 Location ref,
723 Register obj,
724 Location field_offset,
725 Register temp1,
726 Register temp2)
727 : SlowPathCodeARM(instruction),
728 ref_(ref),
729 obj_(obj),
730 field_offset_(field_offset),
731 temp1_(temp1),
732 temp2_(temp2) {
733 DCHECK(kEmitCompilerReadBarrier);
734 }
735
736 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
737
738 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
739 LocationSummary* locations = instruction_->GetLocations();
740 Register ref_reg = ref_.AsRegister<Register>();
741 DCHECK(locations->CanCall());
742 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
743 // This slow path is only used by the UnsafeCASObject intrinsic.
744 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
745 << "Unexpected instruction in read barrier marking and field updating slow path: "
746 << instruction_->DebugName();
747 DCHECK(instruction_->GetLocations()->Intrinsified());
748 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
749 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
750
751 __ Bind(GetEntryLabel());
752
753 // Save the old reference.
754 // Note that we cannot use IP to save the old reference, as IP is
755 // used internally by the ReadBarrierMarkRegX entry point, and we
756 // need the old reference after the call to that entry point.
757 DCHECK_NE(temp1_, IP);
758 __ Mov(temp1_, ref_reg);
759
760 // No need to save live registers; it's taken care of by the
761 // entrypoint. Also, there is no need to update the stack mask,
762 // as this runtime call will not trigger a garbage collection.
763 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
764 DCHECK_NE(ref_reg, SP);
765 DCHECK_NE(ref_reg, LR);
766 DCHECK_NE(ref_reg, PC);
767 // IP is used internally by the ReadBarrierMarkRegX entry point
768 // as a temporary, it cannot be the entry point's input/output.
769 DCHECK_NE(ref_reg, IP);
770 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
771 // "Compact" slow path, saving two moves.
772 //
773 // Instead of using the standard runtime calling convention (input
774 // and output in R0):
775 //
776 // R0 <- ref
777 // R0 <- ReadBarrierMark(R0)
778 // ref <- R0
779 //
780 // we just use rX (the register containing `ref`) as input and output
781 // of a dedicated entrypoint:
782 //
783 // rX <- ReadBarrierMarkRegX(rX)
784 //
785 int32_t entry_point_offset =
786 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
787 // This runtime call does not require a stack map.
788 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
789
790 // If the new reference is different from the old reference,
791 // update the field in the holder (`*(obj_ + field_offset_)`).
792 //
793 // Note that this field could also hold a different object, if
794 // another thread had concurrently changed it. In that case, the
795 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
796 // (CAS) operation below would abort the CAS, leaving the field
797 // as-is.
798 Label done;
799 __ cmp(temp1_, ShifterOperand(ref_reg));
800 __ b(&done, EQ);
801
802 // Update the the holder's field atomically. This may fail if
803 // mutator updates before us, but it's OK. This is achieved
804 // using a strong compare-and-set (CAS) operation with relaxed
805 // memory synchronization ordering, where the expected value is
806 // the old reference and the desired value is the new reference.
807
808 // Convenience aliases.
809 Register base = obj_;
810 // The UnsafeCASObject intrinsic uses a register pair as field
811 // offset ("long offset"), of which only the low part contains
812 // data.
813 Register offset = field_offset_.AsRegisterPairLow<Register>();
814 Register expected = temp1_;
815 Register value = ref_reg;
816 Register tmp_ptr = IP; // Pointer to actual memory.
817 Register tmp = temp2_; // Value in memory.
818
819 __ add(tmp_ptr, base, ShifterOperand(offset));
820
821 if (kPoisonHeapReferences) {
822 __ PoisonHeapReference(expected);
823 if (value == expected) {
824 // Do not poison `value`, as it is the same register as
825 // `expected`, which has just been poisoned.
826 } else {
827 __ PoisonHeapReference(value);
828 }
829 }
830
831 // do {
832 // tmp = [r_ptr] - expected;
833 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
834
Roland Levillain24a4d112016-10-26 13:10:46 +0100835 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100836 __ Bind(&loop_head);
837
838 __ ldrex(tmp, tmp_ptr);
839
840 __ subs(tmp, tmp, ShifterOperand(expected));
841
Roland Levillain24a4d112016-10-26 13:10:46 +0100842 __ it(NE);
843 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100844
Roland Levillain24a4d112016-10-26 13:10:46 +0100845 __ b(&exit_loop, NE);
846
847 __ strex(tmp, value, tmp_ptr);
848 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100849 __ b(&loop_head, EQ);
850
Roland Levillain24a4d112016-10-26 13:10:46 +0100851 __ Bind(&exit_loop);
852
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100853 if (kPoisonHeapReferences) {
854 __ UnpoisonHeapReference(expected);
855 if (value == expected) {
856 // Do not unpoison `value`, as it is the same register as
857 // `expected`, which has just been unpoisoned.
858 } else {
859 __ UnpoisonHeapReference(value);
860 }
861 }
862
863 __ Bind(&done);
864 __ b(GetExitLabel());
865 }
866
867 private:
868 // The location (register) of the marked object reference.
869 const Location ref_;
870 // The register containing the object holding the marked object reference field.
871 const Register obj_;
872 // The location of the offset of the marked reference field within `obj_`.
873 Location field_offset_;
874
875 const Register temp1_;
876 const Register temp2_;
877
878 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
879};
880
Roland Levillain3b359c72015-11-17 19:35:12 +0000881// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100882class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000883 public:
884 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
885 Location out,
886 Location ref,
887 Location obj,
888 uint32_t offset,
889 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100890 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000891 out_(out),
892 ref_(ref),
893 obj_(obj),
894 offset_(offset),
895 index_(index) {
896 DCHECK(kEmitCompilerReadBarrier);
897 // If `obj` is equal to `out` or `ref`, it means the initial object
898 // has been overwritten by (or after) the heap object reference load
899 // to be instrumented, e.g.:
900 //
901 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000902 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000903 //
904 // In that case, we have lost the information about the original
905 // object, and the emitted read barrier cannot work properly.
906 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
907 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
908 }
909
910 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
911 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
912 LocationSummary* locations = instruction_->GetLocations();
913 Register reg_out = out_.AsRegister<Register>();
914 DCHECK(locations->CanCall());
915 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100916 DCHECK(instruction_->IsInstanceFieldGet() ||
917 instruction_->IsStaticFieldGet() ||
918 instruction_->IsArrayGet() ||
919 instruction_->IsInstanceOf() ||
920 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100921 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000922 << "Unexpected instruction in read barrier for heap reference slow path: "
923 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000924 // The read barrier instrumentation of object ArrayGet
925 // instructions does not support the HIntermediateAddress
926 // instruction.
927 DCHECK(!(instruction_->IsArrayGet() &&
928 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +0000929
930 __ Bind(GetEntryLabel());
931 SaveLiveRegisters(codegen, locations);
932
933 // We may have to change the index's value, but as `index_` is a
934 // constant member (like other "inputs" of this slow path),
935 // introduce a copy of it, `index`.
936 Location index = index_;
937 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100938 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000939 if (instruction_->IsArrayGet()) {
940 // Compute the actual memory offset and store it in `index`.
941 Register index_reg = index_.AsRegister<Register>();
942 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
943 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
944 // We are about to change the value of `index_reg` (see the
945 // calls to art::arm::Thumb2Assembler::Lsl and
946 // art::arm::Thumb2Assembler::AddConstant below), but it has
947 // not been saved by the previous call to
948 // art::SlowPathCode::SaveLiveRegisters, as it is a
949 // callee-save register --
950 // art::SlowPathCode::SaveLiveRegisters does not consider
951 // callee-save registers, as it has been designed with the
952 // assumption that callee-save registers are supposed to be
953 // handled by the called function. So, as a callee-save
954 // register, `index_reg` _would_ eventually be saved onto
955 // the stack, but it would be too late: we would have
956 // changed its value earlier. Therefore, we manually save
957 // it here into another freely available register,
958 // `free_reg`, chosen of course among the caller-save
959 // registers (as a callee-save `free_reg` register would
960 // exhibit the same problem).
961 //
962 // Note we could have requested a temporary register from
963 // the register allocator instead; but we prefer not to, as
964 // this is a slow path, and we know we can find a
965 // caller-save register that is available.
966 Register free_reg = FindAvailableCallerSaveRegister(codegen);
967 __ Mov(free_reg, index_reg);
968 index_reg = free_reg;
969 index = Location::RegisterLocation(index_reg);
970 } else {
971 // The initial register stored in `index_` has already been
972 // saved in the call to art::SlowPathCode::SaveLiveRegisters
973 // (as it is not a callee-save register), so we can freely
974 // use it.
975 }
976 // Shifting the index value contained in `index_reg` by the scale
977 // factor (2) cannot overflow in practice, as the runtime is
978 // unable to allocate object arrays with a size larger than
979 // 2^26 - 1 (that is, 2^28 - 4 bytes).
980 __ Lsl(index_reg, index_reg, TIMES_4);
981 static_assert(
982 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
983 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
984 __ AddConstant(index_reg, index_reg, offset_);
985 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100986 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
987 // intrinsics, `index_` is not shifted by a scale factor of 2
988 // (as in the case of ArrayGet), as it is actually an offset
989 // to an object field within an object.
990 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000991 DCHECK(instruction_->GetLocations()->Intrinsified());
992 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
993 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
994 << instruction_->AsInvoke()->GetIntrinsic();
995 DCHECK_EQ(offset_, 0U);
996 DCHECK(index_.IsRegisterPair());
997 // UnsafeGet's offset location is a register pair, the low
998 // part contains the correct offset.
999 index = index_.ToLow();
1000 }
1001 }
1002
1003 // We're moving two or three locations to locations that could
1004 // overlap, so we need a parallel move resolver.
1005 InvokeRuntimeCallingConvention calling_convention;
1006 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1007 parallel_move.AddMove(ref_,
1008 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1009 Primitive::kPrimNot,
1010 nullptr);
1011 parallel_move.AddMove(obj_,
1012 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1013 Primitive::kPrimNot,
1014 nullptr);
1015 if (index.IsValid()) {
1016 parallel_move.AddMove(index,
1017 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1018 Primitive::kPrimInt,
1019 nullptr);
1020 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1021 } else {
1022 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1023 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1024 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001025 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001026 CheckEntrypointTypes<
1027 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1028 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1029
1030 RestoreLiveRegisters(codegen, locations);
1031 __ b(GetExitLabel());
1032 }
1033
1034 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1035
1036 private:
1037 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1038 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1039 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1040 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1041 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1042 return static_cast<Register>(i);
1043 }
1044 }
1045 // We shall never fail to find a free caller-save register, as
1046 // there are more than two core caller-save registers on ARM
1047 // (meaning it is possible to find one which is different from
1048 // `ref` and `obj`).
1049 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1050 LOG(FATAL) << "Could not find a free caller-save register";
1051 UNREACHABLE();
1052 }
1053
Roland Levillain3b359c72015-11-17 19:35:12 +00001054 const Location out_;
1055 const Location ref_;
1056 const Location obj_;
1057 const uint32_t offset_;
1058 // An additional location containing an index to an array.
1059 // Only used for HArrayGet and the UnsafeGetObject &
1060 // UnsafeGetObjectVolatile intrinsics.
1061 const Location index_;
1062
1063 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1064};
1065
1066// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001067class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001068 public:
1069 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001070 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001071 DCHECK(kEmitCompilerReadBarrier);
1072 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001073
1074 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1075 LocationSummary* locations = instruction_->GetLocations();
1076 Register reg_out = out_.AsRegister<Register>();
1077 DCHECK(locations->CanCall());
1078 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001079 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1080 << "Unexpected instruction in read barrier for GC root slow path: "
1081 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001082
1083 __ Bind(GetEntryLabel());
1084 SaveLiveRegisters(codegen, locations);
1085
1086 InvokeRuntimeCallingConvention calling_convention;
1087 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1088 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001089 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001090 instruction_,
1091 instruction_->GetDexPc(),
1092 this);
1093 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1094 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1095
1096 RestoreLiveRegisters(codegen, locations);
1097 __ b(GetExitLabel());
1098 }
1099
1100 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1101
1102 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001103 const Location out_;
1104 const Location root_;
1105
1106 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1107};
1108
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001109#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001110// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1111#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -07001112
Aart Bike9f37602015-10-09 11:15:55 -07001113inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001114 switch (cond) {
1115 case kCondEQ: return EQ;
1116 case kCondNE: return NE;
1117 case kCondLT: return LT;
1118 case kCondLE: return LE;
1119 case kCondGT: return GT;
1120 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001121 case kCondB: return LO;
1122 case kCondBE: return LS;
1123 case kCondA: return HI;
1124 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001125 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001126 LOG(FATAL) << "Unreachable";
1127 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001128}
1129
Aart Bike9f37602015-10-09 11:15:55 -07001130// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001131inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001132 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001133 case kCondEQ: return EQ;
1134 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001135 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001136 case kCondLT: return LO;
1137 case kCondLE: return LS;
1138 case kCondGT: return HI;
1139 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001140 // Unsigned remain unchanged.
1141 case kCondB: return LO;
1142 case kCondBE: return LS;
1143 case kCondA: return HI;
1144 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001145 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001146 LOG(FATAL) << "Unreachable";
1147 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001148}
1149
Vladimir Markod6e069b2016-01-18 11:11:01 +00001150inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1151 // The ARM condition codes can express all the necessary branches, see the
1152 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1153 // There is no dex instruction or HIR that would need the missing conditions
1154 // "equal or unordered" or "not equal".
1155 switch (cond) {
1156 case kCondEQ: return EQ;
1157 case kCondNE: return NE /* unordered */;
1158 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1159 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1160 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1161 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1162 default:
1163 LOG(FATAL) << "UNREACHABLE";
1164 UNREACHABLE();
1165 }
1166}
1167
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001168void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001169 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001170}
1171
1172void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001173 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001174}
1175
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001176size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1177 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1178 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001179}
1180
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001181size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1182 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1183 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001184}
1185
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001186size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1187 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1188 return kArmWordSize;
1189}
1190
1191size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1192 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1193 return kArmWordSize;
1194}
1195
Calin Juravle34166012014-12-19 17:22:29 +00001196CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001197 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001198 const CompilerOptions& compiler_options,
1199 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001200 : CodeGenerator(graph,
1201 kNumberOfCoreRegisters,
1202 kNumberOfSRegisters,
1203 kNumberOfRegisterPairs,
1204 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1205 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001206 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1207 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001208 compiler_options,
1209 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001210 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001211 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001212 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001213 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001214 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001215 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001216 uint32_literals_(std::less<uint32_t>(),
1217 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001218 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1219 boot_image_string_patches_(StringReferenceValueComparator(),
1220 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1221 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001222 boot_image_type_patches_(TypeReferenceValueComparator(),
1223 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1224 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001225 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001226 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1227 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001228 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1229 jit_class_patches_(TypeReferenceValueComparator(),
1230 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001231 // Always save the LR register to mimic Quick.
1232 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001233}
1234
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001235void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1236 // Ensure that we fix up branches and literal loads and emit the literal pool.
1237 __ FinalizeCode();
1238
1239 // Adjust native pc offsets in stack maps.
1240 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
1241 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
1242 uint32_t new_position = __ GetAdjustedPosition(old_position);
1243 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1244 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001245 // Adjust pc offsets for the disassembly information.
1246 if (disasm_info_ != nullptr) {
1247 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1248 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1249 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1250 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1251 it.second.start = __ GetAdjustedPosition(it.second.start);
1252 it.second.end = __ GetAdjustedPosition(it.second.end);
1253 }
1254 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1255 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1256 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1257 }
1258 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001259
1260 CodeGenerator::Finalize(allocator);
1261}
1262
David Brazdil58282f42016-01-14 12:45:10 +00001263void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001264 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001265 blocked_core_registers_[SP] = true;
1266 blocked_core_registers_[LR] = true;
1267 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001268
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001269 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001270 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001271
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001272 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001273 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001274
David Brazdil58282f42016-01-14 12:45:10 +00001275 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001276 // Stubs do not save callee-save floating point registers. If the graph
1277 // is debuggable, we need to deal with these registers differently. For
1278 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001279 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1280 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1281 }
1282 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001283}
1284
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001285InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001286 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001287 assembler_(codegen->GetAssembler()),
1288 codegen_(codegen) {}
1289
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001290void CodeGeneratorARM::ComputeSpillMask() {
1291 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1292 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001293 // There is no easy instruction to restore just the PC on thumb2. We spill and
1294 // restore another arbitrary register.
1295 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001296 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1297 // We use vpush and vpop for saving and restoring floating point registers, which take
1298 // a SRegister and the number of registers to save/restore after that SRegister. We
1299 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1300 // but in the range.
1301 if (fpu_spill_mask_ != 0) {
1302 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1303 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1304 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1305 fpu_spill_mask_ |= (1 << i);
1306 }
1307 }
1308}
1309
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001310static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001311 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001312}
1313
1314static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001315 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001316}
1317
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001318void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001319 bool skip_overflow_check =
1320 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001321 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001322 __ Bind(&frame_entry_label_);
1323
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001324 if (HasEmptyFrame()) {
1325 return;
1326 }
1327
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001328 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001329 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1330 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1331 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001332 }
1333
Andreas Gampe501fd632015-09-10 16:11:06 -07001334 __ PushList(core_spill_mask_);
1335 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1336 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001337 if (fpu_spill_mask_ != 0) {
1338 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1339 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001340 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001341 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001342 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001343
1344 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1345 // Initialize should_deoptimize flag to 0.
1346 __ mov(IP, ShifterOperand(0));
1347 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
1348 }
1349
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001350 int adjust = GetFrameSize() - FrameEntrySpillSize();
1351 __ AddConstant(SP, -adjust);
1352 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001353
1354 // Save the current method if we need it. Note that we do not
1355 // do this in HCurrentMethod, as the instruction might have been removed
1356 // in the SSA graph.
1357 if (RequiresCurrentMethod()) {
1358 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1359 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001360}
1361
1362void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001363 if (HasEmptyFrame()) {
1364 __ bx(LR);
1365 return;
1366 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001367 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001368 int adjust = GetFrameSize() - FrameEntrySpillSize();
1369 __ AddConstant(SP, adjust);
1370 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001371 if (fpu_spill_mask_ != 0) {
1372 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1373 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001374 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001375 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001376 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001377 // Pop LR into PC to return.
1378 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1379 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1380 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001381 __ cfi().RestoreState();
1382 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001383}
1384
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001385void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001386 Label* label = GetLabelOf(block);
1387 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001388}
1389
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001390Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001391 switch (type) {
1392 case Primitive::kPrimBoolean:
1393 case Primitive::kPrimByte:
1394 case Primitive::kPrimChar:
1395 case Primitive::kPrimShort:
1396 case Primitive::kPrimInt:
1397 case Primitive::kPrimNot: {
1398 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001399 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001400 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001401 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001402 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001403 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001404 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001405 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001406
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001407 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001408 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001409 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001410 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001411 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001412 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001413 if (calling_convention.GetRegisterAt(index) == R1) {
1414 // Skip R1, and use R2_R3 instead.
1415 gp_index_++;
1416 index++;
1417 }
1418 }
1419 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1420 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001421 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001422
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001423 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001424 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001425 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001426 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1427 }
1428 }
1429
1430 case Primitive::kPrimFloat: {
1431 uint32_t stack_index = stack_index_++;
1432 if (float_index_ % 2 == 0) {
1433 float_index_ = std::max(double_index_, float_index_);
1434 }
1435 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1436 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1437 } else {
1438 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1439 }
1440 }
1441
1442 case Primitive::kPrimDouble: {
1443 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1444 uint32_t stack_index = stack_index_;
1445 stack_index_ += 2;
1446 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1447 uint32_t index = double_index_;
1448 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001449 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001450 calling_convention.GetFpuRegisterAt(index),
1451 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001452 DCHECK(ExpectedPairLayout(result));
1453 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001454 } else {
1455 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001456 }
1457 }
1458
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001459 case Primitive::kPrimVoid:
1460 LOG(FATAL) << "Unexpected parameter type " << type;
1461 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001462 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001463 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001464}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001465
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001466Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001467 switch (type) {
1468 case Primitive::kPrimBoolean:
1469 case Primitive::kPrimByte:
1470 case Primitive::kPrimChar:
1471 case Primitive::kPrimShort:
1472 case Primitive::kPrimInt:
1473 case Primitive::kPrimNot: {
1474 return Location::RegisterLocation(R0);
1475 }
1476
1477 case Primitive::kPrimFloat: {
1478 return Location::FpuRegisterLocation(S0);
1479 }
1480
1481 case Primitive::kPrimLong: {
1482 return Location::RegisterPairLocation(R0, R1);
1483 }
1484
1485 case Primitive::kPrimDouble: {
1486 return Location::FpuRegisterPairLocation(S0, S1);
1487 }
1488
1489 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001490 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001491 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001492
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001493 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001494}
1495
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001496Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1497 return Location::RegisterLocation(kMethodRegisterArgument);
1498}
1499
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001500void CodeGeneratorARM::Move32(Location destination, Location source) {
1501 if (source.Equals(destination)) {
1502 return;
1503 }
1504 if (destination.IsRegister()) {
1505 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001506 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001507 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001508 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001509 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001510 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001511 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001512 } else if (destination.IsFpuRegister()) {
1513 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001514 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001515 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001516 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001517 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001518 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001519 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001520 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001521 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001522 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001523 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001524 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001525 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001526 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001527 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001528 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1529 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001530 }
1531 }
1532}
1533
1534void CodeGeneratorARM::Move64(Location destination, Location source) {
1535 if (source.Equals(destination)) {
1536 return;
1537 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001538 if (destination.IsRegisterPair()) {
1539 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001540 EmitParallelMoves(
1541 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1542 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001543 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001544 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001545 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1546 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001547 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001548 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001549 } else if (source.IsFpuRegisterPair()) {
1550 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1551 destination.AsRegisterPairHigh<Register>(),
1552 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001553 } else {
1554 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001555 DCHECK(ExpectedPairLayout(destination));
1556 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1557 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001558 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001559 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001560 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001561 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1562 SP,
1563 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001564 } else if (source.IsRegisterPair()) {
1565 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1566 source.AsRegisterPairLow<Register>(),
1567 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001568 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001569 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001570 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001571 } else {
1572 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001573 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001574 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001575 if (source.AsRegisterPairLow<Register>() == R1) {
1576 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001577 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1578 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001579 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001580 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001581 SP, destination.GetStackIndex());
1582 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001583 } else if (source.IsFpuRegisterPair()) {
1584 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1585 SP,
1586 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001587 } else {
1588 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001589 EmitParallelMoves(
1590 Location::StackSlot(source.GetStackIndex()),
1591 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001592 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001593 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001594 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1595 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001596 }
1597 }
1598}
1599
Calin Juravle175dc732015-08-25 15:42:32 +01001600void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1601 DCHECK(location.IsRegister());
1602 __ LoadImmediate(location.AsRegister<Register>(), value);
1603}
1604
Calin Juravlee460d1d2015-09-29 04:52:17 +01001605void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001606 HParallelMove move(GetGraph()->GetArena());
1607 move.AddMove(src, dst, dst_type, nullptr);
1608 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001609}
1610
1611void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1612 if (location.IsRegister()) {
1613 locations->AddTemp(location);
1614 } else if (location.IsRegisterPair()) {
1615 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1616 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1617 } else {
1618 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1619 }
1620}
1621
Calin Juravle175dc732015-08-25 15:42:32 +01001622void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1623 HInstruction* instruction,
1624 uint32_t dex_pc,
1625 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001626 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001627 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001628 if (EntrypointRequiresStackMap(entrypoint)) {
1629 RecordPcInfo(instruction, dex_pc, slow_path);
1630 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001631}
1632
Roland Levillaindec8f632016-07-22 17:10:06 +01001633void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1634 HInstruction* instruction,
1635 SlowPathCode* slow_path) {
1636 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001637 GenerateInvokeRuntime(entry_point_offset);
1638}
1639
1640void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001641 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1642 __ blx(LR);
1643}
1644
David Brazdilfc6a86a2015-06-26 10:33:45 +00001645void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001646 DCHECK(!successor->IsExitBlock());
1647
1648 HBasicBlock* block = got->GetBlock();
1649 HInstruction* previous = got->GetPrevious();
1650
1651 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001652 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001653 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1654 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1655 return;
1656 }
1657
1658 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1659 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1660 }
1661 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001662 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001663 }
1664}
1665
David Brazdilfc6a86a2015-06-26 10:33:45 +00001666void LocationsBuilderARM::VisitGoto(HGoto* got) {
1667 got->SetLocations(nullptr);
1668}
1669
1670void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1671 HandleGoto(got, got->GetSuccessor());
1672}
1673
1674void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1675 try_boundary->SetLocations(nullptr);
1676}
1677
1678void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1679 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1680 if (!successor->IsExitBlock()) {
1681 HandleGoto(try_boundary, successor);
1682 }
1683}
1684
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001685void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001686 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001687}
1688
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001689void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001690}
1691
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001692void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1693 Primitive::Type type = instruction->InputAt(0)->GetType();
1694 Location lhs_loc = instruction->GetLocations()->InAt(0);
1695 Location rhs_loc = instruction->GetLocations()->InAt(1);
1696 if (rhs_loc.IsConstant()) {
1697 // 0.0 is the only immediate that can be encoded directly in
1698 // a VCMP instruction.
1699 //
1700 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1701 // specify that in a floating-point comparison, positive zero
1702 // and negative zero are considered equal, so we can use the
1703 // literal 0.0 for both cases here.
1704 //
1705 // Note however that some methods (Float.equal, Float.compare,
1706 // Float.compareTo, Double.equal, Double.compare,
1707 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1708 // StrictMath.min) consider 0.0 to be (strictly) greater than
1709 // -0.0. So if we ever translate calls to these methods into a
1710 // HCompare instruction, we must handle the -0.0 case with
1711 // care here.
1712 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1713 if (type == Primitive::kPrimFloat) {
1714 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1715 } else {
1716 DCHECK_EQ(type, Primitive::kPrimDouble);
1717 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1718 }
1719 } else {
1720 if (type == Primitive::kPrimFloat) {
1721 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1722 } else {
1723 DCHECK_EQ(type, Primitive::kPrimDouble);
1724 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1725 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1726 }
1727 }
1728}
1729
Roland Levillain4fa13f62015-07-06 18:11:54 +01001730void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1731 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001732 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001733 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001734 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001735}
1736
1737void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1738 Label* true_label,
1739 Label* false_label) {
1740 LocationSummary* locations = cond->GetLocations();
1741 Location left = locations->InAt(0);
1742 Location right = locations->InAt(1);
1743 IfCondition if_cond = cond->GetCondition();
1744
1745 Register left_high = left.AsRegisterPairHigh<Register>();
1746 Register left_low = left.AsRegisterPairLow<Register>();
1747 IfCondition true_high_cond = if_cond;
1748 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001749 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001750
1751 // Set the conditions for the test, remembering that == needs to be
1752 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001753 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001754 switch (if_cond) {
1755 case kCondEQ:
1756 case kCondNE:
1757 // Nothing to do.
1758 break;
1759 case kCondLT:
1760 false_high_cond = kCondGT;
1761 break;
1762 case kCondLE:
1763 true_high_cond = kCondLT;
1764 break;
1765 case kCondGT:
1766 false_high_cond = kCondLT;
1767 break;
1768 case kCondGE:
1769 true_high_cond = kCondGT;
1770 break;
Aart Bike9f37602015-10-09 11:15:55 -07001771 case kCondB:
1772 false_high_cond = kCondA;
1773 break;
1774 case kCondBE:
1775 true_high_cond = kCondB;
1776 break;
1777 case kCondA:
1778 false_high_cond = kCondB;
1779 break;
1780 case kCondAE:
1781 true_high_cond = kCondA;
1782 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001783 }
1784 if (right.IsConstant()) {
1785 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1786 int32_t val_low = Low32Bits(value);
1787 int32_t val_high = High32Bits(value);
1788
Vladimir Markoac6ac102015-12-17 12:14:00 +00001789 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001790 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001791 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001792 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001793 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001794 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001795 __ b(true_label, ARMCondition(true_high_cond));
1796 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001797 }
1798 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001799 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001800 } else {
1801 Register right_high = right.AsRegisterPairHigh<Register>();
1802 Register right_low = right.AsRegisterPairLow<Register>();
1803
1804 __ cmp(left_high, ShifterOperand(right_high));
1805 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001806 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001807 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001808 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001809 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001810 __ b(true_label, ARMCondition(true_high_cond));
1811 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001812 }
1813 // Must be equal high, so compare the lows.
1814 __ cmp(left_low, ShifterOperand(right_low));
1815 }
1816 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001817 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001818 __ b(true_label, final_condition);
1819}
1820
David Brazdil0debae72015-11-12 18:37:00 +00001821void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1822 Label* true_target_in,
1823 Label* false_target_in) {
1824 // Generated branching requires both targets to be explicit. If either of the
1825 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1826 Label fallthrough_target;
1827 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1828 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1829
Roland Levillain4fa13f62015-07-06 18:11:54 +01001830 Primitive::Type type = condition->InputAt(0)->GetType();
1831 switch (type) {
1832 case Primitive::kPrimLong:
1833 GenerateLongComparesAndJumps(condition, true_target, false_target);
1834 break;
1835 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001836 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001837 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001838 GenerateFPJumps(condition, true_target, false_target);
1839 break;
1840 default:
1841 LOG(FATAL) << "Unexpected compare type " << type;
1842 }
1843
David Brazdil0debae72015-11-12 18:37:00 +00001844 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001845 __ b(false_target);
1846 }
David Brazdil0debae72015-11-12 18:37:00 +00001847
1848 if (fallthrough_target.IsLinked()) {
1849 __ Bind(&fallthrough_target);
1850 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001851}
1852
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001853void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001854 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001855 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001856 Label* false_target) {
1857 HInstruction* cond = instruction->InputAt(condition_input_index);
1858
1859 if (true_target == nullptr && false_target == nullptr) {
1860 // Nothing to do. The code always falls through.
1861 return;
1862 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001863 // Constant condition, statically compared against "true" (integer value 1).
1864 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001865 if (true_target != nullptr) {
1866 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001867 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001868 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001869 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001870 if (false_target != nullptr) {
1871 __ b(false_target);
1872 }
1873 }
1874 return;
1875 }
1876
1877 // The following code generates these patterns:
1878 // (1) true_target == nullptr && false_target != nullptr
1879 // - opposite condition true => branch to false_target
1880 // (2) true_target != nullptr && false_target == nullptr
1881 // - condition true => branch to true_target
1882 // (3) true_target != nullptr && false_target != nullptr
1883 // - condition true => branch to true_target
1884 // - branch to false_target
1885 if (IsBooleanValueOrMaterializedCondition(cond)) {
1886 // Condition has been materialized, compare the output to 0.
1887 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1888 DCHECK(cond_val.IsRegister());
1889 if (true_target == nullptr) {
1890 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1891 } else {
1892 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001893 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001894 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001895 // Condition has not been materialized. Use its inputs as the comparison and
1896 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001897 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001898
1899 // If this is a long or FP comparison that has been folded into
1900 // the HCondition, generate the comparison directly.
1901 Primitive::Type type = condition->InputAt(0)->GetType();
1902 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1903 GenerateCompareTestAndBranch(condition, true_target, false_target);
1904 return;
1905 }
1906
1907 LocationSummary* locations = cond->GetLocations();
1908 DCHECK(locations->InAt(0).IsRegister());
1909 Register left = locations->InAt(0).AsRegister<Register>();
1910 Location right = locations->InAt(1);
1911 if (right.IsRegister()) {
1912 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001913 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001914 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001915 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001916 }
1917 if (true_target == nullptr) {
1918 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1919 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001920 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001921 }
Dave Allison20dfc792014-06-16 20:44:29 -07001922 }
David Brazdil0debae72015-11-12 18:37:00 +00001923
1924 // If neither branch falls through (case 3), the conditional branch to `true_target`
1925 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1926 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001927 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001928 }
1929}
1930
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001931void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001932 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1933 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001934 locations->SetInAt(0, Location::RequiresRegister());
1935 }
1936}
1937
1938void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001939 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1940 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1941 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1942 nullptr : codegen_->GetLabelOf(true_successor);
1943 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1944 nullptr : codegen_->GetLabelOf(false_successor);
1945 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001946}
1947
1948void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1949 LocationSummary* locations = new (GetGraph()->GetArena())
1950 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001951 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001952 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001953 locations->SetInAt(0, Location::RequiresRegister());
1954 }
1955}
1956
1957void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001958 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001959 GenerateTestAndBranch(deoptimize,
1960 /* condition_input_index */ 0,
1961 slow_path->GetEntryLabel(),
1962 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001963}
Dave Allison20dfc792014-06-16 20:44:29 -07001964
Mingyao Yang063fc772016-08-02 11:02:54 -07001965void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1966 LocationSummary* locations = new (GetGraph()->GetArena())
1967 LocationSummary(flag, LocationSummary::kNoCall);
1968 locations->SetOut(Location::RequiresRegister());
1969}
1970
1971void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1972 __ LoadFromOffset(kLoadWord,
1973 flag->GetLocations()->Out().AsRegister<Register>(),
1974 SP,
1975 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
1976}
1977
David Brazdil74eb1b22015-12-14 11:44:01 +00001978void LocationsBuilderARM::VisitSelect(HSelect* select) {
1979 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1980 if (Primitive::IsFloatingPointType(select->GetType())) {
1981 locations->SetInAt(0, Location::RequiresFpuRegister());
1982 locations->SetInAt(1, Location::RequiresFpuRegister());
1983 } else {
1984 locations->SetInAt(0, Location::RequiresRegister());
1985 locations->SetInAt(1, Location::RequiresRegister());
1986 }
1987 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1988 locations->SetInAt(2, Location::RequiresRegister());
1989 }
1990 locations->SetOut(Location::SameAsFirstInput());
1991}
1992
1993void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1994 LocationSummary* locations = select->GetLocations();
1995 Label false_target;
1996 GenerateTestAndBranch(select,
1997 /* condition_input_index */ 2,
1998 /* true_target */ nullptr,
1999 &false_target);
2000 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2001 __ Bind(&false_target);
2002}
2003
David Srbecky0cf44932015-12-09 14:09:59 +00002004void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2005 new (GetGraph()->GetArena()) LocationSummary(info);
2006}
2007
David Srbeckyd28f4a02016-03-14 17:14:24 +00002008void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2009 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002010}
2011
2012void CodeGeneratorARM::GenerateNop() {
2013 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002014}
2015
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002016void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002017 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002018 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002019 // Handle the long/FP comparisons made in instruction simplification.
2020 switch (cond->InputAt(0)->GetType()) {
2021 case Primitive::kPrimLong:
2022 locations->SetInAt(0, Location::RequiresRegister());
2023 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002024 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002025 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2026 }
2027 break;
2028
2029 case Primitive::kPrimFloat:
2030 case Primitive::kPrimDouble:
2031 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002032 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002033 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002034 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2035 }
2036 break;
2037
2038 default:
2039 locations->SetInAt(0, Location::RequiresRegister());
2040 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002041 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002042 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2043 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002044 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002045}
2046
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002047void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002048 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002049 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002050 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002051
2052 LocationSummary* locations = cond->GetLocations();
2053 Location left = locations->InAt(0);
2054 Location right = locations->InAt(1);
2055 Register out = locations->Out().AsRegister<Register>();
2056 Label true_label, false_label;
2057
2058 switch (cond->InputAt(0)->GetType()) {
2059 default: {
2060 // Integer case.
2061 if (right.IsRegister()) {
2062 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2063 } else {
2064 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002065 __ CmpConstant(left.AsRegister<Register>(),
2066 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002067 }
Aart Bike9f37602015-10-09 11:15:55 -07002068 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002069 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002070 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002071 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002072 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002073 return;
2074 }
2075 case Primitive::kPrimLong:
2076 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2077 break;
2078 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002079 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002080 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002081 GenerateFPJumps(cond, &true_label, &false_label);
2082 break;
2083 }
2084
2085 // Convert the jumps into the result.
2086 Label done_label;
2087
2088 // False case: result = 0.
2089 __ Bind(&false_label);
2090 __ LoadImmediate(out, 0);
2091 __ b(&done_label);
2092
2093 // True case: result = 1.
2094 __ Bind(&true_label);
2095 __ LoadImmediate(out, 1);
2096 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002097}
2098
2099void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002100 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002101}
2102
2103void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002104 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002105}
2106
2107void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002108 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002109}
2110
2111void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002112 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002113}
2114
2115void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002116 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002117}
2118
2119void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002120 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002121}
2122
2123void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002124 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002125}
2126
2127void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002128 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002129}
2130
2131void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002132 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002133}
2134
2135void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002136 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002137}
2138
2139void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002140 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002141}
2142
2143void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002144 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002145}
2146
Aart Bike9f37602015-10-09 11:15:55 -07002147void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002148 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002149}
2150
2151void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002152 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002153}
2154
2155void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002156 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002157}
2158
2159void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002160 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002161}
2162
2163void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002164 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002165}
2166
2167void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002168 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002169}
2170
2171void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002172 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002173}
2174
2175void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002176 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002177}
2178
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002179void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002180 LocationSummary* locations =
2181 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002182 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002183}
2184
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002185void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002186 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002187}
2188
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002189void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2190 LocationSummary* locations =
2191 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2192 locations->SetOut(Location::ConstantLocation(constant));
2193}
2194
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002195void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002196 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002197}
2198
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002199void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002200 LocationSummary* locations =
2201 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002202 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002203}
2204
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002205void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002206 // Will be generated at use site.
2207}
2208
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002209void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2210 LocationSummary* locations =
2211 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2212 locations->SetOut(Location::ConstantLocation(constant));
2213}
2214
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002215void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002216 // Will be generated at use site.
2217}
2218
2219void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2220 LocationSummary* locations =
2221 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2222 locations->SetOut(Location::ConstantLocation(constant));
2223}
2224
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002225void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002226 // Will be generated at use site.
2227}
2228
Calin Juravle27df7582015-04-17 19:12:31 +01002229void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2230 memory_barrier->SetLocations(nullptr);
2231}
2232
2233void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002234 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002235}
2236
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002237void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002238 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002239}
2240
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002241void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002242 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002243}
2244
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002245void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002246 LocationSummary* locations =
2247 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002248 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002249}
2250
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002251void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002252 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002253}
2254
Calin Juravle175dc732015-08-25 15:42:32 +01002255void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2256 // The trampoline uses the same calling convention as dex calling conventions,
2257 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2258 // the method_idx.
2259 HandleInvoke(invoke);
2260}
2261
2262void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2263 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2264}
2265
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002266void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002267 // Explicit clinit checks triggered by static invokes must have been pruned by
2268 // art::PrepareForRegisterAllocation.
2269 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002270
Vladimir Marko68c981f2016-08-26 13:13:33 +01002271 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002272 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002273 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2274 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2275 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002276 return;
2277 }
2278
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002279 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002280
2281 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2282 if (invoke->HasPcRelativeDexCache()) {
2283 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2284 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002285}
2286
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002287static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2288 if (invoke->GetLocations()->Intrinsified()) {
2289 IntrinsicCodeGeneratorARM intrinsic(codegen);
2290 intrinsic.Dispatch(invoke);
2291 return true;
2292 }
2293 return false;
2294}
2295
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002296void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002297 // Explicit clinit checks triggered by static invokes must have been pruned by
2298 // art::PrepareForRegisterAllocation.
2299 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002300
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002301 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2302 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002303 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002304
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002305 LocationSummary* locations = invoke->GetLocations();
2306 codegen_->GenerateStaticOrDirectCall(
2307 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002308 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002309}
2310
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002311void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002312 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002313 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002314}
2315
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002316void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002317 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002318 if (intrinsic.TryDispatch(invoke)) {
2319 return;
2320 }
2321
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002322 HandleInvoke(invoke);
2323}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002324
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002325void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002326 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2327 return;
2328 }
2329
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002330 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002331 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002332 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002333}
2334
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002335void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2336 HandleInvoke(invoke);
2337 // Add the hidden argument.
2338 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2339}
2340
2341void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2342 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002343 LocationSummary* locations = invoke->GetLocations();
2344 Register temp = locations->GetTemp(0).AsRegister<Register>();
2345 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002346 Location receiver = locations->InAt(0);
2347 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2348
Roland Levillain3b359c72015-11-17 19:35:12 +00002349 // Set the hidden argument. This is safe to do this here, as R12
2350 // won't be modified thereafter, before the `blx` (call) instruction.
2351 DCHECK_EQ(R12, hidden_reg);
2352 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002353
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002354 if (receiver.IsStackSlot()) {
2355 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002356 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002357 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2358 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002359 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002360 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002361 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002362 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002363 // Instead of simply (possibly) unpoisoning `temp` here, we should
2364 // emit a read barrier for the previous class reference load.
2365 // However this is not required in practice, as this is an
2366 // intermediate/temporary reference and because the current
2367 // concurrent copying collector keeps the from-space memory
2368 // intact/accessible until the end of the marking phase (the
2369 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002370 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002371 __ LoadFromOffset(kLoadWord, temp, temp,
2372 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2373 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002374 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002375 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002376 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002377 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002378 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002379 // LR = temp->GetEntryPoint();
2380 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2381 // LR();
2382 __ blx(LR);
2383 DCHECK(!codegen_->IsLeafMethod());
2384 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2385}
2386
Orion Hodsonac141392017-01-13 11:53:47 +00002387void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2388 HandleInvoke(invoke);
2389}
2390
2391void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2392 codegen_->GenerateInvokePolymorphicCall(invoke);
2393}
2394
Roland Levillain88cb1752014-10-20 16:36:47 +01002395void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2396 LocationSummary* locations =
2397 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2398 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002399 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002400 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002401 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2402 break;
2403 }
2404 case Primitive::kPrimLong: {
2405 locations->SetInAt(0, Location::RequiresRegister());
2406 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002407 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002408 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002409
Roland Levillain88cb1752014-10-20 16:36:47 +01002410 case Primitive::kPrimFloat:
2411 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002412 locations->SetInAt(0, Location::RequiresFpuRegister());
2413 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002414 break;
2415
2416 default:
2417 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2418 }
2419}
2420
2421void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2422 LocationSummary* locations = neg->GetLocations();
2423 Location out = locations->Out();
2424 Location in = locations->InAt(0);
2425 switch (neg->GetResultType()) {
2426 case Primitive::kPrimInt:
2427 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002428 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002429 break;
2430
2431 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002432 DCHECK(in.IsRegisterPair());
2433 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2434 __ rsbs(out.AsRegisterPairLow<Register>(),
2435 in.AsRegisterPairLow<Register>(),
2436 ShifterOperand(0));
2437 // We cannot emit an RSC (Reverse Subtract with Carry)
2438 // instruction here, as it does not exist in the Thumb-2
2439 // instruction set. We use the following approach
2440 // using SBC and SUB instead.
2441 //
2442 // out.hi = -C
2443 __ sbc(out.AsRegisterPairHigh<Register>(),
2444 out.AsRegisterPairHigh<Register>(),
2445 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2446 // out.hi = out.hi - in.hi
2447 __ sub(out.AsRegisterPairHigh<Register>(),
2448 out.AsRegisterPairHigh<Register>(),
2449 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2450 break;
2451
Roland Levillain88cb1752014-10-20 16:36:47 +01002452 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002453 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002454 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002455 break;
2456
Roland Levillain88cb1752014-10-20 16:36:47 +01002457 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002458 DCHECK(in.IsFpuRegisterPair());
2459 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2460 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002461 break;
2462
2463 default:
2464 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2465 }
2466}
2467
Roland Levillaindff1f282014-11-05 14:15:05 +00002468void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002469 Primitive::Type result_type = conversion->GetResultType();
2470 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002471 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002472
Roland Levillain5b3ee562015-04-14 16:02:41 +01002473 // The float-to-long, double-to-long and long-to-float type conversions
2474 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002475 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002476 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2477 && result_type == Primitive::kPrimLong)
2478 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002479 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002480 : LocationSummary::kNoCall;
2481 LocationSummary* locations =
2482 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2483
David Brazdilb2bd1c52015-03-25 11:17:37 +00002484 // The Java language does not allow treating boolean as an integral type but
2485 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002486
Roland Levillaindff1f282014-11-05 14:15:05 +00002487 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002488 case Primitive::kPrimByte:
2489 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002490 case Primitive::kPrimLong:
2491 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002492 case Primitive::kPrimBoolean:
2493 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002494 case Primitive::kPrimShort:
2495 case Primitive::kPrimInt:
2496 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002497 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002498 locations->SetInAt(0, Location::RequiresRegister());
2499 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2500 break;
2501
2502 default:
2503 LOG(FATAL) << "Unexpected type conversion from " << input_type
2504 << " to " << result_type;
2505 }
2506 break;
2507
Roland Levillain01a8d712014-11-14 16:27:39 +00002508 case Primitive::kPrimShort:
2509 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002510 case Primitive::kPrimLong:
2511 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002512 case Primitive::kPrimBoolean:
2513 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002514 case Primitive::kPrimByte:
2515 case Primitive::kPrimInt:
2516 case Primitive::kPrimChar:
2517 // Processing a Dex `int-to-short' instruction.
2518 locations->SetInAt(0, Location::RequiresRegister());
2519 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2520 break;
2521
2522 default:
2523 LOG(FATAL) << "Unexpected type conversion from " << input_type
2524 << " to " << result_type;
2525 }
2526 break;
2527
Roland Levillain946e1432014-11-11 17:35:19 +00002528 case Primitive::kPrimInt:
2529 switch (input_type) {
2530 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002531 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002532 locations->SetInAt(0, Location::Any());
2533 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2534 break;
2535
2536 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002537 // Processing a Dex `float-to-int' instruction.
2538 locations->SetInAt(0, Location::RequiresFpuRegister());
2539 locations->SetOut(Location::RequiresRegister());
2540 locations->AddTemp(Location::RequiresFpuRegister());
2541 break;
2542
Roland Levillain946e1432014-11-11 17:35:19 +00002543 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002544 // Processing a Dex `double-to-int' instruction.
2545 locations->SetInAt(0, Location::RequiresFpuRegister());
2546 locations->SetOut(Location::RequiresRegister());
2547 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002548 break;
2549
2550 default:
2551 LOG(FATAL) << "Unexpected type conversion from " << input_type
2552 << " to " << result_type;
2553 }
2554 break;
2555
Roland Levillaindff1f282014-11-05 14:15:05 +00002556 case Primitive::kPrimLong:
2557 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002558 case Primitive::kPrimBoolean:
2559 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002560 case Primitive::kPrimByte:
2561 case Primitive::kPrimShort:
2562 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002563 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002564 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002565 locations->SetInAt(0, Location::RequiresRegister());
2566 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2567 break;
2568
Roland Levillain624279f2014-12-04 11:54:28 +00002569 case Primitive::kPrimFloat: {
2570 // Processing a Dex `float-to-long' instruction.
2571 InvokeRuntimeCallingConvention calling_convention;
2572 locations->SetInAt(0, Location::FpuRegisterLocation(
2573 calling_convention.GetFpuRegisterAt(0)));
2574 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2575 break;
2576 }
2577
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002578 case Primitive::kPrimDouble: {
2579 // Processing a Dex `double-to-long' instruction.
2580 InvokeRuntimeCallingConvention calling_convention;
2581 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2582 calling_convention.GetFpuRegisterAt(0),
2583 calling_convention.GetFpuRegisterAt(1)));
2584 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002585 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002586 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002587
2588 default:
2589 LOG(FATAL) << "Unexpected type conversion from " << input_type
2590 << " to " << result_type;
2591 }
2592 break;
2593
Roland Levillain981e4542014-11-14 11:47:14 +00002594 case Primitive::kPrimChar:
2595 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002596 case Primitive::kPrimLong:
2597 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002598 case Primitive::kPrimBoolean:
2599 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002600 case Primitive::kPrimByte:
2601 case Primitive::kPrimShort:
2602 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002603 // Processing a Dex `int-to-char' instruction.
2604 locations->SetInAt(0, Location::RequiresRegister());
2605 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2606 break;
2607
2608 default:
2609 LOG(FATAL) << "Unexpected type conversion from " << input_type
2610 << " to " << result_type;
2611 }
2612 break;
2613
Roland Levillaindff1f282014-11-05 14:15:05 +00002614 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002615 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002616 case Primitive::kPrimBoolean:
2617 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002618 case Primitive::kPrimByte:
2619 case Primitive::kPrimShort:
2620 case Primitive::kPrimInt:
2621 case Primitive::kPrimChar:
2622 // Processing a Dex `int-to-float' instruction.
2623 locations->SetInAt(0, Location::RequiresRegister());
2624 locations->SetOut(Location::RequiresFpuRegister());
2625 break;
2626
Roland Levillain5b3ee562015-04-14 16:02:41 +01002627 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002628 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002629 InvokeRuntimeCallingConvention calling_convention;
2630 locations->SetInAt(0, Location::RegisterPairLocation(
2631 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2632 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002633 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002634 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002635
Roland Levillaincff13742014-11-17 14:32:17 +00002636 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002637 // Processing a Dex `double-to-float' instruction.
2638 locations->SetInAt(0, Location::RequiresFpuRegister());
2639 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002640 break;
2641
2642 default:
2643 LOG(FATAL) << "Unexpected type conversion from " << input_type
2644 << " to " << result_type;
2645 };
2646 break;
2647
Roland Levillaindff1f282014-11-05 14:15:05 +00002648 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002649 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002650 case Primitive::kPrimBoolean:
2651 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002652 case Primitive::kPrimByte:
2653 case Primitive::kPrimShort:
2654 case Primitive::kPrimInt:
2655 case Primitive::kPrimChar:
2656 // Processing a Dex `int-to-double' instruction.
2657 locations->SetInAt(0, Location::RequiresRegister());
2658 locations->SetOut(Location::RequiresFpuRegister());
2659 break;
2660
2661 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002662 // Processing a Dex `long-to-double' instruction.
2663 locations->SetInAt(0, Location::RequiresRegister());
2664 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002665 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002666 locations->AddTemp(Location::RequiresFpuRegister());
2667 break;
2668
Roland Levillaincff13742014-11-17 14:32:17 +00002669 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002670 // Processing a Dex `float-to-double' instruction.
2671 locations->SetInAt(0, Location::RequiresFpuRegister());
2672 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002673 break;
2674
2675 default:
2676 LOG(FATAL) << "Unexpected type conversion from " << input_type
2677 << " to " << result_type;
2678 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002679 break;
2680
2681 default:
2682 LOG(FATAL) << "Unexpected type conversion from " << input_type
2683 << " to " << result_type;
2684 }
2685}
2686
2687void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2688 LocationSummary* locations = conversion->GetLocations();
2689 Location out = locations->Out();
2690 Location in = locations->InAt(0);
2691 Primitive::Type result_type = conversion->GetResultType();
2692 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002693 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002694 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002695 case Primitive::kPrimByte:
2696 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002697 case Primitive::kPrimLong:
2698 // Type conversion from long to byte is a result of code transformations.
2699 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2700 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002701 case Primitive::kPrimBoolean:
2702 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002703 case Primitive::kPrimShort:
2704 case Primitive::kPrimInt:
2705 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002706 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002707 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002708 break;
2709
2710 default:
2711 LOG(FATAL) << "Unexpected type conversion from " << input_type
2712 << " to " << result_type;
2713 }
2714 break;
2715
Roland Levillain01a8d712014-11-14 16:27:39 +00002716 case Primitive::kPrimShort:
2717 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002718 case Primitive::kPrimLong:
2719 // Type conversion from long to short is a result of code transformations.
2720 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2721 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002722 case Primitive::kPrimBoolean:
2723 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002724 case Primitive::kPrimByte:
2725 case Primitive::kPrimInt:
2726 case Primitive::kPrimChar:
2727 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002728 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002729 break;
2730
2731 default:
2732 LOG(FATAL) << "Unexpected type conversion from " << input_type
2733 << " to " << result_type;
2734 }
2735 break;
2736
Roland Levillain946e1432014-11-11 17:35:19 +00002737 case Primitive::kPrimInt:
2738 switch (input_type) {
2739 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002740 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002741 DCHECK(out.IsRegister());
2742 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002743 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002744 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002745 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002746 } else {
2747 DCHECK(in.IsConstant());
2748 DCHECK(in.GetConstant()->IsLongConstant());
2749 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002750 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002751 }
2752 break;
2753
Roland Levillain3f8f9362014-12-02 17:45:01 +00002754 case Primitive::kPrimFloat: {
2755 // Processing a Dex `float-to-int' instruction.
2756 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002757 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002758 __ vmovrs(out.AsRegister<Register>(), temp);
2759 break;
2760 }
2761
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002762 case Primitive::kPrimDouble: {
2763 // Processing a Dex `double-to-int' instruction.
2764 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002765 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002766 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002767 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002768 }
Roland Levillain946e1432014-11-11 17:35:19 +00002769
2770 default:
2771 LOG(FATAL) << "Unexpected type conversion from " << input_type
2772 << " to " << result_type;
2773 }
2774 break;
2775
Roland Levillaindff1f282014-11-05 14:15:05 +00002776 case Primitive::kPrimLong:
2777 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002778 case Primitive::kPrimBoolean:
2779 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002780 case Primitive::kPrimByte:
2781 case Primitive::kPrimShort:
2782 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002783 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002784 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002785 DCHECK(out.IsRegisterPair());
2786 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002787 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002788 // Sign extension.
2789 __ Asr(out.AsRegisterPairHigh<Register>(),
2790 out.AsRegisterPairLow<Register>(),
2791 31);
2792 break;
2793
2794 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002795 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002796 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002797 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002798 break;
2799
Roland Levillaindff1f282014-11-05 14:15:05 +00002800 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002801 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002802 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002803 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002804 break;
2805
2806 default:
2807 LOG(FATAL) << "Unexpected type conversion from " << input_type
2808 << " to " << result_type;
2809 }
2810 break;
2811
Roland Levillain981e4542014-11-14 11:47:14 +00002812 case Primitive::kPrimChar:
2813 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002814 case Primitive::kPrimLong:
2815 // Type conversion from long to char is a result of code transformations.
2816 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2817 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002818 case Primitive::kPrimBoolean:
2819 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002820 case Primitive::kPrimByte:
2821 case Primitive::kPrimShort:
2822 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002823 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002824 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002825 break;
2826
2827 default:
2828 LOG(FATAL) << "Unexpected type conversion from " << input_type
2829 << " to " << result_type;
2830 }
2831 break;
2832
Roland Levillaindff1f282014-11-05 14:15:05 +00002833 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002834 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002835 case Primitive::kPrimBoolean:
2836 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002837 case Primitive::kPrimByte:
2838 case Primitive::kPrimShort:
2839 case Primitive::kPrimInt:
2840 case Primitive::kPrimChar: {
2841 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002842 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2843 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002844 break;
2845 }
2846
Roland Levillain5b3ee562015-04-14 16:02:41 +01002847 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002848 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002849 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002850 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002851 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002852
Roland Levillaincff13742014-11-17 14:32:17 +00002853 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002854 // Processing a Dex `double-to-float' instruction.
2855 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2856 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002857 break;
2858
2859 default:
2860 LOG(FATAL) << "Unexpected type conversion from " << input_type
2861 << " to " << result_type;
2862 };
2863 break;
2864
Roland Levillaindff1f282014-11-05 14:15:05 +00002865 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002866 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002867 case Primitive::kPrimBoolean:
2868 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002869 case Primitive::kPrimByte:
2870 case Primitive::kPrimShort:
2871 case Primitive::kPrimInt:
2872 case Primitive::kPrimChar: {
2873 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002874 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002875 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2876 out.AsFpuRegisterPairLow<SRegister>());
2877 break;
2878 }
2879
Roland Levillain647b9ed2014-11-27 12:06:00 +00002880 case Primitive::kPrimLong: {
2881 // Processing a Dex `long-to-double' instruction.
2882 Register low = in.AsRegisterPairLow<Register>();
2883 Register high = in.AsRegisterPairHigh<Register>();
2884 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2885 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002886 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002887 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002888 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2889 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002890
Roland Levillain682393c2015-04-14 15:57:52 +01002891 // temp_d = int-to-double(high)
2892 __ vmovsr(temp_s, high);
2893 __ vcvtdi(temp_d, temp_s);
2894 // constant_d = k2Pow32EncodingForDouble
2895 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2896 // out_d = unsigned-to-double(low)
2897 __ vmovsr(out_s, low);
2898 __ vcvtdu(out_d, out_s);
2899 // out_d += temp_d * constant_d
2900 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002901 break;
2902 }
2903
Roland Levillaincff13742014-11-17 14:32:17 +00002904 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002905 // Processing a Dex `float-to-double' instruction.
2906 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2907 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002908 break;
2909
2910 default:
2911 LOG(FATAL) << "Unexpected type conversion from " << input_type
2912 << " to " << result_type;
2913 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002914 break;
2915
2916 default:
2917 LOG(FATAL) << "Unexpected type conversion from " << input_type
2918 << " to " << result_type;
2919 }
2920}
2921
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002922void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002923 LocationSummary* locations =
2924 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002925 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002926 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002927 locations->SetInAt(0, Location::RequiresRegister());
2928 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002929 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2930 break;
2931 }
2932
2933 case Primitive::kPrimLong: {
2934 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002935 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002936 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002937 break;
2938 }
2939
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002940 case Primitive::kPrimFloat:
2941 case Primitive::kPrimDouble: {
2942 locations->SetInAt(0, Location::RequiresFpuRegister());
2943 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002944 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002945 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002946 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002947
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002948 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002949 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002950 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002951}
2952
2953void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2954 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002955 Location out = locations->Out();
2956 Location first = locations->InAt(0);
2957 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002958 switch (add->GetResultType()) {
2959 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002960 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002961 __ add(out.AsRegister<Register>(),
2962 first.AsRegister<Register>(),
2963 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002964 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002965 __ AddConstant(out.AsRegister<Register>(),
2966 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002967 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002968 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002969 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002970
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002971 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002972 if (second.IsConstant()) {
2973 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2974 GenerateAddLongConst(out, first, value);
2975 } else {
2976 DCHECK(second.IsRegisterPair());
2977 __ adds(out.AsRegisterPairLow<Register>(),
2978 first.AsRegisterPairLow<Register>(),
2979 ShifterOperand(second.AsRegisterPairLow<Register>()));
2980 __ adc(out.AsRegisterPairHigh<Register>(),
2981 first.AsRegisterPairHigh<Register>(),
2982 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2983 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002984 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002985 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002986
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002987 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002988 __ vadds(out.AsFpuRegister<SRegister>(),
2989 first.AsFpuRegister<SRegister>(),
2990 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002991 break;
2992
2993 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002994 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2995 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2996 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002997 break;
2998
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002999 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003000 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003001 }
3002}
3003
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003004void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003005 LocationSummary* locations =
3006 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003007 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003008 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003009 locations->SetInAt(0, Location::RequiresRegister());
3010 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003011 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3012 break;
3013 }
3014
3015 case Primitive::kPrimLong: {
3016 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003017 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003018 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003019 break;
3020 }
Calin Juravle11351682014-10-23 15:38:15 +01003021 case Primitive::kPrimFloat:
3022 case Primitive::kPrimDouble: {
3023 locations->SetInAt(0, Location::RequiresFpuRegister());
3024 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003025 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003026 break;
Calin Juravle11351682014-10-23 15:38:15 +01003027 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003028 default:
Calin Juravle11351682014-10-23 15:38:15 +01003029 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003030 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003031}
3032
3033void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3034 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003035 Location out = locations->Out();
3036 Location first = locations->InAt(0);
3037 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003038 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003039 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003040 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003041 __ sub(out.AsRegister<Register>(),
3042 first.AsRegister<Register>(),
3043 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003044 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003045 __ AddConstant(out.AsRegister<Register>(),
3046 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003047 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003048 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003049 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003050 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003051
Calin Juravle11351682014-10-23 15:38:15 +01003052 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003053 if (second.IsConstant()) {
3054 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3055 GenerateAddLongConst(out, first, -value);
3056 } else {
3057 DCHECK(second.IsRegisterPair());
3058 __ subs(out.AsRegisterPairLow<Register>(),
3059 first.AsRegisterPairLow<Register>(),
3060 ShifterOperand(second.AsRegisterPairLow<Register>()));
3061 __ sbc(out.AsRegisterPairHigh<Register>(),
3062 first.AsRegisterPairHigh<Register>(),
3063 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3064 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003065 break;
Calin Juravle11351682014-10-23 15:38:15 +01003066 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003067
Calin Juravle11351682014-10-23 15:38:15 +01003068 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003069 __ vsubs(out.AsFpuRegister<SRegister>(),
3070 first.AsFpuRegister<SRegister>(),
3071 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003072 break;
Calin Juravle11351682014-10-23 15:38:15 +01003073 }
3074
3075 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003076 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3077 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3078 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003079 break;
3080 }
3081
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003082
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003083 default:
Calin Juravle11351682014-10-23 15:38:15 +01003084 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003085 }
3086}
3087
Calin Juravle34bacdf2014-10-07 20:23:36 +01003088void LocationsBuilderARM::VisitMul(HMul* mul) {
3089 LocationSummary* locations =
3090 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3091 switch (mul->GetResultType()) {
3092 case Primitive::kPrimInt:
3093 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003094 locations->SetInAt(0, Location::RequiresRegister());
3095 locations->SetInAt(1, Location::RequiresRegister());
3096 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003097 break;
3098 }
3099
Calin Juravleb5bfa962014-10-21 18:02:24 +01003100 case Primitive::kPrimFloat:
3101 case Primitive::kPrimDouble: {
3102 locations->SetInAt(0, Location::RequiresFpuRegister());
3103 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003104 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003105 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003106 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003107
3108 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003109 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003110 }
3111}
3112
3113void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3114 LocationSummary* locations = mul->GetLocations();
3115 Location out = locations->Out();
3116 Location first = locations->InAt(0);
3117 Location second = locations->InAt(1);
3118 switch (mul->GetResultType()) {
3119 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003120 __ mul(out.AsRegister<Register>(),
3121 first.AsRegister<Register>(),
3122 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003123 break;
3124 }
3125 case Primitive::kPrimLong: {
3126 Register out_hi = out.AsRegisterPairHigh<Register>();
3127 Register out_lo = out.AsRegisterPairLow<Register>();
3128 Register in1_hi = first.AsRegisterPairHigh<Register>();
3129 Register in1_lo = first.AsRegisterPairLow<Register>();
3130 Register in2_hi = second.AsRegisterPairHigh<Register>();
3131 Register in2_lo = second.AsRegisterPairLow<Register>();
3132
3133 // Extra checks to protect caused by the existence of R1_R2.
3134 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3135 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3136 DCHECK_NE(out_hi, in1_lo);
3137 DCHECK_NE(out_hi, in2_lo);
3138
3139 // input: in1 - 64 bits, in2 - 64 bits
3140 // output: out
3141 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3142 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3143 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3144
3145 // IP <- in1.lo * in2.hi
3146 __ mul(IP, in1_lo, in2_hi);
3147 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3148 __ mla(out_hi, in1_hi, in2_lo, IP);
3149 // out.lo <- (in1.lo * in2.lo)[31:0];
3150 __ umull(out_lo, IP, in1_lo, in2_lo);
3151 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3152 __ add(out_hi, out_hi, ShifterOperand(IP));
3153 break;
3154 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003155
3156 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003157 __ vmuls(out.AsFpuRegister<SRegister>(),
3158 first.AsFpuRegister<SRegister>(),
3159 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003160 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003161 }
3162
3163 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003164 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3165 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3166 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003167 break;
3168 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003169
3170 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003171 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003172 }
3173}
3174
Zheng Xuc6667102015-05-15 16:08:45 +08003175void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3176 DCHECK(instruction->IsDiv() || instruction->IsRem());
3177 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3178
3179 LocationSummary* locations = instruction->GetLocations();
3180 Location second = locations->InAt(1);
3181 DCHECK(second.IsConstant());
3182
3183 Register out = locations->Out().AsRegister<Register>();
3184 Register dividend = locations->InAt(0).AsRegister<Register>();
3185 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3186 DCHECK(imm == 1 || imm == -1);
3187
3188 if (instruction->IsRem()) {
3189 __ LoadImmediate(out, 0);
3190 } else {
3191 if (imm == 1) {
3192 __ Mov(out, dividend);
3193 } else {
3194 __ rsb(out, dividend, ShifterOperand(0));
3195 }
3196 }
3197}
3198
3199void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(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 Register temp = locations->GetTemp(0).AsRegister<Register>();
3210 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003211 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003212 int ctz_imm = CTZ(abs_imm);
3213
3214 if (ctz_imm == 1) {
3215 __ Lsr(temp, dividend, 32 - ctz_imm);
3216 } else {
3217 __ Asr(temp, dividend, 31);
3218 __ Lsr(temp, temp, 32 - ctz_imm);
3219 }
3220 __ add(out, temp, ShifterOperand(dividend));
3221
3222 if (instruction->IsDiv()) {
3223 __ Asr(out, out, ctz_imm);
3224 if (imm < 0) {
3225 __ rsb(out, out, ShifterOperand(0));
3226 }
3227 } else {
3228 __ ubfx(out, out, 0, ctz_imm);
3229 __ sub(out, out, ShifterOperand(temp));
3230 }
3231}
3232
3233void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3234 DCHECK(instruction->IsDiv() || instruction->IsRem());
3235 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3236
3237 LocationSummary* locations = instruction->GetLocations();
3238 Location second = locations->InAt(1);
3239 DCHECK(second.IsConstant());
3240
3241 Register out = locations->Out().AsRegister<Register>();
3242 Register dividend = locations->InAt(0).AsRegister<Register>();
3243 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3244 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3245 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3246
3247 int64_t magic;
3248 int shift;
3249 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3250
3251 __ LoadImmediate(temp1, magic);
3252 __ smull(temp2, temp1, dividend, temp1);
3253
3254 if (imm > 0 && magic < 0) {
3255 __ add(temp1, temp1, ShifterOperand(dividend));
3256 } else if (imm < 0 && magic > 0) {
3257 __ sub(temp1, temp1, ShifterOperand(dividend));
3258 }
3259
3260 if (shift != 0) {
3261 __ Asr(temp1, temp1, shift);
3262 }
3263
3264 if (instruction->IsDiv()) {
3265 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3266 } else {
3267 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3268 // TODO: Strength reduction for mls.
3269 __ LoadImmediate(temp2, imm);
3270 __ mls(out, temp1, temp2, dividend);
3271 }
3272}
3273
3274void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3275 DCHECK(instruction->IsDiv() || instruction->IsRem());
3276 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3277
3278 LocationSummary* locations = instruction->GetLocations();
3279 Location second = locations->InAt(1);
3280 DCHECK(second.IsConstant());
3281
3282 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3283 if (imm == 0) {
3284 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3285 } else if (imm == 1 || imm == -1) {
3286 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003287 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003288 DivRemByPowerOfTwo(instruction);
3289 } else {
3290 DCHECK(imm <= -2 || imm >= 2);
3291 GenerateDivRemWithAnyConstant(instruction);
3292 }
3293}
3294
Calin Juravle7c4954d2014-10-28 16:57:40 +00003295void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003296 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3297 if (div->GetResultType() == Primitive::kPrimLong) {
3298 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003299 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003300 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3301 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003302 } else if (div->GetResultType() == Primitive::kPrimInt &&
3303 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3304 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003305 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003306 }
3307
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003308 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3309
Calin Juravle7c4954d2014-10-28 16:57:40 +00003310 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003311 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003312 if (div->InputAt(1)->IsConstant()) {
3313 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003314 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003315 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003316 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3317 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003318 // No temp register required.
3319 } else {
3320 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003321 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003322 locations->AddTemp(Location::RequiresRegister());
3323 }
3324 }
3325 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003326 locations->SetInAt(0, Location::RequiresRegister());
3327 locations->SetInAt(1, Location::RequiresRegister());
3328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3329 } else {
3330 InvokeRuntimeCallingConvention calling_convention;
3331 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3332 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3333 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3334 // we only need the former.
3335 locations->SetOut(Location::RegisterLocation(R0));
3336 }
Calin Juravled0d48522014-11-04 16:40:20 +00003337 break;
3338 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003339 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003340 InvokeRuntimeCallingConvention calling_convention;
3341 locations->SetInAt(0, Location::RegisterPairLocation(
3342 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3343 locations->SetInAt(1, Location::RegisterPairLocation(
3344 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003345 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003346 break;
3347 }
3348 case Primitive::kPrimFloat:
3349 case Primitive::kPrimDouble: {
3350 locations->SetInAt(0, Location::RequiresFpuRegister());
3351 locations->SetInAt(1, Location::RequiresFpuRegister());
3352 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3353 break;
3354 }
3355
3356 default:
3357 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3358 }
3359}
3360
3361void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3362 LocationSummary* locations = div->GetLocations();
3363 Location out = locations->Out();
3364 Location first = locations->InAt(0);
3365 Location second = locations->InAt(1);
3366
3367 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003368 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003369 if (second.IsConstant()) {
3370 GenerateDivRemConstantIntegral(div);
3371 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003372 __ sdiv(out.AsRegister<Register>(),
3373 first.AsRegister<Register>(),
3374 second.AsRegister<Register>());
3375 } else {
3376 InvokeRuntimeCallingConvention calling_convention;
3377 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3378 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3379 DCHECK_EQ(R0, out.AsRegister<Register>());
3380
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003381 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003382 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003383 }
Calin Juravled0d48522014-11-04 16:40:20 +00003384 break;
3385 }
3386
Calin Juravle7c4954d2014-10-28 16:57:40 +00003387 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003388 InvokeRuntimeCallingConvention calling_convention;
3389 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3390 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3391 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3392 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3393 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003394 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003395
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003396 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003397 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003398 break;
3399 }
3400
3401 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003402 __ vdivs(out.AsFpuRegister<SRegister>(),
3403 first.AsFpuRegister<SRegister>(),
3404 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003405 break;
3406 }
3407
3408 case Primitive::kPrimDouble: {
3409 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3410 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3411 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3412 break;
3413 }
3414
3415 default:
3416 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3417 }
3418}
3419
Calin Juravlebacfec32014-11-14 15:54:36 +00003420void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003421 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003422
3423 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003424 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003425 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3426 // sdiv will be replaced by other instruction sequence.
3427 call_kind = LocationSummary::kNoCall;
3428 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3429 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003430 // Have hardware divide instruction for int, do it with three instructions.
3431 call_kind = LocationSummary::kNoCall;
3432 }
3433
Calin Juravlebacfec32014-11-14 15:54:36 +00003434 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3435
Calin Juravled2ec87d2014-12-08 14:24:46 +00003436 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003437 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003438 if (rem->InputAt(1)->IsConstant()) {
3439 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003440 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003441 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003442 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3443 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003444 // No temp register required.
3445 } else {
3446 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003447 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003448 locations->AddTemp(Location::RequiresRegister());
3449 }
3450 }
3451 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003452 locations->SetInAt(0, Location::RequiresRegister());
3453 locations->SetInAt(1, Location::RequiresRegister());
3454 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3455 locations->AddTemp(Location::RequiresRegister());
3456 } else {
3457 InvokeRuntimeCallingConvention calling_convention;
3458 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3459 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3460 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3461 // we only need the latter.
3462 locations->SetOut(Location::RegisterLocation(R1));
3463 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003464 break;
3465 }
3466 case Primitive::kPrimLong: {
3467 InvokeRuntimeCallingConvention calling_convention;
3468 locations->SetInAt(0, Location::RegisterPairLocation(
3469 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3470 locations->SetInAt(1, Location::RegisterPairLocation(
3471 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3472 // The runtime helper puts the output in R2,R3.
3473 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3474 break;
3475 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003476 case Primitive::kPrimFloat: {
3477 InvokeRuntimeCallingConvention calling_convention;
3478 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3479 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3480 locations->SetOut(Location::FpuRegisterLocation(S0));
3481 break;
3482 }
3483
Calin Juravlebacfec32014-11-14 15:54:36 +00003484 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003485 InvokeRuntimeCallingConvention calling_convention;
3486 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3487 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3488 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3489 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3490 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003491 break;
3492 }
3493
3494 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003495 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003496 }
3497}
3498
3499void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3500 LocationSummary* locations = rem->GetLocations();
3501 Location out = locations->Out();
3502 Location first = locations->InAt(0);
3503 Location second = locations->InAt(1);
3504
Calin Juravled2ec87d2014-12-08 14:24:46 +00003505 Primitive::Type type = rem->GetResultType();
3506 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003507 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003508 if (second.IsConstant()) {
3509 GenerateDivRemConstantIntegral(rem);
3510 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003511 Register reg1 = first.AsRegister<Register>();
3512 Register reg2 = second.AsRegister<Register>();
3513 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003514
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003515 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003516 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003517 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003518 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003519 } else {
3520 InvokeRuntimeCallingConvention calling_convention;
3521 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3522 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3523 DCHECK_EQ(R1, out.AsRegister<Register>());
3524
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003525 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003526 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003527 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003528 break;
3529 }
3530
3531 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003532 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003533 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003534 break;
3535 }
3536
Calin Juravled2ec87d2014-12-08 14:24:46 +00003537 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003538 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003539 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003540 break;
3541 }
3542
Calin Juravlebacfec32014-11-14 15:54:36 +00003543 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003544 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003545 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003546 break;
3547 }
3548
3549 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003550 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003551 }
3552}
3553
Calin Juravled0d48522014-11-04 16:40:20 +00003554void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003555 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003556 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003557}
3558
3559void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003560 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003561 codegen_->AddSlowPath(slow_path);
3562
3563 LocationSummary* locations = instruction->GetLocations();
3564 Location value = locations->InAt(0);
3565
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003566 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003567 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003568 case Primitive::kPrimByte:
3569 case Primitive::kPrimChar:
3570 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003571 case Primitive::kPrimInt: {
3572 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003573 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003574 } else {
3575 DCHECK(value.IsConstant()) << value;
3576 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3577 __ b(slow_path->GetEntryLabel());
3578 }
3579 }
3580 break;
3581 }
3582 case Primitive::kPrimLong: {
3583 if (value.IsRegisterPair()) {
3584 __ orrs(IP,
3585 value.AsRegisterPairLow<Register>(),
3586 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3587 __ b(slow_path->GetEntryLabel(), EQ);
3588 } else {
3589 DCHECK(value.IsConstant()) << value;
3590 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3591 __ b(slow_path->GetEntryLabel());
3592 }
3593 }
3594 break;
3595 default:
3596 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3597 }
3598 }
Calin Juravled0d48522014-11-04 16:40:20 +00003599}
3600
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003601void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3602 Register in = locations->InAt(0).AsRegister<Register>();
3603 Location rhs = locations->InAt(1);
3604 Register out = locations->Out().AsRegister<Register>();
3605
3606 if (rhs.IsConstant()) {
3607 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3608 // so map all rotations to a +ve. equivalent in that range.
3609 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3610 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3611 if (rot) {
3612 // Rotate, mapping left rotations to right equivalents if necessary.
3613 // (e.g. left by 2 bits == right by 30.)
3614 __ Ror(out, in, rot);
3615 } else if (out != in) {
3616 __ Mov(out, in);
3617 }
3618 } else {
3619 __ Ror(out, in, rhs.AsRegister<Register>());
3620 }
3621}
3622
3623// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3624// rotates by swapping input regs (effectively rotating by the first 32-bits of
3625// a larger rotation) or flipping direction (thus treating larger right/left
3626// rotations as sub-word sized rotations in the other direction) as appropriate.
3627void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3628 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3629 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3630 Location rhs = locations->InAt(1);
3631 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3632 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3633
3634 if (rhs.IsConstant()) {
3635 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3636 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003637 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003638 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3639 // logic below to a simple pair of binary orr.
3640 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3641 if (rot >= kArmBitsPerWord) {
3642 rot -= kArmBitsPerWord;
3643 std::swap(in_reg_hi, in_reg_lo);
3644 }
3645 // Rotate, or mov to out for zero or word size rotations.
3646 if (rot != 0u) {
3647 __ Lsr(out_reg_hi, in_reg_hi, rot);
3648 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3649 __ Lsr(out_reg_lo, in_reg_lo, rot);
3650 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3651 } else {
3652 __ Mov(out_reg_lo, in_reg_lo);
3653 __ Mov(out_reg_hi, in_reg_hi);
3654 }
3655 } else {
3656 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3657 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3658 Label end;
3659 Label shift_by_32_plus_shift_right;
3660
3661 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3662 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3663 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3664 __ b(&shift_by_32_plus_shift_right, CC);
3665
3666 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3667 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3668 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3669 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3670 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3671 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3672 __ Lsr(shift_left, in_reg_hi, shift_right);
3673 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3674 __ b(&end);
3675
3676 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3677 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3678 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3679 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3680 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3681 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3682 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3683 __ Lsl(shift_right, in_reg_hi, shift_left);
3684 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3685
3686 __ Bind(&end);
3687 }
3688}
Roland Levillain22c49222016-03-18 14:04:28 +00003689
3690void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003691 LocationSummary* locations =
3692 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3693 switch (ror->GetResultType()) {
3694 case Primitive::kPrimInt: {
3695 locations->SetInAt(0, Location::RequiresRegister());
3696 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3697 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3698 break;
3699 }
3700 case Primitive::kPrimLong: {
3701 locations->SetInAt(0, Location::RequiresRegister());
3702 if (ror->InputAt(1)->IsConstant()) {
3703 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3704 } else {
3705 locations->SetInAt(1, Location::RequiresRegister());
3706 locations->AddTemp(Location::RequiresRegister());
3707 locations->AddTemp(Location::RequiresRegister());
3708 }
3709 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3710 break;
3711 }
3712 default:
3713 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3714 }
3715}
3716
Roland Levillain22c49222016-03-18 14:04:28 +00003717void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003718 LocationSummary* locations = ror->GetLocations();
3719 Primitive::Type type = ror->GetResultType();
3720 switch (type) {
3721 case Primitive::kPrimInt: {
3722 HandleIntegerRotate(locations);
3723 break;
3724 }
3725 case Primitive::kPrimLong: {
3726 HandleLongRotate(locations);
3727 break;
3728 }
3729 default:
3730 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003731 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003732 }
3733}
3734
Calin Juravle9aec02f2014-11-18 23:06:35 +00003735void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3736 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3737
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003738 LocationSummary* locations =
3739 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003740
3741 switch (op->GetResultType()) {
3742 case Primitive::kPrimInt: {
3743 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003744 if (op->InputAt(1)->IsConstant()) {
3745 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3746 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3747 } else {
3748 locations->SetInAt(1, Location::RequiresRegister());
3749 // Make the output overlap, as it will be used to hold the masked
3750 // second input.
3751 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3752 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003753 break;
3754 }
3755 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003756 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003757 if (op->InputAt(1)->IsConstant()) {
3758 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3759 // For simplicity, use kOutputOverlap even though we only require that low registers
3760 // don't clash with high registers which the register allocator currently guarantees.
3761 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3762 } else {
3763 locations->SetInAt(1, Location::RequiresRegister());
3764 locations->AddTemp(Location::RequiresRegister());
3765 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3766 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003767 break;
3768 }
3769 default:
3770 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3771 }
3772}
3773
3774void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3775 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3776
3777 LocationSummary* locations = op->GetLocations();
3778 Location out = locations->Out();
3779 Location first = locations->InAt(0);
3780 Location second = locations->InAt(1);
3781
3782 Primitive::Type type = op->GetResultType();
3783 switch (type) {
3784 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003785 Register out_reg = out.AsRegister<Register>();
3786 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003787 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003788 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003789 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003790 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003791 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003792 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003793 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003794 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003795 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003796 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003797 }
3798 } else {
3799 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003800 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003801 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003802 __ Mov(out_reg, first_reg);
3803 } else if (op->IsShl()) {
3804 __ Lsl(out_reg, first_reg, shift_value);
3805 } else if (op->IsShr()) {
3806 __ Asr(out_reg, first_reg, shift_value);
3807 } else {
3808 __ Lsr(out_reg, first_reg, shift_value);
3809 }
3810 }
3811 break;
3812 }
3813 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003814 Register o_h = out.AsRegisterPairHigh<Register>();
3815 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003816
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003817 Register high = first.AsRegisterPairHigh<Register>();
3818 Register low = first.AsRegisterPairLow<Register>();
3819
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003820 if (second.IsRegister()) {
3821 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003822
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003823 Register second_reg = second.AsRegister<Register>();
3824
3825 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003826 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003827 // Shift the high part
3828 __ Lsl(o_h, high, o_l);
3829 // Shift the low part and `or` what overflew on the high part
3830 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3831 __ Lsr(temp, low, temp);
3832 __ orr(o_h, o_h, ShifterOperand(temp));
3833 // If the shift is > 32 bits, override the high part
3834 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3835 __ it(PL);
3836 __ Lsl(o_h, low, temp, PL);
3837 // Shift the low part
3838 __ Lsl(o_l, low, o_l);
3839 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003840 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003841 // Shift the low part
3842 __ Lsr(o_l, low, o_h);
3843 // Shift the high part and `or` what underflew on the low part
3844 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3845 __ Lsl(temp, high, temp);
3846 __ orr(o_l, o_l, ShifterOperand(temp));
3847 // If the shift is > 32 bits, override the low part
3848 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3849 __ it(PL);
3850 __ Asr(o_l, high, temp, PL);
3851 // Shift the high part
3852 __ Asr(o_h, high, o_h);
3853 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003854 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003855 // same as Shr except we use `Lsr`s and not `Asr`s
3856 __ Lsr(o_l, low, o_h);
3857 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3858 __ Lsl(temp, high, temp);
3859 __ orr(o_l, o_l, ShifterOperand(temp));
3860 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3861 __ it(PL);
3862 __ Lsr(o_l, high, temp, PL);
3863 __ Lsr(o_h, high, o_h);
3864 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003865 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003866 // Register allocator doesn't create partial overlap.
3867 DCHECK_NE(o_l, high);
3868 DCHECK_NE(o_h, low);
3869 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003870 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003871 if (shift_value > 32) {
3872 if (op->IsShl()) {
3873 __ Lsl(o_h, low, shift_value - 32);
3874 __ LoadImmediate(o_l, 0);
3875 } else if (op->IsShr()) {
3876 __ Asr(o_l, high, shift_value - 32);
3877 __ Asr(o_h, high, 31);
3878 } else {
3879 __ Lsr(o_l, high, shift_value - 32);
3880 __ LoadImmediate(o_h, 0);
3881 }
3882 } else if (shift_value == 32) {
3883 if (op->IsShl()) {
3884 __ mov(o_h, ShifterOperand(low));
3885 __ LoadImmediate(o_l, 0);
3886 } else if (op->IsShr()) {
3887 __ mov(o_l, ShifterOperand(high));
3888 __ Asr(o_h, high, 31);
3889 } else {
3890 __ mov(o_l, ShifterOperand(high));
3891 __ LoadImmediate(o_h, 0);
3892 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003893 } else if (shift_value == 1) {
3894 if (op->IsShl()) {
3895 __ Lsls(o_l, low, 1);
3896 __ adc(o_h, high, ShifterOperand(high));
3897 } else if (op->IsShr()) {
3898 __ Asrs(o_h, high, 1);
3899 __ Rrx(o_l, low);
3900 } else {
3901 __ Lsrs(o_h, high, 1);
3902 __ Rrx(o_l, low);
3903 }
3904 } else {
3905 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003906 if (op->IsShl()) {
3907 __ Lsl(o_h, high, shift_value);
3908 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3909 __ Lsl(o_l, low, shift_value);
3910 } else if (op->IsShr()) {
3911 __ Lsr(o_l, low, shift_value);
3912 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3913 __ Asr(o_h, high, shift_value);
3914 } else {
3915 __ Lsr(o_l, low, shift_value);
3916 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3917 __ Lsr(o_h, high, shift_value);
3918 }
3919 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003920 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003921 break;
3922 }
3923 default:
3924 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003925 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003926 }
3927}
3928
3929void LocationsBuilderARM::VisitShl(HShl* shl) {
3930 HandleShift(shl);
3931}
3932
3933void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3934 HandleShift(shl);
3935}
3936
3937void LocationsBuilderARM::VisitShr(HShr* shr) {
3938 HandleShift(shr);
3939}
3940
3941void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3942 HandleShift(shr);
3943}
3944
3945void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3946 HandleShift(ushr);
3947}
3948
3949void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3950 HandleShift(ushr);
3951}
3952
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003953void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003954 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003955 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003956 if (instruction->IsStringAlloc()) {
3957 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3958 } else {
3959 InvokeRuntimeCallingConvention calling_convention;
3960 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00003961 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003962 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003963}
3964
3965void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003966 // Note: if heap poisoning is enabled, the entry point takes cares
3967 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003968 if (instruction->IsStringAlloc()) {
3969 // String is allocated through StringFactory. Call NewEmptyString entry point.
3970 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003971 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003972 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3973 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3974 __ blx(LR);
3975 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3976 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003977 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00003978 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00003979 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003980}
3981
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003982void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3983 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003985 InvokeRuntimeCallingConvention calling_convention;
3986 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003987 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003988 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003989 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003990}
3991
3992void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3993 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -08003994 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex().index_);
Roland Levillain4d027112015-07-01 15:41:14 +01003995 // Note: if heap poisoning is enabled, the entry point takes cares
3996 // of poisoning the reference.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003997 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003998 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003999}
4000
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004001void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004002 LocationSummary* locations =
4003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004004 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4005 if (location.IsStackSlot()) {
4006 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4007 } else if (location.IsDoubleStackSlot()) {
4008 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004009 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004010 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004011}
4012
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004013void InstructionCodeGeneratorARM::VisitParameterValue(
4014 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004015 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004016}
4017
4018void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4019 LocationSummary* locations =
4020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4021 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4022}
4023
4024void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4025 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004026}
4027
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004028void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004029 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004030 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004031 locations->SetInAt(0, Location::RequiresRegister());
4032 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004033}
4034
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004035void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4036 LocationSummary* locations = not_->GetLocations();
4037 Location out = locations->Out();
4038 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004039 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004040 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004041 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004042 break;
4043
4044 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004045 __ mvn(out.AsRegisterPairLow<Register>(),
4046 ShifterOperand(in.AsRegisterPairLow<Register>()));
4047 __ mvn(out.AsRegisterPairHigh<Register>(),
4048 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004049 break;
4050
4051 default:
4052 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4053 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004054}
4055
David Brazdil66d126e2015-04-03 16:02:44 +01004056void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4057 LocationSummary* locations =
4058 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4059 locations->SetInAt(0, Location::RequiresRegister());
4060 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4061}
4062
4063void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004064 LocationSummary* locations = bool_not->GetLocations();
4065 Location out = locations->Out();
4066 Location in = locations->InAt(0);
4067 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4068}
4069
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004070void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004071 LocationSummary* locations =
4072 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004073 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004074 case Primitive::kPrimBoolean:
4075 case Primitive::kPrimByte:
4076 case Primitive::kPrimShort:
4077 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004078 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004079 case Primitive::kPrimLong: {
4080 locations->SetInAt(0, Location::RequiresRegister());
4081 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004082 // Output overlaps because it is written before doing the low comparison.
4083 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004084 break;
4085 }
4086 case Primitive::kPrimFloat:
4087 case Primitive::kPrimDouble: {
4088 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004089 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004090 locations->SetOut(Location::RequiresRegister());
4091 break;
4092 }
4093 default:
4094 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4095 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004096}
4097
4098void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004099 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004100 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004101 Location left = locations->InAt(0);
4102 Location right = locations->InAt(1);
4103
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004104 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004105 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004106 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004107 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004108 case Primitive::kPrimBoolean:
4109 case Primitive::kPrimByte:
4110 case Primitive::kPrimShort:
4111 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004112 case Primitive::kPrimInt: {
4113 __ LoadImmediate(out, 0);
4114 __ cmp(left.AsRegister<Register>(),
4115 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4116 less_cond = LT;
4117 break;
4118 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004119 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004120 __ cmp(left.AsRegisterPairHigh<Register>(),
4121 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004122 __ b(&less, LT);
4123 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004124 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004125 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004126 __ cmp(left.AsRegisterPairLow<Register>(),
4127 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004128 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004129 break;
4130 }
4131 case Primitive::kPrimFloat:
4132 case Primitive::kPrimDouble: {
4133 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004134 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004135 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004136 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004137 break;
4138 }
4139 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004140 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004141 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004142 }
Aart Bika19616e2016-02-01 18:57:58 -08004143
Calin Juravleddb7df22014-11-25 20:56:51 +00004144 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004145 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004146
4147 __ Bind(&greater);
4148 __ LoadImmediate(out, 1);
4149 __ b(&done);
4150
4151 __ Bind(&less);
4152 __ LoadImmediate(out, -1);
4153
4154 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004155}
4156
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004157void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004158 LocationSummary* locations =
4159 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004160 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004161 locations->SetInAt(i, Location::Any());
4162 }
4163 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004164}
4165
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004166void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004167 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004168}
4169
Roland Levillainc9285912015-12-18 10:38:42 +00004170void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4171 // TODO (ported from quick): revisit ARM barrier kinds.
4172 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004173 switch (kind) {
4174 case MemBarrierKind::kAnyStore:
4175 case MemBarrierKind::kLoadAny:
4176 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004177 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004178 break;
4179 }
4180 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004181 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004182 break;
4183 }
4184 default:
4185 LOG(FATAL) << "Unexpected memory barrier " << kind;
4186 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004187 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004188}
4189
4190void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4191 uint32_t offset,
4192 Register out_lo,
4193 Register out_hi) {
4194 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004195 // Ensure `out_lo` is different from `addr`, so that loading
4196 // `offset` into `out_lo` does not clutter `addr`.
4197 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004198 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004199 __ add(IP, addr, ShifterOperand(out_lo));
4200 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004201 }
4202 __ ldrexd(out_lo, out_hi, addr);
4203}
4204
4205void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4206 uint32_t offset,
4207 Register value_lo,
4208 Register value_hi,
4209 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004210 Register temp2,
4211 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004212 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004213 if (offset != 0) {
4214 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004215 __ add(IP, addr, ShifterOperand(temp1));
4216 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004217 }
4218 __ Bind(&fail);
4219 // We need a load followed by store. (The address used in a STREX instruction must
4220 // be the same as the address in the most recently executed LDREX instruction.)
4221 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004222 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004223 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004224 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004225}
4226
4227void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4228 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4229
Nicolas Geoffray39468442014-09-02 15:17:15 +01004230 LocationSummary* locations =
4231 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004232 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004233
Calin Juravle52c48962014-12-16 17:02:57 +00004234 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004235 if (Primitive::IsFloatingPointType(field_type)) {
4236 locations->SetInAt(1, Location::RequiresFpuRegister());
4237 } else {
4238 locations->SetInAt(1, Location::RequiresRegister());
4239 }
4240
Calin Juravle52c48962014-12-16 17:02:57 +00004241 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004242 bool generate_volatile = field_info.IsVolatile()
4243 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004244 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004245 bool needs_write_barrier =
4246 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004247 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004248 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004249 if (needs_write_barrier) {
4250 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004251 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004252 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004253 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004254 // - registers need to be consecutive
4255 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004256 // We don't test for ARM yet, and the assertion makes sure that we
4257 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004258 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4259
4260 locations->AddTemp(Location::RequiresRegister());
4261 locations->AddTemp(Location::RequiresRegister());
4262 if (field_type == Primitive::kPrimDouble) {
4263 // For doubles we need two more registers to copy the value.
4264 locations->AddTemp(Location::RegisterLocation(R2));
4265 locations->AddTemp(Location::RegisterLocation(R3));
4266 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004267 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004268}
4269
Calin Juravle52c48962014-12-16 17:02:57 +00004270void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004271 const FieldInfo& field_info,
4272 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004273 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4274
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004275 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004276 Register base = locations->InAt(0).AsRegister<Register>();
4277 Location value = locations->InAt(1);
4278
4279 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004280 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004281 Primitive::Type field_type = field_info.GetFieldType();
4282 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004283 bool needs_write_barrier =
4284 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004285
4286 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004287 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004288 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004289
4290 switch (field_type) {
4291 case Primitive::kPrimBoolean:
4292 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004293 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004294 break;
4295 }
4296
4297 case Primitive::kPrimShort:
4298 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004299 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004300 break;
4301 }
4302
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004303 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004304 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004305 if (kPoisonHeapReferences && needs_write_barrier) {
4306 // Note that in the case where `value` is a null reference,
4307 // we do not enter this block, as a null reference does not
4308 // need poisoning.
4309 DCHECK_EQ(field_type, Primitive::kPrimNot);
4310 Register temp = locations->GetTemp(0).AsRegister<Register>();
4311 __ Mov(temp, value.AsRegister<Register>());
4312 __ PoisonHeapReference(temp);
4313 __ StoreToOffset(kStoreWord, temp, base, offset);
4314 } else {
4315 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4316 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004317 break;
4318 }
4319
4320 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004321 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004322 GenerateWideAtomicStore(base, offset,
4323 value.AsRegisterPairLow<Register>(),
4324 value.AsRegisterPairHigh<Register>(),
4325 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004326 locations->GetTemp(1).AsRegister<Register>(),
4327 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004328 } else {
4329 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004330 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004331 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004332 break;
4333 }
4334
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004335 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004336 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004337 break;
4338 }
4339
4340 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004341 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004342 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004343 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4344 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4345
4346 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4347
4348 GenerateWideAtomicStore(base, offset,
4349 value_reg_lo,
4350 value_reg_hi,
4351 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004352 locations->GetTemp(3).AsRegister<Register>(),
4353 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004354 } else {
4355 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004356 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004357 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004358 break;
4359 }
4360
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004361 case Primitive::kPrimVoid:
4362 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004363 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004364 }
Calin Juravle52c48962014-12-16 17:02:57 +00004365
Calin Juravle77520bc2015-01-12 18:45:46 +00004366 // Longs and doubles are handled in the switch.
4367 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4368 codegen_->MaybeRecordImplicitNullCheck(instruction);
4369 }
4370
4371 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4372 Register temp = locations->GetTemp(0).AsRegister<Register>();
4373 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004374 codegen_->MarkGCCard(
4375 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004376 }
4377
Calin Juravle52c48962014-12-16 17:02:57 +00004378 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004379 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004380 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004381}
4382
Calin Juravle52c48962014-12-16 17:02:57 +00004383void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4384 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004385
4386 bool object_field_get_with_read_barrier =
4387 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004388 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004389 new (GetGraph()->GetArena()) LocationSummary(instruction,
4390 object_field_get_with_read_barrier ?
4391 LocationSummary::kCallOnSlowPath :
4392 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004393 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004394 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004395 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004396 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004397
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004398 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004399 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004400 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004401 // The output overlaps in case of volatile long: we don't want the
4402 // code generated by GenerateWideAtomicLoad to overwrite the
4403 // object's location. Likewise, in the case of an object field get
4404 // with read barriers enabled, we do not want the load to overwrite
4405 // the object's location, as we need it to emit the read barrier.
4406 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4407 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004408
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004409 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4410 locations->SetOut(Location::RequiresFpuRegister());
4411 } else {
4412 locations->SetOut(Location::RequiresRegister(),
4413 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4414 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004415 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004416 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004417 // - registers need to be consecutive
4418 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004419 // We don't test for ARM yet, and the assertion makes sure that we
4420 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004421 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4422 locations->AddTemp(Location::RequiresRegister());
4423 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004424 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4425 // We need a temporary register for the read barrier marking slow
4426 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4427 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004428 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004429}
4430
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004431Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4432 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4433 << input->GetType();
4434 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4435 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4436 return Location::ConstantLocation(input->AsConstant());
4437 } else {
4438 return Location::RequiresFpuRegister();
4439 }
4440}
4441
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004442Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4443 Opcode opcode) {
4444 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4445 if (constant->IsConstant() &&
4446 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4447 return Location::ConstantLocation(constant->AsConstant());
4448 }
4449 return Location::RequiresRegister();
4450}
4451
4452bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4453 Opcode opcode) {
4454 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4455 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004456 Opcode high_opcode = opcode;
4457 SetCc low_set_cc = kCcDontCare;
4458 switch (opcode) {
4459 case SUB:
4460 // Flip the operation to an ADD.
4461 value = -value;
4462 opcode = ADD;
4463 FALLTHROUGH_INTENDED;
4464 case ADD:
4465 if (Low32Bits(value) == 0u) {
4466 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4467 }
4468 high_opcode = ADC;
4469 low_set_cc = kCcSet;
4470 break;
4471 default:
4472 break;
4473 }
4474 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4475 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004476 } else {
4477 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4478 }
4479}
4480
Vladimir Marko59751a72016-08-05 14:37:27 +01004481bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4482 Opcode opcode,
4483 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004484 ShifterOperand so;
4485 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004486 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004487 return true;
4488 }
4489 Opcode neg_opcode = kNoOperand;
4490 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004491 case AND: neg_opcode = BIC; value = ~value; break;
4492 case ORR: neg_opcode = ORN; value = ~value; break;
4493 case ADD: neg_opcode = SUB; value = -value; break;
4494 case ADC: neg_opcode = SBC; value = ~value; break;
4495 case SUB: neg_opcode = ADD; value = -value; break;
4496 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004497 default:
4498 return false;
4499 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004500 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004501}
4502
Calin Juravle52c48962014-12-16 17:02:57 +00004503void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4504 const FieldInfo& field_info) {
4505 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004506
Calin Juravle52c48962014-12-16 17:02:57 +00004507 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004508 Location base_loc = locations->InAt(0);
4509 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004510 Location out = locations->Out();
4511 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004512 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004513 Primitive::Type field_type = field_info.GetFieldType();
4514 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4515
4516 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004517 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004518 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004519 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004520
Roland Levillainc9285912015-12-18 10:38:42 +00004521 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004522 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004523 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004524
Roland Levillainc9285912015-12-18 10:38:42 +00004525 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004526 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004527 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004528
Roland Levillainc9285912015-12-18 10:38:42 +00004529 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004530 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004531 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004532
4533 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004534 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004535 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004536
4537 case Primitive::kPrimNot: {
4538 // /* HeapReference<Object> */ out = *(base + offset)
4539 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4540 Location temp_loc = locations->GetTemp(0);
4541 // Note that a potential implicit null check is handled in this
4542 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4543 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4544 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4545 if (is_volatile) {
4546 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4547 }
4548 } else {
4549 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4550 codegen_->MaybeRecordImplicitNullCheck(instruction);
4551 if (is_volatile) {
4552 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4553 }
4554 // If read barriers are enabled, emit read barriers other than
4555 // Baker's using a slow path (and also unpoison the loaded
4556 // reference, if heap poisoning is enabled).
4557 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4558 }
4559 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004560 }
4561
Roland Levillainc9285912015-12-18 10:38:42 +00004562 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004563 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004564 GenerateWideAtomicLoad(base, offset,
4565 out.AsRegisterPairLow<Register>(),
4566 out.AsRegisterPairHigh<Register>());
4567 } else {
4568 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4569 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004570 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004571
Roland Levillainc9285912015-12-18 10:38:42 +00004572 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004573 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004574 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004575
4576 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004577 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004578 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004579 Register lo = locations->GetTemp(0).AsRegister<Register>();
4580 Register hi = locations->GetTemp(1).AsRegister<Register>();
4581 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004582 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004583 __ vmovdrr(out_reg, lo, hi);
4584 } else {
4585 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004586 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004587 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004588 break;
4589 }
4590
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004591 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004592 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004593 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004594 }
Calin Juravle52c48962014-12-16 17:02:57 +00004595
Roland Levillainc9285912015-12-18 10:38:42 +00004596 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4597 // Potential implicit null checks, in the case of reference or
4598 // double fields, are handled in the previous switch statement.
4599 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004600 codegen_->MaybeRecordImplicitNullCheck(instruction);
4601 }
4602
Calin Juravle52c48962014-12-16 17:02:57 +00004603 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004604 if (field_type == Primitive::kPrimNot) {
4605 // Memory barriers, in the case of references, are also handled
4606 // in the previous switch statement.
4607 } else {
4608 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4609 }
Roland Levillain4d027112015-07-01 15:41:14 +01004610 }
Calin Juravle52c48962014-12-16 17:02:57 +00004611}
4612
4613void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4614 HandleFieldSet(instruction, instruction->GetFieldInfo());
4615}
4616
4617void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004618 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004619}
4620
4621void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4622 HandleFieldGet(instruction, instruction->GetFieldInfo());
4623}
4624
4625void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4626 HandleFieldGet(instruction, instruction->GetFieldInfo());
4627}
4628
4629void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4630 HandleFieldGet(instruction, instruction->GetFieldInfo());
4631}
4632
4633void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4634 HandleFieldGet(instruction, instruction->GetFieldInfo());
4635}
4636
4637void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4638 HandleFieldSet(instruction, instruction->GetFieldInfo());
4639}
4640
4641void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004642 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004643}
4644
Calin Juravlee460d1d2015-09-29 04:52:17 +01004645void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4646 HUnresolvedInstanceFieldGet* instruction) {
4647 FieldAccessCallingConventionARM calling_convention;
4648 codegen_->CreateUnresolvedFieldLocationSummary(
4649 instruction, instruction->GetFieldType(), calling_convention);
4650}
4651
4652void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4653 HUnresolvedInstanceFieldGet* instruction) {
4654 FieldAccessCallingConventionARM calling_convention;
4655 codegen_->GenerateUnresolvedFieldAccess(instruction,
4656 instruction->GetFieldType(),
4657 instruction->GetFieldIndex(),
4658 instruction->GetDexPc(),
4659 calling_convention);
4660}
4661
4662void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4663 HUnresolvedInstanceFieldSet* instruction) {
4664 FieldAccessCallingConventionARM calling_convention;
4665 codegen_->CreateUnresolvedFieldLocationSummary(
4666 instruction, instruction->GetFieldType(), calling_convention);
4667}
4668
4669void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4670 HUnresolvedInstanceFieldSet* instruction) {
4671 FieldAccessCallingConventionARM calling_convention;
4672 codegen_->GenerateUnresolvedFieldAccess(instruction,
4673 instruction->GetFieldType(),
4674 instruction->GetFieldIndex(),
4675 instruction->GetDexPc(),
4676 calling_convention);
4677}
4678
4679void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4680 HUnresolvedStaticFieldGet* instruction) {
4681 FieldAccessCallingConventionARM calling_convention;
4682 codegen_->CreateUnresolvedFieldLocationSummary(
4683 instruction, instruction->GetFieldType(), calling_convention);
4684}
4685
4686void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4687 HUnresolvedStaticFieldGet* instruction) {
4688 FieldAccessCallingConventionARM calling_convention;
4689 codegen_->GenerateUnresolvedFieldAccess(instruction,
4690 instruction->GetFieldType(),
4691 instruction->GetFieldIndex(),
4692 instruction->GetDexPc(),
4693 calling_convention);
4694}
4695
4696void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4697 HUnresolvedStaticFieldSet* instruction) {
4698 FieldAccessCallingConventionARM calling_convention;
4699 codegen_->CreateUnresolvedFieldLocationSummary(
4700 instruction, instruction->GetFieldType(), calling_convention);
4701}
4702
4703void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4704 HUnresolvedStaticFieldSet* instruction) {
4705 FieldAccessCallingConventionARM calling_convention;
4706 codegen_->GenerateUnresolvedFieldAccess(instruction,
4707 instruction->GetFieldType(),
4708 instruction->GetFieldIndex(),
4709 instruction->GetDexPc(),
4710 calling_convention);
4711}
4712
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004713void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004714 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4715 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004716}
4717
Calin Juravle2ae48182016-03-16 14:05:09 +00004718void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4719 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004720 return;
4721 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004722 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004723
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004724 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004725 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004726}
4727
Calin Juravle2ae48182016-03-16 14:05:09 +00004728void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004729 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004730 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004731
4732 LocationSummary* locations = instruction->GetLocations();
4733 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004734
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004735 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004736}
4737
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004738void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004739 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004740}
4741
Artem Serov6c916792016-07-11 14:02:34 +01004742static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4743 switch (type) {
4744 case Primitive::kPrimNot:
4745 return kLoadWord;
4746 case Primitive::kPrimBoolean:
4747 return kLoadUnsignedByte;
4748 case Primitive::kPrimByte:
4749 return kLoadSignedByte;
4750 case Primitive::kPrimChar:
4751 return kLoadUnsignedHalfword;
4752 case Primitive::kPrimShort:
4753 return kLoadSignedHalfword;
4754 case Primitive::kPrimInt:
4755 return kLoadWord;
4756 case Primitive::kPrimLong:
4757 return kLoadWordPair;
4758 case Primitive::kPrimFloat:
4759 return kLoadSWord;
4760 case Primitive::kPrimDouble:
4761 return kLoadDWord;
4762 default:
4763 LOG(FATAL) << "Unreachable type " << type;
4764 UNREACHABLE();
4765 }
4766}
4767
4768static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4769 switch (type) {
4770 case Primitive::kPrimNot:
4771 return kStoreWord;
4772 case Primitive::kPrimBoolean:
4773 case Primitive::kPrimByte:
4774 return kStoreByte;
4775 case Primitive::kPrimChar:
4776 case Primitive::kPrimShort:
4777 return kStoreHalfword;
4778 case Primitive::kPrimInt:
4779 return kStoreWord;
4780 case Primitive::kPrimLong:
4781 return kStoreWordPair;
4782 case Primitive::kPrimFloat:
4783 return kStoreSWord;
4784 case Primitive::kPrimDouble:
4785 return kStoreDWord;
4786 default:
4787 LOG(FATAL) << "Unreachable type " << type;
4788 UNREACHABLE();
4789 }
4790}
4791
4792void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4793 Location out_loc,
4794 Register base,
4795 Register reg_offset,
4796 Condition cond) {
4797 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4798 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4799
4800 switch (type) {
4801 case Primitive::kPrimByte:
4802 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4803 break;
4804 case Primitive::kPrimBoolean:
4805 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4806 break;
4807 case Primitive::kPrimShort:
4808 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4809 break;
4810 case Primitive::kPrimChar:
4811 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4812 break;
4813 case Primitive::kPrimNot:
4814 case Primitive::kPrimInt:
4815 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4816 break;
4817 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4818 case Primitive::kPrimLong:
4819 case Primitive::kPrimFloat:
4820 case Primitive::kPrimDouble:
4821 default:
4822 LOG(FATAL) << "Unreachable type " << type;
4823 UNREACHABLE();
4824 }
4825}
4826
4827void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4828 Location loc,
4829 Register base,
4830 Register reg_offset,
4831 Condition cond) {
4832 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4833 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4834
4835 switch (type) {
4836 case Primitive::kPrimByte:
4837 case Primitive::kPrimBoolean:
4838 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4839 break;
4840 case Primitive::kPrimShort:
4841 case Primitive::kPrimChar:
4842 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4843 break;
4844 case Primitive::kPrimNot:
4845 case Primitive::kPrimInt:
4846 __ str(loc.AsRegister<Register>(), mem_address, cond);
4847 break;
4848 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4849 case Primitive::kPrimLong:
4850 case Primitive::kPrimFloat:
4851 case Primitive::kPrimDouble:
4852 default:
4853 LOG(FATAL) << "Unreachable type " << type;
4854 UNREACHABLE();
4855 }
4856}
4857
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004858void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004859 bool object_array_get_with_read_barrier =
4860 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004861 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004862 new (GetGraph()->GetArena()) LocationSummary(instruction,
4863 object_array_get_with_read_barrier ?
4864 LocationSummary::kCallOnSlowPath :
4865 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004866 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004867 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004868 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004869 locations->SetInAt(0, Location::RequiresRegister());
4870 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004871 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4872 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4873 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004874 // The output overlaps in the case of an object array get with
4875 // read barriers enabled: we do not want the move to overwrite the
4876 // array's location, as we need it to emit the read barrier.
4877 locations->SetOut(
4878 Location::RequiresRegister(),
4879 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004880 }
Roland Levillainc9285912015-12-18 10:38:42 +00004881 // We need a temporary register for the read barrier marking slow
4882 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07004883 // Also need for String compression feature.
4884 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4885 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00004886 locations->AddTemp(Location::RequiresRegister());
4887 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004888}
4889
4890void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4891 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004892 Location obj_loc = locations->InAt(0);
4893 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004894 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004895 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004896 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004897 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07004898 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4899 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01004900 HInstruction* array_instr = instruction->GetArray();
4901 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01004902
Roland Levillain4d027112015-07-01 15:41:14 +01004903 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004904 case Primitive::kPrimBoolean:
4905 case Primitive::kPrimByte:
4906 case Primitive::kPrimShort:
4907 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004908 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004909 Register length;
4910 if (maybe_compressed_char_at) {
4911 length = locations->GetTemp(0).AsRegister<Register>();
4912 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4913 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4914 codegen_->MaybeRecordImplicitNullCheck(instruction);
4915 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004916 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004917 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07004918 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07004919 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004920 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4921 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4922 "Expecting 0=compressed, 1=uncompressed");
4923 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004924 __ LoadFromOffset(kLoadUnsignedByte,
4925 out_loc.AsRegister<Register>(),
4926 obj,
4927 data_offset + const_index);
4928 __ b(&done);
4929 __ Bind(&uncompressed_load);
4930 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4931 out_loc.AsRegister<Register>(),
4932 obj,
4933 data_offset + (const_index << 1));
4934 __ Bind(&done);
4935 } else {
4936 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01004937
jessicahandojo05765752016-09-09 19:01:32 -07004938 LoadOperandType load_type = GetLoadOperandType(type);
4939 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4940 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004941 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004942 Register temp = IP;
4943
4944 if (has_intermediate_address) {
4945 // We do not need to compute the intermediate address from the array: the
4946 // input instruction has done it already. See the comment in
4947 // `TryExtractArrayAccessAddress()`.
4948 if (kIsDebugBuild) {
4949 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4950 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4951 }
4952 temp = obj;
4953 } else {
4954 __ add(temp, obj, ShifterOperand(data_offset));
4955 }
jessicahandojo05765752016-09-09 19:01:32 -07004956 if (maybe_compressed_char_at) {
4957 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004958 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4959 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4960 "Expecting 0=compressed, 1=uncompressed");
4961 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004962 __ ldrb(out_loc.AsRegister<Register>(),
4963 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
4964 __ b(&done);
4965 __ Bind(&uncompressed_load);
4966 __ ldrh(out_loc.AsRegister<Register>(),
4967 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
4968 __ Bind(&done);
4969 } else {
4970 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4971 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004972 }
4973 break;
4974 }
4975
Roland Levillainc9285912015-12-18 10:38:42 +00004976 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00004977 // The read barrier instrumentation of object ArrayGet
4978 // instructions does not support the HIntermediateAddress
4979 // instruction.
4980 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4981
Roland Levillainc9285912015-12-18 10:38:42 +00004982 static_assert(
4983 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4984 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004985 // /* HeapReference<Object> */ out =
4986 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4987 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4988 Location temp = locations->GetTemp(0);
4989 // Note that a potential implicit null check is handled in this
4990 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4991 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4992 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4993 } else {
4994 Register out = out_loc.AsRegister<Register>();
4995 if (index.IsConstant()) {
4996 size_t offset =
4997 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4998 __ LoadFromOffset(kLoadWord, out, obj, offset);
4999 codegen_->MaybeRecordImplicitNullCheck(instruction);
5000 // If read barriers are enabled, emit read barriers other than
5001 // Baker's using a slow path (and also unpoison the loaded
5002 // reference, if heap poisoning is enabled).
5003 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5004 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005005 Register temp = IP;
5006
5007 if (has_intermediate_address) {
5008 // We do not need to compute the intermediate address from the array: the
5009 // input instruction has done it already. See the comment in
5010 // `TryExtractArrayAccessAddress()`.
5011 if (kIsDebugBuild) {
5012 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5013 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5014 }
5015 temp = obj;
5016 } else {
5017 __ add(temp, obj, ShifterOperand(data_offset));
5018 }
5019 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005020
Roland Levillainc9285912015-12-18 10:38:42 +00005021 codegen_->MaybeRecordImplicitNullCheck(instruction);
5022 // If read barriers are enabled, emit read barriers other than
5023 // Baker's using a slow path (and also unpoison the loaded
5024 // reference, if heap poisoning is enabled).
5025 codegen_->MaybeGenerateReadBarrierSlow(
5026 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5027 }
5028 }
5029 break;
5030 }
5031
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005032 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005033 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005034 size_t offset =
5035 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005036 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005037 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005038 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005039 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040 }
5041 break;
5042 }
5043
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005044 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005045 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005046 if (index.IsConstant()) {
5047 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005048 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005049 } else {
5050 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005051 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005052 }
5053 break;
5054 }
5055
5056 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005057 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005058 if (index.IsConstant()) {
5059 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005060 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005061 } else {
5062 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005063 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005064 }
5065 break;
5066 }
5067
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005068 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005069 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005070 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071 }
Roland Levillain4d027112015-07-01 15:41:14 +01005072
5073 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005074 // Potential implicit null checks, in the case of reference
5075 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005076 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005077 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005078 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005079}
5080
5081void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005082 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005083
5084 bool needs_write_barrier =
5085 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005086 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005087
Nicolas Geoffray39468442014-09-02 15:17:15 +01005088 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005089 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005090 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005091 LocationSummary::kCallOnSlowPath :
5092 LocationSummary::kNoCall);
5093
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005094 locations->SetInAt(0, Location::RequiresRegister());
5095 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5096 if (Primitive::IsFloatingPointType(value_type)) {
5097 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005099 locations->SetInAt(2, Location::RequiresRegister());
5100 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005101 if (needs_write_barrier) {
5102 // Temporary registers for the write barrier.
5103 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005104 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005106}
5107
5108void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5109 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005110 Location array_loc = locations->InAt(0);
5111 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005113 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005114 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005115 bool needs_write_barrier =
5116 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005117 uint32_t data_offset =
5118 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5119 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005120 HInstruction* array_instr = instruction->GetArray();
5121 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122
5123 switch (value_type) {
5124 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005125 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005127 case Primitive::kPrimChar:
5128 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005129 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005130 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5131 uint32_t full_offset =
5132 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5133 StoreOperandType store_type = GetStoreOperandType(value_type);
5134 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005135 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005136 Register temp = IP;
5137
5138 if (has_intermediate_address) {
5139 // We do not need to compute the intermediate address from the array: the
5140 // input instruction has done it already. See the comment in
5141 // `TryExtractArrayAccessAddress()`.
5142 if (kIsDebugBuild) {
5143 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5144 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5145 }
5146 temp = array;
5147 } else {
5148 __ add(temp, array, ShifterOperand(data_offset));
5149 }
Artem Serov6c916792016-07-11 14:02:34 +01005150 codegen_->StoreToShiftedRegOffset(value_type,
5151 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005152 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005153 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005154 }
5155 break;
5156 }
5157
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005158 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005159 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005160 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5161 // See the comment in instruction_simplifier_shared.cc.
5162 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005163
5164 if (instruction->InputAt(2)->IsNullConstant()) {
5165 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005166 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005167 size_t offset =
5168 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005169 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005170 } else {
5171 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005172 __ add(IP, array, ShifterOperand(data_offset));
5173 codegen_->StoreToShiftedRegOffset(value_type,
5174 value_loc,
5175 IP,
5176 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005177 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005178 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005179 DCHECK(!needs_write_barrier);
5180 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005181 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005182 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005183
5184 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005185 Location temp1_loc = locations->GetTemp(0);
5186 Register temp1 = temp1_loc.AsRegister<Register>();
5187 Location temp2_loc = locations->GetTemp(1);
5188 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005189 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5190 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5191 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5192 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005193 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005194
Roland Levillain3b359c72015-11-17 19:35:12 +00005195 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005196 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5197 codegen_->AddSlowPath(slow_path);
5198 if (instruction->GetValueCanBeNull()) {
5199 Label non_zero;
5200 __ CompareAndBranchIfNonZero(value, &non_zero);
5201 if (index.IsConstant()) {
5202 size_t offset =
5203 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5204 __ StoreToOffset(kStoreWord, value, array, offset);
5205 } else {
5206 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005207 __ add(IP, array, ShifterOperand(data_offset));
5208 codegen_->StoreToShiftedRegOffset(value_type,
5209 value_loc,
5210 IP,
5211 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005212 }
5213 codegen_->MaybeRecordImplicitNullCheck(instruction);
5214 __ b(&done);
5215 __ Bind(&non_zero);
5216 }
5217
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005218 // Note that when read barriers are enabled, the type checks
5219 // are performed without read barriers. This is fine, even in
5220 // the case where a class object is in the from-space after
5221 // the flip, as a comparison involving such a type would not
5222 // produce a false positive; it may of course produce a false
5223 // negative, in which case we would take the ArraySet slow
5224 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005225
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005226 // /* HeapReference<Class> */ temp1 = array->klass_
5227 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5228 codegen_->MaybeRecordImplicitNullCheck(instruction);
5229 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005230
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005231 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5232 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5233 // /* HeapReference<Class> */ temp2 = value->klass_
5234 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5235 // If heap poisoning is enabled, no need to unpoison `temp1`
5236 // nor `temp2`, as we are comparing two poisoned references.
5237 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005238
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005239 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5240 Label do_put;
5241 __ b(&do_put, EQ);
5242 // If heap poisoning is enabled, the `temp1` reference has
5243 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005244 __ MaybeUnpoisonHeapReference(temp1);
5245
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005246 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5247 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5248 // If heap poisoning is enabled, no need to unpoison
5249 // `temp1`, as we are comparing against null below.
5250 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5251 __ Bind(&do_put);
5252 } else {
5253 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005254 }
5255 }
5256
Artem Serov6c916792016-07-11 14:02:34 +01005257 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005258 if (kPoisonHeapReferences) {
5259 // Note that in the case where `value` is a null reference,
5260 // we do not enter this block, as a null reference does not
5261 // need poisoning.
5262 DCHECK_EQ(value_type, Primitive::kPrimNot);
5263 __ Mov(temp1, value);
5264 __ PoisonHeapReference(temp1);
5265 source = temp1;
5266 }
5267
5268 if (index.IsConstant()) {
5269 size_t offset =
5270 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5271 __ StoreToOffset(kStoreWord, source, array, offset);
5272 } else {
5273 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005274
5275 __ add(IP, array, ShifterOperand(data_offset));
5276 codegen_->StoreToShiftedRegOffset(value_type,
5277 Location::RegisterLocation(source),
5278 IP,
5279 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005280 }
5281
Roland Levillain3b359c72015-11-17 19:35:12 +00005282 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005283 codegen_->MaybeRecordImplicitNullCheck(instruction);
5284 }
5285
5286 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5287
5288 if (done.IsLinked()) {
5289 __ Bind(&done);
5290 }
5291
5292 if (slow_path != nullptr) {
5293 __ Bind(slow_path->GetExitLabel());
5294 }
5295
5296 break;
5297 }
5298
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005300 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005301 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005302 size_t offset =
5303 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005304 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005306 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005307 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005308 }
5309 break;
5310 }
5311
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005312 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005313 Location value = locations->InAt(2);
5314 DCHECK(value.IsFpuRegister());
5315 if (index.IsConstant()) {
5316 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005317 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005318 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005319 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005320 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5321 }
5322 break;
5323 }
5324
5325 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005326 Location value = locations->InAt(2);
5327 DCHECK(value.IsFpuRegisterPair());
5328 if (index.IsConstant()) {
5329 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005330 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005331 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005332 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005333 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5334 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005335
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005336 break;
5337 }
5338
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005339 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005340 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005341 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005342 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005343
Roland Levillain80e67092016-01-08 16:04:55 +00005344 // Objects are handled in the switch.
5345 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005346 codegen_->MaybeRecordImplicitNullCheck(instruction);
5347 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005348}
5349
5350void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005351 LocationSummary* locations =
5352 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005353 locations->SetInAt(0, Location::RequiresRegister());
5354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005355}
5356
5357void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5358 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005359 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005360 Register obj = locations->InAt(0).AsRegister<Register>();
5361 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005362 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005363 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005364 // Mask out compression flag from String's array length.
5365 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005366 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07005367 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368}
5369
Artem Serov328429f2016-07-06 16:23:04 +01005370void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005371 LocationSummary* locations =
5372 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5373
5374 locations->SetInAt(0, Location::RequiresRegister());
5375 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5376 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5377}
5378
5379void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5380 LocationSummary* locations = instruction->GetLocations();
5381 Location out = locations->Out();
5382 Location first = locations->InAt(0);
5383 Location second = locations->InAt(1);
5384
Artem Serov328429f2016-07-06 16:23:04 +01005385 if (second.IsRegister()) {
5386 __ add(out.AsRegister<Register>(),
5387 first.AsRegister<Register>(),
5388 ShifterOperand(second.AsRegister<Register>()));
5389 } else {
5390 __ AddConstant(out.AsRegister<Register>(),
5391 first.AsRegister<Register>(),
5392 second.GetConstant()->AsIntConstant()->GetValue());
5393 }
5394}
5395
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005396void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005397 RegisterSet caller_saves = RegisterSet::Empty();
5398 InvokeRuntimeCallingConvention calling_convention;
5399 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5400 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5401 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402 locations->SetInAt(0, Location::RequiresRegister());
5403 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005404}
5405
5406void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5407 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005408 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005409 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005410 codegen_->AddSlowPath(slow_path);
5411
Roland Levillain271ab9c2014-11-27 15:23:57 +00005412 Register index = locations->InAt(0).AsRegister<Register>();
5413 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005414
5415 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005416 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005417}
5418
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005419void CodeGeneratorARM::MarkGCCard(Register temp,
5420 Register card,
5421 Register object,
5422 Register value,
5423 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005424 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005425 if (can_be_null) {
5426 __ CompareAndBranchIfZero(value, &is_null);
5427 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005428 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005429 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5430 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005431 if (can_be_null) {
5432 __ Bind(&is_null);
5433 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005434}
5435
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005436void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005437 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005438}
5439
5440void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005441 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5442}
5443
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005444void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005445 LocationSummary* locations =
5446 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005447 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005448}
5449
5450void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005451 HBasicBlock* block = instruction->GetBlock();
5452 if (block->GetLoopInformation() != nullptr) {
5453 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5454 // The back edge will generate the suspend check.
5455 return;
5456 }
5457 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5458 // The goto will generate the suspend check.
5459 return;
5460 }
5461 GenerateSuspendCheck(instruction, nullptr);
5462}
5463
5464void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5465 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005466 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005467 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5468 if (slow_path == nullptr) {
5469 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5470 instruction->SetSlowPath(slow_path);
5471 codegen_->AddSlowPath(slow_path);
5472 if (successor != nullptr) {
5473 DCHECK(successor->IsLoopHeader());
5474 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5475 }
5476 } else {
5477 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5478 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005479
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005480 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005481 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005482 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005483 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005484 __ Bind(slow_path->GetReturnLabel());
5485 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005486 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005487 __ b(slow_path->GetEntryLabel());
5488 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005489}
5490
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005491ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5492 return codegen_->GetAssembler();
5493}
5494
5495void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005496 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005497 Location source = move->GetSource();
5498 Location destination = move->GetDestination();
5499
5500 if (source.IsRegister()) {
5501 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005502 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005503 } else if (destination.IsFpuRegister()) {
5504 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005505 } else {
5506 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005507 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005508 SP, destination.GetStackIndex());
5509 }
5510 } else if (source.IsStackSlot()) {
5511 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005512 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005513 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005514 } else if (destination.IsFpuRegister()) {
5515 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005516 } else {
5517 DCHECK(destination.IsStackSlot());
5518 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5519 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5520 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005521 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005522 if (destination.IsRegister()) {
5523 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5524 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005525 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005526 } else {
5527 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005528 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5529 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005530 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005531 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005532 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5533 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005534 } else if (destination.IsRegisterPair()) {
5535 DCHECK(ExpectedPairLayout(destination));
5536 __ LoadFromOffset(
5537 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5538 } else {
5539 DCHECK(destination.IsFpuRegisterPair()) << destination;
5540 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5541 SP,
5542 source.GetStackIndex());
5543 }
5544 } else if (source.IsRegisterPair()) {
5545 if (destination.IsRegisterPair()) {
5546 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5547 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005548 } else if (destination.IsFpuRegisterPair()) {
5549 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5550 source.AsRegisterPairLow<Register>(),
5551 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005552 } else {
5553 DCHECK(destination.IsDoubleStackSlot()) << destination;
5554 DCHECK(ExpectedPairLayout(source));
5555 __ StoreToOffset(
5556 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5557 }
5558 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005559 if (destination.IsRegisterPair()) {
5560 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5561 destination.AsRegisterPairHigh<Register>(),
5562 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5563 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005564 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5565 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5566 } else {
5567 DCHECK(destination.IsDoubleStackSlot()) << destination;
5568 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5569 SP,
5570 destination.GetStackIndex());
5571 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005572 } else {
5573 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005574 HConstant* constant = source.GetConstant();
5575 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5576 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005577 if (destination.IsRegister()) {
5578 __ LoadImmediate(destination.AsRegister<Register>(), value);
5579 } else {
5580 DCHECK(destination.IsStackSlot());
5581 __ LoadImmediate(IP, value);
5582 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5583 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005584 } else if (constant->IsLongConstant()) {
5585 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005586 if (destination.IsRegisterPair()) {
5587 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5588 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005589 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005590 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005591 __ LoadImmediate(IP, Low32Bits(value));
5592 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5593 __ LoadImmediate(IP, High32Bits(value));
5594 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5595 }
5596 } else if (constant->IsDoubleConstant()) {
5597 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005598 if (destination.IsFpuRegisterPair()) {
5599 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005600 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005601 DCHECK(destination.IsDoubleStackSlot()) << destination;
5602 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005603 __ LoadImmediate(IP, Low32Bits(int_value));
5604 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5605 __ LoadImmediate(IP, High32Bits(int_value));
5606 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5607 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005608 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005609 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005610 float value = constant->AsFloatConstant()->GetValue();
5611 if (destination.IsFpuRegister()) {
5612 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5613 } else {
5614 DCHECK(destination.IsStackSlot());
5615 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5616 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5617 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005618 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005619 }
5620}
5621
5622void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5623 __ Mov(IP, reg);
5624 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5625 __ StoreToOffset(kStoreWord, IP, SP, mem);
5626}
5627
5628void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5629 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5630 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5631 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5632 SP, mem1 + stack_offset);
5633 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5634 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5635 SP, mem2 + stack_offset);
5636 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5637}
5638
5639void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005640 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005641 Location source = move->GetSource();
5642 Location destination = move->GetDestination();
5643
5644 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005645 DCHECK_NE(source.AsRegister<Register>(), IP);
5646 DCHECK_NE(destination.AsRegister<Register>(), IP);
5647 __ Mov(IP, source.AsRegister<Register>());
5648 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5649 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005650 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005651 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005652 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005653 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005654 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5655 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005656 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005657 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005658 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005659 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005660 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005661 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005662 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005663 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005664 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5665 destination.AsRegisterPairHigh<Register>(),
5666 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005667 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005668 Register low_reg = source.IsRegisterPair()
5669 ? source.AsRegisterPairLow<Register>()
5670 : destination.AsRegisterPairLow<Register>();
5671 int mem = source.IsRegisterPair()
5672 ? destination.GetStackIndex()
5673 : source.GetStackIndex();
5674 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005675 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005676 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005677 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005678 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005679 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5680 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005681 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005682 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005683 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005684 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5685 DRegister reg = source.IsFpuRegisterPair()
5686 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5687 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5688 int mem = source.IsFpuRegisterPair()
5689 ? destination.GetStackIndex()
5690 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005691 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005692 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005693 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005694 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5695 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5696 : destination.AsFpuRegister<SRegister>();
5697 int mem = source.IsFpuRegister()
5698 ? destination.GetStackIndex()
5699 : source.GetStackIndex();
5700
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005701 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005702 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005703 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005704 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005705 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5706 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005707 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005708 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005709 }
5710}
5711
5712void ParallelMoveResolverARM::SpillScratch(int reg) {
5713 __ Push(static_cast<Register>(reg));
5714}
5715
5716void ParallelMoveResolverARM::RestoreScratch(int reg) {
5717 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005718}
5719
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005720HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5721 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005722 switch (desired_class_load_kind) {
5723 case HLoadClass::LoadKind::kReferrersClass:
5724 break;
5725 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5726 DCHECK(!GetCompilerOptions().GetCompilePic());
5727 break;
5728 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5729 DCHECK(GetCompilerOptions().GetCompilePic());
5730 break;
5731 case HLoadClass::LoadKind::kBootImageAddress:
5732 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005733 case HLoadClass::LoadKind::kBssEntry:
5734 DCHECK(!Runtime::Current()->UseJitCompilation());
5735 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005736 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005737 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005738 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005739 case HLoadClass::LoadKind::kDexCacheViaMethod:
5740 break;
5741 }
5742 return desired_class_load_kind;
5743}
5744
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005745void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005746 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5747 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005748 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005749 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005750 cls,
5751 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005752 Location::RegisterLocation(R0));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005753 return;
5754 }
Vladimir Marko41559982017-01-06 14:04:23 +00005755 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005756
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005757 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5758 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005759 ? LocationSummary::kCallOnSlowPath
5760 : LocationSummary::kNoCall;
5761 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005762 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005763 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005764 }
5765
Vladimir Marko41559982017-01-06 14:04:23 +00005766 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005767 locations->SetInAt(0, Location::RequiresRegister());
5768 }
5769 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005770}
5771
5772void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005773 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5774 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
5775 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01005776 return;
5777 }
Vladimir Marko41559982017-01-06 14:04:23 +00005778 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01005779
Vladimir Marko41559982017-01-06 14:04:23 +00005780 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005781 Location out_loc = locations->Out();
5782 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005783
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005784 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
5785 ? kWithoutReadBarrier
5786 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005787 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00005788 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005789 case HLoadClass::LoadKind::kReferrersClass: {
5790 DCHECK(!cls->CanCallRuntime());
5791 DCHECK(!cls->MustGenerateClinitCheck());
5792 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5793 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005794 GenerateGcRootFieldLoad(cls,
5795 out_loc,
5796 current_method,
5797 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005798 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005799 break;
5800 }
5801 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005802 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005803 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005804 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5805 cls->GetTypeIndex()));
5806 break;
5807 }
5808 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005809 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005810 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005811 CodeGeneratorARM::PcRelativePatchInfo* labels =
5812 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5813 __ BindTrackedLabel(&labels->movw_label);
5814 __ movw(out, /* placeholder */ 0u);
5815 __ BindTrackedLabel(&labels->movt_label);
5816 __ movt(out, /* placeholder */ 0u);
5817 __ BindTrackedLabel(&labels->add_pc_label);
5818 __ add(out, out, ShifterOperand(PC));
5819 break;
5820 }
5821 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005822 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005823 DCHECK_NE(cls->GetAddress(), 0u);
5824 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5825 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5826 break;
5827 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005828 case HLoadClass::LoadKind::kBssEntry: {
5829 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
5830 CodeGeneratorARM::PcRelativePatchInfo* labels =
5831 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5832 __ BindTrackedLabel(&labels->movw_label);
5833 __ movw(out, /* placeholder */ 0u);
5834 __ BindTrackedLabel(&labels->movt_label);
5835 __ movt(out, /* placeholder */ 0u);
5836 __ BindTrackedLabel(&labels->add_pc_label);
5837 __ add(out, out, ShifterOperand(PC));
5838 GenerateGcRootFieldLoad(cls, out_loc, out, 0, kCompilerReadBarrierOption);
5839 generate_null_check = true;
5840 break;
5841 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005842 case HLoadClass::LoadKind::kJitTableAddress: {
5843 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
5844 cls->GetTypeIndex(),
5845 cls->GetAddress()));
5846 // /* GcRoot<mirror::Class> */ out = *out
5847 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005848 break;
5849 }
Vladimir Marko41559982017-01-06 14:04:23 +00005850 case HLoadClass::LoadKind::kDexCacheViaMethod:
5851 LOG(FATAL) << "UNREACHABLE";
5852 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005853 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005854
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005855 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5856 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005857 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005858 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5859 codegen_->AddSlowPath(slow_path);
5860 if (generate_null_check) {
5861 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5862 }
5863 if (cls->MustGenerateClinitCheck()) {
5864 GenerateClassInitializationCheck(slow_path, out);
5865 } else {
5866 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005867 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005868 }
5869}
5870
5871void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5872 LocationSummary* locations =
5873 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5874 locations->SetInAt(0, Location::RequiresRegister());
5875 if (check->HasUses()) {
5876 locations->SetOut(Location::SameAsFirstInput());
5877 }
5878}
5879
5880void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005881 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005882 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005883 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005884 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005885 GenerateClassInitializationCheck(slow_path,
5886 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005887}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005888
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005889void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005890 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005891 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5892 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5893 __ b(slow_path->GetEntryLabel(), LT);
5894 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5895 // properly. Therefore, we do a memory fence.
5896 __ dmb(ISH);
5897 __ Bind(slow_path->GetExitLabel());
5898}
5899
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005900HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5901 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005902 switch (desired_string_load_kind) {
5903 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5904 DCHECK(!GetCompilerOptions().GetCompilePic());
5905 break;
5906 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5907 DCHECK(GetCompilerOptions().GetCompilePic());
5908 break;
5909 case HLoadString::LoadKind::kBootImageAddress:
5910 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005911 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005912 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005913 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005914 case HLoadString::LoadKind::kJitTableAddress:
5915 DCHECK(Runtime::Current()->UseJitCompilation());
5916 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005917 case HLoadString::LoadKind::kDexCacheViaMethod:
5918 break;
5919 }
5920 return desired_string_load_kind;
5921}
5922
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005923void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00005924 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005925 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005926 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005927 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005928 locations->SetOut(Location::RegisterLocation(R0));
5929 } else {
5930 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005931 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5932 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5933 // Rely on the pResolveString and/or marking to save everything, including temps.
5934 // Note that IP may theoretically be clobbered by saving/restoring the live register
5935 // (only one thanks to the custom calling convention), so we request a different temp.
5936 locations->AddTemp(Location::RequiresRegister());
5937 RegisterSet caller_saves = RegisterSet::Empty();
5938 InvokeRuntimeCallingConvention calling_convention;
5939 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5940 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5941 // that the the kPrimNot result register is the same as the first argument register.
5942 locations->SetCustomSlowPathCallerSaves(caller_saves);
5943 } else {
5944 // For non-Baker read barrier we have a temp-clobbering call.
5945 }
5946 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005947 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005948}
5949
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005950// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
5951// move.
5952void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005953 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005954 Location out_loc = locations->Out();
5955 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005956 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005957
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005958 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005959 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005960 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005961 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5962 load->GetStringIndex()));
5963 return; // No dex cache slow path.
5964 }
5965 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005966 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005967 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005968 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005969 __ BindTrackedLabel(&labels->movw_label);
5970 __ movw(out, /* placeholder */ 0u);
5971 __ BindTrackedLabel(&labels->movt_label);
5972 __ movt(out, /* placeholder */ 0u);
5973 __ BindTrackedLabel(&labels->add_pc_label);
5974 __ add(out, out, ShifterOperand(PC));
5975 return; // No dex cache slow path.
5976 }
5977 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00005978 uint32_t address = dchecked_integral_cast<uint32_t>(
5979 reinterpret_cast<uintptr_t>(load->GetString().Get()));
5980 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005981 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5982 return; // No dex cache slow path.
5983 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005984 case HLoadString::LoadKind::kBssEntry: {
5985 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005986 Register temp = locations->GetTemp(0).AsRegister<Register>();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005987 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005988 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00005989 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005990 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005991 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005992 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005993 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005994 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08005995 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005996 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5997 codegen_->AddSlowPath(slow_path);
5998 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5999 __ Bind(slow_path->GetExitLabel());
6000 return;
6001 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006002 case HLoadString::LoadKind::kJitTableAddress: {
6003 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006004 load->GetStringIndex(),
6005 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006006 // /* GcRoot<mirror::String> */ out = *out
6007 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6008 return;
6009 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006010 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006011 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006012 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006013
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006014 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
6015 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6016 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006017 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006018 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006019 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6020 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006021}
6022
David Brazdilcb1c0552015-08-04 16:22:25 +01006023static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006024 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006025}
6026
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006027void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6028 LocationSummary* locations =
6029 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6030 locations->SetOut(Location::RequiresRegister());
6031}
6032
6033void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006034 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006035 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6036}
6037
6038void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6039 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6040}
6041
6042void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006043 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006044 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006045}
6046
6047void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6048 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006049 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006050 InvokeRuntimeCallingConvention calling_convention;
6051 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6052}
6053
6054void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006055 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006056 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006057}
6058
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006059// Temp is used for read barrier.
6060static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6061 if (kEmitCompilerReadBarrier &&
6062 (kUseBakerReadBarrier ||
6063 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6064 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6065 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6066 return 1;
6067 }
6068 return 0;
6069}
6070
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006071// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006072// interface pointer, one for loading the current interface.
6073// The other checks have one temp for loading the object's class.
6074static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6075 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6076 return 3;
6077 }
6078 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006079}
6080
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006081void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006082 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006083 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006084 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006085 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006086 case TypeCheckKind::kExactCheck:
6087 case TypeCheckKind::kAbstractClassCheck:
6088 case TypeCheckKind::kClassHierarchyCheck:
6089 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006090 call_kind =
6091 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006092 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006093 break;
6094 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006095 case TypeCheckKind::kUnresolvedCheck:
6096 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006097 call_kind = LocationSummary::kCallOnSlowPath;
6098 break;
6099 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006100
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006101 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006102 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006103 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006104 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006105 locations->SetInAt(0, Location::RequiresRegister());
6106 locations->SetInAt(1, Location::RequiresRegister());
6107 // The "out" register is used as a temporary, so it overlaps with the inputs.
6108 // Note that TypeCheckSlowPathARM uses this register too.
6109 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006110 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006111}
6112
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006113void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006114 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006115 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006116 Location obj_loc = locations->InAt(0);
6117 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006118 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006119 Location out_loc = locations->Out();
6120 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006121 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6122 DCHECK_LE(num_temps, 1u);
6123 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006124 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006125 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6126 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6127 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006128 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006129 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006130
6131 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006132 // avoid null check if we know obj is not null.
6133 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006134 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006135 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006136
Roland Levillainc9285912015-12-18 10:38:42 +00006137 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006138 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006139 // /* HeapReference<Class> */ out = obj->klass_
6140 GenerateReferenceLoadTwoRegisters(instruction,
6141 out_loc,
6142 obj_loc,
6143 class_offset,
6144 maybe_temp_loc,
6145 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006146 __ cmp(out, ShifterOperand(cls));
6147 // Classes must be equal for the instanceof to succeed.
6148 __ b(&zero, NE);
6149 __ LoadImmediate(out, 1);
6150 __ b(&done);
6151 break;
6152 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006153
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006154 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006155 // /* HeapReference<Class> */ out = obj->klass_
6156 GenerateReferenceLoadTwoRegisters(instruction,
6157 out_loc,
6158 obj_loc,
6159 class_offset,
6160 maybe_temp_loc,
6161 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006162 // If the class is abstract, we eagerly fetch the super class of the
6163 // object to avoid doing a comparison we know will fail.
6164 Label loop;
6165 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006166 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006167 GenerateReferenceLoadOneRegister(instruction,
6168 out_loc,
6169 super_offset,
6170 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006171 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006172 // If `out` is null, we use it for the result, and jump to `done`.
6173 __ CompareAndBranchIfZero(out, &done);
6174 __ cmp(out, ShifterOperand(cls));
6175 __ b(&loop, NE);
6176 __ LoadImmediate(out, 1);
6177 if (zero.IsLinked()) {
6178 __ b(&done);
6179 }
6180 break;
6181 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006182
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006183 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006184 // /* HeapReference<Class> */ out = obj->klass_
6185 GenerateReferenceLoadTwoRegisters(instruction,
6186 out_loc,
6187 obj_loc,
6188 class_offset,
6189 maybe_temp_loc,
6190 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006191 // Walk over the class hierarchy to find a match.
6192 Label loop, success;
6193 __ Bind(&loop);
6194 __ cmp(out, ShifterOperand(cls));
6195 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006196 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006197 GenerateReferenceLoadOneRegister(instruction,
6198 out_loc,
6199 super_offset,
6200 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006201 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006202 __ CompareAndBranchIfNonZero(out, &loop);
6203 // If `out` is null, we use it for the result, and jump to `done`.
6204 __ b(&done);
6205 __ Bind(&success);
6206 __ LoadImmediate(out, 1);
6207 if (zero.IsLinked()) {
6208 __ b(&done);
6209 }
6210 break;
6211 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006212
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006213 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006214 // /* HeapReference<Class> */ out = obj->klass_
6215 GenerateReferenceLoadTwoRegisters(instruction,
6216 out_loc,
6217 obj_loc,
6218 class_offset,
6219 maybe_temp_loc,
6220 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006221 // Do an exact check.
6222 Label exact_check;
6223 __ cmp(out, ShifterOperand(cls));
6224 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006225 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006226 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006227 GenerateReferenceLoadOneRegister(instruction,
6228 out_loc,
6229 component_offset,
6230 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006231 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006232 // If `out` is null, we use it for the result, and jump to `done`.
6233 __ CompareAndBranchIfZero(out, &done);
6234 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6235 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6236 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006237 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006238 __ LoadImmediate(out, 1);
6239 __ b(&done);
6240 break;
6241 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006242
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006243 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006244 // No read barrier since the slow path will retry upon failure.
6245 // /* HeapReference<Class> */ out = obj->klass_
6246 GenerateReferenceLoadTwoRegisters(instruction,
6247 out_loc,
6248 obj_loc,
6249 class_offset,
6250 maybe_temp_loc,
6251 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006252 __ cmp(out, ShifterOperand(cls));
6253 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006254 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6255 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006256 codegen_->AddSlowPath(slow_path);
6257 __ b(slow_path->GetEntryLabel(), NE);
6258 __ LoadImmediate(out, 1);
6259 if (zero.IsLinked()) {
6260 __ b(&done);
6261 }
6262 break;
6263 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006264
Calin Juravle98893e12015-10-02 21:05:03 +01006265 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006266 case TypeCheckKind::kInterfaceCheck: {
6267 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006268 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006269 // cases.
6270 //
6271 // We cannot directly call the InstanceofNonTrivial runtime
6272 // entry point without resorting to a type checking slow path
6273 // here (i.e. by calling InvokeRuntime directly), as it would
6274 // require to assign fixed registers for the inputs of this
6275 // HInstanceOf instruction (following the runtime calling
6276 // convention), which might be cluttered by the potential first
6277 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006278 //
6279 // TODO: Introduce a new runtime entry point taking the object
6280 // to test (instead of its class) as argument, and let it deal
6281 // with the read barrier issues. This will let us refactor this
6282 // case of the `switch` code as it was previously (with a direct
6283 // call to the runtime not using a type checking slow path).
6284 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006285 DCHECK(locations->OnlyCallsOnSlowPath());
6286 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6287 /* is_fatal */ false);
6288 codegen_->AddSlowPath(slow_path);
6289 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006290 if (zero.IsLinked()) {
6291 __ b(&done);
6292 }
6293 break;
6294 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006295 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006296
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006297 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006298 __ Bind(&zero);
6299 __ LoadImmediate(out, 0);
6300 }
6301
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006302 if (done.IsLinked()) {
6303 __ Bind(&done);
6304 }
6305
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006306 if (slow_path != nullptr) {
6307 __ Bind(slow_path->GetExitLabel());
6308 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006309}
6310
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006311void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6313 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6314
Roland Levillain3b359c72015-11-17 19:35:12 +00006315 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6316 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006317 case TypeCheckKind::kExactCheck:
6318 case TypeCheckKind::kAbstractClassCheck:
6319 case TypeCheckKind::kClassHierarchyCheck:
6320 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006321 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6322 LocationSummary::kCallOnSlowPath :
6323 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006324 break;
6325 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006326 case TypeCheckKind::kUnresolvedCheck:
6327 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 call_kind = LocationSummary::kCallOnSlowPath;
6329 break;
6330 }
6331
Roland Levillain3b359c72015-11-17 19:35:12 +00006332 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6333 locations->SetInAt(0, Location::RequiresRegister());
6334 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006335 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006336}
6337
6338void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006339 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006340 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006341 Location obj_loc = locations->InAt(0);
6342 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006343 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006344 Location temp_loc = locations->GetTemp(0);
6345 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006346 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6347 DCHECK_LE(num_temps, 3u);
6348 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6349 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6350 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6351 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6352 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6353 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6354 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6355 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6356 const uint32_t object_array_data_offset =
6357 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006358
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006359 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6360 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6361 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006362 bool is_type_check_slow_path_fatal = false;
6363 if (!kEmitCompilerReadBarrier) {
6364 is_type_check_slow_path_fatal =
6365 (type_check_kind == TypeCheckKind::kExactCheck ||
6366 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6367 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6368 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6369 !instruction->CanThrowIntoCatchBlock();
6370 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01006371 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006372 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6373 is_type_check_slow_path_fatal);
6374 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006375
6376 Label done;
6377 // Avoid null check if we know obj is not null.
6378 if (instruction->MustDoNullCheck()) {
6379 __ CompareAndBranchIfZero(obj, &done);
6380 }
6381
Roland Levillain3b359c72015-11-17 19:35:12 +00006382 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006383 case TypeCheckKind::kExactCheck:
6384 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006385 // /* HeapReference<Class> */ temp = obj->klass_
6386 GenerateReferenceLoadTwoRegisters(instruction,
6387 temp_loc,
6388 obj_loc,
6389 class_offset,
6390 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006391 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006392
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006393 __ cmp(temp, ShifterOperand(cls));
6394 // Jump to slow path for throwing the exception or doing a
6395 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006396 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006397 break;
6398 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006399
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006400 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006401 // /* HeapReference<Class> */ temp = obj->klass_
6402 GenerateReferenceLoadTwoRegisters(instruction,
6403 temp_loc,
6404 obj_loc,
6405 class_offset,
6406 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006407 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006408
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006409 // If the class is abstract, we eagerly fetch the super class of the
6410 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006411 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006412 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006413 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006414 GenerateReferenceLoadOneRegister(instruction,
6415 temp_loc,
6416 super_offset,
6417 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006418 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006419
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006420 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6421 // exception.
6422 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00006423
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006424 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006425 __ cmp(temp, ShifterOperand(cls));
6426 __ b(&loop, NE);
6427 break;
6428 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006429
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006430 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006431 // /* HeapReference<Class> */ temp = obj->klass_
6432 GenerateReferenceLoadTwoRegisters(instruction,
6433 temp_loc,
6434 obj_loc,
6435 class_offset,
6436 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006437 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006438
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006439 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006440 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006441 __ Bind(&loop);
6442 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006443 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006444
Roland Levillain3b359c72015-11-17 19:35:12 +00006445 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006446 GenerateReferenceLoadOneRegister(instruction,
6447 temp_loc,
6448 super_offset,
6449 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006450 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006451
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006452 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6453 // exception.
6454 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6455 // Otherwise, jump to the beginning of the loop.
6456 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006457 break;
6458 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006459
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006460 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006461 // /* HeapReference<Class> */ temp = obj->klass_
6462 GenerateReferenceLoadTwoRegisters(instruction,
6463 temp_loc,
6464 obj_loc,
6465 class_offset,
6466 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006467 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006468
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006469 // Do an exact check.
6470 __ cmp(temp, ShifterOperand(cls));
6471 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006472
6473 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006474 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006475 GenerateReferenceLoadOneRegister(instruction,
6476 temp_loc,
6477 component_offset,
6478 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006479 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006480 // If the component type is null, jump to the slow path to throw the exception.
6481 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6482 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6483 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006484 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006485 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006486 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006487 break;
6488 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006489
Calin Juravle98893e12015-10-02 21:05:03 +01006490 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006491 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00006492 // We cannot directly call the CheckCast runtime entry point
6493 // without resorting to a type checking slow path here (i.e. by
6494 // calling InvokeRuntime directly), as it would require to
6495 // assign fixed registers for the inputs of this HInstanceOf
6496 // instruction (following the runtime calling convention), which
6497 // might be cluttered by the potential first read barrier
6498 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006499
Roland Levillain3b359c72015-11-17 19:35:12 +00006500 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006501 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006502
6503 case TypeCheckKind::kInterfaceCheck: {
6504 // Avoid read barriers to improve performance of the fast path. We can not get false
6505 // positives by doing this.
6506 // /* HeapReference<Class> */ temp = obj->klass_
6507 GenerateReferenceLoadTwoRegisters(instruction,
6508 temp_loc,
6509 obj_loc,
6510 class_offset,
6511 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006512 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006513
6514 // /* HeapReference<Class> */ temp = temp->iftable_
6515 GenerateReferenceLoadTwoRegisters(instruction,
6516 temp_loc,
6517 temp_loc,
6518 iftable_offset,
6519 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006520 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006521 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006522 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006523 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006524 Label start_loop;
6525 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006526 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
6527 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006528 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
6529 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006530 // Go to next interface.
6531 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
6532 __ sub(maybe_temp2_loc.AsRegister<Register>(),
6533 maybe_temp2_loc.AsRegister<Register>(),
6534 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006535 // Compare the classes and continue the loop if they do not match.
6536 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
6537 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006538 break;
6539 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006540 }
6541 __ Bind(&done);
6542
Roland Levillain3b359c72015-11-17 19:35:12 +00006543 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006544}
6545
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006546void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6547 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006548 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006549 InvokeRuntimeCallingConvention calling_convention;
6550 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6551}
6552
6553void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006554 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6555 instruction,
6556 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006557 if (instruction->IsEnter()) {
6558 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6559 } else {
6560 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6561 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006562}
6563
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006564void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6565void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6566void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006567
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006568void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006569 LocationSummary* locations =
6570 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6571 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6572 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006573 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006574 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006575 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006576 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006577}
6578
6579void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6580 HandleBitwiseOperation(instruction);
6581}
6582
6583void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6584 HandleBitwiseOperation(instruction);
6585}
6586
6587void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6588 HandleBitwiseOperation(instruction);
6589}
6590
Artem Serov7fc63502016-02-09 17:15:29 +00006591
6592void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6593 LocationSummary* locations =
6594 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6595 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6596 || instruction->GetResultType() == Primitive::kPrimLong);
6597
6598 locations->SetInAt(0, Location::RequiresRegister());
6599 locations->SetInAt(1, Location::RequiresRegister());
6600 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6601}
6602
6603void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6604 LocationSummary* locations = instruction->GetLocations();
6605 Location first = locations->InAt(0);
6606 Location second = locations->InAt(1);
6607 Location out = locations->Out();
6608
6609 if (instruction->GetResultType() == Primitive::kPrimInt) {
6610 Register first_reg = first.AsRegister<Register>();
6611 ShifterOperand second_reg(second.AsRegister<Register>());
6612 Register out_reg = out.AsRegister<Register>();
6613
6614 switch (instruction->GetOpKind()) {
6615 case HInstruction::kAnd:
6616 __ bic(out_reg, first_reg, second_reg);
6617 break;
6618 case HInstruction::kOr:
6619 __ orn(out_reg, first_reg, second_reg);
6620 break;
6621 // There is no EON on arm.
6622 case HInstruction::kXor:
6623 default:
6624 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6625 UNREACHABLE();
6626 }
6627 return;
6628
6629 } else {
6630 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6631 Register first_low = first.AsRegisterPairLow<Register>();
6632 Register first_high = first.AsRegisterPairHigh<Register>();
6633 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6634 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6635 Register out_low = out.AsRegisterPairLow<Register>();
6636 Register out_high = out.AsRegisterPairHigh<Register>();
6637
6638 switch (instruction->GetOpKind()) {
6639 case HInstruction::kAnd:
6640 __ bic(out_low, first_low, second_low);
6641 __ bic(out_high, first_high, second_high);
6642 break;
6643 case HInstruction::kOr:
6644 __ orn(out_low, first_low, second_low);
6645 __ orn(out_high, first_high, second_high);
6646 break;
6647 // There is no EON on arm.
6648 case HInstruction::kXor:
6649 default:
6650 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6651 UNREACHABLE();
6652 }
6653 }
6654}
6655
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006656void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6657 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6658 if (value == 0xffffffffu) {
6659 if (out != first) {
6660 __ mov(out, ShifterOperand(first));
6661 }
6662 return;
6663 }
6664 if (value == 0u) {
6665 __ mov(out, ShifterOperand(0));
6666 return;
6667 }
6668 ShifterOperand so;
6669 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6670 __ and_(out, first, so);
6671 } else {
6672 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6673 __ bic(out, first, ShifterOperand(~value));
6674 }
6675}
6676
6677void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6678 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6679 if (value == 0u) {
6680 if (out != first) {
6681 __ mov(out, ShifterOperand(first));
6682 }
6683 return;
6684 }
6685 if (value == 0xffffffffu) {
6686 __ mvn(out, ShifterOperand(0));
6687 return;
6688 }
6689 ShifterOperand so;
6690 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6691 __ orr(out, first, so);
6692 } else {
6693 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6694 __ orn(out, first, ShifterOperand(~value));
6695 }
6696}
6697
6698void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6699 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6700 if (value == 0u) {
6701 if (out != first) {
6702 __ mov(out, ShifterOperand(first));
6703 }
6704 return;
6705 }
6706 __ eor(out, first, ShifterOperand(value));
6707}
6708
Vladimir Marko59751a72016-08-05 14:37:27 +01006709void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6710 Location first,
6711 uint64_t value) {
6712 Register out_low = out.AsRegisterPairLow<Register>();
6713 Register out_high = out.AsRegisterPairHigh<Register>();
6714 Register first_low = first.AsRegisterPairLow<Register>();
6715 Register first_high = first.AsRegisterPairHigh<Register>();
6716 uint32_t value_low = Low32Bits(value);
6717 uint32_t value_high = High32Bits(value);
6718 if (value_low == 0u) {
6719 if (out_low != first_low) {
6720 __ mov(out_low, ShifterOperand(first_low));
6721 }
6722 __ AddConstant(out_high, first_high, value_high);
6723 return;
6724 }
6725 __ AddConstantSetFlags(out_low, first_low, value_low);
6726 ShifterOperand so;
6727 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6728 __ adc(out_high, first_high, so);
6729 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6730 __ sbc(out_high, first_high, so);
6731 } else {
6732 LOG(FATAL) << "Unexpected constant " << value_high;
6733 UNREACHABLE();
6734 }
6735}
6736
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006737void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6738 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006739 Location first = locations->InAt(0);
6740 Location second = locations->InAt(1);
6741 Location out = locations->Out();
6742
6743 if (second.IsConstant()) {
6744 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6745 uint32_t value_low = Low32Bits(value);
6746 if (instruction->GetResultType() == Primitive::kPrimInt) {
6747 Register first_reg = first.AsRegister<Register>();
6748 Register out_reg = out.AsRegister<Register>();
6749 if (instruction->IsAnd()) {
6750 GenerateAndConst(out_reg, first_reg, value_low);
6751 } else if (instruction->IsOr()) {
6752 GenerateOrrConst(out_reg, first_reg, value_low);
6753 } else {
6754 DCHECK(instruction->IsXor());
6755 GenerateEorConst(out_reg, first_reg, value_low);
6756 }
6757 } else {
6758 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6759 uint32_t value_high = High32Bits(value);
6760 Register first_low = first.AsRegisterPairLow<Register>();
6761 Register first_high = first.AsRegisterPairHigh<Register>();
6762 Register out_low = out.AsRegisterPairLow<Register>();
6763 Register out_high = out.AsRegisterPairHigh<Register>();
6764 if (instruction->IsAnd()) {
6765 GenerateAndConst(out_low, first_low, value_low);
6766 GenerateAndConst(out_high, first_high, value_high);
6767 } else if (instruction->IsOr()) {
6768 GenerateOrrConst(out_low, first_low, value_low);
6769 GenerateOrrConst(out_high, first_high, value_high);
6770 } else {
6771 DCHECK(instruction->IsXor());
6772 GenerateEorConst(out_low, first_low, value_low);
6773 GenerateEorConst(out_high, first_high, value_high);
6774 }
6775 }
6776 return;
6777 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006778
6779 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006780 Register first_reg = first.AsRegister<Register>();
6781 ShifterOperand second_reg(second.AsRegister<Register>());
6782 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006783 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006784 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006785 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006786 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006787 } else {
6788 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006789 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006790 }
6791 } else {
6792 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006793 Register first_low = first.AsRegisterPairLow<Register>();
6794 Register first_high = first.AsRegisterPairHigh<Register>();
6795 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6796 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6797 Register out_low = out.AsRegisterPairLow<Register>();
6798 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006799 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006800 __ and_(out_low, first_low, second_low);
6801 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006802 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006803 __ orr(out_low, first_low, second_low);
6804 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006805 } else {
6806 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006807 __ eor(out_low, first_low, second_low);
6808 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006809 }
6810 }
6811}
6812
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006813void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
6814 HInstruction* instruction,
6815 Location out,
6816 uint32_t offset,
6817 Location maybe_temp,
6818 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006819 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006820 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006821 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006822 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006823 if (kUseBakerReadBarrier) {
6824 // Load with fast path based Baker's read barrier.
6825 // /* HeapReference<Object> */ out = *(out + offset)
6826 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006827 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006828 } else {
6829 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006830 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006831 // in the following move operation, as we will need it for the
6832 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006833 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006834 // /* HeapReference<Object> */ out = *(out + offset)
6835 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006836 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006837 }
6838 } else {
6839 // Plain load with no read barrier.
6840 // /* HeapReference<Object> */ out = *(out + offset)
6841 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6842 __ MaybeUnpoisonHeapReference(out_reg);
6843 }
6844}
6845
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006846void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
6847 HInstruction* instruction,
6848 Location out,
6849 Location obj,
6850 uint32_t offset,
6851 Location maybe_temp,
6852 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006853 Register out_reg = out.AsRegister<Register>();
6854 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006855 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006856 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006857 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006858 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006859 // Load with fast path based Baker's read barrier.
6860 // /* HeapReference<Object> */ out = *(obj + offset)
6861 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006862 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006863 } else {
6864 // Load with slow path based read barrier.
6865 // /* HeapReference<Object> */ out = *(obj + offset)
6866 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6867 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6868 }
6869 } else {
6870 // Plain load with no read barrier.
6871 // /* HeapReference<Object> */ out = *(obj + offset)
6872 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6873 __ MaybeUnpoisonHeapReference(out_reg);
6874 }
6875}
6876
6877void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6878 Location root,
6879 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006880 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006881 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00006882 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006883 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006884 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006885 if (kUseBakerReadBarrier) {
6886 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6887 // Baker's read barrier are used:
6888 //
6889 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006890 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6891 // if (temp != null) {
6892 // root = temp(root)
Roland Levillainc9285912015-12-18 10:38:42 +00006893 // }
6894
6895 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6896 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6897 static_assert(
6898 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6899 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6900 "have different sizes.");
6901 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6902 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6903 "have different sizes.");
6904
Vladimir Marko953437b2016-08-24 08:30:46 +00006905 // Slow path marking the GC root `root`.
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006906 Location temp = Location::RegisterLocation(LR);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006907 SlowPathCodeARM* slow_path =
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006908 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
6909 instruction,
6910 root,
6911 /*entrypoint*/ temp);
Roland Levillainc9285912015-12-18 10:38:42 +00006912 codegen_->AddSlowPath(slow_path);
6913
Mathieu Chartierfe814e82016-11-09 14:32:49 -08006914 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6915 const int32_t entry_point_offset =
6916 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
6917 // Loading the entrypoint does not require a load acquire since it is only changed when
6918 // threads are suspended or running a checkpoint.
6919 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6920 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6921 // checking GetIsGcMarking.
6922 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00006923 __ Bind(slow_path->GetExitLabel());
6924 } else {
6925 // GC root loaded through a slow path for read barriers other
6926 // than Baker's.
6927 // /* GcRoot<mirror::Object>* */ root = obj + offset
6928 __ AddConstant(root_reg, obj, offset);
6929 // /* mirror::Object* */ root = root->Read()
6930 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6931 }
6932 } else {
6933 // Plain GC root load with no read barrier.
6934 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6935 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6936 // Note that GC roots are not affected by heap poisoning, thus we
6937 // do not have to unpoison `root_reg` here.
6938 }
6939}
6940
6941void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6942 Location ref,
6943 Register obj,
6944 uint32_t offset,
6945 Location temp,
6946 bool needs_null_check) {
6947 DCHECK(kEmitCompilerReadBarrier);
6948 DCHECK(kUseBakerReadBarrier);
6949
6950 // /* HeapReference<Object> */ ref = *(obj + offset)
6951 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006952 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006953 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006954 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006955}
6956
6957void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6958 Location ref,
6959 Register obj,
6960 uint32_t data_offset,
6961 Location index,
6962 Location temp,
6963 bool needs_null_check) {
6964 DCHECK(kEmitCompilerReadBarrier);
6965 DCHECK(kUseBakerReadBarrier);
6966
Roland Levillainbfea3352016-06-23 13:48:47 +01006967 static_assert(
6968 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6969 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006970 // /* HeapReference<Object> */ ref =
6971 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006972 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006973 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006974 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006975}
6976
6977void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6978 Location ref,
6979 Register obj,
6980 uint32_t offset,
6981 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006982 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006983 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006984 bool needs_null_check,
6985 bool always_update_field,
6986 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00006987 DCHECK(kEmitCompilerReadBarrier);
6988 DCHECK(kUseBakerReadBarrier);
6989
6990 // In slow path based read barriers, the read barrier call is
6991 // inserted after the original load. However, in fast path based
6992 // Baker's read barriers, we need to perform the load of
6993 // mirror::Object::monitor_ *before* the original reference load.
6994 // This load-load ordering is required by the read barrier.
6995 // The fast path/slow path (for Baker's algorithm) should look like:
6996 //
6997 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6998 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6999 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007000 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillainc9285912015-12-18 10:38:42 +00007001 // if (is_gray) {
7002 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7003 // }
7004 //
7005 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007006 // slightly more complex as it performs additional checks that we do
7007 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00007008
7009 Register ref_reg = ref.AsRegister<Register>();
7010 Register temp_reg = temp.AsRegister<Register>();
7011 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7012
7013 // /* int32_t */ monitor = obj->monitor_
7014 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7015 if (needs_null_check) {
7016 MaybeRecordImplicitNullCheck(instruction);
7017 }
7018 // /* LockWord */ lock_word = LockWord(monitor)
7019 static_assert(sizeof(LockWord) == sizeof(int32_t),
7020 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007021
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007022 // Introduce a dependency on the lock_word including the rb_state,
7023 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00007024 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01007025 // `obj` is unchanged by this operation, but its value now depends
7026 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007027 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00007028
7029 // The actual reference load.
7030 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007031 // Load types involving an "index": ArrayGet,
7032 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7033 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01007034 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00007035 if (index.IsConstant()) {
7036 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01007037 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00007038 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7039 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01007040 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007041 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7042 // intrinsics, which use a register pair as index ("long
7043 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01007044 Register index_reg = index.IsRegisterPair()
7045 ? index.AsRegisterPairLow<Register>()
7046 : index.AsRegister<Register>();
7047 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00007048 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
7049 }
7050 } else {
7051 // /* HeapReference<Object> */ ref = *(obj + offset)
7052 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7053 }
7054
7055 // Object* ref = ref_addr->AsMirrorPtr()
7056 __ MaybeUnpoisonHeapReference(ref_reg);
7057
Vladimir Marko953437b2016-08-24 08:30:46 +00007058 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007059 SlowPathCodeARM* slow_path;
7060 if (always_update_field) {
7061 DCHECK(temp2 != nullptr);
7062 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
7063 // of the form `obj + field_offset`, where `obj` is a register and
7064 // `field_offset` is a register pair (of which only the lower half
7065 // is used). Thus `offset` and `scale_factor` above are expected
7066 // to be null in this code path.
7067 DCHECK_EQ(offset, 0u);
7068 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7069 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
7070 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7071 } else {
7072 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
7073 }
Roland Levillainc9285912015-12-18 10:38:42 +00007074 AddSlowPath(slow_path);
7075
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007076 // if (rb_state == ReadBarrier::GrayState())
Roland Levillainc9285912015-12-18 10:38:42 +00007077 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007078 // Given the numeric representation, it's enough to check the low bit of the
7079 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7080 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007081 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7082 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007083 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7084 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00007085 __ Bind(slow_path->GetExitLabel());
7086}
7087
7088void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
7089 Location out,
7090 Location ref,
7091 Location obj,
7092 uint32_t offset,
7093 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007094 DCHECK(kEmitCompilerReadBarrier);
7095
Roland Levillainc9285912015-12-18 10:38:42 +00007096 // Insert a slow path based read barrier *after* the reference load.
7097 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007098 // If heap poisoning is enabled, the unpoisoning of the loaded
7099 // reference will be carried out by the runtime within the slow
7100 // path.
7101 //
7102 // Note that `ref` currently does not get unpoisoned (when heap
7103 // poisoning is enabled), which is alright as the `ref` argument is
7104 // not used by the artReadBarrierSlow entry point.
7105 //
7106 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007107 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00007108 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
7109 AddSlowPath(slow_path);
7110
Roland Levillain3b359c72015-11-17 19:35:12 +00007111 __ b(slow_path->GetEntryLabel());
7112 __ Bind(slow_path->GetExitLabel());
7113}
7114
Roland Levillainc9285912015-12-18 10:38:42 +00007115void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7116 Location out,
7117 Location ref,
7118 Location obj,
7119 uint32_t offset,
7120 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007121 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00007122 // Baker's read barriers shall be handled by the fast path
7123 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
7124 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007125 // If heap poisoning is enabled, unpoisoning will be taken care of
7126 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007127 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00007128 } else if (kPoisonHeapReferences) {
7129 __ UnpoisonHeapReference(out.AsRegister<Register>());
7130 }
7131}
7132
Roland Levillainc9285912015-12-18 10:38:42 +00007133void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7134 Location out,
7135 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007136 DCHECK(kEmitCompilerReadBarrier);
7137
Roland Levillainc9285912015-12-18 10:38:42 +00007138 // Insert a slow path based read barrier *after* the GC root load.
7139 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007140 // Note that GC roots are not affected by heap poisoning, so we do
7141 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007142 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007143 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
7144 AddSlowPath(slow_path);
7145
Roland Levillain3b359c72015-11-17 19:35:12 +00007146 __ b(slow_path->GetEntryLabel());
7147 __ Bind(slow_path->GetExitLabel());
7148}
7149
Vladimir Markodc151b22015-10-15 18:02:30 +01007150HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7151 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007152 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007153 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
7154 // We disable pc-relative load when there is an irreducible loop, as the optimization
7155 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007156 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
7157 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007158 if (GetGraph()->HasIrreducibleLoops() &&
7159 (dispatch_info.method_load_kind ==
7160 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
7161 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
7162 }
7163
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007164 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007165}
7166
Vladimir Markob4536b72015-11-24 13:45:23 +00007167Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7168 Register temp) {
7169 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7170 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7171 if (!invoke->GetLocations()->Intrinsified()) {
7172 return location.AsRegister<Register>();
7173 }
7174 // For intrinsics we allow any location, so it may be on the stack.
7175 if (!location.IsRegister()) {
7176 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7177 return temp;
7178 }
7179 // For register locations, check if the register was saved. If so, get it from the stack.
7180 // Note: There is a chance that the register was saved but not overwritten, so we could
7181 // save one load. However, since this is just an intrinsic slow path we prefer this
7182 // simple and more robust approach rather that trying to determine if that's the case.
7183 SlowPathCode* slow_path = GetCurrentSlowPath();
7184 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7185 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7186 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7187 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7188 return temp;
7189 }
7190 return location.AsRegister<Register>();
7191}
7192
Nicolas Geoffray38207af2015-06-01 15:46:22 +01007193void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007194 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7195 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007196 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7197 uint32_t offset =
7198 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007199 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007200 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007201 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007202 }
Vladimir Marko58155012015-08-19 12:49:41 +00007203 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007204 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007205 break;
7206 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7207 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7208 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007209 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7210 HArmDexCacheArraysBase* base =
7211 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7212 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7213 temp.AsRegister<Register>());
7214 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7215 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7216 break;
7217 }
Vladimir Marko58155012015-08-19 12:49:41 +00007218 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007219 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007220 Register method_reg;
7221 Register reg = temp.AsRegister<Register>();
7222 if (current_method.IsRegister()) {
7223 method_reg = current_method.AsRegister<Register>();
7224 } else {
7225 DCHECK(invoke->GetLocations()->Intrinsified());
7226 DCHECK(!current_method.IsValid());
7227 method_reg = reg;
7228 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7229 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007230 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7231 __ LoadFromOffset(kLoadWord,
7232 reg,
7233 method_reg,
7234 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007235 // temp = temp[index_in_cache];
7236 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7237 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007238 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7239 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007240 }
Vladimir Marko58155012015-08-19 12:49:41 +00007241 }
7242
7243 switch (invoke->GetCodePtrLocation()) {
7244 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7245 __ bl(GetFrameEntryLabel());
7246 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007247 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7248 // LR = callee_method->entry_point_from_quick_compiled_code_
7249 __ LoadFromOffset(
7250 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007251 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007252 // LR()
7253 __ blx(LR);
7254 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007255 }
7256
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007257 DCHECK(!IsLeafMethod());
7258}
7259
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007260void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7261 Register temp = temp_location.AsRegister<Register>();
7262 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7263 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007264
7265 // Use the calling convention instead of the location of the receiver, as
7266 // intrinsics may have put the receiver in a different register. In the intrinsics
7267 // slow path, the arguments have been moved to the right place, so here we are
7268 // guaranteed that the receiver is the first register of the calling convention.
7269 InvokeDexCallingConvention calling_convention;
7270 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007271 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007272 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007273 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007274 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007275 // Instead of simply (possibly) unpoisoning `temp` here, we should
7276 // emit a read barrier for the previous class reference load.
7277 // However this is not required in practice, as this is an
7278 // intermediate/temporary reference and because the current
7279 // concurrent copying collector keeps the from-space memory
7280 // intact/accessible until the end of the marking phase (the
7281 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007282 __ MaybeUnpoisonHeapReference(temp);
7283 // temp = temp->GetMethodAt(method_offset);
7284 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007285 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007286 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7287 // LR = temp->GetEntryPoint();
7288 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7289 // LR();
7290 __ blx(LR);
7291}
7292
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007293CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007294 const DexFile& dex_file, dex::StringIndex string_index) {
7295 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007296}
7297
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007298CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08007299 const DexFile& dex_file, dex::TypeIndex type_index) {
7300 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007301}
7302
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007303CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7304 const DexFile& dex_file, uint32_t element_offset) {
7305 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7306}
7307
7308CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7309 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7310 patches->emplace_back(dex_file, offset_or_index);
7311 return &patches->back();
7312}
7313
7314Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007315 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007316 return boot_image_string_patches_.GetOrCreate(
7317 StringReference(&dex_file, string_index),
7318 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7319}
7320
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007321Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007322 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007323 return boot_image_type_patches_.GetOrCreate(
7324 TypeReference(&dex_file, type_index),
7325 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7326}
7327
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007328Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7329 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7330 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7331 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7332}
7333
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007334Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007335 dex::StringIndex string_index,
7336 Handle<mirror::String> handle) {
7337 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7338 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007339 return jit_string_patches_.GetOrCreate(
7340 StringReference(&dex_file, string_index),
7341 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7342}
7343
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007344Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
7345 dex::TypeIndex type_index,
7346 uint64_t address) {
7347 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index), address);
7348 return jit_class_patches_.GetOrCreate(
7349 TypeReference(&dex_file, type_index),
7350 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7351}
7352
Vladimir Markoaad75c62016-10-03 08:46:48 +00007353template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7354inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7355 const ArenaDeque<PcRelativePatchInfo>& infos,
7356 ArenaVector<LinkerPatch>* linker_patches) {
7357 for (const PcRelativePatchInfo& info : infos) {
7358 const DexFile& dex_file = info.target_dex_file;
7359 size_t offset_or_index = info.offset_or_index;
7360 DCHECK(info.add_pc_label.IsBound());
7361 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7362 // Add MOVW patch.
7363 DCHECK(info.movw_label.IsBound());
7364 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7365 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7366 // Add MOVT patch.
7367 DCHECK(info.movt_label.IsBound());
7368 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7369 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7370 }
7371}
7372
Vladimir Marko58155012015-08-19 12:49:41 +00007373void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7374 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007375 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00007376 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007377 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007378 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007379 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007380 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007381 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007382 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007383 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7384 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007385 for (const auto& entry : boot_image_string_patches_) {
7386 const StringReference& target_string = entry.first;
7387 Literal* literal = entry.second;
7388 DCHECK(literal->GetLabel()->IsBound());
7389 uint32_t literal_offset = literal->GetLabel()->Position();
7390 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7391 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007392 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007393 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007394 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007395 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(pc_relative_type_patches_,
7396 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007397 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7398 linker_patches);
7399 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007400 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7401 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007402 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7403 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007404 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007405 for (const auto& entry : boot_image_type_patches_) {
7406 const TypeReference& target_type = entry.first;
7407 Literal* literal = entry.second;
7408 DCHECK(literal->GetLabel()->IsBound());
7409 uint32_t literal_offset = literal->GetLabel()->Position();
7410 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7411 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007412 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007413 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007414 for (const auto& entry : boot_image_address_patches_) {
7415 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7416 Literal* literal = entry.second;
7417 DCHECK(literal->GetLabel()->IsBound());
7418 uint32_t literal_offset = literal->GetLabel()->Position();
7419 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7420 }
7421}
7422
7423Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7424 return map->GetOrCreate(
7425 value,
7426 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007427}
7428
7429Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7430 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007431 return map->GetOrCreate(
7432 target_method,
7433 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007434}
7435
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007436void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7437 LocationSummary* locations =
7438 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7439 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7440 Location::RequiresRegister());
7441 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7442 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7443 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7444}
7445
7446void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7447 LocationSummary* locations = instr->GetLocations();
7448 Register res = locations->Out().AsRegister<Register>();
7449 Register accumulator =
7450 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7451 Register mul_left =
7452 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7453 Register mul_right =
7454 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7455
7456 if (instr->GetOpKind() == HInstruction::kAdd) {
7457 __ mla(res, mul_left, mul_right, accumulator);
7458 } else {
7459 __ mls(res, mul_left, mul_right, accumulator);
7460 }
7461}
7462
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007463void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007464 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007465 LOG(FATAL) << "Unreachable";
7466}
7467
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007468void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007469 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007470 LOG(FATAL) << "Unreachable";
7471}
7472
Mark Mendellfe57faa2015-09-18 09:26:15 -04007473// Simple implementation of packed switch - generate cascaded compare/jumps.
7474void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7475 LocationSummary* locations =
7476 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7477 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007478 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007479 codegen_->GetAssembler()->IsThumb()) {
7480 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7481 if (switch_instr->GetStartValue() != 0) {
7482 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7483 }
7484 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007485}
7486
7487void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7488 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007489 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007490 LocationSummary* locations = switch_instr->GetLocations();
7491 Register value_reg = locations->InAt(0).AsRegister<Register>();
7492 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7493
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007494 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007495 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007496 Register temp_reg = IP;
7497 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7498 // the immediate, because IP is used as the destination register. For the other
7499 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7500 // and they can be encoded in the instruction without making use of IP register.
7501 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7502
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007503 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007504 // Jump to successors[0] if value == lower_bound.
7505 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7506 int32_t last_index = 0;
7507 for (; num_entries - last_index > 2; last_index += 2) {
7508 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7509 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7510 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7511 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7512 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7513 }
7514 if (num_entries - last_index == 2) {
7515 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007516 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007517 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007518 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007519
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007520 // And the default for any other value.
7521 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7522 __ b(codegen_->GetLabelOf(default_block));
7523 }
7524 } else {
7525 // Create a table lookup.
7526 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7527
7528 // Materialize a pointer to the switch table
7529 std::vector<Label*> labels(num_entries);
7530 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7531 for (uint32_t i = 0; i < num_entries; i++) {
7532 labels[i] = codegen_->GetLabelOf(successors[i]);
7533 }
7534 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7535
7536 // Remove the bias.
7537 Register key_reg;
7538 if (lower_bound != 0) {
7539 key_reg = locations->GetTemp(1).AsRegister<Register>();
7540 __ AddConstant(key_reg, value_reg, -lower_bound);
7541 } else {
7542 key_reg = value_reg;
7543 }
7544
7545 // Check whether the value is in the table, jump to default block if not.
7546 __ CmpConstant(key_reg, num_entries - 1);
7547 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7548
7549 // Load the displacement from the table.
7550 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7551
7552 // Dispatch is a direct add to the PC (for Thumb2).
7553 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007554 }
7555}
7556
Vladimir Markob4536b72015-11-24 13:45:23 +00007557void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7558 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7559 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007560}
7561
7562void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7563 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007564 CodeGeneratorARM::PcRelativePatchInfo* labels =
7565 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007566 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007567 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007568 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007569 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007570 __ BindTrackedLabel(&labels->add_pc_label);
7571 __ add(base_reg, base_reg, ShifterOperand(PC));
7572}
7573
Andreas Gampe85b62f22015-09-09 13:15:38 -07007574void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7575 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007576 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007577 return;
7578 }
7579
7580 DCHECK_NE(type, Primitive::kPrimVoid);
7581
7582 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7583 if (return_loc.Equals(trg)) {
7584 return;
7585 }
7586
7587 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7588 // with the last branch.
7589 if (type == Primitive::kPrimLong) {
7590 HParallelMove parallel_move(GetGraph()->GetArena());
7591 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7592 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7593 GetMoveResolver()->EmitNativeCode(&parallel_move);
7594 } else if (type == Primitive::kPrimDouble) {
7595 HParallelMove parallel_move(GetGraph()->GetArena());
7596 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7597 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7598 GetMoveResolver()->EmitNativeCode(&parallel_move);
7599 } else {
7600 // Let the parallel move resolver take care of all of this.
7601 HParallelMove parallel_move(GetGraph()->GetArena());
7602 parallel_move.AddMove(return_loc, trg, type, nullptr);
7603 GetMoveResolver()->EmitNativeCode(&parallel_move);
7604 }
7605}
7606
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007607void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7608 LocationSummary* locations =
7609 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7610 locations->SetInAt(0, Location::RequiresRegister());
7611 locations->SetOut(Location::RequiresRegister());
7612}
7613
7614void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7615 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007616 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007617 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007618 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007619 __ LoadFromOffset(kLoadWord,
7620 locations->Out().AsRegister<Register>(),
7621 locations->InAt(0).AsRegister<Register>(),
7622 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007623 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007624 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007625 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007626 __ LoadFromOffset(kLoadWord,
7627 locations->Out().AsRegister<Register>(),
7628 locations->InAt(0).AsRegister<Register>(),
7629 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7630 __ LoadFromOffset(kLoadWord,
7631 locations->Out().AsRegister<Register>(),
7632 locations->Out().AsRegister<Register>(),
7633 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007634 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007635}
7636
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007637static void PatchJitRootUse(uint8_t* code,
7638 const uint8_t* roots_data,
7639 Literal* literal,
7640 uint64_t index_in_table) {
7641 DCHECK(literal->GetLabel()->IsBound());
7642 uint32_t literal_offset = literal->GetLabel()->Position();
7643 uintptr_t address =
7644 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7645 uint8_t* data = code + literal_offset;
7646 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7647}
7648
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007649void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7650 for (const auto& entry : jit_string_patches_) {
7651 const auto& it = jit_string_roots_.find(entry.first);
7652 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007653 PatchJitRootUse(code, roots_data, entry.second, it->second);
7654 }
7655 for (const auto& entry : jit_class_patches_) {
7656 const auto& it = jit_class_roots_.find(entry.first);
7657 DCHECK(it != jit_class_roots_.end());
7658 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007659 }
7660}
7661
Roland Levillain4d027112015-07-01 15:41:14 +01007662#undef __
7663#undef QUICK_ENTRY_POINT
7664
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007665} // namespace arm
7666} // namespace art