blob: b2bfe42548f311cbee21fae6ba531e21057d877b [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000022#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070023#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080025#include "intrinsics.h"
26#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070029#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010030#include "utils/arm/assembler_arm.h"
31#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000032#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010033#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain3b359c72015-11-17 19:35:12 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace arm {
41
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000042static bool ExpectedPairLayout(Location location) {
43 // We expected this for both core and fpu register pairs.
44 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
45}
46
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010047static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010048static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010049
David Brazdil58282f42016-01-14 12:45:10 +000050static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000051static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070052 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr SRegister kFpuCalleeSaves[] =
54 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010055
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000056// D31 cannot be split into two S registers, and the register allocator only works on
57// S registers. Therefore there is no need to block it.
58static constexpr DRegister DTMP = D31;
59
Vladimir Markof3e0ee22015-12-17 15:23:13 +000060static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070061
Roland Levillain7cbd27f2016-08-11 23:53:33 +010062// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
63#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070064#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010065
Artem Serovf4d6aee2016-07-11 10:41:45 +010066static constexpr int kRegListThreshold = 4;
67
Artem Serovd300d8f2016-07-15 14:00:56 +010068// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
69// for each live D registers they treat two corresponding S registers as live ones.
70//
71// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
72// from a list of contiguous S registers a list of contiguous D registers (processing first/last
73// S registers corner cases) and save/restore this new list treating them as D registers.
74// - decreasing code size
75// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
76// restored and then used in regular non SlowPath code as D register.
77//
78// For the following example (v means the S register is live):
79// D names: | D0 | D1 | D2 | D4 | ...
80// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
81// Live? | | v | v | v | v | v | v | | ...
82//
83// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
84// as D registers.
85static size_t SaveContiguousSRegisterList(size_t first,
86 size_t last,
87 CodeGenerator* codegen,
88 size_t stack_offset) {
89 DCHECK_LE(first, last);
90 if ((first == last) && (first == 0)) {
91 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first);
92 return stack_offset;
93 }
94 if (first % 2 == 1) {
95 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first++);
96 }
97
98 bool save_last = false;
99 if (last % 2 == 0) {
100 save_last = true;
101 --last;
102 }
103
104 if (first < last) {
105 DRegister d_reg = static_cast<DRegister>(first / 2);
106 DCHECK_EQ((last - first + 1) % 2, 0u);
107 size_t number_of_d_regs = (last - first + 1) / 2;
108
109 if (number_of_d_regs == 1) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100110 __ StoreDToOffset(d_reg, SP, stack_offset);
Artem Serovd300d8f2016-07-15 14:00:56 +0100111 } else if (number_of_d_regs > 1) {
112 __ add(IP, SP, ShifterOperand(stack_offset));
113 __ vstmiad(IP, d_reg, number_of_d_regs);
114 }
115 stack_offset += number_of_d_regs * kArmWordSize * 2;
116 }
117
118 if (save_last) {
119 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, last + 1);
120 }
121
122 return stack_offset;
123}
124
125static size_t RestoreContiguousSRegisterList(size_t first,
126 size_t last,
127 CodeGenerator* codegen,
128 size_t stack_offset) {
129 DCHECK_LE(first, last);
130 if ((first == last) && (first == 0)) {
131 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first);
132 return stack_offset;
133 }
134 if (first % 2 == 1) {
135 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first++);
136 }
137
138 bool restore_last = false;
139 if (last % 2 == 0) {
140 restore_last = true;
141 --last;
142 }
143
144 if (first < last) {
145 DRegister d_reg = static_cast<DRegister>(first / 2);
146 DCHECK_EQ((last - first + 1) % 2, 0u);
147 size_t number_of_d_regs = (last - first + 1) / 2;
148 if (number_of_d_regs == 1) {
149 __ LoadDFromOffset(d_reg, SP, stack_offset);
150 } else if (number_of_d_regs > 1) {
151 __ add(IP, SP, ShifterOperand(stack_offset));
152 __ vldmiad(IP, d_reg, number_of_d_regs);
153 }
154 stack_offset += number_of_d_regs * kArmWordSize * 2;
155 }
156
157 if (restore_last) {
158 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, last + 1);
159 }
160
161 return stack_offset;
162}
163
Artem Serovf4d6aee2016-07-11 10:41:45 +0100164void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
165 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
166 size_t orig_offset = stack_offset;
167
168 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
169 for (uint32_t i : LowToHighBits(core_spills)) {
170 // If the register holds an object, update the stack mask.
171 if (locations->RegisterContainsObject(i)) {
172 locations->SetStackBit(stack_offset / kVRegSize);
173 }
174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
176 saved_core_stack_offsets_[i] = stack_offset;
177 stack_offset += kArmWordSize;
178 }
179
180 int reg_num = POPCOUNT(core_spills);
181 if (reg_num != 0) {
182 if (reg_num > kRegListThreshold) {
183 __ StoreList(RegList(core_spills), orig_offset);
184 } else {
185 stack_offset = orig_offset;
186 for (uint32_t i : LowToHighBits(core_spills)) {
187 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
188 }
189 }
190 }
191
Artem Serovd300d8f2016-07-15 14:00:56 +0100192 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
193 orig_offset = stack_offset;
Vladimir Marko804b03f2016-09-14 16:26:36 +0100194 for (uint32_t i : LowToHighBits(fp_spills)) {
Artem Serovf4d6aee2016-07-11 10:41:45 +0100195 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
196 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serovd300d8f2016-07-15 14:00:56 +0100197 stack_offset += kArmWordSize;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100198 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100199
200 stack_offset = orig_offset;
201 while (fp_spills != 0u) {
202 uint32_t begin = CTZ(fp_spills);
203 uint32_t tmp = fp_spills + (1u << begin);
204 fp_spills &= tmp; // Clear the contiguous range of 1s.
205 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
206 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
207 }
208 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100209}
210
211void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
212 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
213 size_t orig_offset = stack_offset;
214
215 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
216 for (uint32_t i : LowToHighBits(core_spills)) {
217 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
218 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
219 stack_offset += kArmWordSize;
220 }
221
222 int reg_num = POPCOUNT(core_spills);
223 if (reg_num != 0) {
224 if (reg_num > kRegListThreshold) {
225 __ LoadList(RegList(core_spills), orig_offset);
226 } else {
227 stack_offset = orig_offset;
228 for (uint32_t i : LowToHighBits(core_spills)) {
229 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
230 }
231 }
232 }
233
Artem Serovd300d8f2016-07-15 14:00:56 +0100234 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
235 while (fp_spills != 0u) {
236 uint32_t begin = CTZ(fp_spills);
237 uint32_t tmp = fp_spills + (1u << begin);
238 fp_spills &= tmp; // Clear the contiguous range of 1s.
239 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
240 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
Artem Serovf4d6aee2016-07-11 10:41:45 +0100241 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100242 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100243}
244
245class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100246 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100247 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100248
Alexandre Rames67555f72014-11-18 10:55:16 +0000249 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100250 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100251 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000252 if (instruction_->CanThrowIntoCatchBlock()) {
253 // Live registers will be restored in the catch block if caught.
254 SaveLiveRegisters(codegen, instruction_->GetLocations());
255 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100256 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
257 instruction_,
258 instruction_->GetDexPc(),
259 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000260 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100261 }
262
Alexandre Rames8158f282015-08-07 10:26:17 +0100263 bool IsFatal() const OVERRIDE { return true; }
264
Alexandre Rames9931f312015-06-19 14:47:01 +0100265 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
266
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100267 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100268 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
269};
270
Artem Serovf4d6aee2016-07-11 10:41:45 +0100271class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000272 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100273 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000274
Alexandre Rames67555f72014-11-18 10:55:16 +0000275 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000276 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
277 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100278 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000279 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000280 }
281
Alexandre Rames8158f282015-08-07 10:26:17 +0100282 bool IsFatal() const OVERRIDE { return true; }
283
Alexandre Rames9931f312015-06-19 14:47:01 +0100284 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
285
Calin Juravled0d48522014-11-04 16:40:20 +0000286 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000287 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
288};
289
Artem Serovf4d6aee2016-07-11 10:41:45 +0100290class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000291 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000292 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100293 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000294
Alexandre Rames67555f72014-11-18 10:55:16 +0000295 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100296 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000297 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100298 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000299 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100300 if (successor_ == nullptr) {
301 __ b(GetReturnLabel());
302 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100303 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100304 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000305 }
306
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100307 Label* GetReturnLabel() {
308 DCHECK(successor_ == nullptr);
309 return &return_label_;
310 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000311
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100312 HBasicBlock* GetSuccessor() const {
313 return successor_;
314 }
315
Alexandre Rames9931f312015-06-19 14:47:01 +0100316 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
317
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000318 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100319 // If not null, the block to branch to after the suspend check.
320 HBasicBlock* const successor_;
321
322 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000323 Label return_label_;
324
325 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
326};
327
Artem Serovf4d6aee2016-07-11 10:41:45 +0100328class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100329 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100330 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100331 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100332
Alexandre Rames67555f72014-11-18 10:55:16 +0000333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100334 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100335 LocationSummary* locations = instruction_->GetLocations();
336
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100337 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000338 if (instruction_->CanThrowIntoCatchBlock()) {
339 // Live registers will be restored in the catch block if caught.
340 SaveLiveRegisters(codegen, instruction_->GetLocations());
341 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000342 // We're moving two locations to locations that could overlap, so we need a parallel
343 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100344 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000345 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100346 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000347 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100348 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100349 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100350 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
351 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100352 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
353 ? kQuickThrowStringBounds
354 : kQuickThrowArrayBounds;
355 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100356 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000357 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100358 }
359
Alexandre Rames8158f282015-08-07 10:26:17 +0100360 bool IsFatal() const OVERRIDE { return true; }
361
Alexandre Rames9931f312015-06-19 14:47:01 +0100362 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
363
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100364 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100365 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
366};
367
Artem Serovf4d6aee2016-07-11 10:41:45 +0100368class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100369 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000370 LoadClassSlowPathARM(HLoadClass* cls,
371 HInstruction* at,
372 uint32_t dex_pc,
373 bool do_clinit)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100374 : SlowPathCodeARM(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000375 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
376 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100377
Alexandre Rames67555f72014-11-18 10:55:16 +0000378 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000379 LocationSummary* locations = at_->GetLocations();
380
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100381 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
382 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000383 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100385 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000386 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100387 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
388 : kQuickInitializeType;
389 arm_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000390 if (do_clinit_) {
391 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
392 } else {
393 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
394 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000395
396 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000397 Location out = locations->Out();
398 if (out.IsValid()) {
399 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000400 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
401 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000402 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100403 __ b(GetExitLabel());
404 }
405
Alexandre Rames9931f312015-06-19 14:47:01 +0100406 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
407
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100408 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000409 // The class this slow path will load.
410 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100411
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000412 // The instruction where this slow path is happening.
413 // (Might be the load class or an initialization check).
414 HInstruction* const at_;
415
416 // The dex PC of `at_`.
417 const uint32_t dex_pc_;
418
419 // Whether to initialize the class.
420 const bool do_clinit_;
421
422 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100423};
424
Vladimir Markoaad75c62016-10-03 08:46:48 +0000425class LoadStringSlowPathARM : public SlowPathCodeARM {
426 public:
427 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
428
429 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
430 LocationSummary* locations = instruction_->GetLocations();
431 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100432 HLoadString* load = instruction_->AsLoadString();
433 const uint32_t string_index = load->GetStringIndex();
434 Register out = locations->Out().AsRegister<Register>();
435 Register temp = locations->GetTemp(0).AsRegister<Register>();
436 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000437
438 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
439 __ Bind(GetEntryLabel());
440 SaveLiveRegisters(codegen, locations);
441
442 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100443 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
444 // the kSaveEverything call (or use `out` for the address after non-kSaveEverything call).
445 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
446 Register entry_address = temp_is_r0 ? out : temp;
447 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
448 if (call_saves_everything_except_r0 && temp_is_r0) {
449 __ mov(entry_address, ShifterOperand(temp));
450 }
451
Vladimir Markoaad75c62016-10-03 08:46:48 +0000452 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index);
453 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
454 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100455
456 // Store the resolved String to the .bss entry.
457 if (call_saves_everything_except_r0) {
458 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
459 __ str(R0, Address(entry_address));
460 } else {
461 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
462 CodeGeneratorARM::PcRelativePatchInfo* labels =
463 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
464 __ BindTrackedLabel(&labels->movw_label);
465 __ movw(entry_address, /* placeholder */ 0u);
466 __ BindTrackedLabel(&labels->movt_label);
467 __ movt(entry_address, /* placeholder */ 0u);
468 __ BindTrackedLabel(&labels->add_pc_label);
469 __ add(entry_address, entry_address, ShifterOperand(PC));
470 __ str(R0, Address(entry_address));
471 }
472
Vladimir Markoaad75c62016-10-03 08:46:48 +0000473 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000474 RestoreLiveRegisters(codegen, locations);
475
Vladimir Markoaad75c62016-10-03 08:46:48 +0000476 __ b(GetExitLabel());
477 }
478
479 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
480
481 private:
482 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
483};
484
Artem Serovf4d6aee2016-07-11 10:41:45 +0100485class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000486 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000487 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100488 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000489
Alexandre Rames67555f72014-11-18 10:55:16 +0000490 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000491 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100492 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
493 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000494 DCHECK(instruction_->IsCheckCast()
495 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000496
497 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
498 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000499
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000500 if (!is_fatal_) {
501 SaveLiveRegisters(codegen, locations);
502 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000503
504 // We're moving two locations to locations that could overlap, so we need a parallel
505 // move resolver.
506 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000507 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100508 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000509 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100510 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100511 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100512 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
513 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000514
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000515 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100516 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100517 instruction_,
518 instruction_->GetDexPc(),
519 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000520 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700521 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000522 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
523 } else {
524 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100525 arm_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000526 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000527 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000528
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000529 if (!is_fatal_) {
530 RestoreLiveRegisters(codegen, locations);
531 __ b(GetExitLabel());
532 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000533 }
534
Alexandre Rames9931f312015-06-19 14:47:01 +0100535 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
536
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000537 bool IsFatal() const OVERRIDE { return is_fatal_; }
538
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000539 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000540 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000541
542 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
543};
544
Artem Serovf4d6aee2016-07-11 10:41:45 +0100545class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700546 public:
Aart Bik42249c32016-01-07 15:33:50 -0800547 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100548 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700549
550 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800551 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700552 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100553 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000554 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700555 }
556
Alexandre Rames9931f312015-06-19 14:47:01 +0100557 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
558
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700559 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700560 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
561};
562
Artem Serovf4d6aee2016-07-11 10:41:45 +0100563class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100564 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100565 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100566
567 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
568 LocationSummary* locations = instruction_->GetLocations();
569 __ Bind(GetEntryLabel());
570 SaveLiveRegisters(codegen, locations);
571
572 InvokeRuntimeCallingConvention calling_convention;
573 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
574 parallel_move.AddMove(
575 locations->InAt(0),
576 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
577 Primitive::kPrimNot,
578 nullptr);
579 parallel_move.AddMove(
580 locations->InAt(1),
581 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
582 Primitive::kPrimInt,
583 nullptr);
584 parallel_move.AddMove(
585 locations->InAt(2),
586 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
587 Primitive::kPrimNot,
588 nullptr);
589 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
590
591 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100592 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000593 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100594 RestoreLiveRegisters(codegen, locations);
595 __ b(GetExitLabel());
596 }
597
598 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
599
600 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100601 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
602};
603
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100604// Slow path marking an object reference `ref` during a read
605// barrier. The field `obj.field` in the object `obj` holding this
606// reference does not get updated by this slow path after marking (see
607// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
608//
609// This means that after the execution of this slow path, `ref` will
610// always be up-to-date, but `obj.field` may not; i.e., after the
611// flip, `ref` will be a to-space reference, but `obj.field` will
612// probably still be a from-space reference (unless it gets updated by
613// another thread, or if another thread installed another object
614// reference (different from `ref`) in `obj.field`).
Artem Serovf4d6aee2016-07-11 10:41:45 +0100615class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000616 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100617 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location ref)
618 : SlowPathCodeARM(instruction), ref_(ref) {
Roland Levillainc9285912015-12-18 10:38:42 +0000619 DCHECK(kEmitCompilerReadBarrier);
620 }
621
622 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
623
624 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
625 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100626 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000627 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100628 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillainc9285912015-12-18 10:38:42 +0000629 DCHECK(instruction_->IsInstanceFieldGet() ||
630 instruction_->IsStaticFieldGet() ||
631 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100632 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000633 instruction_->IsLoadClass() ||
634 instruction_->IsLoadString() ||
635 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100636 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100637 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
638 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000639 << "Unexpected instruction in read barrier marking slow path: "
640 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000641 // The read barrier instrumentation of object ArrayGet
642 // instructions does not support the HIntermediateAddress
643 // instruction.
644 DCHECK(!(instruction_->IsArrayGet() &&
645 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillainc9285912015-12-18 10:38:42 +0000646
647 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100648 // No need to save live registers; it's taken care of by the
649 // entrypoint. Also, there is no need to update the stack mask,
650 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000651 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100652 DCHECK_NE(ref_reg, SP);
653 DCHECK_NE(ref_reg, LR);
654 DCHECK_NE(ref_reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100655 // IP is used internally by the ReadBarrierMarkRegX entry point
656 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100657 DCHECK_NE(ref_reg, IP);
658 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100659 // "Compact" slow path, saving two moves.
660 //
661 // Instead of using the standard runtime calling convention (input
662 // and output in R0):
663 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100664 // R0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100665 // R0 <- ReadBarrierMark(R0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100666 // ref <- R0
Roland Levillain02b75802016-07-13 11:54:35 +0100667 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100668 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100669 // of a dedicated entrypoint:
670 //
671 // rX <- ReadBarrierMarkRegX(rX)
672 //
673 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100674 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100675 // This runtime call does not require a stack map.
676 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000677 __ b(GetExitLabel());
678 }
679
680 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100681 // The location (register) of the marked object reference.
682 const Location ref_;
Roland Levillainc9285912015-12-18 10:38:42 +0000683
684 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
685};
686
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100687// Slow path marking an object reference `ref` during a read barrier,
688// and if needed, atomically updating the field `obj.field` in the
689// object `obj` holding this reference after marking (contrary to
690// ReadBarrierMarkSlowPathARM above, which never tries to update
691// `obj.field`).
692//
693// This means that after the execution of this slow path, both `ref`
694// and `obj.field` will be up-to-date; i.e., after the flip, both will
695// hold the same to-space reference (unless another thread installed
696// another object reference (different from `ref`) in `obj.field`).
697class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
698 public:
699 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
700 Location ref,
701 Register obj,
702 Location field_offset,
703 Register temp1,
704 Register temp2)
705 : SlowPathCodeARM(instruction),
706 ref_(ref),
707 obj_(obj),
708 field_offset_(field_offset),
709 temp1_(temp1),
710 temp2_(temp2) {
711 DCHECK(kEmitCompilerReadBarrier);
712 }
713
714 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
715
716 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
717 LocationSummary* locations = instruction_->GetLocations();
718 Register ref_reg = ref_.AsRegister<Register>();
719 DCHECK(locations->CanCall());
720 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
721 // This slow path is only used by the UnsafeCASObject intrinsic.
722 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
723 << "Unexpected instruction in read barrier marking and field updating slow path: "
724 << instruction_->DebugName();
725 DCHECK(instruction_->GetLocations()->Intrinsified());
726 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
727 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
728
729 __ Bind(GetEntryLabel());
730
731 // Save the old reference.
732 // Note that we cannot use IP to save the old reference, as IP is
733 // used internally by the ReadBarrierMarkRegX entry point, and we
734 // need the old reference after the call to that entry point.
735 DCHECK_NE(temp1_, IP);
736 __ Mov(temp1_, ref_reg);
737
738 // No need to save live registers; it's taken care of by the
739 // entrypoint. Also, there is no need to update the stack mask,
740 // as this runtime call will not trigger a garbage collection.
741 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
742 DCHECK_NE(ref_reg, SP);
743 DCHECK_NE(ref_reg, LR);
744 DCHECK_NE(ref_reg, PC);
745 // IP is used internally by the ReadBarrierMarkRegX entry point
746 // as a temporary, it cannot be the entry point's input/output.
747 DCHECK_NE(ref_reg, IP);
748 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
749 // "Compact" slow path, saving two moves.
750 //
751 // Instead of using the standard runtime calling convention (input
752 // and output in R0):
753 //
754 // R0 <- ref
755 // R0 <- ReadBarrierMark(R0)
756 // ref <- R0
757 //
758 // we just use rX (the register containing `ref`) as input and output
759 // of a dedicated entrypoint:
760 //
761 // rX <- ReadBarrierMarkRegX(rX)
762 //
763 int32_t entry_point_offset =
764 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
765 // This runtime call does not require a stack map.
766 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
767
768 // If the new reference is different from the old reference,
769 // update the field in the holder (`*(obj_ + field_offset_)`).
770 //
771 // Note that this field could also hold a different object, if
772 // another thread had concurrently changed it. In that case, the
773 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
774 // (CAS) operation below would abort the CAS, leaving the field
775 // as-is.
776 Label done;
777 __ cmp(temp1_, ShifterOperand(ref_reg));
778 __ b(&done, EQ);
779
780 // Update the the holder's field atomically. This may fail if
781 // mutator updates before us, but it's OK. This is achieved
782 // using a strong compare-and-set (CAS) operation with relaxed
783 // memory synchronization ordering, where the expected value is
784 // the old reference and the desired value is the new reference.
785
786 // Convenience aliases.
787 Register base = obj_;
788 // The UnsafeCASObject intrinsic uses a register pair as field
789 // offset ("long offset"), of which only the low part contains
790 // data.
791 Register offset = field_offset_.AsRegisterPairLow<Register>();
792 Register expected = temp1_;
793 Register value = ref_reg;
794 Register tmp_ptr = IP; // Pointer to actual memory.
795 Register tmp = temp2_; // Value in memory.
796
797 __ add(tmp_ptr, base, ShifterOperand(offset));
798
799 if (kPoisonHeapReferences) {
800 __ PoisonHeapReference(expected);
801 if (value == expected) {
802 // Do not poison `value`, as it is the same register as
803 // `expected`, which has just been poisoned.
804 } else {
805 __ PoisonHeapReference(value);
806 }
807 }
808
809 // do {
810 // tmp = [r_ptr] - expected;
811 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
812
Roland Levillain24a4d112016-10-26 13:10:46 +0100813 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100814 __ Bind(&loop_head);
815
816 __ ldrex(tmp, tmp_ptr);
817
818 __ subs(tmp, tmp, ShifterOperand(expected));
819
Roland Levillain24a4d112016-10-26 13:10:46 +0100820 __ it(NE);
821 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100822
Roland Levillain24a4d112016-10-26 13:10:46 +0100823 __ b(&exit_loop, NE);
824
825 __ strex(tmp, value, tmp_ptr);
826 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100827 __ b(&loop_head, EQ);
828
Roland Levillain24a4d112016-10-26 13:10:46 +0100829 __ Bind(&exit_loop);
830
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100831 if (kPoisonHeapReferences) {
832 __ UnpoisonHeapReference(expected);
833 if (value == expected) {
834 // Do not unpoison `value`, as it is the same register as
835 // `expected`, which has just been unpoisoned.
836 } else {
837 __ UnpoisonHeapReference(value);
838 }
839 }
840
841 __ Bind(&done);
842 __ b(GetExitLabel());
843 }
844
845 private:
846 // The location (register) of the marked object reference.
847 const Location ref_;
848 // The register containing the object holding the marked object reference field.
849 const Register obj_;
850 // The location of the offset of the marked reference field within `obj_`.
851 Location field_offset_;
852
853 const Register temp1_;
854 const Register temp2_;
855
856 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
857};
858
Roland Levillain3b359c72015-11-17 19:35:12 +0000859// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100860class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000861 public:
862 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
863 Location out,
864 Location ref,
865 Location obj,
866 uint32_t offset,
867 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100868 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000869 out_(out),
870 ref_(ref),
871 obj_(obj),
872 offset_(offset),
873 index_(index) {
874 DCHECK(kEmitCompilerReadBarrier);
875 // If `obj` is equal to `out` or `ref`, it means the initial object
876 // has been overwritten by (or after) the heap object reference load
877 // to be instrumented, e.g.:
878 //
879 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000880 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000881 //
882 // In that case, we have lost the information about the original
883 // object, and the emitted read barrier cannot work properly.
884 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
885 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
886 }
887
888 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
889 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
890 LocationSummary* locations = instruction_->GetLocations();
891 Register reg_out = out_.AsRegister<Register>();
892 DCHECK(locations->CanCall());
893 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100894 DCHECK(instruction_->IsInstanceFieldGet() ||
895 instruction_->IsStaticFieldGet() ||
896 instruction_->IsArrayGet() ||
897 instruction_->IsInstanceOf() ||
898 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100899 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000900 << "Unexpected instruction in read barrier for heap reference slow path: "
901 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000902 // The read barrier instrumentation of object ArrayGet
903 // instructions does not support the HIntermediateAddress
904 // instruction.
905 DCHECK(!(instruction_->IsArrayGet() &&
906 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +0000907
908 __ Bind(GetEntryLabel());
909 SaveLiveRegisters(codegen, locations);
910
911 // We may have to change the index's value, but as `index_` is a
912 // constant member (like other "inputs" of this slow path),
913 // introduce a copy of it, `index`.
914 Location index = index_;
915 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100916 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000917 if (instruction_->IsArrayGet()) {
918 // Compute the actual memory offset and store it in `index`.
919 Register index_reg = index_.AsRegister<Register>();
920 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
921 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
922 // We are about to change the value of `index_reg` (see the
923 // calls to art::arm::Thumb2Assembler::Lsl and
924 // art::arm::Thumb2Assembler::AddConstant below), but it has
925 // not been saved by the previous call to
926 // art::SlowPathCode::SaveLiveRegisters, as it is a
927 // callee-save register --
928 // art::SlowPathCode::SaveLiveRegisters does not consider
929 // callee-save registers, as it has been designed with the
930 // assumption that callee-save registers are supposed to be
931 // handled by the called function. So, as a callee-save
932 // register, `index_reg` _would_ eventually be saved onto
933 // the stack, but it would be too late: we would have
934 // changed its value earlier. Therefore, we manually save
935 // it here into another freely available register,
936 // `free_reg`, chosen of course among the caller-save
937 // registers (as a callee-save `free_reg` register would
938 // exhibit the same problem).
939 //
940 // Note we could have requested a temporary register from
941 // the register allocator instead; but we prefer not to, as
942 // this is a slow path, and we know we can find a
943 // caller-save register that is available.
944 Register free_reg = FindAvailableCallerSaveRegister(codegen);
945 __ Mov(free_reg, index_reg);
946 index_reg = free_reg;
947 index = Location::RegisterLocation(index_reg);
948 } else {
949 // The initial register stored in `index_` has already been
950 // saved in the call to art::SlowPathCode::SaveLiveRegisters
951 // (as it is not a callee-save register), so we can freely
952 // use it.
953 }
954 // Shifting the index value contained in `index_reg` by the scale
955 // factor (2) cannot overflow in practice, as the runtime is
956 // unable to allocate object arrays with a size larger than
957 // 2^26 - 1 (that is, 2^28 - 4 bytes).
958 __ Lsl(index_reg, index_reg, TIMES_4);
959 static_assert(
960 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
961 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
962 __ AddConstant(index_reg, index_reg, offset_);
963 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100964 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
965 // intrinsics, `index_` is not shifted by a scale factor of 2
966 // (as in the case of ArrayGet), as it is actually an offset
967 // to an object field within an object.
968 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000969 DCHECK(instruction_->GetLocations()->Intrinsified());
970 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
971 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
972 << instruction_->AsInvoke()->GetIntrinsic();
973 DCHECK_EQ(offset_, 0U);
974 DCHECK(index_.IsRegisterPair());
975 // UnsafeGet's offset location is a register pair, the low
976 // part contains the correct offset.
977 index = index_.ToLow();
978 }
979 }
980
981 // We're moving two or three locations to locations that could
982 // overlap, so we need a parallel move resolver.
983 InvokeRuntimeCallingConvention calling_convention;
984 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
985 parallel_move.AddMove(ref_,
986 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
987 Primitive::kPrimNot,
988 nullptr);
989 parallel_move.AddMove(obj_,
990 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
991 Primitive::kPrimNot,
992 nullptr);
993 if (index.IsValid()) {
994 parallel_move.AddMove(index,
995 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
996 Primitive::kPrimInt,
997 nullptr);
998 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
999 } else {
1000 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1001 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1002 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001003 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001004 CheckEntrypointTypes<
1005 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1006 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1007
1008 RestoreLiveRegisters(codegen, locations);
1009 __ b(GetExitLabel());
1010 }
1011
1012 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1013
1014 private:
1015 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1016 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1017 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1018 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1019 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1020 return static_cast<Register>(i);
1021 }
1022 }
1023 // We shall never fail to find a free caller-save register, as
1024 // there are more than two core caller-save registers on ARM
1025 // (meaning it is possible to find one which is different from
1026 // `ref` and `obj`).
1027 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1028 LOG(FATAL) << "Could not find a free caller-save register";
1029 UNREACHABLE();
1030 }
1031
Roland Levillain3b359c72015-11-17 19:35:12 +00001032 const Location out_;
1033 const Location ref_;
1034 const Location obj_;
1035 const uint32_t offset_;
1036 // An additional location containing an index to an array.
1037 // Only used for HArrayGet and the UnsafeGetObject &
1038 // UnsafeGetObjectVolatile intrinsics.
1039 const Location index_;
1040
1041 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1042};
1043
1044// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001045class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001046 public:
1047 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001048 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001049 DCHECK(kEmitCompilerReadBarrier);
1050 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001051
1052 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1053 LocationSummary* locations = instruction_->GetLocations();
1054 Register reg_out = out_.AsRegister<Register>();
1055 DCHECK(locations->CanCall());
1056 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001057 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1058 << "Unexpected instruction in read barrier for GC root slow path: "
1059 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001060
1061 __ Bind(GetEntryLabel());
1062 SaveLiveRegisters(codegen, locations);
1063
1064 InvokeRuntimeCallingConvention calling_convention;
1065 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1066 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001067 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001068 instruction_,
1069 instruction_->GetDexPc(),
1070 this);
1071 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1072 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1073
1074 RestoreLiveRegisters(codegen, locations);
1075 __ b(GetExitLabel());
1076 }
1077
1078 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1079
1080 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001081 const Location out_;
1082 const Location root_;
1083
1084 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1085};
1086
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00001087#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001088// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1089#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -07001090
Aart Bike9f37602015-10-09 11:15:55 -07001091inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001092 switch (cond) {
1093 case kCondEQ: return EQ;
1094 case kCondNE: return NE;
1095 case kCondLT: return LT;
1096 case kCondLE: return LE;
1097 case kCondGT: return GT;
1098 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001099 case kCondB: return LO;
1100 case kCondBE: return LS;
1101 case kCondA: return HI;
1102 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001103 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001104 LOG(FATAL) << "Unreachable";
1105 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001106}
1107
Aart Bike9f37602015-10-09 11:15:55 -07001108// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001109inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001110 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001111 case kCondEQ: return EQ;
1112 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001113 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001114 case kCondLT: return LO;
1115 case kCondLE: return LS;
1116 case kCondGT: return HI;
1117 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001118 // Unsigned remain unchanged.
1119 case kCondB: return LO;
1120 case kCondBE: return LS;
1121 case kCondA: return HI;
1122 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001123 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001124 LOG(FATAL) << "Unreachable";
1125 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001126}
1127
Vladimir Markod6e069b2016-01-18 11:11:01 +00001128inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1129 // The ARM condition codes can express all the necessary branches, see the
1130 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1131 // There is no dex instruction or HIR that would need the missing conditions
1132 // "equal or unordered" or "not equal".
1133 switch (cond) {
1134 case kCondEQ: return EQ;
1135 case kCondNE: return NE /* unordered */;
1136 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1137 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1138 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1139 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1140 default:
1141 LOG(FATAL) << "UNREACHABLE";
1142 UNREACHABLE();
1143 }
1144}
1145
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001146void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001147 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001148}
1149
1150void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001151 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001152}
1153
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001154size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1155 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1156 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001157}
1158
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001159size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1160 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1161 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001162}
1163
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001164size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1165 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1166 return kArmWordSize;
1167}
1168
1169size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1170 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1171 return kArmWordSize;
1172}
1173
Calin Juravle34166012014-12-19 17:22:29 +00001174CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001175 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001176 const CompilerOptions& compiler_options,
1177 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001178 : CodeGenerator(graph,
1179 kNumberOfCoreRegisters,
1180 kNumberOfSRegisters,
1181 kNumberOfRegisterPairs,
1182 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1183 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001184 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1185 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001186 compiler_options,
1187 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001188 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001189 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001190 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001191 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001192 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001193 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001194 uint32_literals_(std::less<uint32_t>(),
1195 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +01001196 method_patches_(MethodReferenceComparator(),
1197 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1198 call_patches_(MethodReferenceComparator(),
1199 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +00001200 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001201 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1202 boot_image_string_patches_(StringReferenceValueComparator(),
1203 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1204 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001205 boot_image_type_patches_(TypeReferenceValueComparator(),
1206 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1207 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001208 boot_image_address_patches_(std::less<uint32_t>(),
1209 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001210 // Always save the LR register to mimic Quick.
1211 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001212}
1213
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001214void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1215 // Ensure that we fix up branches and literal loads and emit the literal pool.
1216 __ FinalizeCode();
1217
1218 // Adjust native pc offsets in stack maps.
1219 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
1220 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
1221 uint32_t new_position = __ GetAdjustedPosition(old_position);
1222 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1223 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001224 // Adjust pc offsets for the disassembly information.
1225 if (disasm_info_ != nullptr) {
1226 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1227 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1228 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1229 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1230 it.second.start = __ GetAdjustedPosition(it.second.start);
1231 it.second.end = __ GetAdjustedPosition(it.second.end);
1232 }
1233 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1234 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1235 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1236 }
1237 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001238
1239 CodeGenerator::Finalize(allocator);
1240}
1241
David Brazdil58282f42016-01-14 12:45:10 +00001242void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001243 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001244 blocked_core_registers_[SP] = true;
1245 blocked_core_registers_[LR] = true;
1246 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001247
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001248 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001249 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001250
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001251 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001252 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001253
David Brazdil58282f42016-01-14 12:45:10 +00001254 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001255 // Stubs do not save callee-save floating point registers. If the graph
1256 // is debuggable, we need to deal with these registers differently. For
1257 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001258 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1259 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1260 }
1261 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001262}
1263
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001264InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001265 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001266 assembler_(codegen->GetAssembler()),
1267 codegen_(codegen) {}
1268
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001269void CodeGeneratorARM::ComputeSpillMask() {
1270 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1271 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001272 // There is no easy instruction to restore just the PC on thumb2. We spill and
1273 // restore another arbitrary register.
1274 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001275 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1276 // We use vpush and vpop for saving and restoring floating point registers, which take
1277 // a SRegister and the number of registers to save/restore after that SRegister. We
1278 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1279 // but in the range.
1280 if (fpu_spill_mask_ != 0) {
1281 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1282 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1283 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1284 fpu_spill_mask_ |= (1 << i);
1285 }
1286 }
1287}
1288
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001289static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001290 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001291}
1292
1293static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001294 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001295}
1296
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001297void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001298 bool skip_overflow_check =
1299 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001300 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001301 __ Bind(&frame_entry_label_);
1302
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001303 if (HasEmptyFrame()) {
1304 return;
1305 }
1306
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001307 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001308 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1309 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1310 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001311 }
1312
Andreas Gampe501fd632015-09-10 16:11:06 -07001313 __ PushList(core_spill_mask_);
1314 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1315 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001316 if (fpu_spill_mask_ != 0) {
1317 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1318 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001319 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001320 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001321 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001322 int adjust = GetFrameSize() - FrameEntrySpillSize();
1323 __ AddConstant(SP, -adjust);
1324 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001325
1326 // Save the current method if we need it. Note that we do not
1327 // do this in HCurrentMethod, as the instruction might have been removed
1328 // in the SSA graph.
1329 if (RequiresCurrentMethod()) {
1330 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1331 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001332}
1333
1334void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001335 if (HasEmptyFrame()) {
1336 __ bx(LR);
1337 return;
1338 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001339 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001340 int adjust = GetFrameSize() - FrameEntrySpillSize();
1341 __ AddConstant(SP, adjust);
1342 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001343 if (fpu_spill_mask_ != 0) {
1344 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1345 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001346 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001347 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001348 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001349 // Pop LR into PC to return.
1350 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1351 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1352 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001353 __ cfi().RestoreState();
1354 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001355}
1356
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001357void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001358 Label* label = GetLabelOf(block);
1359 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001360}
1361
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001362Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001363 switch (type) {
1364 case Primitive::kPrimBoolean:
1365 case Primitive::kPrimByte:
1366 case Primitive::kPrimChar:
1367 case Primitive::kPrimShort:
1368 case Primitive::kPrimInt:
1369 case Primitive::kPrimNot: {
1370 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001371 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001372 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001373 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001374 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001375 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001376 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001377 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001378
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001379 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001380 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001381 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001382 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001383 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001384 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001385 if (calling_convention.GetRegisterAt(index) == R1) {
1386 // Skip R1, and use R2_R3 instead.
1387 gp_index_++;
1388 index++;
1389 }
1390 }
1391 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1392 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001393 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001394
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001395 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001396 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001397 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001398 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1399 }
1400 }
1401
1402 case Primitive::kPrimFloat: {
1403 uint32_t stack_index = stack_index_++;
1404 if (float_index_ % 2 == 0) {
1405 float_index_ = std::max(double_index_, float_index_);
1406 }
1407 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1408 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1409 } else {
1410 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1411 }
1412 }
1413
1414 case Primitive::kPrimDouble: {
1415 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1416 uint32_t stack_index = stack_index_;
1417 stack_index_ += 2;
1418 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1419 uint32_t index = double_index_;
1420 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001421 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001422 calling_convention.GetFpuRegisterAt(index),
1423 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001424 DCHECK(ExpectedPairLayout(result));
1425 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001426 } else {
1427 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001428 }
1429 }
1430
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001431 case Primitive::kPrimVoid:
1432 LOG(FATAL) << "Unexpected parameter type " << type;
1433 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001434 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001435 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001436}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001437
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001438Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001439 switch (type) {
1440 case Primitive::kPrimBoolean:
1441 case Primitive::kPrimByte:
1442 case Primitive::kPrimChar:
1443 case Primitive::kPrimShort:
1444 case Primitive::kPrimInt:
1445 case Primitive::kPrimNot: {
1446 return Location::RegisterLocation(R0);
1447 }
1448
1449 case Primitive::kPrimFloat: {
1450 return Location::FpuRegisterLocation(S0);
1451 }
1452
1453 case Primitive::kPrimLong: {
1454 return Location::RegisterPairLocation(R0, R1);
1455 }
1456
1457 case Primitive::kPrimDouble: {
1458 return Location::FpuRegisterPairLocation(S0, S1);
1459 }
1460
1461 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001462 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001463 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001464
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001465 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001466}
1467
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001468Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1469 return Location::RegisterLocation(kMethodRegisterArgument);
1470}
1471
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001472void CodeGeneratorARM::Move32(Location destination, Location source) {
1473 if (source.Equals(destination)) {
1474 return;
1475 }
1476 if (destination.IsRegister()) {
1477 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001478 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001479 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001480 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001481 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001482 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001483 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001484 } else if (destination.IsFpuRegister()) {
1485 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001486 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001487 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001488 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001489 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001490 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001491 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001492 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001493 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001494 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001495 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001496 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001497 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001498 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001499 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001500 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1501 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001502 }
1503 }
1504}
1505
1506void CodeGeneratorARM::Move64(Location destination, Location source) {
1507 if (source.Equals(destination)) {
1508 return;
1509 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001510 if (destination.IsRegisterPair()) {
1511 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001512 EmitParallelMoves(
1513 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1514 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001515 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001516 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001517 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1518 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001519 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001520 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001521 } else if (source.IsFpuRegisterPair()) {
1522 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1523 destination.AsRegisterPairHigh<Register>(),
1524 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001525 } else {
1526 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001527 DCHECK(ExpectedPairLayout(destination));
1528 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1529 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001530 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001531 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001532 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001533 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1534 SP,
1535 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001536 } else if (source.IsRegisterPair()) {
1537 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1538 source.AsRegisterPairLow<Register>(),
1539 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001540 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001541 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001542 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001543 } else {
1544 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001545 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001546 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001547 if (source.AsRegisterPairLow<Register>() == R1) {
1548 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001549 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1550 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001551 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001552 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001553 SP, destination.GetStackIndex());
1554 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001555 } else if (source.IsFpuRegisterPair()) {
1556 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1557 SP,
1558 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001559 } else {
1560 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001561 EmitParallelMoves(
1562 Location::StackSlot(source.GetStackIndex()),
1563 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001564 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001565 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001566 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1567 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001568 }
1569 }
1570}
1571
Calin Juravle175dc732015-08-25 15:42:32 +01001572void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1573 DCHECK(location.IsRegister());
1574 __ LoadImmediate(location.AsRegister<Register>(), value);
1575}
1576
Calin Juravlee460d1d2015-09-29 04:52:17 +01001577void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001578 HParallelMove move(GetGraph()->GetArena());
1579 move.AddMove(src, dst, dst_type, nullptr);
1580 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001581}
1582
1583void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1584 if (location.IsRegister()) {
1585 locations->AddTemp(location);
1586 } else if (location.IsRegisterPair()) {
1587 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1588 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1589 } else {
1590 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1591 }
1592}
1593
Calin Juravle175dc732015-08-25 15:42:32 +01001594void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1595 HInstruction* instruction,
1596 uint32_t dex_pc,
1597 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001598 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001599 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001600 if (EntrypointRequiresStackMap(entrypoint)) {
1601 RecordPcInfo(instruction, dex_pc, slow_path);
1602 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001603}
1604
Roland Levillaindec8f632016-07-22 17:10:06 +01001605void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1606 HInstruction* instruction,
1607 SlowPathCode* slow_path) {
1608 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001609 GenerateInvokeRuntime(entry_point_offset);
1610}
1611
1612void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001613 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1614 __ blx(LR);
1615}
1616
David Brazdilfc6a86a2015-06-26 10:33:45 +00001617void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001618 DCHECK(!successor->IsExitBlock());
1619
1620 HBasicBlock* block = got->GetBlock();
1621 HInstruction* previous = got->GetPrevious();
1622
1623 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001624 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001625 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1626 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1627 return;
1628 }
1629
1630 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1631 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1632 }
1633 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001634 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001635 }
1636}
1637
David Brazdilfc6a86a2015-06-26 10:33:45 +00001638void LocationsBuilderARM::VisitGoto(HGoto* got) {
1639 got->SetLocations(nullptr);
1640}
1641
1642void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1643 HandleGoto(got, got->GetSuccessor());
1644}
1645
1646void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1647 try_boundary->SetLocations(nullptr);
1648}
1649
1650void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1651 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1652 if (!successor->IsExitBlock()) {
1653 HandleGoto(try_boundary, successor);
1654 }
1655}
1656
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001657void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001658 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001659}
1660
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001661void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001662}
1663
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001664void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1665 Primitive::Type type = instruction->InputAt(0)->GetType();
1666 Location lhs_loc = instruction->GetLocations()->InAt(0);
1667 Location rhs_loc = instruction->GetLocations()->InAt(1);
1668 if (rhs_loc.IsConstant()) {
1669 // 0.0 is the only immediate that can be encoded directly in
1670 // a VCMP instruction.
1671 //
1672 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1673 // specify that in a floating-point comparison, positive zero
1674 // and negative zero are considered equal, so we can use the
1675 // literal 0.0 for both cases here.
1676 //
1677 // Note however that some methods (Float.equal, Float.compare,
1678 // Float.compareTo, Double.equal, Double.compare,
1679 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1680 // StrictMath.min) consider 0.0 to be (strictly) greater than
1681 // -0.0. So if we ever translate calls to these methods into a
1682 // HCompare instruction, we must handle the -0.0 case with
1683 // care here.
1684 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1685 if (type == Primitive::kPrimFloat) {
1686 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1687 } else {
1688 DCHECK_EQ(type, Primitive::kPrimDouble);
1689 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1690 }
1691 } else {
1692 if (type == Primitive::kPrimFloat) {
1693 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1694 } else {
1695 DCHECK_EQ(type, Primitive::kPrimDouble);
1696 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1697 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1698 }
1699 }
1700}
1701
Roland Levillain4fa13f62015-07-06 18:11:54 +01001702void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1703 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001704 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001705 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001706 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001707}
1708
1709void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1710 Label* true_label,
1711 Label* false_label) {
1712 LocationSummary* locations = cond->GetLocations();
1713 Location left = locations->InAt(0);
1714 Location right = locations->InAt(1);
1715 IfCondition if_cond = cond->GetCondition();
1716
1717 Register left_high = left.AsRegisterPairHigh<Register>();
1718 Register left_low = left.AsRegisterPairLow<Register>();
1719 IfCondition true_high_cond = if_cond;
1720 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001721 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001722
1723 // Set the conditions for the test, remembering that == needs to be
1724 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001725 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001726 switch (if_cond) {
1727 case kCondEQ:
1728 case kCondNE:
1729 // Nothing to do.
1730 break;
1731 case kCondLT:
1732 false_high_cond = kCondGT;
1733 break;
1734 case kCondLE:
1735 true_high_cond = kCondLT;
1736 break;
1737 case kCondGT:
1738 false_high_cond = kCondLT;
1739 break;
1740 case kCondGE:
1741 true_high_cond = kCondGT;
1742 break;
Aart Bike9f37602015-10-09 11:15:55 -07001743 case kCondB:
1744 false_high_cond = kCondA;
1745 break;
1746 case kCondBE:
1747 true_high_cond = kCondB;
1748 break;
1749 case kCondA:
1750 false_high_cond = kCondB;
1751 break;
1752 case kCondAE:
1753 true_high_cond = kCondA;
1754 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001755 }
1756 if (right.IsConstant()) {
1757 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1758 int32_t val_low = Low32Bits(value);
1759 int32_t val_high = High32Bits(value);
1760
Vladimir Markoac6ac102015-12-17 12:14:00 +00001761 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001762 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001763 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001764 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001765 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001766 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001767 __ b(true_label, ARMCondition(true_high_cond));
1768 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001769 }
1770 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001771 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001772 } else {
1773 Register right_high = right.AsRegisterPairHigh<Register>();
1774 Register right_low = right.AsRegisterPairLow<Register>();
1775
1776 __ cmp(left_high, ShifterOperand(right_high));
1777 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001778 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001779 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001780 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001781 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001782 __ b(true_label, ARMCondition(true_high_cond));
1783 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001784 }
1785 // Must be equal high, so compare the lows.
1786 __ cmp(left_low, ShifterOperand(right_low));
1787 }
1788 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001789 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001790 __ b(true_label, final_condition);
1791}
1792
David Brazdil0debae72015-11-12 18:37:00 +00001793void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1794 Label* true_target_in,
1795 Label* false_target_in) {
1796 // Generated branching requires both targets to be explicit. If either of the
1797 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1798 Label fallthrough_target;
1799 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1800 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1801
Roland Levillain4fa13f62015-07-06 18:11:54 +01001802 Primitive::Type type = condition->InputAt(0)->GetType();
1803 switch (type) {
1804 case Primitive::kPrimLong:
1805 GenerateLongComparesAndJumps(condition, true_target, false_target);
1806 break;
1807 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001808 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001809 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001810 GenerateFPJumps(condition, true_target, false_target);
1811 break;
1812 default:
1813 LOG(FATAL) << "Unexpected compare type " << type;
1814 }
1815
David Brazdil0debae72015-11-12 18:37:00 +00001816 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001817 __ b(false_target);
1818 }
David Brazdil0debae72015-11-12 18:37:00 +00001819
1820 if (fallthrough_target.IsLinked()) {
1821 __ Bind(&fallthrough_target);
1822 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001823}
1824
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001825void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001826 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001827 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001828 Label* false_target) {
1829 HInstruction* cond = instruction->InputAt(condition_input_index);
1830
1831 if (true_target == nullptr && false_target == nullptr) {
1832 // Nothing to do. The code always falls through.
1833 return;
1834 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001835 // Constant condition, statically compared against "true" (integer value 1).
1836 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001837 if (true_target != nullptr) {
1838 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001839 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001840 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001841 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001842 if (false_target != nullptr) {
1843 __ b(false_target);
1844 }
1845 }
1846 return;
1847 }
1848
1849 // The following code generates these patterns:
1850 // (1) true_target == nullptr && false_target != nullptr
1851 // - opposite condition true => branch to false_target
1852 // (2) true_target != nullptr && false_target == nullptr
1853 // - condition true => branch to true_target
1854 // (3) true_target != nullptr && false_target != nullptr
1855 // - condition true => branch to true_target
1856 // - branch to false_target
1857 if (IsBooleanValueOrMaterializedCondition(cond)) {
1858 // Condition has been materialized, compare the output to 0.
1859 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1860 DCHECK(cond_val.IsRegister());
1861 if (true_target == nullptr) {
1862 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1863 } else {
1864 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001865 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001866 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001867 // Condition has not been materialized. Use its inputs as the comparison and
1868 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001869 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001870
1871 // If this is a long or FP comparison that has been folded into
1872 // the HCondition, generate the comparison directly.
1873 Primitive::Type type = condition->InputAt(0)->GetType();
1874 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1875 GenerateCompareTestAndBranch(condition, true_target, false_target);
1876 return;
1877 }
1878
1879 LocationSummary* locations = cond->GetLocations();
1880 DCHECK(locations->InAt(0).IsRegister());
1881 Register left = locations->InAt(0).AsRegister<Register>();
1882 Location right = locations->InAt(1);
1883 if (right.IsRegister()) {
1884 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001885 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001886 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001887 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001888 }
1889 if (true_target == nullptr) {
1890 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1891 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001892 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001893 }
Dave Allison20dfc792014-06-16 20:44:29 -07001894 }
David Brazdil0debae72015-11-12 18:37:00 +00001895
1896 // If neither branch falls through (case 3), the conditional branch to `true_target`
1897 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1898 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001899 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001900 }
1901}
1902
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001903void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001904 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1905 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001906 locations->SetInAt(0, Location::RequiresRegister());
1907 }
1908}
1909
1910void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001911 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1912 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1913 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1914 nullptr : codegen_->GetLabelOf(true_successor);
1915 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1916 nullptr : codegen_->GetLabelOf(false_successor);
1917 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001918}
1919
1920void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1921 LocationSummary* locations = new (GetGraph()->GetArena())
1922 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001923 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001924 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001925 locations->SetInAt(0, Location::RequiresRegister());
1926 }
1927}
1928
1929void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001930 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001931 GenerateTestAndBranch(deoptimize,
1932 /* condition_input_index */ 0,
1933 slow_path->GetEntryLabel(),
1934 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001935}
Dave Allison20dfc792014-06-16 20:44:29 -07001936
David Brazdil74eb1b22015-12-14 11:44:01 +00001937void LocationsBuilderARM::VisitSelect(HSelect* select) {
1938 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1939 if (Primitive::IsFloatingPointType(select->GetType())) {
1940 locations->SetInAt(0, Location::RequiresFpuRegister());
1941 locations->SetInAt(1, Location::RequiresFpuRegister());
1942 } else {
1943 locations->SetInAt(0, Location::RequiresRegister());
1944 locations->SetInAt(1, Location::RequiresRegister());
1945 }
1946 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1947 locations->SetInAt(2, Location::RequiresRegister());
1948 }
1949 locations->SetOut(Location::SameAsFirstInput());
1950}
1951
1952void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1953 LocationSummary* locations = select->GetLocations();
1954 Label false_target;
1955 GenerateTestAndBranch(select,
1956 /* condition_input_index */ 2,
1957 /* true_target */ nullptr,
1958 &false_target);
1959 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1960 __ Bind(&false_target);
1961}
1962
David Srbecky0cf44932015-12-09 14:09:59 +00001963void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1964 new (GetGraph()->GetArena()) LocationSummary(info);
1965}
1966
David Srbeckyd28f4a02016-03-14 17:14:24 +00001967void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1968 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001969}
1970
1971void CodeGeneratorARM::GenerateNop() {
1972 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001973}
1974
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001975void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001976 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001977 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001978 // Handle the long/FP comparisons made in instruction simplification.
1979 switch (cond->InputAt(0)->GetType()) {
1980 case Primitive::kPrimLong:
1981 locations->SetInAt(0, Location::RequiresRegister());
1982 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001983 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001984 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1985 }
1986 break;
1987
1988 case Primitive::kPrimFloat:
1989 case Primitive::kPrimDouble:
1990 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001991 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001992 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001993 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1994 }
1995 break;
1996
1997 default:
1998 locations->SetInAt(0, Location::RequiresRegister());
1999 locations->SetInAt(1, Location::RegisterOrConstant(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 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002003 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002004}
2005
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002006void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002007 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002008 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002009 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002010
2011 LocationSummary* locations = cond->GetLocations();
2012 Location left = locations->InAt(0);
2013 Location right = locations->InAt(1);
2014 Register out = locations->Out().AsRegister<Register>();
2015 Label true_label, false_label;
2016
2017 switch (cond->InputAt(0)->GetType()) {
2018 default: {
2019 // Integer case.
2020 if (right.IsRegister()) {
2021 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2022 } else {
2023 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002024 __ CmpConstant(left.AsRegister<Register>(),
2025 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002026 }
Aart Bike9f37602015-10-09 11:15:55 -07002027 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002028 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002029 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002030 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002031 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002032 return;
2033 }
2034 case Primitive::kPrimLong:
2035 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2036 break;
2037 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002038 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002039 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002040 GenerateFPJumps(cond, &true_label, &false_label);
2041 break;
2042 }
2043
2044 // Convert the jumps into the result.
2045 Label done_label;
2046
2047 // False case: result = 0.
2048 __ Bind(&false_label);
2049 __ LoadImmediate(out, 0);
2050 __ b(&done_label);
2051
2052 // True case: result = 1.
2053 __ Bind(&true_label);
2054 __ LoadImmediate(out, 1);
2055 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002056}
2057
2058void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002059 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002060}
2061
2062void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002063 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002064}
2065
2066void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002067 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002068}
2069
2070void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002071 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002072}
2073
2074void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002075 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002076}
2077
2078void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002079 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002080}
2081
2082void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002083 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002084}
2085
2086void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002087 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002088}
2089
2090void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002091 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002092}
2093
2094void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002095 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002096}
2097
2098void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002099 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002100}
2101
2102void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002103 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002104}
2105
Aart Bike9f37602015-10-09 11:15:55 -07002106void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002107 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002108}
2109
2110void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002111 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002112}
2113
2114void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002115 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002116}
2117
2118void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002119 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002120}
2121
2122void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002123 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002124}
2125
2126void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002127 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002128}
2129
2130void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002131 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002132}
2133
2134void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002135 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002136}
2137
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002138void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002139 LocationSummary* locations =
2140 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002141 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002142}
2143
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002144void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002145 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002146}
2147
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002148void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2149 LocationSummary* locations =
2150 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2151 locations->SetOut(Location::ConstantLocation(constant));
2152}
2153
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002154void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002155 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002156}
2157
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002158void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002159 LocationSummary* locations =
2160 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002161 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002162}
2163
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002164void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002165 // Will be generated at use site.
2166}
2167
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002168void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2169 LocationSummary* locations =
2170 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2171 locations->SetOut(Location::ConstantLocation(constant));
2172}
2173
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002174void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002175 // Will be generated at use site.
2176}
2177
2178void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2179 LocationSummary* locations =
2180 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2181 locations->SetOut(Location::ConstantLocation(constant));
2182}
2183
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002184void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002185 // Will be generated at use site.
2186}
2187
Calin Juravle27df7582015-04-17 19:12:31 +01002188void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2189 memory_barrier->SetLocations(nullptr);
2190}
2191
2192void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002193 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002194}
2195
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002196void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002197 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002198}
2199
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002200void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002201 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002202}
2203
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002204void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002205 LocationSummary* locations =
2206 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002207 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002208}
2209
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002210void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002211 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002212}
2213
Calin Juravle175dc732015-08-25 15:42:32 +01002214void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2215 // The trampoline uses the same calling convention as dex calling conventions,
2216 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2217 // the method_idx.
2218 HandleInvoke(invoke);
2219}
2220
2221void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2222 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2223}
2224
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002225void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002226 // Explicit clinit checks triggered by static invokes must have been pruned by
2227 // art::PrepareForRegisterAllocation.
2228 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002229
Vladimir Marko68c981f2016-08-26 13:13:33 +01002230 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002231 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002232 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2233 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2234 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002235 return;
2236 }
2237
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002238 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002239
2240 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2241 if (invoke->HasPcRelativeDexCache()) {
2242 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2243 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002244}
2245
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002246static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2247 if (invoke->GetLocations()->Intrinsified()) {
2248 IntrinsicCodeGeneratorARM intrinsic(codegen);
2249 intrinsic.Dispatch(invoke);
2250 return true;
2251 }
2252 return false;
2253}
2254
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002255void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002256 // Explicit clinit checks triggered by static invokes must have been pruned by
2257 // art::PrepareForRegisterAllocation.
2258 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002259
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002260 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2261 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002262 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002263
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002264 LocationSummary* locations = invoke->GetLocations();
2265 codegen_->GenerateStaticOrDirectCall(
2266 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002267 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002268}
2269
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002270void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002271 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002272 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002273}
2274
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002275void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002276 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002277 if (intrinsic.TryDispatch(invoke)) {
2278 return;
2279 }
2280
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002281 HandleInvoke(invoke);
2282}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002283
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002284void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002285 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2286 return;
2287 }
2288
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002289 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002290 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002291 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002292}
2293
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002294void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2295 HandleInvoke(invoke);
2296 // Add the hidden argument.
2297 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2298}
2299
2300void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2301 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002302 LocationSummary* locations = invoke->GetLocations();
2303 Register temp = locations->GetTemp(0).AsRegister<Register>();
2304 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002305 Location receiver = locations->InAt(0);
2306 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2307
Roland Levillain3b359c72015-11-17 19:35:12 +00002308 // Set the hidden argument. This is safe to do this here, as R12
2309 // won't be modified thereafter, before the `blx` (call) instruction.
2310 DCHECK_EQ(R12, hidden_reg);
2311 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002312
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002313 if (receiver.IsStackSlot()) {
2314 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002315 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002316 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2317 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002318 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002319 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002320 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002321 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002322 // Instead of simply (possibly) unpoisoning `temp` here, we should
2323 // emit a read barrier for the previous class reference load.
2324 // However this is not required in practice, as this is an
2325 // intermediate/temporary reference and because the current
2326 // concurrent copying collector keeps the from-space memory
2327 // intact/accessible until the end of the marking phase (the
2328 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002329 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002330 __ LoadFromOffset(kLoadWord, temp, temp,
2331 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2332 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002333 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002334 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002335 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002336 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002337 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002338 // LR = temp->GetEntryPoint();
2339 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2340 // LR();
2341 __ blx(LR);
2342 DCHECK(!codegen_->IsLeafMethod());
2343 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2344}
2345
Roland Levillain88cb1752014-10-20 16:36:47 +01002346void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2347 LocationSummary* locations =
2348 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2349 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002350 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002351 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002352 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2353 break;
2354 }
2355 case Primitive::kPrimLong: {
2356 locations->SetInAt(0, Location::RequiresRegister());
2357 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002358 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002359 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002360
Roland Levillain88cb1752014-10-20 16:36:47 +01002361 case Primitive::kPrimFloat:
2362 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002363 locations->SetInAt(0, Location::RequiresFpuRegister());
2364 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002365 break;
2366
2367 default:
2368 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2369 }
2370}
2371
2372void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2373 LocationSummary* locations = neg->GetLocations();
2374 Location out = locations->Out();
2375 Location in = locations->InAt(0);
2376 switch (neg->GetResultType()) {
2377 case Primitive::kPrimInt:
2378 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002379 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002380 break;
2381
2382 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002383 DCHECK(in.IsRegisterPair());
2384 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2385 __ rsbs(out.AsRegisterPairLow<Register>(),
2386 in.AsRegisterPairLow<Register>(),
2387 ShifterOperand(0));
2388 // We cannot emit an RSC (Reverse Subtract with Carry)
2389 // instruction here, as it does not exist in the Thumb-2
2390 // instruction set. We use the following approach
2391 // using SBC and SUB instead.
2392 //
2393 // out.hi = -C
2394 __ sbc(out.AsRegisterPairHigh<Register>(),
2395 out.AsRegisterPairHigh<Register>(),
2396 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2397 // out.hi = out.hi - in.hi
2398 __ sub(out.AsRegisterPairHigh<Register>(),
2399 out.AsRegisterPairHigh<Register>(),
2400 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2401 break;
2402
Roland Levillain88cb1752014-10-20 16:36:47 +01002403 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002404 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002405 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002406 break;
2407
Roland Levillain88cb1752014-10-20 16:36:47 +01002408 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002409 DCHECK(in.IsFpuRegisterPair());
2410 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2411 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002412 break;
2413
2414 default:
2415 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2416 }
2417}
2418
Roland Levillaindff1f282014-11-05 14:15:05 +00002419void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002420 Primitive::Type result_type = conversion->GetResultType();
2421 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002422 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002423
Roland Levillain5b3ee562015-04-14 16:02:41 +01002424 // The float-to-long, double-to-long and long-to-float type conversions
2425 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002426 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002427 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2428 && result_type == Primitive::kPrimLong)
2429 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002430 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002431 : LocationSummary::kNoCall;
2432 LocationSummary* locations =
2433 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2434
David Brazdilb2bd1c52015-03-25 11:17:37 +00002435 // The Java language does not allow treating boolean as an integral type but
2436 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002437
Roland Levillaindff1f282014-11-05 14:15:05 +00002438 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002439 case Primitive::kPrimByte:
2440 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002441 case Primitive::kPrimLong:
2442 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002443 case Primitive::kPrimBoolean:
2444 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002445 case Primitive::kPrimShort:
2446 case Primitive::kPrimInt:
2447 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002448 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002449 locations->SetInAt(0, Location::RequiresRegister());
2450 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2451 break;
2452
2453 default:
2454 LOG(FATAL) << "Unexpected type conversion from " << input_type
2455 << " to " << result_type;
2456 }
2457 break;
2458
Roland Levillain01a8d712014-11-14 16:27:39 +00002459 case Primitive::kPrimShort:
2460 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002461 case Primitive::kPrimLong:
2462 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002463 case Primitive::kPrimBoolean:
2464 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002465 case Primitive::kPrimByte:
2466 case Primitive::kPrimInt:
2467 case Primitive::kPrimChar:
2468 // Processing a Dex `int-to-short' instruction.
2469 locations->SetInAt(0, Location::RequiresRegister());
2470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2471 break;
2472
2473 default:
2474 LOG(FATAL) << "Unexpected type conversion from " << input_type
2475 << " to " << result_type;
2476 }
2477 break;
2478
Roland Levillain946e1432014-11-11 17:35:19 +00002479 case Primitive::kPrimInt:
2480 switch (input_type) {
2481 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002482 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002483 locations->SetInAt(0, Location::Any());
2484 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2485 break;
2486
2487 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002488 // Processing a Dex `float-to-int' instruction.
2489 locations->SetInAt(0, Location::RequiresFpuRegister());
2490 locations->SetOut(Location::RequiresRegister());
2491 locations->AddTemp(Location::RequiresFpuRegister());
2492 break;
2493
Roland Levillain946e1432014-11-11 17:35:19 +00002494 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002495 // Processing a Dex `double-to-int' instruction.
2496 locations->SetInAt(0, Location::RequiresFpuRegister());
2497 locations->SetOut(Location::RequiresRegister());
2498 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002499 break;
2500
2501 default:
2502 LOG(FATAL) << "Unexpected type conversion from " << input_type
2503 << " to " << result_type;
2504 }
2505 break;
2506
Roland Levillaindff1f282014-11-05 14:15:05 +00002507 case Primitive::kPrimLong:
2508 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002509 case Primitive::kPrimBoolean:
2510 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002511 case Primitive::kPrimByte:
2512 case Primitive::kPrimShort:
2513 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002514 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002515 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002516 locations->SetInAt(0, Location::RequiresRegister());
2517 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2518 break;
2519
Roland Levillain624279f2014-12-04 11:54:28 +00002520 case Primitive::kPrimFloat: {
2521 // Processing a Dex `float-to-long' instruction.
2522 InvokeRuntimeCallingConvention calling_convention;
2523 locations->SetInAt(0, Location::FpuRegisterLocation(
2524 calling_convention.GetFpuRegisterAt(0)));
2525 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2526 break;
2527 }
2528
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002529 case Primitive::kPrimDouble: {
2530 // Processing a Dex `double-to-long' instruction.
2531 InvokeRuntimeCallingConvention calling_convention;
2532 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2533 calling_convention.GetFpuRegisterAt(0),
2534 calling_convention.GetFpuRegisterAt(1)));
2535 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002536 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002537 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002538
2539 default:
2540 LOG(FATAL) << "Unexpected type conversion from " << input_type
2541 << " to " << result_type;
2542 }
2543 break;
2544
Roland Levillain981e4542014-11-14 11:47:14 +00002545 case Primitive::kPrimChar:
2546 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002547 case Primitive::kPrimLong:
2548 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002549 case Primitive::kPrimBoolean:
2550 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002551 case Primitive::kPrimByte:
2552 case Primitive::kPrimShort:
2553 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002554 // Processing a Dex `int-to-char' instruction.
2555 locations->SetInAt(0, Location::RequiresRegister());
2556 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2557 break;
2558
2559 default:
2560 LOG(FATAL) << "Unexpected type conversion from " << input_type
2561 << " to " << result_type;
2562 }
2563 break;
2564
Roland Levillaindff1f282014-11-05 14:15:05 +00002565 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002566 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002567 case Primitive::kPrimBoolean:
2568 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002569 case Primitive::kPrimByte:
2570 case Primitive::kPrimShort:
2571 case Primitive::kPrimInt:
2572 case Primitive::kPrimChar:
2573 // Processing a Dex `int-to-float' instruction.
2574 locations->SetInAt(0, Location::RequiresRegister());
2575 locations->SetOut(Location::RequiresFpuRegister());
2576 break;
2577
Roland Levillain5b3ee562015-04-14 16:02:41 +01002578 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002579 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002580 InvokeRuntimeCallingConvention calling_convention;
2581 locations->SetInAt(0, Location::RegisterPairLocation(
2582 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2583 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002584 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002585 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002586
Roland Levillaincff13742014-11-17 14:32:17 +00002587 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002588 // Processing a Dex `double-to-float' instruction.
2589 locations->SetInAt(0, Location::RequiresFpuRegister());
2590 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002591 break;
2592
2593 default:
2594 LOG(FATAL) << "Unexpected type conversion from " << input_type
2595 << " to " << result_type;
2596 };
2597 break;
2598
Roland Levillaindff1f282014-11-05 14:15:05 +00002599 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002600 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002601 case Primitive::kPrimBoolean:
2602 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002603 case Primitive::kPrimByte:
2604 case Primitive::kPrimShort:
2605 case Primitive::kPrimInt:
2606 case Primitive::kPrimChar:
2607 // Processing a Dex `int-to-double' instruction.
2608 locations->SetInAt(0, Location::RequiresRegister());
2609 locations->SetOut(Location::RequiresFpuRegister());
2610 break;
2611
2612 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002613 // Processing a Dex `long-to-double' instruction.
2614 locations->SetInAt(0, Location::RequiresRegister());
2615 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002616 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002617 locations->AddTemp(Location::RequiresFpuRegister());
2618 break;
2619
Roland Levillaincff13742014-11-17 14:32:17 +00002620 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002621 // Processing a Dex `float-to-double' instruction.
2622 locations->SetInAt(0, Location::RequiresFpuRegister());
2623 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002624 break;
2625
2626 default:
2627 LOG(FATAL) << "Unexpected type conversion from " << input_type
2628 << " to " << result_type;
2629 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002630 break;
2631
2632 default:
2633 LOG(FATAL) << "Unexpected type conversion from " << input_type
2634 << " to " << result_type;
2635 }
2636}
2637
2638void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2639 LocationSummary* locations = conversion->GetLocations();
2640 Location out = locations->Out();
2641 Location in = locations->InAt(0);
2642 Primitive::Type result_type = conversion->GetResultType();
2643 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002644 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002645 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002646 case Primitive::kPrimByte:
2647 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002648 case Primitive::kPrimLong:
2649 // Type conversion from long to byte is a result of code transformations.
2650 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2651 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002652 case Primitive::kPrimBoolean:
2653 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002654 case Primitive::kPrimShort:
2655 case Primitive::kPrimInt:
2656 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002657 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002658 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002659 break;
2660
2661 default:
2662 LOG(FATAL) << "Unexpected type conversion from " << input_type
2663 << " to " << result_type;
2664 }
2665 break;
2666
Roland Levillain01a8d712014-11-14 16:27:39 +00002667 case Primitive::kPrimShort:
2668 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002669 case Primitive::kPrimLong:
2670 // Type conversion from long to short is a result of code transformations.
2671 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2672 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002673 case Primitive::kPrimBoolean:
2674 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002675 case Primitive::kPrimByte:
2676 case Primitive::kPrimInt:
2677 case Primitive::kPrimChar:
2678 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002679 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002680 break;
2681
2682 default:
2683 LOG(FATAL) << "Unexpected type conversion from " << input_type
2684 << " to " << result_type;
2685 }
2686 break;
2687
Roland Levillain946e1432014-11-11 17:35:19 +00002688 case Primitive::kPrimInt:
2689 switch (input_type) {
2690 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002691 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002692 DCHECK(out.IsRegister());
2693 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002694 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002695 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002696 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002697 } else {
2698 DCHECK(in.IsConstant());
2699 DCHECK(in.GetConstant()->IsLongConstant());
2700 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002701 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002702 }
2703 break;
2704
Roland Levillain3f8f9362014-12-02 17:45:01 +00002705 case Primitive::kPrimFloat: {
2706 // Processing a Dex `float-to-int' instruction.
2707 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002708 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002709 __ vmovrs(out.AsRegister<Register>(), temp);
2710 break;
2711 }
2712
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002713 case Primitive::kPrimDouble: {
2714 // Processing a Dex `double-to-int' instruction.
2715 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002716 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002717 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002718 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002719 }
Roland Levillain946e1432014-11-11 17:35:19 +00002720
2721 default:
2722 LOG(FATAL) << "Unexpected type conversion from " << input_type
2723 << " to " << result_type;
2724 }
2725 break;
2726
Roland Levillaindff1f282014-11-05 14:15:05 +00002727 case Primitive::kPrimLong:
2728 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002729 case Primitive::kPrimBoolean:
2730 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002731 case Primitive::kPrimByte:
2732 case Primitive::kPrimShort:
2733 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002734 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002735 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002736 DCHECK(out.IsRegisterPair());
2737 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002738 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002739 // Sign extension.
2740 __ Asr(out.AsRegisterPairHigh<Register>(),
2741 out.AsRegisterPairLow<Register>(),
2742 31);
2743 break;
2744
2745 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002746 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002747 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002748 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002749 break;
2750
Roland Levillaindff1f282014-11-05 14:15:05 +00002751 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002752 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002753 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002754 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002755 break;
2756
2757 default:
2758 LOG(FATAL) << "Unexpected type conversion from " << input_type
2759 << " to " << result_type;
2760 }
2761 break;
2762
Roland Levillain981e4542014-11-14 11:47:14 +00002763 case Primitive::kPrimChar:
2764 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002765 case Primitive::kPrimLong:
2766 // Type conversion from long to char is a result of code transformations.
2767 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2768 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002769 case Primitive::kPrimBoolean:
2770 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002771 case Primitive::kPrimByte:
2772 case Primitive::kPrimShort:
2773 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002774 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002775 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002776 break;
2777
2778 default:
2779 LOG(FATAL) << "Unexpected type conversion from " << input_type
2780 << " to " << result_type;
2781 }
2782 break;
2783
Roland Levillaindff1f282014-11-05 14:15:05 +00002784 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002785 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002786 case Primitive::kPrimBoolean:
2787 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002788 case Primitive::kPrimByte:
2789 case Primitive::kPrimShort:
2790 case Primitive::kPrimInt:
2791 case Primitive::kPrimChar: {
2792 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002793 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2794 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002795 break;
2796 }
2797
Roland Levillain5b3ee562015-04-14 16:02:41 +01002798 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002799 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002800 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002801 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002802 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002803
Roland Levillaincff13742014-11-17 14:32:17 +00002804 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002805 // Processing a Dex `double-to-float' instruction.
2806 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2807 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002808 break;
2809
2810 default:
2811 LOG(FATAL) << "Unexpected type conversion from " << input_type
2812 << " to " << result_type;
2813 };
2814 break;
2815
Roland Levillaindff1f282014-11-05 14:15:05 +00002816 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002817 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002818 case Primitive::kPrimBoolean:
2819 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002820 case Primitive::kPrimByte:
2821 case Primitive::kPrimShort:
2822 case Primitive::kPrimInt:
2823 case Primitive::kPrimChar: {
2824 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002825 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002826 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2827 out.AsFpuRegisterPairLow<SRegister>());
2828 break;
2829 }
2830
Roland Levillain647b9ed2014-11-27 12:06:00 +00002831 case Primitive::kPrimLong: {
2832 // Processing a Dex `long-to-double' instruction.
2833 Register low = in.AsRegisterPairLow<Register>();
2834 Register high = in.AsRegisterPairHigh<Register>();
2835 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2836 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002837 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002838 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002839 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2840 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002841
Roland Levillain682393c2015-04-14 15:57:52 +01002842 // temp_d = int-to-double(high)
2843 __ vmovsr(temp_s, high);
2844 __ vcvtdi(temp_d, temp_s);
2845 // constant_d = k2Pow32EncodingForDouble
2846 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2847 // out_d = unsigned-to-double(low)
2848 __ vmovsr(out_s, low);
2849 __ vcvtdu(out_d, out_s);
2850 // out_d += temp_d * constant_d
2851 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002852 break;
2853 }
2854
Roland Levillaincff13742014-11-17 14:32:17 +00002855 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002856 // Processing a Dex `float-to-double' instruction.
2857 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2858 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002859 break;
2860
2861 default:
2862 LOG(FATAL) << "Unexpected type conversion from " << input_type
2863 << " to " << result_type;
2864 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002865 break;
2866
2867 default:
2868 LOG(FATAL) << "Unexpected type conversion from " << input_type
2869 << " to " << result_type;
2870 }
2871}
2872
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002873void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002874 LocationSummary* locations =
2875 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002876 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002877 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002878 locations->SetInAt(0, Location::RequiresRegister());
2879 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002880 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2881 break;
2882 }
2883
2884 case Primitive::kPrimLong: {
2885 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002886 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002887 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002888 break;
2889 }
2890
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002891 case Primitive::kPrimFloat:
2892 case Primitive::kPrimDouble: {
2893 locations->SetInAt(0, Location::RequiresFpuRegister());
2894 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002895 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002896 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002897 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002898
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002899 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002900 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002901 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002902}
2903
2904void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2905 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002906 Location out = locations->Out();
2907 Location first = locations->InAt(0);
2908 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002909 switch (add->GetResultType()) {
2910 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002911 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002912 __ add(out.AsRegister<Register>(),
2913 first.AsRegister<Register>(),
2914 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002915 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002916 __ AddConstant(out.AsRegister<Register>(),
2917 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002918 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002919 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002920 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002921
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002922 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002923 if (second.IsConstant()) {
2924 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2925 GenerateAddLongConst(out, first, value);
2926 } else {
2927 DCHECK(second.IsRegisterPair());
2928 __ adds(out.AsRegisterPairLow<Register>(),
2929 first.AsRegisterPairLow<Register>(),
2930 ShifterOperand(second.AsRegisterPairLow<Register>()));
2931 __ adc(out.AsRegisterPairHigh<Register>(),
2932 first.AsRegisterPairHigh<Register>(),
2933 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2934 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002935 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002936 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002937
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002938 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002939 __ vadds(out.AsFpuRegister<SRegister>(),
2940 first.AsFpuRegister<SRegister>(),
2941 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002942 break;
2943
2944 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002945 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2946 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2947 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002948 break;
2949
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002950 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002951 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002952 }
2953}
2954
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002955void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002956 LocationSummary* locations =
2957 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002958 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002959 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002960 locations->SetInAt(0, Location::RequiresRegister());
2961 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002962 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2963 break;
2964 }
2965
2966 case Primitive::kPrimLong: {
2967 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002968 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002969 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002970 break;
2971 }
Calin Juravle11351682014-10-23 15:38:15 +01002972 case Primitive::kPrimFloat:
2973 case Primitive::kPrimDouble: {
2974 locations->SetInAt(0, Location::RequiresFpuRegister());
2975 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002976 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002977 break;
Calin Juravle11351682014-10-23 15:38:15 +01002978 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002979 default:
Calin Juravle11351682014-10-23 15:38:15 +01002980 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002981 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002982}
2983
2984void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2985 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002986 Location out = locations->Out();
2987 Location first = locations->InAt(0);
2988 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002989 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002990 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002991 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002992 __ sub(out.AsRegister<Register>(),
2993 first.AsRegister<Register>(),
2994 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002995 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002996 __ AddConstant(out.AsRegister<Register>(),
2997 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002998 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002999 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003000 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003001 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003002
Calin Juravle11351682014-10-23 15:38:15 +01003003 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003004 if (second.IsConstant()) {
3005 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3006 GenerateAddLongConst(out, first, -value);
3007 } else {
3008 DCHECK(second.IsRegisterPair());
3009 __ subs(out.AsRegisterPairLow<Register>(),
3010 first.AsRegisterPairLow<Register>(),
3011 ShifterOperand(second.AsRegisterPairLow<Register>()));
3012 __ sbc(out.AsRegisterPairHigh<Register>(),
3013 first.AsRegisterPairHigh<Register>(),
3014 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3015 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003016 break;
Calin Juravle11351682014-10-23 15:38:15 +01003017 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003018
Calin Juravle11351682014-10-23 15:38:15 +01003019 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003020 __ vsubs(out.AsFpuRegister<SRegister>(),
3021 first.AsFpuRegister<SRegister>(),
3022 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003023 break;
Calin Juravle11351682014-10-23 15:38:15 +01003024 }
3025
3026 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003027 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3028 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3029 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003030 break;
3031 }
3032
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003033
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003034 default:
Calin Juravle11351682014-10-23 15:38:15 +01003035 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003036 }
3037}
3038
Calin Juravle34bacdf2014-10-07 20:23:36 +01003039void LocationsBuilderARM::VisitMul(HMul* mul) {
3040 LocationSummary* locations =
3041 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3042 switch (mul->GetResultType()) {
3043 case Primitive::kPrimInt:
3044 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003045 locations->SetInAt(0, Location::RequiresRegister());
3046 locations->SetInAt(1, Location::RequiresRegister());
3047 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003048 break;
3049 }
3050
Calin Juravleb5bfa962014-10-21 18:02:24 +01003051 case Primitive::kPrimFloat:
3052 case Primitive::kPrimDouble: {
3053 locations->SetInAt(0, Location::RequiresFpuRegister());
3054 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003055 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003056 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003057 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003058
3059 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003060 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003061 }
3062}
3063
3064void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3065 LocationSummary* locations = mul->GetLocations();
3066 Location out = locations->Out();
3067 Location first = locations->InAt(0);
3068 Location second = locations->InAt(1);
3069 switch (mul->GetResultType()) {
3070 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003071 __ mul(out.AsRegister<Register>(),
3072 first.AsRegister<Register>(),
3073 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003074 break;
3075 }
3076 case Primitive::kPrimLong: {
3077 Register out_hi = out.AsRegisterPairHigh<Register>();
3078 Register out_lo = out.AsRegisterPairLow<Register>();
3079 Register in1_hi = first.AsRegisterPairHigh<Register>();
3080 Register in1_lo = first.AsRegisterPairLow<Register>();
3081 Register in2_hi = second.AsRegisterPairHigh<Register>();
3082 Register in2_lo = second.AsRegisterPairLow<Register>();
3083
3084 // Extra checks to protect caused by the existence of R1_R2.
3085 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3086 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3087 DCHECK_NE(out_hi, in1_lo);
3088 DCHECK_NE(out_hi, in2_lo);
3089
3090 // input: in1 - 64 bits, in2 - 64 bits
3091 // output: out
3092 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3093 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3094 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3095
3096 // IP <- in1.lo * in2.hi
3097 __ mul(IP, in1_lo, in2_hi);
3098 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3099 __ mla(out_hi, in1_hi, in2_lo, IP);
3100 // out.lo <- (in1.lo * in2.lo)[31:0];
3101 __ umull(out_lo, IP, in1_lo, in2_lo);
3102 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3103 __ add(out_hi, out_hi, ShifterOperand(IP));
3104 break;
3105 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003106
3107 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003108 __ vmuls(out.AsFpuRegister<SRegister>(),
3109 first.AsFpuRegister<SRegister>(),
3110 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003111 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003112 }
3113
3114 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003115 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3116 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3117 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003118 break;
3119 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003120
3121 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003122 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003123 }
3124}
3125
Zheng Xuc6667102015-05-15 16:08:45 +08003126void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3127 DCHECK(instruction->IsDiv() || instruction->IsRem());
3128 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3129
3130 LocationSummary* locations = instruction->GetLocations();
3131 Location second = locations->InAt(1);
3132 DCHECK(second.IsConstant());
3133
3134 Register out = locations->Out().AsRegister<Register>();
3135 Register dividend = locations->InAt(0).AsRegister<Register>();
3136 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3137 DCHECK(imm == 1 || imm == -1);
3138
3139 if (instruction->IsRem()) {
3140 __ LoadImmediate(out, 0);
3141 } else {
3142 if (imm == 1) {
3143 __ Mov(out, dividend);
3144 } else {
3145 __ rsb(out, dividend, ShifterOperand(0));
3146 }
3147 }
3148}
3149
3150void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3151 DCHECK(instruction->IsDiv() || instruction->IsRem());
3152 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3153
3154 LocationSummary* locations = instruction->GetLocations();
3155 Location second = locations->InAt(1);
3156 DCHECK(second.IsConstant());
3157
3158 Register out = locations->Out().AsRegister<Register>();
3159 Register dividend = locations->InAt(0).AsRegister<Register>();
3160 Register temp = locations->GetTemp(0).AsRegister<Register>();
3161 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003162 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003163 int ctz_imm = CTZ(abs_imm);
3164
3165 if (ctz_imm == 1) {
3166 __ Lsr(temp, dividend, 32 - ctz_imm);
3167 } else {
3168 __ Asr(temp, dividend, 31);
3169 __ Lsr(temp, temp, 32 - ctz_imm);
3170 }
3171 __ add(out, temp, ShifterOperand(dividend));
3172
3173 if (instruction->IsDiv()) {
3174 __ Asr(out, out, ctz_imm);
3175 if (imm < 0) {
3176 __ rsb(out, out, ShifterOperand(0));
3177 }
3178 } else {
3179 __ ubfx(out, out, 0, ctz_imm);
3180 __ sub(out, out, ShifterOperand(temp));
3181 }
3182}
3183
3184void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3185 DCHECK(instruction->IsDiv() || instruction->IsRem());
3186 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3187
3188 LocationSummary* locations = instruction->GetLocations();
3189 Location second = locations->InAt(1);
3190 DCHECK(second.IsConstant());
3191
3192 Register out = locations->Out().AsRegister<Register>();
3193 Register dividend = locations->InAt(0).AsRegister<Register>();
3194 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3195 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3196 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3197
3198 int64_t magic;
3199 int shift;
3200 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3201
3202 __ LoadImmediate(temp1, magic);
3203 __ smull(temp2, temp1, dividend, temp1);
3204
3205 if (imm > 0 && magic < 0) {
3206 __ add(temp1, temp1, ShifterOperand(dividend));
3207 } else if (imm < 0 && magic > 0) {
3208 __ sub(temp1, temp1, ShifterOperand(dividend));
3209 }
3210
3211 if (shift != 0) {
3212 __ Asr(temp1, temp1, shift);
3213 }
3214
3215 if (instruction->IsDiv()) {
3216 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3217 } else {
3218 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3219 // TODO: Strength reduction for mls.
3220 __ LoadImmediate(temp2, imm);
3221 __ mls(out, temp1, temp2, dividend);
3222 }
3223}
3224
3225void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3226 DCHECK(instruction->IsDiv() || instruction->IsRem());
3227 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3228
3229 LocationSummary* locations = instruction->GetLocations();
3230 Location second = locations->InAt(1);
3231 DCHECK(second.IsConstant());
3232
3233 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3234 if (imm == 0) {
3235 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3236 } else if (imm == 1 || imm == -1) {
3237 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003238 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003239 DivRemByPowerOfTwo(instruction);
3240 } else {
3241 DCHECK(imm <= -2 || imm >= 2);
3242 GenerateDivRemWithAnyConstant(instruction);
3243 }
3244}
3245
Calin Juravle7c4954d2014-10-28 16:57:40 +00003246void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003247 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3248 if (div->GetResultType() == Primitive::kPrimLong) {
3249 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003250 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003251 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3252 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003253 } else if (div->GetResultType() == Primitive::kPrimInt &&
3254 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3255 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003256 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003257 }
3258
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003259 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3260
Calin Juravle7c4954d2014-10-28 16:57:40 +00003261 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003262 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003263 if (div->InputAt(1)->IsConstant()) {
3264 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003265 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003266 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003267 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3268 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003269 // No temp register required.
3270 } else {
3271 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003272 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003273 locations->AddTemp(Location::RequiresRegister());
3274 }
3275 }
3276 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003277 locations->SetInAt(0, Location::RequiresRegister());
3278 locations->SetInAt(1, Location::RequiresRegister());
3279 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3280 } else {
3281 InvokeRuntimeCallingConvention calling_convention;
3282 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3283 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3284 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3285 // we only need the former.
3286 locations->SetOut(Location::RegisterLocation(R0));
3287 }
Calin Juravled0d48522014-11-04 16:40:20 +00003288 break;
3289 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003290 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003291 InvokeRuntimeCallingConvention calling_convention;
3292 locations->SetInAt(0, Location::RegisterPairLocation(
3293 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3294 locations->SetInAt(1, Location::RegisterPairLocation(
3295 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003296 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003297 break;
3298 }
3299 case Primitive::kPrimFloat:
3300 case Primitive::kPrimDouble: {
3301 locations->SetInAt(0, Location::RequiresFpuRegister());
3302 locations->SetInAt(1, Location::RequiresFpuRegister());
3303 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3304 break;
3305 }
3306
3307 default:
3308 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3309 }
3310}
3311
3312void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3313 LocationSummary* locations = div->GetLocations();
3314 Location out = locations->Out();
3315 Location first = locations->InAt(0);
3316 Location second = locations->InAt(1);
3317
3318 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003319 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003320 if (second.IsConstant()) {
3321 GenerateDivRemConstantIntegral(div);
3322 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003323 __ sdiv(out.AsRegister<Register>(),
3324 first.AsRegister<Register>(),
3325 second.AsRegister<Register>());
3326 } else {
3327 InvokeRuntimeCallingConvention calling_convention;
3328 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3329 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3330 DCHECK_EQ(R0, out.AsRegister<Register>());
3331
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003332 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003333 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003334 }
Calin Juravled0d48522014-11-04 16:40:20 +00003335 break;
3336 }
3337
Calin Juravle7c4954d2014-10-28 16:57:40 +00003338 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003339 InvokeRuntimeCallingConvention calling_convention;
3340 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3341 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3342 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3343 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3344 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003345 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003346
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003347 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003348 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003349 break;
3350 }
3351
3352 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003353 __ vdivs(out.AsFpuRegister<SRegister>(),
3354 first.AsFpuRegister<SRegister>(),
3355 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003356 break;
3357 }
3358
3359 case Primitive::kPrimDouble: {
3360 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3361 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3362 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3363 break;
3364 }
3365
3366 default:
3367 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3368 }
3369}
3370
Calin Juravlebacfec32014-11-14 15:54:36 +00003371void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003372 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003373
3374 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003375 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003376 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3377 // sdiv will be replaced by other instruction sequence.
3378 call_kind = LocationSummary::kNoCall;
3379 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3380 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003381 // Have hardware divide instruction for int, do it with three instructions.
3382 call_kind = LocationSummary::kNoCall;
3383 }
3384
Calin Juravlebacfec32014-11-14 15:54:36 +00003385 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3386
Calin Juravled2ec87d2014-12-08 14:24:46 +00003387 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003388 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003389 if (rem->InputAt(1)->IsConstant()) {
3390 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003391 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003392 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003393 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3394 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003395 // No temp register required.
3396 } else {
3397 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003398 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003399 locations->AddTemp(Location::RequiresRegister());
3400 }
3401 }
3402 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003403 locations->SetInAt(0, Location::RequiresRegister());
3404 locations->SetInAt(1, Location::RequiresRegister());
3405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3406 locations->AddTemp(Location::RequiresRegister());
3407 } else {
3408 InvokeRuntimeCallingConvention calling_convention;
3409 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3410 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3411 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3412 // we only need the latter.
3413 locations->SetOut(Location::RegisterLocation(R1));
3414 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003415 break;
3416 }
3417 case Primitive::kPrimLong: {
3418 InvokeRuntimeCallingConvention calling_convention;
3419 locations->SetInAt(0, Location::RegisterPairLocation(
3420 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3421 locations->SetInAt(1, Location::RegisterPairLocation(
3422 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3423 // The runtime helper puts the output in R2,R3.
3424 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3425 break;
3426 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003427 case Primitive::kPrimFloat: {
3428 InvokeRuntimeCallingConvention calling_convention;
3429 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3430 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3431 locations->SetOut(Location::FpuRegisterLocation(S0));
3432 break;
3433 }
3434
Calin Juravlebacfec32014-11-14 15:54:36 +00003435 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003436 InvokeRuntimeCallingConvention calling_convention;
3437 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3438 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3439 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3440 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3441 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003442 break;
3443 }
3444
3445 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003446 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003447 }
3448}
3449
3450void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3451 LocationSummary* locations = rem->GetLocations();
3452 Location out = locations->Out();
3453 Location first = locations->InAt(0);
3454 Location second = locations->InAt(1);
3455
Calin Juravled2ec87d2014-12-08 14:24:46 +00003456 Primitive::Type type = rem->GetResultType();
3457 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003458 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003459 if (second.IsConstant()) {
3460 GenerateDivRemConstantIntegral(rem);
3461 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003462 Register reg1 = first.AsRegister<Register>();
3463 Register reg2 = second.AsRegister<Register>();
3464 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003465
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003466 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003467 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003468 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003469 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003470 } else {
3471 InvokeRuntimeCallingConvention calling_convention;
3472 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3473 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3474 DCHECK_EQ(R1, out.AsRegister<Register>());
3475
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003476 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003477 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003478 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003479 break;
3480 }
3481
3482 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003483 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003484 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003485 break;
3486 }
3487
Calin Juravled2ec87d2014-12-08 14:24:46 +00003488 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003489 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003490 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003491 break;
3492 }
3493
Calin Juravlebacfec32014-11-14 15:54:36 +00003494 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003495 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003496 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003497 break;
3498 }
3499
3500 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003501 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003502 }
3503}
3504
Calin Juravled0d48522014-11-04 16:40:20 +00003505void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003506 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003507 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003508}
3509
3510void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003511 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003512 codegen_->AddSlowPath(slow_path);
3513
3514 LocationSummary* locations = instruction->GetLocations();
3515 Location value = locations->InAt(0);
3516
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003517 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003518 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003519 case Primitive::kPrimByte:
3520 case Primitive::kPrimChar:
3521 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003522 case Primitive::kPrimInt: {
3523 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003524 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003525 } else {
3526 DCHECK(value.IsConstant()) << value;
3527 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3528 __ b(slow_path->GetEntryLabel());
3529 }
3530 }
3531 break;
3532 }
3533 case Primitive::kPrimLong: {
3534 if (value.IsRegisterPair()) {
3535 __ orrs(IP,
3536 value.AsRegisterPairLow<Register>(),
3537 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3538 __ b(slow_path->GetEntryLabel(), EQ);
3539 } else {
3540 DCHECK(value.IsConstant()) << value;
3541 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3542 __ b(slow_path->GetEntryLabel());
3543 }
3544 }
3545 break;
3546 default:
3547 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3548 }
3549 }
Calin Juravled0d48522014-11-04 16:40:20 +00003550}
3551
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003552void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3553 Register in = locations->InAt(0).AsRegister<Register>();
3554 Location rhs = locations->InAt(1);
3555 Register out = locations->Out().AsRegister<Register>();
3556
3557 if (rhs.IsConstant()) {
3558 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3559 // so map all rotations to a +ve. equivalent in that range.
3560 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3561 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3562 if (rot) {
3563 // Rotate, mapping left rotations to right equivalents if necessary.
3564 // (e.g. left by 2 bits == right by 30.)
3565 __ Ror(out, in, rot);
3566 } else if (out != in) {
3567 __ Mov(out, in);
3568 }
3569 } else {
3570 __ Ror(out, in, rhs.AsRegister<Register>());
3571 }
3572}
3573
3574// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3575// rotates by swapping input regs (effectively rotating by the first 32-bits of
3576// a larger rotation) or flipping direction (thus treating larger right/left
3577// rotations as sub-word sized rotations in the other direction) as appropriate.
3578void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3579 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3580 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3581 Location rhs = locations->InAt(1);
3582 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3583 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3584
3585 if (rhs.IsConstant()) {
3586 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3587 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003588 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003589 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3590 // logic below to a simple pair of binary orr.
3591 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3592 if (rot >= kArmBitsPerWord) {
3593 rot -= kArmBitsPerWord;
3594 std::swap(in_reg_hi, in_reg_lo);
3595 }
3596 // Rotate, or mov to out for zero or word size rotations.
3597 if (rot != 0u) {
3598 __ Lsr(out_reg_hi, in_reg_hi, rot);
3599 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3600 __ Lsr(out_reg_lo, in_reg_lo, rot);
3601 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3602 } else {
3603 __ Mov(out_reg_lo, in_reg_lo);
3604 __ Mov(out_reg_hi, in_reg_hi);
3605 }
3606 } else {
3607 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3608 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3609 Label end;
3610 Label shift_by_32_plus_shift_right;
3611
3612 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3613 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3614 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3615 __ b(&shift_by_32_plus_shift_right, CC);
3616
3617 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3618 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3619 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3620 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3621 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3622 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3623 __ Lsr(shift_left, in_reg_hi, shift_right);
3624 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3625 __ b(&end);
3626
3627 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3628 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3629 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3630 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3631 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3632 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3633 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3634 __ Lsl(shift_right, in_reg_hi, shift_left);
3635 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3636
3637 __ Bind(&end);
3638 }
3639}
Roland Levillain22c49222016-03-18 14:04:28 +00003640
3641void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003642 LocationSummary* locations =
3643 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3644 switch (ror->GetResultType()) {
3645 case Primitive::kPrimInt: {
3646 locations->SetInAt(0, Location::RequiresRegister());
3647 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3648 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3649 break;
3650 }
3651 case Primitive::kPrimLong: {
3652 locations->SetInAt(0, Location::RequiresRegister());
3653 if (ror->InputAt(1)->IsConstant()) {
3654 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3655 } else {
3656 locations->SetInAt(1, Location::RequiresRegister());
3657 locations->AddTemp(Location::RequiresRegister());
3658 locations->AddTemp(Location::RequiresRegister());
3659 }
3660 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3661 break;
3662 }
3663 default:
3664 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3665 }
3666}
3667
Roland Levillain22c49222016-03-18 14:04:28 +00003668void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003669 LocationSummary* locations = ror->GetLocations();
3670 Primitive::Type type = ror->GetResultType();
3671 switch (type) {
3672 case Primitive::kPrimInt: {
3673 HandleIntegerRotate(locations);
3674 break;
3675 }
3676 case Primitive::kPrimLong: {
3677 HandleLongRotate(locations);
3678 break;
3679 }
3680 default:
3681 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003682 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003683 }
3684}
3685
Calin Juravle9aec02f2014-11-18 23:06:35 +00003686void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3687 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3688
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003689 LocationSummary* locations =
3690 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003691
3692 switch (op->GetResultType()) {
3693 case Primitive::kPrimInt: {
3694 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003695 if (op->InputAt(1)->IsConstant()) {
3696 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3697 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3698 } else {
3699 locations->SetInAt(1, Location::RequiresRegister());
3700 // Make the output overlap, as it will be used to hold the masked
3701 // second input.
3702 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3703 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003704 break;
3705 }
3706 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003707 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003708 if (op->InputAt(1)->IsConstant()) {
3709 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3710 // For simplicity, use kOutputOverlap even though we only require that low registers
3711 // don't clash with high registers which the register allocator currently guarantees.
3712 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3713 } else {
3714 locations->SetInAt(1, Location::RequiresRegister());
3715 locations->AddTemp(Location::RequiresRegister());
3716 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3717 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003718 break;
3719 }
3720 default:
3721 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3722 }
3723}
3724
3725void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3726 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3727
3728 LocationSummary* locations = op->GetLocations();
3729 Location out = locations->Out();
3730 Location first = locations->InAt(0);
3731 Location second = locations->InAt(1);
3732
3733 Primitive::Type type = op->GetResultType();
3734 switch (type) {
3735 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003736 Register out_reg = out.AsRegister<Register>();
3737 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003738 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003739 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003740 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003741 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003742 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003743 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003744 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003745 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003746 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003747 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003748 }
3749 } else {
3750 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003751 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003752 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003753 __ Mov(out_reg, first_reg);
3754 } else if (op->IsShl()) {
3755 __ Lsl(out_reg, first_reg, shift_value);
3756 } else if (op->IsShr()) {
3757 __ Asr(out_reg, first_reg, shift_value);
3758 } else {
3759 __ Lsr(out_reg, first_reg, shift_value);
3760 }
3761 }
3762 break;
3763 }
3764 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003765 Register o_h = out.AsRegisterPairHigh<Register>();
3766 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003767
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003768 Register high = first.AsRegisterPairHigh<Register>();
3769 Register low = first.AsRegisterPairLow<Register>();
3770
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003771 if (second.IsRegister()) {
3772 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003773
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003774 Register second_reg = second.AsRegister<Register>();
3775
3776 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003777 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003778 // Shift the high part
3779 __ Lsl(o_h, high, o_l);
3780 // Shift the low part and `or` what overflew on the high part
3781 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3782 __ Lsr(temp, low, temp);
3783 __ orr(o_h, o_h, ShifterOperand(temp));
3784 // If the shift is > 32 bits, override the high part
3785 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3786 __ it(PL);
3787 __ Lsl(o_h, low, temp, PL);
3788 // Shift the low part
3789 __ Lsl(o_l, low, o_l);
3790 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003791 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003792 // Shift the low part
3793 __ Lsr(o_l, low, o_h);
3794 // Shift the high part and `or` what underflew on the low part
3795 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3796 __ Lsl(temp, high, temp);
3797 __ orr(o_l, o_l, ShifterOperand(temp));
3798 // If the shift is > 32 bits, override the low part
3799 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3800 __ it(PL);
3801 __ Asr(o_l, high, temp, PL);
3802 // Shift the high part
3803 __ Asr(o_h, high, o_h);
3804 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003805 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003806 // same as Shr except we use `Lsr`s and not `Asr`s
3807 __ Lsr(o_l, low, o_h);
3808 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3809 __ Lsl(temp, high, temp);
3810 __ orr(o_l, o_l, ShifterOperand(temp));
3811 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3812 __ it(PL);
3813 __ Lsr(o_l, high, temp, PL);
3814 __ Lsr(o_h, high, o_h);
3815 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003816 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003817 // Register allocator doesn't create partial overlap.
3818 DCHECK_NE(o_l, high);
3819 DCHECK_NE(o_h, low);
3820 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003821 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003822 if (shift_value > 32) {
3823 if (op->IsShl()) {
3824 __ Lsl(o_h, low, shift_value - 32);
3825 __ LoadImmediate(o_l, 0);
3826 } else if (op->IsShr()) {
3827 __ Asr(o_l, high, shift_value - 32);
3828 __ Asr(o_h, high, 31);
3829 } else {
3830 __ Lsr(o_l, high, shift_value - 32);
3831 __ LoadImmediate(o_h, 0);
3832 }
3833 } else if (shift_value == 32) {
3834 if (op->IsShl()) {
3835 __ mov(o_h, ShifterOperand(low));
3836 __ LoadImmediate(o_l, 0);
3837 } else if (op->IsShr()) {
3838 __ mov(o_l, ShifterOperand(high));
3839 __ Asr(o_h, high, 31);
3840 } else {
3841 __ mov(o_l, ShifterOperand(high));
3842 __ LoadImmediate(o_h, 0);
3843 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003844 } else if (shift_value == 1) {
3845 if (op->IsShl()) {
3846 __ Lsls(o_l, low, 1);
3847 __ adc(o_h, high, ShifterOperand(high));
3848 } else if (op->IsShr()) {
3849 __ Asrs(o_h, high, 1);
3850 __ Rrx(o_l, low);
3851 } else {
3852 __ Lsrs(o_h, high, 1);
3853 __ Rrx(o_l, low);
3854 }
3855 } else {
3856 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003857 if (op->IsShl()) {
3858 __ Lsl(o_h, high, shift_value);
3859 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3860 __ Lsl(o_l, low, shift_value);
3861 } else if (op->IsShr()) {
3862 __ Lsr(o_l, low, shift_value);
3863 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3864 __ Asr(o_h, high, shift_value);
3865 } else {
3866 __ Lsr(o_l, low, shift_value);
3867 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3868 __ Lsr(o_h, high, shift_value);
3869 }
3870 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003871 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003872 break;
3873 }
3874 default:
3875 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003876 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003877 }
3878}
3879
3880void LocationsBuilderARM::VisitShl(HShl* shl) {
3881 HandleShift(shl);
3882}
3883
3884void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3885 HandleShift(shl);
3886}
3887
3888void LocationsBuilderARM::VisitShr(HShr* shr) {
3889 HandleShift(shr);
3890}
3891
3892void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3893 HandleShift(shr);
3894}
3895
3896void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3897 HandleShift(ushr);
3898}
3899
3900void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3901 HandleShift(ushr);
3902}
3903
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003904void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003905 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003906 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003907 if (instruction->IsStringAlloc()) {
3908 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3909 } else {
3910 InvokeRuntimeCallingConvention calling_convention;
3911 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3912 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3913 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003914 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003915}
3916
3917void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003918 // Note: if heap poisoning is enabled, the entry point takes cares
3919 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003920 if (instruction->IsStringAlloc()) {
3921 // String is allocated through StringFactory. Call NewEmptyString entry point.
3922 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003923 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003924 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3925 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3926 __ blx(LR);
3927 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3928 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003929 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003930 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3931 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003932}
3933
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003934void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3935 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003936 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003937 InvokeRuntimeCallingConvention calling_convention;
3938 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003939 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003940 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003941 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003942}
3943
3944void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3945 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003946 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003947 // Note: if heap poisoning is enabled, the entry point takes cares
3948 // of poisoning the reference.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003949 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003950 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003951}
3952
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003953void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003954 LocationSummary* locations =
3955 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003956 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3957 if (location.IsStackSlot()) {
3958 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3959 } else if (location.IsDoubleStackSlot()) {
3960 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003961 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003962 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003963}
3964
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003965void InstructionCodeGeneratorARM::VisitParameterValue(
3966 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003967 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003968}
3969
3970void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3971 LocationSummary* locations =
3972 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3973 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3974}
3975
3976void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3977 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003978}
3979
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003980void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003981 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003982 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003983 locations->SetInAt(0, Location::RequiresRegister());
3984 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003985}
3986
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003987void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3988 LocationSummary* locations = not_->GetLocations();
3989 Location out = locations->Out();
3990 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003991 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003992 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003993 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003994 break;
3995
3996 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003997 __ mvn(out.AsRegisterPairLow<Register>(),
3998 ShifterOperand(in.AsRegisterPairLow<Register>()));
3999 __ mvn(out.AsRegisterPairHigh<Register>(),
4000 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004001 break;
4002
4003 default:
4004 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4005 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004006}
4007
David Brazdil66d126e2015-04-03 16:02:44 +01004008void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4009 LocationSummary* locations =
4010 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4011 locations->SetInAt(0, Location::RequiresRegister());
4012 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4013}
4014
4015void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004016 LocationSummary* locations = bool_not->GetLocations();
4017 Location out = locations->Out();
4018 Location in = locations->InAt(0);
4019 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4020}
4021
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004022void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004023 LocationSummary* locations =
4024 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004025 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004026 case Primitive::kPrimBoolean:
4027 case Primitive::kPrimByte:
4028 case Primitive::kPrimShort:
4029 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004030 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004031 case Primitive::kPrimLong: {
4032 locations->SetInAt(0, Location::RequiresRegister());
4033 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004034 // Output overlaps because it is written before doing the low comparison.
4035 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004036 break;
4037 }
4038 case Primitive::kPrimFloat:
4039 case Primitive::kPrimDouble: {
4040 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004041 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004042 locations->SetOut(Location::RequiresRegister());
4043 break;
4044 }
4045 default:
4046 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4047 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004048}
4049
4050void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004051 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004052 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004053 Location left = locations->InAt(0);
4054 Location right = locations->InAt(1);
4055
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004056 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004057 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004058 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004059 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004060 case Primitive::kPrimBoolean:
4061 case Primitive::kPrimByte:
4062 case Primitive::kPrimShort:
4063 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004064 case Primitive::kPrimInt: {
4065 __ LoadImmediate(out, 0);
4066 __ cmp(left.AsRegister<Register>(),
4067 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4068 less_cond = LT;
4069 break;
4070 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004071 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004072 __ cmp(left.AsRegisterPairHigh<Register>(),
4073 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004074 __ b(&less, LT);
4075 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004076 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004077 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004078 __ cmp(left.AsRegisterPairLow<Register>(),
4079 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004080 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004081 break;
4082 }
4083 case Primitive::kPrimFloat:
4084 case Primitive::kPrimDouble: {
4085 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004086 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004087 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004088 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004089 break;
4090 }
4091 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004092 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004093 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004094 }
Aart Bika19616e2016-02-01 18:57:58 -08004095
Calin Juravleddb7df22014-11-25 20:56:51 +00004096 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004097 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004098
4099 __ Bind(&greater);
4100 __ LoadImmediate(out, 1);
4101 __ b(&done);
4102
4103 __ Bind(&less);
4104 __ LoadImmediate(out, -1);
4105
4106 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004107}
4108
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004109void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004110 LocationSummary* locations =
4111 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004112 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004113 locations->SetInAt(i, Location::Any());
4114 }
4115 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004116}
4117
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004118void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004119 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004120}
4121
Roland Levillainc9285912015-12-18 10:38:42 +00004122void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4123 // TODO (ported from quick): revisit ARM barrier kinds.
4124 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004125 switch (kind) {
4126 case MemBarrierKind::kAnyStore:
4127 case MemBarrierKind::kLoadAny:
4128 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004129 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004130 break;
4131 }
4132 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004133 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004134 break;
4135 }
4136 default:
4137 LOG(FATAL) << "Unexpected memory barrier " << kind;
4138 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004139 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004140}
4141
4142void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4143 uint32_t offset,
4144 Register out_lo,
4145 Register out_hi) {
4146 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004147 // Ensure `out_lo` is different from `addr`, so that loading
4148 // `offset` into `out_lo` does not clutter `addr`.
4149 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004150 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004151 __ add(IP, addr, ShifterOperand(out_lo));
4152 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004153 }
4154 __ ldrexd(out_lo, out_hi, addr);
4155}
4156
4157void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4158 uint32_t offset,
4159 Register value_lo,
4160 Register value_hi,
4161 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004162 Register temp2,
4163 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004164 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004165 if (offset != 0) {
4166 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004167 __ add(IP, addr, ShifterOperand(temp1));
4168 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004169 }
4170 __ Bind(&fail);
4171 // We need a load followed by store. (The address used in a STREX instruction must
4172 // be the same as the address in the most recently executed LDREX instruction.)
4173 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004174 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004175 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004176 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004177}
4178
4179void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4180 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4181
Nicolas Geoffray39468442014-09-02 15:17:15 +01004182 LocationSummary* locations =
4183 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004184 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004185
Calin Juravle52c48962014-12-16 17:02:57 +00004186 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004187 if (Primitive::IsFloatingPointType(field_type)) {
4188 locations->SetInAt(1, Location::RequiresFpuRegister());
4189 } else {
4190 locations->SetInAt(1, Location::RequiresRegister());
4191 }
4192
Calin Juravle52c48962014-12-16 17:02:57 +00004193 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004194 bool generate_volatile = field_info.IsVolatile()
4195 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004196 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004197 bool needs_write_barrier =
4198 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004199 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004200 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004201 if (needs_write_barrier) {
4202 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004203 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004204 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004205 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004206 // - registers need to be consecutive
4207 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004208 // We don't test for ARM yet, and the assertion makes sure that we
4209 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004210 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4211
4212 locations->AddTemp(Location::RequiresRegister());
4213 locations->AddTemp(Location::RequiresRegister());
4214 if (field_type == Primitive::kPrimDouble) {
4215 // For doubles we need two more registers to copy the value.
4216 locations->AddTemp(Location::RegisterLocation(R2));
4217 locations->AddTemp(Location::RegisterLocation(R3));
4218 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004219 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004220}
4221
Calin Juravle52c48962014-12-16 17:02:57 +00004222void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004223 const FieldInfo& field_info,
4224 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004225 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4226
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004227 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004228 Register base = locations->InAt(0).AsRegister<Register>();
4229 Location value = locations->InAt(1);
4230
4231 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004232 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004233 Primitive::Type field_type = field_info.GetFieldType();
4234 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004235 bool needs_write_barrier =
4236 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004237
4238 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004239 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004240 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004241
4242 switch (field_type) {
4243 case Primitive::kPrimBoolean:
4244 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004245 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004246 break;
4247 }
4248
4249 case Primitive::kPrimShort:
4250 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004251 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004252 break;
4253 }
4254
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004255 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004256 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004257 if (kPoisonHeapReferences && needs_write_barrier) {
4258 // Note that in the case where `value` is a null reference,
4259 // we do not enter this block, as a null reference does not
4260 // need poisoning.
4261 DCHECK_EQ(field_type, Primitive::kPrimNot);
4262 Register temp = locations->GetTemp(0).AsRegister<Register>();
4263 __ Mov(temp, value.AsRegister<Register>());
4264 __ PoisonHeapReference(temp);
4265 __ StoreToOffset(kStoreWord, temp, base, offset);
4266 } else {
4267 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4268 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004269 break;
4270 }
4271
4272 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004273 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004274 GenerateWideAtomicStore(base, offset,
4275 value.AsRegisterPairLow<Register>(),
4276 value.AsRegisterPairHigh<Register>(),
4277 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004278 locations->GetTemp(1).AsRegister<Register>(),
4279 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004280 } else {
4281 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004282 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004283 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004284 break;
4285 }
4286
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004287 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004288 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004289 break;
4290 }
4291
4292 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004293 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004294 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004295 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4296 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4297
4298 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4299
4300 GenerateWideAtomicStore(base, offset,
4301 value_reg_lo,
4302 value_reg_hi,
4303 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004304 locations->GetTemp(3).AsRegister<Register>(),
4305 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004306 } else {
4307 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004308 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004309 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004310 break;
4311 }
4312
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004313 case Primitive::kPrimVoid:
4314 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004315 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004316 }
Calin Juravle52c48962014-12-16 17:02:57 +00004317
Calin Juravle77520bc2015-01-12 18:45:46 +00004318 // Longs and doubles are handled in the switch.
4319 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4320 codegen_->MaybeRecordImplicitNullCheck(instruction);
4321 }
4322
4323 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4324 Register temp = locations->GetTemp(0).AsRegister<Register>();
4325 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004326 codegen_->MarkGCCard(
4327 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004328 }
4329
Calin Juravle52c48962014-12-16 17:02:57 +00004330 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004331 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004332 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004333}
4334
Calin Juravle52c48962014-12-16 17:02:57 +00004335void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4336 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004337
4338 bool object_field_get_with_read_barrier =
4339 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004340 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004341 new (GetGraph()->GetArena()) LocationSummary(instruction,
4342 object_field_get_with_read_barrier ?
4343 LocationSummary::kCallOnSlowPath :
4344 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004345 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004346 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004347 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004348 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004349
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004350 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004351 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004352 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004353 // The output overlaps in case of volatile long: we don't want the
4354 // code generated by GenerateWideAtomicLoad to overwrite the
4355 // object's location. Likewise, in the case of an object field get
4356 // with read barriers enabled, we do not want the load to overwrite
4357 // the object's location, as we need it to emit the read barrier.
4358 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4359 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004360
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004361 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4362 locations->SetOut(Location::RequiresFpuRegister());
4363 } else {
4364 locations->SetOut(Location::RequiresRegister(),
4365 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4366 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004367 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004368 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004369 // - registers need to be consecutive
4370 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004371 // We don't test for ARM yet, and the assertion makes sure that we
4372 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004373 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4374 locations->AddTemp(Location::RequiresRegister());
4375 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004376 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4377 // We need a temporary register for the read barrier marking slow
4378 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4379 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004380 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004381}
4382
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004383Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4384 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4385 << input->GetType();
4386 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4387 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4388 return Location::ConstantLocation(input->AsConstant());
4389 } else {
4390 return Location::RequiresFpuRegister();
4391 }
4392}
4393
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004394Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4395 Opcode opcode) {
4396 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4397 if (constant->IsConstant() &&
4398 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4399 return Location::ConstantLocation(constant->AsConstant());
4400 }
4401 return Location::RequiresRegister();
4402}
4403
4404bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4405 Opcode opcode) {
4406 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4407 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004408 Opcode high_opcode = opcode;
4409 SetCc low_set_cc = kCcDontCare;
4410 switch (opcode) {
4411 case SUB:
4412 // Flip the operation to an ADD.
4413 value = -value;
4414 opcode = ADD;
4415 FALLTHROUGH_INTENDED;
4416 case ADD:
4417 if (Low32Bits(value) == 0u) {
4418 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4419 }
4420 high_opcode = ADC;
4421 low_set_cc = kCcSet;
4422 break;
4423 default:
4424 break;
4425 }
4426 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4427 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004428 } else {
4429 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4430 }
4431}
4432
Vladimir Marko59751a72016-08-05 14:37:27 +01004433bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4434 Opcode opcode,
4435 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004436 ShifterOperand so;
4437 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004438 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004439 return true;
4440 }
4441 Opcode neg_opcode = kNoOperand;
4442 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004443 case AND: neg_opcode = BIC; value = ~value; break;
4444 case ORR: neg_opcode = ORN; value = ~value; break;
4445 case ADD: neg_opcode = SUB; value = -value; break;
4446 case ADC: neg_opcode = SBC; value = ~value; break;
4447 case SUB: neg_opcode = ADD; value = -value; break;
4448 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004449 default:
4450 return false;
4451 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004452 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004453}
4454
Calin Juravle52c48962014-12-16 17:02:57 +00004455void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4456 const FieldInfo& field_info) {
4457 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004458
Calin Juravle52c48962014-12-16 17:02:57 +00004459 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004460 Location base_loc = locations->InAt(0);
4461 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004462 Location out = locations->Out();
4463 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004464 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004465 Primitive::Type field_type = field_info.GetFieldType();
4466 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4467
4468 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004469 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004470 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004471 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004472
Roland Levillainc9285912015-12-18 10:38:42 +00004473 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004474 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004475 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004476
Roland Levillainc9285912015-12-18 10:38:42 +00004477 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004478 __ LoadFromOffset(kLoadSignedHalfword, 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::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004482 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004483 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484
4485 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004486 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004487 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004488
4489 case Primitive::kPrimNot: {
4490 // /* HeapReference<Object> */ out = *(base + offset)
4491 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4492 Location temp_loc = locations->GetTemp(0);
4493 // Note that a potential implicit null check is handled in this
4494 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4495 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4496 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4497 if (is_volatile) {
4498 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4499 }
4500 } else {
4501 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4502 codegen_->MaybeRecordImplicitNullCheck(instruction);
4503 if (is_volatile) {
4504 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4505 }
4506 // If read barriers are enabled, emit read barriers other than
4507 // Baker's using a slow path (and also unpoison the loaded
4508 // reference, if heap poisoning is enabled).
4509 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4510 }
4511 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004512 }
4513
Roland Levillainc9285912015-12-18 10:38:42 +00004514 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004515 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004516 GenerateWideAtomicLoad(base, offset,
4517 out.AsRegisterPairLow<Register>(),
4518 out.AsRegisterPairHigh<Register>());
4519 } else {
4520 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4521 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004522 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004523
Roland Levillainc9285912015-12-18 10:38:42 +00004524 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004525 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004526 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004527
4528 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004529 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004530 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004531 Register lo = locations->GetTemp(0).AsRegister<Register>();
4532 Register hi = locations->GetTemp(1).AsRegister<Register>();
4533 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004534 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004535 __ vmovdrr(out_reg, lo, hi);
4536 } else {
4537 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004538 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004539 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004540 break;
4541 }
4542
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004543 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004544 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004545 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004546 }
Calin Juravle52c48962014-12-16 17:02:57 +00004547
Roland Levillainc9285912015-12-18 10:38:42 +00004548 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4549 // Potential implicit null checks, in the case of reference or
4550 // double fields, are handled in the previous switch statement.
4551 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004552 codegen_->MaybeRecordImplicitNullCheck(instruction);
4553 }
4554
Calin Juravle52c48962014-12-16 17:02:57 +00004555 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004556 if (field_type == Primitive::kPrimNot) {
4557 // Memory barriers, in the case of references, are also handled
4558 // in the previous switch statement.
4559 } else {
4560 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4561 }
Roland Levillain4d027112015-07-01 15:41:14 +01004562 }
Calin Juravle52c48962014-12-16 17:02:57 +00004563}
4564
4565void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4566 HandleFieldSet(instruction, instruction->GetFieldInfo());
4567}
4568
4569void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004570 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004571}
4572
4573void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4574 HandleFieldGet(instruction, instruction->GetFieldInfo());
4575}
4576
4577void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4578 HandleFieldGet(instruction, instruction->GetFieldInfo());
4579}
4580
4581void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4582 HandleFieldGet(instruction, instruction->GetFieldInfo());
4583}
4584
4585void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4586 HandleFieldGet(instruction, instruction->GetFieldInfo());
4587}
4588
4589void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4590 HandleFieldSet(instruction, instruction->GetFieldInfo());
4591}
4592
4593void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004594 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004595}
4596
Calin Juravlee460d1d2015-09-29 04:52:17 +01004597void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4598 HUnresolvedInstanceFieldGet* instruction) {
4599 FieldAccessCallingConventionARM calling_convention;
4600 codegen_->CreateUnresolvedFieldLocationSummary(
4601 instruction, instruction->GetFieldType(), calling_convention);
4602}
4603
4604void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4605 HUnresolvedInstanceFieldGet* instruction) {
4606 FieldAccessCallingConventionARM calling_convention;
4607 codegen_->GenerateUnresolvedFieldAccess(instruction,
4608 instruction->GetFieldType(),
4609 instruction->GetFieldIndex(),
4610 instruction->GetDexPc(),
4611 calling_convention);
4612}
4613
4614void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4615 HUnresolvedInstanceFieldSet* instruction) {
4616 FieldAccessCallingConventionARM calling_convention;
4617 codegen_->CreateUnresolvedFieldLocationSummary(
4618 instruction, instruction->GetFieldType(), calling_convention);
4619}
4620
4621void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4622 HUnresolvedInstanceFieldSet* instruction) {
4623 FieldAccessCallingConventionARM calling_convention;
4624 codegen_->GenerateUnresolvedFieldAccess(instruction,
4625 instruction->GetFieldType(),
4626 instruction->GetFieldIndex(),
4627 instruction->GetDexPc(),
4628 calling_convention);
4629}
4630
4631void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4632 HUnresolvedStaticFieldGet* instruction) {
4633 FieldAccessCallingConventionARM calling_convention;
4634 codegen_->CreateUnresolvedFieldLocationSummary(
4635 instruction, instruction->GetFieldType(), calling_convention);
4636}
4637
4638void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4639 HUnresolvedStaticFieldGet* instruction) {
4640 FieldAccessCallingConventionARM calling_convention;
4641 codegen_->GenerateUnresolvedFieldAccess(instruction,
4642 instruction->GetFieldType(),
4643 instruction->GetFieldIndex(),
4644 instruction->GetDexPc(),
4645 calling_convention);
4646}
4647
4648void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4649 HUnresolvedStaticFieldSet* instruction) {
4650 FieldAccessCallingConventionARM calling_convention;
4651 codegen_->CreateUnresolvedFieldLocationSummary(
4652 instruction, instruction->GetFieldType(), calling_convention);
4653}
4654
4655void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4656 HUnresolvedStaticFieldSet* instruction) {
4657 FieldAccessCallingConventionARM calling_convention;
4658 codegen_->GenerateUnresolvedFieldAccess(instruction,
4659 instruction->GetFieldType(),
4660 instruction->GetFieldIndex(),
4661 instruction->GetDexPc(),
4662 calling_convention);
4663}
4664
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004665void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004666 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4667 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004668}
4669
Calin Juravle2ae48182016-03-16 14:05:09 +00004670void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4671 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004672 return;
4673 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004674 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004675
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004676 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004677 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004678}
4679
Calin Juravle2ae48182016-03-16 14:05:09 +00004680void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004681 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004682 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004683
4684 LocationSummary* locations = instruction->GetLocations();
4685 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004686
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004687 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004688}
4689
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004690void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004691 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004692}
4693
Artem Serov6c916792016-07-11 14:02:34 +01004694static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4695 switch (type) {
4696 case Primitive::kPrimNot:
4697 return kLoadWord;
4698 case Primitive::kPrimBoolean:
4699 return kLoadUnsignedByte;
4700 case Primitive::kPrimByte:
4701 return kLoadSignedByte;
4702 case Primitive::kPrimChar:
4703 return kLoadUnsignedHalfword;
4704 case Primitive::kPrimShort:
4705 return kLoadSignedHalfword;
4706 case Primitive::kPrimInt:
4707 return kLoadWord;
4708 case Primitive::kPrimLong:
4709 return kLoadWordPair;
4710 case Primitive::kPrimFloat:
4711 return kLoadSWord;
4712 case Primitive::kPrimDouble:
4713 return kLoadDWord;
4714 default:
4715 LOG(FATAL) << "Unreachable type " << type;
4716 UNREACHABLE();
4717 }
4718}
4719
4720static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4721 switch (type) {
4722 case Primitive::kPrimNot:
4723 return kStoreWord;
4724 case Primitive::kPrimBoolean:
4725 case Primitive::kPrimByte:
4726 return kStoreByte;
4727 case Primitive::kPrimChar:
4728 case Primitive::kPrimShort:
4729 return kStoreHalfword;
4730 case Primitive::kPrimInt:
4731 return kStoreWord;
4732 case Primitive::kPrimLong:
4733 return kStoreWordPair;
4734 case Primitive::kPrimFloat:
4735 return kStoreSWord;
4736 case Primitive::kPrimDouble:
4737 return kStoreDWord;
4738 default:
4739 LOG(FATAL) << "Unreachable type " << type;
4740 UNREACHABLE();
4741 }
4742}
4743
4744void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4745 Location out_loc,
4746 Register base,
4747 Register reg_offset,
4748 Condition cond) {
4749 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4750 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4751
4752 switch (type) {
4753 case Primitive::kPrimByte:
4754 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4755 break;
4756 case Primitive::kPrimBoolean:
4757 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4758 break;
4759 case Primitive::kPrimShort:
4760 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4761 break;
4762 case Primitive::kPrimChar:
4763 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4764 break;
4765 case Primitive::kPrimNot:
4766 case Primitive::kPrimInt:
4767 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4768 break;
4769 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4770 case Primitive::kPrimLong:
4771 case Primitive::kPrimFloat:
4772 case Primitive::kPrimDouble:
4773 default:
4774 LOG(FATAL) << "Unreachable type " << type;
4775 UNREACHABLE();
4776 }
4777}
4778
4779void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4780 Location loc,
4781 Register base,
4782 Register reg_offset,
4783 Condition cond) {
4784 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4785 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4786
4787 switch (type) {
4788 case Primitive::kPrimByte:
4789 case Primitive::kPrimBoolean:
4790 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4791 break;
4792 case Primitive::kPrimShort:
4793 case Primitive::kPrimChar:
4794 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4795 break;
4796 case Primitive::kPrimNot:
4797 case Primitive::kPrimInt:
4798 __ str(loc.AsRegister<Register>(), mem_address, cond);
4799 break;
4800 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4801 case Primitive::kPrimLong:
4802 case Primitive::kPrimFloat:
4803 case Primitive::kPrimDouble:
4804 default:
4805 LOG(FATAL) << "Unreachable type " << type;
4806 UNREACHABLE();
4807 }
4808}
4809
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004810void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004811 bool object_array_get_with_read_barrier =
4812 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004813 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004814 new (GetGraph()->GetArena()) LocationSummary(instruction,
4815 object_array_get_with_read_barrier ?
4816 LocationSummary::kCallOnSlowPath :
4817 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004818 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004819 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004820 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004821 locations->SetInAt(0, Location::RequiresRegister());
4822 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004823 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4824 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4825 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004826 // The output overlaps in the case of an object array get with
4827 // read barriers enabled: we do not want the move to overwrite the
4828 // array's location, as we need it to emit the read barrier.
4829 locations->SetOut(
4830 Location::RequiresRegister(),
4831 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004832 }
Roland Levillainc9285912015-12-18 10:38:42 +00004833 // We need a temporary register for the read barrier marking slow
4834 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07004835 // Also need for String compression feature.
4836 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
4837 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00004838 locations->AddTemp(Location::RequiresRegister());
4839 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004840}
4841
4842void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4843 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004844 Location obj_loc = locations->InAt(0);
4845 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004846 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004847 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004848 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004849 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07004850 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
4851 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01004852 HInstruction* array_instr = instruction->GetArray();
4853 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01004854
Roland Levillain4d027112015-07-01 15:41:14 +01004855 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004856 case Primitive::kPrimBoolean:
4857 case Primitive::kPrimByte:
4858 case Primitive::kPrimShort:
4859 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004860 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004861 Register length;
4862 if (maybe_compressed_char_at) {
4863 length = locations->GetTemp(0).AsRegister<Register>();
4864 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
4865 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
4866 codegen_->MaybeRecordImplicitNullCheck(instruction);
4867 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004868 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004869 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07004870 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07004871 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004872 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4873 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4874 "Expecting 0=compressed, 1=uncompressed");
4875 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004876 __ LoadFromOffset(kLoadUnsignedByte,
4877 out_loc.AsRegister<Register>(),
4878 obj,
4879 data_offset + const_index);
4880 __ b(&done);
4881 __ Bind(&uncompressed_load);
4882 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
4883 out_loc.AsRegister<Register>(),
4884 obj,
4885 data_offset + (const_index << 1));
4886 __ Bind(&done);
4887 } else {
4888 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01004889
jessicahandojo05765752016-09-09 19:01:32 -07004890 LoadOperandType load_type = GetLoadOperandType(type);
4891 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
4892 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004893 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004894 Register temp = IP;
4895
4896 if (has_intermediate_address) {
4897 // We do not need to compute the intermediate address from the array: the
4898 // input instruction has done it already. See the comment in
4899 // `TryExtractArrayAccessAddress()`.
4900 if (kIsDebugBuild) {
4901 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4902 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4903 }
4904 temp = obj;
4905 } else {
4906 __ add(temp, obj, ShifterOperand(data_offset));
4907 }
jessicahandojo05765752016-09-09 19:01:32 -07004908 if (maybe_compressed_char_at) {
4909 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01004910 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
4911 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
4912 "Expecting 0=compressed, 1=uncompressed");
4913 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07004914 __ ldrb(out_loc.AsRegister<Register>(),
4915 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
4916 __ b(&done);
4917 __ Bind(&uncompressed_load);
4918 __ ldrh(out_loc.AsRegister<Register>(),
4919 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
4920 __ Bind(&done);
4921 } else {
4922 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
4923 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004924 }
4925 break;
4926 }
4927
Roland Levillainc9285912015-12-18 10:38:42 +00004928 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00004929 // The read barrier instrumentation of object ArrayGet
4930 // instructions does not support the HIntermediateAddress
4931 // instruction.
4932 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
4933
Roland Levillainc9285912015-12-18 10:38:42 +00004934 static_assert(
4935 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4936 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004937 // /* HeapReference<Object> */ out =
4938 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4939 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4940 Location temp = locations->GetTemp(0);
4941 // Note that a potential implicit null check is handled in this
4942 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4943 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4944 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4945 } else {
4946 Register out = out_loc.AsRegister<Register>();
4947 if (index.IsConstant()) {
4948 size_t offset =
4949 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4950 __ LoadFromOffset(kLoadWord, out, obj, offset);
4951 codegen_->MaybeRecordImplicitNullCheck(instruction);
4952 // If read barriers are enabled, emit read barriers other than
4953 // Baker's using a slow path (and also unpoison the loaded
4954 // reference, if heap poisoning is enabled).
4955 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4956 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004957 Register temp = IP;
4958
4959 if (has_intermediate_address) {
4960 // We do not need to compute the intermediate address from the array: the
4961 // input instruction has done it already. See the comment in
4962 // `TryExtractArrayAccessAddress()`.
4963 if (kIsDebugBuild) {
4964 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4965 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4966 }
4967 temp = obj;
4968 } else {
4969 __ add(temp, obj, ShifterOperand(data_offset));
4970 }
4971 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004972
Roland Levillainc9285912015-12-18 10:38:42 +00004973 codegen_->MaybeRecordImplicitNullCheck(instruction);
4974 // If read barriers are enabled, emit read barriers other than
4975 // Baker's using a slow path (and also unpoison the loaded
4976 // reference, if heap poisoning is enabled).
4977 codegen_->MaybeGenerateReadBarrierSlow(
4978 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4979 }
4980 }
4981 break;
4982 }
4983
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004984 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004985 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004986 size_t offset =
4987 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004988 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004989 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004990 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004991 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004992 }
4993 break;
4994 }
4995
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004996 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004997 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004998 if (index.IsConstant()) {
4999 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005000 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005001 } else {
5002 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005003 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005004 }
5005 break;
5006 }
5007
5008 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005009 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005010 if (index.IsConstant()) {
5011 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005012 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005013 } else {
5014 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005015 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005016 }
5017 break;
5018 }
5019
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005020 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005021 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005022 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005023 }
Roland Levillain4d027112015-07-01 15:41:14 +01005024
5025 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005026 // Potential implicit null checks, in the case of reference
5027 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005028 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005029 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005030 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005031}
5032
5033void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005034 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005035
5036 bool needs_write_barrier =
5037 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005038 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005039
Nicolas Geoffray39468442014-09-02 15:17:15 +01005040 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005041 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005042 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005043 LocationSummary::kCallOnSlowPath :
5044 LocationSummary::kNoCall);
5045
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005046 locations->SetInAt(0, Location::RequiresRegister());
5047 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5048 if (Primitive::IsFloatingPointType(value_type)) {
5049 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005050 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005051 locations->SetInAt(2, Location::RequiresRegister());
5052 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005053 if (needs_write_barrier) {
5054 // Temporary registers for the write barrier.
5055 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005056 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005058}
5059
5060void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5061 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005062 Location array_loc = locations->InAt(0);
5063 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005065 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005066 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005067 bool needs_write_barrier =
5068 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005069 uint32_t data_offset =
5070 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5071 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005072 HInstruction* array_instr = instruction->GetArray();
5073 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074
5075 switch (value_type) {
5076 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005077 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005078 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005079 case Primitive::kPrimChar:
5080 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005082 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5083 uint32_t full_offset =
5084 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5085 StoreOperandType store_type = GetStoreOperandType(value_type);
5086 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005087 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005088 Register temp = IP;
5089
5090 if (has_intermediate_address) {
5091 // We do not need to compute the intermediate address from the array: the
5092 // input instruction has done it already. See the comment in
5093 // `TryExtractArrayAccessAddress()`.
5094 if (kIsDebugBuild) {
5095 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5096 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5097 }
5098 temp = array;
5099 } else {
5100 __ add(temp, array, ShifterOperand(data_offset));
5101 }
Artem Serov6c916792016-07-11 14:02:34 +01005102 codegen_->StoreToShiftedRegOffset(value_type,
5103 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005104 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005105 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005106 }
5107 break;
5108 }
5109
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005111 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005112 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5113 // See the comment in instruction_simplifier_shared.cc.
5114 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005115
5116 if (instruction->InputAt(2)->IsNullConstant()) {
5117 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005118 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005119 size_t offset =
5120 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005121 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005122 } else {
5123 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005124 __ add(IP, array, ShifterOperand(data_offset));
5125 codegen_->StoreToShiftedRegOffset(value_type,
5126 value_loc,
5127 IP,
5128 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005129 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005130 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005131 DCHECK(!needs_write_barrier);
5132 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005133 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005134 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005135
5136 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005137 Location temp1_loc = locations->GetTemp(0);
5138 Register temp1 = temp1_loc.AsRegister<Register>();
5139 Location temp2_loc = locations->GetTemp(1);
5140 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005141 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5142 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5143 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5144 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005145 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005146
Roland Levillain3b359c72015-11-17 19:35:12 +00005147 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005148 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5149 codegen_->AddSlowPath(slow_path);
5150 if (instruction->GetValueCanBeNull()) {
5151 Label non_zero;
5152 __ CompareAndBranchIfNonZero(value, &non_zero);
5153 if (index.IsConstant()) {
5154 size_t offset =
5155 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5156 __ StoreToOffset(kStoreWord, value, array, offset);
5157 } else {
5158 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005159 __ add(IP, array, ShifterOperand(data_offset));
5160 codegen_->StoreToShiftedRegOffset(value_type,
5161 value_loc,
5162 IP,
5163 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005164 }
5165 codegen_->MaybeRecordImplicitNullCheck(instruction);
5166 __ b(&done);
5167 __ Bind(&non_zero);
5168 }
5169
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005170 // Note that when read barriers are enabled, the type checks
5171 // are performed without read barriers. This is fine, even in
5172 // the case where a class object is in the from-space after
5173 // the flip, as a comparison involving such a type would not
5174 // produce a false positive; it may of course produce a false
5175 // negative, in which case we would take the ArraySet slow
5176 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005177
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005178 // /* HeapReference<Class> */ temp1 = array->klass_
5179 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5180 codegen_->MaybeRecordImplicitNullCheck(instruction);
5181 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005182
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005183 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5184 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5185 // /* HeapReference<Class> */ temp2 = value->klass_
5186 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5187 // If heap poisoning is enabled, no need to unpoison `temp1`
5188 // nor `temp2`, as we are comparing two poisoned references.
5189 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005190
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005191 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5192 Label do_put;
5193 __ b(&do_put, EQ);
5194 // If heap poisoning is enabled, the `temp1` reference has
5195 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005196 __ MaybeUnpoisonHeapReference(temp1);
5197
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005198 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5199 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5200 // If heap poisoning is enabled, no need to unpoison
5201 // `temp1`, as we are comparing against null below.
5202 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5203 __ Bind(&do_put);
5204 } else {
5205 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005206 }
5207 }
5208
Artem Serov6c916792016-07-11 14:02:34 +01005209 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005210 if (kPoisonHeapReferences) {
5211 // Note that in the case where `value` is a null reference,
5212 // we do not enter this block, as a null reference does not
5213 // need poisoning.
5214 DCHECK_EQ(value_type, Primitive::kPrimNot);
5215 __ Mov(temp1, value);
5216 __ PoisonHeapReference(temp1);
5217 source = temp1;
5218 }
5219
5220 if (index.IsConstant()) {
5221 size_t offset =
5222 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5223 __ StoreToOffset(kStoreWord, source, array, offset);
5224 } else {
5225 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005226
5227 __ add(IP, array, ShifterOperand(data_offset));
5228 codegen_->StoreToShiftedRegOffset(value_type,
5229 Location::RegisterLocation(source),
5230 IP,
5231 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005232 }
5233
Roland Levillain3b359c72015-11-17 19:35:12 +00005234 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005235 codegen_->MaybeRecordImplicitNullCheck(instruction);
5236 }
5237
5238 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5239
5240 if (done.IsLinked()) {
5241 __ Bind(&done);
5242 }
5243
5244 if (slow_path != nullptr) {
5245 __ Bind(slow_path->GetExitLabel());
5246 }
5247
5248 break;
5249 }
5250
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005251 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005252 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005253 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005254 size_t offset =
5255 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005256 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005257 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005258 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005259 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 }
5261 break;
5262 }
5263
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005264 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005265 Location value = locations->InAt(2);
5266 DCHECK(value.IsFpuRegister());
5267 if (index.IsConstant()) {
5268 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005269 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005270 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005271 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005272 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5273 }
5274 break;
5275 }
5276
5277 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005278 Location value = locations->InAt(2);
5279 DCHECK(value.IsFpuRegisterPair());
5280 if (index.IsConstant()) {
5281 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005282 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005283 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005284 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005285 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5286 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005287
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005288 break;
5289 }
5290
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005291 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005292 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005293 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005294 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005295
Roland Levillain80e67092016-01-08 16:04:55 +00005296 // Objects are handled in the switch.
5297 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005298 codegen_->MaybeRecordImplicitNullCheck(instruction);
5299 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300}
5301
5302void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005303 LocationSummary* locations =
5304 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005305 locations->SetInAt(0, Location::RequiresRegister());
5306 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005307}
5308
5309void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5310 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005311 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005312 Register obj = locations->InAt(0).AsRegister<Register>();
5313 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005314 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005315 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005316 // Mask out compression flag from String's array length.
5317 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005318 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07005319 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320}
5321
Artem Serov328429f2016-07-06 16:23:04 +01005322void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005323 LocationSummary* locations =
5324 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5325
5326 locations->SetInAt(0, Location::RequiresRegister());
5327 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5329}
5330
5331void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5332 LocationSummary* locations = instruction->GetLocations();
5333 Location out = locations->Out();
5334 Location first = locations->InAt(0);
5335 Location second = locations->InAt(1);
5336
Artem Serov328429f2016-07-06 16:23:04 +01005337 if (second.IsRegister()) {
5338 __ add(out.AsRegister<Register>(),
5339 first.AsRegister<Register>(),
5340 ShifterOperand(second.AsRegister<Register>()));
5341 } else {
5342 __ AddConstant(out.AsRegister<Register>(),
5343 first.AsRegister<Register>(),
5344 second.GetConstant()->AsIntConstant()->GetValue());
5345 }
5346}
5347
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005348void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005349 RegisterSet caller_saves = RegisterSet::Empty();
5350 InvokeRuntimeCallingConvention calling_convention;
5351 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5352 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5353 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005354 locations->SetInAt(0, Location::RequiresRegister());
5355 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005356}
5357
5358void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5359 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005360 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005361 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005362 codegen_->AddSlowPath(slow_path);
5363
Roland Levillain271ab9c2014-11-27 15:23:57 +00005364 Register index = locations->InAt(0).AsRegister<Register>();
5365 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005366
5367 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005368 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005369}
5370
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005371void CodeGeneratorARM::MarkGCCard(Register temp,
5372 Register card,
5373 Register object,
5374 Register value,
5375 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005376 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005377 if (can_be_null) {
5378 __ CompareAndBranchIfZero(value, &is_null);
5379 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005380 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005381 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5382 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005383 if (can_be_null) {
5384 __ Bind(&is_null);
5385 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386}
5387
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005388void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005389 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005390}
5391
5392void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005393 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5394}
5395
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005396void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005397 LocationSummary* locations =
5398 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005399 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005400}
5401
5402void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005403 HBasicBlock* block = instruction->GetBlock();
5404 if (block->GetLoopInformation() != nullptr) {
5405 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5406 // The back edge will generate the suspend check.
5407 return;
5408 }
5409 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5410 // The goto will generate the suspend check.
5411 return;
5412 }
5413 GenerateSuspendCheck(instruction, nullptr);
5414}
5415
5416void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5417 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005418 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005419 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5420 if (slow_path == nullptr) {
5421 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5422 instruction->SetSlowPath(slow_path);
5423 codegen_->AddSlowPath(slow_path);
5424 if (successor != nullptr) {
5425 DCHECK(successor->IsLoopHeader());
5426 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5427 }
5428 } else {
5429 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5430 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005431
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005432 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005433 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005434 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005435 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005436 __ Bind(slow_path->GetReturnLabel());
5437 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005438 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005439 __ b(slow_path->GetEntryLabel());
5440 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005441}
5442
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005443ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5444 return codegen_->GetAssembler();
5445}
5446
5447void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005448 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005449 Location source = move->GetSource();
5450 Location destination = move->GetDestination();
5451
5452 if (source.IsRegister()) {
5453 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005454 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005455 } else if (destination.IsFpuRegister()) {
5456 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005457 } else {
5458 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005459 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005460 SP, destination.GetStackIndex());
5461 }
5462 } else if (source.IsStackSlot()) {
5463 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005464 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005465 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005466 } else if (destination.IsFpuRegister()) {
5467 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005468 } else {
5469 DCHECK(destination.IsStackSlot());
5470 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5471 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5472 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005473 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005474 if (destination.IsRegister()) {
5475 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5476 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005477 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005478 } else {
5479 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005480 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5481 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005482 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005483 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005484 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5485 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005486 } else if (destination.IsRegisterPair()) {
5487 DCHECK(ExpectedPairLayout(destination));
5488 __ LoadFromOffset(
5489 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5490 } else {
5491 DCHECK(destination.IsFpuRegisterPair()) << destination;
5492 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5493 SP,
5494 source.GetStackIndex());
5495 }
5496 } else if (source.IsRegisterPair()) {
5497 if (destination.IsRegisterPair()) {
5498 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5499 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005500 } else if (destination.IsFpuRegisterPair()) {
5501 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5502 source.AsRegisterPairLow<Register>(),
5503 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005504 } else {
5505 DCHECK(destination.IsDoubleStackSlot()) << destination;
5506 DCHECK(ExpectedPairLayout(source));
5507 __ StoreToOffset(
5508 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5509 }
5510 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005511 if (destination.IsRegisterPair()) {
5512 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5513 destination.AsRegisterPairHigh<Register>(),
5514 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5515 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005516 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5517 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5518 } else {
5519 DCHECK(destination.IsDoubleStackSlot()) << destination;
5520 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5521 SP,
5522 destination.GetStackIndex());
5523 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005524 } else {
5525 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005526 HConstant* constant = source.GetConstant();
5527 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5528 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005529 if (destination.IsRegister()) {
5530 __ LoadImmediate(destination.AsRegister<Register>(), value);
5531 } else {
5532 DCHECK(destination.IsStackSlot());
5533 __ LoadImmediate(IP, value);
5534 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5535 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005536 } else if (constant->IsLongConstant()) {
5537 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005538 if (destination.IsRegisterPair()) {
5539 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5540 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005541 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005542 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005543 __ LoadImmediate(IP, Low32Bits(value));
5544 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5545 __ LoadImmediate(IP, High32Bits(value));
5546 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5547 }
5548 } else if (constant->IsDoubleConstant()) {
5549 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005550 if (destination.IsFpuRegisterPair()) {
5551 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005552 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005553 DCHECK(destination.IsDoubleStackSlot()) << destination;
5554 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005555 __ LoadImmediate(IP, Low32Bits(int_value));
5556 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5557 __ LoadImmediate(IP, High32Bits(int_value));
5558 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5559 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005560 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005561 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005562 float value = constant->AsFloatConstant()->GetValue();
5563 if (destination.IsFpuRegister()) {
5564 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5565 } else {
5566 DCHECK(destination.IsStackSlot());
5567 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5568 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5569 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005570 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005571 }
5572}
5573
5574void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5575 __ Mov(IP, reg);
5576 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5577 __ StoreToOffset(kStoreWord, IP, SP, mem);
5578}
5579
5580void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5581 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5582 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5583 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5584 SP, mem1 + stack_offset);
5585 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5586 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5587 SP, mem2 + stack_offset);
5588 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5589}
5590
5591void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005592 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005593 Location source = move->GetSource();
5594 Location destination = move->GetDestination();
5595
5596 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005597 DCHECK_NE(source.AsRegister<Register>(), IP);
5598 DCHECK_NE(destination.AsRegister<Register>(), IP);
5599 __ Mov(IP, source.AsRegister<Register>());
5600 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5601 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005602 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005603 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005604 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005605 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005606 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5607 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005608 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005609 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005610 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005611 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005612 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005613 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005614 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005615 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005616 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5617 destination.AsRegisterPairHigh<Register>(),
5618 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005619 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005620 Register low_reg = source.IsRegisterPair()
5621 ? source.AsRegisterPairLow<Register>()
5622 : destination.AsRegisterPairLow<Register>();
5623 int mem = source.IsRegisterPair()
5624 ? destination.GetStackIndex()
5625 : source.GetStackIndex();
5626 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005627 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005628 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005629 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005630 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005631 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5632 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005633 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005634 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005635 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005636 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5637 DRegister reg = source.IsFpuRegisterPair()
5638 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5639 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5640 int mem = source.IsFpuRegisterPair()
5641 ? destination.GetStackIndex()
5642 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005643 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005644 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005645 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005646 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5647 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5648 : destination.AsFpuRegister<SRegister>();
5649 int mem = source.IsFpuRegister()
5650 ? destination.GetStackIndex()
5651 : source.GetStackIndex();
5652
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005653 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005654 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005655 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005656 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005657 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5658 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005659 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005660 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005661 }
5662}
5663
5664void ParallelMoveResolverARM::SpillScratch(int reg) {
5665 __ Push(static_cast<Register>(reg));
5666}
5667
5668void ParallelMoveResolverARM::RestoreScratch(int reg) {
5669 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005670}
5671
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005672HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5673 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005674 switch (desired_class_load_kind) {
5675 case HLoadClass::LoadKind::kReferrersClass:
5676 break;
5677 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5678 DCHECK(!GetCompilerOptions().GetCompilePic());
5679 break;
5680 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5681 DCHECK(GetCompilerOptions().GetCompilePic());
5682 break;
5683 case HLoadClass::LoadKind::kBootImageAddress:
5684 break;
5685 case HLoadClass::LoadKind::kDexCacheAddress:
5686 DCHECK(Runtime::Current()->UseJitCompilation());
5687 break;
5688 case HLoadClass::LoadKind::kDexCachePcRelative:
5689 DCHECK(!Runtime::Current()->UseJitCompilation());
5690 // We disable pc-relative load when there is an irreducible loop, as the optimization
5691 // is incompatible with it.
5692 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5693 // with irreducible loops.
5694 if (GetGraph()->HasIrreducibleLoops()) {
5695 return HLoadClass::LoadKind::kDexCacheViaMethod;
5696 }
5697 break;
5698 case HLoadClass::LoadKind::kDexCacheViaMethod:
5699 break;
5700 }
5701 return desired_class_load_kind;
5702}
5703
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005704void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005705 if (cls->NeedsAccessCheck()) {
5706 InvokeRuntimeCallingConvention calling_convention;
5707 CodeGenerator::CreateLoadClassLocationSummary(
5708 cls,
5709 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5710 Location::RegisterLocation(R0),
5711 /* code_generator_supports_read_barrier */ true);
5712 return;
5713 }
5714
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005715 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5716 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005717 ? LocationSummary::kCallOnSlowPath
5718 : LocationSummary::kNoCall;
5719 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005720 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005721 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005722 }
5723
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005724 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5725 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5726 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5727 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5728 locations->SetInAt(0, Location::RequiresRegister());
5729 }
5730 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005731}
5732
5733void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005734 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005735 if (cls->NeedsAccessCheck()) {
5736 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005737 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005738 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005739 return;
5740 }
5741
Roland Levillain3b359c72015-11-17 19:35:12 +00005742 Location out_loc = locations->Out();
5743 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005744
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005745 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005746 bool generate_null_check = false;
5747 switch (cls->GetLoadKind()) {
5748 case HLoadClass::LoadKind::kReferrersClass: {
5749 DCHECK(!cls->CanCallRuntime());
5750 DCHECK(!cls->MustGenerateClinitCheck());
5751 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5752 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005753 GenerateGcRootFieldLoad(cls,
5754 out_loc,
5755 current_method,
5756 ArtMethod::DeclaringClassOffset().Int32Value(),
5757 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005758 break;
5759 }
5760 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005761 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005762 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5763 cls->GetTypeIndex()));
5764 break;
5765 }
5766 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005767 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005768 CodeGeneratorARM::PcRelativePatchInfo* labels =
5769 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5770 __ BindTrackedLabel(&labels->movw_label);
5771 __ movw(out, /* placeholder */ 0u);
5772 __ BindTrackedLabel(&labels->movt_label);
5773 __ movt(out, /* placeholder */ 0u);
5774 __ BindTrackedLabel(&labels->add_pc_label);
5775 __ add(out, out, ShifterOperand(PC));
5776 break;
5777 }
5778 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005779 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005780 DCHECK_NE(cls->GetAddress(), 0u);
5781 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5782 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5783 break;
5784 }
5785 case HLoadClass::LoadKind::kDexCacheAddress: {
5786 DCHECK_NE(cls->GetAddress(), 0u);
5787 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5788 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5789 // a 128B range. To try and reduce the number of literals if we load multiple types,
5790 // simply split the dex cache address to a 128B aligned base loaded from a literal
5791 // and the remaining offset embedded in the load.
5792 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5793 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5794 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5795 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5796 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5797 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5798 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005799 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005800 generate_null_check = !cls->IsInDexCache();
5801 break;
5802 }
5803 case HLoadClass::LoadKind::kDexCachePcRelative: {
5804 Register base_reg = locations->InAt(0).AsRegister<Register>();
5805 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5806 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5807 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005808 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005809 generate_null_check = !cls->IsInDexCache();
5810 break;
5811 }
5812 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5813 // /* GcRoot<mirror::Class>[] */ out =
5814 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5815 Register current_method = locations->InAt(0).AsRegister<Register>();
5816 __ LoadFromOffset(kLoadWord,
5817 out,
5818 current_method,
5819 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5820 // /* GcRoot<mirror::Class> */ out = out[type_index]
5821 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005822 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005823 generate_null_check = !cls->IsInDexCache();
5824 }
5825 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005826
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005827 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5828 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005829 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005830 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5831 codegen_->AddSlowPath(slow_path);
5832 if (generate_null_check) {
5833 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5834 }
5835 if (cls->MustGenerateClinitCheck()) {
5836 GenerateClassInitializationCheck(slow_path, out);
5837 } else {
5838 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005839 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005840 }
5841}
5842
5843void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5844 LocationSummary* locations =
5845 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5846 locations->SetInAt(0, Location::RequiresRegister());
5847 if (check->HasUses()) {
5848 locations->SetOut(Location::SameAsFirstInput());
5849 }
5850}
5851
5852void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005853 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005854 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005855 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005856 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005857 GenerateClassInitializationCheck(slow_path,
5858 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005859}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005860
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005861void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005862 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005863 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5864 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5865 __ b(slow_path->GetEntryLabel(), LT);
5866 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5867 // properly. Therefore, we do a memory fence.
5868 __ dmb(ISH);
5869 __ Bind(slow_path->GetExitLabel());
5870}
5871
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005872HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5873 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005874 switch (desired_string_load_kind) {
5875 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5876 DCHECK(!GetCompilerOptions().GetCompilePic());
5877 break;
5878 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5879 DCHECK(GetCompilerOptions().GetCompilePic());
5880 break;
5881 case HLoadString::LoadKind::kBootImageAddress:
5882 break;
5883 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005884 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005885 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00005886 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01005887 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005888 break;
5889 case HLoadString::LoadKind::kDexCacheViaMethod:
5890 break;
5891 }
5892 return desired_string_load_kind;
5893}
5894
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005895void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005896 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
Vladimir Markoaad75c62016-10-03 08:46:48 +00005897 ? ((load->GetLoadKind() == HLoadString::LoadKind::kDexCacheViaMethod)
5898 ? LocationSummary::kCallOnMainOnly
5899 : LocationSummary::kCallOnSlowPath)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005900 : LocationSummary::kNoCall;
5901 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005902
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005903 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005904 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005905 locations->SetOut(Location::RegisterLocation(R0));
5906 } else {
5907 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005908 if (load_kind == HLoadString::LoadKind::kBssEntry) {
5909 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5910 // Rely on the pResolveString and/or marking to save everything, including temps.
5911 // Note that IP may theoretically be clobbered by saving/restoring the live register
5912 // (only one thanks to the custom calling convention), so we request a different temp.
5913 locations->AddTemp(Location::RequiresRegister());
5914 RegisterSet caller_saves = RegisterSet::Empty();
5915 InvokeRuntimeCallingConvention calling_convention;
5916 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5917 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5918 // that the the kPrimNot result register is the same as the first argument register.
5919 locations->SetCustomSlowPathCallerSaves(caller_saves);
5920 } else {
5921 // For non-Baker read barrier we have a temp-clobbering call.
5922 }
5923 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005924 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005925}
5926
5927void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005928 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005929 Location out_loc = locations->Out();
5930 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005931 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005932
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005933 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005934 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005935 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5936 load->GetStringIndex()));
5937 return; // No dex cache slow path.
5938 }
5939 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00005940 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005941 CodeGeneratorARM::PcRelativePatchInfo* labels =
5942 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5943 __ BindTrackedLabel(&labels->movw_label);
5944 __ movw(out, /* placeholder */ 0u);
5945 __ BindTrackedLabel(&labels->movt_label);
5946 __ movt(out, /* placeholder */ 0u);
5947 __ BindTrackedLabel(&labels->add_pc_label);
5948 __ add(out, out, ShifterOperand(PC));
5949 return; // No dex cache slow path.
5950 }
5951 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005952 DCHECK_NE(load->GetAddress(), 0u);
5953 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5954 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5955 return; // No dex cache slow path.
5956 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00005957 case HLoadString::LoadKind::kBssEntry: {
5958 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005959 Register temp = locations->GetTemp(0).AsRegister<Register>();
Vladimir Markoaad75c62016-10-03 08:46:48 +00005960 CodeGeneratorARM::PcRelativePatchInfo* labels =
5961 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5962 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005963 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005964 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005965 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005966 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005967 __ add(temp, temp, ShifterOperand(PC));
Roland Levillain00468f32016-10-27 18:02:48 +01005968 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kEmitCompilerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +00005969 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
5970 codegen_->AddSlowPath(slow_path);
5971 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5972 __ Bind(slow_path->GetExitLabel());
5973 return;
5974 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005975 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005976 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005977 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005978
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005979 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
5980 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5981 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01005982 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005983 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5984 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5985 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005986}
5987
David Brazdilcb1c0552015-08-04 16:22:25 +01005988static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005989 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005990}
5991
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005992void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5993 LocationSummary* locations =
5994 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5995 locations->SetOut(Location::RequiresRegister());
5996}
5997
5998void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005999 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006000 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6001}
6002
6003void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6004 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6005}
6006
6007void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006008 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006009 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006010}
6011
6012void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6013 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006014 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006015 InvokeRuntimeCallingConvention calling_convention;
6016 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6017}
6018
6019void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006020 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006021 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006022}
6023
Roland Levillainc9285912015-12-18 10:38:42 +00006024static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6025 return kEmitCompilerReadBarrier &&
6026 (kUseBakerReadBarrier ||
6027 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6028 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6029 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6030}
6031
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006032void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006033 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006034 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006035 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006036 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006037 case TypeCheckKind::kExactCheck:
6038 case TypeCheckKind::kAbstractClassCheck:
6039 case TypeCheckKind::kClassHierarchyCheck:
6040 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006041 call_kind =
6042 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006043 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006044 break;
6045 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006046 case TypeCheckKind::kUnresolvedCheck:
6047 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006048 call_kind = LocationSummary::kCallOnSlowPath;
6049 break;
6050 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006051
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006052 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006053 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006054 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006055 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006056 locations->SetInAt(0, Location::RequiresRegister());
6057 locations->SetInAt(1, Location::RequiresRegister());
6058 // The "out" register is used as a temporary, so it overlaps with the inputs.
6059 // Note that TypeCheckSlowPathARM uses this register too.
6060 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
6061 // When read barriers are enabled, we need a temporary register for
6062 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00006063 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006064 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006065 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006066}
6067
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006068void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006069 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006070 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006071 Location obj_loc = locations->InAt(0);
6072 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006073 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006074 Location out_loc = locations->Out();
6075 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006076 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00006077 locations->GetTemp(0) :
6078 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006079 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006080 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6081 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6082 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006083 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006084 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006085
6086 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006087 // avoid null check if we know obj is not null.
6088 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006089 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006090 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006091
Roland Levillain3b359c72015-11-17 19:35:12 +00006092 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006093 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006094
Roland Levillainc9285912015-12-18 10:38:42 +00006095 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006096 case TypeCheckKind::kExactCheck: {
6097 __ cmp(out, ShifterOperand(cls));
6098 // Classes must be equal for the instanceof to succeed.
6099 __ b(&zero, NE);
6100 __ LoadImmediate(out, 1);
6101 __ b(&done);
6102 break;
6103 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006104
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006105 case TypeCheckKind::kAbstractClassCheck: {
6106 // If the class is abstract, we eagerly fetch the super class of the
6107 // object to avoid doing a comparison we know will fail.
6108 Label loop;
6109 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006110 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006111 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006112 // If `out` is null, we use it for the result, and jump to `done`.
6113 __ CompareAndBranchIfZero(out, &done);
6114 __ cmp(out, ShifterOperand(cls));
6115 __ b(&loop, NE);
6116 __ LoadImmediate(out, 1);
6117 if (zero.IsLinked()) {
6118 __ b(&done);
6119 }
6120 break;
6121 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006122
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006123 case TypeCheckKind::kClassHierarchyCheck: {
6124 // Walk over the class hierarchy to find a match.
6125 Label loop, success;
6126 __ Bind(&loop);
6127 __ cmp(out, ShifterOperand(cls));
6128 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006129 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006130 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006131 __ CompareAndBranchIfNonZero(out, &loop);
6132 // If `out` is null, we use it for the result, and jump to `done`.
6133 __ b(&done);
6134 __ Bind(&success);
6135 __ LoadImmediate(out, 1);
6136 if (zero.IsLinked()) {
6137 __ b(&done);
6138 }
6139 break;
6140 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006141
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006143 // Do an exact check.
6144 Label exact_check;
6145 __ cmp(out, ShifterOperand(cls));
6146 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006147 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006148 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006149 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006150 // If `out` is null, we use it for the result, and jump to `done`.
6151 __ CompareAndBranchIfZero(out, &done);
6152 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6153 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6154 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006155 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006156 __ LoadImmediate(out, 1);
6157 __ b(&done);
6158 break;
6159 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006160
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006161 case TypeCheckKind::kArrayCheck: {
6162 __ cmp(out, ShifterOperand(cls));
6163 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006164 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6165 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166 codegen_->AddSlowPath(slow_path);
6167 __ b(slow_path->GetEntryLabel(), NE);
6168 __ LoadImmediate(out, 1);
6169 if (zero.IsLinked()) {
6170 __ b(&done);
6171 }
6172 break;
6173 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006174
Calin Juravle98893e12015-10-02 21:05:03 +01006175 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006176 case TypeCheckKind::kInterfaceCheck: {
6177 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006178 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006179 // cases.
6180 //
6181 // We cannot directly call the InstanceofNonTrivial runtime
6182 // entry point without resorting to a type checking slow path
6183 // here (i.e. by calling InvokeRuntime directly), as it would
6184 // require to assign fixed registers for the inputs of this
6185 // HInstanceOf instruction (following the runtime calling
6186 // convention), which might be cluttered by the potential first
6187 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006188 //
6189 // TODO: Introduce a new runtime entry point taking the object
6190 // to test (instead of its class) as argument, and let it deal
6191 // with the read barrier issues. This will let us refactor this
6192 // case of the `switch` code as it was previously (with a direct
6193 // call to the runtime not using a type checking slow path).
6194 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006195 DCHECK(locations->OnlyCallsOnSlowPath());
6196 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6197 /* is_fatal */ false);
6198 codegen_->AddSlowPath(slow_path);
6199 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006200 if (zero.IsLinked()) {
6201 __ b(&done);
6202 }
6203 break;
6204 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006205 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006206
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006207 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006208 __ Bind(&zero);
6209 __ LoadImmediate(out, 0);
6210 }
6211
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006212 if (done.IsLinked()) {
6213 __ Bind(&done);
6214 }
6215
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006216 if (slow_path != nullptr) {
6217 __ Bind(slow_path->GetExitLabel());
6218 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006219}
6220
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006221void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006222 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6223 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6224
Roland Levillain3b359c72015-11-17 19:35:12 +00006225 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6226 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006227 case TypeCheckKind::kExactCheck:
6228 case TypeCheckKind::kAbstractClassCheck:
6229 case TypeCheckKind::kClassHierarchyCheck:
6230 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006231 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6232 LocationSummary::kCallOnSlowPath :
6233 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006234 break;
6235 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006236 case TypeCheckKind::kUnresolvedCheck:
6237 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006238 call_kind = LocationSummary::kCallOnSlowPath;
6239 break;
6240 }
6241
Roland Levillain3b359c72015-11-17 19:35:12 +00006242 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6243 locations->SetInAt(0, Location::RequiresRegister());
6244 locations->SetInAt(1, Location::RequiresRegister());
6245 // Note that TypeCheckSlowPathARM uses this "temp" register too.
6246 locations->AddTemp(Location::RequiresRegister());
6247 // When read barriers are enabled, we need an additional temporary
6248 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00006249 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006250 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006251 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006252}
6253
6254void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006255 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006256 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006257 Location obj_loc = locations->InAt(0);
6258 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006259 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006260 Location temp_loc = locations->GetTemp(0);
6261 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006262 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00006263 locations->GetTemp(1) :
6264 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006265 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006266 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6267 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6268 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006269
Roland Levillain3b359c72015-11-17 19:35:12 +00006270 bool is_type_check_slow_path_fatal =
6271 (type_check_kind == TypeCheckKind::kExactCheck ||
6272 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6273 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6274 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6275 !instruction->CanThrowIntoCatchBlock();
Artem Serovf4d6aee2016-07-11 10:41:45 +01006276 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006277 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6278 is_type_check_slow_path_fatal);
6279 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006280
6281 Label done;
6282 // Avoid null check if we know obj is not null.
6283 if (instruction->MustDoNullCheck()) {
6284 __ CompareAndBranchIfZero(obj, &done);
6285 }
6286
Roland Levillain3b359c72015-11-17 19:35:12 +00006287 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006288 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006289
Roland Levillain3b359c72015-11-17 19:35:12 +00006290 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006291 case TypeCheckKind::kExactCheck:
6292 case TypeCheckKind::kArrayCheck: {
6293 __ cmp(temp, ShifterOperand(cls));
6294 // Jump to slow path for throwing the exception or doing a
6295 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006296 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006297 break;
6298 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006299
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006300 case TypeCheckKind::kAbstractClassCheck: {
6301 // If the class is abstract, we eagerly fetch the super class of the
6302 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00006303 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006304 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006305 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006306 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006307
6308 // If the class reference currently in `temp` is not null, jump
6309 // to the `compare_classes` label to compare it with the checked
6310 // class.
6311 __ CompareAndBranchIfNonZero(temp, &compare_classes);
6312 // Otherwise, jump to the slow path to throw the exception.
6313 //
6314 // But before, move back the object's class into `temp` before
6315 // going into the slow path, as it has been overwritten in the
6316 // meantime.
6317 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006318 GenerateReferenceLoadTwoRegisters(
6319 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006320 __ b(type_check_slow_path->GetEntryLabel());
6321
6322 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006323 __ cmp(temp, ShifterOperand(cls));
6324 __ b(&loop, NE);
6325 break;
6326 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006327
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 case TypeCheckKind::kClassHierarchyCheck: {
6329 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006330 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006331 __ Bind(&loop);
6332 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006333 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006334
Roland Levillain3b359c72015-11-17 19:35:12 +00006335 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006336 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006337
6338 // If the class reference currently in `temp` is not null, jump
6339 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006340 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006341 // Otherwise, jump to the slow path to throw the exception.
6342 //
6343 // But before, move back the object's class into `temp` before
6344 // going into the slow path, as it has been overwritten in the
6345 // meantime.
6346 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006347 GenerateReferenceLoadTwoRegisters(
6348 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006349 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006350 break;
6351 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006352
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006353 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006354 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006355 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006356 __ cmp(temp, ShifterOperand(cls));
6357 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006358
6359 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006360 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006361 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006362
6363 // If the component type is not null (i.e. the object is indeed
6364 // an array), jump to label `check_non_primitive_component_type`
6365 // to further check that this component type is not a primitive
6366 // type.
6367 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
6368 // Otherwise, jump to the slow path to throw the exception.
6369 //
6370 // But before, move back the object's class into `temp` before
6371 // going into the slow path, as it has been overwritten in the
6372 // meantime.
6373 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006374 GenerateReferenceLoadTwoRegisters(
6375 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006376 __ b(type_check_slow_path->GetEntryLabel());
6377
6378 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006379 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006380 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
6381 __ CompareAndBranchIfZero(temp, &done);
6382 // Same comment as above regarding `temp` and the slow path.
6383 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006384 GenerateReferenceLoadTwoRegisters(
6385 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006386 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006387 break;
6388 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006389
Calin Juravle98893e12015-10-02 21:05:03 +01006390 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006391 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006392 // We always go into the type check slow path for the unresolved
6393 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00006394 //
6395 // We cannot directly call the CheckCast runtime entry point
6396 // without resorting to a type checking slow path here (i.e. by
6397 // calling InvokeRuntime directly), as it would require to
6398 // assign fixed registers for the inputs of this HInstanceOf
6399 // instruction (following the runtime calling convention), which
6400 // might be cluttered by the potential first read barrier
6401 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006402 //
6403 // TODO: Introduce a new runtime entry point taking the object
6404 // to test (instead of its class) as argument, and let it deal
6405 // with the read barrier issues. This will let us refactor this
6406 // case of the `switch` code as it was previously (with a direct
6407 // call to the runtime not using a type checking slow path).
6408 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006409 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006410 break;
6411 }
6412 __ Bind(&done);
6413
Roland Levillain3b359c72015-11-17 19:35:12 +00006414 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006415}
6416
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006417void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6418 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006419 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006420 InvokeRuntimeCallingConvention calling_convention;
6421 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6422}
6423
6424void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006425 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6426 instruction,
6427 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006428 if (instruction->IsEnter()) {
6429 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6430 } else {
6431 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6432 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006433}
6434
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006435void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6436void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6437void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006438
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006439void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006440 LocationSummary* locations =
6441 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6442 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6443 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006444 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006445 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006446 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006447 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006448}
6449
6450void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6451 HandleBitwiseOperation(instruction);
6452}
6453
6454void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6455 HandleBitwiseOperation(instruction);
6456}
6457
6458void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6459 HandleBitwiseOperation(instruction);
6460}
6461
Artem Serov7fc63502016-02-09 17:15:29 +00006462
6463void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6464 LocationSummary* locations =
6465 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6466 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6467 || instruction->GetResultType() == Primitive::kPrimLong);
6468
6469 locations->SetInAt(0, Location::RequiresRegister());
6470 locations->SetInAt(1, Location::RequiresRegister());
6471 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6472}
6473
6474void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6475 LocationSummary* locations = instruction->GetLocations();
6476 Location first = locations->InAt(0);
6477 Location second = locations->InAt(1);
6478 Location out = locations->Out();
6479
6480 if (instruction->GetResultType() == Primitive::kPrimInt) {
6481 Register first_reg = first.AsRegister<Register>();
6482 ShifterOperand second_reg(second.AsRegister<Register>());
6483 Register out_reg = out.AsRegister<Register>();
6484
6485 switch (instruction->GetOpKind()) {
6486 case HInstruction::kAnd:
6487 __ bic(out_reg, first_reg, second_reg);
6488 break;
6489 case HInstruction::kOr:
6490 __ orn(out_reg, first_reg, second_reg);
6491 break;
6492 // There is no EON on arm.
6493 case HInstruction::kXor:
6494 default:
6495 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6496 UNREACHABLE();
6497 }
6498 return;
6499
6500 } else {
6501 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6502 Register first_low = first.AsRegisterPairLow<Register>();
6503 Register first_high = first.AsRegisterPairHigh<Register>();
6504 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6505 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6506 Register out_low = out.AsRegisterPairLow<Register>();
6507 Register out_high = out.AsRegisterPairHigh<Register>();
6508
6509 switch (instruction->GetOpKind()) {
6510 case HInstruction::kAnd:
6511 __ bic(out_low, first_low, second_low);
6512 __ bic(out_high, first_high, second_high);
6513 break;
6514 case HInstruction::kOr:
6515 __ orn(out_low, first_low, second_low);
6516 __ orn(out_high, first_high, second_high);
6517 break;
6518 // There is no EON on arm.
6519 case HInstruction::kXor:
6520 default:
6521 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6522 UNREACHABLE();
6523 }
6524 }
6525}
6526
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006527void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6528 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6529 if (value == 0xffffffffu) {
6530 if (out != first) {
6531 __ mov(out, ShifterOperand(first));
6532 }
6533 return;
6534 }
6535 if (value == 0u) {
6536 __ mov(out, ShifterOperand(0));
6537 return;
6538 }
6539 ShifterOperand so;
6540 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6541 __ and_(out, first, so);
6542 } else {
6543 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6544 __ bic(out, first, ShifterOperand(~value));
6545 }
6546}
6547
6548void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6549 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6550 if (value == 0u) {
6551 if (out != first) {
6552 __ mov(out, ShifterOperand(first));
6553 }
6554 return;
6555 }
6556 if (value == 0xffffffffu) {
6557 __ mvn(out, ShifterOperand(0));
6558 return;
6559 }
6560 ShifterOperand so;
6561 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6562 __ orr(out, first, so);
6563 } else {
6564 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6565 __ orn(out, first, ShifterOperand(~value));
6566 }
6567}
6568
6569void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6570 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6571 if (value == 0u) {
6572 if (out != first) {
6573 __ mov(out, ShifterOperand(first));
6574 }
6575 return;
6576 }
6577 __ eor(out, first, ShifterOperand(value));
6578}
6579
Vladimir Marko59751a72016-08-05 14:37:27 +01006580void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6581 Location first,
6582 uint64_t value) {
6583 Register out_low = out.AsRegisterPairLow<Register>();
6584 Register out_high = out.AsRegisterPairHigh<Register>();
6585 Register first_low = first.AsRegisterPairLow<Register>();
6586 Register first_high = first.AsRegisterPairHigh<Register>();
6587 uint32_t value_low = Low32Bits(value);
6588 uint32_t value_high = High32Bits(value);
6589 if (value_low == 0u) {
6590 if (out_low != first_low) {
6591 __ mov(out_low, ShifterOperand(first_low));
6592 }
6593 __ AddConstant(out_high, first_high, value_high);
6594 return;
6595 }
6596 __ AddConstantSetFlags(out_low, first_low, value_low);
6597 ShifterOperand so;
6598 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6599 __ adc(out_high, first_high, so);
6600 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6601 __ sbc(out_high, first_high, so);
6602 } else {
6603 LOG(FATAL) << "Unexpected constant " << value_high;
6604 UNREACHABLE();
6605 }
6606}
6607
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006608void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6609 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006610 Location first = locations->InAt(0);
6611 Location second = locations->InAt(1);
6612 Location out = locations->Out();
6613
6614 if (second.IsConstant()) {
6615 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6616 uint32_t value_low = Low32Bits(value);
6617 if (instruction->GetResultType() == Primitive::kPrimInt) {
6618 Register first_reg = first.AsRegister<Register>();
6619 Register out_reg = out.AsRegister<Register>();
6620 if (instruction->IsAnd()) {
6621 GenerateAndConst(out_reg, first_reg, value_low);
6622 } else if (instruction->IsOr()) {
6623 GenerateOrrConst(out_reg, first_reg, value_low);
6624 } else {
6625 DCHECK(instruction->IsXor());
6626 GenerateEorConst(out_reg, first_reg, value_low);
6627 }
6628 } else {
6629 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6630 uint32_t value_high = High32Bits(value);
6631 Register first_low = first.AsRegisterPairLow<Register>();
6632 Register first_high = first.AsRegisterPairHigh<Register>();
6633 Register out_low = out.AsRegisterPairLow<Register>();
6634 Register out_high = out.AsRegisterPairHigh<Register>();
6635 if (instruction->IsAnd()) {
6636 GenerateAndConst(out_low, first_low, value_low);
6637 GenerateAndConst(out_high, first_high, value_high);
6638 } else if (instruction->IsOr()) {
6639 GenerateOrrConst(out_low, first_low, value_low);
6640 GenerateOrrConst(out_high, first_high, value_high);
6641 } else {
6642 DCHECK(instruction->IsXor());
6643 GenerateEorConst(out_low, first_low, value_low);
6644 GenerateEorConst(out_high, first_high, value_high);
6645 }
6646 }
6647 return;
6648 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006649
6650 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006651 Register first_reg = first.AsRegister<Register>();
6652 ShifterOperand second_reg(second.AsRegister<Register>());
6653 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006654 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006655 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006656 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006657 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006658 } else {
6659 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006660 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006661 }
6662 } else {
6663 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006664 Register first_low = first.AsRegisterPairLow<Register>();
6665 Register first_high = first.AsRegisterPairHigh<Register>();
6666 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6667 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6668 Register out_low = out.AsRegisterPairLow<Register>();
6669 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006670 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006671 __ and_(out_low, first_low, second_low);
6672 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006673 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006674 __ orr(out_low, first_low, second_low);
6675 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006676 } else {
6677 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006678 __ eor(out_low, first_low, second_low);
6679 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006680 }
6681 }
6682}
6683
Roland Levillainc9285912015-12-18 10:38:42 +00006684void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6685 Location out,
6686 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006687 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006688 Register out_reg = out.AsRegister<Register>();
6689 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006690 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006691 if (kUseBakerReadBarrier) {
6692 // Load with fast path based Baker's read barrier.
6693 // /* HeapReference<Object> */ out = *(out + offset)
6694 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006695 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006696 } else {
6697 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006698 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006699 // in the following move operation, as we will need it for the
6700 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006701 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006702 // /* HeapReference<Object> */ out = *(out + offset)
6703 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006704 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006705 }
6706 } else {
6707 // Plain load with no read barrier.
6708 // /* HeapReference<Object> */ out = *(out + offset)
6709 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6710 __ MaybeUnpoisonHeapReference(out_reg);
6711 }
6712}
6713
6714void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6715 Location out,
6716 Location obj,
6717 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006718 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006719 Register out_reg = out.AsRegister<Register>();
6720 Register obj_reg = obj.AsRegister<Register>();
6721 if (kEmitCompilerReadBarrier) {
6722 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006723 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006724 // Load with fast path based Baker's read barrier.
6725 // /* HeapReference<Object> */ out = *(obj + offset)
6726 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006727 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006728 } else {
6729 // Load with slow path based read barrier.
6730 // /* HeapReference<Object> */ out = *(obj + offset)
6731 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6732 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6733 }
6734 } else {
6735 // Plain load with no read barrier.
6736 // /* HeapReference<Object> */ out = *(obj + offset)
6737 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6738 __ MaybeUnpoisonHeapReference(out_reg);
6739 }
6740}
6741
6742void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6743 Location root,
6744 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006745 uint32_t offset,
6746 bool requires_read_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006747 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006748 if (requires_read_barrier) {
6749 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006750 if (kUseBakerReadBarrier) {
6751 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6752 // Baker's read barrier are used:
6753 //
6754 // root = obj.field;
6755 // if (Thread::Current()->GetIsGcMarking()) {
6756 // root = ReadBarrier::Mark(root)
6757 // }
6758
6759 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6760 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6761 static_assert(
6762 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6763 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6764 "have different sizes.");
6765 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6766 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6767 "have different sizes.");
6768
Vladimir Marko953437b2016-08-24 08:30:46 +00006769 // Slow path marking the GC root `root`.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006770 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006771 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006772 codegen_->AddSlowPath(slow_path);
6773
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006774 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006775 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006776 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006777 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6778 __ Bind(slow_path->GetExitLabel());
6779 } else {
6780 // GC root loaded through a slow path for read barriers other
6781 // than Baker's.
6782 // /* GcRoot<mirror::Object>* */ root = obj + offset
6783 __ AddConstant(root_reg, obj, offset);
6784 // /* mirror::Object* */ root = root->Read()
6785 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6786 }
6787 } else {
6788 // Plain GC root load with no read barrier.
6789 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6790 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6791 // Note that GC roots are not affected by heap poisoning, thus we
6792 // do not have to unpoison `root_reg` here.
6793 }
6794}
6795
6796void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6797 Location ref,
6798 Register obj,
6799 uint32_t offset,
6800 Location temp,
6801 bool needs_null_check) {
6802 DCHECK(kEmitCompilerReadBarrier);
6803 DCHECK(kUseBakerReadBarrier);
6804
6805 // /* HeapReference<Object> */ ref = *(obj + offset)
6806 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006807 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006808 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006809 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006810}
6811
6812void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6813 Location ref,
6814 Register obj,
6815 uint32_t data_offset,
6816 Location index,
6817 Location temp,
6818 bool needs_null_check) {
6819 DCHECK(kEmitCompilerReadBarrier);
6820 DCHECK(kUseBakerReadBarrier);
6821
Roland Levillainbfea3352016-06-23 13:48:47 +01006822 static_assert(
6823 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6824 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006825 // /* HeapReference<Object> */ ref =
6826 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006827 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006828 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006829 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006830}
6831
6832void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6833 Location ref,
6834 Register obj,
6835 uint32_t offset,
6836 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006837 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006838 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006839 bool needs_null_check,
6840 bool always_update_field,
6841 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00006842 DCHECK(kEmitCompilerReadBarrier);
6843 DCHECK(kUseBakerReadBarrier);
6844
6845 // In slow path based read barriers, the read barrier call is
6846 // inserted after the original load. However, in fast path based
6847 // Baker's read barriers, we need to perform the load of
6848 // mirror::Object::monitor_ *before* the original reference load.
6849 // This load-load ordering is required by the read barrier.
6850 // The fast path/slow path (for Baker's algorithm) should look like:
6851 //
6852 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6853 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6854 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006855 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillainc9285912015-12-18 10:38:42 +00006856 // if (is_gray) {
6857 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6858 // }
6859 //
6860 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006861 // slightly more complex as it performs additional checks that we do
6862 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006863
6864 Register ref_reg = ref.AsRegister<Register>();
6865 Register temp_reg = temp.AsRegister<Register>();
6866 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6867
6868 // /* int32_t */ monitor = obj->monitor_
6869 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6870 if (needs_null_check) {
6871 MaybeRecordImplicitNullCheck(instruction);
6872 }
6873 // /* LockWord */ lock_word = LockWord(monitor)
6874 static_assert(sizeof(LockWord) == sizeof(int32_t),
6875 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006876
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006877 // Introduce a dependency on the lock_word including the rb_state,
6878 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006879 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006880 // `obj` is unchanged by this operation, but its value now depends
6881 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006882 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006883
6884 // The actual reference load.
6885 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006886 // Load types involving an "index": ArrayGet,
6887 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6888 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01006889 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006890 if (index.IsConstant()) {
6891 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006892 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006893 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6894 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006895 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006896 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6897 // intrinsics, which use a register pair as index ("long
6898 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01006899 Register index_reg = index.IsRegisterPair()
6900 ? index.AsRegisterPairLow<Register>()
6901 : index.AsRegister<Register>();
6902 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006903 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6904 }
6905 } else {
6906 // /* HeapReference<Object> */ ref = *(obj + offset)
6907 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6908 }
6909
6910 // Object* ref = ref_addr->AsMirrorPtr()
6911 __ MaybeUnpoisonHeapReference(ref_reg);
6912
Vladimir Marko953437b2016-08-24 08:30:46 +00006913 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01006914 SlowPathCodeARM* slow_path;
6915 if (always_update_field) {
6916 DCHECK(temp2 != nullptr);
6917 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
6918 // of the form `obj + field_offset`, where `obj` is a register and
6919 // `field_offset` is a register pair (of which only the lower half
6920 // is used). Thus `offset` and `scale_factor` above are expected
6921 // to be null in this code path.
6922 DCHECK_EQ(offset, 0u);
6923 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6924 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
6925 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
6926 } else {
6927 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
6928 }
Roland Levillainc9285912015-12-18 10:38:42 +00006929 AddSlowPath(slow_path);
6930
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006931 // if (rb_state == ReadBarrier::GrayState())
Roland Levillainc9285912015-12-18 10:38:42 +00006932 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006933 // Given the numeric representation, it's enough to check the low bit of the
6934 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6935 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07006936 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6937 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006938 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6939 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006940 __ Bind(slow_path->GetExitLabel());
6941}
6942
6943void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6944 Location out,
6945 Location ref,
6946 Location obj,
6947 uint32_t offset,
6948 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006949 DCHECK(kEmitCompilerReadBarrier);
6950
Roland Levillainc9285912015-12-18 10:38:42 +00006951 // Insert a slow path based read barrier *after* the reference load.
6952 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006953 // If heap poisoning is enabled, the unpoisoning of the loaded
6954 // reference will be carried out by the runtime within the slow
6955 // path.
6956 //
6957 // Note that `ref` currently does not get unpoisoned (when heap
6958 // poisoning is enabled), which is alright as the `ref` argument is
6959 // not used by the artReadBarrierSlow entry point.
6960 //
6961 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006962 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00006963 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6964 AddSlowPath(slow_path);
6965
Roland Levillain3b359c72015-11-17 19:35:12 +00006966 __ b(slow_path->GetEntryLabel());
6967 __ Bind(slow_path->GetExitLabel());
6968}
6969
Roland Levillainc9285912015-12-18 10:38:42 +00006970void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6971 Location out,
6972 Location ref,
6973 Location obj,
6974 uint32_t offset,
6975 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006976 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006977 // Baker's read barriers shall be handled by the fast path
6978 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6979 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006980 // If heap poisoning is enabled, unpoisoning will be taken care of
6981 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006982 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006983 } else if (kPoisonHeapReferences) {
6984 __ UnpoisonHeapReference(out.AsRegister<Register>());
6985 }
6986}
6987
Roland Levillainc9285912015-12-18 10:38:42 +00006988void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6989 Location out,
6990 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006991 DCHECK(kEmitCompilerReadBarrier);
6992
Roland Levillainc9285912015-12-18 10:38:42 +00006993 // Insert a slow path based read barrier *after* the GC root load.
6994 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006995 // Note that GC roots are not affected by heap poisoning, so we do
6996 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006997 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006998 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6999 AddSlowPath(slow_path);
7000
Roland Levillain3b359c72015-11-17 19:35:12 +00007001 __ b(slow_path->GetEntryLabel());
7002 __ Bind(slow_path->GetExitLabel());
7003}
7004
Vladimir Markodc151b22015-10-15 18:02:30 +01007005HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7006 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007007 HInvokeStaticOrDirect* invoke) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007008 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
7009 // We disable pc-relative load when there is an irreducible loop, as the optimization
7010 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007011 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
7012 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007013 if (GetGraph()->HasIrreducibleLoops() &&
7014 (dispatch_info.method_load_kind ==
7015 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
7016 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
7017 }
7018
7019 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01007020 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007021 if (&outer_dex_file != invoke->GetTargetMethod().dex_file) {
Vladimir Markodc151b22015-10-15 18:02:30 +01007022 // Calls across dex files are more likely to exceed the available BL range,
7023 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
7024 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
7025 (desired_dispatch_info.method_load_kind ==
7026 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
7027 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
7028 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
7029 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007030 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01007031 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007032 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01007033 0u
7034 };
7035 }
7036 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00007037 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007038}
7039
Vladimir Markob4536b72015-11-24 13:45:23 +00007040Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7041 Register temp) {
7042 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7043 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7044 if (!invoke->GetLocations()->Intrinsified()) {
7045 return location.AsRegister<Register>();
7046 }
7047 // For intrinsics we allow any location, so it may be on the stack.
7048 if (!location.IsRegister()) {
7049 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7050 return temp;
7051 }
7052 // For register locations, check if the register was saved. If so, get it from the stack.
7053 // Note: There is a chance that the register was saved but not overwritten, so we could
7054 // save one load. However, since this is just an intrinsic slow path we prefer this
7055 // simple and more robust approach rather that trying to determine if that's the case.
7056 SlowPathCode* slow_path = GetCurrentSlowPath();
7057 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7058 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7059 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7060 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7061 return temp;
7062 }
7063 return location.AsRegister<Register>();
7064}
7065
Nicolas Geoffray38207af2015-06-01 15:46:22 +01007066void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007067 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00007068 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00007069 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7070 // LR = code address from literal pool with link-time patch.
7071 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00007072 break;
7073 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7074 // LR = invoke->GetDirectCodePtr();
7075 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00007076 break;
7077 default:
7078 break;
7079 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007080
Vladimir Marko58155012015-08-19 12:49:41 +00007081 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7082 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007083 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7084 uint32_t offset =
7085 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007086 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007087 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007088 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007089 }
Vladimir Marko58155012015-08-19 12:49:41 +00007090 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007091 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007092 break;
7093 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7094 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7095 break;
7096 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
7097 __ LoadLiteral(temp.AsRegister<Register>(),
7098 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
7099 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007100 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7101 HArmDexCacheArraysBase* base =
7102 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7103 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7104 temp.AsRegister<Register>());
7105 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7106 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7107 break;
7108 }
Vladimir Marko58155012015-08-19 12:49:41 +00007109 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007110 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007111 Register method_reg;
7112 Register reg = temp.AsRegister<Register>();
7113 if (current_method.IsRegister()) {
7114 method_reg = current_method.AsRegister<Register>();
7115 } else {
7116 DCHECK(invoke->GetLocations()->Intrinsified());
7117 DCHECK(!current_method.IsValid());
7118 method_reg = reg;
7119 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7120 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007121 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7122 __ LoadFromOffset(kLoadWord,
7123 reg,
7124 method_reg,
7125 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007126 // temp = temp[index_in_cache];
7127 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7128 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007129 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7130 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007131 }
Vladimir Marko58155012015-08-19 12:49:41 +00007132 }
7133
7134 switch (invoke->GetCodePtrLocation()) {
7135 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7136 __ bl(GetFrameEntryLabel());
7137 break;
7138 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007139 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
7140 invoke->GetTargetMethod().dex_method_index);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007141 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01007142 // Arbitrarily branch to the BL itself, override at link time.
7143 __ bl(&relative_call_patches_.back().label);
7144 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007145 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
7146 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
7147 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00007148 // LR()
7149 __ blx(LR);
7150 break;
7151 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7152 // LR = callee_method->entry_point_from_quick_compiled_code_
7153 __ LoadFromOffset(
7154 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007155 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007156 // LR()
7157 __ blx(LR);
7158 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007159 }
7160
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007161 DCHECK(!IsLeafMethod());
7162}
7163
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007164void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7165 Register temp = temp_location.AsRegister<Register>();
7166 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7167 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007168
7169 // Use the calling convention instead of the location of the receiver, as
7170 // intrinsics may have put the receiver in a different register. In the intrinsics
7171 // slow path, the arguments have been moved to the right place, so here we are
7172 // guaranteed that the receiver is the first register of the calling convention.
7173 InvokeDexCallingConvention calling_convention;
7174 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007175 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007176 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007177 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007178 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007179 // Instead of simply (possibly) unpoisoning `temp` here, we should
7180 // emit a read barrier for the previous class reference load.
7181 // However this is not required in practice, as this is an
7182 // intermediate/temporary reference and because the current
7183 // concurrent copying collector keeps the from-space memory
7184 // intact/accessible until the end of the marking phase (the
7185 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007186 __ MaybeUnpoisonHeapReference(temp);
7187 // temp = temp->GetMethodAt(method_offset);
7188 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007189 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007190 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7191 // LR = temp->GetEntryPoint();
7192 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7193 // LR();
7194 __ blx(LR);
7195}
7196
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007197CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
7198 const DexFile& dex_file, uint32_t string_index) {
7199 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
7200}
7201
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007202CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
7203 const DexFile& dex_file, uint32_t type_index) {
7204 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
7205}
7206
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007207CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7208 const DexFile& dex_file, uint32_t element_offset) {
7209 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7210}
7211
7212CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7213 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7214 patches->emplace_back(dex_file, offset_or_index);
7215 return &patches->back();
7216}
7217
7218Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
7219 uint32_t string_index) {
7220 return boot_image_string_patches_.GetOrCreate(
7221 StringReference(&dex_file, string_index),
7222 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7223}
7224
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007225Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
7226 uint32_t type_index) {
7227 return boot_image_type_patches_.GetOrCreate(
7228 TypeReference(&dex_file, type_index),
7229 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7230}
7231
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007232Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7233 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7234 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7235 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7236}
7237
7238Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
7239 return DeduplicateUint32Literal(address, &uint32_literals_);
7240}
7241
Vladimir Markoaad75c62016-10-03 08:46:48 +00007242template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7243inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7244 const ArenaDeque<PcRelativePatchInfo>& infos,
7245 ArenaVector<LinkerPatch>* linker_patches) {
7246 for (const PcRelativePatchInfo& info : infos) {
7247 const DexFile& dex_file = info.target_dex_file;
7248 size_t offset_or_index = info.offset_or_index;
7249 DCHECK(info.add_pc_label.IsBound());
7250 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7251 // Add MOVW patch.
7252 DCHECK(info.movw_label.IsBound());
7253 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7254 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7255 // Add MOVT patch.
7256 DCHECK(info.movt_label.IsBound());
7257 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7258 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7259 }
7260}
7261
Vladimir Marko58155012015-08-19 12:49:41 +00007262void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7263 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007264 size_t size =
7265 method_patches_.size() +
7266 call_patches_.size() +
7267 relative_call_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007268 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007269 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007270 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007271 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007272 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007273 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007274 linker_patches->reserve(size);
7275 for (const auto& entry : method_patches_) {
7276 const MethodReference& target_method = entry.first;
7277 Literal* literal = entry.second;
7278 DCHECK(literal->GetLabel()->IsBound());
7279 uint32_t literal_offset = literal->GetLabel()->Position();
7280 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
7281 target_method.dex_file,
7282 target_method.dex_method_index));
7283 }
7284 for (const auto& entry : call_patches_) {
7285 const MethodReference& target_method = entry.first;
7286 Literal* literal = entry.second;
7287 DCHECK(literal->GetLabel()->IsBound());
7288 uint32_t literal_offset = literal->GetLabel()->Position();
7289 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
7290 target_method.dex_file,
7291 target_method.dex_method_index));
7292 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007293 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko58155012015-08-19 12:49:41 +00007294 uint32_t literal_offset = info.label.Position();
Vladimir Markoaad75c62016-10-03 08:46:48 +00007295 linker_patches->push_back(
7296 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00007297 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007298 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7299 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007300 for (const auto& entry : boot_image_string_patches_) {
7301 const StringReference& target_string = entry.first;
7302 Literal* literal = entry.second;
7303 DCHECK(literal->GetLabel()->IsBound());
7304 uint32_t literal_offset = literal->GetLabel()->Position();
7305 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7306 target_string.dex_file,
7307 target_string.string_index));
7308 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007309 if (!GetCompilerOptions().IsBootImage()) {
7310 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7311 linker_patches);
7312 } else {
7313 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7314 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007315 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007316 for (const auto& entry : boot_image_type_patches_) {
7317 const TypeReference& target_type = entry.first;
7318 Literal* literal = entry.second;
7319 DCHECK(literal->GetLabel()->IsBound());
7320 uint32_t literal_offset = literal->GetLabel()->Position();
7321 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7322 target_type.dex_file,
7323 target_type.type_index));
7324 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007325 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7326 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007327 for (const auto& entry : boot_image_address_patches_) {
7328 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7329 Literal* literal = entry.second;
7330 DCHECK(literal->GetLabel()->IsBound());
7331 uint32_t literal_offset = literal->GetLabel()->Position();
7332 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7333 }
7334}
7335
7336Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7337 return map->GetOrCreate(
7338 value,
7339 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007340}
7341
7342Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7343 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007344 return map->GetOrCreate(
7345 target_method,
7346 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007347}
7348
7349Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
7350 return DeduplicateMethodLiteral(target_method, &method_patches_);
7351}
7352
7353Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
7354 return DeduplicateMethodLiteral(target_method, &call_patches_);
7355}
7356
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007357void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7358 LocationSummary* locations =
7359 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7360 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7361 Location::RequiresRegister());
7362 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7363 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7364 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7365}
7366
7367void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7368 LocationSummary* locations = instr->GetLocations();
7369 Register res = locations->Out().AsRegister<Register>();
7370 Register accumulator =
7371 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7372 Register mul_left =
7373 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7374 Register mul_right =
7375 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7376
7377 if (instr->GetOpKind() == HInstruction::kAdd) {
7378 __ mla(res, mul_left, mul_right, accumulator);
7379 } else {
7380 __ mls(res, mul_left, mul_right, accumulator);
7381 }
7382}
7383
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007384void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007385 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007386 LOG(FATAL) << "Unreachable";
7387}
7388
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007389void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007390 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007391 LOG(FATAL) << "Unreachable";
7392}
7393
Mark Mendellfe57faa2015-09-18 09:26:15 -04007394// Simple implementation of packed switch - generate cascaded compare/jumps.
7395void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7396 LocationSummary* locations =
7397 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7398 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007399 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007400 codegen_->GetAssembler()->IsThumb()) {
7401 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7402 if (switch_instr->GetStartValue() != 0) {
7403 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7404 }
7405 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007406}
7407
7408void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7409 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007410 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007411 LocationSummary* locations = switch_instr->GetLocations();
7412 Register value_reg = locations->InAt(0).AsRegister<Register>();
7413 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7414
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007415 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007416 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007417 Register temp_reg = IP;
7418 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7419 // the immediate, because IP is used as the destination register. For the other
7420 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7421 // and they can be encoded in the instruction without making use of IP register.
7422 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7423
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007424 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007425 // Jump to successors[0] if value == lower_bound.
7426 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7427 int32_t last_index = 0;
7428 for (; num_entries - last_index > 2; last_index += 2) {
7429 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7430 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7431 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7432 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7433 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7434 }
7435 if (num_entries - last_index == 2) {
7436 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007437 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007438 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007439 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007440
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007441 // And the default for any other value.
7442 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7443 __ b(codegen_->GetLabelOf(default_block));
7444 }
7445 } else {
7446 // Create a table lookup.
7447 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7448
7449 // Materialize a pointer to the switch table
7450 std::vector<Label*> labels(num_entries);
7451 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7452 for (uint32_t i = 0; i < num_entries; i++) {
7453 labels[i] = codegen_->GetLabelOf(successors[i]);
7454 }
7455 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7456
7457 // Remove the bias.
7458 Register key_reg;
7459 if (lower_bound != 0) {
7460 key_reg = locations->GetTemp(1).AsRegister<Register>();
7461 __ AddConstant(key_reg, value_reg, -lower_bound);
7462 } else {
7463 key_reg = value_reg;
7464 }
7465
7466 // Check whether the value is in the table, jump to default block if not.
7467 __ CmpConstant(key_reg, num_entries - 1);
7468 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7469
7470 // Load the displacement from the table.
7471 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7472
7473 // Dispatch is a direct add to the PC (for Thumb2).
7474 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007475 }
7476}
7477
Vladimir Markob4536b72015-11-24 13:45:23 +00007478void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7479 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7480 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007481}
7482
7483void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7484 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007485 CodeGeneratorARM::PcRelativePatchInfo* labels =
7486 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007487 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007488 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007489 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007490 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007491 __ BindTrackedLabel(&labels->add_pc_label);
7492 __ add(base_reg, base_reg, ShifterOperand(PC));
7493}
7494
Andreas Gampe85b62f22015-09-09 13:15:38 -07007495void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7496 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007497 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007498 return;
7499 }
7500
7501 DCHECK_NE(type, Primitive::kPrimVoid);
7502
7503 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7504 if (return_loc.Equals(trg)) {
7505 return;
7506 }
7507
7508 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7509 // with the last branch.
7510 if (type == Primitive::kPrimLong) {
7511 HParallelMove parallel_move(GetGraph()->GetArena());
7512 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7513 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7514 GetMoveResolver()->EmitNativeCode(&parallel_move);
7515 } else if (type == Primitive::kPrimDouble) {
7516 HParallelMove parallel_move(GetGraph()->GetArena());
7517 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7518 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7519 GetMoveResolver()->EmitNativeCode(&parallel_move);
7520 } else {
7521 // Let the parallel move resolver take care of all of this.
7522 HParallelMove parallel_move(GetGraph()->GetArena());
7523 parallel_move.AddMove(return_loc, trg, type, nullptr);
7524 GetMoveResolver()->EmitNativeCode(&parallel_move);
7525 }
7526}
7527
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007528void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7529 LocationSummary* locations =
7530 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7531 locations->SetInAt(0, Location::RequiresRegister());
7532 locations->SetOut(Location::RequiresRegister());
7533}
7534
7535void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7536 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007537 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007538 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007539 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007540 __ LoadFromOffset(kLoadWord,
7541 locations->Out().AsRegister<Register>(),
7542 locations->InAt(0).AsRegister<Register>(),
7543 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007544 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007545 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007546 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007547 __ LoadFromOffset(kLoadWord,
7548 locations->Out().AsRegister<Register>(),
7549 locations->InAt(0).AsRegister<Register>(),
7550 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7551 __ LoadFromOffset(kLoadWord,
7552 locations->Out().AsRegister<Register>(),
7553 locations->Out().AsRegister<Register>(),
7554 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007555 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007556}
7557
Roland Levillain4d027112015-07-01 15:41:14 +01007558#undef __
7559#undef QUICK_ENTRY_POINT
7560
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007561} // namespace arm
7562} // namespace art