blob: 57823c968455dba6927257abbcada66684386644 [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)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100374 : SlowPathCodeARM(at), cls_(cls), at_(at), 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 {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000379 LocationSummary* locations = at_->GetLocations();
380
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;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000386 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100387 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
388 : kQuickInitializeType;
389 arm_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000390 if (do_clinit_) {
391 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
392 } else {
393 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
394 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000395
396 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000397 Location out = locations->Out();
398 if (out.IsValid()) {
399 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000400 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
401 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000402 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100403 __ b(GetExitLabel());
404 }
405
Alexandre Rames9931f312015-06-19 14:47:01 +0100406 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
407
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100408 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000409 // The class this slow path will load.
410 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100411
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000412 // The instruction where this slow path is happening.
413 // (Might be the load class or an initialization check).
414 HInstruction* const at_;
415
416 // The dex PC of `at_`.
417 const uint32_t dex_pc_;
418
419 // Whether to initialize the class.
420 const bool do_clinit_;
421
422 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100423};
424
Vladimir Markoaad75c62016-10-03 08:46:48 +0000425class LoadStringSlowPathARM : public SlowPathCodeARM {
426 public:
427 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
428
429 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
430 LocationSummary* locations = instruction_->GetLocations();
431 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100432 HLoadString* load = instruction_->AsLoadString();
433 const uint32_t string_index = load->GetStringIndex();
434 Register out = locations->Out().AsRegister<Register>();
435 Register temp = locations->GetTemp(0).AsRegister<Register>();
436 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000437
438 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
439 __ Bind(GetEntryLabel());
440 SaveLiveRegisters(codegen, locations);
441
442 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100443 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
444 // the kSaveEverything call (or use `out` for the address after non-kSaveEverything call).
445 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
446 Register entry_address = temp_is_r0 ? out : temp;
447 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
448 if (call_saves_everything_except_r0 && temp_is_r0) {
449 __ mov(entry_address, ShifterOperand(temp));
450 }
451
Vladimir Markoaad75c62016-10-03 08:46:48 +0000452 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index);
453 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
454 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100455
456 // Store the resolved String to the .bss entry.
457 if (call_saves_everything_except_r0) {
458 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
459 __ str(R0, Address(entry_address));
460 } else {
461 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
462 CodeGeneratorARM::PcRelativePatchInfo* labels =
463 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
464 __ BindTrackedLabel(&labels->movw_label);
465 __ movw(entry_address, /* placeholder */ 0u);
466 __ BindTrackedLabel(&labels->movt_label);
467 __ movt(entry_address, /* placeholder */ 0u);
468 __ BindTrackedLabel(&labels->add_pc_label);
469 __ add(entry_address, entry_address, ShifterOperand(PC));
470 __ str(R0, Address(entry_address));
471 }
472
Vladimir Markoaad75c62016-10-03 08:46:48 +0000473 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000474 RestoreLiveRegisters(codegen, locations);
475
Vladimir Markoaad75c62016-10-03 08:46:48 +0000476 __ b(GetExitLabel());
477 }
478
479 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
480
481 private:
482 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
483};
484
Artem Serovf4d6aee2016-07-11 10:41:45 +0100485class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000486 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000487 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100488 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000489
Alexandre Rames67555f72014-11-18 10:55:16 +0000490 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000491 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100492 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
493 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000494 DCHECK(instruction_->IsCheckCast()
495 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000496
497 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
498 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000499
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000500 if (!is_fatal_) {
501 SaveLiveRegisters(codegen, locations);
502 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000503
504 // We're moving two locations to locations that could overlap, so we need a parallel
505 // move resolver.
506 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000507 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100508 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000509 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100510 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100511 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100512 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
513 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000514
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000515 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100516 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100517 instruction_,
518 instruction_->GetDexPc(),
519 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000520 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700521 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000522 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
523 } else {
524 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100525 arm_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000526 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000527 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000528
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000529 if (!is_fatal_) {
530 RestoreLiveRegisters(codegen, locations);
531 __ b(GetExitLabel());
532 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000533 }
534
Alexandre Rames9931f312015-06-19 14:47:01 +0100535 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
536
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000537 bool IsFatal() const OVERRIDE { return is_fatal_; }
538
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000539 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000540 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000541
542 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
543};
544
Artem Serovf4d6aee2016-07-11 10:41:45 +0100545class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700546 public:
Aart Bik42249c32016-01-07 15:33:50 -0800547 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100548 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700549
550 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800551 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700552 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100553 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000554 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700555 }
556
Alexandre Rames9931f312015-06-19 14:47:01 +0100557 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
558
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700559 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700560 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
561};
562
Artem Serovf4d6aee2016-07-11 10:41:45 +0100563class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100564 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100565 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100566
567 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
568 LocationSummary* locations = instruction_->GetLocations();
569 __ Bind(GetEntryLabel());
570 SaveLiveRegisters(codegen, locations);
571
572 InvokeRuntimeCallingConvention calling_convention;
573 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
574 parallel_move.AddMove(
575 locations->InAt(0),
576 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
577 Primitive::kPrimNot,
578 nullptr);
579 parallel_move.AddMove(
580 locations->InAt(1),
581 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
582 Primitive::kPrimInt,
583 nullptr);
584 parallel_move.AddMove(
585 locations->InAt(2),
586 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
587 Primitive::kPrimNot,
588 nullptr);
589 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
590
591 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100592 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000593 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100594 RestoreLiveRegisters(codegen, locations);
595 __ b(GetExitLabel());
596 }
597
598 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
599
600 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100601 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
602};
603
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100604// Slow path marking an object reference `ref` during a read
605// barrier. The field `obj.field` in the object `obj` holding this
606// reference does not get updated by this slow path after marking (see
607// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
608//
609// This means that after the execution of this slow path, `ref` will
610// always be up-to-date, but `obj.field` may not; i.e., after the
611// flip, `ref` will be a to-space reference, but `obj.field` will
612// probably still be a from-space reference (unless it gets updated by
613// another thread, or if another thread installed another object
614// reference (different from `ref`) in `obj.field`).
Artem Serovf4d6aee2016-07-11 10:41:45 +0100615class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000616 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100617 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location ref)
618 : SlowPathCodeARM(instruction), ref_(ref) {
Roland Levillainc9285912015-12-18 10:38:42 +0000619 DCHECK(kEmitCompilerReadBarrier);
620 }
621
622 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
623
624 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
625 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100626 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000627 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100628 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillainc9285912015-12-18 10:38:42 +0000629 DCHECK(instruction_->IsInstanceFieldGet() ||
630 instruction_->IsStaticFieldGet() ||
631 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100632 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000633 instruction_->IsLoadClass() ||
634 instruction_->IsLoadString() ||
635 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100636 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100637 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
638 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000639 << "Unexpected instruction in read barrier marking slow path: "
640 << instruction_->DebugName();
641
642 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100643 // No need to save live registers; it's taken care of by the
644 // entrypoint. Also, there is no need to update the stack mask,
645 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000646 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100647 DCHECK_NE(ref_reg, SP);
648 DCHECK_NE(ref_reg, LR);
649 DCHECK_NE(ref_reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100650 // IP is used internally by the ReadBarrierMarkRegX entry point
651 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100652 DCHECK_NE(ref_reg, IP);
653 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100654 // "Compact" slow path, saving two moves.
655 //
656 // Instead of using the standard runtime calling convention (input
657 // and output in R0):
658 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100659 // R0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100660 // R0 <- ReadBarrierMark(R0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100661 // ref <- R0
Roland Levillain02b75802016-07-13 11:54:35 +0100662 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100663 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100664 // of a dedicated entrypoint:
665 //
666 // rX <- ReadBarrierMarkRegX(rX)
667 //
668 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100669 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100670 // This runtime call does not require a stack map.
671 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000672 __ b(GetExitLabel());
673 }
674
675 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100676 // The location (register) of the marked object reference.
677 const Location ref_;
Roland Levillainc9285912015-12-18 10:38:42 +0000678
679 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
680};
681
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100682// Slow path marking an object reference `ref` during a read barrier,
683// and if needed, atomically updating the field `obj.field` in the
684// object `obj` holding this reference after marking (contrary to
685// ReadBarrierMarkSlowPathARM above, which never tries to update
686// `obj.field`).
687//
688// This means that after the execution of this slow path, both `ref`
689// and `obj.field` will be up-to-date; i.e., after the flip, both will
690// hold the same to-space reference (unless another thread installed
691// another object reference (different from `ref`) in `obj.field`).
692class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
693 public:
694 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
695 Location ref,
696 Register obj,
697 Location field_offset,
698 Register temp1,
699 Register temp2)
700 : SlowPathCodeARM(instruction),
701 ref_(ref),
702 obj_(obj),
703 field_offset_(field_offset),
704 temp1_(temp1),
705 temp2_(temp2) {
706 DCHECK(kEmitCompilerReadBarrier);
707 }
708
709 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
710
711 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
712 LocationSummary* locations = instruction_->GetLocations();
713 Register ref_reg = ref_.AsRegister<Register>();
714 DCHECK(locations->CanCall());
715 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
716 // This slow path is only used by the UnsafeCASObject intrinsic.
717 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
718 << "Unexpected instruction in read barrier marking and field updating slow path: "
719 << instruction_->DebugName();
720 DCHECK(instruction_->GetLocations()->Intrinsified());
721 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
722 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
723
724 __ Bind(GetEntryLabel());
725
726 // Save the old reference.
727 // Note that we cannot use IP to save the old reference, as IP is
728 // used internally by the ReadBarrierMarkRegX entry point, and we
729 // need the old reference after the call to that entry point.
730 DCHECK_NE(temp1_, IP);
731 __ Mov(temp1_, ref_reg);
732
733 // No need to save live registers; it's taken care of by the
734 // entrypoint. Also, there is no need to update the stack mask,
735 // as this runtime call will not trigger a garbage collection.
736 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
737 DCHECK_NE(ref_reg, SP);
738 DCHECK_NE(ref_reg, LR);
739 DCHECK_NE(ref_reg, PC);
740 // IP is used internally by the ReadBarrierMarkRegX entry point
741 // as a temporary, it cannot be the entry point's input/output.
742 DCHECK_NE(ref_reg, IP);
743 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
744 // "Compact" slow path, saving two moves.
745 //
746 // Instead of using the standard runtime calling convention (input
747 // and output in R0):
748 //
749 // R0 <- ref
750 // R0 <- ReadBarrierMark(R0)
751 // ref <- R0
752 //
753 // we just use rX (the register containing `ref`) as input and output
754 // of a dedicated entrypoint:
755 //
756 // rX <- ReadBarrierMarkRegX(rX)
757 //
758 int32_t entry_point_offset =
759 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
760 // This runtime call does not require a stack map.
761 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
762
763 // If the new reference is different from the old reference,
764 // update the field in the holder (`*(obj_ + field_offset_)`).
765 //
766 // Note that this field could also hold a different object, if
767 // another thread had concurrently changed it. In that case, the
768 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
769 // (CAS) operation below would abort the CAS, leaving the field
770 // as-is.
771 Label done;
772 __ cmp(temp1_, ShifterOperand(ref_reg));
773 __ b(&done, EQ);
774
775 // Update the the holder's field atomically. This may fail if
776 // mutator updates before us, but it's OK. This is achieved
777 // using a strong compare-and-set (CAS) operation with relaxed
778 // memory synchronization ordering, where the expected value is
779 // the old reference and the desired value is the new reference.
780
781 // Convenience aliases.
782 Register base = obj_;
783 // The UnsafeCASObject intrinsic uses a register pair as field
784 // offset ("long offset"), of which only the low part contains
785 // data.
786 Register offset = field_offset_.AsRegisterPairLow<Register>();
787 Register expected = temp1_;
788 Register value = ref_reg;
789 Register tmp_ptr = IP; // Pointer to actual memory.
790 Register tmp = temp2_; // Value in memory.
791
792 __ add(tmp_ptr, base, ShifterOperand(offset));
793
794 if (kPoisonHeapReferences) {
795 __ PoisonHeapReference(expected);
796 if (value == expected) {
797 // Do not poison `value`, as it is the same register as
798 // `expected`, which has just been poisoned.
799 } else {
800 __ PoisonHeapReference(value);
801 }
802 }
803
804 // do {
805 // tmp = [r_ptr] - expected;
806 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
807
Roland Levillain24a4d112016-10-26 13:10:46 +0100808 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100809 __ Bind(&loop_head);
810
811 __ ldrex(tmp, tmp_ptr);
812
813 __ subs(tmp, tmp, ShifterOperand(expected));
814
Roland Levillain24a4d112016-10-26 13:10:46 +0100815 __ it(NE);
816 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100817
Roland Levillain24a4d112016-10-26 13:10:46 +0100818 __ b(&exit_loop, NE);
819
820 __ strex(tmp, value, tmp_ptr);
821 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100822 __ b(&loop_head, EQ);
823
Roland Levillain24a4d112016-10-26 13:10:46 +0100824 __ Bind(&exit_loop);
825
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100826 if (kPoisonHeapReferences) {
827 __ UnpoisonHeapReference(expected);
828 if (value == expected) {
829 // Do not unpoison `value`, as it is the same register as
830 // `expected`, which has just been unpoisoned.
831 } else {
832 __ UnpoisonHeapReference(value);
833 }
834 }
835
836 __ Bind(&done);
837 __ b(GetExitLabel());
838 }
839
840 private:
841 // The location (register) of the marked object reference.
842 const Location ref_;
843 // The register containing the object holding the marked object reference field.
844 const Register obj_;
845 // The location of the offset of the marked reference field within `obj_`.
846 Location field_offset_;
847
848 const Register temp1_;
849 const Register temp2_;
850
851 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
852};
853
Roland Levillain3b359c72015-11-17 19:35:12 +0000854// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100855class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000856 public:
857 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
858 Location out,
859 Location ref,
860 Location obj,
861 uint32_t offset,
862 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100863 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000864 out_(out),
865 ref_(ref),
866 obj_(obj),
867 offset_(offset),
868 index_(index) {
869 DCHECK(kEmitCompilerReadBarrier);
870 // If `obj` is equal to `out` or `ref`, it means the initial object
871 // has been overwritten by (or after) the heap object reference load
872 // to be instrumented, e.g.:
873 //
874 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000875 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000876 //
877 // In that case, we have lost the information about the original
878 // object, and the emitted read barrier cannot work properly.
879 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
880 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
881 }
882
883 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
884 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
885 LocationSummary* locations = instruction_->GetLocations();
886 Register reg_out = out_.AsRegister<Register>();
887 DCHECK(locations->CanCall());
888 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100889 DCHECK(instruction_->IsInstanceFieldGet() ||
890 instruction_->IsStaticFieldGet() ||
891 instruction_->IsArrayGet() ||
892 instruction_->IsInstanceOf() ||
893 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100894 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000895 << "Unexpected instruction in read barrier for heap reference slow path: "
896 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000897
898 __ Bind(GetEntryLabel());
899 SaveLiveRegisters(codegen, locations);
900
901 // We may have to change the index's value, but as `index_` is a
902 // constant member (like other "inputs" of this slow path),
903 // introduce a copy of it, `index`.
904 Location index = index_;
905 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100906 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000907 if (instruction_->IsArrayGet()) {
908 // Compute the actual memory offset and store it in `index`.
909 Register index_reg = index_.AsRegister<Register>();
910 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
911 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
912 // We are about to change the value of `index_reg` (see the
913 // calls to art::arm::Thumb2Assembler::Lsl and
914 // art::arm::Thumb2Assembler::AddConstant below), but it has
915 // not been saved by the previous call to
916 // art::SlowPathCode::SaveLiveRegisters, as it is a
917 // callee-save register --
918 // art::SlowPathCode::SaveLiveRegisters does not consider
919 // callee-save registers, as it has been designed with the
920 // assumption that callee-save registers are supposed to be
921 // handled by the called function. So, as a callee-save
922 // register, `index_reg` _would_ eventually be saved onto
923 // the stack, but it would be too late: we would have
924 // changed its value earlier. Therefore, we manually save
925 // it here into another freely available register,
926 // `free_reg`, chosen of course among the caller-save
927 // registers (as a callee-save `free_reg` register would
928 // exhibit the same problem).
929 //
930 // Note we could have requested a temporary register from
931 // the register allocator instead; but we prefer not to, as
932 // this is a slow path, and we know we can find a
933 // caller-save register that is available.
934 Register free_reg = FindAvailableCallerSaveRegister(codegen);
935 __ Mov(free_reg, index_reg);
936 index_reg = free_reg;
937 index = Location::RegisterLocation(index_reg);
938 } else {
939 // The initial register stored in `index_` has already been
940 // saved in the call to art::SlowPathCode::SaveLiveRegisters
941 // (as it is not a callee-save register), so we can freely
942 // use it.
943 }
944 // Shifting the index value contained in `index_reg` by the scale
945 // factor (2) cannot overflow in practice, as the runtime is
946 // unable to allocate object arrays with a size larger than
947 // 2^26 - 1 (that is, 2^28 - 4 bytes).
948 __ Lsl(index_reg, index_reg, TIMES_4);
949 static_assert(
950 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
951 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
952 __ AddConstant(index_reg, index_reg, offset_);
953 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100954 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
955 // intrinsics, `index_` is not shifted by a scale factor of 2
956 // (as in the case of ArrayGet), as it is actually an offset
957 // to an object field within an object.
958 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000959 DCHECK(instruction_->GetLocations()->Intrinsified());
960 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
961 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
962 << instruction_->AsInvoke()->GetIntrinsic();
963 DCHECK_EQ(offset_, 0U);
964 DCHECK(index_.IsRegisterPair());
965 // UnsafeGet's offset location is a register pair, the low
966 // part contains the correct offset.
967 index = index_.ToLow();
968 }
969 }
970
971 // We're moving two or three locations to locations that could
972 // overlap, so we need a parallel move resolver.
973 InvokeRuntimeCallingConvention calling_convention;
974 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
975 parallel_move.AddMove(ref_,
976 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
977 Primitive::kPrimNot,
978 nullptr);
979 parallel_move.AddMove(obj_,
980 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
981 Primitive::kPrimNot,
982 nullptr);
983 if (index.IsValid()) {
984 parallel_move.AddMove(index,
985 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
986 Primitive::kPrimInt,
987 nullptr);
988 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
989 } else {
990 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
991 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
992 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100993 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000994 CheckEntrypointTypes<
995 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
996 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
997
998 RestoreLiveRegisters(codegen, locations);
999 __ b(GetExitLabel());
1000 }
1001
1002 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1003
1004 private:
1005 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1006 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1007 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1008 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1009 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1010 return static_cast<Register>(i);
1011 }
1012 }
1013 // We shall never fail to find a free caller-save register, as
1014 // there are more than two core caller-save registers on ARM
1015 // (meaning it is possible to find one which is different from
1016 // `ref` and `obj`).
1017 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1018 LOG(FATAL) << "Could not find a free caller-save register";
1019 UNREACHABLE();
1020 }
1021
Roland Levillain3b359c72015-11-17 19:35:12 +00001022 const Location out_;
1023 const Location ref_;
1024 const Location obj_;
1025 const uint32_t offset_;
1026 // An additional location containing an index to an array.
1027 // Only used for HArrayGet and the UnsafeGetObject &
1028 // UnsafeGetObjectVolatile intrinsics.
1029 const Location index_;
1030
1031 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1032};
1033
1034// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001035class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001036 public:
1037 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001038 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001039 DCHECK(kEmitCompilerReadBarrier);
1040 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001041
1042 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1043 LocationSummary* locations = instruction_->GetLocations();
1044 Register reg_out = out_.AsRegister<Register>();
1045 DCHECK(locations->CanCall());
1046 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001047 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1048 << "Unexpected instruction in read barrier for GC root slow path: "
1049 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001050
1051 __ Bind(GetEntryLabel());
1052 SaveLiveRegisters(codegen, locations);
1053
1054 InvokeRuntimeCallingConvention calling_convention;
1055 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1056 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001057 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001058 instruction_,
1059 instruction_->GetDexPc(),
1060 this);
1061 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1062 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1063
1064 RestoreLiveRegisters(codegen, locations);
1065 __ b(GetExitLabel());
1066 }
1067
1068 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1069
1070 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001071 const Location out_;
1072 const Location root_;
1073
1074 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1075};
1076
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001077#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001078// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1079#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -07001080
Aart Bike9f37602015-10-09 11:15:55 -07001081inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001082 switch (cond) {
1083 case kCondEQ: return EQ;
1084 case kCondNE: return NE;
1085 case kCondLT: return LT;
1086 case kCondLE: return LE;
1087 case kCondGT: return GT;
1088 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001089 case kCondB: return LO;
1090 case kCondBE: return LS;
1091 case kCondA: return HI;
1092 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001093 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001094 LOG(FATAL) << "Unreachable";
1095 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001096}
1097
Aart Bike9f37602015-10-09 11:15:55 -07001098// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001099inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001100 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001101 case kCondEQ: return EQ;
1102 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001103 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001104 case kCondLT: return LO;
1105 case kCondLE: return LS;
1106 case kCondGT: return HI;
1107 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001108 // Unsigned remain unchanged.
1109 case kCondB: return LO;
1110 case kCondBE: return LS;
1111 case kCondA: return HI;
1112 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001113 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001114 LOG(FATAL) << "Unreachable";
1115 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001116}
1117
Vladimir Markod6e069b2016-01-18 11:11:01 +00001118inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1119 // The ARM condition codes can express all the necessary branches, see the
1120 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1121 // There is no dex instruction or HIR that would need the missing conditions
1122 // "equal or unordered" or "not equal".
1123 switch (cond) {
1124 case kCondEQ: return EQ;
1125 case kCondNE: return NE /* unordered */;
1126 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1127 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1128 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1129 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1130 default:
1131 LOG(FATAL) << "UNREACHABLE";
1132 UNREACHABLE();
1133 }
1134}
1135
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001136void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001137 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001138}
1139
1140void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001141 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001142}
1143
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001144size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1145 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1146 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001147}
1148
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001149size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1150 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1151 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001152}
1153
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001154size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1155 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1156 return kArmWordSize;
1157}
1158
1159size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1160 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1161 return kArmWordSize;
1162}
1163
Calin Juravle34166012014-12-19 17:22:29 +00001164CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001165 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001166 const CompilerOptions& compiler_options,
1167 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001168 : CodeGenerator(graph,
1169 kNumberOfCoreRegisters,
1170 kNumberOfSRegisters,
1171 kNumberOfRegisterPairs,
1172 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1173 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001174 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1175 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001176 compiler_options,
1177 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001178 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001179 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001180 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001181 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001182 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001183 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001184 uint32_literals_(std::less<uint32_t>(),
1185 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001186 method_patches_(MethodReferenceComparator(),
1187 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1188 call_patches_(MethodReferenceComparator(),
1189 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +00001190 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001191 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1192 boot_image_string_patches_(StringReferenceValueComparator(),
1193 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1194 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001195 boot_image_type_patches_(TypeReferenceValueComparator(),
1196 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1197 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001198 boot_image_address_patches_(std::less<uint32_t>(),
1199 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001200 // Always save the LR register to mimic Quick.
1201 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001202}
1203
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001204void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1205 // Ensure that we fix up branches and literal loads and emit the literal pool.
1206 __ FinalizeCode();
1207
1208 // Adjust native pc offsets in stack maps.
1209 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
1210 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
1211 uint32_t new_position = __ GetAdjustedPosition(old_position);
1212 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1213 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001214 // Adjust pc offsets for the disassembly information.
1215 if (disasm_info_ != nullptr) {
1216 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1217 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1218 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1219 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1220 it.second.start = __ GetAdjustedPosition(it.second.start);
1221 it.second.end = __ GetAdjustedPosition(it.second.end);
1222 }
1223 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1224 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1225 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1226 }
1227 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001228
1229 CodeGenerator::Finalize(allocator);
1230}
1231
David Brazdil58282f42016-01-14 12:45:10 +00001232void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001233 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001234 blocked_core_registers_[SP] = true;
1235 blocked_core_registers_[LR] = true;
1236 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001237
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001238 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001239 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001240
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001241 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001242 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001243
David Brazdil58282f42016-01-14 12:45:10 +00001244 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001245 // Stubs do not save callee-save floating point registers. If the graph
1246 // is debuggable, we need to deal with these registers differently. For
1247 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001248 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1249 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1250 }
1251 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001252}
1253
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001254InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001255 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001256 assembler_(codegen->GetAssembler()),
1257 codegen_(codegen) {}
1258
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001259void CodeGeneratorARM::ComputeSpillMask() {
1260 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1261 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001262 // There is no easy instruction to restore just the PC on thumb2. We spill and
1263 // restore another arbitrary register.
1264 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001265 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1266 // We use vpush and vpop for saving and restoring floating point registers, which take
1267 // a SRegister and the number of registers to save/restore after that SRegister. We
1268 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1269 // but in the range.
1270 if (fpu_spill_mask_ != 0) {
1271 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1272 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1273 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1274 fpu_spill_mask_ |= (1 << i);
1275 }
1276 }
1277}
1278
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001279static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001280 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001281}
1282
1283static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001284 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001285}
1286
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001287void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001288 bool skip_overflow_check =
1289 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001290 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001291 __ Bind(&frame_entry_label_);
1292
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001293 if (HasEmptyFrame()) {
1294 return;
1295 }
1296
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001297 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001298 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1299 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1300 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001301 }
1302
Andreas Gampe501fd632015-09-10 16:11:06 -07001303 __ PushList(core_spill_mask_);
1304 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1305 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001306 if (fpu_spill_mask_ != 0) {
1307 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1308 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001309 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001310 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001311 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001312 int adjust = GetFrameSize() - FrameEntrySpillSize();
1313 __ AddConstant(SP, -adjust);
1314 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001315
1316 // Save the current method if we need it. Note that we do not
1317 // do this in HCurrentMethod, as the instruction might have been removed
1318 // in the SSA graph.
1319 if (RequiresCurrentMethod()) {
1320 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1321 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001322}
1323
1324void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001325 if (HasEmptyFrame()) {
1326 __ bx(LR);
1327 return;
1328 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001329 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001330 int adjust = GetFrameSize() - FrameEntrySpillSize();
1331 __ AddConstant(SP, adjust);
1332 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001333 if (fpu_spill_mask_ != 0) {
1334 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1335 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001336 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001337 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001338 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001339 // Pop LR into PC to return.
1340 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1341 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1342 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001343 __ cfi().RestoreState();
1344 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001345}
1346
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001347void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001348 Label* label = GetLabelOf(block);
1349 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001350}
1351
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001352Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001353 switch (type) {
1354 case Primitive::kPrimBoolean:
1355 case Primitive::kPrimByte:
1356 case Primitive::kPrimChar:
1357 case Primitive::kPrimShort:
1358 case Primitive::kPrimInt:
1359 case Primitive::kPrimNot: {
1360 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001361 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001362 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001363 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001364 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001365 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001366 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001367 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001368
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001369 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001370 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001371 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001372 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001373 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001374 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001375 if (calling_convention.GetRegisterAt(index) == R1) {
1376 // Skip R1, and use R2_R3 instead.
1377 gp_index_++;
1378 index++;
1379 }
1380 }
1381 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1382 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001383 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001384
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001385 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001386 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001387 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001388 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1389 }
1390 }
1391
1392 case Primitive::kPrimFloat: {
1393 uint32_t stack_index = stack_index_++;
1394 if (float_index_ % 2 == 0) {
1395 float_index_ = std::max(double_index_, float_index_);
1396 }
1397 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1398 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1399 } else {
1400 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1401 }
1402 }
1403
1404 case Primitive::kPrimDouble: {
1405 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1406 uint32_t stack_index = stack_index_;
1407 stack_index_ += 2;
1408 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1409 uint32_t index = double_index_;
1410 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001411 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001412 calling_convention.GetFpuRegisterAt(index),
1413 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001414 DCHECK(ExpectedPairLayout(result));
1415 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001416 } else {
1417 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001418 }
1419 }
1420
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001421 case Primitive::kPrimVoid:
1422 LOG(FATAL) << "Unexpected parameter type " << type;
1423 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001424 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001425 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001426}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001427
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001428Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001429 switch (type) {
1430 case Primitive::kPrimBoolean:
1431 case Primitive::kPrimByte:
1432 case Primitive::kPrimChar:
1433 case Primitive::kPrimShort:
1434 case Primitive::kPrimInt:
1435 case Primitive::kPrimNot: {
1436 return Location::RegisterLocation(R0);
1437 }
1438
1439 case Primitive::kPrimFloat: {
1440 return Location::FpuRegisterLocation(S0);
1441 }
1442
1443 case Primitive::kPrimLong: {
1444 return Location::RegisterPairLocation(R0, R1);
1445 }
1446
1447 case Primitive::kPrimDouble: {
1448 return Location::FpuRegisterPairLocation(S0, S1);
1449 }
1450
1451 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001452 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001453 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001454
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001455 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001456}
1457
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001458Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1459 return Location::RegisterLocation(kMethodRegisterArgument);
1460}
1461
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001462void CodeGeneratorARM::Move32(Location destination, Location source) {
1463 if (source.Equals(destination)) {
1464 return;
1465 }
1466 if (destination.IsRegister()) {
1467 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001468 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001469 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001470 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001471 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001472 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001473 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001474 } else if (destination.IsFpuRegister()) {
1475 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001476 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001477 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001478 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001479 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001480 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001481 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001482 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001483 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001484 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001485 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001486 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001487 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001488 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001489 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001490 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1491 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001492 }
1493 }
1494}
1495
1496void CodeGeneratorARM::Move64(Location destination, Location source) {
1497 if (source.Equals(destination)) {
1498 return;
1499 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001500 if (destination.IsRegisterPair()) {
1501 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001502 EmitParallelMoves(
1503 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1504 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001505 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001506 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001507 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1508 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001509 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001510 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001511 } else if (source.IsFpuRegisterPair()) {
1512 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1513 destination.AsRegisterPairHigh<Register>(),
1514 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001515 } else {
1516 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001517 DCHECK(ExpectedPairLayout(destination));
1518 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1519 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001520 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001521 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001522 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001523 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1524 SP,
1525 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001526 } else if (source.IsRegisterPair()) {
1527 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1528 source.AsRegisterPairLow<Register>(),
1529 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001530 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001531 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001532 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001533 } else {
1534 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001535 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001536 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001537 if (source.AsRegisterPairLow<Register>() == R1) {
1538 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001539 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1540 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001541 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001542 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001543 SP, destination.GetStackIndex());
1544 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001545 } else if (source.IsFpuRegisterPair()) {
1546 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1547 SP,
1548 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001549 } else {
1550 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001551 EmitParallelMoves(
1552 Location::StackSlot(source.GetStackIndex()),
1553 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001554 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001555 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001556 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1557 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001558 }
1559 }
1560}
1561
Calin Juravle175dc732015-08-25 15:42:32 +01001562void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1563 DCHECK(location.IsRegister());
1564 __ LoadImmediate(location.AsRegister<Register>(), value);
1565}
1566
Calin Juravlee460d1d2015-09-29 04:52:17 +01001567void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001568 HParallelMove move(GetGraph()->GetArena());
1569 move.AddMove(src, dst, dst_type, nullptr);
1570 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001571}
1572
1573void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1574 if (location.IsRegister()) {
1575 locations->AddTemp(location);
1576 } else if (location.IsRegisterPair()) {
1577 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1578 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1579 } else {
1580 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1581 }
1582}
1583
Calin Juravle175dc732015-08-25 15:42:32 +01001584void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1585 HInstruction* instruction,
1586 uint32_t dex_pc,
1587 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001588 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001589 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001590 if (EntrypointRequiresStackMap(entrypoint)) {
1591 RecordPcInfo(instruction, dex_pc, slow_path);
1592 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001593}
1594
Roland Levillaindec8f632016-07-22 17:10:06 +01001595void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1596 HInstruction* instruction,
1597 SlowPathCode* slow_path) {
1598 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001599 GenerateInvokeRuntime(entry_point_offset);
1600}
1601
1602void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001603 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1604 __ blx(LR);
1605}
1606
David Brazdilfc6a86a2015-06-26 10:33:45 +00001607void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001608 DCHECK(!successor->IsExitBlock());
1609
1610 HBasicBlock* block = got->GetBlock();
1611 HInstruction* previous = got->GetPrevious();
1612
1613 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001614 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001615 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1616 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1617 return;
1618 }
1619
1620 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1621 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1622 }
1623 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001624 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001625 }
1626}
1627
David Brazdilfc6a86a2015-06-26 10:33:45 +00001628void LocationsBuilderARM::VisitGoto(HGoto* got) {
1629 got->SetLocations(nullptr);
1630}
1631
1632void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1633 HandleGoto(got, got->GetSuccessor());
1634}
1635
1636void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1637 try_boundary->SetLocations(nullptr);
1638}
1639
1640void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1641 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1642 if (!successor->IsExitBlock()) {
1643 HandleGoto(try_boundary, successor);
1644 }
1645}
1646
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001647void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001648 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001649}
1650
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001651void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001652}
1653
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001654void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1655 Primitive::Type type = instruction->InputAt(0)->GetType();
1656 Location lhs_loc = instruction->GetLocations()->InAt(0);
1657 Location rhs_loc = instruction->GetLocations()->InAt(1);
1658 if (rhs_loc.IsConstant()) {
1659 // 0.0 is the only immediate that can be encoded directly in
1660 // a VCMP instruction.
1661 //
1662 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1663 // specify that in a floating-point comparison, positive zero
1664 // and negative zero are considered equal, so we can use the
1665 // literal 0.0 for both cases here.
1666 //
1667 // Note however that some methods (Float.equal, Float.compare,
1668 // Float.compareTo, Double.equal, Double.compare,
1669 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1670 // StrictMath.min) consider 0.0 to be (strictly) greater than
1671 // -0.0. So if we ever translate calls to these methods into a
1672 // HCompare instruction, we must handle the -0.0 case with
1673 // care here.
1674 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1675 if (type == Primitive::kPrimFloat) {
1676 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1677 } else {
1678 DCHECK_EQ(type, Primitive::kPrimDouble);
1679 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1680 }
1681 } else {
1682 if (type == Primitive::kPrimFloat) {
1683 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1684 } else {
1685 DCHECK_EQ(type, Primitive::kPrimDouble);
1686 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1687 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1688 }
1689 }
1690}
1691
Roland Levillain4fa13f62015-07-06 18:11:54 +01001692void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1693 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001694 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001695 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001696 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001697}
1698
1699void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1700 Label* true_label,
1701 Label* false_label) {
1702 LocationSummary* locations = cond->GetLocations();
1703 Location left = locations->InAt(0);
1704 Location right = locations->InAt(1);
1705 IfCondition if_cond = cond->GetCondition();
1706
1707 Register left_high = left.AsRegisterPairHigh<Register>();
1708 Register left_low = left.AsRegisterPairLow<Register>();
1709 IfCondition true_high_cond = if_cond;
1710 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001711 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001712
1713 // Set the conditions for the test, remembering that == needs to be
1714 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001715 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001716 switch (if_cond) {
1717 case kCondEQ:
1718 case kCondNE:
1719 // Nothing to do.
1720 break;
1721 case kCondLT:
1722 false_high_cond = kCondGT;
1723 break;
1724 case kCondLE:
1725 true_high_cond = kCondLT;
1726 break;
1727 case kCondGT:
1728 false_high_cond = kCondLT;
1729 break;
1730 case kCondGE:
1731 true_high_cond = kCondGT;
1732 break;
Aart Bike9f37602015-10-09 11:15:55 -07001733 case kCondB:
1734 false_high_cond = kCondA;
1735 break;
1736 case kCondBE:
1737 true_high_cond = kCondB;
1738 break;
1739 case kCondA:
1740 false_high_cond = kCondB;
1741 break;
1742 case kCondAE:
1743 true_high_cond = kCondA;
1744 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001745 }
1746 if (right.IsConstant()) {
1747 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1748 int32_t val_low = Low32Bits(value);
1749 int32_t val_high = High32Bits(value);
1750
Vladimir Markoac6ac102015-12-17 12:14:00 +00001751 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001752 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001753 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001754 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001755 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001756 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001757 __ b(true_label, ARMCondition(true_high_cond));
1758 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001759 }
1760 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001761 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001762 } else {
1763 Register right_high = right.AsRegisterPairHigh<Register>();
1764 Register right_low = right.AsRegisterPairLow<Register>();
1765
1766 __ cmp(left_high, ShifterOperand(right_high));
1767 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001768 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001769 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001770 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001771 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001772 __ b(true_label, ARMCondition(true_high_cond));
1773 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001774 }
1775 // Must be equal high, so compare the lows.
1776 __ cmp(left_low, ShifterOperand(right_low));
1777 }
1778 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001779 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001780 __ b(true_label, final_condition);
1781}
1782
David Brazdil0debae72015-11-12 18:37:00 +00001783void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1784 Label* true_target_in,
1785 Label* false_target_in) {
1786 // Generated branching requires both targets to be explicit. If either of the
1787 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1788 Label fallthrough_target;
1789 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1790 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1791
Roland Levillain4fa13f62015-07-06 18:11:54 +01001792 Primitive::Type type = condition->InputAt(0)->GetType();
1793 switch (type) {
1794 case Primitive::kPrimLong:
1795 GenerateLongComparesAndJumps(condition, true_target, false_target);
1796 break;
1797 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001798 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001799 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001800 GenerateFPJumps(condition, true_target, false_target);
1801 break;
1802 default:
1803 LOG(FATAL) << "Unexpected compare type " << type;
1804 }
1805
David Brazdil0debae72015-11-12 18:37:00 +00001806 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001807 __ b(false_target);
1808 }
David Brazdil0debae72015-11-12 18:37:00 +00001809
1810 if (fallthrough_target.IsLinked()) {
1811 __ Bind(&fallthrough_target);
1812 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001813}
1814
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001815void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001816 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001817 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001818 Label* false_target) {
1819 HInstruction* cond = instruction->InputAt(condition_input_index);
1820
1821 if (true_target == nullptr && false_target == nullptr) {
1822 // Nothing to do. The code always falls through.
1823 return;
1824 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001825 // Constant condition, statically compared against "true" (integer value 1).
1826 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001827 if (true_target != nullptr) {
1828 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001829 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001830 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001831 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001832 if (false_target != nullptr) {
1833 __ b(false_target);
1834 }
1835 }
1836 return;
1837 }
1838
1839 // The following code generates these patterns:
1840 // (1) true_target == nullptr && false_target != nullptr
1841 // - opposite condition true => branch to false_target
1842 // (2) true_target != nullptr && false_target == nullptr
1843 // - condition true => branch to true_target
1844 // (3) true_target != nullptr && false_target != nullptr
1845 // - condition true => branch to true_target
1846 // - branch to false_target
1847 if (IsBooleanValueOrMaterializedCondition(cond)) {
1848 // Condition has been materialized, compare the output to 0.
1849 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1850 DCHECK(cond_val.IsRegister());
1851 if (true_target == nullptr) {
1852 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1853 } else {
1854 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001855 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001856 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001857 // Condition has not been materialized. Use its inputs as the comparison and
1858 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001859 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001860
1861 // If this is a long or FP comparison that has been folded into
1862 // the HCondition, generate the comparison directly.
1863 Primitive::Type type = condition->InputAt(0)->GetType();
1864 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1865 GenerateCompareTestAndBranch(condition, true_target, false_target);
1866 return;
1867 }
1868
1869 LocationSummary* locations = cond->GetLocations();
1870 DCHECK(locations->InAt(0).IsRegister());
1871 Register left = locations->InAt(0).AsRegister<Register>();
1872 Location right = locations->InAt(1);
1873 if (right.IsRegister()) {
1874 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001875 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001876 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001877 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001878 }
1879 if (true_target == nullptr) {
1880 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1881 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001882 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001883 }
Dave Allison20dfc792014-06-16 20:44:29 -07001884 }
David Brazdil0debae72015-11-12 18:37:00 +00001885
1886 // If neither branch falls through (case 3), the conditional branch to `true_target`
1887 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1888 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001889 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001890 }
1891}
1892
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001893void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001894 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1895 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001896 locations->SetInAt(0, Location::RequiresRegister());
1897 }
1898}
1899
1900void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001901 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1902 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1903 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1904 nullptr : codegen_->GetLabelOf(true_successor);
1905 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1906 nullptr : codegen_->GetLabelOf(false_successor);
1907 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001908}
1909
1910void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1911 LocationSummary* locations = new (GetGraph()->GetArena())
1912 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001913 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001914 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001915 locations->SetInAt(0, Location::RequiresRegister());
1916 }
1917}
1918
1919void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001920 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001921 GenerateTestAndBranch(deoptimize,
1922 /* condition_input_index */ 0,
1923 slow_path->GetEntryLabel(),
1924 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001925}
Dave Allison20dfc792014-06-16 20:44:29 -07001926
David Brazdil74eb1b22015-12-14 11:44:01 +00001927void LocationsBuilderARM::VisitSelect(HSelect* select) {
1928 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1929 if (Primitive::IsFloatingPointType(select->GetType())) {
1930 locations->SetInAt(0, Location::RequiresFpuRegister());
1931 locations->SetInAt(1, Location::RequiresFpuRegister());
1932 } else {
1933 locations->SetInAt(0, Location::RequiresRegister());
1934 locations->SetInAt(1, Location::RequiresRegister());
1935 }
1936 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1937 locations->SetInAt(2, Location::RequiresRegister());
1938 }
1939 locations->SetOut(Location::SameAsFirstInput());
1940}
1941
1942void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1943 LocationSummary* locations = select->GetLocations();
1944 Label false_target;
1945 GenerateTestAndBranch(select,
1946 /* condition_input_index */ 2,
1947 /* true_target */ nullptr,
1948 &false_target);
1949 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1950 __ Bind(&false_target);
1951}
1952
David Srbecky0cf44932015-12-09 14:09:59 +00001953void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1954 new (GetGraph()->GetArena()) LocationSummary(info);
1955}
1956
David Srbeckyd28f4a02016-03-14 17:14:24 +00001957void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1958 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001959}
1960
1961void CodeGeneratorARM::GenerateNop() {
1962 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001963}
1964
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001965void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001966 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001967 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001968 // Handle the long/FP comparisons made in instruction simplification.
1969 switch (cond->InputAt(0)->GetType()) {
1970 case Primitive::kPrimLong:
1971 locations->SetInAt(0, Location::RequiresRegister());
1972 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001973 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001974 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1975 }
1976 break;
1977
1978 case Primitive::kPrimFloat:
1979 case Primitive::kPrimDouble:
1980 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001981 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001982 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001983 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1984 }
1985 break;
1986
1987 default:
1988 locations->SetInAt(0, Location::RequiresRegister());
1989 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001990 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001991 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1992 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001993 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001994}
1995
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001996void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001997 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001998 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001999 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002000
2001 LocationSummary* locations = cond->GetLocations();
2002 Location left = locations->InAt(0);
2003 Location right = locations->InAt(1);
2004 Register out = locations->Out().AsRegister<Register>();
2005 Label true_label, false_label;
2006
2007 switch (cond->InputAt(0)->GetType()) {
2008 default: {
2009 // Integer case.
2010 if (right.IsRegister()) {
2011 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2012 } else {
2013 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002014 __ CmpConstant(left.AsRegister<Register>(),
2015 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002016 }
Aart Bike9f37602015-10-09 11:15:55 -07002017 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002018 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002019 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002020 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002021 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002022 return;
2023 }
2024 case Primitive::kPrimLong:
2025 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2026 break;
2027 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002028 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002029 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002030 GenerateFPJumps(cond, &true_label, &false_label);
2031 break;
2032 }
2033
2034 // Convert the jumps into the result.
2035 Label done_label;
2036
2037 // False case: result = 0.
2038 __ Bind(&false_label);
2039 __ LoadImmediate(out, 0);
2040 __ b(&done_label);
2041
2042 // True case: result = 1.
2043 __ Bind(&true_label);
2044 __ LoadImmediate(out, 1);
2045 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002046}
2047
2048void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002049 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002050}
2051
2052void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002053 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002054}
2055
2056void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002057 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002058}
2059
2060void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002061 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002062}
2063
2064void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002065 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002066}
2067
2068void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002069 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002070}
2071
2072void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002073 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002074}
2075
2076void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002077 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002078}
2079
2080void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002081 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002082}
2083
2084void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002085 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002086}
2087
2088void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002089 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002090}
2091
2092void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002093 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002094}
2095
Aart Bike9f37602015-10-09 11:15:55 -07002096void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002097 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002098}
2099
2100void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002101 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002102}
2103
2104void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002105 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002106}
2107
2108void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002109 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002110}
2111
2112void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002113 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002114}
2115
2116void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002117 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002118}
2119
2120void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002121 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002122}
2123
2124void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002125 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002126}
2127
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002128void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002129 LocationSummary* locations =
2130 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002131 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002132}
2133
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002134void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002135 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002136}
2137
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002138void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2139 LocationSummary* locations =
2140 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2141 locations->SetOut(Location::ConstantLocation(constant));
2142}
2143
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002144void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002145 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002146}
2147
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002148void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002149 LocationSummary* locations =
2150 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002151 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002152}
2153
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002154void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002155 // Will be generated at use site.
2156}
2157
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002158void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2159 LocationSummary* locations =
2160 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2161 locations->SetOut(Location::ConstantLocation(constant));
2162}
2163
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002164void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002165 // Will be generated at use site.
2166}
2167
2168void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2169 LocationSummary* locations =
2170 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2171 locations->SetOut(Location::ConstantLocation(constant));
2172}
2173
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002174void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002175 // Will be generated at use site.
2176}
2177
Calin Juravle27df7582015-04-17 19:12:31 +01002178void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2179 memory_barrier->SetLocations(nullptr);
2180}
2181
2182void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002183 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002184}
2185
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002186void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002187 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002188}
2189
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002190void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002191 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002192}
2193
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002194void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002195 LocationSummary* locations =
2196 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002197 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002198}
2199
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002200void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002201 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002202}
2203
Calin Juravle175dc732015-08-25 15:42:32 +01002204void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2205 // The trampoline uses the same calling convention as dex calling conventions,
2206 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2207 // the method_idx.
2208 HandleInvoke(invoke);
2209}
2210
2211void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2212 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2213}
2214
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002215void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002216 // Explicit clinit checks triggered by static invokes must have been pruned by
2217 // art::PrepareForRegisterAllocation.
2218 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002219
Vladimir Marko68c981f2016-08-26 13:13:33 +01002220 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002221 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002222 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2223 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2224 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002225 return;
2226 }
2227
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002228 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002229
2230 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2231 if (invoke->HasPcRelativeDexCache()) {
2232 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2233 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002234}
2235
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002236static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2237 if (invoke->GetLocations()->Intrinsified()) {
2238 IntrinsicCodeGeneratorARM intrinsic(codegen);
2239 intrinsic.Dispatch(invoke);
2240 return true;
2241 }
2242 return false;
2243}
2244
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002245void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002246 // Explicit clinit checks triggered by static invokes must have been pruned by
2247 // art::PrepareForRegisterAllocation.
2248 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002249
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002250 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2251 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002252 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002253
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002254 LocationSummary* locations = invoke->GetLocations();
2255 codegen_->GenerateStaticOrDirectCall(
2256 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002257 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002258}
2259
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002260void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002261 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002262 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002263}
2264
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002265void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002266 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002267 if (intrinsic.TryDispatch(invoke)) {
2268 return;
2269 }
2270
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002271 HandleInvoke(invoke);
2272}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002273
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002274void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002275 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2276 return;
2277 }
2278
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002279 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002280 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002281 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002282}
2283
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002284void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2285 HandleInvoke(invoke);
2286 // Add the hidden argument.
2287 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2288}
2289
2290void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2291 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002292 LocationSummary* locations = invoke->GetLocations();
2293 Register temp = locations->GetTemp(0).AsRegister<Register>();
2294 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002295 Location receiver = locations->InAt(0);
2296 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2297
Roland Levillain3b359c72015-11-17 19:35:12 +00002298 // Set the hidden argument. This is safe to do this here, as R12
2299 // won't be modified thereafter, before the `blx` (call) instruction.
2300 DCHECK_EQ(R12, hidden_reg);
2301 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002302
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002303 if (receiver.IsStackSlot()) {
2304 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002305 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002306 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2307 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002308 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002309 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002310 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002311 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002312 // Instead of simply (possibly) unpoisoning `temp` here, we should
2313 // emit a read barrier for the previous class reference load.
2314 // However this is not required in practice, as this is an
2315 // intermediate/temporary reference and because the current
2316 // concurrent copying collector keeps the from-space memory
2317 // intact/accessible until the end of the marking phase (the
2318 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002319 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002320 __ LoadFromOffset(kLoadWord, temp, temp,
2321 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2322 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002323 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002324 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002325 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002326 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002327 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002328 // LR = temp->GetEntryPoint();
2329 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2330 // LR();
2331 __ blx(LR);
2332 DCHECK(!codegen_->IsLeafMethod());
2333 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2334}
2335
Roland Levillain88cb1752014-10-20 16:36:47 +01002336void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2337 LocationSummary* locations =
2338 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2339 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002340 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002341 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002342 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2343 break;
2344 }
2345 case Primitive::kPrimLong: {
2346 locations->SetInAt(0, Location::RequiresRegister());
2347 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002348 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002349 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002350
Roland Levillain88cb1752014-10-20 16:36:47 +01002351 case Primitive::kPrimFloat:
2352 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002353 locations->SetInAt(0, Location::RequiresFpuRegister());
2354 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002355 break;
2356
2357 default:
2358 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2359 }
2360}
2361
2362void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2363 LocationSummary* locations = neg->GetLocations();
2364 Location out = locations->Out();
2365 Location in = locations->InAt(0);
2366 switch (neg->GetResultType()) {
2367 case Primitive::kPrimInt:
2368 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002369 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002370 break;
2371
2372 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002373 DCHECK(in.IsRegisterPair());
2374 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2375 __ rsbs(out.AsRegisterPairLow<Register>(),
2376 in.AsRegisterPairLow<Register>(),
2377 ShifterOperand(0));
2378 // We cannot emit an RSC (Reverse Subtract with Carry)
2379 // instruction here, as it does not exist in the Thumb-2
2380 // instruction set. We use the following approach
2381 // using SBC and SUB instead.
2382 //
2383 // out.hi = -C
2384 __ sbc(out.AsRegisterPairHigh<Register>(),
2385 out.AsRegisterPairHigh<Register>(),
2386 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2387 // out.hi = out.hi - in.hi
2388 __ sub(out.AsRegisterPairHigh<Register>(),
2389 out.AsRegisterPairHigh<Register>(),
2390 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2391 break;
2392
Roland Levillain88cb1752014-10-20 16:36:47 +01002393 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002394 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002395 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002396 break;
2397
Roland Levillain88cb1752014-10-20 16:36:47 +01002398 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002399 DCHECK(in.IsFpuRegisterPair());
2400 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2401 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002402 break;
2403
2404 default:
2405 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2406 }
2407}
2408
Roland Levillaindff1f282014-11-05 14:15:05 +00002409void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002410 Primitive::Type result_type = conversion->GetResultType();
2411 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002412 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002413
Roland Levillain5b3ee562015-04-14 16:02:41 +01002414 // The float-to-long, double-to-long and long-to-float type conversions
2415 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002416 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002417 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2418 && result_type == Primitive::kPrimLong)
2419 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002420 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002421 : LocationSummary::kNoCall;
2422 LocationSummary* locations =
2423 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2424
David Brazdilb2bd1c52015-03-25 11:17:37 +00002425 // The Java language does not allow treating boolean as an integral type but
2426 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002427
Roland Levillaindff1f282014-11-05 14:15:05 +00002428 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002429 case Primitive::kPrimByte:
2430 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002431 case Primitive::kPrimLong:
2432 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002433 case Primitive::kPrimBoolean:
2434 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002435 case Primitive::kPrimShort:
2436 case Primitive::kPrimInt:
2437 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002438 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002439 locations->SetInAt(0, Location::RequiresRegister());
2440 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2441 break;
2442
2443 default:
2444 LOG(FATAL) << "Unexpected type conversion from " << input_type
2445 << " to " << result_type;
2446 }
2447 break;
2448
Roland Levillain01a8d712014-11-14 16:27:39 +00002449 case Primitive::kPrimShort:
2450 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002451 case Primitive::kPrimLong:
2452 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002453 case Primitive::kPrimBoolean:
2454 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002455 case Primitive::kPrimByte:
2456 case Primitive::kPrimInt:
2457 case Primitive::kPrimChar:
2458 // Processing a Dex `int-to-short' instruction.
2459 locations->SetInAt(0, Location::RequiresRegister());
2460 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2461 break;
2462
2463 default:
2464 LOG(FATAL) << "Unexpected type conversion from " << input_type
2465 << " to " << result_type;
2466 }
2467 break;
2468
Roland Levillain946e1432014-11-11 17:35:19 +00002469 case Primitive::kPrimInt:
2470 switch (input_type) {
2471 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002472 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002473 locations->SetInAt(0, Location::Any());
2474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2475 break;
2476
2477 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002478 // Processing a Dex `float-to-int' instruction.
2479 locations->SetInAt(0, Location::RequiresFpuRegister());
2480 locations->SetOut(Location::RequiresRegister());
2481 locations->AddTemp(Location::RequiresFpuRegister());
2482 break;
2483
Roland Levillain946e1432014-11-11 17:35:19 +00002484 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002485 // Processing a Dex `double-to-int' instruction.
2486 locations->SetInAt(0, Location::RequiresFpuRegister());
2487 locations->SetOut(Location::RequiresRegister());
2488 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002489 break;
2490
2491 default:
2492 LOG(FATAL) << "Unexpected type conversion from " << input_type
2493 << " to " << result_type;
2494 }
2495 break;
2496
Roland Levillaindff1f282014-11-05 14:15:05 +00002497 case Primitive::kPrimLong:
2498 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002499 case Primitive::kPrimBoolean:
2500 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002501 case Primitive::kPrimByte:
2502 case Primitive::kPrimShort:
2503 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002504 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002505 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002506 locations->SetInAt(0, Location::RequiresRegister());
2507 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2508 break;
2509
Roland Levillain624279f2014-12-04 11:54:28 +00002510 case Primitive::kPrimFloat: {
2511 // Processing a Dex `float-to-long' instruction.
2512 InvokeRuntimeCallingConvention calling_convention;
2513 locations->SetInAt(0, Location::FpuRegisterLocation(
2514 calling_convention.GetFpuRegisterAt(0)));
2515 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2516 break;
2517 }
2518
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002519 case Primitive::kPrimDouble: {
2520 // Processing a Dex `double-to-long' instruction.
2521 InvokeRuntimeCallingConvention calling_convention;
2522 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2523 calling_convention.GetFpuRegisterAt(0),
2524 calling_convention.GetFpuRegisterAt(1)));
2525 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002526 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002527 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002528
2529 default:
2530 LOG(FATAL) << "Unexpected type conversion from " << input_type
2531 << " to " << result_type;
2532 }
2533 break;
2534
Roland Levillain981e4542014-11-14 11:47:14 +00002535 case Primitive::kPrimChar:
2536 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002537 case Primitive::kPrimLong:
2538 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002539 case Primitive::kPrimBoolean:
2540 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002541 case Primitive::kPrimByte:
2542 case Primitive::kPrimShort:
2543 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002544 // Processing a Dex `int-to-char' instruction.
2545 locations->SetInAt(0, Location::RequiresRegister());
2546 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2547 break;
2548
2549 default:
2550 LOG(FATAL) << "Unexpected type conversion from " << input_type
2551 << " to " << result_type;
2552 }
2553 break;
2554
Roland Levillaindff1f282014-11-05 14:15:05 +00002555 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002556 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002557 case Primitive::kPrimBoolean:
2558 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002559 case Primitive::kPrimByte:
2560 case Primitive::kPrimShort:
2561 case Primitive::kPrimInt:
2562 case Primitive::kPrimChar:
2563 // Processing a Dex `int-to-float' instruction.
2564 locations->SetInAt(0, Location::RequiresRegister());
2565 locations->SetOut(Location::RequiresFpuRegister());
2566 break;
2567
Roland Levillain5b3ee562015-04-14 16:02:41 +01002568 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002569 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002570 InvokeRuntimeCallingConvention calling_convention;
2571 locations->SetInAt(0, Location::RegisterPairLocation(
2572 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2573 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002574 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002575 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002576
Roland Levillaincff13742014-11-17 14:32:17 +00002577 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002578 // Processing a Dex `double-to-float' instruction.
2579 locations->SetInAt(0, Location::RequiresFpuRegister());
2580 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002581 break;
2582
2583 default:
2584 LOG(FATAL) << "Unexpected type conversion from " << input_type
2585 << " to " << result_type;
2586 };
2587 break;
2588
Roland Levillaindff1f282014-11-05 14:15:05 +00002589 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002590 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002591 case Primitive::kPrimBoolean:
2592 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002593 case Primitive::kPrimByte:
2594 case Primitive::kPrimShort:
2595 case Primitive::kPrimInt:
2596 case Primitive::kPrimChar:
2597 // Processing a Dex `int-to-double' instruction.
2598 locations->SetInAt(0, Location::RequiresRegister());
2599 locations->SetOut(Location::RequiresFpuRegister());
2600 break;
2601
2602 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002603 // Processing a Dex `long-to-double' instruction.
2604 locations->SetInAt(0, Location::RequiresRegister());
2605 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002606 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002607 locations->AddTemp(Location::RequiresFpuRegister());
2608 break;
2609
Roland Levillaincff13742014-11-17 14:32:17 +00002610 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002611 // Processing a Dex `float-to-double' instruction.
2612 locations->SetInAt(0, Location::RequiresFpuRegister());
2613 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002614 break;
2615
2616 default:
2617 LOG(FATAL) << "Unexpected type conversion from " << input_type
2618 << " to " << result_type;
2619 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002620 break;
2621
2622 default:
2623 LOG(FATAL) << "Unexpected type conversion from " << input_type
2624 << " to " << result_type;
2625 }
2626}
2627
2628void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2629 LocationSummary* locations = conversion->GetLocations();
2630 Location out = locations->Out();
2631 Location in = locations->InAt(0);
2632 Primitive::Type result_type = conversion->GetResultType();
2633 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002634 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002635 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002636 case Primitive::kPrimByte:
2637 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002638 case Primitive::kPrimLong:
2639 // Type conversion from long to byte is a result of code transformations.
2640 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2641 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002642 case Primitive::kPrimBoolean:
2643 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002644 case Primitive::kPrimShort:
2645 case Primitive::kPrimInt:
2646 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002647 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002648 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002649 break;
2650
2651 default:
2652 LOG(FATAL) << "Unexpected type conversion from " << input_type
2653 << " to " << result_type;
2654 }
2655 break;
2656
Roland Levillain01a8d712014-11-14 16:27:39 +00002657 case Primitive::kPrimShort:
2658 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002659 case Primitive::kPrimLong:
2660 // Type conversion from long to short is a result of code transformations.
2661 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2662 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002663 case Primitive::kPrimBoolean:
2664 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002665 case Primitive::kPrimByte:
2666 case Primitive::kPrimInt:
2667 case Primitive::kPrimChar:
2668 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002669 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002670 break;
2671
2672 default:
2673 LOG(FATAL) << "Unexpected type conversion from " << input_type
2674 << " to " << result_type;
2675 }
2676 break;
2677
Roland Levillain946e1432014-11-11 17:35:19 +00002678 case Primitive::kPrimInt:
2679 switch (input_type) {
2680 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002681 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002682 DCHECK(out.IsRegister());
2683 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002684 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002685 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002687 } else {
2688 DCHECK(in.IsConstant());
2689 DCHECK(in.GetConstant()->IsLongConstant());
2690 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002691 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002692 }
2693 break;
2694
Roland Levillain3f8f9362014-12-02 17:45:01 +00002695 case Primitive::kPrimFloat: {
2696 // Processing a Dex `float-to-int' instruction.
2697 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002698 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002699 __ vmovrs(out.AsRegister<Register>(), temp);
2700 break;
2701 }
2702
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002703 case Primitive::kPrimDouble: {
2704 // Processing a Dex `double-to-int' instruction.
2705 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002706 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002707 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002708 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002709 }
Roland Levillain946e1432014-11-11 17:35:19 +00002710
2711 default:
2712 LOG(FATAL) << "Unexpected type conversion from " << input_type
2713 << " to " << result_type;
2714 }
2715 break;
2716
Roland Levillaindff1f282014-11-05 14:15:05 +00002717 case Primitive::kPrimLong:
2718 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002719 case Primitive::kPrimBoolean:
2720 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002721 case Primitive::kPrimByte:
2722 case Primitive::kPrimShort:
2723 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002724 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002725 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002726 DCHECK(out.IsRegisterPair());
2727 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002728 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002729 // Sign extension.
2730 __ Asr(out.AsRegisterPairHigh<Register>(),
2731 out.AsRegisterPairLow<Register>(),
2732 31);
2733 break;
2734
2735 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002736 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002737 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002738 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002739 break;
2740
Roland Levillaindff1f282014-11-05 14:15:05 +00002741 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002742 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002743 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002744 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002745 break;
2746
2747 default:
2748 LOG(FATAL) << "Unexpected type conversion from " << input_type
2749 << " to " << result_type;
2750 }
2751 break;
2752
Roland Levillain981e4542014-11-14 11:47:14 +00002753 case Primitive::kPrimChar:
2754 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002755 case Primitive::kPrimLong:
2756 // Type conversion from long to char is a result of code transformations.
2757 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2758 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002759 case Primitive::kPrimBoolean:
2760 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002761 case Primitive::kPrimByte:
2762 case Primitive::kPrimShort:
2763 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002764 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002765 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002766 break;
2767
2768 default:
2769 LOG(FATAL) << "Unexpected type conversion from " << input_type
2770 << " to " << result_type;
2771 }
2772 break;
2773
Roland Levillaindff1f282014-11-05 14:15:05 +00002774 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002775 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002776 case Primitive::kPrimBoolean:
2777 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002778 case Primitive::kPrimByte:
2779 case Primitive::kPrimShort:
2780 case Primitive::kPrimInt:
2781 case Primitive::kPrimChar: {
2782 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002783 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2784 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002785 break;
2786 }
2787
Roland Levillain5b3ee562015-04-14 16:02:41 +01002788 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002789 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002790 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002791 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002792 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002793
Roland Levillaincff13742014-11-17 14:32:17 +00002794 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002795 // Processing a Dex `double-to-float' instruction.
2796 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2797 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002798 break;
2799
2800 default:
2801 LOG(FATAL) << "Unexpected type conversion from " << input_type
2802 << " to " << result_type;
2803 };
2804 break;
2805
Roland Levillaindff1f282014-11-05 14:15:05 +00002806 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002807 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002808 case Primitive::kPrimBoolean:
2809 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002810 case Primitive::kPrimByte:
2811 case Primitive::kPrimShort:
2812 case Primitive::kPrimInt:
2813 case Primitive::kPrimChar: {
2814 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002815 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002816 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2817 out.AsFpuRegisterPairLow<SRegister>());
2818 break;
2819 }
2820
Roland Levillain647b9ed2014-11-27 12:06:00 +00002821 case Primitive::kPrimLong: {
2822 // Processing a Dex `long-to-double' instruction.
2823 Register low = in.AsRegisterPairLow<Register>();
2824 Register high = in.AsRegisterPairHigh<Register>();
2825 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2826 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002827 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002828 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002829 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2830 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002831
Roland Levillain682393c2015-04-14 15:57:52 +01002832 // temp_d = int-to-double(high)
2833 __ vmovsr(temp_s, high);
2834 __ vcvtdi(temp_d, temp_s);
2835 // constant_d = k2Pow32EncodingForDouble
2836 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2837 // out_d = unsigned-to-double(low)
2838 __ vmovsr(out_s, low);
2839 __ vcvtdu(out_d, out_s);
2840 // out_d += temp_d * constant_d
2841 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002842 break;
2843 }
2844
Roland Levillaincff13742014-11-17 14:32:17 +00002845 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002846 // Processing a Dex `float-to-double' instruction.
2847 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2848 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002849 break;
2850
2851 default:
2852 LOG(FATAL) << "Unexpected type conversion from " << input_type
2853 << " to " << result_type;
2854 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002855 break;
2856
2857 default:
2858 LOG(FATAL) << "Unexpected type conversion from " << input_type
2859 << " to " << result_type;
2860 }
2861}
2862
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002863void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002864 LocationSummary* locations =
2865 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002866 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002867 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002868 locations->SetInAt(0, Location::RequiresRegister());
2869 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002870 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2871 break;
2872 }
2873
2874 case Primitive::kPrimLong: {
2875 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002876 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002877 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002878 break;
2879 }
2880
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002881 case Primitive::kPrimFloat:
2882 case Primitive::kPrimDouble: {
2883 locations->SetInAt(0, Location::RequiresFpuRegister());
2884 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002885 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002886 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002887 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002888
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002889 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002890 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002891 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002892}
2893
2894void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2895 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002896 Location out = locations->Out();
2897 Location first = locations->InAt(0);
2898 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002899 switch (add->GetResultType()) {
2900 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002901 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002902 __ add(out.AsRegister<Register>(),
2903 first.AsRegister<Register>(),
2904 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002905 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002906 __ AddConstant(out.AsRegister<Register>(),
2907 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002908 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002909 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002910 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002911
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002912 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002913 if (second.IsConstant()) {
2914 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2915 GenerateAddLongConst(out, first, value);
2916 } else {
2917 DCHECK(second.IsRegisterPair());
2918 __ adds(out.AsRegisterPairLow<Register>(),
2919 first.AsRegisterPairLow<Register>(),
2920 ShifterOperand(second.AsRegisterPairLow<Register>()));
2921 __ adc(out.AsRegisterPairHigh<Register>(),
2922 first.AsRegisterPairHigh<Register>(),
2923 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2924 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002925 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002926 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002927
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002928 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002929 __ vadds(out.AsFpuRegister<SRegister>(),
2930 first.AsFpuRegister<SRegister>(),
2931 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002932 break;
2933
2934 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002935 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2936 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2937 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002938 break;
2939
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002940 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002941 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002942 }
2943}
2944
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002945void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002946 LocationSummary* locations =
2947 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002948 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002949 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002950 locations->SetInAt(0, Location::RequiresRegister());
2951 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002952 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2953 break;
2954 }
2955
2956 case Primitive::kPrimLong: {
2957 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002958 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002959 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002960 break;
2961 }
Calin Juravle11351682014-10-23 15:38:15 +01002962 case Primitive::kPrimFloat:
2963 case Primitive::kPrimDouble: {
2964 locations->SetInAt(0, Location::RequiresFpuRegister());
2965 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002966 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002967 break;
Calin Juravle11351682014-10-23 15:38:15 +01002968 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002969 default:
Calin Juravle11351682014-10-23 15:38:15 +01002970 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002971 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002972}
2973
2974void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2975 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002976 Location out = locations->Out();
2977 Location first = locations->InAt(0);
2978 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002979 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002980 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002981 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002982 __ sub(out.AsRegister<Register>(),
2983 first.AsRegister<Register>(),
2984 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002985 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002986 __ AddConstant(out.AsRegister<Register>(),
2987 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002988 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002989 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002990 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002991 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002992
Calin Juravle11351682014-10-23 15:38:15 +01002993 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002994 if (second.IsConstant()) {
2995 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2996 GenerateAddLongConst(out, first, -value);
2997 } else {
2998 DCHECK(second.IsRegisterPair());
2999 __ subs(out.AsRegisterPairLow<Register>(),
3000 first.AsRegisterPairLow<Register>(),
3001 ShifterOperand(second.AsRegisterPairLow<Register>()));
3002 __ sbc(out.AsRegisterPairHigh<Register>(),
3003 first.AsRegisterPairHigh<Register>(),
3004 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3005 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003006 break;
Calin Juravle11351682014-10-23 15:38:15 +01003007 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003008
Calin Juravle11351682014-10-23 15:38:15 +01003009 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003010 __ vsubs(out.AsFpuRegister<SRegister>(),
3011 first.AsFpuRegister<SRegister>(),
3012 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003013 break;
Calin Juravle11351682014-10-23 15:38:15 +01003014 }
3015
3016 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003017 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3018 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3019 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003020 break;
3021 }
3022
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003023
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003024 default:
Calin Juravle11351682014-10-23 15:38:15 +01003025 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003026 }
3027}
3028
Calin Juravle34bacdf2014-10-07 20:23:36 +01003029void LocationsBuilderARM::VisitMul(HMul* mul) {
3030 LocationSummary* locations =
3031 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3032 switch (mul->GetResultType()) {
3033 case Primitive::kPrimInt:
3034 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003035 locations->SetInAt(0, Location::RequiresRegister());
3036 locations->SetInAt(1, Location::RequiresRegister());
3037 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003038 break;
3039 }
3040
Calin Juravleb5bfa962014-10-21 18:02:24 +01003041 case Primitive::kPrimFloat:
3042 case Primitive::kPrimDouble: {
3043 locations->SetInAt(0, Location::RequiresFpuRegister());
3044 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003045 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003046 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003047 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003048
3049 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003050 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003051 }
3052}
3053
3054void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3055 LocationSummary* locations = mul->GetLocations();
3056 Location out = locations->Out();
3057 Location first = locations->InAt(0);
3058 Location second = locations->InAt(1);
3059 switch (mul->GetResultType()) {
3060 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003061 __ mul(out.AsRegister<Register>(),
3062 first.AsRegister<Register>(),
3063 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003064 break;
3065 }
3066 case Primitive::kPrimLong: {
3067 Register out_hi = out.AsRegisterPairHigh<Register>();
3068 Register out_lo = out.AsRegisterPairLow<Register>();
3069 Register in1_hi = first.AsRegisterPairHigh<Register>();
3070 Register in1_lo = first.AsRegisterPairLow<Register>();
3071 Register in2_hi = second.AsRegisterPairHigh<Register>();
3072 Register in2_lo = second.AsRegisterPairLow<Register>();
3073
3074 // Extra checks to protect caused by the existence of R1_R2.
3075 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3076 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3077 DCHECK_NE(out_hi, in1_lo);
3078 DCHECK_NE(out_hi, in2_lo);
3079
3080 // input: in1 - 64 bits, in2 - 64 bits
3081 // output: out
3082 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3083 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3084 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3085
3086 // IP <- in1.lo * in2.hi
3087 __ mul(IP, in1_lo, in2_hi);
3088 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3089 __ mla(out_hi, in1_hi, in2_lo, IP);
3090 // out.lo <- (in1.lo * in2.lo)[31:0];
3091 __ umull(out_lo, IP, in1_lo, in2_lo);
3092 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3093 __ add(out_hi, out_hi, ShifterOperand(IP));
3094 break;
3095 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003096
3097 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003098 __ vmuls(out.AsFpuRegister<SRegister>(),
3099 first.AsFpuRegister<SRegister>(),
3100 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003101 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003102 }
3103
3104 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003105 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3106 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3107 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003108 break;
3109 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003110
3111 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003112 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003113 }
3114}
3115
Zheng Xuc6667102015-05-15 16:08:45 +08003116void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3117 DCHECK(instruction->IsDiv() || instruction->IsRem());
3118 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3119
3120 LocationSummary* locations = instruction->GetLocations();
3121 Location second = locations->InAt(1);
3122 DCHECK(second.IsConstant());
3123
3124 Register out = locations->Out().AsRegister<Register>();
3125 Register dividend = locations->InAt(0).AsRegister<Register>();
3126 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3127 DCHECK(imm == 1 || imm == -1);
3128
3129 if (instruction->IsRem()) {
3130 __ LoadImmediate(out, 0);
3131 } else {
3132 if (imm == 1) {
3133 __ Mov(out, dividend);
3134 } else {
3135 __ rsb(out, dividend, ShifterOperand(0));
3136 }
3137 }
3138}
3139
3140void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3141 DCHECK(instruction->IsDiv() || instruction->IsRem());
3142 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3143
3144 LocationSummary* locations = instruction->GetLocations();
3145 Location second = locations->InAt(1);
3146 DCHECK(second.IsConstant());
3147
3148 Register out = locations->Out().AsRegister<Register>();
3149 Register dividend = locations->InAt(0).AsRegister<Register>();
3150 Register temp = locations->GetTemp(0).AsRegister<Register>();
3151 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003152 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003153 int ctz_imm = CTZ(abs_imm);
3154
3155 if (ctz_imm == 1) {
3156 __ Lsr(temp, dividend, 32 - ctz_imm);
3157 } else {
3158 __ Asr(temp, dividend, 31);
3159 __ Lsr(temp, temp, 32 - ctz_imm);
3160 }
3161 __ add(out, temp, ShifterOperand(dividend));
3162
3163 if (instruction->IsDiv()) {
3164 __ Asr(out, out, ctz_imm);
3165 if (imm < 0) {
3166 __ rsb(out, out, ShifterOperand(0));
3167 }
3168 } else {
3169 __ ubfx(out, out, 0, ctz_imm);
3170 __ sub(out, out, ShifterOperand(temp));
3171 }
3172}
3173
3174void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3175 DCHECK(instruction->IsDiv() || instruction->IsRem());
3176 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3177
3178 LocationSummary* locations = instruction->GetLocations();
3179 Location second = locations->InAt(1);
3180 DCHECK(second.IsConstant());
3181
3182 Register out = locations->Out().AsRegister<Register>();
3183 Register dividend = locations->InAt(0).AsRegister<Register>();
3184 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3185 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3186 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3187
3188 int64_t magic;
3189 int shift;
3190 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3191
3192 __ LoadImmediate(temp1, magic);
3193 __ smull(temp2, temp1, dividend, temp1);
3194
3195 if (imm > 0 && magic < 0) {
3196 __ add(temp1, temp1, ShifterOperand(dividend));
3197 } else if (imm < 0 && magic > 0) {
3198 __ sub(temp1, temp1, ShifterOperand(dividend));
3199 }
3200
3201 if (shift != 0) {
3202 __ Asr(temp1, temp1, shift);
3203 }
3204
3205 if (instruction->IsDiv()) {
3206 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3207 } else {
3208 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3209 // TODO: Strength reduction for mls.
3210 __ LoadImmediate(temp2, imm);
3211 __ mls(out, temp1, temp2, dividend);
3212 }
3213}
3214
3215void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3216 DCHECK(instruction->IsDiv() || instruction->IsRem());
3217 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3218
3219 LocationSummary* locations = instruction->GetLocations();
3220 Location second = locations->InAt(1);
3221 DCHECK(second.IsConstant());
3222
3223 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3224 if (imm == 0) {
3225 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3226 } else if (imm == 1 || imm == -1) {
3227 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003228 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003229 DivRemByPowerOfTwo(instruction);
3230 } else {
3231 DCHECK(imm <= -2 || imm >= 2);
3232 GenerateDivRemWithAnyConstant(instruction);
3233 }
3234}
3235
Calin Juravle7c4954d2014-10-28 16:57:40 +00003236void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003237 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3238 if (div->GetResultType() == Primitive::kPrimLong) {
3239 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003240 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003241 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3242 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003243 } else if (div->GetResultType() == Primitive::kPrimInt &&
3244 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3245 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003246 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003247 }
3248
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003249 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3250
Calin Juravle7c4954d2014-10-28 16:57:40 +00003251 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003252 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003253 if (div->InputAt(1)->IsConstant()) {
3254 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003255 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003256 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003257 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3258 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003259 // No temp register required.
3260 } else {
3261 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003262 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003263 locations->AddTemp(Location::RequiresRegister());
3264 }
3265 }
3266 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003267 locations->SetInAt(0, Location::RequiresRegister());
3268 locations->SetInAt(1, Location::RequiresRegister());
3269 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3270 } else {
3271 InvokeRuntimeCallingConvention calling_convention;
3272 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3273 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3274 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3275 // we only need the former.
3276 locations->SetOut(Location::RegisterLocation(R0));
3277 }
Calin Juravled0d48522014-11-04 16:40:20 +00003278 break;
3279 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003280 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003281 InvokeRuntimeCallingConvention calling_convention;
3282 locations->SetInAt(0, Location::RegisterPairLocation(
3283 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3284 locations->SetInAt(1, Location::RegisterPairLocation(
3285 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003286 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003287 break;
3288 }
3289 case Primitive::kPrimFloat:
3290 case Primitive::kPrimDouble: {
3291 locations->SetInAt(0, Location::RequiresFpuRegister());
3292 locations->SetInAt(1, Location::RequiresFpuRegister());
3293 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3294 break;
3295 }
3296
3297 default:
3298 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3299 }
3300}
3301
3302void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3303 LocationSummary* locations = div->GetLocations();
3304 Location out = locations->Out();
3305 Location first = locations->InAt(0);
3306 Location second = locations->InAt(1);
3307
3308 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003309 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003310 if (second.IsConstant()) {
3311 GenerateDivRemConstantIntegral(div);
3312 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003313 __ sdiv(out.AsRegister<Register>(),
3314 first.AsRegister<Register>(),
3315 second.AsRegister<Register>());
3316 } else {
3317 InvokeRuntimeCallingConvention calling_convention;
3318 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3319 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3320 DCHECK_EQ(R0, out.AsRegister<Register>());
3321
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003322 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003323 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003324 }
Calin Juravled0d48522014-11-04 16:40:20 +00003325 break;
3326 }
3327
Calin Juravle7c4954d2014-10-28 16:57:40 +00003328 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003329 InvokeRuntimeCallingConvention calling_convention;
3330 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3331 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3332 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3333 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3334 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003335 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003336
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003337 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003338 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003339 break;
3340 }
3341
3342 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003343 __ vdivs(out.AsFpuRegister<SRegister>(),
3344 first.AsFpuRegister<SRegister>(),
3345 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003346 break;
3347 }
3348
3349 case Primitive::kPrimDouble: {
3350 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3351 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3352 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3353 break;
3354 }
3355
3356 default:
3357 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3358 }
3359}
3360
Calin Juravlebacfec32014-11-14 15:54:36 +00003361void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003362 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003363
3364 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003365 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003366 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3367 // sdiv will be replaced by other instruction sequence.
3368 call_kind = LocationSummary::kNoCall;
3369 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3370 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003371 // Have hardware divide instruction for int, do it with three instructions.
3372 call_kind = LocationSummary::kNoCall;
3373 }
3374
Calin Juravlebacfec32014-11-14 15:54:36 +00003375 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3376
Calin Juravled2ec87d2014-12-08 14:24:46 +00003377 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003378 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003379 if (rem->InputAt(1)->IsConstant()) {
3380 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003381 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003383 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3384 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003385 // No temp register required.
3386 } else {
3387 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003388 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003389 locations->AddTemp(Location::RequiresRegister());
3390 }
3391 }
3392 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003393 locations->SetInAt(0, Location::RequiresRegister());
3394 locations->SetInAt(1, Location::RequiresRegister());
3395 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3396 locations->AddTemp(Location::RequiresRegister());
3397 } else {
3398 InvokeRuntimeCallingConvention calling_convention;
3399 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3400 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3401 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3402 // we only need the latter.
3403 locations->SetOut(Location::RegisterLocation(R1));
3404 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003405 break;
3406 }
3407 case Primitive::kPrimLong: {
3408 InvokeRuntimeCallingConvention calling_convention;
3409 locations->SetInAt(0, Location::RegisterPairLocation(
3410 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3411 locations->SetInAt(1, Location::RegisterPairLocation(
3412 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3413 // The runtime helper puts the output in R2,R3.
3414 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3415 break;
3416 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003417 case Primitive::kPrimFloat: {
3418 InvokeRuntimeCallingConvention calling_convention;
3419 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3420 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3421 locations->SetOut(Location::FpuRegisterLocation(S0));
3422 break;
3423 }
3424
Calin Juravlebacfec32014-11-14 15:54:36 +00003425 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003426 InvokeRuntimeCallingConvention calling_convention;
3427 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3428 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3429 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3430 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3431 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003432 break;
3433 }
3434
3435 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003436 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003437 }
3438}
3439
3440void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3441 LocationSummary* locations = rem->GetLocations();
3442 Location out = locations->Out();
3443 Location first = locations->InAt(0);
3444 Location second = locations->InAt(1);
3445
Calin Juravled2ec87d2014-12-08 14:24:46 +00003446 Primitive::Type type = rem->GetResultType();
3447 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003448 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003449 if (second.IsConstant()) {
3450 GenerateDivRemConstantIntegral(rem);
3451 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003452 Register reg1 = first.AsRegister<Register>();
3453 Register reg2 = second.AsRegister<Register>();
3454 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003455
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003456 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003457 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003458 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003459 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003460 } else {
3461 InvokeRuntimeCallingConvention calling_convention;
3462 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3463 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3464 DCHECK_EQ(R1, out.AsRegister<Register>());
3465
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003466 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003467 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003468 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003469 break;
3470 }
3471
3472 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003473 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003474 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003475 break;
3476 }
3477
Calin Juravled2ec87d2014-12-08 14:24:46 +00003478 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003479 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003480 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003481 break;
3482 }
3483
Calin Juravlebacfec32014-11-14 15:54:36 +00003484 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003485 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003486 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003487 break;
3488 }
3489
3490 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003491 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003492 }
3493}
3494
Calin Juravled0d48522014-11-04 16:40:20 +00003495void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003496 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003497 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003498}
3499
3500void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003501 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003502 codegen_->AddSlowPath(slow_path);
3503
3504 LocationSummary* locations = instruction->GetLocations();
3505 Location value = locations->InAt(0);
3506
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003507 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003508 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003509 case Primitive::kPrimByte:
3510 case Primitive::kPrimChar:
3511 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003512 case Primitive::kPrimInt: {
3513 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003514 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003515 } else {
3516 DCHECK(value.IsConstant()) << value;
3517 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3518 __ b(slow_path->GetEntryLabel());
3519 }
3520 }
3521 break;
3522 }
3523 case Primitive::kPrimLong: {
3524 if (value.IsRegisterPair()) {
3525 __ orrs(IP,
3526 value.AsRegisterPairLow<Register>(),
3527 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3528 __ b(slow_path->GetEntryLabel(), EQ);
3529 } else {
3530 DCHECK(value.IsConstant()) << value;
3531 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3532 __ b(slow_path->GetEntryLabel());
3533 }
3534 }
3535 break;
3536 default:
3537 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3538 }
3539 }
Calin Juravled0d48522014-11-04 16:40:20 +00003540}
3541
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003542void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3543 Register in = locations->InAt(0).AsRegister<Register>();
3544 Location rhs = locations->InAt(1);
3545 Register out = locations->Out().AsRegister<Register>();
3546
3547 if (rhs.IsConstant()) {
3548 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3549 // so map all rotations to a +ve. equivalent in that range.
3550 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3551 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3552 if (rot) {
3553 // Rotate, mapping left rotations to right equivalents if necessary.
3554 // (e.g. left by 2 bits == right by 30.)
3555 __ Ror(out, in, rot);
3556 } else if (out != in) {
3557 __ Mov(out, in);
3558 }
3559 } else {
3560 __ Ror(out, in, rhs.AsRegister<Register>());
3561 }
3562}
3563
3564// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3565// rotates by swapping input regs (effectively rotating by the first 32-bits of
3566// a larger rotation) or flipping direction (thus treating larger right/left
3567// rotations as sub-word sized rotations in the other direction) as appropriate.
3568void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3569 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3570 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3571 Location rhs = locations->InAt(1);
3572 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3573 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3574
3575 if (rhs.IsConstant()) {
3576 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3577 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003578 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003579 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3580 // logic below to a simple pair of binary orr.
3581 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3582 if (rot >= kArmBitsPerWord) {
3583 rot -= kArmBitsPerWord;
3584 std::swap(in_reg_hi, in_reg_lo);
3585 }
3586 // Rotate, or mov to out for zero or word size rotations.
3587 if (rot != 0u) {
3588 __ Lsr(out_reg_hi, in_reg_hi, rot);
3589 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3590 __ Lsr(out_reg_lo, in_reg_lo, rot);
3591 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3592 } else {
3593 __ Mov(out_reg_lo, in_reg_lo);
3594 __ Mov(out_reg_hi, in_reg_hi);
3595 }
3596 } else {
3597 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3598 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3599 Label end;
3600 Label shift_by_32_plus_shift_right;
3601
3602 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3603 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3604 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3605 __ b(&shift_by_32_plus_shift_right, CC);
3606
3607 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3608 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3609 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3610 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3611 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3612 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3613 __ Lsr(shift_left, in_reg_hi, shift_right);
3614 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3615 __ b(&end);
3616
3617 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3618 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3619 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3620 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3621 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3622 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3623 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3624 __ Lsl(shift_right, in_reg_hi, shift_left);
3625 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3626
3627 __ Bind(&end);
3628 }
3629}
Roland Levillain22c49222016-03-18 14:04:28 +00003630
3631void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003632 LocationSummary* locations =
3633 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3634 switch (ror->GetResultType()) {
3635 case Primitive::kPrimInt: {
3636 locations->SetInAt(0, Location::RequiresRegister());
3637 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3638 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3639 break;
3640 }
3641 case Primitive::kPrimLong: {
3642 locations->SetInAt(0, Location::RequiresRegister());
3643 if (ror->InputAt(1)->IsConstant()) {
3644 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3645 } else {
3646 locations->SetInAt(1, Location::RequiresRegister());
3647 locations->AddTemp(Location::RequiresRegister());
3648 locations->AddTemp(Location::RequiresRegister());
3649 }
3650 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3651 break;
3652 }
3653 default:
3654 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3655 }
3656}
3657
Roland Levillain22c49222016-03-18 14:04:28 +00003658void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003659 LocationSummary* locations = ror->GetLocations();
3660 Primitive::Type type = ror->GetResultType();
3661 switch (type) {
3662 case Primitive::kPrimInt: {
3663 HandleIntegerRotate(locations);
3664 break;
3665 }
3666 case Primitive::kPrimLong: {
3667 HandleLongRotate(locations);
3668 break;
3669 }
3670 default:
3671 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003672 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003673 }
3674}
3675
Calin Juravle9aec02f2014-11-18 23:06:35 +00003676void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3677 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3678
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003679 LocationSummary* locations =
3680 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003681
3682 switch (op->GetResultType()) {
3683 case Primitive::kPrimInt: {
3684 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003685 if (op->InputAt(1)->IsConstant()) {
3686 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3687 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3688 } else {
3689 locations->SetInAt(1, Location::RequiresRegister());
3690 // Make the output overlap, as it will be used to hold the masked
3691 // second input.
3692 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3693 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003694 break;
3695 }
3696 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003697 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003698 if (op->InputAt(1)->IsConstant()) {
3699 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3700 // For simplicity, use kOutputOverlap even though we only require that low registers
3701 // don't clash with high registers which the register allocator currently guarantees.
3702 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3703 } else {
3704 locations->SetInAt(1, Location::RequiresRegister());
3705 locations->AddTemp(Location::RequiresRegister());
3706 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3707 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003708 break;
3709 }
3710 default:
3711 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3712 }
3713}
3714
3715void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3716 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3717
3718 LocationSummary* locations = op->GetLocations();
3719 Location out = locations->Out();
3720 Location first = locations->InAt(0);
3721 Location second = locations->InAt(1);
3722
3723 Primitive::Type type = op->GetResultType();
3724 switch (type) {
3725 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003726 Register out_reg = out.AsRegister<Register>();
3727 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003728 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003729 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003730 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003731 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003732 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003733 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003734 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003735 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003736 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003737 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003738 }
3739 } else {
3740 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003741 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003742 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003743 __ Mov(out_reg, first_reg);
3744 } else if (op->IsShl()) {
3745 __ Lsl(out_reg, first_reg, shift_value);
3746 } else if (op->IsShr()) {
3747 __ Asr(out_reg, first_reg, shift_value);
3748 } else {
3749 __ Lsr(out_reg, first_reg, shift_value);
3750 }
3751 }
3752 break;
3753 }
3754 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003755 Register o_h = out.AsRegisterPairHigh<Register>();
3756 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003757
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003758 Register high = first.AsRegisterPairHigh<Register>();
3759 Register low = first.AsRegisterPairLow<Register>();
3760
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003761 if (second.IsRegister()) {
3762 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003763
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003764 Register second_reg = second.AsRegister<Register>();
3765
3766 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003767 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003768 // Shift the high part
3769 __ Lsl(o_h, high, o_l);
3770 // Shift the low part and `or` what overflew on the high part
3771 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3772 __ Lsr(temp, low, temp);
3773 __ orr(o_h, o_h, ShifterOperand(temp));
3774 // If the shift is > 32 bits, override the high part
3775 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3776 __ it(PL);
3777 __ Lsl(o_h, low, temp, PL);
3778 // Shift the low part
3779 __ Lsl(o_l, low, o_l);
3780 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003781 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003782 // Shift the low part
3783 __ Lsr(o_l, low, o_h);
3784 // Shift the high part and `or` what underflew on the low part
3785 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3786 __ Lsl(temp, high, temp);
3787 __ orr(o_l, o_l, ShifterOperand(temp));
3788 // If the shift is > 32 bits, override the low part
3789 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3790 __ it(PL);
3791 __ Asr(o_l, high, temp, PL);
3792 // Shift the high part
3793 __ Asr(o_h, high, o_h);
3794 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003795 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003796 // same as Shr except we use `Lsr`s and not `Asr`s
3797 __ Lsr(o_l, low, o_h);
3798 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3799 __ Lsl(temp, high, temp);
3800 __ orr(o_l, o_l, ShifterOperand(temp));
3801 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3802 __ it(PL);
3803 __ Lsr(o_l, high, temp, PL);
3804 __ Lsr(o_h, high, o_h);
3805 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003806 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003807 // Register allocator doesn't create partial overlap.
3808 DCHECK_NE(o_l, high);
3809 DCHECK_NE(o_h, low);
3810 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003811 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003812 if (shift_value > 32) {
3813 if (op->IsShl()) {
3814 __ Lsl(o_h, low, shift_value - 32);
3815 __ LoadImmediate(o_l, 0);
3816 } else if (op->IsShr()) {
3817 __ Asr(o_l, high, shift_value - 32);
3818 __ Asr(o_h, high, 31);
3819 } else {
3820 __ Lsr(o_l, high, shift_value - 32);
3821 __ LoadImmediate(o_h, 0);
3822 }
3823 } else if (shift_value == 32) {
3824 if (op->IsShl()) {
3825 __ mov(o_h, ShifterOperand(low));
3826 __ LoadImmediate(o_l, 0);
3827 } else if (op->IsShr()) {
3828 __ mov(o_l, ShifterOperand(high));
3829 __ Asr(o_h, high, 31);
3830 } else {
3831 __ mov(o_l, ShifterOperand(high));
3832 __ LoadImmediate(o_h, 0);
3833 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003834 } else if (shift_value == 1) {
3835 if (op->IsShl()) {
3836 __ Lsls(o_l, low, 1);
3837 __ adc(o_h, high, ShifterOperand(high));
3838 } else if (op->IsShr()) {
3839 __ Asrs(o_h, high, 1);
3840 __ Rrx(o_l, low);
3841 } else {
3842 __ Lsrs(o_h, high, 1);
3843 __ Rrx(o_l, low);
3844 }
3845 } else {
3846 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003847 if (op->IsShl()) {
3848 __ Lsl(o_h, high, shift_value);
3849 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3850 __ Lsl(o_l, low, shift_value);
3851 } else if (op->IsShr()) {
3852 __ Lsr(o_l, low, shift_value);
3853 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3854 __ Asr(o_h, high, shift_value);
3855 } else {
3856 __ Lsr(o_l, low, shift_value);
3857 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3858 __ Lsr(o_h, high, shift_value);
3859 }
3860 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003861 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003862 break;
3863 }
3864 default:
3865 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003866 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003867 }
3868}
3869
3870void LocationsBuilderARM::VisitShl(HShl* shl) {
3871 HandleShift(shl);
3872}
3873
3874void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3875 HandleShift(shl);
3876}
3877
3878void LocationsBuilderARM::VisitShr(HShr* shr) {
3879 HandleShift(shr);
3880}
3881
3882void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3883 HandleShift(shr);
3884}
3885
3886void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3887 HandleShift(ushr);
3888}
3889
3890void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3891 HandleShift(ushr);
3892}
3893
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003894void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003895 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003896 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003897 if (instruction->IsStringAlloc()) {
3898 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3899 } else {
3900 InvokeRuntimeCallingConvention calling_convention;
3901 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3902 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3903 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003904 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003905}
3906
3907void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003908 // Note: if heap poisoning is enabled, the entry point takes cares
3909 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003910 if (instruction->IsStringAlloc()) {
3911 // String is allocated through StringFactory. Call NewEmptyString entry point.
3912 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003913 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003914 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3915 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3916 __ blx(LR);
3917 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3918 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003919 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003920 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3921 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003922}
3923
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003924void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3925 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003926 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003927 InvokeRuntimeCallingConvention calling_convention;
3928 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003929 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003930 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003931 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003932}
3933
3934void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3935 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003936 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003937 // Note: if heap poisoning is enabled, the entry point takes cares
3938 // of poisoning the reference.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003939 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003940 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003941}
3942
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003943void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003944 LocationSummary* locations =
3945 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003946 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3947 if (location.IsStackSlot()) {
3948 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3949 } else if (location.IsDoubleStackSlot()) {
3950 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003951 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003952 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003953}
3954
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003955void InstructionCodeGeneratorARM::VisitParameterValue(
3956 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003957 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003958}
3959
3960void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3961 LocationSummary* locations =
3962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3963 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3964}
3965
3966void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3967 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003968}
3969
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003970void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003971 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003972 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003973 locations->SetInAt(0, Location::RequiresRegister());
3974 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003975}
3976
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003977void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3978 LocationSummary* locations = not_->GetLocations();
3979 Location out = locations->Out();
3980 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003981 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003982 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003983 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003984 break;
3985
3986 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003987 __ mvn(out.AsRegisterPairLow<Register>(),
3988 ShifterOperand(in.AsRegisterPairLow<Register>()));
3989 __ mvn(out.AsRegisterPairHigh<Register>(),
3990 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003991 break;
3992
3993 default:
3994 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3995 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003996}
3997
David Brazdil66d126e2015-04-03 16:02:44 +01003998void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3999 LocationSummary* locations =
4000 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4001 locations->SetInAt(0, Location::RequiresRegister());
4002 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4003}
4004
4005void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004006 LocationSummary* locations = bool_not->GetLocations();
4007 Location out = locations->Out();
4008 Location in = locations->InAt(0);
4009 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4010}
4011
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004012void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004013 LocationSummary* locations =
4014 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004015 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004016 case Primitive::kPrimBoolean:
4017 case Primitive::kPrimByte:
4018 case Primitive::kPrimShort:
4019 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004020 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004021 case Primitive::kPrimLong: {
4022 locations->SetInAt(0, Location::RequiresRegister());
4023 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004024 // Output overlaps because it is written before doing the low comparison.
4025 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004026 break;
4027 }
4028 case Primitive::kPrimFloat:
4029 case Primitive::kPrimDouble: {
4030 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004031 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004032 locations->SetOut(Location::RequiresRegister());
4033 break;
4034 }
4035 default:
4036 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4037 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004038}
4039
4040void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004041 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004042 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004043 Location left = locations->InAt(0);
4044 Location right = locations->InAt(1);
4045
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004046 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004047 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004048 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004049 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004050 case Primitive::kPrimBoolean:
4051 case Primitive::kPrimByte:
4052 case Primitive::kPrimShort:
4053 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004054 case Primitive::kPrimInt: {
4055 __ LoadImmediate(out, 0);
4056 __ cmp(left.AsRegister<Register>(),
4057 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4058 less_cond = LT;
4059 break;
4060 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004061 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004062 __ cmp(left.AsRegisterPairHigh<Register>(),
4063 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004064 __ b(&less, LT);
4065 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004066 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004067 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004068 __ cmp(left.AsRegisterPairLow<Register>(),
4069 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004070 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004071 break;
4072 }
4073 case Primitive::kPrimFloat:
4074 case Primitive::kPrimDouble: {
4075 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004076 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004077 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004078 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004079 break;
4080 }
4081 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004082 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004083 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004084 }
Aart Bika19616e2016-02-01 18:57:58 -08004085
Calin Juravleddb7df22014-11-25 20:56:51 +00004086 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004087 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004088
4089 __ Bind(&greater);
4090 __ LoadImmediate(out, 1);
4091 __ b(&done);
4092
4093 __ Bind(&less);
4094 __ LoadImmediate(out, -1);
4095
4096 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004097}
4098
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004099void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004100 LocationSummary* locations =
4101 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004102 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004103 locations->SetInAt(i, Location::Any());
4104 }
4105 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004106}
4107
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004108void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004109 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004110}
4111
Roland Levillainc9285912015-12-18 10:38:42 +00004112void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4113 // TODO (ported from quick): revisit ARM barrier kinds.
4114 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004115 switch (kind) {
4116 case MemBarrierKind::kAnyStore:
4117 case MemBarrierKind::kLoadAny:
4118 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004119 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004120 break;
4121 }
4122 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004123 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004124 break;
4125 }
4126 default:
4127 LOG(FATAL) << "Unexpected memory barrier " << kind;
4128 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004129 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004130}
4131
4132void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4133 uint32_t offset,
4134 Register out_lo,
4135 Register out_hi) {
4136 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004137 // Ensure `out_lo` is different from `addr`, so that loading
4138 // `offset` into `out_lo` does not clutter `addr`.
4139 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004140 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004141 __ add(IP, addr, ShifterOperand(out_lo));
4142 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004143 }
4144 __ ldrexd(out_lo, out_hi, addr);
4145}
4146
4147void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4148 uint32_t offset,
4149 Register value_lo,
4150 Register value_hi,
4151 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004152 Register temp2,
4153 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004154 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004155 if (offset != 0) {
4156 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004157 __ add(IP, addr, ShifterOperand(temp1));
4158 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004159 }
4160 __ Bind(&fail);
4161 // We need a load followed by store. (The address used in a STREX instruction must
4162 // be the same as the address in the most recently executed LDREX instruction.)
4163 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004164 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004165 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004166 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004167}
4168
4169void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4170 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4171
Nicolas Geoffray39468442014-09-02 15:17:15 +01004172 LocationSummary* locations =
4173 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004174 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004175
Calin Juravle52c48962014-12-16 17:02:57 +00004176 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004177 if (Primitive::IsFloatingPointType(field_type)) {
4178 locations->SetInAt(1, Location::RequiresFpuRegister());
4179 } else {
4180 locations->SetInAt(1, Location::RequiresRegister());
4181 }
4182
Calin Juravle52c48962014-12-16 17:02:57 +00004183 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004184 bool generate_volatile = field_info.IsVolatile()
4185 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004186 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004187 bool needs_write_barrier =
4188 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004189 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004190 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004191 if (needs_write_barrier) {
4192 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004193 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004194 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004195 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004196 // - registers need to be consecutive
4197 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004198 // We don't test for ARM yet, and the assertion makes sure that we
4199 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004200 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4201
4202 locations->AddTemp(Location::RequiresRegister());
4203 locations->AddTemp(Location::RequiresRegister());
4204 if (field_type == Primitive::kPrimDouble) {
4205 // For doubles we need two more registers to copy the value.
4206 locations->AddTemp(Location::RegisterLocation(R2));
4207 locations->AddTemp(Location::RegisterLocation(R3));
4208 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004209 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004210}
4211
Calin Juravle52c48962014-12-16 17:02:57 +00004212void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004213 const FieldInfo& field_info,
4214 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004215 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4216
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004217 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004218 Register base = locations->InAt(0).AsRegister<Register>();
4219 Location value = locations->InAt(1);
4220
4221 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004222 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004223 Primitive::Type field_type = field_info.GetFieldType();
4224 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004225 bool needs_write_barrier =
4226 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004227
4228 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004229 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004230 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004231
4232 switch (field_type) {
4233 case Primitive::kPrimBoolean:
4234 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004235 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004236 break;
4237 }
4238
4239 case Primitive::kPrimShort:
4240 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004241 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004242 break;
4243 }
4244
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004245 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004246 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004247 if (kPoisonHeapReferences && needs_write_barrier) {
4248 // Note that in the case where `value` is a null reference,
4249 // we do not enter this block, as a null reference does not
4250 // need poisoning.
4251 DCHECK_EQ(field_type, Primitive::kPrimNot);
4252 Register temp = locations->GetTemp(0).AsRegister<Register>();
4253 __ Mov(temp, value.AsRegister<Register>());
4254 __ PoisonHeapReference(temp);
4255 __ StoreToOffset(kStoreWord, temp, base, offset);
4256 } else {
4257 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4258 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004259 break;
4260 }
4261
4262 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004263 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004264 GenerateWideAtomicStore(base, offset,
4265 value.AsRegisterPairLow<Register>(),
4266 value.AsRegisterPairHigh<Register>(),
4267 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004268 locations->GetTemp(1).AsRegister<Register>(),
4269 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004270 } else {
4271 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004272 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004273 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004274 break;
4275 }
4276
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004277 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004278 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004279 break;
4280 }
4281
4282 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004283 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004284 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004285 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4286 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4287
4288 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4289
4290 GenerateWideAtomicStore(base, offset,
4291 value_reg_lo,
4292 value_reg_hi,
4293 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004294 locations->GetTemp(3).AsRegister<Register>(),
4295 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004296 } else {
4297 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004298 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004299 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004300 break;
4301 }
4302
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004303 case Primitive::kPrimVoid:
4304 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004305 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004306 }
Calin Juravle52c48962014-12-16 17:02:57 +00004307
Calin Juravle77520bc2015-01-12 18:45:46 +00004308 // Longs and doubles are handled in the switch.
4309 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4310 codegen_->MaybeRecordImplicitNullCheck(instruction);
4311 }
4312
4313 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4314 Register temp = locations->GetTemp(0).AsRegister<Register>();
4315 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004316 codegen_->MarkGCCard(
4317 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004318 }
4319
Calin Juravle52c48962014-12-16 17:02:57 +00004320 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004321 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004322 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004323}
4324
Calin Juravle52c48962014-12-16 17:02:57 +00004325void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4326 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004327
4328 bool object_field_get_with_read_barrier =
4329 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004330 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004331 new (GetGraph()->GetArena()) LocationSummary(instruction,
4332 object_field_get_with_read_barrier ?
4333 LocationSummary::kCallOnSlowPath :
4334 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004335 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004336 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004337 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004338 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004339
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004340 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004341 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004342 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004343 // The output overlaps in case of volatile long: we don't want the
4344 // code generated by GenerateWideAtomicLoad to overwrite the
4345 // object's location. Likewise, in the case of an object field get
4346 // with read barriers enabled, we do not want the load to overwrite
4347 // the object's location, as we need it to emit the read barrier.
4348 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4349 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004350
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004351 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4352 locations->SetOut(Location::RequiresFpuRegister());
4353 } else {
4354 locations->SetOut(Location::RequiresRegister(),
4355 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4356 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004357 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004358 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004359 // - registers need to be consecutive
4360 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004361 // We don't test for ARM yet, and the assertion makes sure that we
4362 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004363 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4364 locations->AddTemp(Location::RequiresRegister());
4365 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004366 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4367 // We need a temporary register for the read barrier marking slow
4368 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4369 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004370 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004371}
4372
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004373Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4374 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4375 << input->GetType();
4376 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4377 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4378 return Location::ConstantLocation(input->AsConstant());
4379 } else {
4380 return Location::RequiresFpuRegister();
4381 }
4382}
4383
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004384Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4385 Opcode opcode) {
4386 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4387 if (constant->IsConstant() &&
4388 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4389 return Location::ConstantLocation(constant->AsConstant());
4390 }
4391 return Location::RequiresRegister();
4392}
4393
4394bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4395 Opcode opcode) {
4396 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4397 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004398 Opcode high_opcode = opcode;
4399 SetCc low_set_cc = kCcDontCare;
4400 switch (opcode) {
4401 case SUB:
4402 // Flip the operation to an ADD.
4403 value = -value;
4404 opcode = ADD;
4405 FALLTHROUGH_INTENDED;
4406 case ADD:
4407 if (Low32Bits(value) == 0u) {
4408 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4409 }
4410 high_opcode = ADC;
4411 low_set_cc = kCcSet;
4412 break;
4413 default:
4414 break;
4415 }
4416 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4417 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004418 } else {
4419 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4420 }
4421}
4422
Vladimir Marko59751a72016-08-05 14:37:27 +01004423bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4424 Opcode opcode,
4425 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004426 ShifterOperand so;
4427 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004428 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004429 return true;
4430 }
4431 Opcode neg_opcode = kNoOperand;
4432 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004433 case AND: neg_opcode = BIC; value = ~value; break;
4434 case ORR: neg_opcode = ORN; value = ~value; break;
4435 case ADD: neg_opcode = SUB; value = -value; break;
4436 case ADC: neg_opcode = SBC; value = ~value; break;
4437 case SUB: neg_opcode = ADD; value = -value; break;
4438 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004439 default:
4440 return false;
4441 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004442 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004443}
4444
Calin Juravle52c48962014-12-16 17:02:57 +00004445void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4446 const FieldInfo& field_info) {
4447 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004448
Calin Juravle52c48962014-12-16 17:02:57 +00004449 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004450 Location base_loc = locations->InAt(0);
4451 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004452 Location out = locations->Out();
4453 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004454 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004455 Primitive::Type field_type = field_info.GetFieldType();
4456 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4457
4458 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004459 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004460 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004461 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004462
Roland Levillainc9285912015-12-18 10:38:42 +00004463 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004464 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004465 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004466
Roland Levillainc9285912015-12-18 10:38:42 +00004467 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004468 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004469 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004470
Roland Levillainc9285912015-12-18 10:38:42 +00004471 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004472 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004473 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004474
4475 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004476 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004477 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004478
4479 case Primitive::kPrimNot: {
4480 // /* HeapReference<Object> */ out = *(base + offset)
4481 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4482 Location temp_loc = locations->GetTemp(0);
4483 // Note that a potential implicit null check is handled in this
4484 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4485 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4486 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4487 if (is_volatile) {
4488 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4489 }
4490 } else {
4491 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4492 codegen_->MaybeRecordImplicitNullCheck(instruction);
4493 if (is_volatile) {
4494 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4495 }
4496 // If read barriers are enabled, emit read barriers other than
4497 // Baker's using a slow path (and also unpoison the loaded
4498 // reference, if heap poisoning is enabled).
4499 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4500 }
4501 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004502 }
4503
Roland Levillainc9285912015-12-18 10:38:42 +00004504 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004505 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004506 GenerateWideAtomicLoad(base, offset,
4507 out.AsRegisterPairLow<Register>(),
4508 out.AsRegisterPairHigh<Register>());
4509 } else {
4510 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4511 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004512 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004513
Roland Levillainc9285912015-12-18 10:38:42 +00004514 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004515 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004516 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004517
4518 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004519 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004520 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004521 Register lo = locations->GetTemp(0).AsRegister<Register>();
4522 Register hi = locations->GetTemp(1).AsRegister<Register>();
4523 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004524 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004525 __ vmovdrr(out_reg, lo, hi);
4526 } else {
4527 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004528 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004529 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004530 break;
4531 }
4532
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004533 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004534 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004535 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004536 }
Calin Juravle52c48962014-12-16 17:02:57 +00004537
Roland Levillainc9285912015-12-18 10:38:42 +00004538 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4539 // Potential implicit null checks, in the case of reference or
4540 // double fields, are handled in the previous switch statement.
4541 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004542 codegen_->MaybeRecordImplicitNullCheck(instruction);
4543 }
4544
Calin Juravle52c48962014-12-16 17:02:57 +00004545 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004546 if (field_type == Primitive::kPrimNot) {
4547 // Memory barriers, in the case of references, are also handled
4548 // in the previous switch statement.
4549 } else {
4550 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4551 }
Roland Levillain4d027112015-07-01 15:41:14 +01004552 }
Calin Juravle52c48962014-12-16 17:02:57 +00004553}
4554
4555void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4556 HandleFieldSet(instruction, instruction->GetFieldInfo());
4557}
4558
4559void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004560 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004561}
4562
4563void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4564 HandleFieldGet(instruction, instruction->GetFieldInfo());
4565}
4566
4567void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4568 HandleFieldGet(instruction, instruction->GetFieldInfo());
4569}
4570
4571void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4572 HandleFieldGet(instruction, instruction->GetFieldInfo());
4573}
4574
4575void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4576 HandleFieldGet(instruction, instruction->GetFieldInfo());
4577}
4578
4579void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4580 HandleFieldSet(instruction, instruction->GetFieldInfo());
4581}
4582
4583void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004584 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004585}
4586
Calin Juravlee460d1d2015-09-29 04:52:17 +01004587void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4588 HUnresolvedInstanceFieldGet* instruction) {
4589 FieldAccessCallingConventionARM calling_convention;
4590 codegen_->CreateUnresolvedFieldLocationSummary(
4591 instruction, instruction->GetFieldType(), calling_convention);
4592}
4593
4594void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4595 HUnresolvedInstanceFieldGet* instruction) {
4596 FieldAccessCallingConventionARM calling_convention;
4597 codegen_->GenerateUnresolvedFieldAccess(instruction,
4598 instruction->GetFieldType(),
4599 instruction->GetFieldIndex(),
4600 instruction->GetDexPc(),
4601 calling_convention);
4602}
4603
4604void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4605 HUnresolvedInstanceFieldSet* instruction) {
4606 FieldAccessCallingConventionARM calling_convention;
4607 codegen_->CreateUnresolvedFieldLocationSummary(
4608 instruction, instruction->GetFieldType(), calling_convention);
4609}
4610
4611void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4612 HUnresolvedInstanceFieldSet* instruction) {
4613 FieldAccessCallingConventionARM calling_convention;
4614 codegen_->GenerateUnresolvedFieldAccess(instruction,
4615 instruction->GetFieldType(),
4616 instruction->GetFieldIndex(),
4617 instruction->GetDexPc(),
4618 calling_convention);
4619}
4620
4621void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4622 HUnresolvedStaticFieldGet* instruction) {
4623 FieldAccessCallingConventionARM calling_convention;
4624 codegen_->CreateUnresolvedFieldLocationSummary(
4625 instruction, instruction->GetFieldType(), calling_convention);
4626}
4627
4628void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4629 HUnresolvedStaticFieldGet* instruction) {
4630 FieldAccessCallingConventionARM calling_convention;
4631 codegen_->GenerateUnresolvedFieldAccess(instruction,
4632 instruction->GetFieldType(),
4633 instruction->GetFieldIndex(),
4634 instruction->GetDexPc(),
4635 calling_convention);
4636}
4637
4638void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4639 HUnresolvedStaticFieldSet* instruction) {
4640 FieldAccessCallingConventionARM calling_convention;
4641 codegen_->CreateUnresolvedFieldLocationSummary(
4642 instruction, instruction->GetFieldType(), calling_convention);
4643}
4644
4645void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4646 HUnresolvedStaticFieldSet* instruction) {
4647 FieldAccessCallingConventionARM calling_convention;
4648 codegen_->GenerateUnresolvedFieldAccess(instruction,
4649 instruction->GetFieldType(),
4650 instruction->GetFieldIndex(),
4651 instruction->GetDexPc(),
4652 calling_convention);
4653}
4654
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004656 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4657 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004658}
4659
Calin Juravle2ae48182016-03-16 14:05:09 +00004660void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4661 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004662 return;
4663 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004664 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004665
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004666 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004667 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004668}
4669
Calin Juravle2ae48182016-03-16 14:05:09 +00004670void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004671 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004672 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004673
4674 LocationSummary* locations = instruction->GetLocations();
4675 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004676
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004677 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004678}
4679
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004680void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004681 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004682}
4683
Artem Serov6c916792016-07-11 14:02:34 +01004684static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4685 switch (type) {
4686 case Primitive::kPrimNot:
4687 return kLoadWord;
4688 case Primitive::kPrimBoolean:
4689 return kLoadUnsignedByte;
4690 case Primitive::kPrimByte:
4691 return kLoadSignedByte;
4692 case Primitive::kPrimChar:
4693 return kLoadUnsignedHalfword;
4694 case Primitive::kPrimShort:
4695 return kLoadSignedHalfword;
4696 case Primitive::kPrimInt:
4697 return kLoadWord;
4698 case Primitive::kPrimLong:
4699 return kLoadWordPair;
4700 case Primitive::kPrimFloat:
4701 return kLoadSWord;
4702 case Primitive::kPrimDouble:
4703 return kLoadDWord;
4704 default:
4705 LOG(FATAL) << "Unreachable type " << type;
4706 UNREACHABLE();
4707 }
4708}
4709
4710static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4711 switch (type) {
4712 case Primitive::kPrimNot:
4713 return kStoreWord;
4714 case Primitive::kPrimBoolean:
4715 case Primitive::kPrimByte:
4716 return kStoreByte;
4717 case Primitive::kPrimChar:
4718 case Primitive::kPrimShort:
4719 return kStoreHalfword;
4720 case Primitive::kPrimInt:
4721 return kStoreWord;
4722 case Primitive::kPrimLong:
4723 return kStoreWordPair;
4724 case Primitive::kPrimFloat:
4725 return kStoreSWord;
4726 case Primitive::kPrimDouble:
4727 return kStoreDWord;
4728 default:
4729 LOG(FATAL) << "Unreachable type " << type;
4730 UNREACHABLE();
4731 }
4732}
4733
4734void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4735 Location out_loc,
4736 Register base,
4737 Register reg_offset,
4738 Condition cond) {
4739 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4740 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4741
4742 switch (type) {
4743 case Primitive::kPrimByte:
4744 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4745 break;
4746 case Primitive::kPrimBoolean:
4747 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4748 break;
4749 case Primitive::kPrimShort:
4750 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4751 break;
4752 case Primitive::kPrimChar:
4753 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4754 break;
4755 case Primitive::kPrimNot:
4756 case Primitive::kPrimInt:
4757 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4758 break;
4759 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4760 case Primitive::kPrimLong:
4761 case Primitive::kPrimFloat:
4762 case Primitive::kPrimDouble:
4763 default:
4764 LOG(FATAL) << "Unreachable type " << type;
4765 UNREACHABLE();
4766 }
4767}
4768
4769void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4770 Location loc,
4771 Register base,
4772 Register reg_offset,
4773 Condition cond) {
4774 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4775 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4776
4777 switch (type) {
4778 case Primitive::kPrimByte:
4779 case Primitive::kPrimBoolean:
4780 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4781 break;
4782 case Primitive::kPrimShort:
4783 case Primitive::kPrimChar:
4784 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4785 break;
4786 case Primitive::kPrimNot:
4787 case Primitive::kPrimInt:
4788 __ str(loc.AsRegister<Register>(), mem_address, cond);
4789 break;
4790 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4791 case Primitive::kPrimLong:
4792 case Primitive::kPrimFloat:
4793 case Primitive::kPrimDouble:
4794 default:
4795 LOG(FATAL) << "Unreachable type " << type;
4796 UNREACHABLE();
4797 }
4798}
4799
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004800void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004801 bool object_array_get_with_read_barrier =
4802 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004803 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004804 new (GetGraph()->GetArena()) LocationSummary(instruction,
4805 object_array_get_with_read_barrier ?
4806 LocationSummary::kCallOnSlowPath :
4807 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004808 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004809 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004810 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004811 locations->SetInAt(0, Location::RequiresRegister());
4812 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004813 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4814 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4815 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004816 // The output overlaps in the case of an object array get with
4817 // read barriers enabled: we do not want the move to overwrite the
4818 // array's location, as we need it to emit the read barrier.
4819 locations->SetOut(
4820 Location::RequiresRegister(),
4821 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004822 }
Roland Levillainc9285912015-12-18 10:38:42 +00004823 // We need a temporary register for the read barrier marking slow
4824 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07004825 // Also need for String compression feature.
4826 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4827 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00004828 locations->AddTemp(Location::RequiresRegister());
4829 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004830}
4831
4832void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4833 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004834 Location obj_loc = locations->InAt(0);
4835 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004836 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004837 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004838 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004839 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07004840 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4841 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01004842 HInstruction* array_instr = instruction->GetArray();
4843 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004844 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4845 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Artem Serov6c916792016-07-11 14:02:34 +01004846
Roland Levillain4d027112015-07-01 15:41:14 +01004847 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004848 case Primitive::kPrimBoolean:
4849 case Primitive::kPrimByte:
4850 case Primitive::kPrimShort:
4851 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004852 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004853 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004854 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07004855 if (maybe_compressed_char_at) {
4856 Register length = IP;
4857 Label uncompressed_load, done;
4858 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4859 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4860 codegen_->MaybeRecordImplicitNullCheck(instruction);
4861 __ cmp(length, ShifterOperand(0));
4862 __ b(&uncompressed_load, GE);
4863 __ LoadFromOffset(kLoadUnsignedByte,
4864 out_loc.AsRegister<Register>(),
4865 obj,
4866 data_offset + const_index);
4867 __ b(&done);
4868 __ Bind(&uncompressed_load);
4869 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4870 out_loc.AsRegister<Register>(),
4871 obj,
4872 data_offset + (const_index << 1));
4873 __ Bind(&done);
4874 } else {
4875 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01004876
jessicahandojo05765752016-09-09 19:01:32 -07004877 LoadOperandType load_type = GetLoadOperandType(type);
4878 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4879 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004880 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004881 Register temp = IP;
4882
4883 if (has_intermediate_address) {
4884 // We do not need to compute the intermediate address from the array: the
4885 // input instruction has done it already. See the comment in
4886 // `TryExtractArrayAccessAddress()`.
4887 if (kIsDebugBuild) {
4888 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4889 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4890 }
4891 temp = obj;
4892 } else {
4893 __ add(temp, obj, ShifterOperand(data_offset));
4894 }
jessicahandojo05765752016-09-09 19:01:32 -07004895 if (maybe_compressed_char_at) {
4896 Label uncompressed_load, done;
4897 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4898 Register length = locations->GetTemp(0).AsRegister<Register>();
4899 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4900 codegen_->MaybeRecordImplicitNullCheck(instruction);
4901 __ cmp(length, ShifterOperand(0));
4902 __ b(&uncompressed_load, GE);
4903 __ ldrb(out_loc.AsRegister<Register>(),
4904 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
4905 __ b(&done);
4906 __ Bind(&uncompressed_load);
4907 __ ldrh(out_loc.AsRegister<Register>(),
4908 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
4909 __ Bind(&done);
4910 } else {
4911 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4912 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004913 }
4914 break;
4915 }
4916
Roland Levillainc9285912015-12-18 10:38:42 +00004917 case Primitive::kPrimNot: {
4918 static_assert(
4919 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4920 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004921 // /* HeapReference<Object> */ out =
4922 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4923 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4924 Location temp = locations->GetTemp(0);
4925 // Note that a potential implicit null check is handled in this
4926 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4927 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4928 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4929 } else {
4930 Register out = out_loc.AsRegister<Register>();
4931 if (index.IsConstant()) {
4932 size_t offset =
4933 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4934 __ LoadFromOffset(kLoadWord, out, obj, offset);
4935 codegen_->MaybeRecordImplicitNullCheck(instruction);
4936 // If read barriers are enabled, emit read barriers other than
4937 // Baker's using a slow path (and also unpoison the loaded
4938 // reference, if heap poisoning is enabled).
4939 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4940 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004941 Register temp = IP;
4942
4943 if (has_intermediate_address) {
4944 // We do not need to compute the intermediate address from the array: the
4945 // input instruction has done it already. See the comment in
4946 // `TryExtractArrayAccessAddress()`.
4947 if (kIsDebugBuild) {
4948 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4949 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4950 }
4951 temp = obj;
4952 } else {
4953 __ add(temp, obj, ShifterOperand(data_offset));
4954 }
4955 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004956
Roland Levillainc9285912015-12-18 10:38:42 +00004957 codegen_->MaybeRecordImplicitNullCheck(instruction);
4958 // If read barriers are enabled, emit read barriers other than
4959 // Baker's using a slow path (and also unpoison the loaded
4960 // reference, if heap poisoning is enabled).
4961 codegen_->MaybeGenerateReadBarrierSlow(
4962 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4963 }
4964 }
4965 break;
4966 }
4967
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004968 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004969 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004970 size_t offset =
4971 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004972 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004973 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004974 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004975 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004976 }
4977 break;
4978 }
4979
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004980 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004981 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004982 if (index.IsConstant()) {
4983 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004984 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004985 } else {
4986 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004987 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004988 }
4989 break;
4990 }
4991
4992 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004993 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004994 if (index.IsConstant()) {
4995 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004996 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004997 } else {
4998 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004999 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005000 }
5001 break;
5002 }
5003
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005004 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005005 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005006 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005007 }
Roland Levillain4d027112015-07-01 15:41:14 +01005008
5009 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005010 // Potential implicit null checks, in the case of reference
5011 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005012 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005013 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005014 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005015}
5016
5017void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005018 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005019
5020 bool needs_write_barrier =
5021 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005022 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005023
Nicolas Geoffray39468442014-09-02 15:17:15 +01005024 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005025 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005026 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005027 LocationSummary::kCallOnSlowPath :
5028 LocationSummary::kNoCall);
5029
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005030 locations->SetInAt(0, Location::RequiresRegister());
5031 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5032 if (Primitive::IsFloatingPointType(value_type)) {
5033 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005034 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005035 locations->SetInAt(2, Location::RequiresRegister());
5036 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005037 if (needs_write_barrier) {
5038 // Temporary registers for the write barrier.
5039 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005040 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005042}
5043
5044void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5045 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005046 Location array_loc = locations->InAt(0);
5047 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005048 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005049 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005050 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005051 bool needs_write_barrier =
5052 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005053 uint32_t data_offset =
5054 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5055 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005056 HInstruction* array_instr = instruction->GetArray();
5057 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00005058 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
5059 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005060
5061 switch (value_type) {
5062 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005063 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005065 case Primitive::kPrimChar:
5066 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005067 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005068 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5069 uint32_t full_offset =
5070 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5071 StoreOperandType store_type = GetStoreOperandType(value_type);
5072 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005073 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005074 Register temp = IP;
5075
5076 if (has_intermediate_address) {
5077 // We do not need to compute the intermediate address from the array: the
5078 // input instruction has done it already. See the comment in
5079 // `TryExtractArrayAccessAddress()`.
5080 if (kIsDebugBuild) {
5081 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5082 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5083 }
5084 temp = array;
5085 } else {
5086 __ add(temp, array, ShifterOperand(data_offset));
5087 }
Artem Serov6c916792016-07-11 14:02:34 +01005088 codegen_->StoreToShiftedRegOffset(value_type,
5089 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005090 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005091 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005092 }
5093 break;
5094 }
5095
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005096 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005097 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005098 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5099 // See the comment in instruction_simplifier_shared.cc.
5100 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005101
5102 if (instruction->InputAt(2)->IsNullConstant()) {
5103 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005104 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005105 size_t offset =
5106 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005107 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005108 } else {
5109 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005110 __ add(IP, array, ShifterOperand(data_offset));
5111 codegen_->StoreToShiftedRegOffset(value_type,
5112 value_loc,
5113 IP,
5114 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005115 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005116 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005117 DCHECK(!needs_write_barrier);
5118 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005119 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005120 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005121
5122 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005123 Location temp1_loc = locations->GetTemp(0);
5124 Register temp1 = temp1_loc.AsRegister<Register>();
5125 Location temp2_loc = locations->GetTemp(1);
5126 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005127 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5128 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5129 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5130 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005131 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005132
Roland Levillain3b359c72015-11-17 19:35:12 +00005133 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005134 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5135 codegen_->AddSlowPath(slow_path);
5136 if (instruction->GetValueCanBeNull()) {
5137 Label non_zero;
5138 __ CompareAndBranchIfNonZero(value, &non_zero);
5139 if (index.IsConstant()) {
5140 size_t offset =
5141 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5142 __ StoreToOffset(kStoreWord, value, array, offset);
5143 } else {
5144 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005145 __ add(IP, array, ShifterOperand(data_offset));
5146 codegen_->StoreToShiftedRegOffset(value_type,
5147 value_loc,
5148 IP,
5149 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005150 }
5151 codegen_->MaybeRecordImplicitNullCheck(instruction);
5152 __ b(&done);
5153 __ Bind(&non_zero);
5154 }
5155
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005156 // Note that when read barriers are enabled, the type checks
5157 // are performed without read barriers. This is fine, even in
5158 // the case where a class object is in the from-space after
5159 // the flip, as a comparison involving such a type would not
5160 // produce a false positive; it may of course produce a false
5161 // negative, in which case we would take the ArraySet slow
5162 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005163
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005164 // /* HeapReference<Class> */ temp1 = array->klass_
5165 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5166 codegen_->MaybeRecordImplicitNullCheck(instruction);
5167 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005168
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005169 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5170 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5171 // /* HeapReference<Class> */ temp2 = value->klass_
5172 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5173 // If heap poisoning is enabled, no need to unpoison `temp1`
5174 // nor `temp2`, as we are comparing two poisoned references.
5175 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005176
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005177 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5178 Label do_put;
5179 __ b(&do_put, EQ);
5180 // If heap poisoning is enabled, the `temp1` reference has
5181 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005182 __ MaybeUnpoisonHeapReference(temp1);
5183
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005184 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5185 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5186 // If heap poisoning is enabled, no need to unpoison
5187 // `temp1`, as we are comparing against null below.
5188 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5189 __ Bind(&do_put);
5190 } else {
5191 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005192 }
5193 }
5194
Artem Serov6c916792016-07-11 14:02:34 +01005195 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005196 if (kPoisonHeapReferences) {
5197 // Note that in the case where `value` is a null reference,
5198 // we do not enter this block, as a null reference does not
5199 // need poisoning.
5200 DCHECK_EQ(value_type, Primitive::kPrimNot);
5201 __ Mov(temp1, value);
5202 __ PoisonHeapReference(temp1);
5203 source = temp1;
5204 }
5205
5206 if (index.IsConstant()) {
5207 size_t offset =
5208 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5209 __ StoreToOffset(kStoreWord, source, array, offset);
5210 } else {
5211 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005212
5213 __ add(IP, array, ShifterOperand(data_offset));
5214 codegen_->StoreToShiftedRegOffset(value_type,
5215 Location::RegisterLocation(source),
5216 IP,
5217 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005218 }
5219
Roland Levillain3b359c72015-11-17 19:35:12 +00005220 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005221 codegen_->MaybeRecordImplicitNullCheck(instruction);
5222 }
5223
5224 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5225
5226 if (done.IsLinked()) {
5227 __ Bind(&done);
5228 }
5229
5230 if (slow_path != nullptr) {
5231 __ Bind(slow_path->GetExitLabel());
5232 }
5233
5234 break;
5235 }
5236
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005237 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005238 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005239 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005240 size_t offset =
5241 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005242 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005243 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005244 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005245 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005246 }
5247 break;
5248 }
5249
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005250 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005251 Location value = locations->InAt(2);
5252 DCHECK(value.IsFpuRegister());
5253 if (index.IsConstant()) {
5254 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005255 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005256 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005257 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005258 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5259 }
5260 break;
5261 }
5262
5263 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005264 Location value = locations->InAt(2);
5265 DCHECK(value.IsFpuRegisterPair());
5266 if (index.IsConstant()) {
5267 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005268 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005269 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005270 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005271 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5272 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005273
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005274 break;
5275 }
5276
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005277 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005278 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005279 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005280 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005281
Roland Levillain80e67092016-01-08 16:04:55 +00005282 // Objects are handled in the switch.
5283 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005284 codegen_->MaybeRecordImplicitNullCheck(instruction);
5285 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005286}
5287
5288void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005289 LocationSummary* locations =
5290 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005291 locations->SetInAt(0, Location::RequiresRegister());
5292 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293}
5294
5295void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5296 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005297 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005298 Register obj = locations->InAt(0).AsRegister<Register>();
5299 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005301 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005302 // Mask out compression flag from String's array length.
5303 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
5304 __ bic(out, out, ShifterOperand(1u << 31));
5305 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306}
5307
Artem Serov328429f2016-07-06 16:23:04 +01005308void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00005309 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
5310 DCHECK(!kEmitCompilerReadBarrier);
Artem Serov328429f2016-07-06 16:23:04 +01005311 LocationSummary* locations =
5312 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5313
5314 locations->SetInAt(0, Location::RequiresRegister());
5315 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5316 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5317}
5318
5319void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5320 LocationSummary* locations = instruction->GetLocations();
5321 Location out = locations->Out();
5322 Location first = locations->InAt(0);
5323 Location second = locations->InAt(1);
5324
Roland Levillain4a3aa572016-08-15 13:17:06 +00005325 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
5326 DCHECK(!kEmitCompilerReadBarrier);
5327
Artem Serov328429f2016-07-06 16:23:04 +01005328 if (second.IsRegister()) {
5329 __ add(out.AsRegister<Register>(),
5330 first.AsRegister<Register>(),
5331 ShifterOperand(second.AsRegister<Register>()));
5332 } else {
5333 __ AddConstant(out.AsRegister<Register>(),
5334 first.AsRegister<Register>(),
5335 second.GetConstant()->AsIntConstant()->GetValue());
5336 }
5337}
5338
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005339void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005340 RegisterSet caller_saves = RegisterSet::Empty();
5341 InvokeRuntimeCallingConvention calling_convention;
5342 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5343 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5344 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005345 locations->SetInAt(0, Location::RequiresRegister());
5346 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005347}
5348
5349void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5350 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005351 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005352 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353 codegen_->AddSlowPath(slow_path);
5354
Roland Levillain271ab9c2014-11-27 15:23:57 +00005355 Register index = locations->InAt(0).AsRegister<Register>();
5356 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005357
5358 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005359 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005360}
5361
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005362void CodeGeneratorARM::MarkGCCard(Register temp,
5363 Register card,
5364 Register object,
5365 Register value,
5366 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005367 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005368 if (can_be_null) {
5369 __ CompareAndBranchIfZero(value, &is_null);
5370 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005371 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005372 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5373 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005374 if (can_be_null) {
5375 __ Bind(&is_null);
5376 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005377}
5378
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005379void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005380 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005381}
5382
5383void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005384 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5385}
5386
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005387void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005388 LocationSummary* locations =
5389 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005390 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005391}
5392
5393void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005394 HBasicBlock* block = instruction->GetBlock();
5395 if (block->GetLoopInformation() != nullptr) {
5396 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5397 // The back edge will generate the suspend check.
5398 return;
5399 }
5400 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5401 // The goto will generate the suspend check.
5402 return;
5403 }
5404 GenerateSuspendCheck(instruction, nullptr);
5405}
5406
5407void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5408 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005409 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005410 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5411 if (slow_path == nullptr) {
5412 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5413 instruction->SetSlowPath(slow_path);
5414 codegen_->AddSlowPath(slow_path);
5415 if (successor != nullptr) {
5416 DCHECK(successor->IsLoopHeader());
5417 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5418 }
5419 } else {
5420 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5421 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005422
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005423 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005424 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005425 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005426 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005427 __ Bind(slow_path->GetReturnLabel());
5428 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005429 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005430 __ b(slow_path->GetEntryLabel());
5431 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005432}
5433
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005434ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5435 return codegen_->GetAssembler();
5436}
5437
5438void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005439 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005440 Location source = move->GetSource();
5441 Location destination = move->GetDestination();
5442
5443 if (source.IsRegister()) {
5444 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005445 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005446 } else if (destination.IsFpuRegister()) {
5447 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005448 } else {
5449 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005450 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005451 SP, destination.GetStackIndex());
5452 }
5453 } else if (source.IsStackSlot()) {
5454 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005455 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005456 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005457 } else if (destination.IsFpuRegister()) {
5458 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005459 } else {
5460 DCHECK(destination.IsStackSlot());
5461 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5462 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5463 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005464 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005465 if (destination.IsRegister()) {
5466 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5467 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005468 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005469 } else {
5470 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005471 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5472 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005473 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005474 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005475 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5476 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005477 } else if (destination.IsRegisterPair()) {
5478 DCHECK(ExpectedPairLayout(destination));
5479 __ LoadFromOffset(
5480 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5481 } else {
5482 DCHECK(destination.IsFpuRegisterPair()) << destination;
5483 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5484 SP,
5485 source.GetStackIndex());
5486 }
5487 } else if (source.IsRegisterPair()) {
5488 if (destination.IsRegisterPair()) {
5489 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5490 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005491 } else if (destination.IsFpuRegisterPair()) {
5492 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5493 source.AsRegisterPairLow<Register>(),
5494 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005495 } else {
5496 DCHECK(destination.IsDoubleStackSlot()) << destination;
5497 DCHECK(ExpectedPairLayout(source));
5498 __ StoreToOffset(
5499 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5500 }
5501 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005502 if (destination.IsRegisterPair()) {
5503 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5504 destination.AsRegisterPairHigh<Register>(),
5505 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5506 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005507 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5508 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5509 } else {
5510 DCHECK(destination.IsDoubleStackSlot()) << destination;
5511 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5512 SP,
5513 destination.GetStackIndex());
5514 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005515 } else {
5516 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005517 HConstant* constant = source.GetConstant();
5518 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5519 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005520 if (destination.IsRegister()) {
5521 __ LoadImmediate(destination.AsRegister<Register>(), value);
5522 } else {
5523 DCHECK(destination.IsStackSlot());
5524 __ LoadImmediate(IP, value);
5525 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5526 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005527 } else if (constant->IsLongConstant()) {
5528 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005529 if (destination.IsRegisterPair()) {
5530 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5531 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005532 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005533 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005534 __ LoadImmediate(IP, Low32Bits(value));
5535 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5536 __ LoadImmediate(IP, High32Bits(value));
5537 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5538 }
5539 } else if (constant->IsDoubleConstant()) {
5540 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005541 if (destination.IsFpuRegisterPair()) {
5542 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005543 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005544 DCHECK(destination.IsDoubleStackSlot()) << destination;
5545 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005546 __ LoadImmediate(IP, Low32Bits(int_value));
5547 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5548 __ LoadImmediate(IP, High32Bits(int_value));
5549 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5550 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005551 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005552 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005553 float value = constant->AsFloatConstant()->GetValue();
5554 if (destination.IsFpuRegister()) {
5555 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5556 } else {
5557 DCHECK(destination.IsStackSlot());
5558 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5559 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5560 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005561 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005562 }
5563}
5564
5565void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5566 __ Mov(IP, reg);
5567 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5568 __ StoreToOffset(kStoreWord, IP, SP, mem);
5569}
5570
5571void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5572 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5573 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5574 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5575 SP, mem1 + stack_offset);
5576 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5577 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5578 SP, mem2 + stack_offset);
5579 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5580}
5581
5582void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005583 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005584 Location source = move->GetSource();
5585 Location destination = move->GetDestination();
5586
5587 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005588 DCHECK_NE(source.AsRegister<Register>(), IP);
5589 DCHECK_NE(destination.AsRegister<Register>(), IP);
5590 __ Mov(IP, source.AsRegister<Register>());
5591 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5592 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005593 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005594 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005595 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005596 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005597 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5598 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005599 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005600 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005601 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005602 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005603 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005604 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005605 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005606 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005607 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5608 destination.AsRegisterPairHigh<Register>(),
5609 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005610 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005611 Register low_reg = source.IsRegisterPair()
5612 ? source.AsRegisterPairLow<Register>()
5613 : destination.AsRegisterPairLow<Register>();
5614 int mem = source.IsRegisterPair()
5615 ? destination.GetStackIndex()
5616 : source.GetStackIndex();
5617 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005618 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005619 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005620 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005621 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005622 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5623 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005624 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005625 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005626 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005627 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5628 DRegister reg = source.IsFpuRegisterPair()
5629 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5630 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5631 int mem = source.IsFpuRegisterPair()
5632 ? destination.GetStackIndex()
5633 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005634 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005635 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005636 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005637 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5638 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5639 : destination.AsFpuRegister<SRegister>();
5640 int mem = source.IsFpuRegister()
5641 ? destination.GetStackIndex()
5642 : source.GetStackIndex();
5643
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005644 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005645 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005646 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005647 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005648 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5649 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005650 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005651 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005652 }
5653}
5654
5655void ParallelMoveResolverARM::SpillScratch(int reg) {
5656 __ Push(static_cast<Register>(reg));
5657}
5658
5659void ParallelMoveResolverARM::RestoreScratch(int reg) {
5660 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005661}
5662
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005663HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5664 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005665 switch (desired_class_load_kind) {
5666 case HLoadClass::LoadKind::kReferrersClass:
5667 break;
5668 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5669 DCHECK(!GetCompilerOptions().GetCompilePic());
5670 break;
5671 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5672 DCHECK(GetCompilerOptions().GetCompilePic());
5673 break;
5674 case HLoadClass::LoadKind::kBootImageAddress:
5675 break;
5676 case HLoadClass::LoadKind::kDexCacheAddress:
5677 DCHECK(Runtime::Current()->UseJitCompilation());
5678 break;
5679 case HLoadClass::LoadKind::kDexCachePcRelative:
5680 DCHECK(!Runtime::Current()->UseJitCompilation());
5681 // We disable pc-relative load when there is an irreducible loop, as the optimization
5682 // is incompatible with it.
5683 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5684 // with irreducible loops.
5685 if (GetGraph()->HasIrreducibleLoops()) {
5686 return HLoadClass::LoadKind::kDexCacheViaMethod;
5687 }
5688 break;
5689 case HLoadClass::LoadKind::kDexCacheViaMethod:
5690 break;
5691 }
5692 return desired_class_load_kind;
5693}
5694
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005695void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005696 if (cls->NeedsAccessCheck()) {
5697 InvokeRuntimeCallingConvention calling_convention;
5698 CodeGenerator::CreateLoadClassLocationSummary(
5699 cls,
5700 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5701 Location::RegisterLocation(R0),
5702 /* code_generator_supports_read_barrier */ true);
5703 return;
5704 }
5705
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005706 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5707 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005708 ? LocationSummary::kCallOnSlowPath
5709 : LocationSummary::kNoCall;
5710 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005711 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005712 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005713 }
5714
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005715 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5716 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5717 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5718 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5719 locations->SetInAt(0, Location::RequiresRegister());
5720 }
5721 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005722}
5723
5724void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005725 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005726 if (cls->NeedsAccessCheck()) {
5727 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005728 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005729 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005730 return;
5731 }
5732
Roland Levillain3b359c72015-11-17 19:35:12 +00005733 Location out_loc = locations->Out();
5734 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005735
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005736 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005737 bool generate_null_check = false;
5738 switch (cls->GetLoadKind()) {
5739 case HLoadClass::LoadKind::kReferrersClass: {
5740 DCHECK(!cls->CanCallRuntime());
5741 DCHECK(!cls->MustGenerateClinitCheck());
5742 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5743 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005744 GenerateGcRootFieldLoad(cls,
5745 out_loc,
5746 current_method,
5747 ArtMethod::DeclaringClassOffset().Int32Value(),
5748 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005749 break;
5750 }
5751 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005752 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005753 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5754 cls->GetTypeIndex()));
5755 break;
5756 }
5757 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005758 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005759 CodeGeneratorARM::PcRelativePatchInfo* labels =
5760 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5761 __ BindTrackedLabel(&labels->movw_label);
5762 __ movw(out, /* placeholder */ 0u);
5763 __ BindTrackedLabel(&labels->movt_label);
5764 __ movt(out, /* placeholder */ 0u);
5765 __ BindTrackedLabel(&labels->add_pc_label);
5766 __ add(out, out, ShifterOperand(PC));
5767 break;
5768 }
5769 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005770 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005771 DCHECK_NE(cls->GetAddress(), 0u);
5772 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5773 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5774 break;
5775 }
5776 case HLoadClass::LoadKind::kDexCacheAddress: {
5777 DCHECK_NE(cls->GetAddress(), 0u);
5778 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5779 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5780 // a 128B range. To try and reduce the number of literals if we load multiple types,
5781 // simply split the dex cache address to a 128B aligned base loaded from a literal
5782 // and the remaining offset embedded in the load.
5783 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5784 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5785 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5786 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5787 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5788 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5789 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005790 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005791 generate_null_check = !cls->IsInDexCache();
5792 break;
5793 }
5794 case HLoadClass::LoadKind::kDexCachePcRelative: {
5795 Register base_reg = locations->InAt(0).AsRegister<Register>();
5796 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5797 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5798 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005799 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005800 generate_null_check = !cls->IsInDexCache();
5801 break;
5802 }
5803 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5804 // /* GcRoot<mirror::Class>[] */ out =
5805 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5806 Register current_method = locations->InAt(0).AsRegister<Register>();
5807 __ LoadFromOffset(kLoadWord,
5808 out,
5809 current_method,
5810 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5811 // /* GcRoot<mirror::Class> */ out = out[type_index]
5812 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005813 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005814 generate_null_check = !cls->IsInDexCache();
5815 }
5816 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005817
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005818 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5819 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005820 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005821 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5822 codegen_->AddSlowPath(slow_path);
5823 if (generate_null_check) {
5824 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5825 }
5826 if (cls->MustGenerateClinitCheck()) {
5827 GenerateClassInitializationCheck(slow_path, out);
5828 } else {
5829 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005830 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005831 }
5832}
5833
5834void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5835 LocationSummary* locations =
5836 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5837 locations->SetInAt(0, Location::RequiresRegister());
5838 if (check->HasUses()) {
5839 locations->SetOut(Location::SameAsFirstInput());
5840 }
5841}
5842
5843void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005844 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005845 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005846 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005847 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005848 GenerateClassInitializationCheck(slow_path,
5849 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005850}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005851
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005852void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005853 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005854 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5855 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5856 __ b(slow_path->GetEntryLabel(), LT);
5857 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5858 // properly. Therefore, we do a memory fence.
5859 __ dmb(ISH);
5860 __ Bind(slow_path->GetExitLabel());
5861}
5862
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005863HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5864 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005865 switch (desired_string_load_kind) {
5866 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5867 DCHECK(!GetCompilerOptions().GetCompilePic());
5868 break;
5869 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5870 DCHECK(GetCompilerOptions().GetCompilePic());
5871 break;
5872 case HLoadString::LoadKind::kBootImageAddress:
5873 break;
5874 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005875 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005876 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005877 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005878 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005879 break;
5880 case HLoadString::LoadKind::kDexCacheViaMethod:
5881 break;
5882 }
5883 return desired_string_load_kind;
5884}
5885
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005886void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005887 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00005888 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5889 ? LocationSummary::kCallOnMainOnly
5890 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005891 : LocationSummary::kNoCall;
5892 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005893
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005894 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005895 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005896 locations->SetOut(Location::RegisterLocation(R0));
5897 } else {
5898 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005899 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5900 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5901 // Rely on the pResolveString and/or marking to save everything, including temps.
5902 // Note that IP may theoretically be clobbered by saving/restoring the live register
5903 // (only one thanks to the custom calling convention), so we request a different temp.
5904 locations->AddTemp(Location::RequiresRegister());
5905 RegisterSet caller_saves = RegisterSet::Empty();
5906 InvokeRuntimeCallingConvention calling_convention;
5907 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5908 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5909 // that the the kPrimNot result register is the same as the first argument register.
5910 locations->SetCustomSlowPathCallerSaves(caller_saves);
5911 } else {
5912 // For non-Baker read barrier we have a temp-clobbering call.
5913 }
5914 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005915 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005916}
5917
5918void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005919 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005920 Location out_loc = locations->Out();
5921 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005922 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005923
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005924 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005925 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005926 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5927 load->GetStringIndex()));
5928 return; // No dex cache slow path.
5929 }
5930 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005931 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005932 CodeGeneratorARM::PcRelativePatchInfo* labels =
5933 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5934 __ BindTrackedLabel(&labels->movw_label);
5935 __ movw(out, /* placeholder */ 0u);
5936 __ BindTrackedLabel(&labels->movt_label);
5937 __ movt(out, /* placeholder */ 0u);
5938 __ BindTrackedLabel(&labels->add_pc_label);
5939 __ add(out, out, ShifterOperand(PC));
5940 return; // No dex cache slow path.
5941 }
5942 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005943 DCHECK_NE(load->GetAddress(), 0u);
5944 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5945 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5946 return; // No dex cache slow path.
5947 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005948 case HLoadString::LoadKind::kBssEntry: {
5949 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005950 Register temp = locations->GetTemp(0).AsRegister<Register>();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005951 CodeGeneratorARM::PcRelativePatchInfo* labels =
5952 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5953 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005954 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005955 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005956 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005957 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005958 __ add(temp, temp, ShifterOperand(PC));
Roland Levillain00468f32016-10-27 18:02:48 +01005959 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005960 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5961 codegen_->AddSlowPath(slow_path);
5962 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5963 __ Bind(slow_path->GetExitLabel());
5964 return;
5965 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005966 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005967 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005968 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005969
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005970 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
5971 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5972 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005973 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005974 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5975 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5976 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005977}
5978
David Brazdilcb1c0552015-08-04 16:22:25 +01005979static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005980 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005981}
5982
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005983void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5984 LocationSummary* locations =
5985 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5986 locations->SetOut(Location::RequiresRegister());
5987}
5988
5989void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005990 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005991 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5992}
5993
5994void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5995 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5996}
5997
5998void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005999 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006000 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006001}
6002
6003void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6004 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006005 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006006 InvokeRuntimeCallingConvention calling_convention;
6007 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6008}
6009
6010void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006011 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006012 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006013}
6014
Roland Levillainc9285912015-12-18 10:38:42 +00006015static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6016 return kEmitCompilerReadBarrier &&
6017 (kUseBakerReadBarrier ||
6018 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6019 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6020 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6021}
6022
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006023void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006024 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006025 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006026 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006027 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006028 case TypeCheckKind::kExactCheck:
6029 case TypeCheckKind::kAbstractClassCheck:
6030 case TypeCheckKind::kClassHierarchyCheck:
6031 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006032 call_kind =
6033 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006034 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006035 break;
6036 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006037 case TypeCheckKind::kUnresolvedCheck:
6038 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006039 call_kind = LocationSummary::kCallOnSlowPath;
6040 break;
6041 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006042
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006043 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006044 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006045 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006046 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006047 locations->SetInAt(0, Location::RequiresRegister());
6048 locations->SetInAt(1, Location::RequiresRegister());
6049 // The "out" register is used as a temporary, so it overlaps with the inputs.
6050 // Note that TypeCheckSlowPathARM uses this register too.
6051 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
6052 // When read barriers are enabled, we need a temporary register for
6053 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00006054 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006055 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006056 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006057}
6058
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006059void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006060 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006061 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006062 Location obj_loc = locations->InAt(0);
6063 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006064 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006065 Location out_loc = locations->Out();
6066 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006067 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00006068 locations->GetTemp(0) :
6069 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006070 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006071 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6072 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6073 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006074 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006075 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006076
6077 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006078 // avoid null check if we know obj is not null.
6079 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006080 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006081 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006082
Roland Levillain3b359c72015-11-17 19:35:12 +00006083 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006084 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006085
Roland Levillainc9285912015-12-18 10:38:42 +00006086 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006087 case TypeCheckKind::kExactCheck: {
6088 __ cmp(out, ShifterOperand(cls));
6089 // Classes must be equal for the instanceof to succeed.
6090 __ b(&zero, NE);
6091 __ LoadImmediate(out, 1);
6092 __ b(&done);
6093 break;
6094 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006095
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006096 case TypeCheckKind::kAbstractClassCheck: {
6097 // If the class is abstract, we eagerly fetch the super class of the
6098 // object to avoid doing a comparison we know will fail.
6099 Label loop;
6100 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006101 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006102 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006103 // If `out` is null, we use it for the result, and jump to `done`.
6104 __ CompareAndBranchIfZero(out, &done);
6105 __ cmp(out, ShifterOperand(cls));
6106 __ b(&loop, NE);
6107 __ LoadImmediate(out, 1);
6108 if (zero.IsLinked()) {
6109 __ b(&done);
6110 }
6111 break;
6112 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006113
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006114 case TypeCheckKind::kClassHierarchyCheck: {
6115 // Walk over the class hierarchy to find a match.
6116 Label loop, success;
6117 __ Bind(&loop);
6118 __ cmp(out, ShifterOperand(cls));
6119 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006120 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006121 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006122 __ CompareAndBranchIfNonZero(out, &loop);
6123 // If `out` is null, we use it for the result, and jump to `done`.
6124 __ b(&done);
6125 __ Bind(&success);
6126 __ LoadImmediate(out, 1);
6127 if (zero.IsLinked()) {
6128 __ b(&done);
6129 }
6130 break;
6131 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006132
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006133 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006134 // Do an exact check.
6135 Label exact_check;
6136 __ cmp(out, ShifterOperand(cls));
6137 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006138 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006139 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006140 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141 // If `out` is null, we use it for the result, and jump to `done`.
6142 __ CompareAndBranchIfZero(out, &done);
6143 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6144 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6145 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006146 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006147 __ LoadImmediate(out, 1);
6148 __ b(&done);
6149 break;
6150 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006151
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152 case TypeCheckKind::kArrayCheck: {
6153 __ cmp(out, ShifterOperand(cls));
6154 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006155 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6156 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006157 codegen_->AddSlowPath(slow_path);
6158 __ b(slow_path->GetEntryLabel(), NE);
6159 __ LoadImmediate(out, 1);
6160 if (zero.IsLinked()) {
6161 __ b(&done);
6162 }
6163 break;
6164 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006165
Calin Juravle98893e12015-10-02 21:05:03 +01006166 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006167 case TypeCheckKind::kInterfaceCheck: {
6168 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006169 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006170 // cases.
6171 //
6172 // We cannot directly call the InstanceofNonTrivial runtime
6173 // entry point without resorting to a type checking slow path
6174 // here (i.e. by calling InvokeRuntime directly), as it would
6175 // require to assign fixed registers for the inputs of this
6176 // HInstanceOf instruction (following the runtime calling
6177 // convention), which might be cluttered by the potential first
6178 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006179 //
6180 // TODO: Introduce a new runtime entry point taking the object
6181 // to test (instead of its class) as argument, and let it deal
6182 // with the read barrier issues. This will let us refactor this
6183 // case of the `switch` code as it was previously (with a direct
6184 // call to the runtime not using a type checking slow path).
6185 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006186 DCHECK(locations->OnlyCallsOnSlowPath());
6187 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6188 /* is_fatal */ false);
6189 codegen_->AddSlowPath(slow_path);
6190 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006191 if (zero.IsLinked()) {
6192 __ b(&done);
6193 }
6194 break;
6195 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006196 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006197
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006198 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006199 __ Bind(&zero);
6200 __ LoadImmediate(out, 0);
6201 }
6202
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006203 if (done.IsLinked()) {
6204 __ Bind(&done);
6205 }
6206
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006207 if (slow_path != nullptr) {
6208 __ Bind(slow_path->GetExitLabel());
6209 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006210}
6211
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006212void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006213 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6214 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6215
Roland Levillain3b359c72015-11-17 19:35:12 +00006216 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6217 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006218 case TypeCheckKind::kExactCheck:
6219 case TypeCheckKind::kAbstractClassCheck:
6220 case TypeCheckKind::kClassHierarchyCheck:
6221 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006222 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6223 LocationSummary::kCallOnSlowPath :
6224 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006225 break;
6226 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006227 case TypeCheckKind::kUnresolvedCheck:
6228 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006229 call_kind = LocationSummary::kCallOnSlowPath;
6230 break;
6231 }
6232
Roland Levillain3b359c72015-11-17 19:35:12 +00006233 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6234 locations->SetInAt(0, Location::RequiresRegister());
6235 locations->SetInAt(1, Location::RequiresRegister());
6236 // Note that TypeCheckSlowPathARM uses this "temp" register too.
6237 locations->AddTemp(Location::RequiresRegister());
6238 // When read barriers are enabled, we need an additional temporary
6239 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00006240 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006241 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006242 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006243}
6244
6245void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006246 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006247 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006248 Location obj_loc = locations->InAt(0);
6249 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006250 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006251 Location temp_loc = locations->GetTemp(0);
6252 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006253 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00006254 locations->GetTemp(1) :
6255 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006256 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006257 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6258 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6259 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006260
Roland Levillain3b359c72015-11-17 19:35:12 +00006261 bool is_type_check_slow_path_fatal =
6262 (type_check_kind == TypeCheckKind::kExactCheck ||
6263 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6264 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6265 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6266 !instruction->CanThrowIntoCatchBlock();
Artem Serovf4d6aee2016-07-11 10:41:45 +01006267 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006268 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6269 is_type_check_slow_path_fatal);
6270 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006271
6272 Label done;
6273 // Avoid null check if we know obj is not null.
6274 if (instruction->MustDoNullCheck()) {
6275 __ CompareAndBranchIfZero(obj, &done);
6276 }
6277
Roland Levillain3b359c72015-11-17 19:35:12 +00006278 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006279 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006280
Roland Levillain3b359c72015-11-17 19:35:12 +00006281 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006282 case TypeCheckKind::kExactCheck:
6283 case TypeCheckKind::kArrayCheck: {
6284 __ cmp(temp, ShifterOperand(cls));
6285 // Jump to slow path for throwing the exception or doing a
6286 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006287 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006288 break;
6289 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006290
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006291 case TypeCheckKind::kAbstractClassCheck: {
6292 // If the class is abstract, we eagerly fetch the super class of the
6293 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00006294 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006295 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006296 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006297 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006298
6299 // If the class reference currently in `temp` is not null, jump
6300 // to the `compare_classes` label to compare it with the checked
6301 // class.
6302 __ CompareAndBranchIfNonZero(temp, &compare_classes);
6303 // Otherwise, jump to the slow path to throw the exception.
6304 //
6305 // But before, move back the object's class into `temp` before
6306 // going into the slow path, as it has been overwritten in the
6307 // meantime.
6308 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006309 GenerateReferenceLoadTwoRegisters(
6310 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006311 __ b(type_check_slow_path->GetEntryLabel());
6312
6313 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006314 __ cmp(temp, ShifterOperand(cls));
6315 __ b(&loop, NE);
6316 break;
6317 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006318
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006319 case TypeCheckKind::kClassHierarchyCheck: {
6320 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006321 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006322 __ Bind(&loop);
6323 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006324 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006325
Roland Levillain3b359c72015-11-17 19:35:12 +00006326 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006327 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006328
6329 // If the class reference currently in `temp` is not null, jump
6330 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006331 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006332 // Otherwise, jump to the slow path to throw the exception.
6333 //
6334 // But before, move back the object's class into `temp` before
6335 // going into the slow path, as it has been overwritten in the
6336 // meantime.
6337 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006338 GenerateReferenceLoadTwoRegisters(
6339 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006340 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006341 break;
6342 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006343
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006344 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006345 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006346 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006347 __ cmp(temp, ShifterOperand(cls));
6348 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006349
6350 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006351 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006352 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006353
6354 // If the component type is not null (i.e. the object is indeed
6355 // an array), jump to label `check_non_primitive_component_type`
6356 // to further check that this component type is not a primitive
6357 // type.
6358 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
6359 // Otherwise, jump to the slow path to throw the exception.
6360 //
6361 // But before, move back the object's class into `temp` before
6362 // going into the slow path, as it has been overwritten in the
6363 // meantime.
6364 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006365 GenerateReferenceLoadTwoRegisters(
6366 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006367 __ b(type_check_slow_path->GetEntryLabel());
6368
6369 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006370 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006371 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
6372 __ CompareAndBranchIfZero(temp, &done);
6373 // Same comment as above regarding `temp` and the slow path.
6374 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006375 GenerateReferenceLoadTwoRegisters(
6376 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006377 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006378 break;
6379 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006380
Calin Juravle98893e12015-10-02 21:05:03 +01006381 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006382 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006383 // We always go into the type check slow path for the unresolved
6384 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00006385 //
6386 // We cannot directly call the CheckCast runtime entry point
6387 // without resorting to a type checking slow path here (i.e. by
6388 // calling InvokeRuntime directly), as it would require to
6389 // assign fixed registers for the inputs of this HInstanceOf
6390 // instruction (following the runtime calling convention), which
6391 // might be cluttered by the potential first read barrier
6392 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006393 //
6394 // TODO: Introduce a new runtime entry point taking the object
6395 // to test (instead of its class) as argument, and let it deal
6396 // with the read barrier issues. This will let us refactor this
6397 // case of the `switch` code as it was previously (with a direct
6398 // call to the runtime not using a type checking slow path).
6399 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006400 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006401 break;
6402 }
6403 __ Bind(&done);
6404
Roland Levillain3b359c72015-11-17 19:35:12 +00006405 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006406}
6407
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006408void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6409 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006410 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006411 InvokeRuntimeCallingConvention calling_convention;
6412 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6413}
6414
6415void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006416 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6417 instruction,
6418 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006419 if (instruction->IsEnter()) {
6420 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6421 } else {
6422 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6423 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006424}
6425
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006426void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6427void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6428void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006429
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006430void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006431 LocationSummary* locations =
6432 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6433 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6434 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006435 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006436 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006437 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006438 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006439}
6440
6441void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6442 HandleBitwiseOperation(instruction);
6443}
6444
6445void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6446 HandleBitwiseOperation(instruction);
6447}
6448
6449void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6450 HandleBitwiseOperation(instruction);
6451}
6452
Artem Serov7fc63502016-02-09 17:15:29 +00006453
6454void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6455 LocationSummary* locations =
6456 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6457 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6458 || instruction->GetResultType() == Primitive::kPrimLong);
6459
6460 locations->SetInAt(0, Location::RequiresRegister());
6461 locations->SetInAt(1, Location::RequiresRegister());
6462 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6463}
6464
6465void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6466 LocationSummary* locations = instruction->GetLocations();
6467 Location first = locations->InAt(0);
6468 Location second = locations->InAt(1);
6469 Location out = locations->Out();
6470
6471 if (instruction->GetResultType() == Primitive::kPrimInt) {
6472 Register first_reg = first.AsRegister<Register>();
6473 ShifterOperand second_reg(second.AsRegister<Register>());
6474 Register out_reg = out.AsRegister<Register>();
6475
6476 switch (instruction->GetOpKind()) {
6477 case HInstruction::kAnd:
6478 __ bic(out_reg, first_reg, second_reg);
6479 break;
6480 case HInstruction::kOr:
6481 __ orn(out_reg, first_reg, second_reg);
6482 break;
6483 // There is no EON on arm.
6484 case HInstruction::kXor:
6485 default:
6486 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6487 UNREACHABLE();
6488 }
6489 return;
6490
6491 } else {
6492 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6493 Register first_low = first.AsRegisterPairLow<Register>();
6494 Register first_high = first.AsRegisterPairHigh<Register>();
6495 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6496 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6497 Register out_low = out.AsRegisterPairLow<Register>();
6498 Register out_high = out.AsRegisterPairHigh<Register>();
6499
6500 switch (instruction->GetOpKind()) {
6501 case HInstruction::kAnd:
6502 __ bic(out_low, first_low, second_low);
6503 __ bic(out_high, first_high, second_high);
6504 break;
6505 case HInstruction::kOr:
6506 __ orn(out_low, first_low, second_low);
6507 __ orn(out_high, first_high, second_high);
6508 break;
6509 // There is no EON on arm.
6510 case HInstruction::kXor:
6511 default:
6512 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6513 UNREACHABLE();
6514 }
6515 }
6516}
6517
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006518void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6519 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6520 if (value == 0xffffffffu) {
6521 if (out != first) {
6522 __ mov(out, ShifterOperand(first));
6523 }
6524 return;
6525 }
6526 if (value == 0u) {
6527 __ mov(out, ShifterOperand(0));
6528 return;
6529 }
6530 ShifterOperand so;
6531 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6532 __ and_(out, first, so);
6533 } else {
6534 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6535 __ bic(out, first, ShifterOperand(~value));
6536 }
6537}
6538
6539void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6540 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6541 if (value == 0u) {
6542 if (out != first) {
6543 __ mov(out, ShifterOperand(first));
6544 }
6545 return;
6546 }
6547 if (value == 0xffffffffu) {
6548 __ mvn(out, ShifterOperand(0));
6549 return;
6550 }
6551 ShifterOperand so;
6552 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6553 __ orr(out, first, so);
6554 } else {
6555 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6556 __ orn(out, first, ShifterOperand(~value));
6557 }
6558}
6559
6560void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6561 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6562 if (value == 0u) {
6563 if (out != first) {
6564 __ mov(out, ShifterOperand(first));
6565 }
6566 return;
6567 }
6568 __ eor(out, first, ShifterOperand(value));
6569}
6570
Vladimir Marko59751a72016-08-05 14:37:27 +01006571void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6572 Location first,
6573 uint64_t value) {
6574 Register out_low = out.AsRegisterPairLow<Register>();
6575 Register out_high = out.AsRegisterPairHigh<Register>();
6576 Register first_low = first.AsRegisterPairLow<Register>();
6577 Register first_high = first.AsRegisterPairHigh<Register>();
6578 uint32_t value_low = Low32Bits(value);
6579 uint32_t value_high = High32Bits(value);
6580 if (value_low == 0u) {
6581 if (out_low != first_low) {
6582 __ mov(out_low, ShifterOperand(first_low));
6583 }
6584 __ AddConstant(out_high, first_high, value_high);
6585 return;
6586 }
6587 __ AddConstantSetFlags(out_low, first_low, value_low);
6588 ShifterOperand so;
6589 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6590 __ adc(out_high, first_high, so);
6591 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6592 __ sbc(out_high, first_high, so);
6593 } else {
6594 LOG(FATAL) << "Unexpected constant " << value_high;
6595 UNREACHABLE();
6596 }
6597}
6598
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006599void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6600 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006601 Location first = locations->InAt(0);
6602 Location second = locations->InAt(1);
6603 Location out = locations->Out();
6604
6605 if (second.IsConstant()) {
6606 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6607 uint32_t value_low = Low32Bits(value);
6608 if (instruction->GetResultType() == Primitive::kPrimInt) {
6609 Register first_reg = first.AsRegister<Register>();
6610 Register out_reg = out.AsRegister<Register>();
6611 if (instruction->IsAnd()) {
6612 GenerateAndConst(out_reg, first_reg, value_low);
6613 } else if (instruction->IsOr()) {
6614 GenerateOrrConst(out_reg, first_reg, value_low);
6615 } else {
6616 DCHECK(instruction->IsXor());
6617 GenerateEorConst(out_reg, first_reg, value_low);
6618 }
6619 } else {
6620 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6621 uint32_t value_high = High32Bits(value);
6622 Register first_low = first.AsRegisterPairLow<Register>();
6623 Register first_high = first.AsRegisterPairHigh<Register>();
6624 Register out_low = out.AsRegisterPairLow<Register>();
6625 Register out_high = out.AsRegisterPairHigh<Register>();
6626 if (instruction->IsAnd()) {
6627 GenerateAndConst(out_low, first_low, value_low);
6628 GenerateAndConst(out_high, first_high, value_high);
6629 } else if (instruction->IsOr()) {
6630 GenerateOrrConst(out_low, first_low, value_low);
6631 GenerateOrrConst(out_high, first_high, value_high);
6632 } else {
6633 DCHECK(instruction->IsXor());
6634 GenerateEorConst(out_low, first_low, value_low);
6635 GenerateEorConst(out_high, first_high, value_high);
6636 }
6637 }
6638 return;
6639 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006640
6641 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006642 Register first_reg = first.AsRegister<Register>();
6643 ShifterOperand second_reg(second.AsRegister<Register>());
6644 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006645 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006646 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006647 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006648 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006649 } else {
6650 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006651 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006652 }
6653 } else {
6654 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006655 Register first_low = first.AsRegisterPairLow<Register>();
6656 Register first_high = first.AsRegisterPairHigh<Register>();
6657 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6658 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6659 Register out_low = out.AsRegisterPairLow<Register>();
6660 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006661 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006662 __ and_(out_low, first_low, second_low);
6663 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006664 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006665 __ orr(out_low, first_low, second_low);
6666 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006667 } else {
6668 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006669 __ eor(out_low, first_low, second_low);
6670 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006671 }
6672 }
6673}
6674
Roland Levillainc9285912015-12-18 10:38:42 +00006675void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6676 Location out,
6677 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006678 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006679 Register out_reg = out.AsRegister<Register>();
6680 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006681 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006682 if (kUseBakerReadBarrier) {
6683 // Load with fast path based Baker's read barrier.
6684 // /* HeapReference<Object> */ out = *(out + offset)
6685 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006686 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006687 } else {
6688 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006689 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006690 // in the following move operation, as we will need it for the
6691 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006692 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006693 // /* HeapReference<Object> */ out = *(out + offset)
6694 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006695 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006696 }
6697 } else {
6698 // Plain load with no read barrier.
6699 // /* HeapReference<Object> */ out = *(out + offset)
6700 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6701 __ MaybeUnpoisonHeapReference(out_reg);
6702 }
6703}
6704
6705void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6706 Location out,
6707 Location obj,
6708 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006709 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006710 Register out_reg = out.AsRegister<Register>();
6711 Register obj_reg = obj.AsRegister<Register>();
6712 if (kEmitCompilerReadBarrier) {
6713 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006714 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006715 // Load with fast path based Baker's read barrier.
6716 // /* HeapReference<Object> */ out = *(obj + offset)
6717 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006718 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006719 } else {
6720 // Load with slow path based read barrier.
6721 // /* HeapReference<Object> */ out = *(obj + offset)
6722 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6723 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6724 }
6725 } else {
6726 // Plain load with no read barrier.
6727 // /* HeapReference<Object> */ out = *(obj + offset)
6728 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6729 __ MaybeUnpoisonHeapReference(out_reg);
6730 }
6731}
6732
6733void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6734 Location root,
6735 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006736 uint32_t offset,
6737 bool requires_read_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006738 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006739 if (requires_read_barrier) {
6740 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006741 if (kUseBakerReadBarrier) {
6742 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6743 // Baker's read barrier are used:
6744 //
6745 // root = obj.field;
6746 // if (Thread::Current()->GetIsGcMarking()) {
6747 // root = ReadBarrier::Mark(root)
6748 // }
6749
6750 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6751 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6752 static_assert(
6753 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6754 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6755 "have different sizes.");
6756 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6757 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6758 "have different sizes.");
6759
Vladimir Marko953437b2016-08-24 08:30:46 +00006760 // Slow path marking the GC root `root`.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006761 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006762 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006763 codegen_->AddSlowPath(slow_path);
6764
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006765 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006766 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006767 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006768 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6769 __ Bind(slow_path->GetExitLabel());
6770 } else {
6771 // GC root loaded through a slow path for read barriers other
6772 // than Baker's.
6773 // /* GcRoot<mirror::Object>* */ root = obj + offset
6774 __ AddConstant(root_reg, obj, offset);
6775 // /* mirror::Object* */ root = root->Read()
6776 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6777 }
6778 } else {
6779 // Plain GC root load with no read barrier.
6780 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6781 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6782 // Note that GC roots are not affected by heap poisoning, thus we
6783 // do not have to unpoison `root_reg` here.
6784 }
6785}
6786
6787void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6788 Location ref,
6789 Register obj,
6790 uint32_t offset,
6791 Location temp,
6792 bool needs_null_check) {
6793 DCHECK(kEmitCompilerReadBarrier);
6794 DCHECK(kUseBakerReadBarrier);
6795
6796 // /* HeapReference<Object> */ ref = *(obj + offset)
6797 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006798 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006799 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006800 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006801}
6802
6803void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6804 Location ref,
6805 Register obj,
6806 uint32_t data_offset,
6807 Location index,
6808 Location temp,
6809 bool needs_null_check) {
6810 DCHECK(kEmitCompilerReadBarrier);
6811 DCHECK(kUseBakerReadBarrier);
6812
Roland Levillainbfea3352016-06-23 13:48:47 +01006813 static_assert(
6814 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6815 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006816 // /* HeapReference<Object> */ ref =
6817 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006818 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006819 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006820 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006821}
6822
6823void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6824 Location ref,
6825 Register obj,
6826 uint32_t offset,
6827 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006828 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006829 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006830 bool needs_null_check,
6831 bool always_update_field,
6832 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00006833 DCHECK(kEmitCompilerReadBarrier);
6834 DCHECK(kUseBakerReadBarrier);
6835
6836 // In slow path based read barriers, the read barrier call is
6837 // inserted after the original load. However, in fast path based
6838 // Baker's read barriers, we need to perform the load of
6839 // mirror::Object::monitor_ *before* the original reference load.
6840 // This load-load ordering is required by the read barrier.
6841 // The fast path/slow path (for Baker's algorithm) should look like:
6842 //
6843 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6844 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6845 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006846 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillainc9285912015-12-18 10:38:42 +00006847 // if (is_gray) {
6848 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6849 // }
6850 //
6851 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006852 // slightly more complex as it performs additional checks that we do
6853 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006854
6855 Register ref_reg = ref.AsRegister<Register>();
6856 Register temp_reg = temp.AsRegister<Register>();
6857 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6858
6859 // /* int32_t */ monitor = obj->monitor_
6860 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6861 if (needs_null_check) {
6862 MaybeRecordImplicitNullCheck(instruction);
6863 }
6864 // /* LockWord */ lock_word = LockWord(monitor)
6865 static_assert(sizeof(LockWord) == sizeof(int32_t),
6866 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006867
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006868 // Introduce a dependency on the lock_word including the rb_state,
6869 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006870 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006871 // `obj` is unchanged by this operation, but its value now depends
6872 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006873 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006874
6875 // The actual reference load.
6876 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006877 // Load types involving an "index": ArrayGet,
6878 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6879 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006880 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006881 if (index.IsConstant()) {
6882 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006883 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006884 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6885 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006886 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006887 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6888 // intrinsics, which use a register pair as index ("long
6889 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01006890 Register index_reg = index.IsRegisterPair()
6891 ? index.AsRegisterPairLow<Register>()
6892 : index.AsRegister<Register>();
6893 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006894 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6895 }
6896 } else {
6897 // /* HeapReference<Object> */ ref = *(obj + offset)
6898 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6899 }
6900
6901 // Object* ref = ref_addr->AsMirrorPtr()
6902 __ MaybeUnpoisonHeapReference(ref_reg);
6903
Vladimir Marko953437b2016-08-24 08:30:46 +00006904 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006905 SlowPathCodeARM* slow_path;
6906 if (always_update_field) {
6907 DCHECK(temp2 != nullptr);
6908 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
6909 // of the form `obj + field_offset`, where `obj` is a register and
6910 // `field_offset` is a register pair (of which only the lower half
6911 // is used). Thus `offset` and `scale_factor` above are expected
6912 // to be null in this code path.
6913 DCHECK_EQ(offset, 0u);
6914 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6915 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
6916 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
6917 } else {
6918 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
6919 }
Roland Levillainc9285912015-12-18 10:38:42 +00006920 AddSlowPath(slow_path);
6921
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006922 // if (rb_state == ReadBarrier::GrayState())
Roland Levillainc9285912015-12-18 10:38:42 +00006923 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006924 // Given the numeric representation, it's enough to check the low bit of the
6925 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6926 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006927 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6928 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006929 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6930 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006931 __ Bind(slow_path->GetExitLabel());
6932}
6933
6934void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6935 Location out,
6936 Location ref,
6937 Location obj,
6938 uint32_t offset,
6939 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006940 DCHECK(kEmitCompilerReadBarrier);
6941
Roland Levillainc9285912015-12-18 10:38:42 +00006942 // Insert a slow path based read barrier *after* the reference load.
6943 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006944 // If heap poisoning is enabled, the unpoisoning of the loaded
6945 // reference will be carried out by the runtime within the slow
6946 // path.
6947 //
6948 // Note that `ref` currently does not get unpoisoned (when heap
6949 // poisoning is enabled), which is alright as the `ref` argument is
6950 // not used by the artReadBarrierSlow entry point.
6951 //
6952 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006953 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00006954 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6955 AddSlowPath(slow_path);
6956
Roland Levillain3b359c72015-11-17 19:35:12 +00006957 __ b(slow_path->GetEntryLabel());
6958 __ Bind(slow_path->GetExitLabel());
6959}
6960
Roland Levillainc9285912015-12-18 10:38:42 +00006961void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6962 Location out,
6963 Location ref,
6964 Location obj,
6965 uint32_t offset,
6966 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006967 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006968 // Baker's read barriers shall be handled by the fast path
6969 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6970 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006971 // If heap poisoning is enabled, unpoisoning will be taken care of
6972 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006973 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006974 } else if (kPoisonHeapReferences) {
6975 __ UnpoisonHeapReference(out.AsRegister<Register>());
6976 }
6977}
6978
Roland Levillainc9285912015-12-18 10:38:42 +00006979void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6980 Location out,
6981 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006982 DCHECK(kEmitCompilerReadBarrier);
6983
Roland Levillainc9285912015-12-18 10:38:42 +00006984 // Insert a slow path based read barrier *after* the GC root load.
6985 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006986 // Note that GC roots are not affected by heap poisoning, so we do
6987 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006988 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006989 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6990 AddSlowPath(slow_path);
6991
Roland Levillain3b359c72015-11-17 19:35:12 +00006992 __ b(slow_path->GetEntryLabel());
6993 __ Bind(slow_path->GetExitLabel());
6994}
6995
Vladimir Markodc151b22015-10-15 18:02:30 +01006996HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6997 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01006998 HInvokeStaticOrDirect* invoke) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006999 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
7000 // We disable pc-relative load when there is an irreducible loop, as the optimization
7001 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007002 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
7003 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007004 if (GetGraph()->HasIrreducibleLoops() &&
7005 (dispatch_info.method_load_kind ==
7006 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
7007 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
7008 }
7009
7010 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01007011 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007012 if (&outer_dex_file != invoke->GetTargetMethod().dex_file) {
Vladimir Markodc151b22015-10-15 18:02:30 +01007013 // Calls across dex files are more likely to exceed the available BL range,
7014 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
7015 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
7016 (desired_dispatch_info.method_load_kind ==
7017 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
7018 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
7019 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
7020 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007021 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01007022 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007023 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01007024 0u
7025 };
7026 }
7027 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007028 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007029}
7030
Vladimir Markob4536b72015-11-24 13:45:23 +00007031Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7032 Register temp) {
7033 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7034 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7035 if (!invoke->GetLocations()->Intrinsified()) {
7036 return location.AsRegister<Register>();
7037 }
7038 // For intrinsics we allow any location, so it may be on the stack.
7039 if (!location.IsRegister()) {
7040 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7041 return temp;
7042 }
7043 // For register locations, check if the register was saved. If so, get it from the stack.
7044 // Note: There is a chance that the register was saved but not overwritten, so we could
7045 // save one load. However, since this is just an intrinsic slow path we prefer this
7046 // simple and more robust approach rather that trying to determine if that's the case.
7047 SlowPathCode* slow_path = GetCurrentSlowPath();
7048 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7049 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7050 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7051 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7052 return temp;
7053 }
7054 return location.AsRegister<Register>();
7055}
7056
Nicolas Geoffray38207af2015-06-01 15:46:22 +01007057void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007058 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00007059 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00007060 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7061 // LR = code address from literal pool with link-time patch.
7062 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00007063 break;
7064 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7065 // LR = invoke->GetDirectCodePtr();
7066 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00007067 break;
7068 default:
7069 break;
7070 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007071
Vladimir Marko58155012015-08-19 12:49:41 +00007072 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7073 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007074 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7075 uint32_t offset =
7076 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007077 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007078 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007079 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007080 }
Vladimir Marko58155012015-08-19 12:49:41 +00007081 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007082 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007083 break;
7084 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7085 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7086 break;
7087 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
7088 __ LoadLiteral(temp.AsRegister<Register>(),
7089 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
7090 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007091 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7092 HArmDexCacheArraysBase* base =
7093 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7094 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7095 temp.AsRegister<Register>());
7096 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7097 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7098 break;
7099 }
Vladimir Marko58155012015-08-19 12:49:41 +00007100 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007101 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007102 Register method_reg;
7103 Register reg = temp.AsRegister<Register>();
7104 if (current_method.IsRegister()) {
7105 method_reg = current_method.AsRegister<Register>();
7106 } else {
7107 DCHECK(invoke->GetLocations()->Intrinsified());
7108 DCHECK(!current_method.IsValid());
7109 method_reg = reg;
7110 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7111 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007112 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7113 __ LoadFromOffset(kLoadWord,
7114 reg,
7115 method_reg,
7116 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007117 // temp = temp[index_in_cache];
7118 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7119 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007120 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7121 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007122 }
Vladimir Marko58155012015-08-19 12:49:41 +00007123 }
7124
7125 switch (invoke->GetCodePtrLocation()) {
7126 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7127 __ bl(GetFrameEntryLabel());
7128 break;
7129 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007130 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
7131 invoke->GetTargetMethod().dex_method_index);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007132 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01007133 // Arbitrarily branch to the BL itself, override at link time.
7134 __ bl(&relative_call_patches_.back().label);
7135 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007136 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7137 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7138 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00007139 // LR()
7140 __ blx(LR);
7141 break;
7142 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7143 // LR = callee_method->entry_point_from_quick_compiled_code_
7144 __ LoadFromOffset(
7145 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007146 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007147 // LR()
7148 __ blx(LR);
7149 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007150 }
7151
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007152 DCHECK(!IsLeafMethod());
7153}
7154
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007155void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7156 Register temp = temp_location.AsRegister<Register>();
7157 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7158 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007159
7160 // Use the calling convention instead of the location of the receiver, as
7161 // intrinsics may have put the receiver in a different register. In the intrinsics
7162 // slow path, the arguments have been moved to the right place, so here we are
7163 // guaranteed that the receiver is the first register of the calling convention.
7164 InvokeDexCallingConvention calling_convention;
7165 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007166 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007167 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007168 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007169 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007170 // Instead of simply (possibly) unpoisoning `temp` here, we should
7171 // emit a read barrier for the previous class reference load.
7172 // However this is not required in practice, as this is an
7173 // intermediate/temporary reference and because the current
7174 // concurrent copying collector keeps the from-space memory
7175 // intact/accessible until the end of the marking phase (the
7176 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007177 __ MaybeUnpoisonHeapReference(temp);
7178 // temp = temp->GetMethodAt(method_offset);
7179 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007180 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007181 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7182 // LR = temp->GetEntryPoint();
7183 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7184 // LR();
7185 __ blx(LR);
7186}
7187
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007188CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
7189 const DexFile& dex_file, uint32_t string_index) {
7190 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
7191}
7192
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007193CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
7194 const DexFile& dex_file, uint32_t type_index) {
7195 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
7196}
7197
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007198CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7199 const DexFile& dex_file, uint32_t element_offset) {
7200 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7201}
7202
7203CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7204 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7205 patches->emplace_back(dex_file, offset_or_index);
7206 return &patches->back();
7207}
7208
7209Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
7210 uint32_t string_index) {
7211 return boot_image_string_patches_.GetOrCreate(
7212 StringReference(&dex_file, string_index),
7213 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7214}
7215
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007216Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
7217 uint32_t type_index) {
7218 return boot_image_type_patches_.GetOrCreate(
7219 TypeReference(&dex_file, type_index),
7220 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7221}
7222
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007223Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7224 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7225 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7226 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7227}
7228
7229Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
7230 return DeduplicateUint32Literal(address, &uint32_literals_);
7231}
7232
Vladimir Markoaad75c62016-10-03 08:46:48 +00007233template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7234inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7235 const ArenaDeque<PcRelativePatchInfo>& infos,
7236 ArenaVector<LinkerPatch>* linker_patches) {
7237 for (const PcRelativePatchInfo& info : infos) {
7238 const DexFile& dex_file = info.target_dex_file;
7239 size_t offset_or_index = info.offset_or_index;
7240 DCHECK(info.add_pc_label.IsBound());
7241 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7242 // Add MOVW patch.
7243 DCHECK(info.movw_label.IsBound());
7244 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7245 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7246 // Add MOVT patch.
7247 DCHECK(info.movt_label.IsBound());
7248 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7249 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7250 }
7251}
7252
Vladimir Marko58155012015-08-19 12:49:41 +00007253void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7254 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007255 size_t size =
7256 method_patches_.size() +
7257 call_patches_.size() +
7258 relative_call_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007259 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007260 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007261 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007262 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007263 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007264 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007265 linker_patches->reserve(size);
7266 for (const auto& entry : method_patches_) {
7267 const MethodReference& target_method = entry.first;
7268 Literal* literal = entry.second;
7269 DCHECK(literal->GetLabel()->IsBound());
7270 uint32_t literal_offset = literal->GetLabel()->Position();
7271 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
7272 target_method.dex_file,
7273 target_method.dex_method_index));
7274 }
7275 for (const auto& entry : call_patches_) {
7276 const MethodReference& target_method = entry.first;
7277 Literal* literal = entry.second;
7278 DCHECK(literal->GetLabel()->IsBound());
7279 uint32_t literal_offset = literal->GetLabel()->Position();
7280 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
7281 target_method.dex_file,
7282 target_method.dex_method_index));
7283 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007284 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko58155012015-08-19 12:49:41 +00007285 uint32_t literal_offset = info.label.Position();
Vladimir Markoaad75c62016-10-03 08:46:48 +00007286 linker_patches->push_back(
7287 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00007288 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007289 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7290 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007291 for (const auto& entry : boot_image_string_patches_) {
7292 const StringReference& target_string = entry.first;
7293 Literal* literal = entry.second;
7294 DCHECK(literal->GetLabel()->IsBound());
7295 uint32_t literal_offset = literal->GetLabel()->Position();
7296 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7297 target_string.dex_file,
7298 target_string.string_index));
7299 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007300 if (!GetCompilerOptions().IsBootImage()) {
7301 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7302 linker_patches);
7303 } else {
7304 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7305 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007306 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007307 for (const auto& entry : boot_image_type_patches_) {
7308 const TypeReference& target_type = entry.first;
7309 Literal* literal = entry.second;
7310 DCHECK(literal->GetLabel()->IsBound());
7311 uint32_t literal_offset = literal->GetLabel()->Position();
7312 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7313 target_type.dex_file,
7314 target_type.type_index));
7315 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007316 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7317 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007318 for (const auto& entry : boot_image_address_patches_) {
7319 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7320 Literal* literal = entry.second;
7321 DCHECK(literal->GetLabel()->IsBound());
7322 uint32_t literal_offset = literal->GetLabel()->Position();
7323 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7324 }
7325}
7326
7327Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7328 return map->GetOrCreate(
7329 value,
7330 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007331}
7332
7333Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7334 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007335 return map->GetOrCreate(
7336 target_method,
7337 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007338}
7339
7340Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
7341 return DeduplicateMethodLiteral(target_method, &method_patches_);
7342}
7343
7344Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
7345 return DeduplicateMethodLiteral(target_method, &call_patches_);
7346}
7347
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007348void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7349 LocationSummary* locations =
7350 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7351 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7352 Location::RequiresRegister());
7353 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7354 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7355 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7356}
7357
7358void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7359 LocationSummary* locations = instr->GetLocations();
7360 Register res = locations->Out().AsRegister<Register>();
7361 Register accumulator =
7362 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7363 Register mul_left =
7364 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7365 Register mul_right =
7366 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7367
7368 if (instr->GetOpKind() == HInstruction::kAdd) {
7369 __ mla(res, mul_left, mul_right, accumulator);
7370 } else {
7371 __ mls(res, mul_left, mul_right, accumulator);
7372 }
7373}
7374
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007375void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007376 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007377 LOG(FATAL) << "Unreachable";
7378}
7379
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007380void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007381 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007382 LOG(FATAL) << "Unreachable";
7383}
7384
Mark Mendellfe57faa2015-09-18 09:26:15 -04007385// Simple implementation of packed switch - generate cascaded compare/jumps.
7386void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7387 LocationSummary* locations =
7388 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7389 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007390 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007391 codegen_->GetAssembler()->IsThumb()) {
7392 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7393 if (switch_instr->GetStartValue() != 0) {
7394 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7395 }
7396 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007397}
7398
7399void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7400 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007401 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007402 LocationSummary* locations = switch_instr->GetLocations();
7403 Register value_reg = locations->InAt(0).AsRegister<Register>();
7404 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7405
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007406 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007407 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007408 Register temp_reg = IP;
7409 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7410 // the immediate, because IP is used as the destination register. For the other
7411 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7412 // and they can be encoded in the instruction without making use of IP register.
7413 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7414
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007415 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007416 // Jump to successors[0] if value == lower_bound.
7417 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7418 int32_t last_index = 0;
7419 for (; num_entries - last_index > 2; last_index += 2) {
7420 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7421 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7422 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7423 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7424 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7425 }
7426 if (num_entries - last_index == 2) {
7427 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007428 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007429 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007430 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007431
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007432 // And the default for any other value.
7433 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7434 __ b(codegen_->GetLabelOf(default_block));
7435 }
7436 } else {
7437 // Create a table lookup.
7438 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7439
7440 // Materialize a pointer to the switch table
7441 std::vector<Label*> labels(num_entries);
7442 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7443 for (uint32_t i = 0; i < num_entries; i++) {
7444 labels[i] = codegen_->GetLabelOf(successors[i]);
7445 }
7446 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7447
7448 // Remove the bias.
7449 Register key_reg;
7450 if (lower_bound != 0) {
7451 key_reg = locations->GetTemp(1).AsRegister<Register>();
7452 __ AddConstant(key_reg, value_reg, -lower_bound);
7453 } else {
7454 key_reg = value_reg;
7455 }
7456
7457 // Check whether the value is in the table, jump to default block if not.
7458 __ CmpConstant(key_reg, num_entries - 1);
7459 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7460
7461 // Load the displacement from the table.
7462 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7463
7464 // Dispatch is a direct add to the PC (for Thumb2).
7465 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007466 }
7467}
7468
Vladimir Markob4536b72015-11-24 13:45:23 +00007469void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7470 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7471 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007472}
7473
7474void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7475 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007476 CodeGeneratorARM::PcRelativePatchInfo* labels =
7477 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007478 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007479 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007480 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007481 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007482 __ BindTrackedLabel(&labels->add_pc_label);
7483 __ add(base_reg, base_reg, ShifterOperand(PC));
7484}
7485
Andreas Gampe85b62f22015-09-09 13:15:38 -07007486void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7487 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007488 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007489 return;
7490 }
7491
7492 DCHECK_NE(type, Primitive::kPrimVoid);
7493
7494 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7495 if (return_loc.Equals(trg)) {
7496 return;
7497 }
7498
7499 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7500 // with the last branch.
7501 if (type == Primitive::kPrimLong) {
7502 HParallelMove parallel_move(GetGraph()->GetArena());
7503 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7504 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7505 GetMoveResolver()->EmitNativeCode(&parallel_move);
7506 } else if (type == Primitive::kPrimDouble) {
7507 HParallelMove parallel_move(GetGraph()->GetArena());
7508 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7509 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7510 GetMoveResolver()->EmitNativeCode(&parallel_move);
7511 } else {
7512 // Let the parallel move resolver take care of all of this.
7513 HParallelMove parallel_move(GetGraph()->GetArena());
7514 parallel_move.AddMove(return_loc, trg, type, nullptr);
7515 GetMoveResolver()->EmitNativeCode(&parallel_move);
7516 }
7517}
7518
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007519void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7520 LocationSummary* locations =
7521 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7522 locations->SetInAt(0, Location::RequiresRegister());
7523 locations->SetOut(Location::RequiresRegister());
7524}
7525
7526void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7527 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007528 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007529 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007530 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007531 __ LoadFromOffset(kLoadWord,
7532 locations->Out().AsRegister<Register>(),
7533 locations->InAt(0).AsRegister<Register>(),
7534 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007535 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007536 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007537 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007538 __ LoadFromOffset(kLoadWord,
7539 locations->Out().AsRegister<Register>(),
7540 locations->InAt(0).AsRegister<Register>(),
7541 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7542 __ LoadFromOffset(kLoadWord,
7543 locations->Out().AsRegister<Register>(),
7544 locations->Out().AsRegister<Register>(),
7545 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007546 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007547}
7548
Roland Levillain4d027112015-07-01 15:41:14 +01007549#undef __
7550#undef QUICK_ENTRY_POINT
7551
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007552} // namespace arm
7553} // namespace art