blob: f6893e721a1375442af7e0fc163ccb252ba34f7a [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();
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800492 Location arg0, arg1;
493 if (instruction_->IsInstanceOf()) {
494 arg0 = locations->InAt(1);
495 arg1 = locations->Out();
496 } else {
497 arg0 = locations->InAt(0);
498 arg1 = locations->InAt(1);
499 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000500 DCHECK(instruction_->IsCheckCast()
501 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000502
503 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
504 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000505
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000506 if (!is_fatal_) {
507 SaveLiveRegisters(codegen, locations);
508 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000509
510 // We're moving two locations to locations that could overlap, so we need a parallel
511 // move resolver.
512 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800513 codegen->EmitParallelMoves(arg0,
514 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
515 Primitive::kPrimNot,
516 arg1,
517 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
518 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000519 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100520 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100521 instruction_,
522 instruction_->GetDexPc(),
523 this);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800524 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Class*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000525 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
526 } else {
527 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800528 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
529 instruction_,
530 instruction_->GetDexPc(),
531 this);
532 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000533 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000534
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000535 if (!is_fatal_) {
536 RestoreLiveRegisters(codegen, locations);
537 __ b(GetExitLabel());
538 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000539 }
540
Alexandre Rames9931f312015-06-19 14:47:01 +0100541 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
542
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000543 bool IsFatal() const OVERRIDE { return is_fatal_; }
544
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000545 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000546 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000547
548 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
549};
550
Artem Serovf4d6aee2016-07-11 10:41:45 +0100551class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700552 public:
Aart Bik42249c32016-01-07 15:33:50 -0800553 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100554 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700555
556 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800557 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700558 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100559 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000560 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700561 }
562
Alexandre Rames9931f312015-06-19 14:47:01 +0100563 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
564
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700565 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700566 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
567};
568
Artem Serovf4d6aee2016-07-11 10:41:45 +0100569class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100570 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100571 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100572
573 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
574 LocationSummary* locations = instruction_->GetLocations();
575 __ Bind(GetEntryLabel());
576 SaveLiveRegisters(codegen, locations);
577
578 InvokeRuntimeCallingConvention calling_convention;
579 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
580 parallel_move.AddMove(
581 locations->InAt(0),
582 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
583 Primitive::kPrimNot,
584 nullptr);
585 parallel_move.AddMove(
586 locations->InAt(1),
587 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
588 Primitive::kPrimInt,
589 nullptr);
590 parallel_move.AddMove(
591 locations->InAt(2),
592 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
593 Primitive::kPrimNot,
594 nullptr);
595 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
596
597 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100598 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000599 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100600 RestoreLiveRegisters(codegen, locations);
601 __ b(GetExitLabel());
602 }
603
604 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
605
606 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100607 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
608};
609
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100610// Slow path marking an object reference `ref` during a read
611// barrier. The field `obj.field` in the object `obj` holding this
612// reference does not get updated by this slow path after marking (see
613// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
614//
615// This means that after the execution of this slow path, `ref` will
616// always be up-to-date, but `obj.field` may not; i.e., after the
617// flip, `ref` will be a to-space reference, but `obj.field` will
618// probably still be a from-space reference (unless it gets updated by
619// another thread, or if another thread installed another object
620// reference (different from `ref`) in `obj.field`).
Artem Serovf4d6aee2016-07-11 10:41:45 +0100621class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000622 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100623 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location ref)
624 : SlowPathCodeARM(instruction), ref_(ref) {
Roland Levillainc9285912015-12-18 10:38:42 +0000625 DCHECK(kEmitCompilerReadBarrier);
626 }
627
628 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
629
630 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
631 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100632 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000633 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100634 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillainc9285912015-12-18 10:38:42 +0000635 DCHECK(instruction_->IsInstanceFieldGet() ||
636 instruction_->IsStaticFieldGet() ||
637 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100638 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000639 instruction_->IsLoadClass() ||
640 instruction_->IsLoadString() ||
641 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100642 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100643 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
644 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000645 << "Unexpected instruction in read barrier marking slow path: "
646 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000647 // The read barrier instrumentation of object ArrayGet
648 // instructions does not support the HIntermediateAddress
649 // instruction.
650 DCHECK(!(instruction_->IsArrayGet() &&
651 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillainc9285912015-12-18 10:38:42 +0000652
653 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100654 // No need to save live registers; it's taken care of by the
655 // entrypoint. Also, there is no need to update the stack mask,
656 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000657 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100658 DCHECK_NE(ref_reg, SP);
659 DCHECK_NE(ref_reg, LR);
660 DCHECK_NE(ref_reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100661 // IP is used internally by the ReadBarrierMarkRegX entry point
662 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100663 DCHECK_NE(ref_reg, IP);
664 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100665 // "Compact" slow path, saving two moves.
666 //
667 // Instead of using the standard runtime calling convention (input
668 // and output in R0):
669 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100670 // R0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100671 // R0 <- ReadBarrierMark(R0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100672 // ref <- R0
Roland Levillain02b75802016-07-13 11:54:35 +0100673 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100674 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100675 // of a dedicated entrypoint:
676 //
677 // rX <- ReadBarrierMarkRegX(rX)
678 //
679 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100680 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100681 // This runtime call does not require a stack map.
682 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000683 __ b(GetExitLabel());
684 }
685
686 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100687 // The location (register) of the marked object reference.
688 const Location ref_;
Roland Levillainc9285912015-12-18 10:38:42 +0000689
690 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
691};
692
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100693// Slow path marking an object reference `ref` during a read barrier,
694// and if needed, atomically updating the field `obj.field` in the
695// object `obj` holding this reference after marking (contrary to
696// ReadBarrierMarkSlowPathARM above, which never tries to update
697// `obj.field`).
698//
699// This means that after the execution of this slow path, both `ref`
700// and `obj.field` will be up-to-date; i.e., after the flip, both will
701// hold the same to-space reference (unless another thread installed
702// another object reference (different from `ref`) in `obj.field`).
703class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
704 public:
705 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
706 Location ref,
707 Register obj,
708 Location field_offset,
709 Register temp1,
710 Register temp2)
711 : SlowPathCodeARM(instruction),
712 ref_(ref),
713 obj_(obj),
714 field_offset_(field_offset),
715 temp1_(temp1),
716 temp2_(temp2) {
717 DCHECK(kEmitCompilerReadBarrier);
718 }
719
720 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
721
722 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
723 LocationSummary* locations = instruction_->GetLocations();
724 Register ref_reg = ref_.AsRegister<Register>();
725 DCHECK(locations->CanCall());
726 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
727 // This slow path is only used by the UnsafeCASObject intrinsic.
728 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
729 << "Unexpected instruction in read barrier marking and field updating slow path: "
730 << instruction_->DebugName();
731 DCHECK(instruction_->GetLocations()->Intrinsified());
732 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
733 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
734
735 __ Bind(GetEntryLabel());
736
737 // Save the old reference.
738 // Note that we cannot use IP to save the old reference, as IP is
739 // used internally by the ReadBarrierMarkRegX entry point, and we
740 // need the old reference after the call to that entry point.
741 DCHECK_NE(temp1_, IP);
742 __ Mov(temp1_, ref_reg);
743
744 // No need to save live registers; it's taken care of by the
745 // entrypoint. Also, there is no need to update the stack mask,
746 // as this runtime call will not trigger a garbage collection.
747 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
748 DCHECK_NE(ref_reg, SP);
749 DCHECK_NE(ref_reg, LR);
750 DCHECK_NE(ref_reg, PC);
751 // IP is used internally by the ReadBarrierMarkRegX entry point
752 // as a temporary, it cannot be the entry point's input/output.
753 DCHECK_NE(ref_reg, IP);
754 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
755 // "Compact" slow path, saving two moves.
756 //
757 // Instead of using the standard runtime calling convention (input
758 // and output in R0):
759 //
760 // R0 <- ref
761 // R0 <- ReadBarrierMark(R0)
762 // ref <- R0
763 //
764 // we just use rX (the register containing `ref`) as input and output
765 // of a dedicated entrypoint:
766 //
767 // rX <- ReadBarrierMarkRegX(rX)
768 //
769 int32_t entry_point_offset =
770 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
771 // This runtime call does not require a stack map.
772 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
773
774 // If the new reference is different from the old reference,
775 // update the field in the holder (`*(obj_ + field_offset_)`).
776 //
777 // Note that this field could also hold a different object, if
778 // another thread had concurrently changed it. In that case, the
779 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
780 // (CAS) operation below would abort the CAS, leaving the field
781 // as-is.
782 Label done;
783 __ cmp(temp1_, ShifterOperand(ref_reg));
784 __ b(&done, EQ);
785
786 // Update the the holder's field atomically. This may fail if
787 // mutator updates before us, but it's OK. This is achieved
788 // using a strong compare-and-set (CAS) operation with relaxed
789 // memory synchronization ordering, where the expected value is
790 // the old reference and the desired value is the new reference.
791
792 // Convenience aliases.
793 Register base = obj_;
794 // The UnsafeCASObject intrinsic uses a register pair as field
795 // offset ("long offset"), of which only the low part contains
796 // data.
797 Register offset = field_offset_.AsRegisterPairLow<Register>();
798 Register expected = temp1_;
799 Register value = ref_reg;
800 Register tmp_ptr = IP; // Pointer to actual memory.
801 Register tmp = temp2_; // Value in memory.
802
803 __ add(tmp_ptr, base, ShifterOperand(offset));
804
805 if (kPoisonHeapReferences) {
806 __ PoisonHeapReference(expected);
807 if (value == expected) {
808 // Do not poison `value`, as it is the same register as
809 // `expected`, which has just been poisoned.
810 } else {
811 __ PoisonHeapReference(value);
812 }
813 }
814
815 // do {
816 // tmp = [r_ptr] - expected;
817 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
818
Roland Levillain24a4d112016-10-26 13:10:46 +0100819 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100820 __ Bind(&loop_head);
821
822 __ ldrex(tmp, tmp_ptr);
823
824 __ subs(tmp, tmp, ShifterOperand(expected));
825
Roland Levillain24a4d112016-10-26 13:10:46 +0100826 __ it(NE);
827 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100828
Roland Levillain24a4d112016-10-26 13:10:46 +0100829 __ b(&exit_loop, NE);
830
831 __ strex(tmp, value, tmp_ptr);
832 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100833 __ b(&loop_head, EQ);
834
Roland Levillain24a4d112016-10-26 13:10:46 +0100835 __ Bind(&exit_loop);
836
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100837 if (kPoisonHeapReferences) {
838 __ UnpoisonHeapReference(expected);
839 if (value == expected) {
840 // Do not unpoison `value`, as it is the same register as
841 // `expected`, which has just been unpoisoned.
842 } else {
843 __ UnpoisonHeapReference(value);
844 }
845 }
846
847 __ Bind(&done);
848 __ b(GetExitLabel());
849 }
850
851 private:
852 // The location (register) of the marked object reference.
853 const Location ref_;
854 // The register containing the object holding the marked object reference field.
855 const Register obj_;
856 // The location of the offset of the marked reference field within `obj_`.
857 Location field_offset_;
858
859 const Register temp1_;
860 const Register temp2_;
861
862 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
863};
864
Roland Levillain3b359c72015-11-17 19:35:12 +0000865// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100866class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000867 public:
868 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
869 Location out,
870 Location ref,
871 Location obj,
872 uint32_t offset,
873 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100874 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000875 out_(out),
876 ref_(ref),
877 obj_(obj),
878 offset_(offset),
879 index_(index) {
880 DCHECK(kEmitCompilerReadBarrier);
881 // If `obj` is equal to `out` or `ref`, it means the initial object
882 // has been overwritten by (or after) the heap object reference load
883 // to be instrumented, e.g.:
884 //
885 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000886 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000887 //
888 // In that case, we have lost the information about the original
889 // object, and the emitted read barrier cannot work properly.
890 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
891 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
892 }
893
894 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
895 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
896 LocationSummary* locations = instruction_->GetLocations();
897 Register reg_out = out_.AsRegister<Register>();
898 DCHECK(locations->CanCall());
899 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100900 DCHECK(instruction_->IsInstanceFieldGet() ||
901 instruction_->IsStaticFieldGet() ||
902 instruction_->IsArrayGet() ||
903 instruction_->IsInstanceOf() ||
904 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100905 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000906 << "Unexpected instruction in read barrier for heap reference slow path: "
907 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000908 // The read barrier instrumentation of object ArrayGet
909 // instructions does not support the HIntermediateAddress
910 // instruction.
911 DCHECK(!(instruction_->IsArrayGet() &&
912 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +0000913
914 __ Bind(GetEntryLabel());
915 SaveLiveRegisters(codegen, locations);
916
917 // We may have to change the index's value, but as `index_` is a
918 // constant member (like other "inputs" of this slow path),
919 // introduce a copy of it, `index`.
920 Location index = index_;
921 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100922 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000923 if (instruction_->IsArrayGet()) {
924 // Compute the actual memory offset and store it in `index`.
925 Register index_reg = index_.AsRegister<Register>();
926 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
927 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
928 // We are about to change the value of `index_reg` (see the
929 // calls to art::arm::Thumb2Assembler::Lsl and
930 // art::arm::Thumb2Assembler::AddConstant below), but it has
931 // not been saved by the previous call to
932 // art::SlowPathCode::SaveLiveRegisters, as it is a
933 // callee-save register --
934 // art::SlowPathCode::SaveLiveRegisters does not consider
935 // callee-save registers, as it has been designed with the
936 // assumption that callee-save registers are supposed to be
937 // handled by the called function. So, as a callee-save
938 // register, `index_reg` _would_ eventually be saved onto
939 // the stack, but it would be too late: we would have
940 // changed its value earlier. Therefore, we manually save
941 // it here into another freely available register,
942 // `free_reg`, chosen of course among the caller-save
943 // registers (as a callee-save `free_reg` register would
944 // exhibit the same problem).
945 //
946 // Note we could have requested a temporary register from
947 // the register allocator instead; but we prefer not to, as
948 // this is a slow path, and we know we can find a
949 // caller-save register that is available.
950 Register free_reg = FindAvailableCallerSaveRegister(codegen);
951 __ Mov(free_reg, index_reg);
952 index_reg = free_reg;
953 index = Location::RegisterLocation(index_reg);
954 } else {
955 // The initial register stored in `index_` has already been
956 // saved in the call to art::SlowPathCode::SaveLiveRegisters
957 // (as it is not a callee-save register), so we can freely
958 // use it.
959 }
960 // Shifting the index value contained in `index_reg` by the scale
961 // factor (2) cannot overflow in practice, as the runtime is
962 // unable to allocate object arrays with a size larger than
963 // 2^26 - 1 (that is, 2^28 - 4 bytes).
964 __ Lsl(index_reg, index_reg, TIMES_4);
965 static_assert(
966 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
967 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
968 __ AddConstant(index_reg, index_reg, offset_);
969 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100970 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
971 // intrinsics, `index_` is not shifted by a scale factor of 2
972 // (as in the case of ArrayGet), as it is actually an offset
973 // to an object field within an object.
974 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000975 DCHECK(instruction_->GetLocations()->Intrinsified());
976 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
977 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
978 << instruction_->AsInvoke()->GetIntrinsic();
979 DCHECK_EQ(offset_, 0U);
980 DCHECK(index_.IsRegisterPair());
981 // UnsafeGet's offset location is a register pair, the low
982 // part contains the correct offset.
983 index = index_.ToLow();
984 }
985 }
986
987 // We're moving two or three locations to locations that could
988 // overlap, so we need a parallel move resolver.
989 InvokeRuntimeCallingConvention calling_convention;
990 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
991 parallel_move.AddMove(ref_,
992 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
993 Primitive::kPrimNot,
994 nullptr);
995 parallel_move.AddMove(obj_,
996 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
997 Primitive::kPrimNot,
998 nullptr);
999 if (index.IsValid()) {
1000 parallel_move.AddMove(index,
1001 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1002 Primitive::kPrimInt,
1003 nullptr);
1004 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1005 } else {
1006 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1007 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1008 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001009 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001010 CheckEntrypointTypes<
1011 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1012 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1013
1014 RestoreLiveRegisters(codegen, locations);
1015 __ b(GetExitLabel());
1016 }
1017
1018 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1019
1020 private:
1021 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1022 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1023 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1024 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1025 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1026 return static_cast<Register>(i);
1027 }
1028 }
1029 // We shall never fail to find a free caller-save register, as
1030 // there are more than two core caller-save registers on ARM
1031 // (meaning it is possible to find one which is different from
1032 // `ref` and `obj`).
1033 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1034 LOG(FATAL) << "Could not find a free caller-save register";
1035 UNREACHABLE();
1036 }
1037
Roland Levillain3b359c72015-11-17 19:35:12 +00001038 const Location out_;
1039 const Location ref_;
1040 const Location obj_;
1041 const uint32_t offset_;
1042 // An additional location containing an index to an array.
1043 // Only used for HArrayGet and the UnsafeGetObject &
1044 // UnsafeGetObjectVolatile intrinsics.
1045 const Location index_;
1046
1047 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1048};
1049
1050// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001051class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001052 public:
1053 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001054 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001055 DCHECK(kEmitCompilerReadBarrier);
1056 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001057
1058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1059 LocationSummary* locations = instruction_->GetLocations();
1060 Register reg_out = out_.AsRegister<Register>();
1061 DCHECK(locations->CanCall());
1062 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001063 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1064 << "Unexpected instruction in read barrier for GC root slow path: "
1065 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001066
1067 __ Bind(GetEntryLabel());
1068 SaveLiveRegisters(codegen, locations);
1069
1070 InvokeRuntimeCallingConvention calling_convention;
1071 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1072 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001073 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001074 instruction_,
1075 instruction_->GetDexPc(),
1076 this);
1077 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1078 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1079
1080 RestoreLiveRegisters(codegen, locations);
1081 __ b(GetExitLabel());
1082 }
1083
1084 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1085
1086 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001087 const Location out_;
1088 const Location root_;
1089
1090 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1091};
1092
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001093#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001094// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1095#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -07001096
Aart Bike9f37602015-10-09 11:15:55 -07001097inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001098 switch (cond) {
1099 case kCondEQ: return EQ;
1100 case kCondNE: return NE;
1101 case kCondLT: return LT;
1102 case kCondLE: return LE;
1103 case kCondGT: return GT;
1104 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001105 case kCondB: return LO;
1106 case kCondBE: return LS;
1107 case kCondA: return HI;
1108 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001109 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001110 LOG(FATAL) << "Unreachable";
1111 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001112}
1113
Aart Bike9f37602015-10-09 11:15:55 -07001114// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001115inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001116 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001117 case kCondEQ: return EQ;
1118 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001119 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001120 case kCondLT: return LO;
1121 case kCondLE: return LS;
1122 case kCondGT: return HI;
1123 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001124 // Unsigned remain unchanged.
1125 case kCondB: return LO;
1126 case kCondBE: return LS;
1127 case kCondA: return HI;
1128 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001129 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001130 LOG(FATAL) << "Unreachable";
1131 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001132}
1133
Vladimir Markod6e069b2016-01-18 11:11:01 +00001134inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1135 // The ARM condition codes can express all the necessary branches, see the
1136 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1137 // There is no dex instruction or HIR that would need the missing conditions
1138 // "equal or unordered" or "not equal".
1139 switch (cond) {
1140 case kCondEQ: return EQ;
1141 case kCondNE: return NE /* unordered */;
1142 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1143 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1144 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1145 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1146 default:
1147 LOG(FATAL) << "UNREACHABLE";
1148 UNREACHABLE();
1149 }
1150}
1151
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001152void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001153 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001154}
1155
1156void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001157 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001158}
1159
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001160size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1161 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1162 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001163}
1164
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001165size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1166 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1167 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001168}
1169
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001170size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1171 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1172 return kArmWordSize;
1173}
1174
1175size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1176 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1177 return kArmWordSize;
1178}
1179
Calin Juravle34166012014-12-19 17:22:29 +00001180CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001181 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001182 const CompilerOptions& compiler_options,
1183 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001184 : CodeGenerator(graph,
1185 kNumberOfCoreRegisters,
1186 kNumberOfSRegisters,
1187 kNumberOfRegisterPairs,
1188 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1189 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001190 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1191 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001192 compiler_options,
1193 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001194 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001195 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001196 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001197 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001198 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001199 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001200 uint32_literals_(std::less<uint32_t>(),
1201 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001202 method_patches_(MethodReferenceComparator(),
1203 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1204 call_patches_(MethodReferenceComparator(),
1205 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +00001206 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001207 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1208 boot_image_string_patches_(StringReferenceValueComparator(),
1209 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1210 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001211 boot_image_type_patches_(TypeReferenceValueComparator(),
1212 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1213 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001214 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray997d1212016-11-09 10:36:29 +00001215 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1216 jit_string_patches_(StringReferenceValueComparator(),
1217 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001218 // Always save the LR register to mimic Quick.
1219 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001220}
1221
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001222void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1223 // Ensure that we fix up branches and literal loads and emit the literal pool.
1224 __ FinalizeCode();
1225
1226 // Adjust native pc offsets in stack maps.
1227 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
1228 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
1229 uint32_t new_position = __ GetAdjustedPosition(old_position);
1230 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1231 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001232 // Adjust pc offsets for the disassembly information.
1233 if (disasm_info_ != nullptr) {
1234 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1235 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1236 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1237 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1238 it.second.start = __ GetAdjustedPosition(it.second.start);
1239 it.second.end = __ GetAdjustedPosition(it.second.end);
1240 }
1241 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1242 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1243 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1244 }
1245 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001246
1247 CodeGenerator::Finalize(allocator);
1248}
1249
David Brazdil58282f42016-01-14 12:45:10 +00001250void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001251 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001252 blocked_core_registers_[SP] = true;
1253 blocked_core_registers_[LR] = true;
1254 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001255
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001256 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001257 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001258
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001259 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001260 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001261
David Brazdil58282f42016-01-14 12:45:10 +00001262 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001263 // Stubs do not save callee-save floating point registers. If the graph
1264 // is debuggable, we need to deal with these registers differently. For
1265 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001266 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1267 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1268 }
1269 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001270}
1271
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001272InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001273 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001274 assembler_(codegen->GetAssembler()),
1275 codegen_(codegen) {}
1276
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001277void CodeGeneratorARM::ComputeSpillMask() {
1278 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1279 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001280 // There is no easy instruction to restore just the PC on thumb2. We spill and
1281 // restore another arbitrary register.
1282 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001283 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1284 // We use vpush and vpop for saving and restoring floating point registers, which take
1285 // a SRegister and the number of registers to save/restore after that SRegister. We
1286 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1287 // but in the range.
1288 if (fpu_spill_mask_ != 0) {
1289 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1290 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1291 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1292 fpu_spill_mask_ |= (1 << i);
1293 }
1294 }
1295}
1296
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001297static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001298 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001299}
1300
1301static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001302 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001303}
1304
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001305void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001306 bool skip_overflow_check =
1307 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001308 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001309 __ Bind(&frame_entry_label_);
1310
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001311 if (HasEmptyFrame()) {
1312 return;
1313 }
1314
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001315 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001316 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1317 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1318 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001319 }
1320
Andreas Gampe501fd632015-09-10 16:11:06 -07001321 __ PushList(core_spill_mask_);
1322 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1323 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001324 if (fpu_spill_mask_ != 0) {
1325 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1326 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001327 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001328 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001329 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001330 int adjust = GetFrameSize() - FrameEntrySpillSize();
1331 __ AddConstant(SP, -adjust);
1332 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001333
1334 // Save the current method if we need it. Note that we do not
1335 // do this in HCurrentMethod, as the instruction might have been removed
1336 // in the SSA graph.
1337 if (RequiresCurrentMethod()) {
1338 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1339 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001340}
1341
1342void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001343 if (HasEmptyFrame()) {
1344 __ bx(LR);
1345 return;
1346 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001347 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001348 int adjust = GetFrameSize() - FrameEntrySpillSize();
1349 __ AddConstant(SP, adjust);
1350 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001351 if (fpu_spill_mask_ != 0) {
1352 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1353 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001354 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001355 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001356 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001357 // Pop LR into PC to return.
1358 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1359 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1360 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001361 __ cfi().RestoreState();
1362 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001363}
1364
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001365void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001366 Label* label = GetLabelOf(block);
1367 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001368}
1369
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001370Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001371 switch (type) {
1372 case Primitive::kPrimBoolean:
1373 case Primitive::kPrimByte:
1374 case Primitive::kPrimChar:
1375 case Primitive::kPrimShort:
1376 case Primitive::kPrimInt:
1377 case Primitive::kPrimNot: {
1378 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001379 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001380 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001381 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001382 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001383 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001384 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001385 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001386
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001387 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001388 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001389 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001390 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001391 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001392 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001393 if (calling_convention.GetRegisterAt(index) == R1) {
1394 // Skip R1, and use R2_R3 instead.
1395 gp_index_++;
1396 index++;
1397 }
1398 }
1399 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1400 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001401 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001402
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001403 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001404 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001405 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001406 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1407 }
1408 }
1409
1410 case Primitive::kPrimFloat: {
1411 uint32_t stack_index = stack_index_++;
1412 if (float_index_ % 2 == 0) {
1413 float_index_ = std::max(double_index_, float_index_);
1414 }
1415 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1416 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1417 } else {
1418 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1419 }
1420 }
1421
1422 case Primitive::kPrimDouble: {
1423 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1424 uint32_t stack_index = stack_index_;
1425 stack_index_ += 2;
1426 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1427 uint32_t index = double_index_;
1428 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001429 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001430 calling_convention.GetFpuRegisterAt(index),
1431 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001432 DCHECK(ExpectedPairLayout(result));
1433 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001434 } else {
1435 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001436 }
1437 }
1438
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001439 case Primitive::kPrimVoid:
1440 LOG(FATAL) << "Unexpected parameter type " << type;
1441 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001442 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001443 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001444}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001445
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001446Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001447 switch (type) {
1448 case Primitive::kPrimBoolean:
1449 case Primitive::kPrimByte:
1450 case Primitive::kPrimChar:
1451 case Primitive::kPrimShort:
1452 case Primitive::kPrimInt:
1453 case Primitive::kPrimNot: {
1454 return Location::RegisterLocation(R0);
1455 }
1456
1457 case Primitive::kPrimFloat: {
1458 return Location::FpuRegisterLocation(S0);
1459 }
1460
1461 case Primitive::kPrimLong: {
1462 return Location::RegisterPairLocation(R0, R1);
1463 }
1464
1465 case Primitive::kPrimDouble: {
1466 return Location::FpuRegisterPairLocation(S0, S1);
1467 }
1468
1469 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001470 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001471 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001472
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001473 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001474}
1475
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001476Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1477 return Location::RegisterLocation(kMethodRegisterArgument);
1478}
1479
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001480void CodeGeneratorARM::Move32(Location destination, Location source) {
1481 if (source.Equals(destination)) {
1482 return;
1483 }
1484 if (destination.IsRegister()) {
1485 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001486 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001487 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001488 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001489 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001490 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001491 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001492 } else if (destination.IsFpuRegister()) {
1493 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001494 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001495 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001496 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001497 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001498 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001499 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001500 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001501 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001502 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001503 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001504 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001505 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001506 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001507 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001508 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1509 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001510 }
1511 }
1512}
1513
1514void CodeGeneratorARM::Move64(Location destination, Location source) {
1515 if (source.Equals(destination)) {
1516 return;
1517 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001518 if (destination.IsRegisterPair()) {
1519 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001520 EmitParallelMoves(
1521 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1522 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001523 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001524 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001525 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1526 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001527 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001528 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001529 } else if (source.IsFpuRegisterPair()) {
1530 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1531 destination.AsRegisterPairHigh<Register>(),
1532 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001533 } else {
1534 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001535 DCHECK(ExpectedPairLayout(destination));
1536 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1537 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001538 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001539 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001540 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001541 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1542 SP,
1543 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001544 } else if (source.IsRegisterPair()) {
1545 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1546 source.AsRegisterPairLow<Register>(),
1547 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001548 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001549 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001550 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001551 } else {
1552 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001553 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001554 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001555 if (source.AsRegisterPairLow<Register>() == R1) {
1556 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001557 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1558 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001559 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001560 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001561 SP, destination.GetStackIndex());
1562 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001563 } else if (source.IsFpuRegisterPair()) {
1564 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1565 SP,
1566 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001567 } else {
1568 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001569 EmitParallelMoves(
1570 Location::StackSlot(source.GetStackIndex()),
1571 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001572 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001573 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001574 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1575 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001576 }
1577 }
1578}
1579
Calin Juravle175dc732015-08-25 15:42:32 +01001580void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1581 DCHECK(location.IsRegister());
1582 __ LoadImmediate(location.AsRegister<Register>(), value);
1583}
1584
Calin Juravlee460d1d2015-09-29 04:52:17 +01001585void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001586 HParallelMove move(GetGraph()->GetArena());
1587 move.AddMove(src, dst, dst_type, nullptr);
1588 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001589}
1590
1591void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1592 if (location.IsRegister()) {
1593 locations->AddTemp(location);
1594 } else if (location.IsRegisterPair()) {
1595 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1596 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1597 } else {
1598 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1599 }
1600}
1601
Calin Juravle175dc732015-08-25 15:42:32 +01001602void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1603 HInstruction* instruction,
1604 uint32_t dex_pc,
1605 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001606 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001607 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001608 if (EntrypointRequiresStackMap(entrypoint)) {
1609 RecordPcInfo(instruction, dex_pc, slow_path);
1610 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001611}
1612
Roland Levillaindec8f632016-07-22 17:10:06 +01001613void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1614 HInstruction* instruction,
1615 SlowPathCode* slow_path) {
1616 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001617 GenerateInvokeRuntime(entry_point_offset);
1618}
1619
1620void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001621 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1622 __ blx(LR);
1623}
1624
David Brazdilfc6a86a2015-06-26 10:33:45 +00001625void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001626 DCHECK(!successor->IsExitBlock());
1627
1628 HBasicBlock* block = got->GetBlock();
1629 HInstruction* previous = got->GetPrevious();
1630
1631 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001632 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001633 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1634 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1635 return;
1636 }
1637
1638 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1639 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1640 }
1641 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001642 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001643 }
1644}
1645
David Brazdilfc6a86a2015-06-26 10:33:45 +00001646void LocationsBuilderARM::VisitGoto(HGoto* got) {
1647 got->SetLocations(nullptr);
1648}
1649
1650void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1651 HandleGoto(got, got->GetSuccessor());
1652}
1653
1654void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1655 try_boundary->SetLocations(nullptr);
1656}
1657
1658void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1659 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1660 if (!successor->IsExitBlock()) {
1661 HandleGoto(try_boundary, successor);
1662 }
1663}
1664
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001665void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001666 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001667}
1668
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001669void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001670}
1671
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001672void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1673 Primitive::Type type = instruction->InputAt(0)->GetType();
1674 Location lhs_loc = instruction->GetLocations()->InAt(0);
1675 Location rhs_loc = instruction->GetLocations()->InAt(1);
1676 if (rhs_loc.IsConstant()) {
1677 // 0.0 is the only immediate that can be encoded directly in
1678 // a VCMP instruction.
1679 //
1680 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1681 // specify that in a floating-point comparison, positive zero
1682 // and negative zero are considered equal, so we can use the
1683 // literal 0.0 for both cases here.
1684 //
1685 // Note however that some methods (Float.equal, Float.compare,
1686 // Float.compareTo, Double.equal, Double.compare,
1687 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1688 // StrictMath.min) consider 0.0 to be (strictly) greater than
1689 // -0.0. So if we ever translate calls to these methods into a
1690 // HCompare instruction, we must handle the -0.0 case with
1691 // care here.
1692 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1693 if (type == Primitive::kPrimFloat) {
1694 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1695 } else {
1696 DCHECK_EQ(type, Primitive::kPrimDouble);
1697 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1698 }
1699 } else {
1700 if (type == Primitive::kPrimFloat) {
1701 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1702 } else {
1703 DCHECK_EQ(type, Primitive::kPrimDouble);
1704 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1705 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1706 }
1707 }
1708}
1709
Roland Levillain4fa13f62015-07-06 18:11:54 +01001710void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1711 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001712 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001713 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001714 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001715}
1716
1717void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1718 Label* true_label,
1719 Label* false_label) {
1720 LocationSummary* locations = cond->GetLocations();
1721 Location left = locations->InAt(0);
1722 Location right = locations->InAt(1);
1723 IfCondition if_cond = cond->GetCondition();
1724
1725 Register left_high = left.AsRegisterPairHigh<Register>();
1726 Register left_low = left.AsRegisterPairLow<Register>();
1727 IfCondition true_high_cond = if_cond;
1728 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001729 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001730
1731 // Set the conditions for the test, remembering that == needs to be
1732 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001733 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001734 switch (if_cond) {
1735 case kCondEQ:
1736 case kCondNE:
1737 // Nothing to do.
1738 break;
1739 case kCondLT:
1740 false_high_cond = kCondGT;
1741 break;
1742 case kCondLE:
1743 true_high_cond = kCondLT;
1744 break;
1745 case kCondGT:
1746 false_high_cond = kCondLT;
1747 break;
1748 case kCondGE:
1749 true_high_cond = kCondGT;
1750 break;
Aart Bike9f37602015-10-09 11:15:55 -07001751 case kCondB:
1752 false_high_cond = kCondA;
1753 break;
1754 case kCondBE:
1755 true_high_cond = kCondB;
1756 break;
1757 case kCondA:
1758 false_high_cond = kCondB;
1759 break;
1760 case kCondAE:
1761 true_high_cond = kCondA;
1762 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001763 }
1764 if (right.IsConstant()) {
1765 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1766 int32_t val_low = Low32Bits(value);
1767 int32_t val_high = High32Bits(value);
1768
Vladimir Markoac6ac102015-12-17 12:14:00 +00001769 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001770 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001771 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001772 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001773 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001774 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001775 __ b(true_label, ARMCondition(true_high_cond));
1776 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001777 }
1778 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001779 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001780 } else {
1781 Register right_high = right.AsRegisterPairHigh<Register>();
1782 Register right_low = right.AsRegisterPairLow<Register>();
1783
1784 __ cmp(left_high, ShifterOperand(right_high));
1785 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001786 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001787 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001788 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001789 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001790 __ b(true_label, ARMCondition(true_high_cond));
1791 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001792 }
1793 // Must be equal high, so compare the lows.
1794 __ cmp(left_low, ShifterOperand(right_low));
1795 }
1796 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001797 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001798 __ b(true_label, final_condition);
1799}
1800
David Brazdil0debae72015-11-12 18:37:00 +00001801void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1802 Label* true_target_in,
1803 Label* false_target_in) {
1804 // Generated branching requires both targets to be explicit. If either of the
1805 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1806 Label fallthrough_target;
1807 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1808 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1809
Roland Levillain4fa13f62015-07-06 18:11:54 +01001810 Primitive::Type type = condition->InputAt(0)->GetType();
1811 switch (type) {
1812 case Primitive::kPrimLong:
1813 GenerateLongComparesAndJumps(condition, true_target, false_target);
1814 break;
1815 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001816 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001817 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001818 GenerateFPJumps(condition, true_target, false_target);
1819 break;
1820 default:
1821 LOG(FATAL) << "Unexpected compare type " << type;
1822 }
1823
David Brazdil0debae72015-11-12 18:37:00 +00001824 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001825 __ b(false_target);
1826 }
David Brazdil0debae72015-11-12 18:37:00 +00001827
1828 if (fallthrough_target.IsLinked()) {
1829 __ Bind(&fallthrough_target);
1830 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001831}
1832
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001833void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001834 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001835 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001836 Label* false_target) {
1837 HInstruction* cond = instruction->InputAt(condition_input_index);
1838
1839 if (true_target == nullptr && false_target == nullptr) {
1840 // Nothing to do. The code always falls through.
1841 return;
1842 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001843 // Constant condition, statically compared against "true" (integer value 1).
1844 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001845 if (true_target != nullptr) {
1846 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001847 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001848 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001849 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001850 if (false_target != nullptr) {
1851 __ b(false_target);
1852 }
1853 }
1854 return;
1855 }
1856
1857 // The following code generates these patterns:
1858 // (1) true_target == nullptr && false_target != nullptr
1859 // - opposite condition true => branch to false_target
1860 // (2) true_target != nullptr && false_target == nullptr
1861 // - condition true => branch to true_target
1862 // (3) true_target != nullptr && false_target != nullptr
1863 // - condition true => branch to true_target
1864 // - branch to false_target
1865 if (IsBooleanValueOrMaterializedCondition(cond)) {
1866 // Condition has been materialized, compare the output to 0.
1867 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1868 DCHECK(cond_val.IsRegister());
1869 if (true_target == nullptr) {
1870 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1871 } else {
1872 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001873 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001874 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001875 // Condition has not been materialized. Use its inputs as the comparison and
1876 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001877 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001878
1879 // If this is a long or FP comparison that has been folded into
1880 // the HCondition, generate the comparison directly.
1881 Primitive::Type type = condition->InputAt(0)->GetType();
1882 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1883 GenerateCompareTestAndBranch(condition, true_target, false_target);
1884 return;
1885 }
1886
1887 LocationSummary* locations = cond->GetLocations();
1888 DCHECK(locations->InAt(0).IsRegister());
1889 Register left = locations->InAt(0).AsRegister<Register>();
1890 Location right = locations->InAt(1);
1891 if (right.IsRegister()) {
1892 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001893 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001894 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001895 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001896 }
1897 if (true_target == nullptr) {
1898 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1899 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001900 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001901 }
Dave Allison20dfc792014-06-16 20:44:29 -07001902 }
David Brazdil0debae72015-11-12 18:37:00 +00001903
1904 // If neither branch falls through (case 3), the conditional branch to `true_target`
1905 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1906 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001907 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001908 }
1909}
1910
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001911void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001912 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1913 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001914 locations->SetInAt(0, Location::RequiresRegister());
1915 }
1916}
1917
1918void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001919 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1920 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1921 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1922 nullptr : codegen_->GetLabelOf(true_successor);
1923 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1924 nullptr : codegen_->GetLabelOf(false_successor);
1925 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001926}
1927
1928void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1929 LocationSummary* locations = new (GetGraph()->GetArena())
1930 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001931 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001932 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001933 locations->SetInAt(0, Location::RequiresRegister());
1934 }
1935}
1936
1937void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001938 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001939 GenerateTestAndBranch(deoptimize,
1940 /* condition_input_index */ 0,
1941 slow_path->GetEntryLabel(),
1942 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001943}
Dave Allison20dfc792014-06-16 20:44:29 -07001944
David Brazdil74eb1b22015-12-14 11:44:01 +00001945void LocationsBuilderARM::VisitSelect(HSelect* select) {
1946 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1947 if (Primitive::IsFloatingPointType(select->GetType())) {
1948 locations->SetInAt(0, Location::RequiresFpuRegister());
1949 locations->SetInAt(1, Location::RequiresFpuRegister());
1950 } else {
1951 locations->SetInAt(0, Location::RequiresRegister());
1952 locations->SetInAt(1, Location::RequiresRegister());
1953 }
1954 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1955 locations->SetInAt(2, Location::RequiresRegister());
1956 }
1957 locations->SetOut(Location::SameAsFirstInput());
1958}
1959
1960void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1961 LocationSummary* locations = select->GetLocations();
1962 Label false_target;
1963 GenerateTestAndBranch(select,
1964 /* condition_input_index */ 2,
1965 /* true_target */ nullptr,
1966 &false_target);
1967 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1968 __ Bind(&false_target);
1969}
1970
David Srbecky0cf44932015-12-09 14:09:59 +00001971void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1972 new (GetGraph()->GetArena()) LocationSummary(info);
1973}
1974
David Srbeckyd28f4a02016-03-14 17:14:24 +00001975void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1976 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001977}
1978
1979void CodeGeneratorARM::GenerateNop() {
1980 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001981}
1982
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001984 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001985 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001986 // Handle the long/FP comparisons made in instruction simplification.
1987 switch (cond->InputAt(0)->GetType()) {
1988 case Primitive::kPrimLong:
1989 locations->SetInAt(0, Location::RequiresRegister());
1990 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001991 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001992 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1993 }
1994 break;
1995
1996 case Primitive::kPrimFloat:
1997 case Primitive::kPrimDouble:
1998 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001999 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002000 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002001 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2002 }
2003 break;
2004
2005 default:
2006 locations->SetInAt(0, Location::RequiresRegister());
2007 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002008 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002009 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2010 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002011 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002012}
2013
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002014void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002015 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002016 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002017 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002018
2019 LocationSummary* locations = cond->GetLocations();
2020 Location left = locations->InAt(0);
2021 Location right = locations->InAt(1);
2022 Register out = locations->Out().AsRegister<Register>();
2023 Label true_label, false_label;
2024
2025 switch (cond->InputAt(0)->GetType()) {
2026 default: {
2027 // Integer case.
2028 if (right.IsRegister()) {
2029 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2030 } else {
2031 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002032 __ CmpConstant(left.AsRegister<Register>(),
2033 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002034 }
Aart Bike9f37602015-10-09 11:15:55 -07002035 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002036 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002037 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002038 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002039 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002040 return;
2041 }
2042 case Primitive::kPrimLong:
2043 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2044 break;
2045 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002046 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002047 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002048 GenerateFPJumps(cond, &true_label, &false_label);
2049 break;
2050 }
2051
2052 // Convert the jumps into the result.
2053 Label done_label;
2054
2055 // False case: result = 0.
2056 __ Bind(&false_label);
2057 __ LoadImmediate(out, 0);
2058 __ b(&done_label);
2059
2060 // True case: result = 1.
2061 __ Bind(&true_label);
2062 __ LoadImmediate(out, 1);
2063 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002064}
2065
2066void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002067 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002068}
2069
2070void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002071 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002072}
2073
2074void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002075 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002076}
2077
2078void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002079 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002080}
2081
2082void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002083 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002084}
2085
2086void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002087 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002088}
2089
2090void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002091 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002092}
2093
2094void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002095 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002096}
2097
2098void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002099 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002100}
2101
2102void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002103 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002104}
2105
2106void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002107 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002108}
2109
2110void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002111 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002112}
2113
Aart Bike9f37602015-10-09 11:15:55 -07002114void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002115 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002116}
2117
2118void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002119 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002120}
2121
2122void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002123 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002124}
2125
2126void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002127 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002128}
2129
2130void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002131 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002132}
2133
2134void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002135 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002136}
2137
2138void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002139 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002140}
2141
2142void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002143 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002144}
2145
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002146void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002147 LocationSummary* locations =
2148 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002149 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002150}
2151
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002152void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002153 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002154}
2155
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002156void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2157 LocationSummary* locations =
2158 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2159 locations->SetOut(Location::ConstantLocation(constant));
2160}
2161
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002162void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002163 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002164}
2165
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002166void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002167 LocationSummary* locations =
2168 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002169 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002170}
2171
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002172void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002173 // Will be generated at use site.
2174}
2175
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002176void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2177 LocationSummary* locations =
2178 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2179 locations->SetOut(Location::ConstantLocation(constant));
2180}
2181
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002182void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002183 // Will be generated at use site.
2184}
2185
2186void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2187 LocationSummary* locations =
2188 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2189 locations->SetOut(Location::ConstantLocation(constant));
2190}
2191
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002192void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002193 // Will be generated at use site.
2194}
2195
Calin Juravle27df7582015-04-17 19:12:31 +01002196void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2197 memory_barrier->SetLocations(nullptr);
2198}
2199
2200void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002201 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002202}
2203
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002204void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002205 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002206}
2207
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002208void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002209 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002210}
2211
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002212void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002213 LocationSummary* locations =
2214 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002215 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002216}
2217
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002218void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002219 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002220}
2221
Calin Juravle175dc732015-08-25 15:42:32 +01002222void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2223 // The trampoline uses the same calling convention as dex calling conventions,
2224 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2225 // the method_idx.
2226 HandleInvoke(invoke);
2227}
2228
2229void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2230 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2231}
2232
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002233void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002234 // Explicit clinit checks triggered by static invokes must have been pruned by
2235 // art::PrepareForRegisterAllocation.
2236 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002237
Vladimir Marko68c981f2016-08-26 13:13:33 +01002238 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002239 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002240 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2241 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2242 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002243 return;
2244 }
2245
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002246 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002247
2248 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2249 if (invoke->HasPcRelativeDexCache()) {
2250 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2251 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002252}
2253
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002254static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2255 if (invoke->GetLocations()->Intrinsified()) {
2256 IntrinsicCodeGeneratorARM intrinsic(codegen);
2257 intrinsic.Dispatch(invoke);
2258 return true;
2259 }
2260 return false;
2261}
2262
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002263void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002264 // Explicit clinit checks triggered by static invokes must have been pruned by
2265 // art::PrepareForRegisterAllocation.
2266 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002267
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002268 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2269 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002270 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002271
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002272 LocationSummary* locations = invoke->GetLocations();
2273 codegen_->GenerateStaticOrDirectCall(
2274 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002275 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002276}
2277
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002278void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002279 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002280 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002281}
2282
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002283void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002284 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002285 if (intrinsic.TryDispatch(invoke)) {
2286 return;
2287 }
2288
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002289 HandleInvoke(invoke);
2290}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002291
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002292void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002293 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2294 return;
2295 }
2296
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002297 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002298 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002299 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002300}
2301
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002302void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2303 HandleInvoke(invoke);
2304 // Add the hidden argument.
2305 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2306}
2307
2308void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2309 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002310 LocationSummary* locations = invoke->GetLocations();
2311 Register temp = locations->GetTemp(0).AsRegister<Register>();
2312 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002313 Location receiver = locations->InAt(0);
2314 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2315
Roland Levillain3b359c72015-11-17 19:35:12 +00002316 // Set the hidden argument. This is safe to do this here, as R12
2317 // won't be modified thereafter, before the `blx` (call) instruction.
2318 DCHECK_EQ(R12, hidden_reg);
2319 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002320
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002321 if (receiver.IsStackSlot()) {
2322 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002323 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002324 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2325 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002326 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002327 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002328 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002329 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002330 // Instead of simply (possibly) unpoisoning `temp` here, we should
2331 // emit a read barrier for the previous class reference load.
2332 // However this is not required in practice, as this is an
2333 // intermediate/temporary reference and because the current
2334 // concurrent copying collector keeps the from-space memory
2335 // intact/accessible until the end of the marking phase (the
2336 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002337 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002338 __ LoadFromOffset(kLoadWord, temp, temp,
2339 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2340 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002341 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002342 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002343 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002344 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002345 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002346 // LR = temp->GetEntryPoint();
2347 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2348 // LR();
2349 __ blx(LR);
2350 DCHECK(!codegen_->IsLeafMethod());
2351 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2352}
2353
Roland Levillain88cb1752014-10-20 16:36:47 +01002354void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2355 LocationSummary* locations =
2356 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2357 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002358 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002359 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002360 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2361 break;
2362 }
2363 case Primitive::kPrimLong: {
2364 locations->SetInAt(0, Location::RequiresRegister());
2365 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002366 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002367 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002368
Roland Levillain88cb1752014-10-20 16:36:47 +01002369 case Primitive::kPrimFloat:
2370 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002371 locations->SetInAt(0, Location::RequiresFpuRegister());
2372 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002373 break;
2374
2375 default:
2376 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2377 }
2378}
2379
2380void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2381 LocationSummary* locations = neg->GetLocations();
2382 Location out = locations->Out();
2383 Location in = locations->InAt(0);
2384 switch (neg->GetResultType()) {
2385 case Primitive::kPrimInt:
2386 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002387 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002388 break;
2389
2390 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002391 DCHECK(in.IsRegisterPair());
2392 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2393 __ rsbs(out.AsRegisterPairLow<Register>(),
2394 in.AsRegisterPairLow<Register>(),
2395 ShifterOperand(0));
2396 // We cannot emit an RSC (Reverse Subtract with Carry)
2397 // instruction here, as it does not exist in the Thumb-2
2398 // instruction set. We use the following approach
2399 // using SBC and SUB instead.
2400 //
2401 // out.hi = -C
2402 __ sbc(out.AsRegisterPairHigh<Register>(),
2403 out.AsRegisterPairHigh<Register>(),
2404 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2405 // out.hi = out.hi - in.hi
2406 __ sub(out.AsRegisterPairHigh<Register>(),
2407 out.AsRegisterPairHigh<Register>(),
2408 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2409 break;
2410
Roland Levillain88cb1752014-10-20 16:36:47 +01002411 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002412 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002413 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002414 break;
2415
Roland Levillain88cb1752014-10-20 16:36:47 +01002416 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002417 DCHECK(in.IsFpuRegisterPair());
2418 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2419 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002420 break;
2421
2422 default:
2423 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2424 }
2425}
2426
Roland Levillaindff1f282014-11-05 14:15:05 +00002427void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002428 Primitive::Type result_type = conversion->GetResultType();
2429 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002430 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002431
Roland Levillain5b3ee562015-04-14 16:02:41 +01002432 // The float-to-long, double-to-long and long-to-float type conversions
2433 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002434 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002435 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2436 && result_type == Primitive::kPrimLong)
2437 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002438 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002439 : LocationSummary::kNoCall;
2440 LocationSummary* locations =
2441 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2442
David Brazdilb2bd1c52015-03-25 11:17:37 +00002443 // The Java language does not allow treating boolean as an integral type but
2444 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002445
Roland Levillaindff1f282014-11-05 14:15:05 +00002446 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002447 case Primitive::kPrimByte:
2448 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002449 case Primitive::kPrimLong:
2450 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002451 case Primitive::kPrimBoolean:
2452 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002453 case Primitive::kPrimShort:
2454 case Primitive::kPrimInt:
2455 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002456 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002457 locations->SetInAt(0, Location::RequiresRegister());
2458 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2459 break;
2460
2461 default:
2462 LOG(FATAL) << "Unexpected type conversion from " << input_type
2463 << " to " << result_type;
2464 }
2465 break;
2466
Roland Levillain01a8d712014-11-14 16:27:39 +00002467 case Primitive::kPrimShort:
2468 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002469 case Primitive::kPrimLong:
2470 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002471 case Primitive::kPrimBoolean:
2472 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002473 case Primitive::kPrimByte:
2474 case Primitive::kPrimInt:
2475 case Primitive::kPrimChar:
2476 // Processing a Dex `int-to-short' instruction.
2477 locations->SetInAt(0, Location::RequiresRegister());
2478 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2479 break;
2480
2481 default:
2482 LOG(FATAL) << "Unexpected type conversion from " << input_type
2483 << " to " << result_type;
2484 }
2485 break;
2486
Roland Levillain946e1432014-11-11 17:35:19 +00002487 case Primitive::kPrimInt:
2488 switch (input_type) {
2489 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002490 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002491 locations->SetInAt(0, Location::Any());
2492 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2493 break;
2494
2495 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002496 // Processing a Dex `float-to-int' instruction.
2497 locations->SetInAt(0, Location::RequiresFpuRegister());
2498 locations->SetOut(Location::RequiresRegister());
2499 locations->AddTemp(Location::RequiresFpuRegister());
2500 break;
2501
Roland Levillain946e1432014-11-11 17:35:19 +00002502 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002503 // Processing a Dex `double-to-int' instruction.
2504 locations->SetInAt(0, Location::RequiresFpuRegister());
2505 locations->SetOut(Location::RequiresRegister());
2506 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002507 break;
2508
2509 default:
2510 LOG(FATAL) << "Unexpected type conversion from " << input_type
2511 << " to " << result_type;
2512 }
2513 break;
2514
Roland Levillaindff1f282014-11-05 14:15:05 +00002515 case Primitive::kPrimLong:
2516 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002517 case Primitive::kPrimBoolean:
2518 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002519 case Primitive::kPrimByte:
2520 case Primitive::kPrimShort:
2521 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002522 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002523 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002524 locations->SetInAt(0, Location::RequiresRegister());
2525 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2526 break;
2527
Roland Levillain624279f2014-12-04 11:54:28 +00002528 case Primitive::kPrimFloat: {
2529 // Processing a Dex `float-to-long' instruction.
2530 InvokeRuntimeCallingConvention calling_convention;
2531 locations->SetInAt(0, Location::FpuRegisterLocation(
2532 calling_convention.GetFpuRegisterAt(0)));
2533 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2534 break;
2535 }
2536
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002537 case Primitive::kPrimDouble: {
2538 // Processing a Dex `double-to-long' instruction.
2539 InvokeRuntimeCallingConvention calling_convention;
2540 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2541 calling_convention.GetFpuRegisterAt(0),
2542 calling_convention.GetFpuRegisterAt(1)));
2543 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002544 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002545 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002546
2547 default:
2548 LOG(FATAL) << "Unexpected type conversion from " << input_type
2549 << " to " << result_type;
2550 }
2551 break;
2552
Roland Levillain981e4542014-11-14 11:47:14 +00002553 case Primitive::kPrimChar:
2554 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002555 case Primitive::kPrimLong:
2556 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002557 case Primitive::kPrimBoolean:
2558 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002559 case Primitive::kPrimByte:
2560 case Primitive::kPrimShort:
2561 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002562 // Processing a Dex `int-to-char' instruction.
2563 locations->SetInAt(0, Location::RequiresRegister());
2564 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2565 break;
2566
2567 default:
2568 LOG(FATAL) << "Unexpected type conversion from " << input_type
2569 << " to " << result_type;
2570 }
2571 break;
2572
Roland Levillaindff1f282014-11-05 14:15:05 +00002573 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002574 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002575 case Primitive::kPrimBoolean:
2576 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002577 case Primitive::kPrimByte:
2578 case Primitive::kPrimShort:
2579 case Primitive::kPrimInt:
2580 case Primitive::kPrimChar:
2581 // Processing a Dex `int-to-float' instruction.
2582 locations->SetInAt(0, Location::RequiresRegister());
2583 locations->SetOut(Location::RequiresFpuRegister());
2584 break;
2585
Roland Levillain5b3ee562015-04-14 16:02:41 +01002586 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002587 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002588 InvokeRuntimeCallingConvention calling_convention;
2589 locations->SetInAt(0, Location::RegisterPairLocation(
2590 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2591 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002592 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002593 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002594
Roland Levillaincff13742014-11-17 14:32:17 +00002595 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002596 // Processing a Dex `double-to-float' instruction.
2597 locations->SetInAt(0, Location::RequiresFpuRegister());
2598 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002599 break;
2600
2601 default:
2602 LOG(FATAL) << "Unexpected type conversion from " << input_type
2603 << " to " << result_type;
2604 };
2605 break;
2606
Roland Levillaindff1f282014-11-05 14:15:05 +00002607 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002608 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002609 case Primitive::kPrimBoolean:
2610 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002611 case Primitive::kPrimByte:
2612 case Primitive::kPrimShort:
2613 case Primitive::kPrimInt:
2614 case Primitive::kPrimChar:
2615 // Processing a Dex `int-to-double' instruction.
2616 locations->SetInAt(0, Location::RequiresRegister());
2617 locations->SetOut(Location::RequiresFpuRegister());
2618 break;
2619
2620 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002621 // Processing a Dex `long-to-double' instruction.
2622 locations->SetInAt(0, Location::RequiresRegister());
2623 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002624 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002625 locations->AddTemp(Location::RequiresFpuRegister());
2626 break;
2627
Roland Levillaincff13742014-11-17 14:32:17 +00002628 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002629 // Processing a Dex `float-to-double' instruction.
2630 locations->SetInAt(0, Location::RequiresFpuRegister());
2631 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002632 break;
2633
2634 default:
2635 LOG(FATAL) << "Unexpected type conversion from " << input_type
2636 << " to " << result_type;
2637 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002638 break;
2639
2640 default:
2641 LOG(FATAL) << "Unexpected type conversion from " << input_type
2642 << " to " << result_type;
2643 }
2644}
2645
2646void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2647 LocationSummary* locations = conversion->GetLocations();
2648 Location out = locations->Out();
2649 Location in = locations->InAt(0);
2650 Primitive::Type result_type = conversion->GetResultType();
2651 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002652 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002653 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002654 case Primitive::kPrimByte:
2655 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002656 case Primitive::kPrimLong:
2657 // Type conversion from long to byte is a result of code transformations.
2658 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2659 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002660 case Primitive::kPrimBoolean:
2661 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002662 case Primitive::kPrimShort:
2663 case Primitive::kPrimInt:
2664 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002665 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002666 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002667 break;
2668
2669 default:
2670 LOG(FATAL) << "Unexpected type conversion from " << input_type
2671 << " to " << result_type;
2672 }
2673 break;
2674
Roland Levillain01a8d712014-11-14 16:27:39 +00002675 case Primitive::kPrimShort:
2676 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002677 case Primitive::kPrimLong:
2678 // Type conversion from long to short is a result of code transformations.
2679 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2680 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002681 case Primitive::kPrimBoolean:
2682 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002683 case Primitive::kPrimByte:
2684 case Primitive::kPrimInt:
2685 case Primitive::kPrimChar:
2686 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002687 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002688 break;
2689
2690 default:
2691 LOG(FATAL) << "Unexpected type conversion from " << input_type
2692 << " to " << result_type;
2693 }
2694 break;
2695
Roland Levillain946e1432014-11-11 17:35:19 +00002696 case Primitive::kPrimInt:
2697 switch (input_type) {
2698 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002699 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002700 DCHECK(out.IsRegister());
2701 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002702 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002703 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002704 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002705 } else {
2706 DCHECK(in.IsConstant());
2707 DCHECK(in.GetConstant()->IsLongConstant());
2708 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002709 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002710 }
2711 break;
2712
Roland Levillain3f8f9362014-12-02 17:45:01 +00002713 case Primitive::kPrimFloat: {
2714 // Processing a Dex `float-to-int' instruction.
2715 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002716 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002717 __ vmovrs(out.AsRegister<Register>(), temp);
2718 break;
2719 }
2720
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002721 case Primitive::kPrimDouble: {
2722 // Processing a Dex `double-to-int' instruction.
2723 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002724 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002725 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002726 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002727 }
Roland Levillain946e1432014-11-11 17:35:19 +00002728
2729 default:
2730 LOG(FATAL) << "Unexpected type conversion from " << input_type
2731 << " to " << result_type;
2732 }
2733 break;
2734
Roland Levillaindff1f282014-11-05 14:15:05 +00002735 case Primitive::kPrimLong:
2736 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002737 case Primitive::kPrimBoolean:
2738 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002739 case Primitive::kPrimByte:
2740 case Primitive::kPrimShort:
2741 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002742 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002743 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002744 DCHECK(out.IsRegisterPair());
2745 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002746 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002747 // Sign extension.
2748 __ Asr(out.AsRegisterPairHigh<Register>(),
2749 out.AsRegisterPairLow<Register>(),
2750 31);
2751 break;
2752
2753 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002754 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002755 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002756 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002757 break;
2758
Roland Levillaindff1f282014-11-05 14:15:05 +00002759 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002760 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002761 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002762 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002763 break;
2764
2765 default:
2766 LOG(FATAL) << "Unexpected type conversion from " << input_type
2767 << " to " << result_type;
2768 }
2769 break;
2770
Roland Levillain981e4542014-11-14 11:47:14 +00002771 case Primitive::kPrimChar:
2772 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002773 case Primitive::kPrimLong:
2774 // Type conversion from long to char is a result of code transformations.
2775 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2776 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002777 case Primitive::kPrimBoolean:
2778 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002779 case Primitive::kPrimByte:
2780 case Primitive::kPrimShort:
2781 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002782 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002783 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002784 break;
2785
2786 default:
2787 LOG(FATAL) << "Unexpected type conversion from " << input_type
2788 << " to " << result_type;
2789 }
2790 break;
2791
Roland Levillaindff1f282014-11-05 14:15:05 +00002792 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002793 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002794 case Primitive::kPrimBoolean:
2795 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002796 case Primitive::kPrimByte:
2797 case Primitive::kPrimShort:
2798 case Primitive::kPrimInt:
2799 case Primitive::kPrimChar: {
2800 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002801 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2802 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002803 break;
2804 }
2805
Roland Levillain5b3ee562015-04-14 16:02:41 +01002806 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002807 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002808 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002809 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002810 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002811
Roland Levillaincff13742014-11-17 14:32:17 +00002812 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002813 // Processing a Dex `double-to-float' instruction.
2814 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2815 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002816 break;
2817
2818 default:
2819 LOG(FATAL) << "Unexpected type conversion from " << input_type
2820 << " to " << result_type;
2821 };
2822 break;
2823
Roland Levillaindff1f282014-11-05 14:15:05 +00002824 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002825 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002826 case Primitive::kPrimBoolean:
2827 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002828 case Primitive::kPrimByte:
2829 case Primitive::kPrimShort:
2830 case Primitive::kPrimInt:
2831 case Primitive::kPrimChar: {
2832 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002833 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002834 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2835 out.AsFpuRegisterPairLow<SRegister>());
2836 break;
2837 }
2838
Roland Levillain647b9ed2014-11-27 12:06:00 +00002839 case Primitive::kPrimLong: {
2840 // Processing a Dex `long-to-double' instruction.
2841 Register low = in.AsRegisterPairLow<Register>();
2842 Register high = in.AsRegisterPairHigh<Register>();
2843 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2844 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002845 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002846 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002847 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2848 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002849
Roland Levillain682393c2015-04-14 15:57:52 +01002850 // temp_d = int-to-double(high)
2851 __ vmovsr(temp_s, high);
2852 __ vcvtdi(temp_d, temp_s);
2853 // constant_d = k2Pow32EncodingForDouble
2854 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2855 // out_d = unsigned-to-double(low)
2856 __ vmovsr(out_s, low);
2857 __ vcvtdu(out_d, out_s);
2858 // out_d += temp_d * constant_d
2859 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002860 break;
2861 }
2862
Roland Levillaincff13742014-11-17 14:32:17 +00002863 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002864 // Processing a Dex `float-to-double' instruction.
2865 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2866 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002867 break;
2868
2869 default:
2870 LOG(FATAL) << "Unexpected type conversion from " << input_type
2871 << " to " << result_type;
2872 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002873 break;
2874
2875 default:
2876 LOG(FATAL) << "Unexpected type conversion from " << input_type
2877 << " to " << result_type;
2878 }
2879}
2880
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002881void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002882 LocationSummary* locations =
2883 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002884 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002885 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002886 locations->SetInAt(0, Location::RequiresRegister());
2887 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002888 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2889 break;
2890 }
2891
2892 case Primitive::kPrimLong: {
2893 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002894 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002896 break;
2897 }
2898
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002899 case Primitive::kPrimFloat:
2900 case Primitive::kPrimDouble: {
2901 locations->SetInAt(0, Location::RequiresFpuRegister());
2902 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002903 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002904 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002905 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002906
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002907 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002908 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002909 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002910}
2911
2912void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2913 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002914 Location out = locations->Out();
2915 Location first = locations->InAt(0);
2916 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002917 switch (add->GetResultType()) {
2918 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002919 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002920 __ add(out.AsRegister<Register>(),
2921 first.AsRegister<Register>(),
2922 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002923 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002924 __ AddConstant(out.AsRegister<Register>(),
2925 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002926 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002927 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002928 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002929
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002930 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002931 if (second.IsConstant()) {
2932 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2933 GenerateAddLongConst(out, first, value);
2934 } else {
2935 DCHECK(second.IsRegisterPair());
2936 __ adds(out.AsRegisterPairLow<Register>(),
2937 first.AsRegisterPairLow<Register>(),
2938 ShifterOperand(second.AsRegisterPairLow<Register>()));
2939 __ adc(out.AsRegisterPairHigh<Register>(),
2940 first.AsRegisterPairHigh<Register>(),
2941 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2942 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002943 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002944 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002945
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002946 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002947 __ vadds(out.AsFpuRegister<SRegister>(),
2948 first.AsFpuRegister<SRegister>(),
2949 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002950 break;
2951
2952 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002953 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2954 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2955 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002956 break;
2957
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002958 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002959 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002960 }
2961}
2962
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002963void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002964 LocationSummary* locations =
2965 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002966 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002967 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002968 locations->SetInAt(0, Location::RequiresRegister());
2969 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002970 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2971 break;
2972 }
2973
2974 case Primitive::kPrimLong: {
2975 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002976 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002977 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002978 break;
2979 }
Calin Juravle11351682014-10-23 15:38:15 +01002980 case Primitive::kPrimFloat:
2981 case Primitive::kPrimDouble: {
2982 locations->SetInAt(0, Location::RequiresFpuRegister());
2983 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002984 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002985 break;
Calin Juravle11351682014-10-23 15:38:15 +01002986 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002987 default:
Calin Juravle11351682014-10-23 15:38:15 +01002988 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002989 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002990}
2991
2992void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2993 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002994 Location out = locations->Out();
2995 Location first = locations->InAt(0);
2996 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002997 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002998 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002999 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003000 __ sub(out.AsRegister<Register>(),
3001 first.AsRegister<Register>(),
3002 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003003 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003004 __ AddConstant(out.AsRegister<Register>(),
3005 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003006 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003007 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003008 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003009 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003010
Calin Juravle11351682014-10-23 15:38:15 +01003011 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003012 if (second.IsConstant()) {
3013 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3014 GenerateAddLongConst(out, first, -value);
3015 } else {
3016 DCHECK(second.IsRegisterPair());
3017 __ subs(out.AsRegisterPairLow<Register>(),
3018 first.AsRegisterPairLow<Register>(),
3019 ShifterOperand(second.AsRegisterPairLow<Register>()));
3020 __ sbc(out.AsRegisterPairHigh<Register>(),
3021 first.AsRegisterPairHigh<Register>(),
3022 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3023 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003024 break;
Calin Juravle11351682014-10-23 15:38:15 +01003025 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003026
Calin Juravle11351682014-10-23 15:38:15 +01003027 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003028 __ vsubs(out.AsFpuRegister<SRegister>(),
3029 first.AsFpuRegister<SRegister>(),
3030 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003031 break;
Calin Juravle11351682014-10-23 15:38:15 +01003032 }
3033
3034 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003035 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3036 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3037 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003038 break;
3039 }
3040
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003041
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003042 default:
Calin Juravle11351682014-10-23 15:38:15 +01003043 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003044 }
3045}
3046
Calin Juravle34bacdf2014-10-07 20:23:36 +01003047void LocationsBuilderARM::VisitMul(HMul* mul) {
3048 LocationSummary* locations =
3049 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3050 switch (mul->GetResultType()) {
3051 case Primitive::kPrimInt:
3052 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003053 locations->SetInAt(0, Location::RequiresRegister());
3054 locations->SetInAt(1, Location::RequiresRegister());
3055 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003056 break;
3057 }
3058
Calin Juravleb5bfa962014-10-21 18:02:24 +01003059 case Primitive::kPrimFloat:
3060 case Primitive::kPrimDouble: {
3061 locations->SetInAt(0, Location::RequiresFpuRegister());
3062 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003063 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003064 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003065 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003066
3067 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003068 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003069 }
3070}
3071
3072void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3073 LocationSummary* locations = mul->GetLocations();
3074 Location out = locations->Out();
3075 Location first = locations->InAt(0);
3076 Location second = locations->InAt(1);
3077 switch (mul->GetResultType()) {
3078 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003079 __ mul(out.AsRegister<Register>(),
3080 first.AsRegister<Register>(),
3081 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003082 break;
3083 }
3084 case Primitive::kPrimLong: {
3085 Register out_hi = out.AsRegisterPairHigh<Register>();
3086 Register out_lo = out.AsRegisterPairLow<Register>();
3087 Register in1_hi = first.AsRegisterPairHigh<Register>();
3088 Register in1_lo = first.AsRegisterPairLow<Register>();
3089 Register in2_hi = second.AsRegisterPairHigh<Register>();
3090 Register in2_lo = second.AsRegisterPairLow<Register>();
3091
3092 // Extra checks to protect caused by the existence of R1_R2.
3093 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3094 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3095 DCHECK_NE(out_hi, in1_lo);
3096 DCHECK_NE(out_hi, in2_lo);
3097
3098 // input: in1 - 64 bits, in2 - 64 bits
3099 // output: out
3100 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3101 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3102 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3103
3104 // IP <- in1.lo * in2.hi
3105 __ mul(IP, in1_lo, in2_hi);
3106 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3107 __ mla(out_hi, in1_hi, in2_lo, IP);
3108 // out.lo <- (in1.lo * in2.lo)[31:0];
3109 __ umull(out_lo, IP, in1_lo, in2_lo);
3110 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3111 __ add(out_hi, out_hi, ShifterOperand(IP));
3112 break;
3113 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003114
3115 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003116 __ vmuls(out.AsFpuRegister<SRegister>(),
3117 first.AsFpuRegister<SRegister>(),
3118 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003119 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003120 }
3121
3122 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003123 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3124 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3125 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003126 break;
3127 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003128
3129 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003130 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003131 }
3132}
3133
Zheng Xuc6667102015-05-15 16:08:45 +08003134void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3135 DCHECK(instruction->IsDiv() || instruction->IsRem());
3136 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3137
3138 LocationSummary* locations = instruction->GetLocations();
3139 Location second = locations->InAt(1);
3140 DCHECK(second.IsConstant());
3141
3142 Register out = locations->Out().AsRegister<Register>();
3143 Register dividend = locations->InAt(0).AsRegister<Register>();
3144 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3145 DCHECK(imm == 1 || imm == -1);
3146
3147 if (instruction->IsRem()) {
3148 __ LoadImmediate(out, 0);
3149 } else {
3150 if (imm == 1) {
3151 __ Mov(out, dividend);
3152 } else {
3153 __ rsb(out, dividend, ShifterOperand(0));
3154 }
3155 }
3156}
3157
3158void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3159 DCHECK(instruction->IsDiv() || instruction->IsRem());
3160 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3161
3162 LocationSummary* locations = instruction->GetLocations();
3163 Location second = locations->InAt(1);
3164 DCHECK(second.IsConstant());
3165
3166 Register out = locations->Out().AsRegister<Register>();
3167 Register dividend = locations->InAt(0).AsRegister<Register>();
3168 Register temp = locations->GetTemp(0).AsRegister<Register>();
3169 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003170 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003171 int ctz_imm = CTZ(abs_imm);
3172
3173 if (ctz_imm == 1) {
3174 __ Lsr(temp, dividend, 32 - ctz_imm);
3175 } else {
3176 __ Asr(temp, dividend, 31);
3177 __ Lsr(temp, temp, 32 - ctz_imm);
3178 }
3179 __ add(out, temp, ShifterOperand(dividend));
3180
3181 if (instruction->IsDiv()) {
3182 __ Asr(out, out, ctz_imm);
3183 if (imm < 0) {
3184 __ rsb(out, out, ShifterOperand(0));
3185 }
3186 } else {
3187 __ ubfx(out, out, 0, ctz_imm);
3188 __ sub(out, out, ShifterOperand(temp));
3189 }
3190}
3191
3192void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3193 DCHECK(instruction->IsDiv() || instruction->IsRem());
3194 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3195
3196 LocationSummary* locations = instruction->GetLocations();
3197 Location second = locations->InAt(1);
3198 DCHECK(second.IsConstant());
3199
3200 Register out = locations->Out().AsRegister<Register>();
3201 Register dividend = locations->InAt(0).AsRegister<Register>();
3202 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3203 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3204 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3205
3206 int64_t magic;
3207 int shift;
3208 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3209
3210 __ LoadImmediate(temp1, magic);
3211 __ smull(temp2, temp1, dividend, temp1);
3212
3213 if (imm > 0 && magic < 0) {
3214 __ add(temp1, temp1, ShifterOperand(dividend));
3215 } else if (imm < 0 && magic > 0) {
3216 __ sub(temp1, temp1, ShifterOperand(dividend));
3217 }
3218
3219 if (shift != 0) {
3220 __ Asr(temp1, temp1, shift);
3221 }
3222
3223 if (instruction->IsDiv()) {
3224 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3225 } else {
3226 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3227 // TODO: Strength reduction for mls.
3228 __ LoadImmediate(temp2, imm);
3229 __ mls(out, temp1, temp2, dividend);
3230 }
3231}
3232
3233void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3234 DCHECK(instruction->IsDiv() || instruction->IsRem());
3235 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3236
3237 LocationSummary* locations = instruction->GetLocations();
3238 Location second = locations->InAt(1);
3239 DCHECK(second.IsConstant());
3240
3241 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3242 if (imm == 0) {
3243 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3244 } else if (imm == 1 || imm == -1) {
3245 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003246 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003247 DivRemByPowerOfTwo(instruction);
3248 } else {
3249 DCHECK(imm <= -2 || imm >= 2);
3250 GenerateDivRemWithAnyConstant(instruction);
3251 }
3252}
3253
Calin Juravle7c4954d2014-10-28 16:57:40 +00003254void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003255 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3256 if (div->GetResultType() == Primitive::kPrimLong) {
3257 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003258 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003259 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3260 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003261 } else if (div->GetResultType() == Primitive::kPrimInt &&
3262 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3263 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003264 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003265 }
3266
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003267 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3268
Calin Juravle7c4954d2014-10-28 16:57:40 +00003269 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003270 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003271 if (div->InputAt(1)->IsConstant()) {
3272 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003273 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003275 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3276 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003277 // No temp register required.
3278 } else {
3279 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003280 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003281 locations->AddTemp(Location::RequiresRegister());
3282 }
3283 }
3284 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003285 locations->SetInAt(0, Location::RequiresRegister());
3286 locations->SetInAt(1, Location::RequiresRegister());
3287 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3288 } else {
3289 InvokeRuntimeCallingConvention calling_convention;
3290 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3291 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3292 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3293 // we only need the former.
3294 locations->SetOut(Location::RegisterLocation(R0));
3295 }
Calin Juravled0d48522014-11-04 16:40:20 +00003296 break;
3297 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003298 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003299 InvokeRuntimeCallingConvention calling_convention;
3300 locations->SetInAt(0, Location::RegisterPairLocation(
3301 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3302 locations->SetInAt(1, Location::RegisterPairLocation(
3303 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003304 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003305 break;
3306 }
3307 case Primitive::kPrimFloat:
3308 case Primitive::kPrimDouble: {
3309 locations->SetInAt(0, Location::RequiresFpuRegister());
3310 locations->SetInAt(1, Location::RequiresFpuRegister());
3311 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3312 break;
3313 }
3314
3315 default:
3316 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3317 }
3318}
3319
3320void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3321 LocationSummary* locations = div->GetLocations();
3322 Location out = locations->Out();
3323 Location first = locations->InAt(0);
3324 Location second = locations->InAt(1);
3325
3326 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003327 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003328 if (second.IsConstant()) {
3329 GenerateDivRemConstantIntegral(div);
3330 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003331 __ sdiv(out.AsRegister<Register>(),
3332 first.AsRegister<Register>(),
3333 second.AsRegister<Register>());
3334 } else {
3335 InvokeRuntimeCallingConvention calling_convention;
3336 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3337 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3338 DCHECK_EQ(R0, out.AsRegister<Register>());
3339
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003340 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003341 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003342 }
Calin Juravled0d48522014-11-04 16:40:20 +00003343 break;
3344 }
3345
Calin Juravle7c4954d2014-10-28 16:57:40 +00003346 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003347 InvokeRuntimeCallingConvention calling_convention;
3348 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3349 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3350 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3351 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3352 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003353 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003354
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003355 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003356 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003357 break;
3358 }
3359
3360 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003361 __ vdivs(out.AsFpuRegister<SRegister>(),
3362 first.AsFpuRegister<SRegister>(),
3363 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003364 break;
3365 }
3366
3367 case Primitive::kPrimDouble: {
3368 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3369 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3370 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3371 break;
3372 }
3373
3374 default:
3375 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3376 }
3377}
3378
Calin Juravlebacfec32014-11-14 15:54:36 +00003379void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003380 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003381
3382 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003383 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003384 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3385 // sdiv will be replaced by other instruction sequence.
3386 call_kind = LocationSummary::kNoCall;
3387 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3388 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003389 // Have hardware divide instruction for int, do it with three instructions.
3390 call_kind = LocationSummary::kNoCall;
3391 }
3392
Calin Juravlebacfec32014-11-14 15:54:36 +00003393 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3394
Calin Juravled2ec87d2014-12-08 14:24:46 +00003395 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003396 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003397 if (rem->InputAt(1)->IsConstant()) {
3398 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003399 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003400 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003401 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3402 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003403 // No temp register required.
3404 } else {
3405 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003406 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003407 locations->AddTemp(Location::RequiresRegister());
3408 }
3409 }
3410 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003411 locations->SetInAt(0, Location::RequiresRegister());
3412 locations->SetInAt(1, Location::RequiresRegister());
3413 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3414 locations->AddTemp(Location::RequiresRegister());
3415 } else {
3416 InvokeRuntimeCallingConvention calling_convention;
3417 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3418 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3419 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3420 // we only need the latter.
3421 locations->SetOut(Location::RegisterLocation(R1));
3422 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003423 break;
3424 }
3425 case Primitive::kPrimLong: {
3426 InvokeRuntimeCallingConvention calling_convention;
3427 locations->SetInAt(0, Location::RegisterPairLocation(
3428 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3429 locations->SetInAt(1, Location::RegisterPairLocation(
3430 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3431 // The runtime helper puts the output in R2,R3.
3432 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3433 break;
3434 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003435 case Primitive::kPrimFloat: {
3436 InvokeRuntimeCallingConvention calling_convention;
3437 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3438 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3439 locations->SetOut(Location::FpuRegisterLocation(S0));
3440 break;
3441 }
3442
Calin Juravlebacfec32014-11-14 15:54:36 +00003443 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003444 InvokeRuntimeCallingConvention calling_convention;
3445 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3446 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3447 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3448 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3449 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003450 break;
3451 }
3452
3453 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003454 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003455 }
3456}
3457
3458void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3459 LocationSummary* locations = rem->GetLocations();
3460 Location out = locations->Out();
3461 Location first = locations->InAt(0);
3462 Location second = locations->InAt(1);
3463
Calin Juravled2ec87d2014-12-08 14:24:46 +00003464 Primitive::Type type = rem->GetResultType();
3465 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003466 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003467 if (second.IsConstant()) {
3468 GenerateDivRemConstantIntegral(rem);
3469 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003470 Register reg1 = first.AsRegister<Register>();
3471 Register reg2 = second.AsRegister<Register>();
3472 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003473
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003474 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003475 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003476 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003477 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003478 } else {
3479 InvokeRuntimeCallingConvention calling_convention;
3480 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3481 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3482 DCHECK_EQ(R1, out.AsRegister<Register>());
3483
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003484 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003485 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003486 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003487 break;
3488 }
3489
3490 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003491 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003492 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003493 break;
3494 }
3495
Calin Juravled2ec87d2014-12-08 14:24:46 +00003496 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003497 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003498 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003499 break;
3500 }
3501
Calin Juravlebacfec32014-11-14 15:54:36 +00003502 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003503 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003504 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003505 break;
3506 }
3507
3508 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003509 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003510 }
3511}
3512
Calin Juravled0d48522014-11-04 16:40:20 +00003513void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003514 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003515 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003516}
3517
3518void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003519 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003520 codegen_->AddSlowPath(slow_path);
3521
3522 LocationSummary* locations = instruction->GetLocations();
3523 Location value = locations->InAt(0);
3524
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003525 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003526 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003527 case Primitive::kPrimByte:
3528 case Primitive::kPrimChar:
3529 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003530 case Primitive::kPrimInt: {
3531 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003532 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003533 } else {
3534 DCHECK(value.IsConstant()) << value;
3535 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3536 __ b(slow_path->GetEntryLabel());
3537 }
3538 }
3539 break;
3540 }
3541 case Primitive::kPrimLong: {
3542 if (value.IsRegisterPair()) {
3543 __ orrs(IP,
3544 value.AsRegisterPairLow<Register>(),
3545 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3546 __ b(slow_path->GetEntryLabel(), EQ);
3547 } else {
3548 DCHECK(value.IsConstant()) << value;
3549 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3550 __ b(slow_path->GetEntryLabel());
3551 }
3552 }
3553 break;
3554 default:
3555 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3556 }
3557 }
Calin Juravled0d48522014-11-04 16:40:20 +00003558}
3559
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003560void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3561 Register in = locations->InAt(0).AsRegister<Register>();
3562 Location rhs = locations->InAt(1);
3563 Register out = locations->Out().AsRegister<Register>();
3564
3565 if (rhs.IsConstant()) {
3566 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3567 // so map all rotations to a +ve. equivalent in that range.
3568 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3569 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3570 if (rot) {
3571 // Rotate, mapping left rotations to right equivalents if necessary.
3572 // (e.g. left by 2 bits == right by 30.)
3573 __ Ror(out, in, rot);
3574 } else if (out != in) {
3575 __ Mov(out, in);
3576 }
3577 } else {
3578 __ Ror(out, in, rhs.AsRegister<Register>());
3579 }
3580}
3581
3582// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3583// rotates by swapping input regs (effectively rotating by the first 32-bits of
3584// a larger rotation) or flipping direction (thus treating larger right/left
3585// rotations as sub-word sized rotations in the other direction) as appropriate.
3586void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3587 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3588 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3589 Location rhs = locations->InAt(1);
3590 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3591 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3592
3593 if (rhs.IsConstant()) {
3594 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3595 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003596 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003597 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3598 // logic below to a simple pair of binary orr.
3599 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3600 if (rot >= kArmBitsPerWord) {
3601 rot -= kArmBitsPerWord;
3602 std::swap(in_reg_hi, in_reg_lo);
3603 }
3604 // Rotate, or mov to out for zero or word size rotations.
3605 if (rot != 0u) {
3606 __ Lsr(out_reg_hi, in_reg_hi, rot);
3607 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3608 __ Lsr(out_reg_lo, in_reg_lo, rot);
3609 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3610 } else {
3611 __ Mov(out_reg_lo, in_reg_lo);
3612 __ Mov(out_reg_hi, in_reg_hi);
3613 }
3614 } else {
3615 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3616 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3617 Label end;
3618 Label shift_by_32_plus_shift_right;
3619
3620 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3621 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3622 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3623 __ b(&shift_by_32_plus_shift_right, CC);
3624
3625 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3626 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3627 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3628 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3629 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3630 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3631 __ Lsr(shift_left, in_reg_hi, shift_right);
3632 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3633 __ b(&end);
3634
3635 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3636 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3637 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3638 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3639 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3640 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3641 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3642 __ Lsl(shift_right, in_reg_hi, shift_left);
3643 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3644
3645 __ Bind(&end);
3646 }
3647}
Roland Levillain22c49222016-03-18 14:04:28 +00003648
3649void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003650 LocationSummary* locations =
3651 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3652 switch (ror->GetResultType()) {
3653 case Primitive::kPrimInt: {
3654 locations->SetInAt(0, Location::RequiresRegister());
3655 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3657 break;
3658 }
3659 case Primitive::kPrimLong: {
3660 locations->SetInAt(0, Location::RequiresRegister());
3661 if (ror->InputAt(1)->IsConstant()) {
3662 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3663 } else {
3664 locations->SetInAt(1, Location::RequiresRegister());
3665 locations->AddTemp(Location::RequiresRegister());
3666 locations->AddTemp(Location::RequiresRegister());
3667 }
3668 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3669 break;
3670 }
3671 default:
3672 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3673 }
3674}
3675
Roland Levillain22c49222016-03-18 14:04:28 +00003676void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003677 LocationSummary* locations = ror->GetLocations();
3678 Primitive::Type type = ror->GetResultType();
3679 switch (type) {
3680 case Primitive::kPrimInt: {
3681 HandleIntegerRotate(locations);
3682 break;
3683 }
3684 case Primitive::kPrimLong: {
3685 HandleLongRotate(locations);
3686 break;
3687 }
3688 default:
3689 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003690 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003691 }
3692}
3693
Calin Juravle9aec02f2014-11-18 23:06:35 +00003694void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3695 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3696
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003697 LocationSummary* locations =
3698 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003699
3700 switch (op->GetResultType()) {
3701 case Primitive::kPrimInt: {
3702 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003703 if (op->InputAt(1)->IsConstant()) {
3704 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3705 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3706 } else {
3707 locations->SetInAt(1, Location::RequiresRegister());
3708 // Make the output overlap, as it will be used to hold the masked
3709 // second input.
3710 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3711 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003712 break;
3713 }
3714 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003715 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003716 if (op->InputAt(1)->IsConstant()) {
3717 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3718 // For simplicity, use kOutputOverlap even though we only require that low registers
3719 // don't clash with high registers which the register allocator currently guarantees.
3720 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3721 } else {
3722 locations->SetInAt(1, Location::RequiresRegister());
3723 locations->AddTemp(Location::RequiresRegister());
3724 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3725 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003726 break;
3727 }
3728 default:
3729 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3730 }
3731}
3732
3733void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3734 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3735
3736 LocationSummary* locations = op->GetLocations();
3737 Location out = locations->Out();
3738 Location first = locations->InAt(0);
3739 Location second = locations->InAt(1);
3740
3741 Primitive::Type type = op->GetResultType();
3742 switch (type) {
3743 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003744 Register out_reg = out.AsRegister<Register>();
3745 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003746 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003747 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003748 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003749 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003750 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003751 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003752 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003753 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003754 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003755 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003756 }
3757 } else {
3758 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003759 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003760 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003761 __ Mov(out_reg, first_reg);
3762 } else if (op->IsShl()) {
3763 __ Lsl(out_reg, first_reg, shift_value);
3764 } else if (op->IsShr()) {
3765 __ Asr(out_reg, first_reg, shift_value);
3766 } else {
3767 __ Lsr(out_reg, first_reg, shift_value);
3768 }
3769 }
3770 break;
3771 }
3772 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003773 Register o_h = out.AsRegisterPairHigh<Register>();
3774 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003775
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003776 Register high = first.AsRegisterPairHigh<Register>();
3777 Register low = first.AsRegisterPairLow<Register>();
3778
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003779 if (second.IsRegister()) {
3780 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003781
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003782 Register second_reg = second.AsRegister<Register>();
3783
3784 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003785 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003786 // Shift the high part
3787 __ Lsl(o_h, high, o_l);
3788 // Shift the low part and `or` what overflew on the high part
3789 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3790 __ Lsr(temp, low, temp);
3791 __ orr(o_h, o_h, ShifterOperand(temp));
3792 // If the shift is > 32 bits, override the high part
3793 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3794 __ it(PL);
3795 __ Lsl(o_h, low, temp, PL);
3796 // Shift the low part
3797 __ Lsl(o_l, low, o_l);
3798 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003799 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003800 // Shift the low part
3801 __ Lsr(o_l, low, o_h);
3802 // Shift the high part and `or` what underflew on the low part
3803 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3804 __ Lsl(temp, high, temp);
3805 __ orr(o_l, o_l, ShifterOperand(temp));
3806 // If the shift is > 32 bits, override the low part
3807 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3808 __ it(PL);
3809 __ Asr(o_l, high, temp, PL);
3810 // Shift the high part
3811 __ Asr(o_h, high, o_h);
3812 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003813 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003814 // same as Shr except we use `Lsr`s and not `Asr`s
3815 __ Lsr(o_l, low, o_h);
3816 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3817 __ Lsl(temp, high, temp);
3818 __ orr(o_l, o_l, ShifterOperand(temp));
3819 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3820 __ it(PL);
3821 __ Lsr(o_l, high, temp, PL);
3822 __ Lsr(o_h, high, o_h);
3823 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003824 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003825 // Register allocator doesn't create partial overlap.
3826 DCHECK_NE(o_l, high);
3827 DCHECK_NE(o_h, low);
3828 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003829 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003830 if (shift_value > 32) {
3831 if (op->IsShl()) {
3832 __ Lsl(o_h, low, shift_value - 32);
3833 __ LoadImmediate(o_l, 0);
3834 } else if (op->IsShr()) {
3835 __ Asr(o_l, high, shift_value - 32);
3836 __ Asr(o_h, high, 31);
3837 } else {
3838 __ Lsr(o_l, high, shift_value - 32);
3839 __ LoadImmediate(o_h, 0);
3840 }
3841 } else if (shift_value == 32) {
3842 if (op->IsShl()) {
3843 __ mov(o_h, ShifterOperand(low));
3844 __ LoadImmediate(o_l, 0);
3845 } else if (op->IsShr()) {
3846 __ mov(o_l, ShifterOperand(high));
3847 __ Asr(o_h, high, 31);
3848 } else {
3849 __ mov(o_l, ShifterOperand(high));
3850 __ LoadImmediate(o_h, 0);
3851 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003852 } else if (shift_value == 1) {
3853 if (op->IsShl()) {
3854 __ Lsls(o_l, low, 1);
3855 __ adc(o_h, high, ShifterOperand(high));
3856 } else if (op->IsShr()) {
3857 __ Asrs(o_h, high, 1);
3858 __ Rrx(o_l, low);
3859 } else {
3860 __ Lsrs(o_h, high, 1);
3861 __ Rrx(o_l, low);
3862 }
3863 } else {
3864 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003865 if (op->IsShl()) {
3866 __ Lsl(o_h, high, shift_value);
3867 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3868 __ Lsl(o_l, low, shift_value);
3869 } else if (op->IsShr()) {
3870 __ Lsr(o_l, low, shift_value);
3871 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3872 __ Asr(o_h, high, shift_value);
3873 } else {
3874 __ Lsr(o_l, low, shift_value);
3875 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3876 __ Lsr(o_h, high, shift_value);
3877 }
3878 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003879 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003880 break;
3881 }
3882 default:
3883 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003884 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003885 }
3886}
3887
3888void LocationsBuilderARM::VisitShl(HShl* shl) {
3889 HandleShift(shl);
3890}
3891
3892void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3893 HandleShift(shl);
3894}
3895
3896void LocationsBuilderARM::VisitShr(HShr* shr) {
3897 HandleShift(shr);
3898}
3899
3900void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3901 HandleShift(shr);
3902}
3903
3904void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3905 HandleShift(ushr);
3906}
3907
3908void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3909 HandleShift(ushr);
3910}
3911
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003912void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003913 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003914 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003915 if (instruction->IsStringAlloc()) {
3916 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3917 } else {
3918 InvokeRuntimeCallingConvention calling_convention;
3919 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3920 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3921 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003922 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003923}
3924
3925void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003926 // Note: if heap poisoning is enabled, the entry point takes cares
3927 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003928 if (instruction->IsStringAlloc()) {
3929 // String is allocated through StringFactory. Call NewEmptyString entry point.
3930 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003931 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003932 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3933 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3934 __ blx(LR);
3935 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3936 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003937 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003938 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3939 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003940}
3941
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003942void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3943 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003944 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003945 InvokeRuntimeCallingConvention calling_convention;
3946 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003947 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003948 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003949 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003950}
3951
3952void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3953 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003954 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003955 // Note: if heap poisoning is enabled, the entry point takes cares
3956 // of poisoning the reference.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003957 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003958 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003959}
3960
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003961void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003962 LocationSummary* locations =
3963 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003964 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3965 if (location.IsStackSlot()) {
3966 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3967 } else if (location.IsDoubleStackSlot()) {
3968 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003969 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003970 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003971}
3972
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003973void InstructionCodeGeneratorARM::VisitParameterValue(
3974 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003975 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003976}
3977
3978void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3979 LocationSummary* locations =
3980 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3981 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3982}
3983
3984void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3985 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003986}
3987
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003988void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003989 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003990 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003991 locations->SetInAt(0, Location::RequiresRegister());
3992 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003993}
3994
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003995void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3996 LocationSummary* locations = not_->GetLocations();
3997 Location out = locations->Out();
3998 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003999 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004000 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004001 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004002 break;
4003
4004 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004005 __ mvn(out.AsRegisterPairLow<Register>(),
4006 ShifterOperand(in.AsRegisterPairLow<Register>()));
4007 __ mvn(out.AsRegisterPairHigh<Register>(),
4008 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004009 break;
4010
4011 default:
4012 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4013 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004014}
4015
David Brazdil66d126e2015-04-03 16:02:44 +01004016void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4017 LocationSummary* locations =
4018 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4019 locations->SetInAt(0, Location::RequiresRegister());
4020 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4021}
4022
4023void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004024 LocationSummary* locations = bool_not->GetLocations();
4025 Location out = locations->Out();
4026 Location in = locations->InAt(0);
4027 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4028}
4029
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004030void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004031 LocationSummary* locations =
4032 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004033 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004034 case Primitive::kPrimBoolean:
4035 case Primitive::kPrimByte:
4036 case Primitive::kPrimShort:
4037 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004038 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004039 case Primitive::kPrimLong: {
4040 locations->SetInAt(0, Location::RequiresRegister());
4041 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004042 // Output overlaps because it is written before doing the low comparison.
4043 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004044 break;
4045 }
4046 case Primitive::kPrimFloat:
4047 case Primitive::kPrimDouble: {
4048 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004049 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004050 locations->SetOut(Location::RequiresRegister());
4051 break;
4052 }
4053 default:
4054 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4055 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004056}
4057
4058void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004059 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004060 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004061 Location left = locations->InAt(0);
4062 Location right = locations->InAt(1);
4063
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004064 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004065 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004066 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004067 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004068 case Primitive::kPrimBoolean:
4069 case Primitive::kPrimByte:
4070 case Primitive::kPrimShort:
4071 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004072 case Primitive::kPrimInt: {
4073 __ LoadImmediate(out, 0);
4074 __ cmp(left.AsRegister<Register>(),
4075 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4076 less_cond = LT;
4077 break;
4078 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004079 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004080 __ cmp(left.AsRegisterPairHigh<Register>(),
4081 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004082 __ b(&less, LT);
4083 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004084 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004085 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004086 __ cmp(left.AsRegisterPairLow<Register>(),
4087 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004088 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004089 break;
4090 }
4091 case Primitive::kPrimFloat:
4092 case Primitive::kPrimDouble: {
4093 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004094 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004095 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004096 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004097 break;
4098 }
4099 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004100 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004101 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004102 }
Aart Bika19616e2016-02-01 18:57:58 -08004103
Calin Juravleddb7df22014-11-25 20:56:51 +00004104 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004105 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004106
4107 __ Bind(&greater);
4108 __ LoadImmediate(out, 1);
4109 __ b(&done);
4110
4111 __ Bind(&less);
4112 __ LoadImmediate(out, -1);
4113
4114 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004115}
4116
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004117void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004118 LocationSummary* locations =
4119 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004120 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004121 locations->SetInAt(i, Location::Any());
4122 }
4123 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004124}
4125
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004126void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004127 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004128}
4129
Roland Levillainc9285912015-12-18 10:38:42 +00004130void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4131 // TODO (ported from quick): revisit ARM barrier kinds.
4132 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004133 switch (kind) {
4134 case MemBarrierKind::kAnyStore:
4135 case MemBarrierKind::kLoadAny:
4136 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004137 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004138 break;
4139 }
4140 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004141 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004142 break;
4143 }
4144 default:
4145 LOG(FATAL) << "Unexpected memory barrier " << kind;
4146 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004147 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004148}
4149
4150void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4151 uint32_t offset,
4152 Register out_lo,
4153 Register out_hi) {
4154 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004155 // Ensure `out_lo` is different from `addr`, so that loading
4156 // `offset` into `out_lo` does not clutter `addr`.
4157 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004158 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004159 __ add(IP, addr, ShifterOperand(out_lo));
4160 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004161 }
4162 __ ldrexd(out_lo, out_hi, addr);
4163}
4164
4165void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4166 uint32_t offset,
4167 Register value_lo,
4168 Register value_hi,
4169 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004170 Register temp2,
4171 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004172 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004173 if (offset != 0) {
4174 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004175 __ add(IP, addr, ShifterOperand(temp1));
4176 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004177 }
4178 __ Bind(&fail);
4179 // We need a load followed by store. (The address used in a STREX instruction must
4180 // be the same as the address in the most recently executed LDREX instruction.)
4181 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004182 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004183 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004184 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004185}
4186
4187void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4188 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4189
Nicolas Geoffray39468442014-09-02 15:17:15 +01004190 LocationSummary* locations =
4191 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004192 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004193
Calin Juravle52c48962014-12-16 17:02:57 +00004194 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004195 if (Primitive::IsFloatingPointType(field_type)) {
4196 locations->SetInAt(1, Location::RequiresFpuRegister());
4197 } else {
4198 locations->SetInAt(1, Location::RequiresRegister());
4199 }
4200
Calin Juravle52c48962014-12-16 17:02:57 +00004201 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004202 bool generate_volatile = field_info.IsVolatile()
4203 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004204 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004205 bool needs_write_barrier =
4206 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004207 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004208 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004209 if (needs_write_barrier) {
4210 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004211 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004212 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004213 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004214 // - registers need to be consecutive
4215 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004216 // We don't test for ARM yet, and the assertion makes sure that we
4217 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004218 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4219
4220 locations->AddTemp(Location::RequiresRegister());
4221 locations->AddTemp(Location::RequiresRegister());
4222 if (field_type == Primitive::kPrimDouble) {
4223 // For doubles we need two more registers to copy the value.
4224 locations->AddTemp(Location::RegisterLocation(R2));
4225 locations->AddTemp(Location::RegisterLocation(R3));
4226 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004227 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004228}
4229
Calin Juravle52c48962014-12-16 17:02:57 +00004230void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004231 const FieldInfo& field_info,
4232 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004233 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4234
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004235 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004236 Register base = locations->InAt(0).AsRegister<Register>();
4237 Location value = locations->InAt(1);
4238
4239 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004240 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004241 Primitive::Type field_type = field_info.GetFieldType();
4242 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004243 bool needs_write_barrier =
4244 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004245
4246 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004247 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004248 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004249
4250 switch (field_type) {
4251 case Primitive::kPrimBoolean:
4252 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004253 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004254 break;
4255 }
4256
4257 case Primitive::kPrimShort:
4258 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004259 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004260 break;
4261 }
4262
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004263 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004264 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004265 if (kPoisonHeapReferences && needs_write_barrier) {
4266 // Note that in the case where `value` is a null reference,
4267 // we do not enter this block, as a null reference does not
4268 // need poisoning.
4269 DCHECK_EQ(field_type, Primitive::kPrimNot);
4270 Register temp = locations->GetTemp(0).AsRegister<Register>();
4271 __ Mov(temp, value.AsRegister<Register>());
4272 __ PoisonHeapReference(temp);
4273 __ StoreToOffset(kStoreWord, temp, base, offset);
4274 } else {
4275 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4276 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004277 break;
4278 }
4279
4280 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004281 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004282 GenerateWideAtomicStore(base, offset,
4283 value.AsRegisterPairLow<Register>(),
4284 value.AsRegisterPairHigh<Register>(),
4285 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004286 locations->GetTemp(1).AsRegister<Register>(),
4287 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004288 } else {
4289 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004290 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004291 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004292 break;
4293 }
4294
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004295 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004296 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004297 break;
4298 }
4299
4300 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004301 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004302 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004303 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4304 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4305
4306 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4307
4308 GenerateWideAtomicStore(base, offset,
4309 value_reg_lo,
4310 value_reg_hi,
4311 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004312 locations->GetTemp(3).AsRegister<Register>(),
4313 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004314 } else {
4315 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004316 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004317 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004318 break;
4319 }
4320
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004321 case Primitive::kPrimVoid:
4322 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004323 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004324 }
Calin Juravle52c48962014-12-16 17:02:57 +00004325
Calin Juravle77520bc2015-01-12 18:45:46 +00004326 // Longs and doubles are handled in the switch.
4327 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4328 codegen_->MaybeRecordImplicitNullCheck(instruction);
4329 }
4330
4331 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4332 Register temp = locations->GetTemp(0).AsRegister<Register>();
4333 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004334 codegen_->MarkGCCard(
4335 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004336 }
4337
Calin Juravle52c48962014-12-16 17:02:57 +00004338 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004339 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004340 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004341}
4342
Calin Juravle52c48962014-12-16 17:02:57 +00004343void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4344 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004345
4346 bool object_field_get_with_read_barrier =
4347 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004348 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004349 new (GetGraph()->GetArena()) LocationSummary(instruction,
4350 object_field_get_with_read_barrier ?
4351 LocationSummary::kCallOnSlowPath :
4352 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004353 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004354 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004355 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004356 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004357
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004358 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004359 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004360 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004361 // The output overlaps in case of volatile long: we don't want the
4362 // code generated by GenerateWideAtomicLoad to overwrite the
4363 // object's location. Likewise, in the case of an object field get
4364 // with read barriers enabled, we do not want the load to overwrite
4365 // the object's location, as we need it to emit the read barrier.
4366 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4367 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004368
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004369 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4370 locations->SetOut(Location::RequiresFpuRegister());
4371 } else {
4372 locations->SetOut(Location::RequiresRegister(),
4373 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4374 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004375 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004376 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004377 // - registers need to be consecutive
4378 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004379 // We don't test for ARM yet, and the assertion makes sure that we
4380 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004381 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4382 locations->AddTemp(Location::RequiresRegister());
4383 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004384 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4385 // We need a temporary register for the read barrier marking slow
4386 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4387 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004388 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004389}
4390
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004391Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4392 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4393 << input->GetType();
4394 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4395 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4396 return Location::ConstantLocation(input->AsConstant());
4397 } else {
4398 return Location::RequiresFpuRegister();
4399 }
4400}
4401
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004402Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4403 Opcode opcode) {
4404 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4405 if (constant->IsConstant() &&
4406 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4407 return Location::ConstantLocation(constant->AsConstant());
4408 }
4409 return Location::RequiresRegister();
4410}
4411
4412bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4413 Opcode opcode) {
4414 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4415 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004416 Opcode high_opcode = opcode;
4417 SetCc low_set_cc = kCcDontCare;
4418 switch (opcode) {
4419 case SUB:
4420 // Flip the operation to an ADD.
4421 value = -value;
4422 opcode = ADD;
4423 FALLTHROUGH_INTENDED;
4424 case ADD:
4425 if (Low32Bits(value) == 0u) {
4426 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4427 }
4428 high_opcode = ADC;
4429 low_set_cc = kCcSet;
4430 break;
4431 default:
4432 break;
4433 }
4434 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4435 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004436 } else {
4437 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4438 }
4439}
4440
Vladimir Marko59751a72016-08-05 14:37:27 +01004441bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4442 Opcode opcode,
4443 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004444 ShifterOperand so;
4445 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004446 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004447 return true;
4448 }
4449 Opcode neg_opcode = kNoOperand;
4450 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004451 case AND: neg_opcode = BIC; value = ~value; break;
4452 case ORR: neg_opcode = ORN; value = ~value; break;
4453 case ADD: neg_opcode = SUB; value = -value; break;
4454 case ADC: neg_opcode = SBC; value = ~value; break;
4455 case SUB: neg_opcode = ADD; value = -value; break;
4456 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004457 default:
4458 return false;
4459 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004460 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004461}
4462
Calin Juravle52c48962014-12-16 17:02:57 +00004463void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4464 const FieldInfo& field_info) {
4465 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004466
Calin Juravle52c48962014-12-16 17:02:57 +00004467 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004468 Location base_loc = locations->InAt(0);
4469 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004470 Location out = locations->Out();
4471 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004472 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004473 Primitive::Type field_type = field_info.GetFieldType();
4474 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4475
4476 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004477 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004478 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004479 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004480
Roland Levillainc9285912015-12-18 10:38:42 +00004481 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004482 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004483 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484
Roland Levillainc9285912015-12-18 10:38:42 +00004485 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004486 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004487 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004488
Roland Levillainc9285912015-12-18 10:38:42 +00004489 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004490 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004491 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004492
4493 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004494 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004495 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004496
4497 case Primitive::kPrimNot: {
4498 // /* HeapReference<Object> */ out = *(base + offset)
4499 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4500 Location temp_loc = locations->GetTemp(0);
4501 // Note that a potential implicit null check is handled in this
4502 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4503 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4504 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4505 if (is_volatile) {
4506 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4507 }
4508 } else {
4509 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4510 codegen_->MaybeRecordImplicitNullCheck(instruction);
4511 if (is_volatile) {
4512 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4513 }
4514 // If read barriers are enabled, emit read barriers other than
4515 // Baker's using a slow path (and also unpoison the loaded
4516 // reference, if heap poisoning is enabled).
4517 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4518 }
4519 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004520 }
4521
Roland Levillainc9285912015-12-18 10:38:42 +00004522 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004523 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004524 GenerateWideAtomicLoad(base, offset,
4525 out.AsRegisterPairLow<Register>(),
4526 out.AsRegisterPairHigh<Register>());
4527 } else {
4528 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4529 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004530 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004531
Roland Levillainc9285912015-12-18 10:38:42 +00004532 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004533 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004534 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004535
4536 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004537 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004538 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004539 Register lo = locations->GetTemp(0).AsRegister<Register>();
4540 Register hi = locations->GetTemp(1).AsRegister<Register>();
4541 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004542 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004543 __ vmovdrr(out_reg, lo, hi);
4544 } else {
4545 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004546 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004547 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004548 break;
4549 }
4550
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004551 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004552 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004553 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004554 }
Calin Juravle52c48962014-12-16 17:02:57 +00004555
Roland Levillainc9285912015-12-18 10:38:42 +00004556 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4557 // Potential implicit null checks, in the case of reference or
4558 // double fields, are handled in the previous switch statement.
4559 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004560 codegen_->MaybeRecordImplicitNullCheck(instruction);
4561 }
4562
Calin Juravle52c48962014-12-16 17:02:57 +00004563 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004564 if (field_type == Primitive::kPrimNot) {
4565 // Memory barriers, in the case of references, are also handled
4566 // in the previous switch statement.
4567 } else {
4568 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4569 }
Roland Levillain4d027112015-07-01 15:41:14 +01004570 }
Calin Juravle52c48962014-12-16 17:02:57 +00004571}
4572
4573void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4574 HandleFieldSet(instruction, instruction->GetFieldInfo());
4575}
4576
4577void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004578 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004579}
4580
4581void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4582 HandleFieldGet(instruction, instruction->GetFieldInfo());
4583}
4584
4585void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4586 HandleFieldGet(instruction, instruction->GetFieldInfo());
4587}
4588
4589void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4590 HandleFieldGet(instruction, instruction->GetFieldInfo());
4591}
4592
4593void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4594 HandleFieldGet(instruction, instruction->GetFieldInfo());
4595}
4596
4597void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4598 HandleFieldSet(instruction, instruction->GetFieldInfo());
4599}
4600
4601void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004602 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004603}
4604
Calin Juravlee460d1d2015-09-29 04:52:17 +01004605void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4606 HUnresolvedInstanceFieldGet* instruction) {
4607 FieldAccessCallingConventionARM calling_convention;
4608 codegen_->CreateUnresolvedFieldLocationSummary(
4609 instruction, instruction->GetFieldType(), calling_convention);
4610}
4611
4612void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4613 HUnresolvedInstanceFieldGet* instruction) {
4614 FieldAccessCallingConventionARM calling_convention;
4615 codegen_->GenerateUnresolvedFieldAccess(instruction,
4616 instruction->GetFieldType(),
4617 instruction->GetFieldIndex(),
4618 instruction->GetDexPc(),
4619 calling_convention);
4620}
4621
4622void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4623 HUnresolvedInstanceFieldSet* instruction) {
4624 FieldAccessCallingConventionARM calling_convention;
4625 codegen_->CreateUnresolvedFieldLocationSummary(
4626 instruction, instruction->GetFieldType(), calling_convention);
4627}
4628
4629void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4630 HUnresolvedInstanceFieldSet* instruction) {
4631 FieldAccessCallingConventionARM calling_convention;
4632 codegen_->GenerateUnresolvedFieldAccess(instruction,
4633 instruction->GetFieldType(),
4634 instruction->GetFieldIndex(),
4635 instruction->GetDexPc(),
4636 calling_convention);
4637}
4638
4639void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4640 HUnresolvedStaticFieldGet* instruction) {
4641 FieldAccessCallingConventionARM calling_convention;
4642 codegen_->CreateUnresolvedFieldLocationSummary(
4643 instruction, instruction->GetFieldType(), calling_convention);
4644}
4645
4646void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4647 HUnresolvedStaticFieldGet* instruction) {
4648 FieldAccessCallingConventionARM calling_convention;
4649 codegen_->GenerateUnresolvedFieldAccess(instruction,
4650 instruction->GetFieldType(),
4651 instruction->GetFieldIndex(),
4652 instruction->GetDexPc(),
4653 calling_convention);
4654}
4655
4656void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4657 HUnresolvedStaticFieldSet* instruction) {
4658 FieldAccessCallingConventionARM calling_convention;
4659 codegen_->CreateUnresolvedFieldLocationSummary(
4660 instruction, instruction->GetFieldType(), calling_convention);
4661}
4662
4663void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4664 HUnresolvedStaticFieldSet* instruction) {
4665 FieldAccessCallingConventionARM calling_convention;
4666 codegen_->GenerateUnresolvedFieldAccess(instruction,
4667 instruction->GetFieldType(),
4668 instruction->GetFieldIndex(),
4669 instruction->GetDexPc(),
4670 calling_convention);
4671}
4672
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004673void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004674 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4675 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004676}
4677
Calin Juravle2ae48182016-03-16 14:05:09 +00004678void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4679 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004680 return;
4681 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004682 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004683
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004684 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004685 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004686}
4687
Calin Juravle2ae48182016-03-16 14:05:09 +00004688void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004689 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004690 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004691
4692 LocationSummary* locations = instruction->GetLocations();
4693 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004694
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004695 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004696}
4697
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004698void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004699 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004700}
4701
Artem Serov6c916792016-07-11 14:02:34 +01004702static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4703 switch (type) {
4704 case Primitive::kPrimNot:
4705 return kLoadWord;
4706 case Primitive::kPrimBoolean:
4707 return kLoadUnsignedByte;
4708 case Primitive::kPrimByte:
4709 return kLoadSignedByte;
4710 case Primitive::kPrimChar:
4711 return kLoadUnsignedHalfword;
4712 case Primitive::kPrimShort:
4713 return kLoadSignedHalfword;
4714 case Primitive::kPrimInt:
4715 return kLoadWord;
4716 case Primitive::kPrimLong:
4717 return kLoadWordPair;
4718 case Primitive::kPrimFloat:
4719 return kLoadSWord;
4720 case Primitive::kPrimDouble:
4721 return kLoadDWord;
4722 default:
4723 LOG(FATAL) << "Unreachable type " << type;
4724 UNREACHABLE();
4725 }
4726}
4727
4728static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4729 switch (type) {
4730 case Primitive::kPrimNot:
4731 return kStoreWord;
4732 case Primitive::kPrimBoolean:
4733 case Primitive::kPrimByte:
4734 return kStoreByte;
4735 case Primitive::kPrimChar:
4736 case Primitive::kPrimShort:
4737 return kStoreHalfword;
4738 case Primitive::kPrimInt:
4739 return kStoreWord;
4740 case Primitive::kPrimLong:
4741 return kStoreWordPair;
4742 case Primitive::kPrimFloat:
4743 return kStoreSWord;
4744 case Primitive::kPrimDouble:
4745 return kStoreDWord;
4746 default:
4747 LOG(FATAL) << "Unreachable type " << type;
4748 UNREACHABLE();
4749 }
4750}
4751
4752void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4753 Location out_loc,
4754 Register base,
4755 Register reg_offset,
4756 Condition cond) {
4757 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4758 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4759
4760 switch (type) {
4761 case Primitive::kPrimByte:
4762 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4763 break;
4764 case Primitive::kPrimBoolean:
4765 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4766 break;
4767 case Primitive::kPrimShort:
4768 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4769 break;
4770 case Primitive::kPrimChar:
4771 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4772 break;
4773 case Primitive::kPrimNot:
4774 case Primitive::kPrimInt:
4775 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4776 break;
4777 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4778 case Primitive::kPrimLong:
4779 case Primitive::kPrimFloat:
4780 case Primitive::kPrimDouble:
4781 default:
4782 LOG(FATAL) << "Unreachable type " << type;
4783 UNREACHABLE();
4784 }
4785}
4786
4787void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4788 Location loc,
4789 Register base,
4790 Register reg_offset,
4791 Condition cond) {
4792 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4793 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4794
4795 switch (type) {
4796 case Primitive::kPrimByte:
4797 case Primitive::kPrimBoolean:
4798 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4799 break;
4800 case Primitive::kPrimShort:
4801 case Primitive::kPrimChar:
4802 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4803 break;
4804 case Primitive::kPrimNot:
4805 case Primitive::kPrimInt:
4806 __ str(loc.AsRegister<Register>(), mem_address, cond);
4807 break;
4808 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4809 case Primitive::kPrimLong:
4810 case Primitive::kPrimFloat:
4811 case Primitive::kPrimDouble:
4812 default:
4813 LOG(FATAL) << "Unreachable type " << type;
4814 UNREACHABLE();
4815 }
4816}
4817
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004818void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004819 bool object_array_get_with_read_barrier =
4820 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004821 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004822 new (GetGraph()->GetArena()) LocationSummary(instruction,
4823 object_array_get_with_read_barrier ?
4824 LocationSummary::kCallOnSlowPath :
4825 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004826 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004827 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004828 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004829 locations->SetInAt(0, Location::RequiresRegister());
4830 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004831 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4832 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4833 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004834 // The output overlaps in the case of an object array get with
4835 // read barriers enabled: we do not want the move to overwrite the
4836 // array's location, as we need it to emit the read barrier.
4837 locations->SetOut(
4838 Location::RequiresRegister(),
4839 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004840 }
Roland Levillainc9285912015-12-18 10:38:42 +00004841 // We need a temporary register for the read barrier marking slow
4842 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07004843 // Also need for String compression feature.
4844 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4845 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00004846 locations->AddTemp(Location::RequiresRegister());
4847 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004848}
4849
4850void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4851 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004852 Location obj_loc = locations->InAt(0);
4853 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004854 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004855 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004856 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004857 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07004858 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4859 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01004860 HInstruction* array_instr = instruction->GetArray();
4861 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01004862
Roland Levillain4d027112015-07-01 15:41:14 +01004863 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004864 case Primitive::kPrimBoolean:
4865 case Primitive::kPrimByte:
4866 case Primitive::kPrimShort:
4867 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004868 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004869 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004870 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07004871 if (maybe_compressed_char_at) {
4872 Register length = IP;
4873 Label uncompressed_load, done;
4874 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4875 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4876 codegen_->MaybeRecordImplicitNullCheck(instruction);
4877 __ cmp(length, ShifterOperand(0));
4878 __ b(&uncompressed_load, GE);
4879 __ LoadFromOffset(kLoadUnsignedByte,
4880 out_loc.AsRegister<Register>(),
4881 obj,
4882 data_offset + const_index);
4883 __ b(&done);
4884 __ Bind(&uncompressed_load);
4885 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4886 out_loc.AsRegister<Register>(),
4887 obj,
4888 data_offset + (const_index << 1));
4889 __ Bind(&done);
4890 } else {
4891 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01004892
jessicahandojo05765752016-09-09 19:01:32 -07004893 LoadOperandType load_type = GetLoadOperandType(type);
4894 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4895 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004896 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004897 Register temp = IP;
4898
4899 if (has_intermediate_address) {
4900 // We do not need to compute the intermediate address from the array: the
4901 // input instruction has done it already. See the comment in
4902 // `TryExtractArrayAccessAddress()`.
4903 if (kIsDebugBuild) {
4904 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4905 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4906 }
4907 temp = obj;
4908 } else {
4909 __ add(temp, obj, ShifterOperand(data_offset));
4910 }
jessicahandojo05765752016-09-09 19:01:32 -07004911 if (maybe_compressed_char_at) {
4912 Label uncompressed_load, done;
4913 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4914 Register length = locations->GetTemp(0).AsRegister<Register>();
4915 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4916 codegen_->MaybeRecordImplicitNullCheck(instruction);
4917 __ cmp(length, ShifterOperand(0));
4918 __ b(&uncompressed_load, GE);
4919 __ ldrb(out_loc.AsRegister<Register>(),
4920 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
4921 __ b(&done);
4922 __ Bind(&uncompressed_load);
4923 __ ldrh(out_loc.AsRegister<Register>(),
4924 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
4925 __ Bind(&done);
4926 } else {
4927 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4928 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004929 }
4930 break;
4931 }
4932
Roland Levillainc9285912015-12-18 10:38:42 +00004933 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00004934 // The read barrier instrumentation of object ArrayGet
4935 // instructions does not support the HIntermediateAddress
4936 // instruction.
4937 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4938
Roland Levillainc9285912015-12-18 10:38:42 +00004939 static_assert(
4940 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4941 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004942 // /* HeapReference<Object> */ out =
4943 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4944 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4945 Location temp = locations->GetTemp(0);
4946 // Note that a potential implicit null check is handled in this
4947 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4948 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4949 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4950 } else {
4951 Register out = out_loc.AsRegister<Register>();
4952 if (index.IsConstant()) {
4953 size_t offset =
4954 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4955 __ LoadFromOffset(kLoadWord, out, obj, offset);
4956 codegen_->MaybeRecordImplicitNullCheck(instruction);
4957 // If read barriers are enabled, emit read barriers other than
4958 // Baker's using a slow path (and also unpoison the loaded
4959 // reference, if heap poisoning is enabled).
4960 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4961 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004962 Register temp = IP;
4963
4964 if (has_intermediate_address) {
4965 // We do not need to compute the intermediate address from the array: the
4966 // input instruction has done it already. See the comment in
4967 // `TryExtractArrayAccessAddress()`.
4968 if (kIsDebugBuild) {
4969 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4970 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4971 }
4972 temp = obj;
4973 } else {
4974 __ add(temp, obj, ShifterOperand(data_offset));
4975 }
4976 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004977
Roland Levillainc9285912015-12-18 10:38:42 +00004978 codegen_->MaybeRecordImplicitNullCheck(instruction);
4979 // If read barriers are enabled, emit read barriers other than
4980 // Baker's using a slow path (and also unpoison the loaded
4981 // reference, if heap poisoning is enabled).
4982 codegen_->MaybeGenerateReadBarrierSlow(
4983 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4984 }
4985 }
4986 break;
4987 }
4988
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004989 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004990 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004991 size_t offset =
4992 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004993 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004994 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004995 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004996 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004997 }
4998 break;
4999 }
5000
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005001 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005002 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005003 if (index.IsConstant()) {
5004 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005005 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005006 } else {
5007 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005008 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005009 }
5010 break;
5011 }
5012
5013 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005014 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005015 if (index.IsConstant()) {
5016 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005017 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005018 } else {
5019 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005020 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005021 }
5022 break;
5023 }
5024
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005025 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005026 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005027 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005028 }
Roland Levillain4d027112015-07-01 15:41:14 +01005029
5030 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005031 // Potential implicit null checks, in the case of reference
5032 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005033 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005034 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005035 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005036}
5037
5038void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005039 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005040
5041 bool needs_write_barrier =
5042 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005043 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005044
Nicolas Geoffray39468442014-09-02 15:17:15 +01005045 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005046 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005047 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005048 LocationSummary::kCallOnSlowPath :
5049 LocationSummary::kNoCall);
5050
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005051 locations->SetInAt(0, Location::RequiresRegister());
5052 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5053 if (Primitive::IsFloatingPointType(value_type)) {
5054 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005055 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005056 locations->SetInAt(2, Location::RequiresRegister());
5057 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005058 if (needs_write_barrier) {
5059 // Temporary registers for the write barrier.
5060 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005061 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005062 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005063}
5064
5065void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5066 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005067 Location array_loc = locations->InAt(0);
5068 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005070 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005071 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005072 bool needs_write_barrier =
5073 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005074 uint32_t data_offset =
5075 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5076 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005077 HInstruction* array_instr = instruction->GetArray();
5078 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005079
5080 switch (value_type) {
5081 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005082 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005084 case Primitive::kPrimChar:
5085 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005086 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005087 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5088 uint32_t full_offset =
5089 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5090 StoreOperandType store_type = GetStoreOperandType(value_type);
5091 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005092 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005093 Register temp = IP;
5094
5095 if (has_intermediate_address) {
5096 // We do not need to compute the intermediate address from the array: the
5097 // input instruction has done it already. See the comment in
5098 // `TryExtractArrayAccessAddress()`.
5099 if (kIsDebugBuild) {
5100 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5101 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5102 }
5103 temp = array;
5104 } else {
5105 __ add(temp, array, ShifterOperand(data_offset));
5106 }
Artem Serov6c916792016-07-11 14:02:34 +01005107 codegen_->StoreToShiftedRegOffset(value_type,
5108 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005109 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005110 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005111 }
5112 break;
5113 }
5114
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005115 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005116 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005117 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5118 // See the comment in instruction_simplifier_shared.cc.
5119 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005120
5121 if (instruction->InputAt(2)->IsNullConstant()) {
5122 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005123 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005124 size_t offset =
5125 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005126 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005127 } else {
5128 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005129 __ add(IP, array, ShifterOperand(data_offset));
5130 codegen_->StoreToShiftedRegOffset(value_type,
5131 value_loc,
5132 IP,
5133 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005134 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005135 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005136 DCHECK(!needs_write_barrier);
5137 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005138 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005139 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005140
5141 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005142 Location temp1_loc = locations->GetTemp(0);
5143 Register temp1 = temp1_loc.AsRegister<Register>();
5144 Location temp2_loc = locations->GetTemp(1);
5145 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005146 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5147 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5148 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5149 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005150 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005151
Roland Levillain3b359c72015-11-17 19:35:12 +00005152 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005153 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5154 codegen_->AddSlowPath(slow_path);
5155 if (instruction->GetValueCanBeNull()) {
5156 Label non_zero;
5157 __ CompareAndBranchIfNonZero(value, &non_zero);
5158 if (index.IsConstant()) {
5159 size_t offset =
5160 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5161 __ StoreToOffset(kStoreWord, value, array, offset);
5162 } else {
5163 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005164 __ add(IP, array, ShifterOperand(data_offset));
5165 codegen_->StoreToShiftedRegOffset(value_type,
5166 value_loc,
5167 IP,
5168 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005169 }
5170 codegen_->MaybeRecordImplicitNullCheck(instruction);
5171 __ b(&done);
5172 __ Bind(&non_zero);
5173 }
5174
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005175 // Note that when read barriers are enabled, the type checks
5176 // are performed without read barriers. This is fine, even in
5177 // the case where a class object is in the from-space after
5178 // the flip, as a comparison involving such a type would not
5179 // produce a false positive; it may of course produce a false
5180 // negative, in which case we would take the ArraySet slow
5181 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005182
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005183 // /* HeapReference<Class> */ temp1 = array->klass_
5184 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5185 codegen_->MaybeRecordImplicitNullCheck(instruction);
5186 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005187
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005188 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5189 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5190 // /* HeapReference<Class> */ temp2 = value->klass_
5191 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5192 // If heap poisoning is enabled, no need to unpoison `temp1`
5193 // nor `temp2`, as we are comparing two poisoned references.
5194 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005195
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005196 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5197 Label do_put;
5198 __ b(&do_put, EQ);
5199 // If heap poisoning is enabled, the `temp1` reference has
5200 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005201 __ MaybeUnpoisonHeapReference(temp1);
5202
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005203 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5204 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5205 // If heap poisoning is enabled, no need to unpoison
5206 // `temp1`, as we are comparing against null below.
5207 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5208 __ Bind(&do_put);
5209 } else {
5210 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005211 }
5212 }
5213
Artem Serov6c916792016-07-11 14:02:34 +01005214 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005215 if (kPoisonHeapReferences) {
5216 // Note that in the case where `value` is a null reference,
5217 // we do not enter this block, as a null reference does not
5218 // need poisoning.
5219 DCHECK_EQ(value_type, Primitive::kPrimNot);
5220 __ Mov(temp1, value);
5221 __ PoisonHeapReference(temp1);
5222 source = temp1;
5223 }
5224
5225 if (index.IsConstant()) {
5226 size_t offset =
5227 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5228 __ StoreToOffset(kStoreWord, source, array, offset);
5229 } else {
5230 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005231
5232 __ add(IP, array, ShifterOperand(data_offset));
5233 codegen_->StoreToShiftedRegOffset(value_type,
5234 Location::RegisterLocation(source),
5235 IP,
5236 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005237 }
5238
Roland Levillain3b359c72015-11-17 19:35:12 +00005239 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005240 codegen_->MaybeRecordImplicitNullCheck(instruction);
5241 }
5242
5243 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5244
5245 if (done.IsLinked()) {
5246 __ Bind(&done);
5247 }
5248
5249 if (slow_path != nullptr) {
5250 __ Bind(slow_path->GetExitLabel());
5251 }
5252
5253 break;
5254 }
5255
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005256 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005257 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005258 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005259 size_t offset =
5260 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005261 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005262 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005263 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005264 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005265 }
5266 break;
5267 }
5268
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005269 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005270 Location value = locations->InAt(2);
5271 DCHECK(value.IsFpuRegister());
5272 if (index.IsConstant()) {
5273 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005274 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005275 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005276 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005277 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5278 }
5279 break;
5280 }
5281
5282 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005283 Location value = locations->InAt(2);
5284 DCHECK(value.IsFpuRegisterPair());
5285 if (index.IsConstant()) {
5286 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005287 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005288 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005289 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005290 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5291 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005292
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005293 break;
5294 }
5295
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005296 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005297 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005298 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005299 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005300
Roland Levillain80e67092016-01-08 16:04:55 +00005301 // Objects are handled in the switch.
5302 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005303 codegen_->MaybeRecordImplicitNullCheck(instruction);
5304 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305}
5306
5307void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005308 LocationSummary* locations =
5309 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005310 locations->SetInAt(0, Location::RequiresRegister());
5311 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005312}
5313
5314void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5315 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005316 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005317 Register obj = locations->InAt(0).AsRegister<Register>();
5318 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005319 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005320 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005321 // Mask out compression flag from String's array length.
5322 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
5323 __ bic(out, out, ShifterOperand(1u << 31));
5324 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005325}
5326
Artem Serov328429f2016-07-06 16:23:04 +01005327void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005328 LocationSummary* locations =
5329 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5330
5331 locations->SetInAt(0, Location::RequiresRegister());
5332 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5333 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5334}
5335
5336void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5337 LocationSummary* locations = instruction->GetLocations();
5338 Location out = locations->Out();
5339 Location first = locations->InAt(0);
5340 Location second = locations->InAt(1);
5341
Artem Serov328429f2016-07-06 16:23:04 +01005342 if (second.IsRegister()) {
5343 __ add(out.AsRegister<Register>(),
5344 first.AsRegister<Register>(),
5345 ShifterOperand(second.AsRegister<Register>()));
5346 } else {
5347 __ AddConstant(out.AsRegister<Register>(),
5348 first.AsRegister<Register>(),
5349 second.GetConstant()->AsIntConstant()->GetValue());
5350 }
5351}
5352
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005354 RegisterSet caller_saves = RegisterSet::Empty();
5355 InvokeRuntimeCallingConvention calling_convention;
5356 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5357 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5358 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005359 locations->SetInAt(0, Location::RequiresRegister());
5360 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361}
5362
5363void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5364 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005365 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005366 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005367 codegen_->AddSlowPath(slow_path);
5368
Roland Levillain271ab9c2014-11-27 15:23:57 +00005369 Register index = locations->InAt(0).AsRegister<Register>();
5370 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005371
5372 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005373 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374}
5375
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005376void CodeGeneratorARM::MarkGCCard(Register temp,
5377 Register card,
5378 Register object,
5379 Register value,
5380 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005381 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005382 if (can_be_null) {
5383 __ CompareAndBranchIfZero(value, &is_null);
5384 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005385 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5387 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005388 if (can_be_null) {
5389 __ Bind(&is_null);
5390 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005391}
5392
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005393void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005394 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005395}
5396
5397void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005398 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5399}
5400
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005401void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005402 LocationSummary* locations =
5403 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005404 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005405}
5406
5407void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005408 HBasicBlock* block = instruction->GetBlock();
5409 if (block->GetLoopInformation() != nullptr) {
5410 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5411 // The back edge will generate the suspend check.
5412 return;
5413 }
5414 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5415 // The goto will generate the suspend check.
5416 return;
5417 }
5418 GenerateSuspendCheck(instruction, nullptr);
5419}
5420
5421void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5422 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005423 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005424 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5425 if (slow_path == nullptr) {
5426 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5427 instruction->SetSlowPath(slow_path);
5428 codegen_->AddSlowPath(slow_path);
5429 if (successor != nullptr) {
5430 DCHECK(successor->IsLoopHeader());
5431 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5432 }
5433 } else {
5434 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5435 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005436
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005437 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005438 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005439 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005440 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005441 __ Bind(slow_path->GetReturnLabel());
5442 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005443 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005444 __ b(slow_path->GetEntryLabel());
5445 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005446}
5447
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005448ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5449 return codegen_->GetAssembler();
5450}
5451
5452void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005453 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005454 Location source = move->GetSource();
5455 Location destination = move->GetDestination();
5456
5457 if (source.IsRegister()) {
5458 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005459 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005460 } else if (destination.IsFpuRegister()) {
5461 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005462 } else {
5463 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005464 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005465 SP, destination.GetStackIndex());
5466 }
5467 } else if (source.IsStackSlot()) {
5468 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005469 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005470 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005471 } else if (destination.IsFpuRegister()) {
5472 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005473 } else {
5474 DCHECK(destination.IsStackSlot());
5475 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5476 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5477 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005478 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005479 if (destination.IsRegister()) {
5480 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5481 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005482 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005483 } else {
5484 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005485 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5486 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005487 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005488 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005489 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5490 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005491 } else if (destination.IsRegisterPair()) {
5492 DCHECK(ExpectedPairLayout(destination));
5493 __ LoadFromOffset(
5494 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5495 } else {
5496 DCHECK(destination.IsFpuRegisterPair()) << destination;
5497 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5498 SP,
5499 source.GetStackIndex());
5500 }
5501 } else if (source.IsRegisterPair()) {
5502 if (destination.IsRegisterPair()) {
5503 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5504 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005505 } else if (destination.IsFpuRegisterPair()) {
5506 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5507 source.AsRegisterPairLow<Register>(),
5508 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005509 } else {
5510 DCHECK(destination.IsDoubleStackSlot()) << destination;
5511 DCHECK(ExpectedPairLayout(source));
5512 __ StoreToOffset(
5513 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5514 }
5515 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005516 if (destination.IsRegisterPair()) {
5517 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5518 destination.AsRegisterPairHigh<Register>(),
5519 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5520 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005521 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5522 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5523 } else {
5524 DCHECK(destination.IsDoubleStackSlot()) << destination;
5525 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5526 SP,
5527 destination.GetStackIndex());
5528 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005529 } else {
5530 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005531 HConstant* constant = source.GetConstant();
5532 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5533 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005534 if (destination.IsRegister()) {
5535 __ LoadImmediate(destination.AsRegister<Register>(), value);
5536 } else {
5537 DCHECK(destination.IsStackSlot());
5538 __ LoadImmediate(IP, value);
5539 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5540 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005541 } else if (constant->IsLongConstant()) {
5542 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005543 if (destination.IsRegisterPair()) {
5544 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5545 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005546 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005547 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005548 __ LoadImmediate(IP, Low32Bits(value));
5549 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5550 __ LoadImmediate(IP, High32Bits(value));
5551 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5552 }
5553 } else if (constant->IsDoubleConstant()) {
5554 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005555 if (destination.IsFpuRegisterPair()) {
5556 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005557 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005558 DCHECK(destination.IsDoubleStackSlot()) << destination;
5559 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005560 __ LoadImmediate(IP, Low32Bits(int_value));
5561 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5562 __ LoadImmediate(IP, High32Bits(int_value));
5563 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5564 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005565 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005566 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005567 float value = constant->AsFloatConstant()->GetValue();
5568 if (destination.IsFpuRegister()) {
5569 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5570 } else {
5571 DCHECK(destination.IsStackSlot());
5572 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5573 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5574 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005575 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005576 }
5577}
5578
5579void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5580 __ Mov(IP, reg);
5581 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5582 __ StoreToOffset(kStoreWord, IP, SP, mem);
5583}
5584
5585void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5586 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5587 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5588 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5589 SP, mem1 + stack_offset);
5590 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5591 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5592 SP, mem2 + stack_offset);
5593 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5594}
5595
5596void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005597 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005598 Location source = move->GetSource();
5599 Location destination = move->GetDestination();
5600
5601 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005602 DCHECK_NE(source.AsRegister<Register>(), IP);
5603 DCHECK_NE(destination.AsRegister<Register>(), IP);
5604 __ Mov(IP, source.AsRegister<Register>());
5605 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5606 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005607 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005608 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005609 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005610 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005611 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5612 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005613 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005614 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005615 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005616 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005617 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005618 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005619 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005620 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005621 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5622 destination.AsRegisterPairHigh<Register>(),
5623 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005624 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005625 Register low_reg = source.IsRegisterPair()
5626 ? source.AsRegisterPairLow<Register>()
5627 : destination.AsRegisterPairLow<Register>();
5628 int mem = source.IsRegisterPair()
5629 ? destination.GetStackIndex()
5630 : source.GetStackIndex();
5631 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005632 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005633 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005634 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005635 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005636 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5637 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005638 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005639 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005640 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005641 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5642 DRegister reg = source.IsFpuRegisterPair()
5643 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5644 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5645 int mem = source.IsFpuRegisterPair()
5646 ? destination.GetStackIndex()
5647 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005648 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005649 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005650 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005651 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5652 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5653 : destination.AsFpuRegister<SRegister>();
5654 int mem = source.IsFpuRegister()
5655 ? destination.GetStackIndex()
5656 : source.GetStackIndex();
5657
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005658 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005659 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005660 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005661 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005662 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5663 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005664 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005665 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005666 }
5667}
5668
5669void ParallelMoveResolverARM::SpillScratch(int reg) {
5670 __ Push(static_cast<Register>(reg));
5671}
5672
5673void ParallelMoveResolverARM::RestoreScratch(int reg) {
5674 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005675}
5676
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005677HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5678 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005679 switch (desired_class_load_kind) {
5680 case HLoadClass::LoadKind::kReferrersClass:
5681 break;
5682 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5683 DCHECK(!GetCompilerOptions().GetCompilePic());
5684 break;
5685 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5686 DCHECK(GetCompilerOptions().GetCompilePic());
5687 break;
5688 case HLoadClass::LoadKind::kBootImageAddress:
5689 break;
5690 case HLoadClass::LoadKind::kDexCacheAddress:
5691 DCHECK(Runtime::Current()->UseJitCompilation());
5692 break;
5693 case HLoadClass::LoadKind::kDexCachePcRelative:
5694 DCHECK(!Runtime::Current()->UseJitCompilation());
5695 // We disable pc-relative load when there is an irreducible loop, as the optimization
5696 // is incompatible with it.
5697 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5698 // with irreducible loops.
5699 if (GetGraph()->HasIrreducibleLoops()) {
5700 return HLoadClass::LoadKind::kDexCacheViaMethod;
5701 }
5702 break;
5703 case HLoadClass::LoadKind::kDexCacheViaMethod:
5704 break;
5705 }
5706 return desired_class_load_kind;
5707}
5708
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005709void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005710 if (cls->NeedsAccessCheck()) {
5711 InvokeRuntimeCallingConvention calling_convention;
5712 CodeGenerator::CreateLoadClassLocationSummary(
5713 cls,
5714 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5715 Location::RegisterLocation(R0),
5716 /* code_generator_supports_read_barrier */ true);
5717 return;
5718 }
5719
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005720 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5721 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005722 ? LocationSummary::kCallOnSlowPath
5723 : LocationSummary::kNoCall;
5724 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005725 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005726 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005727 }
5728
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005729 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5730 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5731 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5732 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5733 locations->SetInAt(0, Location::RequiresRegister());
5734 }
5735 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005736}
5737
5738void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005739 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005740 if (cls->NeedsAccessCheck()) {
5741 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005742 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005743 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005744 return;
5745 }
5746
Roland Levillain3b359c72015-11-17 19:35:12 +00005747 Location out_loc = locations->Out();
5748 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005749
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005750 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005751 bool generate_null_check = false;
5752 switch (cls->GetLoadKind()) {
5753 case HLoadClass::LoadKind::kReferrersClass: {
5754 DCHECK(!cls->CanCallRuntime());
5755 DCHECK(!cls->MustGenerateClinitCheck());
5756 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5757 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005758 GenerateGcRootFieldLoad(cls,
5759 out_loc,
5760 current_method,
5761 ArtMethod::DeclaringClassOffset().Int32Value(),
5762 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005763 break;
5764 }
5765 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005766 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005767 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5768 cls->GetTypeIndex()));
5769 break;
5770 }
5771 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005772 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005773 CodeGeneratorARM::PcRelativePatchInfo* labels =
5774 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5775 __ BindTrackedLabel(&labels->movw_label);
5776 __ movw(out, /* placeholder */ 0u);
5777 __ BindTrackedLabel(&labels->movt_label);
5778 __ movt(out, /* placeholder */ 0u);
5779 __ BindTrackedLabel(&labels->add_pc_label);
5780 __ add(out, out, ShifterOperand(PC));
5781 break;
5782 }
5783 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005784 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005785 DCHECK_NE(cls->GetAddress(), 0u);
5786 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5787 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5788 break;
5789 }
5790 case HLoadClass::LoadKind::kDexCacheAddress: {
5791 DCHECK_NE(cls->GetAddress(), 0u);
5792 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5793 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5794 // a 128B range. To try and reduce the number of literals if we load multiple types,
5795 // simply split the dex cache address to a 128B aligned base loaded from a literal
5796 // and the remaining offset embedded in the load.
5797 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5798 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5799 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5800 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5801 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5802 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5803 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005804 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005805 generate_null_check = !cls->IsInDexCache();
5806 break;
5807 }
5808 case HLoadClass::LoadKind::kDexCachePcRelative: {
5809 Register base_reg = locations->InAt(0).AsRegister<Register>();
5810 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5811 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5812 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005813 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005814 generate_null_check = !cls->IsInDexCache();
5815 break;
5816 }
5817 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5818 // /* GcRoot<mirror::Class>[] */ out =
5819 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5820 Register current_method = locations->InAt(0).AsRegister<Register>();
5821 __ LoadFromOffset(kLoadWord,
5822 out,
5823 current_method,
5824 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5825 // /* GcRoot<mirror::Class> */ out = out[type_index]
5826 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005827 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005828 generate_null_check = !cls->IsInDexCache();
5829 }
5830 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005831
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005832 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5833 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005834 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005835 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5836 codegen_->AddSlowPath(slow_path);
5837 if (generate_null_check) {
5838 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5839 }
5840 if (cls->MustGenerateClinitCheck()) {
5841 GenerateClassInitializationCheck(slow_path, out);
5842 } else {
5843 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005844 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005845 }
5846}
5847
5848void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5849 LocationSummary* locations =
5850 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5851 locations->SetInAt(0, Location::RequiresRegister());
5852 if (check->HasUses()) {
5853 locations->SetOut(Location::SameAsFirstInput());
5854 }
5855}
5856
5857void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005858 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005859 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005860 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005861 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005862 GenerateClassInitializationCheck(slow_path,
5863 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005864}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005865
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005866void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005867 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005868 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5869 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5870 __ b(slow_path->GetEntryLabel(), LT);
5871 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5872 // properly. Therefore, we do a memory fence.
5873 __ dmb(ISH);
5874 __ Bind(slow_path->GetExitLabel());
5875}
5876
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005877HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5878 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005879 switch (desired_string_load_kind) {
5880 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5881 DCHECK(!GetCompilerOptions().GetCompilePic());
5882 break;
5883 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5884 DCHECK(GetCompilerOptions().GetCompilePic());
5885 break;
5886 case HLoadString::LoadKind::kBootImageAddress:
5887 break;
5888 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005889 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005890 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005891 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005892 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005893 break;
Nicolas Geoffrayac3ebc32016-10-05 13:13:50 +01005894 case HLoadString::LoadKind::kJitTableAddress:
5895 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray997d1212016-11-09 10:36:29 +00005896 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005897 case HLoadString::LoadKind::kDexCacheViaMethod:
5898 break;
5899 }
5900 return desired_string_load_kind;
5901}
5902
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005903void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005904 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00005905 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5906 ? LocationSummary::kCallOnMainOnly
5907 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005908 : LocationSummary::kNoCall;
5909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005910
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005911 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005912 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005913 locations->SetOut(Location::RegisterLocation(R0));
5914 } else {
5915 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005916 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5917 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5918 // Rely on the pResolveString and/or marking to save everything, including temps.
5919 // Note that IP may theoretically be clobbered by saving/restoring the live register
5920 // (only one thanks to the custom calling convention), so we request a different temp.
5921 locations->AddTemp(Location::RequiresRegister());
5922 RegisterSet caller_saves = RegisterSet::Empty();
5923 InvokeRuntimeCallingConvention calling_convention;
5924 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5925 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5926 // that the the kPrimNot result register is the same as the first argument register.
5927 locations->SetCustomSlowPathCallerSaves(caller_saves);
5928 } else {
5929 // For non-Baker read barrier we have a temp-clobbering call.
5930 }
5931 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005932 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005933}
5934
5935void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005936 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005937 Location out_loc = locations->Out();
5938 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005939 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005940
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005941 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005942 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005943 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5944 load->GetStringIndex()));
5945 return; // No dex cache slow path.
5946 }
5947 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005948 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005949 CodeGeneratorARM::PcRelativePatchInfo* labels =
5950 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5951 __ BindTrackedLabel(&labels->movw_label);
5952 __ movw(out, /* placeholder */ 0u);
5953 __ BindTrackedLabel(&labels->movt_label);
5954 __ movt(out, /* placeholder */ 0u);
5955 __ BindTrackedLabel(&labels->add_pc_label);
5956 __ add(out, out, ShifterOperand(PC));
5957 return; // No dex cache slow path.
5958 }
5959 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005960 DCHECK_NE(load->GetAddress(), 0u);
5961 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5962 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5963 return; // No dex cache slow path.
5964 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005965 case HLoadString::LoadKind::kBssEntry: {
5966 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005967 Register temp = locations->GetTemp(0).AsRegister<Register>();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005968 CodeGeneratorARM::PcRelativePatchInfo* labels =
5969 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5970 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005971 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005972 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005973 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005974 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005975 __ add(temp, temp, ShifterOperand(PC));
Roland Levillain00468f32016-10-27 18:02:48 +01005976 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005977 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5978 codegen_->AddSlowPath(slow_path);
5979 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5980 __ Bind(slow_path->GetExitLabel());
5981 return;
5982 }
Nicolas Geoffray997d1212016-11-09 10:36:29 +00005983 case HLoadString::LoadKind::kJitTableAddress: {
5984 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
5985 load->GetStringIndex()));
5986 // /* GcRoot<mirror::String> */ out = *out
5987 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kEmitCompilerReadBarrier);
5988 return;
5989 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005990 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005991 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005992 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005993
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005994 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
5995 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5996 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005997 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005998 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5999 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6000 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006001}
6002
David Brazdilcb1c0552015-08-04 16:22:25 +01006003static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006004 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006005}
6006
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006007void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6008 LocationSummary* locations =
6009 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6010 locations->SetOut(Location::RequiresRegister());
6011}
6012
6013void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006014 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006015 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6016}
6017
6018void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6019 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6020}
6021
6022void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006023 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006024 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006025}
6026
6027void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6028 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006029 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006030 InvokeRuntimeCallingConvention calling_convention;
6031 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6032}
6033
6034void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006035 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006036 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006037}
6038
Roland Levillainc9285912015-12-18 10:38:42 +00006039static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6040 return kEmitCompilerReadBarrier &&
6041 (kUseBakerReadBarrier ||
6042 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6043 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6044 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6045}
6046
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006047void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006048 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006049 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006050 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006051 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006052 case TypeCheckKind::kExactCheck:
6053 case TypeCheckKind::kAbstractClassCheck:
6054 case TypeCheckKind::kClassHierarchyCheck:
6055 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006056 call_kind =
6057 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006058 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006059 break;
6060 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006061 case TypeCheckKind::kUnresolvedCheck:
6062 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006063 call_kind = LocationSummary::kCallOnSlowPath;
6064 break;
6065 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006066
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006067 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006068 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006069 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006070 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006071 locations->SetInAt(0, Location::RequiresRegister());
6072 locations->SetInAt(1, Location::RequiresRegister());
6073 // The "out" register is used as a temporary, so it overlaps with the inputs.
6074 // Note that TypeCheckSlowPathARM uses this register too.
6075 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
6076 // When read barriers are enabled, we need a temporary register for
6077 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00006078 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006079 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006080 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006081}
6082
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006083void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006084 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006085 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006086 Location obj_loc = locations->InAt(0);
6087 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006088 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006089 Location out_loc = locations->Out();
6090 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006091 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00006092 locations->GetTemp(0) :
6093 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006094 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006095 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6096 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6097 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006098 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006099 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006100
6101 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006102 // avoid null check if we know obj is not null.
6103 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006104 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006105 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006106
Roland Levillain3b359c72015-11-17 19:35:12 +00006107 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006108 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006109
Roland Levillainc9285912015-12-18 10:38:42 +00006110 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006111 case TypeCheckKind::kExactCheck: {
6112 __ cmp(out, ShifterOperand(cls));
6113 // Classes must be equal for the instanceof to succeed.
6114 __ b(&zero, NE);
6115 __ LoadImmediate(out, 1);
6116 __ b(&done);
6117 break;
6118 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006119
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006120 case TypeCheckKind::kAbstractClassCheck: {
6121 // If the class is abstract, we eagerly fetch the super class of the
6122 // object to avoid doing a comparison we know will fail.
6123 Label loop;
6124 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006125 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006126 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006127 // If `out` is null, we use it for the result, and jump to `done`.
6128 __ CompareAndBranchIfZero(out, &done);
6129 __ cmp(out, ShifterOperand(cls));
6130 __ b(&loop, NE);
6131 __ LoadImmediate(out, 1);
6132 if (zero.IsLinked()) {
6133 __ b(&done);
6134 }
6135 break;
6136 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006137
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006138 case TypeCheckKind::kClassHierarchyCheck: {
6139 // Walk over the class hierarchy to find a match.
6140 Label loop, success;
6141 __ Bind(&loop);
6142 __ cmp(out, ShifterOperand(cls));
6143 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006144 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006145 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006146 __ CompareAndBranchIfNonZero(out, &loop);
6147 // If `out` is null, we use it for the result, and jump to `done`.
6148 __ b(&done);
6149 __ Bind(&success);
6150 __ LoadImmediate(out, 1);
6151 if (zero.IsLinked()) {
6152 __ b(&done);
6153 }
6154 break;
6155 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006156
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006157 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006158 // Do an exact check.
6159 Label exact_check;
6160 __ cmp(out, ShifterOperand(cls));
6161 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006162 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006163 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006164 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 // If `out` is null, we use it for the result, and jump to `done`.
6166 __ CompareAndBranchIfZero(out, &done);
6167 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6168 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6169 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006170 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006171 __ LoadImmediate(out, 1);
6172 __ b(&done);
6173 break;
6174 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006175
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006176 case TypeCheckKind::kArrayCheck: {
6177 __ cmp(out, ShifterOperand(cls));
6178 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006179 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6180 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006181 codegen_->AddSlowPath(slow_path);
6182 __ b(slow_path->GetEntryLabel(), NE);
6183 __ LoadImmediate(out, 1);
6184 if (zero.IsLinked()) {
6185 __ b(&done);
6186 }
6187 break;
6188 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006189
Calin Juravle98893e12015-10-02 21:05:03 +01006190 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006191 case TypeCheckKind::kInterfaceCheck: {
6192 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006193 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006194 // cases.
6195 //
6196 // We cannot directly call the InstanceofNonTrivial runtime
6197 // entry point without resorting to a type checking slow path
6198 // here (i.e. by calling InvokeRuntime directly), as it would
6199 // require to assign fixed registers for the inputs of this
6200 // HInstanceOf instruction (following the runtime calling
6201 // convention), which might be cluttered by the potential first
6202 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006203 //
6204 // TODO: Introduce a new runtime entry point taking the object
6205 // to test (instead of its class) as argument, and let it deal
6206 // with the read barrier issues. This will let us refactor this
6207 // case of the `switch` code as it was previously (with a direct
6208 // call to the runtime not using a type checking slow path).
6209 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006210 DCHECK(locations->OnlyCallsOnSlowPath());
6211 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6212 /* is_fatal */ false);
6213 codegen_->AddSlowPath(slow_path);
6214 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006215 if (zero.IsLinked()) {
6216 __ b(&done);
6217 }
6218 break;
6219 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006220 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006221
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006222 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006223 __ Bind(&zero);
6224 __ LoadImmediate(out, 0);
6225 }
6226
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006227 if (done.IsLinked()) {
6228 __ Bind(&done);
6229 }
6230
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006231 if (slow_path != nullptr) {
6232 __ Bind(slow_path->GetExitLabel());
6233 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006234}
6235
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006236void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006237 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6238 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6239
Roland Levillain3b359c72015-11-17 19:35:12 +00006240 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6241 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006242 case TypeCheckKind::kExactCheck:
6243 case TypeCheckKind::kAbstractClassCheck:
6244 case TypeCheckKind::kClassHierarchyCheck:
6245 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006246 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6247 LocationSummary::kCallOnSlowPath :
6248 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006249 break;
6250 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006251 case TypeCheckKind::kUnresolvedCheck:
6252 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006253 call_kind = LocationSummary::kCallOnSlowPath;
6254 break;
6255 }
6256
Roland Levillain3b359c72015-11-17 19:35:12 +00006257 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6258 locations->SetInAt(0, Location::RequiresRegister());
6259 locations->SetInAt(1, Location::RequiresRegister());
6260 // Note that TypeCheckSlowPathARM uses this "temp" register too.
6261 locations->AddTemp(Location::RequiresRegister());
6262 // When read barriers are enabled, we need an additional temporary
6263 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00006264 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006265 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006266 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006267}
6268
6269void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006270 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006271 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006272 Location obj_loc = locations->InAt(0);
6273 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006274 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006275 Location temp_loc = locations->GetTemp(0);
6276 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006277 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00006278 locations->GetTemp(1) :
6279 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006280 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006281 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6282 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6283 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006284
Roland Levillain3b359c72015-11-17 19:35:12 +00006285 bool is_type_check_slow_path_fatal =
6286 (type_check_kind == TypeCheckKind::kExactCheck ||
6287 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6288 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6289 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6290 !instruction->CanThrowIntoCatchBlock();
Artem Serovf4d6aee2016-07-11 10:41:45 +01006291 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006292 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6293 is_type_check_slow_path_fatal);
6294 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006295
6296 Label done;
6297 // Avoid null check if we know obj is not null.
6298 if (instruction->MustDoNullCheck()) {
6299 __ CompareAndBranchIfZero(obj, &done);
6300 }
6301
Roland Levillain3b359c72015-11-17 19:35:12 +00006302 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006303 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006304
Roland Levillain3b359c72015-11-17 19:35:12 +00006305 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006306 case TypeCheckKind::kExactCheck:
6307 case TypeCheckKind::kArrayCheck: {
6308 __ cmp(temp, ShifterOperand(cls));
6309 // Jump to slow path for throwing the exception or doing a
6310 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006311 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312 break;
6313 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006314
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006315 case TypeCheckKind::kAbstractClassCheck: {
6316 // If the class is abstract, we eagerly fetch the super class of the
6317 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006318 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006319 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006320 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006321 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006322
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006323 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6324 // exception.
6325 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00006326
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006327 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 __ cmp(temp, ShifterOperand(cls));
6329 __ b(&loop, NE);
6330 break;
6331 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006332
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006333 case TypeCheckKind::kClassHierarchyCheck: {
6334 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006335 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006336 __ Bind(&loop);
6337 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006338 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006339
Roland Levillain3b359c72015-11-17 19:35:12 +00006340 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006341 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006342
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006343 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6344 // exception.
6345 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6346 // Otherwise, jump to the beginning of the loop.
6347 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006348 break;
6349 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006350
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006351 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006352 // Do an exact check.
6353 __ cmp(temp, ShifterOperand(cls));
6354 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006355
6356 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006357 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006358 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006359 // If the component type is null, jump to the slow path to throw the exception.
6360 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6361 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6362 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006363 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006364 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006365 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006366 break;
6367 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006368
Calin Juravle98893e12015-10-02 21:05:03 +01006369 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006370 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006371 // We always go into the type check slow path for the unresolved
6372 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00006373 //
6374 // We cannot directly call the CheckCast runtime entry point
6375 // without resorting to a type checking slow path here (i.e. by
6376 // calling InvokeRuntime directly), as it would require to
6377 // assign fixed registers for the inputs of this HInstanceOf
6378 // instruction (following the runtime calling convention), which
6379 // might be cluttered by the potential first read barrier
6380 // emission at the beginning of this method.
6381 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006382 break;
6383 }
6384 __ Bind(&done);
6385
Roland Levillain3b359c72015-11-17 19:35:12 +00006386 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006387}
6388
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006389void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6390 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006391 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006392 InvokeRuntimeCallingConvention calling_convention;
6393 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6394}
6395
6396void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006397 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6398 instruction,
6399 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006400 if (instruction->IsEnter()) {
6401 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6402 } else {
6403 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6404 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006405}
6406
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006407void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6408void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6409void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006410
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006411void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006412 LocationSummary* locations =
6413 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6414 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6415 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006416 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006417 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006418 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006419 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006420}
6421
6422void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6423 HandleBitwiseOperation(instruction);
6424}
6425
6426void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6427 HandleBitwiseOperation(instruction);
6428}
6429
6430void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6431 HandleBitwiseOperation(instruction);
6432}
6433
Artem Serov7fc63502016-02-09 17:15:29 +00006434
6435void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6436 LocationSummary* locations =
6437 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6438 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6439 || instruction->GetResultType() == Primitive::kPrimLong);
6440
6441 locations->SetInAt(0, Location::RequiresRegister());
6442 locations->SetInAt(1, Location::RequiresRegister());
6443 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6444}
6445
6446void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6447 LocationSummary* locations = instruction->GetLocations();
6448 Location first = locations->InAt(0);
6449 Location second = locations->InAt(1);
6450 Location out = locations->Out();
6451
6452 if (instruction->GetResultType() == Primitive::kPrimInt) {
6453 Register first_reg = first.AsRegister<Register>();
6454 ShifterOperand second_reg(second.AsRegister<Register>());
6455 Register out_reg = out.AsRegister<Register>();
6456
6457 switch (instruction->GetOpKind()) {
6458 case HInstruction::kAnd:
6459 __ bic(out_reg, first_reg, second_reg);
6460 break;
6461 case HInstruction::kOr:
6462 __ orn(out_reg, first_reg, second_reg);
6463 break;
6464 // There is no EON on arm.
6465 case HInstruction::kXor:
6466 default:
6467 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6468 UNREACHABLE();
6469 }
6470 return;
6471
6472 } else {
6473 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6474 Register first_low = first.AsRegisterPairLow<Register>();
6475 Register first_high = first.AsRegisterPairHigh<Register>();
6476 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6477 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6478 Register out_low = out.AsRegisterPairLow<Register>();
6479 Register out_high = out.AsRegisterPairHigh<Register>();
6480
6481 switch (instruction->GetOpKind()) {
6482 case HInstruction::kAnd:
6483 __ bic(out_low, first_low, second_low);
6484 __ bic(out_high, first_high, second_high);
6485 break;
6486 case HInstruction::kOr:
6487 __ orn(out_low, first_low, second_low);
6488 __ orn(out_high, first_high, second_high);
6489 break;
6490 // There is no EON on arm.
6491 case HInstruction::kXor:
6492 default:
6493 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6494 UNREACHABLE();
6495 }
6496 }
6497}
6498
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006499void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6500 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6501 if (value == 0xffffffffu) {
6502 if (out != first) {
6503 __ mov(out, ShifterOperand(first));
6504 }
6505 return;
6506 }
6507 if (value == 0u) {
6508 __ mov(out, ShifterOperand(0));
6509 return;
6510 }
6511 ShifterOperand so;
6512 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6513 __ and_(out, first, so);
6514 } else {
6515 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6516 __ bic(out, first, ShifterOperand(~value));
6517 }
6518}
6519
6520void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6521 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6522 if (value == 0u) {
6523 if (out != first) {
6524 __ mov(out, ShifterOperand(first));
6525 }
6526 return;
6527 }
6528 if (value == 0xffffffffu) {
6529 __ mvn(out, ShifterOperand(0));
6530 return;
6531 }
6532 ShifterOperand so;
6533 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6534 __ orr(out, first, so);
6535 } else {
6536 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6537 __ orn(out, first, ShifterOperand(~value));
6538 }
6539}
6540
6541void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6542 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6543 if (value == 0u) {
6544 if (out != first) {
6545 __ mov(out, ShifterOperand(first));
6546 }
6547 return;
6548 }
6549 __ eor(out, first, ShifterOperand(value));
6550}
6551
Vladimir Marko59751a72016-08-05 14:37:27 +01006552void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6553 Location first,
6554 uint64_t value) {
6555 Register out_low = out.AsRegisterPairLow<Register>();
6556 Register out_high = out.AsRegisterPairHigh<Register>();
6557 Register first_low = first.AsRegisterPairLow<Register>();
6558 Register first_high = first.AsRegisterPairHigh<Register>();
6559 uint32_t value_low = Low32Bits(value);
6560 uint32_t value_high = High32Bits(value);
6561 if (value_low == 0u) {
6562 if (out_low != first_low) {
6563 __ mov(out_low, ShifterOperand(first_low));
6564 }
6565 __ AddConstant(out_high, first_high, value_high);
6566 return;
6567 }
6568 __ AddConstantSetFlags(out_low, first_low, value_low);
6569 ShifterOperand so;
6570 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6571 __ adc(out_high, first_high, so);
6572 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6573 __ sbc(out_high, first_high, so);
6574 } else {
6575 LOG(FATAL) << "Unexpected constant " << value_high;
6576 UNREACHABLE();
6577 }
6578}
6579
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006580void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6581 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006582 Location first = locations->InAt(0);
6583 Location second = locations->InAt(1);
6584 Location out = locations->Out();
6585
6586 if (second.IsConstant()) {
6587 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6588 uint32_t value_low = Low32Bits(value);
6589 if (instruction->GetResultType() == Primitive::kPrimInt) {
6590 Register first_reg = first.AsRegister<Register>();
6591 Register out_reg = out.AsRegister<Register>();
6592 if (instruction->IsAnd()) {
6593 GenerateAndConst(out_reg, first_reg, value_low);
6594 } else if (instruction->IsOr()) {
6595 GenerateOrrConst(out_reg, first_reg, value_low);
6596 } else {
6597 DCHECK(instruction->IsXor());
6598 GenerateEorConst(out_reg, first_reg, value_low);
6599 }
6600 } else {
6601 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6602 uint32_t value_high = High32Bits(value);
6603 Register first_low = first.AsRegisterPairLow<Register>();
6604 Register first_high = first.AsRegisterPairHigh<Register>();
6605 Register out_low = out.AsRegisterPairLow<Register>();
6606 Register out_high = out.AsRegisterPairHigh<Register>();
6607 if (instruction->IsAnd()) {
6608 GenerateAndConst(out_low, first_low, value_low);
6609 GenerateAndConst(out_high, first_high, value_high);
6610 } else if (instruction->IsOr()) {
6611 GenerateOrrConst(out_low, first_low, value_low);
6612 GenerateOrrConst(out_high, first_high, value_high);
6613 } else {
6614 DCHECK(instruction->IsXor());
6615 GenerateEorConst(out_low, first_low, value_low);
6616 GenerateEorConst(out_high, first_high, value_high);
6617 }
6618 }
6619 return;
6620 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006621
6622 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006623 Register first_reg = first.AsRegister<Register>();
6624 ShifterOperand second_reg(second.AsRegister<Register>());
6625 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006626 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006627 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006628 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006629 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006630 } else {
6631 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006632 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006633 }
6634 } else {
6635 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006636 Register first_low = first.AsRegisterPairLow<Register>();
6637 Register first_high = first.AsRegisterPairHigh<Register>();
6638 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6639 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6640 Register out_low = out.AsRegisterPairLow<Register>();
6641 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006642 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006643 __ and_(out_low, first_low, second_low);
6644 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006645 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006646 __ orr(out_low, first_low, second_low);
6647 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006648 } else {
6649 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006650 __ eor(out_low, first_low, second_low);
6651 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006652 }
6653 }
6654}
6655
Roland Levillainc9285912015-12-18 10:38:42 +00006656void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6657 Location out,
6658 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006659 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006660 Register out_reg = out.AsRegister<Register>();
6661 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006662 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006663 if (kUseBakerReadBarrier) {
6664 // Load with fast path based Baker's read barrier.
6665 // /* HeapReference<Object> */ out = *(out + offset)
6666 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006667 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006668 } else {
6669 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006670 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006671 // in the following move operation, as we will need it for the
6672 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006673 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006674 // /* HeapReference<Object> */ out = *(out + offset)
6675 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006676 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006677 }
6678 } else {
6679 // Plain load with no read barrier.
6680 // /* HeapReference<Object> */ out = *(out + offset)
6681 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6682 __ MaybeUnpoisonHeapReference(out_reg);
6683 }
6684}
6685
6686void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6687 Location out,
6688 Location obj,
6689 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006690 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006691 Register out_reg = out.AsRegister<Register>();
6692 Register obj_reg = obj.AsRegister<Register>();
6693 if (kEmitCompilerReadBarrier) {
6694 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006695 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006696 // Load with fast path based Baker's read barrier.
6697 // /* HeapReference<Object> */ out = *(obj + offset)
6698 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006699 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006700 } else {
6701 // Load with slow path based read barrier.
6702 // /* HeapReference<Object> */ out = *(obj + offset)
6703 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6704 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6705 }
6706 } else {
6707 // Plain load with no read barrier.
6708 // /* HeapReference<Object> */ out = *(obj + offset)
6709 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6710 __ MaybeUnpoisonHeapReference(out_reg);
6711 }
6712}
6713
6714void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6715 Location root,
6716 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006717 uint32_t offset,
6718 bool requires_read_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006719 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006720 if (requires_read_barrier) {
6721 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006722 if (kUseBakerReadBarrier) {
6723 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6724 // Baker's read barrier are used:
6725 //
6726 // root = obj.field;
6727 // if (Thread::Current()->GetIsGcMarking()) {
6728 // root = ReadBarrier::Mark(root)
6729 // }
6730
6731 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6732 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6733 static_assert(
6734 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6735 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6736 "have different sizes.");
6737 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6738 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6739 "have different sizes.");
6740
Vladimir Marko953437b2016-08-24 08:30:46 +00006741 // Slow path marking the GC root `root`.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006742 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006743 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006744 codegen_->AddSlowPath(slow_path);
6745
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006746 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006747 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006748 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006749 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6750 __ Bind(slow_path->GetExitLabel());
6751 } else {
6752 // GC root loaded through a slow path for read barriers other
6753 // than Baker's.
6754 // /* GcRoot<mirror::Object>* */ root = obj + offset
6755 __ AddConstant(root_reg, obj, offset);
6756 // /* mirror::Object* */ root = root->Read()
6757 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6758 }
6759 } else {
6760 // Plain GC root load with no read barrier.
6761 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6762 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6763 // Note that GC roots are not affected by heap poisoning, thus we
6764 // do not have to unpoison `root_reg` here.
6765 }
6766}
6767
6768void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6769 Location ref,
6770 Register obj,
6771 uint32_t offset,
6772 Location temp,
6773 bool needs_null_check) {
6774 DCHECK(kEmitCompilerReadBarrier);
6775 DCHECK(kUseBakerReadBarrier);
6776
6777 // /* HeapReference<Object> */ ref = *(obj + offset)
6778 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006779 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006780 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006781 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006782}
6783
6784void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6785 Location ref,
6786 Register obj,
6787 uint32_t data_offset,
6788 Location index,
6789 Location temp,
6790 bool needs_null_check) {
6791 DCHECK(kEmitCompilerReadBarrier);
6792 DCHECK(kUseBakerReadBarrier);
6793
Roland Levillainbfea3352016-06-23 13:48:47 +01006794 static_assert(
6795 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6796 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006797 // /* HeapReference<Object> */ ref =
6798 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006799 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006800 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006801 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006802}
6803
6804void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6805 Location ref,
6806 Register obj,
6807 uint32_t offset,
6808 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006809 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006810 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006811 bool needs_null_check,
6812 bool always_update_field,
6813 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00006814 DCHECK(kEmitCompilerReadBarrier);
6815 DCHECK(kUseBakerReadBarrier);
6816
6817 // In slow path based read barriers, the read barrier call is
6818 // inserted after the original load. However, in fast path based
6819 // Baker's read barriers, we need to perform the load of
6820 // mirror::Object::monitor_ *before* the original reference load.
6821 // This load-load ordering is required by the read barrier.
6822 // The fast path/slow path (for Baker's algorithm) should look like:
6823 //
6824 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6825 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6826 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006827 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillainc9285912015-12-18 10:38:42 +00006828 // if (is_gray) {
6829 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6830 // }
6831 //
6832 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006833 // slightly more complex as it performs additional checks that we do
6834 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006835
6836 Register ref_reg = ref.AsRegister<Register>();
6837 Register temp_reg = temp.AsRegister<Register>();
6838 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6839
6840 // /* int32_t */ monitor = obj->monitor_
6841 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6842 if (needs_null_check) {
6843 MaybeRecordImplicitNullCheck(instruction);
6844 }
6845 // /* LockWord */ lock_word = LockWord(monitor)
6846 static_assert(sizeof(LockWord) == sizeof(int32_t),
6847 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006848
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006849 // Introduce a dependency on the lock_word including the rb_state,
6850 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006851 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006852 // `obj` is unchanged by this operation, but its value now depends
6853 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006854 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006855
6856 // The actual reference load.
6857 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006858 // Load types involving an "index": ArrayGet,
6859 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6860 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006861 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006862 if (index.IsConstant()) {
6863 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006864 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006865 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6866 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006867 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006868 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6869 // intrinsics, which use a register pair as index ("long
6870 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01006871 Register index_reg = index.IsRegisterPair()
6872 ? index.AsRegisterPairLow<Register>()
6873 : index.AsRegister<Register>();
6874 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006875 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6876 }
6877 } else {
6878 // /* HeapReference<Object> */ ref = *(obj + offset)
6879 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6880 }
6881
6882 // Object* ref = ref_addr->AsMirrorPtr()
6883 __ MaybeUnpoisonHeapReference(ref_reg);
6884
Vladimir Marko953437b2016-08-24 08:30:46 +00006885 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006886 SlowPathCodeARM* slow_path;
6887 if (always_update_field) {
6888 DCHECK(temp2 != nullptr);
6889 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
6890 // of the form `obj + field_offset`, where `obj` is a register and
6891 // `field_offset` is a register pair (of which only the lower half
6892 // is used). Thus `offset` and `scale_factor` above are expected
6893 // to be null in this code path.
6894 DCHECK_EQ(offset, 0u);
6895 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6896 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
6897 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
6898 } else {
6899 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
6900 }
Roland Levillainc9285912015-12-18 10:38:42 +00006901 AddSlowPath(slow_path);
6902
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006903 // if (rb_state == ReadBarrier::GrayState())
Roland Levillainc9285912015-12-18 10:38:42 +00006904 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006905 // Given the numeric representation, it's enough to check the low bit of the
6906 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6907 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006908 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6909 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006910 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6911 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006912 __ Bind(slow_path->GetExitLabel());
6913}
6914
6915void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6916 Location out,
6917 Location ref,
6918 Location obj,
6919 uint32_t offset,
6920 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006921 DCHECK(kEmitCompilerReadBarrier);
6922
Roland Levillainc9285912015-12-18 10:38:42 +00006923 // Insert a slow path based read barrier *after* the reference load.
6924 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006925 // If heap poisoning is enabled, the unpoisoning of the loaded
6926 // reference will be carried out by the runtime within the slow
6927 // path.
6928 //
6929 // Note that `ref` currently does not get unpoisoned (when heap
6930 // poisoning is enabled), which is alright as the `ref` argument is
6931 // not used by the artReadBarrierSlow entry point.
6932 //
6933 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006934 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00006935 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6936 AddSlowPath(slow_path);
6937
Roland Levillain3b359c72015-11-17 19:35:12 +00006938 __ b(slow_path->GetEntryLabel());
6939 __ Bind(slow_path->GetExitLabel());
6940}
6941
Roland Levillainc9285912015-12-18 10:38:42 +00006942void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6943 Location out,
6944 Location ref,
6945 Location obj,
6946 uint32_t offset,
6947 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006948 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006949 // Baker's read barriers shall be handled by the fast path
6950 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6951 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006952 // If heap poisoning is enabled, unpoisoning will be taken care of
6953 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006954 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006955 } else if (kPoisonHeapReferences) {
6956 __ UnpoisonHeapReference(out.AsRegister<Register>());
6957 }
6958}
6959
Roland Levillainc9285912015-12-18 10:38:42 +00006960void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6961 Location out,
6962 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006963 DCHECK(kEmitCompilerReadBarrier);
6964
Roland Levillainc9285912015-12-18 10:38:42 +00006965 // Insert a slow path based read barrier *after* the GC root load.
6966 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006967 // Note that GC roots are not affected by heap poisoning, so we do
6968 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006969 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006970 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6971 AddSlowPath(slow_path);
6972
Roland Levillain3b359c72015-11-17 19:35:12 +00006973 __ b(slow_path->GetEntryLabel());
6974 __ Bind(slow_path->GetExitLabel());
6975}
6976
Vladimir Markodc151b22015-10-15 18:02:30 +01006977HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6978 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01006979 HInvokeStaticOrDirect* invoke) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006980 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6981 // We disable pc-relative load when there is an irreducible loop, as the optimization
6982 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006983 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6984 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006985 if (GetGraph()->HasIrreducibleLoops() &&
6986 (dispatch_info.method_load_kind ==
6987 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6988 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6989 }
6990
6991 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006992 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01006993 if (&outer_dex_file != invoke->GetTargetMethod().dex_file) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006994 // Calls across dex files are more likely to exceed the available BL range,
6995 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6996 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6997 (desired_dispatch_info.method_load_kind ==
6998 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6999 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
7000 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
7001 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007002 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01007003 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007004 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01007005 0u
7006 };
7007 }
7008 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007009 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007010}
7011
Vladimir Markob4536b72015-11-24 13:45:23 +00007012Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7013 Register temp) {
7014 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7015 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7016 if (!invoke->GetLocations()->Intrinsified()) {
7017 return location.AsRegister<Register>();
7018 }
7019 // For intrinsics we allow any location, so it may be on the stack.
7020 if (!location.IsRegister()) {
7021 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7022 return temp;
7023 }
7024 // For register locations, check if the register was saved. If so, get it from the stack.
7025 // Note: There is a chance that the register was saved but not overwritten, so we could
7026 // save one load. However, since this is just an intrinsic slow path we prefer this
7027 // simple and more robust approach rather that trying to determine if that's the case.
7028 SlowPathCode* slow_path = GetCurrentSlowPath();
7029 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7030 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7031 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7032 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7033 return temp;
7034 }
7035 return location.AsRegister<Register>();
7036}
7037
Nicolas Geoffray38207af2015-06-01 15:46:22 +01007038void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007039 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00007040 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00007041 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7042 // LR = code address from literal pool with link-time patch.
7043 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00007044 break;
7045 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7046 // LR = invoke->GetDirectCodePtr();
7047 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00007048 break;
7049 default:
7050 break;
7051 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007052
Vladimir Marko58155012015-08-19 12:49:41 +00007053 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7054 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007055 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7056 uint32_t offset =
7057 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007058 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007059 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007060 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007061 }
Vladimir Marko58155012015-08-19 12:49:41 +00007062 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007063 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007064 break;
7065 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7066 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7067 break;
7068 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
7069 __ LoadLiteral(temp.AsRegister<Register>(),
7070 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
7071 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007072 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7073 HArmDexCacheArraysBase* base =
7074 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7075 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7076 temp.AsRegister<Register>());
7077 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7078 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7079 break;
7080 }
Vladimir Marko58155012015-08-19 12:49:41 +00007081 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007082 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007083 Register method_reg;
7084 Register reg = temp.AsRegister<Register>();
7085 if (current_method.IsRegister()) {
7086 method_reg = current_method.AsRegister<Register>();
7087 } else {
7088 DCHECK(invoke->GetLocations()->Intrinsified());
7089 DCHECK(!current_method.IsValid());
7090 method_reg = reg;
7091 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7092 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007093 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7094 __ LoadFromOffset(kLoadWord,
7095 reg,
7096 method_reg,
7097 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007098 // temp = temp[index_in_cache];
7099 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7100 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007101 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7102 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007103 }
Vladimir Marko58155012015-08-19 12:49:41 +00007104 }
7105
7106 switch (invoke->GetCodePtrLocation()) {
7107 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7108 __ bl(GetFrameEntryLabel());
7109 break;
7110 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007111 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
7112 invoke->GetTargetMethod().dex_method_index);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007113 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01007114 // Arbitrarily branch to the BL itself, override at link time.
7115 __ bl(&relative_call_patches_.back().label);
7116 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007117 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7118 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7119 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00007120 // LR()
7121 __ blx(LR);
7122 break;
7123 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7124 // LR = callee_method->entry_point_from_quick_compiled_code_
7125 __ LoadFromOffset(
7126 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007127 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007128 // LR()
7129 __ blx(LR);
7130 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007131 }
7132
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007133 DCHECK(!IsLeafMethod());
7134}
7135
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007136void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7137 Register temp = temp_location.AsRegister<Register>();
7138 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7139 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007140
7141 // Use the calling convention instead of the location of the receiver, as
7142 // intrinsics may have put the receiver in a different register. In the intrinsics
7143 // slow path, the arguments have been moved to the right place, so here we are
7144 // guaranteed that the receiver is the first register of the calling convention.
7145 InvokeDexCallingConvention calling_convention;
7146 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007147 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007148 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007149 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007150 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007151 // Instead of simply (possibly) unpoisoning `temp` here, we should
7152 // emit a read barrier for the previous class reference load.
7153 // However this is not required in practice, as this is an
7154 // intermediate/temporary reference and because the current
7155 // concurrent copying collector keeps the from-space memory
7156 // intact/accessible until the end of the marking phase (the
7157 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007158 __ MaybeUnpoisonHeapReference(temp);
7159 // temp = temp->GetMethodAt(method_offset);
7160 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007161 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007162 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7163 // LR = temp->GetEntryPoint();
7164 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7165 // LR();
7166 __ blx(LR);
7167}
7168
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007169CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
7170 const DexFile& dex_file, uint32_t string_index) {
7171 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
7172}
7173
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007174CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
7175 const DexFile& dex_file, uint32_t type_index) {
7176 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
7177}
7178
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007179CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7180 const DexFile& dex_file, uint32_t element_offset) {
7181 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7182}
7183
7184CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7185 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7186 patches->emplace_back(dex_file, offset_or_index);
7187 return &patches->back();
7188}
7189
7190Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
7191 uint32_t string_index) {
7192 return boot_image_string_patches_.GetOrCreate(
7193 StringReference(&dex_file, string_index),
7194 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7195}
7196
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007197Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
7198 uint32_t type_index) {
7199 return boot_image_type_patches_.GetOrCreate(
7200 TypeReference(&dex_file, type_index),
7201 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7202}
7203
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007204Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7205 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7206 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7207 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7208}
7209
7210Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
7211 return DeduplicateUint32Literal(address, &uint32_literals_);
7212}
7213
Nicolas Geoffray997d1212016-11-09 10:36:29 +00007214Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
7215 uint32_t string_index) {
7216 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index), /* placeholder */ 0u);
7217 return jit_string_patches_.GetOrCreate(
7218 StringReference(&dex_file, string_index),
7219 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7220}
7221
Vladimir Markoaad75c62016-10-03 08:46:48 +00007222template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7223inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7224 const ArenaDeque<PcRelativePatchInfo>& infos,
7225 ArenaVector<LinkerPatch>* linker_patches) {
7226 for (const PcRelativePatchInfo& info : infos) {
7227 const DexFile& dex_file = info.target_dex_file;
7228 size_t offset_or_index = info.offset_or_index;
7229 DCHECK(info.add_pc_label.IsBound());
7230 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7231 // Add MOVW patch.
7232 DCHECK(info.movw_label.IsBound());
7233 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7234 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7235 // Add MOVT patch.
7236 DCHECK(info.movt_label.IsBound());
7237 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7238 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7239 }
7240}
7241
Vladimir Marko58155012015-08-19 12:49:41 +00007242void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7243 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007244 size_t size =
7245 method_patches_.size() +
7246 call_patches_.size() +
7247 relative_call_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007248 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007249 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007250 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007251 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007252 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007253 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007254 linker_patches->reserve(size);
7255 for (const auto& entry : method_patches_) {
7256 const MethodReference& target_method = entry.first;
7257 Literal* literal = entry.second;
7258 DCHECK(literal->GetLabel()->IsBound());
7259 uint32_t literal_offset = literal->GetLabel()->Position();
7260 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
7261 target_method.dex_file,
7262 target_method.dex_method_index));
7263 }
7264 for (const auto& entry : call_patches_) {
7265 const MethodReference& target_method = entry.first;
7266 Literal* literal = entry.second;
7267 DCHECK(literal->GetLabel()->IsBound());
7268 uint32_t literal_offset = literal->GetLabel()->Position();
7269 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
7270 target_method.dex_file,
7271 target_method.dex_method_index));
7272 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007273 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko58155012015-08-19 12:49:41 +00007274 uint32_t literal_offset = info.label.Position();
Vladimir Markoaad75c62016-10-03 08:46:48 +00007275 linker_patches->push_back(
7276 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00007277 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007278 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7279 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007280 for (const auto& entry : boot_image_string_patches_) {
7281 const StringReference& target_string = entry.first;
7282 Literal* literal = entry.second;
7283 DCHECK(literal->GetLabel()->IsBound());
7284 uint32_t literal_offset = literal->GetLabel()->Position();
7285 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7286 target_string.dex_file,
7287 target_string.string_index));
7288 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007289 if (!GetCompilerOptions().IsBootImage()) {
7290 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7291 linker_patches);
7292 } else {
7293 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7294 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007295 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007296 for (const auto& entry : boot_image_type_patches_) {
7297 const TypeReference& target_type = entry.first;
7298 Literal* literal = entry.second;
7299 DCHECK(literal->GetLabel()->IsBound());
7300 uint32_t literal_offset = literal->GetLabel()->Position();
7301 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7302 target_type.dex_file,
7303 target_type.type_index));
7304 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007305 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7306 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007307 for (const auto& entry : boot_image_address_patches_) {
7308 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7309 Literal* literal = entry.second;
7310 DCHECK(literal->GetLabel()->IsBound());
7311 uint32_t literal_offset = literal->GetLabel()->Position();
7312 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7313 }
7314}
7315
7316Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7317 return map->GetOrCreate(
7318 value,
7319 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007320}
7321
7322Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7323 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007324 return map->GetOrCreate(
7325 target_method,
7326 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007327}
7328
7329Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
7330 return DeduplicateMethodLiteral(target_method, &method_patches_);
7331}
7332
7333Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
7334 return DeduplicateMethodLiteral(target_method, &call_patches_);
7335}
7336
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007337void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7338 LocationSummary* locations =
7339 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7340 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7341 Location::RequiresRegister());
7342 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7343 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7344 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7345}
7346
7347void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7348 LocationSummary* locations = instr->GetLocations();
7349 Register res = locations->Out().AsRegister<Register>();
7350 Register accumulator =
7351 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7352 Register mul_left =
7353 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7354 Register mul_right =
7355 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7356
7357 if (instr->GetOpKind() == HInstruction::kAdd) {
7358 __ mla(res, mul_left, mul_right, accumulator);
7359 } else {
7360 __ mls(res, mul_left, mul_right, accumulator);
7361 }
7362}
7363
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007364void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007365 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007366 LOG(FATAL) << "Unreachable";
7367}
7368
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007369void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007370 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007371 LOG(FATAL) << "Unreachable";
7372}
7373
Mark Mendellfe57faa2015-09-18 09:26:15 -04007374// Simple implementation of packed switch - generate cascaded compare/jumps.
7375void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7376 LocationSummary* locations =
7377 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7378 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007379 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007380 codegen_->GetAssembler()->IsThumb()) {
7381 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7382 if (switch_instr->GetStartValue() != 0) {
7383 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7384 }
7385 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007386}
7387
7388void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7389 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007390 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007391 LocationSummary* locations = switch_instr->GetLocations();
7392 Register value_reg = locations->InAt(0).AsRegister<Register>();
7393 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7394
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007395 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007396 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007397 Register temp_reg = IP;
7398 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7399 // the immediate, because IP is used as the destination register. For the other
7400 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7401 // and they can be encoded in the instruction without making use of IP register.
7402 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7403
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007404 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007405 // Jump to successors[0] if value == lower_bound.
7406 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7407 int32_t last_index = 0;
7408 for (; num_entries - last_index > 2; last_index += 2) {
7409 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7410 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7411 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7412 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7413 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7414 }
7415 if (num_entries - last_index == 2) {
7416 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007417 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007418 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007419 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007420
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007421 // And the default for any other value.
7422 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7423 __ b(codegen_->GetLabelOf(default_block));
7424 }
7425 } else {
7426 // Create a table lookup.
7427 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7428
7429 // Materialize a pointer to the switch table
7430 std::vector<Label*> labels(num_entries);
7431 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7432 for (uint32_t i = 0; i < num_entries; i++) {
7433 labels[i] = codegen_->GetLabelOf(successors[i]);
7434 }
7435 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7436
7437 // Remove the bias.
7438 Register key_reg;
7439 if (lower_bound != 0) {
7440 key_reg = locations->GetTemp(1).AsRegister<Register>();
7441 __ AddConstant(key_reg, value_reg, -lower_bound);
7442 } else {
7443 key_reg = value_reg;
7444 }
7445
7446 // Check whether the value is in the table, jump to default block if not.
7447 __ CmpConstant(key_reg, num_entries - 1);
7448 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7449
7450 // Load the displacement from the table.
7451 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7452
7453 // Dispatch is a direct add to the PC (for Thumb2).
7454 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007455 }
7456}
7457
Vladimir Markob4536b72015-11-24 13:45:23 +00007458void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7459 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7460 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007461}
7462
7463void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7464 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007465 CodeGeneratorARM::PcRelativePatchInfo* labels =
7466 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007467 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007468 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007469 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007470 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007471 __ BindTrackedLabel(&labels->add_pc_label);
7472 __ add(base_reg, base_reg, ShifterOperand(PC));
7473}
7474
Andreas Gampe85b62f22015-09-09 13:15:38 -07007475void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7476 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007477 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007478 return;
7479 }
7480
7481 DCHECK_NE(type, Primitive::kPrimVoid);
7482
7483 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7484 if (return_loc.Equals(trg)) {
7485 return;
7486 }
7487
7488 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7489 // with the last branch.
7490 if (type == Primitive::kPrimLong) {
7491 HParallelMove parallel_move(GetGraph()->GetArena());
7492 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7493 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7494 GetMoveResolver()->EmitNativeCode(&parallel_move);
7495 } else if (type == Primitive::kPrimDouble) {
7496 HParallelMove parallel_move(GetGraph()->GetArena());
7497 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7498 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7499 GetMoveResolver()->EmitNativeCode(&parallel_move);
7500 } else {
7501 // Let the parallel move resolver take care of all of this.
7502 HParallelMove parallel_move(GetGraph()->GetArena());
7503 parallel_move.AddMove(return_loc, trg, type, nullptr);
7504 GetMoveResolver()->EmitNativeCode(&parallel_move);
7505 }
7506}
7507
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007508void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7509 LocationSummary* locations =
7510 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7511 locations->SetInAt(0, Location::RequiresRegister());
7512 locations->SetOut(Location::RequiresRegister());
7513}
7514
7515void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7516 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007517 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007518 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007519 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007520 __ LoadFromOffset(kLoadWord,
7521 locations->Out().AsRegister<Register>(),
7522 locations->InAt(0).AsRegister<Register>(),
7523 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007524 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007525 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007526 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007527 __ LoadFromOffset(kLoadWord,
7528 locations->Out().AsRegister<Register>(),
7529 locations->InAt(0).AsRegister<Register>(),
7530 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7531 __ LoadFromOffset(kLoadWord,
7532 locations->Out().AsRegister<Register>(),
7533 locations->Out().AsRegister<Register>(),
7534 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007535 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007536}
7537
Nicolas Geoffray997d1212016-11-09 10:36:29 +00007538void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7539 for (const auto& entry : jit_string_patches_) {
7540 const auto& it = jit_string_roots_.find(entry.first);
7541 DCHECK(it != jit_string_roots_.end());
7542 size_t index_in_table = it->second;
7543 Literal* literal = entry.second;
7544 DCHECK(literal->GetLabel()->IsBound());
7545 uint32_t literal_offset = literal->GetLabel()->Position();
7546 uintptr_t address =
7547 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7548 uint8_t* data = code + literal_offset;
7549 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7550 }
7551}
7552
Roland Levillain4d027112015-07-01 15:41:14 +01007553#undef __
7554#undef QUICK_ENTRY_POINT
7555
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007556} // namespace arm
7557} // namespace art