blob: 3b2758bc422601a010cfcf195168b80dd3f4fa5f [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) {
110 __ StoreDToOffset(d_reg, SP, stack_offset);
111 } 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;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100194 for (size_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());
David Brazdil77a48ae2015-09-15 12:34:04 +0000278 if (instruction_->CanThrowIntoCatchBlock()) {
279 // Live registers will be restored in the catch block if caught.
280 SaveLiveRegisters(codegen, instruction_->GetLocations());
281 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100282 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000283 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000284 }
285
Alexandre Rames8158f282015-08-07 10:26:17 +0100286 bool IsFatal() const OVERRIDE { return true; }
287
Alexandre Rames9931f312015-06-19 14:47:01 +0100288 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
289
Calin Juravled0d48522014-11-04 16:40:20 +0000290 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000291 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
292};
293
Artem Serovf4d6aee2016-07-11 10:41:45 +0100294class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000295 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000296 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100297 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000298
Alexandre Rames67555f72014-11-18 10:55:16 +0000299 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100300 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000301 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100302 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000303 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100304 if (successor_ == nullptr) {
305 __ b(GetReturnLabel());
306 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100307 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100308 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000309 }
310
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100311 Label* GetReturnLabel() {
312 DCHECK(successor_ == nullptr);
313 return &return_label_;
314 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000315
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100316 HBasicBlock* GetSuccessor() const {
317 return successor_;
318 }
319
Alexandre Rames9931f312015-06-19 14:47:01 +0100320 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
321
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000322 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100323 // If not null, the block to branch to after the suspend check.
324 HBasicBlock* const successor_;
325
326 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000327 Label return_label_;
328
329 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
330};
331
Artem Serovf4d6aee2016-07-11 10:41:45 +0100332class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100333 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100334 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100335 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100336
Alexandre Rames67555f72014-11-18 10:55:16 +0000337 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100338 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100339 LocationSummary* locations = instruction_->GetLocations();
340
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100341 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000342 if (instruction_->CanThrowIntoCatchBlock()) {
343 // Live registers will be restored in the catch block if caught.
344 SaveLiveRegisters(codegen, instruction_->GetLocations());
345 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000346 // We're moving two locations to locations that could overlap, so we need a parallel
347 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100348 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000349 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100350 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000351 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100352 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100353 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100354 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
355 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100356 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
357 ? kQuickThrowStringBounds
358 : kQuickThrowArrayBounds;
359 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100360 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000361 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100362 }
363
Alexandre Rames8158f282015-08-07 10:26:17 +0100364 bool IsFatal() const OVERRIDE { return true; }
365
Alexandre Rames9931f312015-06-19 14:47:01 +0100366 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
367
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100368 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100369 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
370};
371
Artem Serovf4d6aee2016-07-11 10:41:45 +0100372class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100373 public:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000374 LoadClassSlowPathARM(HLoadClass* cls,
375 HInstruction* at,
376 uint32_t dex_pc,
377 bool do_clinit)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100378 : SlowPathCodeARM(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000379 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
380 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100381
Alexandre Rames67555f72014-11-18 10:55:16 +0000382 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000383 LocationSummary* locations = at_->GetLocations();
384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100385 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
386 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000387 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100388
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100389 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000390 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100391 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
392 : kQuickInitializeType;
393 arm_codegen->InvokeRuntime(entrypoint, at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000394 if (do_clinit_) {
395 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
396 } else {
397 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
398 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000399
400 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000401 Location out = locations->Out();
402 if (out.IsValid()) {
403 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000404 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
405 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000406 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100407 __ b(GetExitLabel());
408 }
409
Alexandre Rames9931f312015-06-19 14:47:01 +0100410 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
411
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100412 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000413 // The class this slow path will load.
414 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100415
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000416 // The instruction where this slow path is happening.
417 // (Might be the load class or an initialization check).
418 HInstruction* const at_;
419
420 // The dex PC of `at_`.
421 const uint32_t dex_pc_;
422
423 // Whether to initialize the class.
424 const bool do_clinit_;
425
426 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100427};
428
Artem Serovf4d6aee2016-07-11 10:41:45 +0100429class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000430 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000431 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100432 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000433
Alexandre Rames67555f72014-11-18 10:55:16 +0000434 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000435 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100436 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
437 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000438 DCHECK(instruction_->IsCheckCast()
439 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000440
441 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
442 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000443
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000444 if (!is_fatal_) {
445 SaveLiveRegisters(codegen, locations);
446 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000447
448 // We're moving two locations to locations that could overlap, so we need a parallel
449 // move resolver.
450 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000451 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100452 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000453 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100454 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100455 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100456 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
457 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000458
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000459 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100460 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100461 instruction_,
462 instruction_->GetDexPc(),
463 this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000464 CheckEntrypointTypes<
Andreas Gampe67409972016-07-19 22:34:53 -0700465 kQuickInstanceofNonTrivial, size_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000466 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
467 } else {
468 DCHECK(instruction_->IsCheckCast());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100469 arm_codegen->InvokeRuntime(kQuickCheckCast, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000470 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000471 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000472
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000473 if (!is_fatal_) {
474 RestoreLiveRegisters(codegen, locations);
475 __ b(GetExitLabel());
476 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000477 }
478
Alexandre Rames9931f312015-06-19 14:47:01 +0100479 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
480
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000481 bool IsFatal() const OVERRIDE { return is_fatal_; }
482
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000483 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000484 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000485
486 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
487};
488
Artem Serovf4d6aee2016-07-11 10:41:45 +0100489class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700490 public:
Aart Bik42249c32016-01-07 15:33:50 -0800491 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100492 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700493
494 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800495 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700496 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100497 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000498 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700499 }
500
Alexandre Rames9931f312015-06-19 14:47:01 +0100501 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
502
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700503 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700504 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
505};
506
Artem Serovf4d6aee2016-07-11 10:41:45 +0100507class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100508 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100509 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100510
511 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
512 LocationSummary* locations = instruction_->GetLocations();
513 __ Bind(GetEntryLabel());
514 SaveLiveRegisters(codegen, locations);
515
516 InvokeRuntimeCallingConvention calling_convention;
517 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
518 parallel_move.AddMove(
519 locations->InAt(0),
520 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
521 Primitive::kPrimNot,
522 nullptr);
523 parallel_move.AddMove(
524 locations->InAt(1),
525 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
526 Primitive::kPrimInt,
527 nullptr);
528 parallel_move.AddMove(
529 locations->InAt(2),
530 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
531 Primitive::kPrimNot,
532 nullptr);
533 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
534
535 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100536 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000537 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100538 RestoreLiveRegisters(codegen, locations);
539 __ b(GetExitLabel());
540 }
541
542 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
543
544 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100545 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
546};
547
Roland Levillainc9285912015-12-18 10:38:42 +0000548// Slow path marking an object during a read barrier.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100549class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000550 public:
Roland Levillain02b75802016-07-13 11:54:35 +0100551 ReadBarrierMarkSlowPathARM(HInstruction* instruction, Location obj)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100552 : SlowPathCodeARM(instruction), obj_(obj) {
Roland Levillainc9285912015-12-18 10:38:42 +0000553 DCHECK(kEmitCompilerReadBarrier);
554 }
555
556 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
557
558 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
559 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain02b75802016-07-13 11:54:35 +0100560 Register reg = obj_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000561 DCHECK(locations->CanCall());
Roland Levillain02b75802016-07-13 11:54:35 +0100562 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg));
Roland Levillainc9285912015-12-18 10:38:42 +0000563 DCHECK(instruction_->IsInstanceFieldGet() ||
564 instruction_->IsStaticFieldGet() ||
565 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100566 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000567 instruction_->IsLoadClass() ||
568 instruction_->IsLoadString() ||
569 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100570 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100571 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
572 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000573 << "Unexpected instruction in read barrier marking slow path: "
574 << instruction_->DebugName();
575
576 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100577 // No need to save live registers; it's taken care of by the
578 // entrypoint. Also, there is no need to update the stack mask,
579 // as this runtime call will not trigger a garbage collection.
Roland Levillainc9285912015-12-18 10:38:42 +0000580 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillain02b75802016-07-13 11:54:35 +0100581 DCHECK_NE(reg, SP);
582 DCHECK_NE(reg, LR);
583 DCHECK_NE(reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100584 // IP is used internally by the ReadBarrierMarkRegX entry point
585 // as a temporary, it cannot be the entry point's input/output.
586 DCHECK_NE(reg, IP);
Roland Levillain02b75802016-07-13 11:54:35 +0100587 DCHECK(0 <= reg && reg < kNumberOfCoreRegisters) << reg;
588 // "Compact" slow path, saving two moves.
589 //
590 // Instead of using the standard runtime calling convention (input
591 // and output in R0):
592 //
593 // R0 <- obj
594 // R0 <- ReadBarrierMark(R0)
595 // obj <- R0
596 //
597 // we just use rX (the register holding `obj`) as input and output
598 // of a dedicated entrypoint:
599 //
600 // rX <- ReadBarrierMarkRegX(rX)
601 //
602 int32_t entry_point_offset =
Andreas Gampe542451c2016-07-26 09:02:02 -0700603 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100604 // This runtime call does not require a stack map.
605 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillainc9285912015-12-18 10:38:42 +0000606 __ b(GetExitLabel());
607 }
608
609 private:
Roland Levillainc9285912015-12-18 10:38:42 +0000610 const Location obj_;
611
612 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
613};
614
Roland Levillain3b359c72015-11-17 19:35:12 +0000615// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100616class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000617 public:
618 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
619 Location out,
620 Location ref,
621 Location obj,
622 uint32_t offset,
623 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100624 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000625 out_(out),
626 ref_(ref),
627 obj_(obj),
628 offset_(offset),
629 index_(index) {
630 DCHECK(kEmitCompilerReadBarrier);
631 // If `obj` is equal to `out` or `ref`, it means the initial object
632 // has been overwritten by (or after) the heap object reference load
633 // to be instrumented, e.g.:
634 //
635 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000636 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000637 //
638 // In that case, we have lost the information about the original
639 // object, and the emitted read barrier cannot work properly.
640 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
641 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
642 }
643
644 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
645 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
646 LocationSummary* locations = instruction_->GetLocations();
647 Register reg_out = out_.AsRegister<Register>();
648 DCHECK(locations->CanCall());
649 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100650 DCHECK(instruction_->IsInstanceFieldGet() ||
651 instruction_->IsStaticFieldGet() ||
652 instruction_->IsArrayGet() ||
653 instruction_->IsInstanceOf() ||
654 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100655 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000656 << "Unexpected instruction in read barrier for heap reference slow path: "
657 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000658
659 __ Bind(GetEntryLabel());
660 SaveLiveRegisters(codegen, locations);
661
662 // We may have to change the index's value, but as `index_` is a
663 // constant member (like other "inputs" of this slow path),
664 // introduce a copy of it, `index`.
665 Location index = index_;
666 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100667 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000668 if (instruction_->IsArrayGet()) {
669 // Compute the actual memory offset and store it in `index`.
670 Register index_reg = index_.AsRegister<Register>();
671 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
672 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
673 // We are about to change the value of `index_reg` (see the
674 // calls to art::arm::Thumb2Assembler::Lsl and
675 // art::arm::Thumb2Assembler::AddConstant below), but it has
676 // not been saved by the previous call to
677 // art::SlowPathCode::SaveLiveRegisters, as it is a
678 // callee-save register --
679 // art::SlowPathCode::SaveLiveRegisters does not consider
680 // callee-save registers, as it has been designed with the
681 // assumption that callee-save registers are supposed to be
682 // handled by the called function. So, as a callee-save
683 // register, `index_reg` _would_ eventually be saved onto
684 // the stack, but it would be too late: we would have
685 // changed its value earlier. Therefore, we manually save
686 // it here into another freely available register,
687 // `free_reg`, chosen of course among the caller-save
688 // registers (as a callee-save `free_reg` register would
689 // exhibit the same problem).
690 //
691 // Note we could have requested a temporary register from
692 // the register allocator instead; but we prefer not to, as
693 // this is a slow path, and we know we can find a
694 // caller-save register that is available.
695 Register free_reg = FindAvailableCallerSaveRegister(codegen);
696 __ Mov(free_reg, index_reg);
697 index_reg = free_reg;
698 index = Location::RegisterLocation(index_reg);
699 } else {
700 // The initial register stored in `index_` has already been
701 // saved in the call to art::SlowPathCode::SaveLiveRegisters
702 // (as it is not a callee-save register), so we can freely
703 // use it.
704 }
705 // Shifting the index value contained in `index_reg` by the scale
706 // factor (2) cannot overflow in practice, as the runtime is
707 // unable to allocate object arrays with a size larger than
708 // 2^26 - 1 (that is, 2^28 - 4 bytes).
709 __ Lsl(index_reg, index_reg, TIMES_4);
710 static_assert(
711 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
712 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
713 __ AddConstant(index_reg, index_reg, offset_);
714 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100715 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
716 // intrinsics, `index_` is not shifted by a scale factor of 2
717 // (as in the case of ArrayGet), as it is actually an offset
718 // to an object field within an object.
719 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000720 DCHECK(instruction_->GetLocations()->Intrinsified());
721 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
722 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
723 << instruction_->AsInvoke()->GetIntrinsic();
724 DCHECK_EQ(offset_, 0U);
725 DCHECK(index_.IsRegisterPair());
726 // UnsafeGet's offset location is a register pair, the low
727 // part contains the correct offset.
728 index = index_.ToLow();
729 }
730 }
731
732 // We're moving two or three locations to locations that could
733 // overlap, so we need a parallel move resolver.
734 InvokeRuntimeCallingConvention calling_convention;
735 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
736 parallel_move.AddMove(ref_,
737 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
738 Primitive::kPrimNot,
739 nullptr);
740 parallel_move.AddMove(obj_,
741 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
742 Primitive::kPrimNot,
743 nullptr);
744 if (index.IsValid()) {
745 parallel_move.AddMove(index,
746 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
747 Primitive::kPrimInt,
748 nullptr);
749 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
750 } else {
751 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
752 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
753 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100754 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +0000755 CheckEntrypointTypes<
756 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
757 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
758
759 RestoreLiveRegisters(codegen, locations);
760 __ b(GetExitLabel());
761 }
762
763 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
764
765 private:
766 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
767 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
768 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
769 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
770 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
771 return static_cast<Register>(i);
772 }
773 }
774 // We shall never fail to find a free caller-save register, as
775 // there are more than two core caller-save registers on ARM
776 // (meaning it is possible to find one which is different from
777 // `ref` and `obj`).
778 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
779 LOG(FATAL) << "Could not find a free caller-save register";
780 UNREACHABLE();
781 }
782
Roland Levillain3b359c72015-11-17 19:35:12 +0000783 const Location out_;
784 const Location ref_;
785 const Location obj_;
786 const uint32_t offset_;
787 // An additional location containing an index to an array.
788 // Only used for HArrayGet and the UnsafeGetObject &
789 // UnsafeGetObjectVolatile intrinsics.
790 const Location index_;
791
792 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
793};
794
795// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100796class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000797 public:
798 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100799 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +0000800 DCHECK(kEmitCompilerReadBarrier);
801 }
Roland Levillain3b359c72015-11-17 19:35:12 +0000802
803 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
804 LocationSummary* locations = instruction_->GetLocations();
805 Register reg_out = out_.AsRegister<Register>();
806 DCHECK(locations->CanCall());
807 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +0000808 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
809 << "Unexpected instruction in read barrier for GC root slow path: "
810 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +0000811
812 __ Bind(GetEntryLabel());
813 SaveLiveRegisters(codegen, locations);
814
815 InvokeRuntimeCallingConvention calling_convention;
816 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
817 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100818 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +0000819 instruction_,
820 instruction_->GetDexPc(),
821 this);
822 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
823 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
824
825 RestoreLiveRegisters(codegen, locations);
826 __ b(GetExitLabel());
827 }
828
829 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
830
831 private:
Roland Levillain3b359c72015-11-17 19:35:12 +0000832 const Location out_;
833 const Location root_;
834
835 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
836};
837
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000838#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100839// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
840#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
Dave Allison20dfc792014-06-16 20:44:29 -0700841
Aart Bike9f37602015-10-09 11:15:55 -0700842inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700843 switch (cond) {
844 case kCondEQ: return EQ;
845 case kCondNE: return NE;
846 case kCondLT: return LT;
847 case kCondLE: return LE;
848 case kCondGT: return GT;
849 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -0700850 case kCondB: return LO;
851 case kCondBE: return LS;
852 case kCondA: return HI;
853 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700854 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100855 LOG(FATAL) << "Unreachable";
856 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700857}
858
Aart Bike9f37602015-10-09 11:15:55 -0700859// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100860inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700861 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +0100862 case kCondEQ: return EQ;
863 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -0700864 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100865 case kCondLT: return LO;
866 case kCondLE: return LS;
867 case kCondGT: return HI;
868 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -0700869 // Unsigned remain unchanged.
870 case kCondB: return LO;
871 case kCondBE: return LS;
872 case kCondA: return HI;
873 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -0700874 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100875 LOG(FATAL) << "Unreachable";
876 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700877}
878
Vladimir Markod6e069b2016-01-18 11:11:01 +0000879inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
880 // The ARM condition codes can express all the necessary branches, see the
881 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
882 // There is no dex instruction or HIR that would need the missing conditions
883 // "equal or unordered" or "not equal".
884 switch (cond) {
885 case kCondEQ: return EQ;
886 case kCondNE: return NE /* unordered */;
887 case kCondLT: return gt_bias ? CC : LT /* unordered */;
888 case kCondLE: return gt_bias ? LS : LE /* unordered */;
889 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
890 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
891 default:
892 LOG(FATAL) << "UNREACHABLE";
893 UNREACHABLE();
894 }
895}
896
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100897void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100898 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100899}
900
901void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100902 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100903}
904
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100905size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
906 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
907 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100908}
909
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100910size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
911 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
912 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100913}
914
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000915size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
916 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
917 return kArmWordSize;
918}
919
920size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
921 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
922 return kArmWordSize;
923}
924
Calin Juravle34166012014-12-19 17:22:29 +0000925CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000926 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100927 const CompilerOptions& compiler_options,
928 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000929 : CodeGenerator(graph,
930 kNumberOfCoreRegisters,
931 kNumberOfSRegisters,
932 kNumberOfRegisterPairs,
933 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
934 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +0000935 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
936 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100937 compiler_options,
938 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100939 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100940 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +0100941 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100942 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100943 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000944 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000945 uint32_literals_(std::less<uint32_t>(),
946 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100947 method_patches_(MethodReferenceComparator(),
948 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
949 call_patches_(MethodReferenceComparator(),
950 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markob4536b72015-11-24 13:45:23 +0000951 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000952 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
953 boot_image_string_patches_(StringReferenceValueComparator(),
954 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
955 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100956 boot_image_type_patches_(TypeReferenceValueComparator(),
957 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
958 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000959 boot_image_address_patches_(std::less<uint32_t>(),
960 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -0700961 // Always save the LR register to mimic Quick.
962 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100963}
964
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000965void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
966 // Ensure that we fix up branches and literal loads and emit the literal pool.
967 __ FinalizeCode();
968
969 // Adjust native pc offsets in stack maps.
970 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
971 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset;
972 uint32_t new_position = __ GetAdjustedPosition(old_position);
973 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
974 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100975 // Adjust pc offsets for the disassembly information.
976 if (disasm_info_ != nullptr) {
977 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
978 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
979 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
980 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
981 it.second.start = __ GetAdjustedPosition(it.second.start);
982 it.second.end = __ GetAdjustedPosition(it.second.end);
983 }
984 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
985 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
986 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
987 }
988 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +0000989
990 CodeGenerator::Finalize(allocator);
991}
992
David Brazdil58282f42016-01-14 12:45:10 +0000993void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100994 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100995 blocked_register_pairs_[R1_R2] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100996
997 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100998 blocked_core_registers_[SP] = true;
999 blocked_core_registers_[LR] = true;
1000 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001001
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001002 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001003 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001004
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001005 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001006 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001007
David Brazdil58282f42016-01-14 12:45:10 +00001008 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001009 // Stubs do not save callee-save floating point registers. If the graph
1010 // is debuggable, we need to deal with these registers differently. For
1011 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001012 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1013 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1014 }
1015 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01001016
1017 UpdateBlockedPairRegisters();
1018}
1019
1020void CodeGeneratorARM::UpdateBlockedPairRegisters() const {
1021 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
1022 ArmManagedRegister current =
1023 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
1024 if (blocked_core_registers_[current.AsRegisterPairLow()]
1025 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
1026 blocked_register_pairs_[i] = true;
1027 }
1028 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001029}
1030
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001031InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001032 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001033 assembler_(codegen->GetAssembler()),
1034 codegen_(codegen) {}
1035
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001036void CodeGeneratorARM::ComputeSpillMask() {
1037 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1038 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001039 // There is no easy instruction to restore just the PC on thumb2. We spill and
1040 // restore another arbitrary register.
1041 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001042 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1043 // We use vpush and vpop for saving and restoring floating point registers, which take
1044 // a SRegister and the number of registers to save/restore after that SRegister. We
1045 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1046 // but in the range.
1047 if (fpu_spill_mask_ != 0) {
1048 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1049 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1050 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1051 fpu_spill_mask_ |= (1 << i);
1052 }
1053 }
1054}
1055
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001056static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001057 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001058}
1059
1060static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001061 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001062}
1063
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001064void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001065 bool skip_overflow_check =
1066 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001067 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001068 __ Bind(&frame_entry_label_);
1069
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001070 if (HasEmptyFrame()) {
1071 return;
1072 }
1073
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001074 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001075 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1076 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1077 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001078 }
1079
Andreas Gampe501fd632015-09-10 16:11:06 -07001080 __ PushList(core_spill_mask_);
1081 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1082 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001083 if (fpu_spill_mask_ != 0) {
1084 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1085 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001086 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001087 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001088 }
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001089 int adjust = GetFrameSize() - FrameEntrySpillSize();
1090 __ AddConstant(SP, -adjust);
1091 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001092 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001093}
1094
1095void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001096 if (HasEmptyFrame()) {
1097 __ bx(LR);
1098 return;
1099 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001100 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001101 int adjust = GetFrameSize() - FrameEntrySpillSize();
1102 __ AddConstant(SP, adjust);
1103 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001104 if (fpu_spill_mask_ != 0) {
1105 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1106 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001107 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001108 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001109 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001110 // Pop LR into PC to return.
1111 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1112 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1113 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001114 __ cfi().RestoreState();
1115 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001116}
1117
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001118void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001119 Label* label = GetLabelOf(block);
1120 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001121}
1122
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001123Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001124 switch (type) {
1125 case Primitive::kPrimBoolean:
1126 case Primitive::kPrimByte:
1127 case Primitive::kPrimChar:
1128 case Primitive::kPrimShort:
1129 case Primitive::kPrimInt:
1130 case Primitive::kPrimNot: {
1131 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001132 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001133 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001134 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001135 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001136 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001137 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001138 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001139
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001140 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001141 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001142 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001143 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001144 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001145 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001146 if (calling_convention.GetRegisterAt(index) == R1) {
1147 // Skip R1, and use R2_R3 instead.
1148 gp_index_++;
1149 index++;
1150 }
1151 }
1152 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1153 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001154 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001155
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001156 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001157 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001158 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001159 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1160 }
1161 }
1162
1163 case Primitive::kPrimFloat: {
1164 uint32_t stack_index = stack_index_++;
1165 if (float_index_ % 2 == 0) {
1166 float_index_ = std::max(double_index_, float_index_);
1167 }
1168 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1169 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1170 } else {
1171 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1172 }
1173 }
1174
1175 case Primitive::kPrimDouble: {
1176 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1177 uint32_t stack_index = stack_index_;
1178 stack_index_ += 2;
1179 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1180 uint32_t index = double_index_;
1181 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001182 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001183 calling_convention.GetFpuRegisterAt(index),
1184 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001185 DCHECK(ExpectedPairLayout(result));
1186 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001187 } else {
1188 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001189 }
1190 }
1191
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001192 case Primitive::kPrimVoid:
1193 LOG(FATAL) << "Unexpected parameter type " << type;
1194 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001195 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001196 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001197}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001198
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001199Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001200 switch (type) {
1201 case Primitive::kPrimBoolean:
1202 case Primitive::kPrimByte:
1203 case Primitive::kPrimChar:
1204 case Primitive::kPrimShort:
1205 case Primitive::kPrimInt:
1206 case Primitive::kPrimNot: {
1207 return Location::RegisterLocation(R0);
1208 }
1209
1210 case Primitive::kPrimFloat: {
1211 return Location::FpuRegisterLocation(S0);
1212 }
1213
1214 case Primitive::kPrimLong: {
1215 return Location::RegisterPairLocation(R0, R1);
1216 }
1217
1218 case Primitive::kPrimDouble: {
1219 return Location::FpuRegisterPairLocation(S0, S1);
1220 }
1221
1222 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001223 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001224 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001225
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001226 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001227}
1228
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001229Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1230 return Location::RegisterLocation(kMethodRegisterArgument);
1231}
1232
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001233void CodeGeneratorARM::Move32(Location destination, Location source) {
1234 if (source.Equals(destination)) {
1235 return;
1236 }
1237 if (destination.IsRegister()) {
1238 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001239 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001240 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001241 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001242 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001243 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001244 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001245 } else if (destination.IsFpuRegister()) {
1246 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001247 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001248 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001249 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001250 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001251 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001252 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001253 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001254 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001255 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001256 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001257 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001258 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001259 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001260 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001261 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1262 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001263 }
1264 }
1265}
1266
1267void CodeGeneratorARM::Move64(Location destination, Location source) {
1268 if (source.Equals(destination)) {
1269 return;
1270 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001271 if (destination.IsRegisterPair()) {
1272 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001273 EmitParallelMoves(
1274 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1275 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001276 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001277 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001278 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1279 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001280 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001281 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001282 } else if (source.IsFpuRegisterPair()) {
1283 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1284 destination.AsRegisterPairHigh<Register>(),
1285 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001286 } else {
1287 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001288 DCHECK(ExpectedPairLayout(destination));
1289 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1290 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001291 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001292 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001293 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001294 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1295 SP,
1296 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001297 } else if (source.IsRegisterPair()) {
1298 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1299 source.AsRegisterPairLow<Register>(),
1300 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001301 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001302 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001303 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001304 } else {
1305 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001306 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001307 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001308 if (source.AsRegisterPairLow<Register>() == R1) {
1309 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001310 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1311 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001312 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001313 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001314 SP, destination.GetStackIndex());
1315 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001316 } else if (source.IsFpuRegisterPair()) {
1317 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1318 SP,
1319 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001320 } else {
1321 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001322 EmitParallelMoves(
1323 Location::StackSlot(source.GetStackIndex()),
1324 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001325 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001326 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001327 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1328 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001329 }
1330 }
1331}
1332
Calin Juravle175dc732015-08-25 15:42:32 +01001333void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1334 DCHECK(location.IsRegister());
1335 __ LoadImmediate(location.AsRegister<Register>(), value);
1336}
1337
Calin Juravlee460d1d2015-09-29 04:52:17 +01001338void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001339 HParallelMove move(GetGraph()->GetArena());
1340 move.AddMove(src, dst, dst_type, nullptr);
1341 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001342}
1343
1344void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1345 if (location.IsRegister()) {
1346 locations->AddTemp(location);
1347 } else if (location.IsRegisterPair()) {
1348 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1349 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1350 } else {
1351 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1352 }
1353}
1354
Calin Juravle175dc732015-08-25 15:42:32 +01001355void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1356 HInstruction* instruction,
1357 uint32_t dex_pc,
1358 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001359 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001360 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001361 if (EntrypointRequiresStackMap(entrypoint)) {
1362 RecordPcInfo(instruction, dex_pc, slow_path);
1363 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001364}
1365
Roland Levillaindec8f632016-07-22 17:10:06 +01001366void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1367 HInstruction* instruction,
1368 SlowPathCode* slow_path) {
1369 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001370 GenerateInvokeRuntime(entry_point_offset);
1371}
1372
1373void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001374 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1375 __ blx(LR);
1376}
1377
David Brazdilfc6a86a2015-06-26 10:33:45 +00001378void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001379 DCHECK(!successor->IsExitBlock());
1380
1381 HBasicBlock* block = got->GetBlock();
1382 HInstruction* previous = got->GetPrevious();
1383
1384 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001385 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001386 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1387 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1388 return;
1389 }
1390
1391 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1392 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1393 }
1394 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001395 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001396 }
1397}
1398
David Brazdilfc6a86a2015-06-26 10:33:45 +00001399void LocationsBuilderARM::VisitGoto(HGoto* got) {
1400 got->SetLocations(nullptr);
1401}
1402
1403void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1404 HandleGoto(got, got->GetSuccessor());
1405}
1406
1407void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1408 try_boundary->SetLocations(nullptr);
1409}
1410
1411void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1412 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1413 if (!successor->IsExitBlock()) {
1414 HandleGoto(try_boundary, successor);
1415 }
1416}
1417
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001418void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001419 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001420}
1421
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001422void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001423}
1424
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001425void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1426 Primitive::Type type = instruction->InputAt(0)->GetType();
1427 Location lhs_loc = instruction->GetLocations()->InAt(0);
1428 Location rhs_loc = instruction->GetLocations()->InAt(1);
1429 if (rhs_loc.IsConstant()) {
1430 // 0.0 is the only immediate that can be encoded directly in
1431 // a VCMP instruction.
1432 //
1433 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1434 // specify that in a floating-point comparison, positive zero
1435 // and negative zero are considered equal, so we can use the
1436 // literal 0.0 for both cases here.
1437 //
1438 // Note however that some methods (Float.equal, Float.compare,
1439 // Float.compareTo, Double.equal, Double.compare,
1440 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1441 // StrictMath.min) consider 0.0 to be (strictly) greater than
1442 // -0.0. So if we ever translate calls to these methods into a
1443 // HCompare instruction, we must handle the -0.0 case with
1444 // care here.
1445 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1446 if (type == Primitive::kPrimFloat) {
1447 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1448 } else {
1449 DCHECK_EQ(type, Primitive::kPrimDouble);
1450 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1451 }
1452 } else {
1453 if (type == Primitive::kPrimFloat) {
1454 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1455 } else {
1456 DCHECK_EQ(type, Primitive::kPrimDouble);
1457 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1458 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1459 }
1460 }
1461}
1462
Roland Levillain4fa13f62015-07-06 18:11:54 +01001463void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1464 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001465 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001466 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001467 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001468}
1469
1470void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1471 Label* true_label,
1472 Label* false_label) {
1473 LocationSummary* locations = cond->GetLocations();
1474 Location left = locations->InAt(0);
1475 Location right = locations->InAt(1);
1476 IfCondition if_cond = cond->GetCondition();
1477
1478 Register left_high = left.AsRegisterPairHigh<Register>();
1479 Register left_low = left.AsRegisterPairLow<Register>();
1480 IfCondition true_high_cond = if_cond;
1481 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001482 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001483
1484 // Set the conditions for the test, remembering that == needs to be
1485 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001486 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001487 switch (if_cond) {
1488 case kCondEQ:
1489 case kCondNE:
1490 // Nothing to do.
1491 break;
1492 case kCondLT:
1493 false_high_cond = kCondGT;
1494 break;
1495 case kCondLE:
1496 true_high_cond = kCondLT;
1497 break;
1498 case kCondGT:
1499 false_high_cond = kCondLT;
1500 break;
1501 case kCondGE:
1502 true_high_cond = kCondGT;
1503 break;
Aart Bike9f37602015-10-09 11:15:55 -07001504 case kCondB:
1505 false_high_cond = kCondA;
1506 break;
1507 case kCondBE:
1508 true_high_cond = kCondB;
1509 break;
1510 case kCondA:
1511 false_high_cond = kCondB;
1512 break;
1513 case kCondAE:
1514 true_high_cond = kCondA;
1515 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001516 }
1517 if (right.IsConstant()) {
1518 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1519 int32_t val_low = Low32Bits(value);
1520 int32_t val_high = High32Bits(value);
1521
Vladimir Markoac6ac102015-12-17 12:14:00 +00001522 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001523 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001524 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001525 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001526 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001527 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001528 __ b(true_label, ARMCondition(true_high_cond));
1529 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001530 }
1531 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00001532 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001533 } else {
1534 Register right_high = right.AsRegisterPairHigh<Register>();
1535 Register right_low = right.AsRegisterPairLow<Register>();
1536
1537 __ cmp(left_high, ShifterOperand(right_high));
1538 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001539 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001540 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001541 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001542 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001543 __ b(true_label, ARMCondition(true_high_cond));
1544 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001545 }
1546 // Must be equal high, so compare the lows.
1547 __ cmp(left_low, ShifterOperand(right_low));
1548 }
1549 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07001550 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01001551 __ b(true_label, final_condition);
1552}
1553
David Brazdil0debae72015-11-12 18:37:00 +00001554void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
1555 Label* true_target_in,
1556 Label* false_target_in) {
1557 // Generated branching requires both targets to be explicit. If either of the
1558 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1559 Label fallthrough_target;
1560 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1561 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1562
Roland Levillain4fa13f62015-07-06 18:11:54 +01001563 Primitive::Type type = condition->InputAt(0)->GetType();
1564 switch (type) {
1565 case Primitive::kPrimLong:
1566 GenerateLongComparesAndJumps(condition, true_target, false_target);
1567 break;
1568 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001569 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001570 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001571 GenerateFPJumps(condition, true_target, false_target);
1572 break;
1573 default:
1574 LOG(FATAL) << "Unexpected compare type " << type;
1575 }
1576
David Brazdil0debae72015-11-12 18:37:00 +00001577 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001578 __ b(false_target);
1579 }
David Brazdil0debae72015-11-12 18:37:00 +00001580
1581 if (fallthrough_target.IsLinked()) {
1582 __ Bind(&fallthrough_target);
1583 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001584}
1585
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001586void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001587 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001588 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001589 Label* false_target) {
1590 HInstruction* cond = instruction->InputAt(condition_input_index);
1591
1592 if (true_target == nullptr && false_target == nullptr) {
1593 // Nothing to do. The code always falls through.
1594 return;
1595 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001596 // Constant condition, statically compared against "true" (integer value 1).
1597 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001598 if (true_target != nullptr) {
1599 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001600 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001601 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001602 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001603 if (false_target != nullptr) {
1604 __ b(false_target);
1605 }
1606 }
1607 return;
1608 }
1609
1610 // The following code generates these patterns:
1611 // (1) true_target == nullptr && false_target != nullptr
1612 // - opposite condition true => branch to false_target
1613 // (2) true_target != nullptr && false_target == nullptr
1614 // - condition true => branch to true_target
1615 // (3) true_target != nullptr && false_target != nullptr
1616 // - condition true => branch to true_target
1617 // - branch to false_target
1618 if (IsBooleanValueOrMaterializedCondition(cond)) {
1619 // Condition has been materialized, compare the output to 0.
1620 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
1621 DCHECK(cond_val.IsRegister());
1622 if (true_target == nullptr) {
1623 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
1624 } else {
1625 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001626 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001627 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001628 // Condition has not been materialized. Use its inputs as the comparison and
1629 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001630 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001631
1632 // If this is a long or FP comparison that has been folded into
1633 // the HCondition, generate the comparison directly.
1634 Primitive::Type type = condition->InputAt(0)->GetType();
1635 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1636 GenerateCompareTestAndBranch(condition, true_target, false_target);
1637 return;
1638 }
1639
1640 LocationSummary* locations = cond->GetLocations();
1641 DCHECK(locations->InAt(0).IsRegister());
1642 Register left = locations->InAt(0).AsRegister<Register>();
1643 Location right = locations->InAt(1);
1644 if (right.IsRegister()) {
1645 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001646 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001647 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001648 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00001649 }
1650 if (true_target == nullptr) {
1651 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
1652 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001653 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001654 }
Dave Allison20dfc792014-06-16 20:44:29 -07001655 }
David Brazdil0debae72015-11-12 18:37:00 +00001656
1657 // If neither branch falls through (case 3), the conditional branch to `true_target`
1658 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1659 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001660 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001661 }
1662}
1663
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001664void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001665 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1666 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001667 locations->SetInAt(0, Location::RequiresRegister());
1668 }
1669}
1670
1671void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001672 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1673 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1674 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1675 nullptr : codegen_->GetLabelOf(true_successor);
1676 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1677 nullptr : codegen_->GetLabelOf(false_successor);
1678 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001679}
1680
1681void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
1682 LocationSummary* locations = new (GetGraph()->GetArena())
1683 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko239d6ea2016-09-05 10:44:04 +01001684 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001685 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001686 locations->SetInAt(0, Location::RequiresRegister());
1687 }
1688}
1689
1690void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01001691 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001692 GenerateTestAndBranch(deoptimize,
1693 /* condition_input_index */ 0,
1694 slow_path->GetEntryLabel(),
1695 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001696}
Dave Allison20dfc792014-06-16 20:44:29 -07001697
David Brazdil74eb1b22015-12-14 11:44:01 +00001698void LocationsBuilderARM::VisitSelect(HSelect* select) {
1699 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1700 if (Primitive::IsFloatingPointType(select->GetType())) {
1701 locations->SetInAt(0, Location::RequiresFpuRegister());
1702 locations->SetInAt(1, Location::RequiresFpuRegister());
1703 } else {
1704 locations->SetInAt(0, Location::RequiresRegister());
1705 locations->SetInAt(1, Location::RequiresRegister());
1706 }
1707 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1708 locations->SetInAt(2, Location::RequiresRegister());
1709 }
1710 locations->SetOut(Location::SameAsFirstInput());
1711}
1712
1713void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
1714 LocationSummary* locations = select->GetLocations();
1715 Label false_target;
1716 GenerateTestAndBranch(select,
1717 /* condition_input_index */ 2,
1718 /* true_target */ nullptr,
1719 &false_target);
1720 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1721 __ Bind(&false_target);
1722}
1723
David Srbecky0cf44932015-12-09 14:09:59 +00001724void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1725 new (GetGraph()->GetArena()) LocationSummary(info);
1726}
1727
David Srbeckyd28f4a02016-03-14 17:14:24 +00001728void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
1729 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001730}
1731
1732void CodeGeneratorARM::GenerateNop() {
1733 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001734}
1735
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001736void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001737 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001738 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001739 // Handle the long/FP comparisons made in instruction simplification.
1740 switch (cond->InputAt(0)->GetType()) {
1741 case Primitive::kPrimLong:
1742 locations->SetInAt(0, Location::RequiresRegister());
1743 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001744 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001745 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
1746 }
1747 break;
1748
1749 case Primitive::kPrimFloat:
1750 case Primitive::kPrimDouble:
1751 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001752 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001753 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001754 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1755 }
1756 break;
1757
1758 default:
1759 locations->SetInAt(0, Location::RequiresRegister());
1760 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001761 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001762 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1763 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001764 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001765}
1766
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001767void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001768 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001769 return;
Dave Allison20dfc792014-06-16 20:44:29 -07001770 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001771
1772 LocationSummary* locations = cond->GetLocations();
1773 Location left = locations->InAt(0);
1774 Location right = locations->InAt(1);
1775 Register out = locations->Out().AsRegister<Register>();
1776 Label true_label, false_label;
1777
1778 switch (cond->InputAt(0)->GetType()) {
1779 default: {
1780 // Integer case.
1781 if (right.IsRegister()) {
1782 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
1783 } else {
1784 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00001785 __ CmpConstant(left.AsRegister<Register>(),
1786 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001787 }
Aart Bike9f37602015-10-09 11:15:55 -07001788 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001789 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07001790 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001791 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07001792 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001793 return;
1794 }
1795 case Primitive::kPrimLong:
1796 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1797 break;
1798 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001799 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001800 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001801 GenerateFPJumps(cond, &true_label, &false_label);
1802 break;
1803 }
1804
1805 // Convert the jumps into the result.
1806 Label done_label;
1807
1808 // False case: result = 0.
1809 __ Bind(&false_label);
1810 __ LoadImmediate(out, 0);
1811 __ b(&done_label);
1812
1813 // True case: result = 1.
1814 __ Bind(&true_label);
1815 __ LoadImmediate(out, 1);
1816 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001817}
1818
1819void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001821}
1822
1823void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001825}
1826
1827void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001829}
1830
1831void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001833}
1834
1835void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001836 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001837}
1838
1839void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001840 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001841}
1842
1843void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001844 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001845}
1846
1847void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001848 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001849}
1850
1851void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001852 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001853}
1854
1855void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001856 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001857}
1858
1859void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001860 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001861}
1862
1863void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001864 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001865}
1866
Aart Bike9f37602015-10-09 11:15:55 -07001867void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001868 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001869}
1870
1871void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001872 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001873}
1874
1875void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001876 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001877}
1878
1879void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001880 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001881}
1882
1883void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001884 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001885}
1886
1887void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001888 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001889}
1890
1891void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001892 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001893}
1894
1895void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001896 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001897}
1898
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001899void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001900 LocationSummary* locations =
1901 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001902 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001903}
1904
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001905void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001906 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001907}
1908
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001909void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
1910 LocationSummary* locations =
1911 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1912 locations->SetOut(Location::ConstantLocation(constant));
1913}
1914
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001915void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001916 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001917}
1918
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001919void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001920 LocationSummary* locations =
1921 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001922 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001923}
1924
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001925void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926 // Will be generated at use site.
1927}
1928
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001929void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
1930 LocationSummary* locations =
1931 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1932 locations->SetOut(Location::ConstantLocation(constant));
1933}
1934
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001935void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001936 // Will be generated at use site.
1937}
1938
1939void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
1940 LocationSummary* locations =
1941 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1942 locations->SetOut(Location::ConstantLocation(constant));
1943}
1944
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001945void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001946 // Will be generated at use site.
1947}
1948
Calin Juravle27df7582015-04-17 19:12:31 +01001949void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1950 memory_barrier->SetLocations(nullptr);
1951}
1952
1953void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00001954 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001955}
1956
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001957void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001958 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001959}
1960
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001961void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001962 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001963}
1964
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001965void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001966 LocationSummary* locations =
1967 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001968 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001969}
1970
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001971void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001972 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001973}
1974
Calin Juravle175dc732015-08-25 15:42:32 +01001975void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1976 // The trampoline uses the same calling convention as dex calling conventions,
1977 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1978 // the method_idx.
1979 HandleInvoke(invoke);
1980}
1981
1982void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1983 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1984}
1985
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001986void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001987 // Explicit clinit checks triggered by static invokes must have been pruned by
1988 // art::PrepareForRegisterAllocation.
1989 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001990
Vladimir Marko68c981f2016-08-26 13:13:33 +01001991 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001992 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001993 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1994 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
1995 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08001996 return;
1997 }
1998
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001999 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002000
2001 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2002 if (invoke->HasPcRelativeDexCache()) {
2003 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2004 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002005}
2006
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002007static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2008 if (invoke->GetLocations()->Intrinsified()) {
2009 IntrinsicCodeGeneratorARM intrinsic(codegen);
2010 intrinsic.Dispatch(invoke);
2011 return true;
2012 }
2013 return false;
2014}
2015
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002016void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002017 // Explicit clinit checks triggered by static invokes must have been pruned by
2018 // art::PrepareForRegisterAllocation.
2019 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002020
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002021 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2022 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002023 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002024
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002025 LocationSummary* locations = invoke->GetLocations();
2026 codegen_->GenerateStaticOrDirectCall(
2027 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002028 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002029}
2030
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002031void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002032 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002033 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002034}
2035
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002036void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002037 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002038 if (intrinsic.TryDispatch(invoke)) {
2039 return;
2040 }
2041
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002042 HandleInvoke(invoke);
2043}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002044
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002045void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002046 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2047 return;
2048 }
2049
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002050 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002051 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002052 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002053}
2054
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002055void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2056 HandleInvoke(invoke);
2057 // Add the hidden argument.
2058 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2059}
2060
2061void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2062 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002063 LocationSummary* locations = invoke->GetLocations();
2064 Register temp = locations->GetTemp(0).AsRegister<Register>();
2065 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002066 Location receiver = locations->InAt(0);
2067 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2068
Roland Levillain3b359c72015-11-17 19:35:12 +00002069 // Set the hidden argument. This is safe to do this here, as R12
2070 // won't be modified thereafter, before the `blx` (call) instruction.
2071 DCHECK_EQ(R12, hidden_reg);
2072 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002073
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002074 if (receiver.IsStackSlot()) {
2075 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002076 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002077 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2078 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002079 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002080 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002081 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002082 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002083 // Instead of simply (possibly) unpoisoning `temp` here, we should
2084 // emit a read barrier for the previous class reference load.
2085 // However this is not required in practice, as this is an
2086 // intermediate/temporary reference and because the current
2087 // concurrent copying collector keeps the from-space memory
2088 // intact/accessible until the end of the marking phase (the
2089 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002090 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002091 __ LoadFromOffset(kLoadWord, temp, temp,
2092 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2093 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002094 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002095 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002096 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002097 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002098 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002099 // LR = temp->GetEntryPoint();
2100 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2101 // LR();
2102 __ blx(LR);
2103 DCHECK(!codegen_->IsLeafMethod());
2104 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2105}
2106
Roland Levillain88cb1752014-10-20 16:36:47 +01002107void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2108 LocationSummary* locations =
2109 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2110 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002111 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002112 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002113 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2114 break;
2115 }
2116 case Primitive::kPrimLong: {
2117 locations->SetInAt(0, Location::RequiresRegister());
2118 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002119 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002120 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002121
Roland Levillain88cb1752014-10-20 16:36:47 +01002122 case Primitive::kPrimFloat:
2123 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002124 locations->SetInAt(0, Location::RequiresFpuRegister());
2125 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002126 break;
2127
2128 default:
2129 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2130 }
2131}
2132
2133void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2134 LocationSummary* locations = neg->GetLocations();
2135 Location out = locations->Out();
2136 Location in = locations->InAt(0);
2137 switch (neg->GetResultType()) {
2138 case Primitive::kPrimInt:
2139 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002140 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002141 break;
2142
2143 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002144 DCHECK(in.IsRegisterPair());
2145 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2146 __ rsbs(out.AsRegisterPairLow<Register>(),
2147 in.AsRegisterPairLow<Register>(),
2148 ShifterOperand(0));
2149 // We cannot emit an RSC (Reverse Subtract with Carry)
2150 // instruction here, as it does not exist in the Thumb-2
2151 // instruction set. We use the following approach
2152 // using SBC and SUB instead.
2153 //
2154 // out.hi = -C
2155 __ sbc(out.AsRegisterPairHigh<Register>(),
2156 out.AsRegisterPairHigh<Register>(),
2157 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2158 // out.hi = out.hi - in.hi
2159 __ sub(out.AsRegisterPairHigh<Register>(),
2160 out.AsRegisterPairHigh<Register>(),
2161 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2162 break;
2163
Roland Levillain88cb1752014-10-20 16:36:47 +01002164 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002165 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002166 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002167 break;
2168
Roland Levillain88cb1752014-10-20 16:36:47 +01002169 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002170 DCHECK(in.IsFpuRegisterPair());
2171 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2172 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002173 break;
2174
2175 default:
2176 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2177 }
2178}
2179
Roland Levillaindff1f282014-11-05 14:15:05 +00002180void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002181 Primitive::Type result_type = conversion->GetResultType();
2182 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002183 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002184
Roland Levillain5b3ee562015-04-14 16:02:41 +01002185 // The float-to-long, double-to-long and long-to-float type conversions
2186 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002187 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002188 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2189 && result_type == Primitive::kPrimLong)
2190 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002191 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002192 : LocationSummary::kNoCall;
2193 LocationSummary* locations =
2194 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2195
David Brazdilb2bd1c52015-03-25 11:17:37 +00002196 // The Java language does not allow treating boolean as an integral type but
2197 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002198
Roland Levillaindff1f282014-11-05 14:15:05 +00002199 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002200 case Primitive::kPrimByte:
2201 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002202 case Primitive::kPrimLong:
2203 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002204 case Primitive::kPrimBoolean:
2205 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002206 case Primitive::kPrimShort:
2207 case Primitive::kPrimInt:
2208 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002209 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002210 locations->SetInAt(0, Location::RequiresRegister());
2211 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2212 break;
2213
2214 default:
2215 LOG(FATAL) << "Unexpected type conversion from " << input_type
2216 << " to " << result_type;
2217 }
2218 break;
2219
Roland Levillain01a8d712014-11-14 16:27:39 +00002220 case Primitive::kPrimShort:
2221 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002222 case Primitive::kPrimLong:
2223 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002224 case Primitive::kPrimBoolean:
2225 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002226 case Primitive::kPrimByte:
2227 case Primitive::kPrimInt:
2228 case Primitive::kPrimChar:
2229 // Processing a Dex `int-to-short' instruction.
2230 locations->SetInAt(0, Location::RequiresRegister());
2231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2232 break;
2233
2234 default:
2235 LOG(FATAL) << "Unexpected type conversion from " << input_type
2236 << " to " << result_type;
2237 }
2238 break;
2239
Roland Levillain946e1432014-11-11 17:35:19 +00002240 case Primitive::kPrimInt:
2241 switch (input_type) {
2242 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002243 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002244 locations->SetInAt(0, Location::Any());
2245 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2246 break;
2247
2248 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002249 // Processing a Dex `float-to-int' instruction.
2250 locations->SetInAt(0, Location::RequiresFpuRegister());
2251 locations->SetOut(Location::RequiresRegister());
2252 locations->AddTemp(Location::RequiresFpuRegister());
2253 break;
2254
Roland Levillain946e1432014-11-11 17:35:19 +00002255 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002256 // Processing a Dex `double-to-int' instruction.
2257 locations->SetInAt(0, Location::RequiresFpuRegister());
2258 locations->SetOut(Location::RequiresRegister());
2259 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002260 break;
2261
2262 default:
2263 LOG(FATAL) << "Unexpected type conversion from " << input_type
2264 << " to " << result_type;
2265 }
2266 break;
2267
Roland Levillaindff1f282014-11-05 14:15:05 +00002268 case Primitive::kPrimLong:
2269 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002270 case Primitive::kPrimBoolean:
2271 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002272 case Primitive::kPrimByte:
2273 case Primitive::kPrimShort:
2274 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002275 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002276 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002277 locations->SetInAt(0, Location::RequiresRegister());
2278 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2279 break;
2280
Roland Levillain624279f2014-12-04 11:54:28 +00002281 case Primitive::kPrimFloat: {
2282 // Processing a Dex `float-to-long' instruction.
2283 InvokeRuntimeCallingConvention calling_convention;
2284 locations->SetInAt(0, Location::FpuRegisterLocation(
2285 calling_convention.GetFpuRegisterAt(0)));
2286 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2287 break;
2288 }
2289
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002290 case Primitive::kPrimDouble: {
2291 // Processing a Dex `double-to-long' instruction.
2292 InvokeRuntimeCallingConvention calling_convention;
2293 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2294 calling_convention.GetFpuRegisterAt(0),
2295 calling_convention.GetFpuRegisterAt(1)));
2296 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002297 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002298 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002299
2300 default:
2301 LOG(FATAL) << "Unexpected type conversion from " << input_type
2302 << " to " << result_type;
2303 }
2304 break;
2305
Roland Levillain981e4542014-11-14 11:47:14 +00002306 case Primitive::kPrimChar:
2307 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002308 case Primitive::kPrimLong:
2309 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002310 case Primitive::kPrimBoolean:
2311 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002312 case Primitive::kPrimByte:
2313 case Primitive::kPrimShort:
2314 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002315 // Processing a Dex `int-to-char' instruction.
2316 locations->SetInAt(0, Location::RequiresRegister());
2317 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2318 break;
2319
2320 default:
2321 LOG(FATAL) << "Unexpected type conversion from " << input_type
2322 << " to " << result_type;
2323 }
2324 break;
2325
Roland Levillaindff1f282014-11-05 14:15:05 +00002326 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002327 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002328 case Primitive::kPrimBoolean:
2329 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002330 case Primitive::kPrimByte:
2331 case Primitive::kPrimShort:
2332 case Primitive::kPrimInt:
2333 case Primitive::kPrimChar:
2334 // Processing a Dex `int-to-float' instruction.
2335 locations->SetInAt(0, Location::RequiresRegister());
2336 locations->SetOut(Location::RequiresFpuRegister());
2337 break;
2338
Roland Levillain5b3ee562015-04-14 16:02:41 +01002339 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002340 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002341 InvokeRuntimeCallingConvention calling_convention;
2342 locations->SetInAt(0, Location::RegisterPairLocation(
2343 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2344 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002345 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002346 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002347
Roland Levillaincff13742014-11-17 14:32:17 +00002348 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002349 // Processing a Dex `double-to-float' instruction.
2350 locations->SetInAt(0, Location::RequiresFpuRegister());
2351 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002352 break;
2353
2354 default:
2355 LOG(FATAL) << "Unexpected type conversion from " << input_type
2356 << " to " << result_type;
2357 };
2358 break;
2359
Roland Levillaindff1f282014-11-05 14:15:05 +00002360 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002361 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002362 case Primitive::kPrimBoolean:
2363 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002364 case Primitive::kPrimByte:
2365 case Primitive::kPrimShort:
2366 case Primitive::kPrimInt:
2367 case Primitive::kPrimChar:
2368 // Processing a Dex `int-to-double' instruction.
2369 locations->SetInAt(0, Location::RequiresRegister());
2370 locations->SetOut(Location::RequiresFpuRegister());
2371 break;
2372
2373 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002374 // Processing a Dex `long-to-double' instruction.
2375 locations->SetInAt(0, Location::RequiresRegister());
2376 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002377 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002378 locations->AddTemp(Location::RequiresFpuRegister());
2379 break;
2380
Roland Levillaincff13742014-11-17 14:32:17 +00002381 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002382 // Processing a Dex `float-to-double' instruction.
2383 locations->SetInAt(0, Location::RequiresFpuRegister());
2384 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002385 break;
2386
2387 default:
2388 LOG(FATAL) << "Unexpected type conversion from " << input_type
2389 << " to " << result_type;
2390 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002391 break;
2392
2393 default:
2394 LOG(FATAL) << "Unexpected type conversion from " << input_type
2395 << " to " << result_type;
2396 }
2397}
2398
2399void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2400 LocationSummary* locations = conversion->GetLocations();
2401 Location out = locations->Out();
2402 Location in = locations->InAt(0);
2403 Primitive::Type result_type = conversion->GetResultType();
2404 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002405 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002406 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002407 case Primitive::kPrimByte:
2408 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002409 case Primitive::kPrimLong:
2410 // Type conversion from long to byte is a result of code transformations.
2411 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2412 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002413 case Primitive::kPrimBoolean:
2414 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002415 case Primitive::kPrimShort:
2416 case Primitive::kPrimInt:
2417 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002418 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002419 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002420 break;
2421
2422 default:
2423 LOG(FATAL) << "Unexpected type conversion from " << input_type
2424 << " to " << result_type;
2425 }
2426 break;
2427
Roland Levillain01a8d712014-11-14 16:27:39 +00002428 case Primitive::kPrimShort:
2429 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002430 case Primitive::kPrimLong:
2431 // Type conversion from long to short is a result of code transformations.
2432 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2433 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002434 case Primitive::kPrimBoolean:
2435 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002436 case Primitive::kPrimByte:
2437 case Primitive::kPrimInt:
2438 case Primitive::kPrimChar:
2439 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002440 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002441 break;
2442
2443 default:
2444 LOG(FATAL) << "Unexpected type conversion from " << input_type
2445 << " to " << result_type;
2446 }
2447 break;
2448
Roland Levillain946e1432014-11-11 17:35:19 +00002449 case Primitive::kPrimInt:
2450 switch (input_type) {
2451 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002452 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002453 DCHECK(out.IsRegister());
2454 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002455 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002456 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002457 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002458 } else {
2459 DCHECK(in.IsConstant());
2460 DCHECK(in.GetConstant()->IsLongConstant());
2461 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002462 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002463 }
2464 break;
2465
Roland Levillain3f8f9362014-12-02 17:45:01 +00002466 case Primitive::kPrimFloat: {
2467 // Processing a Dex `float-to-int' instruction.
2468 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002469 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002470 __ vmovrs(out.AsRegister<Register>(), temp);
2471 break;
2472 }
2473
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002474 case Primitive::kPrimDouble: {
2475 // Processing a Dex `double-to-int' instruction.
2476 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002477 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002478 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002479 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002480 }
Roland Levillain946e1432014-11-11 17:35:19 +00002481
2482 default:
2483 LOG(FATAL) << "Unexpected type conversion from " << input_type
2484 << " to " << result_type;
2485 }
2486 break;
2487
Roland Levillaindff1f282014-11-05 14:15:05 +00002488 case Primitive::kPrimLong:
2489 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002490 case Primitive::kPrimBoolean:
2491 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002492 case Primitive::kPrimByte:
2493 case Primitive::kPrimShort:
2494 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002495 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002496 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002497 DCHECK(out.IsRegisterPair());
2498 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002499 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00002500 // Sign extension.
2501 __ Asr(out.AsRegisterPairHigh<Register>(),
2502 out.AsRegisterPairLow<Register>(),
2503 31);
2504 break;
2505
2506 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002507 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002508 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002509 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002510 break;
2511
Roland Levillaindff1f282014-11-05 14:15:05 +00002512 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002513 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002514 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002515 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002516 break;
2517
2518 default:
2519 LOG(FATAL) << "Unexpected type conversion from " << input_type
2520 << " to " << result_type;
2521 }
2522 break;
2523
Roland Levillain981e4542014-11-14 11:47:14 +00002524 case Primitive::kPrimChar:
2525 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002526 case Primitive::kPrimLong:
2527 // Type conversion from long to char is a result of code transformations.
2528 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2529 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002530 case Primitive::kPrimBoolean:
2531 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002532 case Primitive::kPrimByte:
2533 case Primitive::kPrimShort:
2534 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002535 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002536 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00002537 break;
2538
2539 default:
2540 LOG(FATAL) << "Unexpected type conversion from " << input_type
2541 << " to " << result_type;
2542 }
2543 break;
2544
Roland Levillaindff1f282014-11-05 14:15:05 +00002545 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002546 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002547 case Primitive::kPrimBoolean:
2548 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002549 case Primitive::kPrimByte:
2550 case Primitive::kPrimShort:
2551 case Primitive::kPrimInt:
2552 case Primitive::kPrimChar: {
2553 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002554 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
2555 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002556 break;
2557 }
2558
Roland Levillain5b3ee562015-04-14 16:02:41 +01002559 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002560 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002561 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002562 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00002563 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002564
Roland Levillaincff13742014-11-17 14:32:17 +00002565 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002566 // Processing a Dex `double-to-float' instruction.
2567 __ vcvtsd(out.AsFpuRegister<SRegister>(),
2568 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00002569 break;
2570
2571 default:
2572 LOG(FATAL) << "Unexpected type conversion from " << input_type
2573 << " to " << result_type;
2574 };
2575 break;
2576
Roland Levillaindff1f282014-11-05 14:15:05 +00002577 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002578 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002579 case Primitive::kPrimBoolean:
2580 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002581 case Primitive::kPrimByte:
2582 case Primitive::kPrimShort:
2583 case Primitive::kPrimInt:
2584 case Primitive::kPrimChar: {
2585 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002586 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002587 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2588 out.AsFpuRegisterPairLow<SRegister>());
2589 break;
2590 }
2591
Roland Levillain647b9ed2014-11-27 12:06:00 +00002592 case Primitive::kPrimLong: {
2593 // Processing a Dex `long-to-double' instruction.
2594 Register low = in.AsRegisterPairLow<Register>();
2595 Register high = in.AsRegisterPairHigh<Register>();
2596 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
2597 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002598 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00002599 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01002600 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
2601 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002602
Roland Levillain682393c2015-04-14 15:57:52 +01002603 // temp_d = int-to-double(high)
2604 __ vmovsr(temp_s, high);
2605 __ vcvtdi(temp_d, temp_s);
2606 // constant_d = k2Pow32EncodingForDouble
2607 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
2608 // out_d = unsigned-to-double(low)
2609 __ vmovsr(out_s, low);
2610 __ vcvtdu(out_d, out_s);
2611 // out_d += temp_d * constant_d
2612 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00002613 break;
2614 }
2615
Roland Levillaincff13742014-11-17 14:32:17 +00002616 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002617 // Processing a Dex `float-to-double' instruction.
2618 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2619 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002620 break;
2621
2622 default:
2623 LOG(FATAL) << "Unexpected type conversion from " << input_type
2624 << " to " << result_type;
2625 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002626 break;
2627
2628 default:
2629 LOG(FATAL) << "Unexpected type conversion from " << input_type
2630 << " to " << result_type;
2631 }
2632}
2633
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002634void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002635 LocationSummary* locations =
2636 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002637 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002638 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002639 locations->SetInAt(0, Location::RequiresRegister());
2640 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002641 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2642 break;
2643 }
2644
2645 case Primitive::kPrimLong: {
2646 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002647 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002648 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002649 break;
2650 }
2651
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002652 case Primitive::kPrimFloat:
2653 case Primitive::kPrimDouble: {
2654 locations->SetInAt(0, Location::RequiresFpuRegister());
2655 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002656 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002657 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002658 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002659
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002660 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002661 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002662 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002663}
2664
2665void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
2666 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002667 Location out = locations->Out();
2668 Location first = locations->InAt(0);
2669 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002670 switch (add->GetResultType()) {
2671 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002672 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002673 __ add(out.AsRegister<Register>(),
2674 first.AsRegister<Register>(),
2675 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002676 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002677 __ AddConstant(out.AsRegister<Register>(),
2678 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002679 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002680 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002681 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002682
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002683 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002684 if (second.IsConstant()) {
2685 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2686 GenerateAddLongConst(out, first, value);
2687 } else {
2688 DCHECK(second.IsRegisterPair());
2689 __ adds(out.AsRegisterPairLow<Register>(),
2690 first.AsRegisterPairLow<Register>(),
2691 ShifterOperand(second.AsRegisterPairLow<Register>()));
2692 __ adc(out.AsRegisterPairHigh<Register>(),
2693 first.AsRegisterPairHigh<Register>(),
2694 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2695 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002696 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002697 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002698
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002699 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00002700 __ vadds(out.AsFpuRegister<SRegister>(),
2701 first.AsFpuRegister<SRegister>(),
2702 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002703 break;
2704
2705 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002706 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2707 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2708 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002709 break;
2710
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002711 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002712 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002713 }
2714}
2715
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002716void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002717 LocationSummary* locations =
2718 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002719 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002720 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002721 locations->SetInAt(0, Location::RequiresRegister());
2722 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002723 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2724 break;
2725 }
2726
2727 case Primitive::kPrimLong: {
2728 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01002729 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002730 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002731 break;
2732 }
Calin Juravle11351682014-10-23 15:38:15 +01002733 case Primitive::kPrimFloat:
2734 case Primitive::kPrimDouble: {
2735 locations->SetInAt(0, Location::RequiresFpuRegister());
2736 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002737 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002738 break;
Calin Juravle11351682014-10-23 15:38:15 +01002739 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002740 default:
Calin Juravle11351682014-10-23 15:38:15 +01002741 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002742 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002743}
2744
2745void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
2746 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01002747 Location out = locations->Out();
2748 Location first = locations->InAt(0);
2749 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002750 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002751 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01002752 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002753 __ sub(out.AsRegister<Register>(),
2754 first.AsRegister<Register>(),
2755 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002756 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002757 __ AddConstant(out.AsRegister<Register>(),
2758 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01002759 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002760 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002761 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002762 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002763
Calin Juravle11351682014-10-23 15:38:15 +01002764 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01002765 if (second.IsConstant()) {
2766 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
2767 GenerateAddLongConst(out, first, -value);
2768 } else {
2769 DCHECK(second.IsRegisterPair());
2770 __ subs(out.AsRegisterPairLow<Register>(),
2771 first.AsRegisterPairLow<Register>(),
2772 ShifterOperand(second.AsRegisterPairLow<Register>()));
2773 __ sbc(out.AsRegisterPairHigh<Register>(),
2774 first.AsRegisterPairHigh<Register>(),
2775 ShifterOperand(second.AsRegisterPairHigh<Register>()));
2776 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002777 break;
Calin Juravle11351682014-10-23 15:38:15 +01002778 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002779
Calin Juravle11351682014-10-23 15:38:15 +01002780 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002781 __ vsubs(out.AsFpuRegister<SRegister>(),
2782 first.AsFpuRegister<SRegister>(),
2783 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002784 break;
Calin Juravle11351682014-10-23 15:38:15 +01002785 }
2786
2787 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002788 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2789 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2790 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01002791 break;
2792 }
2793
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002794
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002795 default:
Calin Juravle11351682014-10-23 15:38:15 +01002796 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002797 }
2798}
2799
Calin Juravle34bacdf2014-10-07 20:23:36 +01002800void LocationsBuilderARM::VisitMul(HMul* mul) {
2801 LocationSummary* locations =
2802 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2803 switch (mul->GetResultType()) {
2804 case Primitive::kPrimInt:
2805 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01002806 locations->SetInAt(0, Location::RequiresRegister());
2807 locations->SetInAt(1, Location::RequiresRegister());
2808 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002809 break;
2810 }
2811
Calin Juravleb5bfa962014-10-21 18:02:24 +01002812 case Primitive::kPrimFloat:
2813 case Primitive::kPrimDouble: {
2814 locations->SetInAt(0, Location::RequiresFpuRegister());
2815 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00002816 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01002817 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002818 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002819
2820 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002821 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002822 }
2823}
2824
2825void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
2826 LocationSummary* locations = mul->GetLocations();
2827 Location out = locations->Out();
2828 Location first = locations->InAt(0);
2829 Location second = locations->InAt(1);
2830 switch (mul->GetResultType()) {
2831 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00002832 __ mul(out.AsRegister<Register>(),
2833 first.AsRegister<Register>(),
2834 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002835 break;
2836 }
2837 case Primitive::kPrimLong: {
2838 Register out_hi = out.AsRegisterPairHigh<Register>();
2839 Register out_lo = out.AsRegisterPairLow<Register>();
2840 Register in1_hi = first.AsRegisterPairHigh<Register>();
2841 Register in1_lo = first.AsRegisterPairLow<Register>();
2842 Register in2_hi = second.AsRegisterPairHigh<Register>();
2843 Register in2_lo = second.AsRegisterPairLow<Register>();
2844
2845 // Extra checks to protect caused by the existence of R1_R2.
2846 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
2847 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
2848 DCHECK_NE(out_hi, in1_lo);
2849 DCHECK_NE(out_hi, in2_lo);
2850
2851 // input: in1 - 64 bits, in2 - 64 bits
2852 // output: out
2853 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2854 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2855 // parts: out.lo = (in1.lo * in2.lo)[31:0]
2856
2857 // IP <- in1.lo * in2.hi
2858 __ mul(IP, in1_lo, in2_hi);
2859 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2860 __ mla(out_hi, in1_hi, in2_lo, IP);
2861 // out.lo <- (in1.lo * in2.lo)[31:0];
2862 __ umull(out_lo, IP, in1_lo, in2_lo);
2863 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2864 __ add(out_hi, out_hi, ShifterOperand(IP));
2865 break;
2866 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002867
2868 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00002869 __ vmuls(out.AsFpuRegister<SRegister>(),
2870 first.AsFpuRegister<SRegister>(),
2871 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002872 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002873 }
2874
2875 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002876 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2877 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
2878 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01002879 break;
2880 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002881
2882 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002883 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002884 }
2885}
2886
Zheng Xuc6667102015-05-15 16:08:45 +08002887void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2888 DCHECK(instruction->IsDiv() || instruction->IsRem());
2889 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2890
2891 LocationSummary* locations = instruction->GetLocations();
2892 Location second = locations->InAt(1);
2893 DCHECK(second.IsConstant());
2894
2895 Register out = locations->Out().AsRegister<Register>();
2896 Register dividend = locations->InAt(0).AsRegister<Register>();
2897 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2898 DCHECK(imm == 1 || imm == -1);
2899
2900 if (instruction->IsRem()) {
2901 __ LoadImmediate(out, 0);
2902 } else {
2903 if (imm == 1) {
2904 __ Mov(out, dividend);
2905 } else {
2906 __ rsb(out, dividend, ShifterOperand(0));
2907 }
2908 }
2909}
2910
2911void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
2912 DCHECK(instruction->IsDiv() || instruction->IsRem());
2913 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2914
2915 LocationSummary* locations = instruction->GetLocations();
2916 Location second = locations->InAt(1);
2917 DCHECK(second.IsConstant());
2918
2919 Register out = locations->Out().AsRegister<Register>();
2920 Register dividend = locations->InAt(0).AsRegister<Register>();
2921 Register temp = locations->GetTemp(0).AsRegister<Register>();
2922 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002923 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08002924 int ctz_imm = CTZ(abs_imm);
2925
2926 if (ctz_imm == 1) {
2927 __ Lsr(temp, dividend, 32 - ctz_imm);
2928 } else {
2929 __ Asr(temp, dividend, 31);
2930 __ Lsr(temp, temp, 32 - ctz_imm);
2931 }
2932 __ add(out, temp, ShifterOperand(dividend));
2933
2934 if (instruction->IsDiv()) {
2935 __ Asr(out, out, ctz_imm);
2936 if (imm < 0) {
2937 __ rsb(out, out, ShifterOperand(0));
2938 }
2939 } else {
2940 __ ubfx(out, out, 0, ctz_imm);
2941 __ sub(out, out, ShifterOperand(temp));
2942 }
2943}
2944
2945void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2946 DCHECK(instruction->IsDiv() || instruction->IsRem());
2947 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2948
2949 LocationSummary* locations = instruction->GetLocations();
2950 Location second = locations->InAt(1);
2951 DCHECK(second.IsConstant());
2952
2953 Register out = locations->Out().AsRegister<Register>();
2954 Register dividend = locations->InAt(0).AsRegister<Register>();
2955 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2956 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
2957 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2958
2959 int64_t magic;
2960 int shift;
2961 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2962
2963 __ LoadImmediate(temp1, magic);
2964 __ smull(temp2, temp1, dividend, temp1);
2965
2966 if (imm > 0 && magic < 0) {
2967 __ add(temp1, temp1, ShifterOperand(dividend));
2968 } else if (imm < 0 && magic > 0) {
2969 __ sub(temp1, temp1, ShifterOperand(dividend));
2970 }
2971
2972 if (shift != 0) {
2973 __ Asr(temp1, temp1, shift);
2974 }
2975
2976 if (instruction->IsDiv()) {
2977 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
2978 } else {
2979 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
2980 // TODO: Strength reduction for mls.
2981 __ LoadImmediate(temp2, imm);
2982 __ mls(out, temp1, temp2, dividend);
2983 }
2984}
2985
2986void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
2987 DCHECK(instruction->IsDiv() || instruction->IsRem());
2988 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
2989
2990 LocationSummary* locations = instruction->GetLocations();
2991 Location second = locations->InAt(1);
2992 DCHECK(second.IsConstant());
2993
2994 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
2995 if (imm == 0) {
2996 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
2997 } else if (imm == 1 || imm == -1) {
2998 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00002999 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003000 DivRemByPowerOfTwo(instruction);
3001 } else {
3002 DCHECK(imm <= -2 || imm >= 2);
3003 GenerateDivRemWithAnyConstant(instruction);
3004 }
3005}
3006
Calin Juravle7c4954d2014-10-28 16:57:40 +00003007void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003008 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3009 if (div->GetResultType() == Primitive::kPrimLong) {
3010 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003011 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003012 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3013 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003014 } else if (div->GetResultType() == Primitive::kPrimInt &&
3015 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3016 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003017 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003018 }
3019
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003020 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3021
Calin Juravle7c4954d2014-10-28 16:57:40 +00003022 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003023 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003024 if (div->InputAt(1)->IsConstant()) {
3025 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003026 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003027 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003028 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3029 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003030 // No temp register required.
3031 } else {
3032 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003033 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003034 locations->AddTemp(Location::RequiresRegister());
3035 }
3036 }
3037 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003038 locations->SetInAt(0, Location::RequiresRegister());
3039 locations->SetInAt(1, Location::RequiresRegister());
3040 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3041 } else {
3042 InvokeRuntimeCallingConvention calling_convention;
3043 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3044 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3045 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3046 // we only need the former.
3047 locations->SetOut(Location::RegisterLocation(R0));
3048 }
Calin Juravled0d48522014-11-04 16:40:20 +00003049 break;
3050 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003051 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003052 InvokeRuntimeCallingConvention calling_convention;
3053 locations->SetInAt(0, Location::RegisterPairLocation(
3054 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3055 locations->SetInAt(1, Location::RegisterPairLocation(
3056 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003057 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003058 break;
3059 }
3060 case Primitive::kPrimFloat:
3061 case Primitive::kPrimDouble: {
3062 locations->SetInAt(0, Location::RequiresFpuRegister());
3063 locations->SetInAt(1, Location::RequiresFpuRegister());
3064 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3065 break;
3066 }
3067
3068 default:
3069 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3070 }
3071}
3072
3073void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3074 LocationSummary* locations = div->GetLocations();
3075 Location out = locations->Out();
3076 Location first = locations->InAt(0);
3077 Location second = locations->InAt(1);
3078
3079 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003080 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003081 if (second.IsConstant()) {
3082 GenerateDivRemConstantIntegral(div);
3083 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003084 __ sdiv(out.AsRegister<Register>(),
3085 first.AsRegister<Register>(),
3086 second.AsRegister<Register>());
3087 } else {
3088 InvokeRuntimeCallingConvention calling_convention;
3089 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3090 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3091 DCHECK_EQ(R0, out.AsRegister<Register>());
3092
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003093 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003094 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003095 }
Calin Juravled0d48522014-11-04 16:40:20 +00003096 break;
3097 }
3098
Calin Juravle7c4954d2014-10-28 16:57:40 +00003099 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003100 InvokeRuntimeCallingConvention calling_convention;
3101 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3102 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3103 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3104 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3105 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003106 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003107
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003108 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003109 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003110 break;
3111 }
3112
3113 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003114 __ vdivs(out.AsFpuRegister<SRegister>(),
3115 first.AsFpuRegister<SRegister>(),
3116 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003117 break;
3118 }
3119
3120 case Primitive::kPrimDouble: {
3121 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3122 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3123 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3124 break;
3125 }
3126
3127 default:
3128 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3129 }
3130}
3131
Calin Juravlebacfec32014-11-14 15:54:36 +00003132void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003133 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003134
3135 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003136 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003137 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3138 // sdiv will be replaced by other instruction sequence.
3139 call_kind = LocationSummary::kNoCall;
3140 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3141 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003142 // Have hardware divide instruction for int, do it with three instructions.
3143 call_kind = LocationSummary::kNoCall;
3144 }
3145
Calin Juravlebacfec32014-11-14 15:54:36 +00003146 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3147
Calin Juravled2ec87d2014-12-08 14:24:46 +00003148 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003149 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003150 if (rem->InputAt(1)->IsConstant()) {
3151 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003152 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003153 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003154 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3155 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003156 // No temp register required.
3157 } else {
3158 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003159 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003160 locations->AddTemp(Location::RequiresRegister());
3161 }
3162 }
3163 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003164 locations->SetInAt(0, Location::RequiresRegister());
3165 locations->SetInAt(1, Location::RequiresRegister());
3166 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3167 locations->AddTemp(Location::RequiresRegister());
3168 } else {
3169 InvokeRuntimeCallingConvention calling_convention;
3170 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3171 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3172 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but
3173 // we only need the latter.
3174 locations->SetOut(Location::RegisterLocation(R1));
3175 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003176 break;
3177 }
3178 case Primitive::kPrimLong: {
3179 InvokeRuntimeCallingConvention calling_convention;
3180 locations->SetInAt(0, Location::RegisterPairLocation(
3181 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3182 locations->SetInAt(1, Location::RegisterPairLocation(
3183 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3184 // The runtime helper puts the output in R2,R3.
3185 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3186 break;
3187 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003188 case Primitive::kPrimFloat: {
3189 InvokeRuntimeCallingConvention calling_convention;
3190 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3191 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3192 locations->SetOut(Location::FpuRegisterLocation(S0));
3193 break;
3194 }
3195
Calin Juravlebacfec32014-11-14 15:54:36 +00003196 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003197 InvokeRuntimeCallingConvention calling_convention;
3198 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3199 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3200 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3201 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3202 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003203 break;
3204 }
3205
3206 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003207 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003208 }
3209}
3210
3211void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3212 LocationSummary* locations = rem->GetLocations();
3213 Location out = locations->Out();
3214 Location first = locations->InAt(0);
3215 Location second = locations->InAt(1);
3216
Calin Juravled2ec87d2014-12-08 14:24:46 +00003217 Primitive::Type type = rem->GetResultType();
3218 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003219 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003220 if (second.IsConstant()) {
3221 GenerateDivRemConstantIntegral(rem);
3222 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003223 Register reg1 = first.AsRegister<Register>();
3224 Register reg2 = second.AsRegister<Register>();
3225 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003226
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003227 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003228 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003229 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003230 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003231 } else {
3232 InvokeRuntimeCallingConvention calling_convention;
3233 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3234 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3235 DCHECK_EQ(R1, out.AsRegister<Register>());
3236
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003237 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003238 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003239 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003240 break;
3241 }
3242
3243 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003244 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003245 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003246 break;
3247 }
3248
Calin Juravled2ec87d2014-12-08 14:24:46 +00003249 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003250 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003251 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003252 break;
3253 }
3254
Calin Juravlebacfec32014-11-14 15:54:36 +00003255 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003256 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003257 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003258 break;
3259 }
3260
3261 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003262 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003263 }
3264}
3265
Calin Juravled0d48522014-11-04 16:40:20 +00003266void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003267 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3268 ? LocationSummary::kCallOnSlowPath
3269 : LocationSummary::kNoCall;
3270 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003271 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003272 if (instruction->HasUses()) {
3273 locations->SetOut(Location::SameAsFirstInput());
3274 }
3275}
3276
3277void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003278 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003279 codegen_->AddSlowPath(slow_path);
3280
3281 LocationSummary* locations = instruction->GetLocations();
3282 Location value = locations->InAt(0);
3283
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003284 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003285 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003286 case Primitive::kPrimByte:
3287 case Primitive::kPrimChar:
3288 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003289 case Primitive::kPrimInt: {
3290 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003291 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003292 } else {
3293 DCHECK(value.IsConstant()) << value;
3294 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3295 __ b(slow_path->GetEntryLabel());
3296 }
3297 }
3298 break;
3299 }
3300 case Primitive::kPrimLong: {
3301 if (value.IsRegisterPair()) {
3302 __ orrs(IP,
3303 value.AsRegisterPairLow<Register>(),
3304 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3305 __ b(slow_path->GetEntryLabel(), EQ);
3306 } else {
3307 DCHECK(value.IsConstant()) << value;
3308 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3309 __ b(slow_path->GetEntryLabel());
3310 }
3311 }
3312 break;
3313 default:
3314 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3315 }
3316 }
Calin Juravled0d48522014-11-04 16:40:20 +00003317}
3318
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003319void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3320 Register in = locations->InAt(0).AsRegister<Register>();
3321 Location rhs = locations->InAt(1);
3322 Register out = locations->Out().AsRegister<Register>();
3323
3324 if (rhs.IsConstant()) {
3325 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3326 // so map all rotations to a +ve. equivalent in that range.
3327 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3328 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3329 if (rot) {
3330 // Rotate, mapping left rotations to right equivalents if necessary.
3331 // (e.g. left by 2 bits == right by 30.)
3332 __ Ror(out, in, rot);
3333 } else if (out != in) {
3334 __ Mov(out, in);
3335 }
3336 } else {
3337 __ Ror(out, in, rhs.AsRegister<Register>());
3338 }
3339}
3340
3341// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3342// rotates by swapping input regs (effectively rotating by the first 32-bits of
3343// a larger rotation) or flipping direction (thus treating larger right/left
3344// rotations as sub-word sized rotations in the other direction) as appropriate.
3345void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3346 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3347 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3348 Location rhs = locations->InAt(1);
3349 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3350 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3351
3352 if (rhs.IsConstant()) {
3353 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3354 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003355 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003356 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3357 // logic below to a simple pair of binary orr.
3358 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3359 if (rot >= kArmBitsPerWord) {
3360 rot -= kArmBitsPerWord;
3361 std::swap(in_reg_hi, in_reg_lo);
3362 }
3363 // Rotate, or mov to out for zero or word size rotations.
3364 if (rot != 0u) {
3365 __ Lsr(out_reg_hi, in_reg_hi, rot);
3366 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3367 __ Lsr(out_reg_lo, in_reg_lo, rot);
3368 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3369 } else {
3370 __ Mov(out_reg_lo, in_reg_lo);
3371 __ Mov(out_reg_hi, in_reg_hi);
3372 }
3373 } else {
3374 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3375 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3376 Label end;
3377 Label shift_by_32_plus_shift_right;
3378
3379 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3380 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3381 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3382 __ b(&shift_by_32_plus_shift_right, CC);
3383
3384 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3385 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3386 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3387 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3388 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3389 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3390 __ Lsr(shift_left, in_reg_hi, shift_right);
3391 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3392 __ b(&end);
3393
3394 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3395 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3396 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3397 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3398 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3399 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3400 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3401 __ Lsl(shift_right, in_reg_hi, shift_left);
3402 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3403
3404 __ Bind(&end);
3405 }
3406}
Roland Levillain22c49222016-03-18 14:04:28 +00003407
3408void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003409 LocationSummary* locations =
3410 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3411 switch (ror->GetResultType()) {
3412 case Primitive::kPrimInt: {
3413 locations->SetInAt(0, Location::RequiresRegister());
3414 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3415 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3416 break;
3417 }
3418 case Primitive::kPrimLong: {
3419 locations->SetInAt(0, Location::RequiresRegister());
3420 if (ror->InputAt(1)->IsConstant()) {
3421 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3422 } else {
3423 locations->SetInAt(1, Location::RequiresRegister());
3424 locations->AddTemp(Location::RequiresRegister());
3425 locations->AddTemp(Location::RequiresRegister());
3426 }
3427 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3428 break;
3429 }
3430 default:
3431 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3432 }
3433}
3434
Roland Levillain22c49222016-03-18 14:04:28 +00003435void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003436 LocationSummary* locations = ror->GetLocations();
3437 Primitive::Type type = ror->GetResultType();
3438 switch (type) {
3439 case Primitive::kPrimInt: {
3440 HandleIntegerRotate(locations);
3441 break;
3442 }
3443 case Primitive::kPrimLong: {
3444 HandleLongRotate(locations);
3445 break;
3446 }
3447 default:
3448 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003449 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003450 }
3451}
3452
Calin Juravle9aec02f2014-11-18 23:06:35 +00003453void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3454 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3455
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003456 LocationSummary* locations =
3457 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003458
3459 switch (op->GetResultType()) {
3460 case Primitive::kPrimInt: {
3461 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003462 if (op->InputAt(1)->IsConstant()) {
3463 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3464 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3465 } else {
3466 locations->SetInAt(1, Location::RequiresRegister());
3467 // Make the output overlap, as it will be used to hold the masked
3468 // second input.
3469 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3470 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003471 break;
3472 }
3473 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003474 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003475 if (op->InputAt(1)->IsConstant()) {
3476 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3477 // For simplicity, use kOutputOverlap even though we only require that low registers
3478 // don't clash with high registers which the register allocator currently guarantees.
3479 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3480 } else {
3481 locations->SetInAt(1, Location::RequiresRegister());
3482 locations->AddTemp(Location::RequiresRegister());
3483 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3484 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003485 break;
3486 }
3487 default:
3488 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3489 }
3490}
3491
3492void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3493 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3494
3495 LocationSummary* locations = op->GetLocations();
3496 Location out = locations->Out();
3497 Location first = locations->InAt(0);
3498 Location second = locations->InAt(1);
3499
3500 Primitive::Type type = op->GetResultType();
3501 switch (type) {
3502 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003503 Register out_reg = out.AsRegister<Register>();
3504 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003505 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003506 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00003507 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003508 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003509 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003510 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003511 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003512 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003513 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01003514 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003515 }
3516 } else {
3517 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003518 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00003519 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00003520 __ Mov(out_reg, first_reg);
3521 } else if (op->IsShl()) {
3522 __ Lsl(out_reg, first_reg, shift_value);
3523 } else if (op->IsShr()) {
3524 __ Asr(out_reg, first_reg, shift_value);
3525 } else {
3526 __ Lsr(out_reg, first_reg, shift_value);
3527 }
3528 }
3529 break;
3530 }
3531 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003532 Register o_h = out.AsRegisterPairHigh<Register>();
3533 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003534
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003535 Register high = first.AsRegisterPairHigh<Register>();
3536 Register low = first.AsRegisterPairLow<Register>();
3537
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003538 if (second.IsRegister()) {
3539 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003540
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003541 Register second_reg = second.AsRegister<Register>();
3542
3543 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003544 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003545 // Shift the high part
3546 __ Lsl(o_h, high, o_l);
3547 // Shift the low part and `or` what overflew on the high part
3548 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
3549 __ Lsr(temp, low, temp);
3550 __ orr(o_h, o_h, ShifterOperand(temp));
3551 // If the shift is > 32 bits, override the high part
3552 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
3553 __ it(PL);
3554 __ Lsl(o_h, low, temp, PL);
3555 // Shift the low part
3556 __ Lsl(o_l, low, o_l);
3557 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003558 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003559 // Shift the low part
3560 __ Lsr(o_l, low, o_h);
3561 // Shift the high part and `or` what underflew on the low part
3562 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3563 __ Lsl(temp, high, temp);
3564 __ orr(o_l, o_l, ShifterOperand(temp));
3565 // If the shift is > 32 bits, override the low part
3566 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3567 __ it(PL);
3568 __ Asr(o_l, high, temp, PL);
3569 // Shift the high part
3570 __ Asr(o_h, high, o_h);
3571 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003572 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003573 // same as Shr except we use `Lsr`s and not `Asr`s
3574 __ Lsr(o_l, low, o_h);
3575 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
3576 __ Lsl(temp, high, temp);
3577 __ orr(o_l, o_l, ShifterOperand(temp));
3578 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
3579 __ it(PL);
3580 __ Lsr(o_l, high, temp, PL);
3581 __ Lsr(o_h, high, o_h);
3582 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003583 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003584 // Register allocator doesn't create partial overlap.
3585 DCHECK_NE(o_l, high);
3586 DCHECK_NE(o_h, low);
3587 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00003588 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003589 if (shift_value > 32) {
3590 if (op->IsShl()) {
3591 __ Lsl(o_h, low, shift_value - 32);
3592 __ LoadImmediate(o_l, 0);
3593 } else if (op->IsShr()) {
3594 __ Asr(o_l, high, shift_value - 32);
3595 __ Asr(o_h, high, 31);
3596 } else {
3597 __ Lsr(o_l, high, shift_value - 32);
3598 __ LoadImmediate(o_h, 0);
3599 }
3600 } else if (shift_value == 32) {
3601 if (op->IsShl()) {
3602 __ mov(o_h, ShifterOperand(low));
3603 __ LoadImmediate(o_l, 0);
3604 } else if (op->IsShr()) {
3605 __ mov(o_l, ShifterOperand(high));
3606 __ Asr(o_h, high, 31);
3607 } else {
3608 __ mov(o_l, ShifterOperand(high));
3609 __ LoadImmediate(o_h, 0);
3610 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00003611 } else if (shift_value == 1) {
3612 if (op->IsShl()) {
3613 __ Lsls(o_l, low, 1);
3614 __ adc(o_h, high, ShifterOperand(high));
3615 } else if (op->IsShr()) {
3616 __ Asrs(o_h, high, 1);
3617 __ Rrx(o_l, low);
3618 } else {
3619 __ Lsrs(o_h, high, 1);
3620 __ Rrx(o_l, low);
3621 }
3622 } else {
3623 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003624 if (op->IsShl()) {
3625 __ Lsl(o_h, high, shift_value);
3626 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
3627 __ Lsl(o_l, low, shift_value);
3628 } else if (op->IsShr()) {
3629 __ Lsr(o_l, low, shift_value);
3630 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3631 __ Asr(o_h, high, shift_value);
3632 } else {
3633 __ Lsr(o_l, low, shift_value);
3634 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
3635 __ Lsr(o_h, high, shift_value);
3636 }
3637 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003638 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003639 break;
3640 }
3641 default:
3642 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003643 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003644 }
3645}
3646
3647void LocationsBuilderARM::VisitShl(HShl* shl) {
3648 HandleShift(shl);
3649}
3650
3651void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
3652 HandleShift(shl);
3653}
3654
3655void LocationsBuilderARM::VisitShr(HShr* shr) {
3656 HandleShift(shr);
3657}
3658
3659void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
3660 HandleShift(shr);
3661}
3662
3663void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
3664 HandleShift(ushr);
3665}
3666
3667void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
3668 HandleShift(ushr);
3669}
3670
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003671void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003672 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003673 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00003674 if (instruction->IsStringAlloc()) {
3675 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3676 } else {
3677 InvokeRuntimeCallingConvention calling_convention;
3678 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3679 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3680 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003681 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003682}
3683
3684void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003685 // Note: if heap poisoning is enabled, the entry point takes cares
3686 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003687 if (instruction->IsStringAlloc()) {
3688 // String is allocated through StringFactory. Call NewEmptyString entry point.
3689 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07003690 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00003691 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
3692 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
3693 __ blx(LR);
3694 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3695 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003696 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00003697 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3698 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003699}
3700
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003701void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
3702 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003703 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003704 InvokeRuntimeCallingConvention calling_convention;
3705 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003706 locations->SetOut(Location::RegisterLocation(R0));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003707 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003708 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003709}
3710
3711void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
3712 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003713 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex());
Roland Levillain4d027112015-07-01 15:41:14 +01003714 // Note: if heap poisoning is enabled, the entry point takes cares
3715 // of poisoning the reference.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003716 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003717 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003718}
3719
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003720void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003721 LocationSummary* locations =
3722 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003723 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3724 if (location.IsStackSlot()) {
3725 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3726 } else if (location.IsDoubleStackSlot()) {
3727 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003728 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003729 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003730}
3731
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003732void InstructionCodeGeneratorARM::VisitParameterValue(
3733 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003734 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003735}
3736
3737void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
3738 LocationSummary* locations =
3739 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3740 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3741}
3742
3743void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
3744 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003745}
3746
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003747void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003748 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003749 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003750 locations->SetInAt(0, Location::RequiresRegister());
3751 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003752}
3753
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003754void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
3755 LocationSummary* locations = not_->GetLocations();
3756 Location out = locations->Out();
3757 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003758 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003759 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003760 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003761 break;
3762
3763 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003764 __ mvn(out.AsRegisterPairLow<Register>(),
3765 ShifterOperand(in.AsRegisterPairLow<Register>()));
3766 __ mvn(out.AsRegisterPairHigh<Register>(),
3767 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003768 break;
3769
3770 default:
3771 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3772 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003773}
3774
David Brazdil66d126e2015-04-03 16:02:44 +01003775void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
3776 LocationSummary* locations =
3777 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3778 locations->SetInAt(0, Location::RequiresRegister());
3779 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3780}
3781
3782void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003783 LocationSummary* locations = bool_not->GetLocations();
3784 Location out = locations->Out();
3785 Location in = locations->InAt(0);
3786 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
3787}
3788
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003789void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003790 LocationSummary* locations =
3791 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003792 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003793 case Primitive::kPrimBoolean:
3794 case Primitive::kPrimByte:
3795 case Primitive::kPrimShort:
3796 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003797 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00003798 case Primitive::kPrimLong: {
3799 locations->SetInAt(0, Location::RequiresRegister());
3800 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003801 // Output overlaps because it is written before doing the low comparison.
3802 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00003803 break;
3804 }
3805 case Primitive::kPrimFloat:
3806 case Primitive::kPrimDouble: {
3807 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003808 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00003809 locations->SetOut(Location::RequiresRegister());
3810 break;
3811 }
3812 default:
3813 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3814 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003815}
3816
3817void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003818 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003819 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003820 Location left = locations->InAt(0);
3821 Location right = locations->InAt(1);
3822
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003823 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00003824 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00003825 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00003826 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003827 case Primitive::kPrimBoolean:
3828 case Primitive::kPrimByte:
3829 case Primitive::kPrimShort:
3830 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003831 case Primitive::kPrimInt: {
3832 __ LoadImmediate(out, 0);
3833 __ cmp(left.AsRegister<Register>(),
3834 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
3835 less_cond = LT;
3836 break;
3837 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003838 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003839 __ cmp(left.AsRegisterPairHigh<Register>(),
3840 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003841 __ b(&less, LT);
3842 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003843 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00003844 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003845 __ cmp(left.AsRegisterPairLow<Register>(),
3846 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003847 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00003848 break;
3849 }
3850 case Primitive::kPrimFloat:
3851 case Primitive::kPrimDouble: {
3852 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003853 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00003854 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00003855 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003856 break;
3857 }
3858 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003859 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00003860 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003861 }
Aart Bika19616e2016-02-01 18:57:58 -08003862
Calin Juravleddb7df22014-11-25 20:56:51 +00003863 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00003864 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00003865
3866 __ Bind(&greater);
3867 __ LoadImmediate(out, 1);
3868 __ b(&done);
3869
3870 __ Bind(&less);
3871 __ LoadImmediate(out, -1);
3872
3873 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003874}
3875
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003876void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003877 LocationSummary* locations =
3878 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01003879 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003880 locations->SetInAt(i, Location::Any());
3881 }
3882 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003883}
3884
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003885void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01003886 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003887}
3888
Roland Levillainc9285912015-12-18 10:38:42 +00003889void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
3890 // TODO (ported from quick): revisit ARM barrier kinds.
3891 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00003892 switch (kind) {
3893 case MemBarrierKind::kAnyStore:
3894 case MemBarrierKind::kLoadAny:
3895 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003896 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00003897 break;
3898 }
3899 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07003900 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00003901 break;
3902 }
3903 default:
3904 LOG(FATAL) << "Unexpected memory barrier " << kind;
3905 }
Kenny Root1d8199d2015-06-02 11:01:10 -07003906 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00003907}
3908
3909void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
3910 uint32_t offset,
3911 Register out_lo,
3912 Register out_hi) {
3913 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00003914 // Ensure `out_lo` is different from `addr`, so that loading
3915 // `offset` into `out_lo` does not clutter `addr`.
3916 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00003917 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003918 __ add(IP, addr, ShifterOperand(out_lo));
3919 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003920 }
3921 __ ldrexd(out_lo, out_hi, addr);
3922}
3923
3924void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
3925 uint32_t offset,
3926 Register value_lo,
3927 Register value_hi,
3928 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00003929 Register temp2,
3930 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00003931 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00003932 if (offset != 0) {
3933 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00003934 __ add(IP, addr, ShifterOperand(temp1));
3935 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00003936 }
3937 __ Bind(&fail);
3938 // We need a load followed by store. (The address used in a STREX instruction must
3939 // be the same as the address in the most recently executed LDREX instruction.)
3940 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00003941 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003942 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003943 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00003944}
3945
3946void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3947 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3948
Nicolas Geoffray39468442014-09-02 15:17:15 +01003949 LocationSummary* locations =
3950 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003951 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003952
Calin Juravle52c48962014-12-16 17:02:57 +00003953 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003954 if (Primitive::IsFloatingPointType(field_type)) {
3955 locations->SetInAt(1, Location::RequiresFpuRegister());
3956 } else {
3957 locations->SetInAt(1, Location::RequiresRegister());
3958 }
3959
Calin Juravle52c48962014-12-16 17:02:57 +00003960 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00003961 bool generate_volatile = field_info.IsVolatile()
3962 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003963 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01003964 bool needs_write_barrier =
3965 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003966 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00003967 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01003968 if (needs_write_barrier) {
3969 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003970 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00003971 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00003972 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00003973 // - registers need to be consecutive
3974 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00003975 // We don't test for ARM yet, and the assertion makes sure that we
3976 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00003977 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
3978
3979 locations->AddTemp(Location::RequiresRegister());
3980 locations->AddTemp(Location::RequiresRegister());
3981 if (field_type == Primitive::kPrimDouble) {
3982 // For doubles we need two more registers to copy the value.
3983 locations->AddTemp(Location::RegisterLocation(R2));
3984 locations->AddTemp(Location::RegisterLocation(R3));
3985 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003986 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003987}
3988
Calin Juravle52c48962014-12-16 17:02:57 +00003989void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003990 const FieldInfo& field_info,
3991 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003992 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3993
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003994 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00003995 Register base = locations->InAt(0).AsRegister<Register>();
3996 Location value = locations->InAt(1);
3997
3998 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00003999 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004000 Primitive::Type field_type = field_info.GetFieldType();
4001 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004002 bool needs_write_barrier =
4003 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004004
4005 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004006 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004007 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004008
4009 switch (field_type) {
4010 case Primitive::kPrimBoolean:
4011 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004012 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004013 break;
4014 }
4015
4016 case Primitive::kPrimShort:
4017 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004018 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004019 break;
4020 }
4021
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004022 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004023 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004024 if (kPoisonHeapReferences && needs_write_barrier) {
4025 // Note that in the case where `value` is a null reference,
4026 // we do not enter this block, as a null reference does not
4027 // need poisoning.
4028 DCHECK_EQ(field_type, Primitive::kPrimNot);
4029 Register temp = locations->GetTemp(0).AsRegister<Register>();
4030 __ Mov(temp, value.AsRegister<Register>());
4031 __ PoisonHeapReference(temp);
4032 __ StoreToOffset(kStoreWord, temp, base, offset);
4033 } else {
4034 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4035 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004036 break;
4037 }
4038
4039 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004040 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004041 GenerateWideAtomicStore(base, offset,
4042 value.AsRegisterPairLow<Register>(),
4043 value.AsRegisterPairHigh<Register>(),
4044 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004045 locations->GetTemp(1).AsRegister<Register>(),
4046 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004047 } else {
4048 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004049 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004050 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004051 break;
4052 }
4053
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004054 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004055 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004056 break;
4057 }
4058
4059 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004060 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004061 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004062 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4063 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4064
4065 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4066
4067 GenerateWideAtomicStore(base, offset,
4068 value_reg_lo,
4069 value_reg_hi,
4070 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004071 locations->GetTemp(3).AsRegister<Register>(),
4072 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004073 } else {
4074 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004075 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004076 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004077 break;
4078 }
4079
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004080 case Primitive::kPrimVoid:
4081 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004082 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004083 }
Calin Juravle52c48962014-12-16 17:02:57 +00004084
Calin Juravle77520bc2015-01-12 18:45:46 +00004085 // Longs and doubles are handled in the switch.
4086 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4087 codegen_->MaybeRecordImplicitNullCheck(instruction);
4088 }
4089
4090 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4091 Register temp = locations->GetTemp(0).AsRegister<Register>();
4092 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004093 codegen_->MarkGCCard(
4094 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004095 }
4096
Calin Juravle52c48962014-12-16 17:02:57 +00004097 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004098 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004099 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004100}
4101
Calin Juravle52c48962014-12-16 17:02:57 +00004102void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4103 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004104
4105 bool object_field_get_with_read_barrier =
4106 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004107 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004108 new (GetGraph()->GetArena()) LocationSummary(instruction,
4109 object_field_get_with_read_barrier ?
4110 LocationSummary::kCallOnSlowPath :
4111 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004112 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4113 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4114 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004115 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004116
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004117 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004118 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004119 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004120 // The output overlaps in case of volatile long: we don't want the
4121 // code generated by GenerateWideAtomicLoad to overwrite the
4122 // object's location. Likewise, in the case of an object field get
4123 // with read barriers enabled, we do not want the load to overwrite
4124 // the object's location, as we need it to emit the read barrier.
4125 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4126 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004127
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004128 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4129 locations->SetOut(Location::RequiresFpuRegister());
4130 } else {
4131 locations->SetOut(Location::RequiresRegister(),
4132 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4133 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004134 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004135 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004136 // - registers need to be consecutive
4137 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004138 // We don't test for ARM yet, and the assertion makes sure that we
4139 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004140 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4141 locations->AddTemp(Location::RequiresRegister());
4142 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004143 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4144 // We need a temporary register for the read barrier marking slow
4145 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4146 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004147 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004148}
4149
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004150Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4151 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4152 << input->GetType();
4153 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4154 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4155 return Location::ConstantLocation(input->AsConstant());
4156 } else {
4157 return Location::RequiresFpuRegister();
4158 }
4159}
4160
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004161Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4162 Opcode opcode) {
4163 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4164 if (constant->IsConstant() &&
4165 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4166 return Location::ConstantLocation(constant->AsConstant());
4167 }
4168 return Location::RequiresRegister();
4169}
4170
4171bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4172 Opcode opcode) {
4173 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4174 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004175 Opcode high_opcode = opcode;
4176 SetCc low_set_cc = kCcDontCare;
4177 switch (opcode) {
4178 case SUB:
4179 // Flip the operation to an ADD.
4180 value = -value;
4181 opcode = ADD;
4182 FALLTHROUGH_INTENDED;
4183 case ADD:
4184 if (Low32Bits(value) == 0u) {
4185 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4186 }
4187 high_opcode = ADC;
4188 low_set_cc = kCcSet;
4189 break;
4190 default:
4191 break;
4192 }
4193 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4194 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004195 } else {
4196 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4197 }
4198}
4199
Vladimir Marko59751a72016-08-05 14:37:27 +01004200bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4201 Opcode opcode,
4202 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004203 ShifterOperand so;
4204 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004205 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004206 return true;
4207 }
4208 Opcode neg_opcode = kNoOperand;
4209 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004210 case AND: neg_opcode = BIC; value = ~value; break;
4211 case ORR: neg_opcode = ORN; value = ~value; break;
4212 case ADD: neg_opcode = SUB; value = -value; break;
4213 case ADC: neg_opcode = SBC; value = ~value; break;
4214 case SUB: neg_opcode = ADD; value = -value; break;
4215 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004216 default:
4217 return false;
4218 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004219 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004220}
4221
Calin Juravle52c48962014-12-16 17:02:57 +00004222void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4223 const FieldInfo& field_info) {
4224 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004225
Calin Juravle52c48962014-12-16 17:02:57 +00004226 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004227 Location base_loc = locations->InAt(0);
4228 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004229 Location out = locations->Out();
4230 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004231 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004232 Primitive::Type field_type = field_info.GetFieldType();
4233 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4234
4235 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004236 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004237 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004238 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004239
Roland Levillainc9285912015-12-18 10:38:42 +00004240 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004241 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004242 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004243
Roland Levillainc9285912015-12-18 10:38:42 +00004244 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004245 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004246 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004247
Roland Levillainc9285912015-12-18 10:38:42 +00004248 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004249 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004250 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004251
4252 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004253 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004254 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004255
4256 case Primitive::kPrimNot: {
4257 // /* HeapReference<Object> */ out = *(base + offset)
4258 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4259 Location temp_loc = locations->GetTemp(0);
4260 // Note that a potential implicit null check is handled in this
4261 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4262 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4263 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4264 if (is_volatile) {
4265 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4266 }
4267 } else {
4268 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4269 codegen_->MaybeRecordImplicitNullCheck(instruction);
4270 if (is_volatile) {
4271 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4272 }
4273 // If read barriers are enabled, emit read barriers other than
4274 // Baker's using a slow path (and also unpoison the loaded
4275 // reference, if heap poisoning is enabled).
4276 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4277 }
4278 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004279 }
4280
Roland Levillainc9285912015-12-18 10:38:42 +00004281 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004282 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004283 GenerateWideAtomicLoad(base, offset,
4284 out.AsRegisterPairLow<Register>(),
4285 out.AsRegisterPairHigh<Register>());
4286 } else {
4287 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4288 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004289 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004290
Roland Levillainc9285912015-12-18 10:38:42 +00004291 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004292 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004293 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004294
4295 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004296 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004297 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004298 Register lo = locations->GetTemp(0).AsRegister<Register>();
4299 Register hi = locations->GetTemp(1).AsRegister<Register>();
4300 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004301 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004302 __ vmovdrr(out_reg, lo, hi);
4303 } else {
4304 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004305 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004306 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004307 break;
4308 }
4309
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004310 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004311 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004312 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004313 }
Calin Juravle52c48962014-12-16 17:02:57 +00004314
Roland Levillainc9285912015-12-18 10:38:42 +00004315 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4316 // Potential implicit null checks, in the case of reference or
4317 // double fields, are handled in the previous switch statement.
4318 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004319 codegen_->MaybeRecordImplicitNullCheck(instruction);
4320 }
4321
Calin Juravle52c48962014-12-16 17:02:57 +00004322 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004323 if (field_type == Primitive::kPrimNot) {
4324 // Memory barriers, in the case of references, are also handled
4325 // in the previous switch statement.
4326 } else {
4327 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4328 }
Roland Levillain4d027112015-07-01 15:41:14 +01004329 }
Calin Juravle52c48962014-12-16 17:02:57 +00004330}
4331
4332void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4333 HandleFieldSet(instruction, instruction->GetFieldInfo());
4334}
4335
4336void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004337 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004338}
4339
4340void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4341 HandleFieldGet(instruction, instruction->GetFieldInfo());
4342}
4343
4344void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4345 HandleFieldGet(instruction, instruction->GetFieldInfo());
4346}
4347
4348void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4349 HandleFieldGet(instruction, instruction->GetFieldInfo());
4350}
4351
4352void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4353 HandleFieldGet(instruction, instruction->GetFieldInfo());
4354}
4355
4356void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4357 HandleFieldSet(instruction, instruction->GetFieldInfo());
4358}
4359
4360void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004361 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004362}
4363
Calin Juravlee460d1d2015-09-29 04:52:17 +01004364void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4365 HUnresolvedInstanceFieldGet* instruction) {
4366 FieldAccessCallingConventionARM calling_convention;
4367 codegen_->CreateUnresolvedFieldLocationSummary(
4368 instruction, instruction->GetFieldType(), calling_convention);
4369}
4370
4371void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4372 HUnresolvedInstanceFieldGet* instruction) {
4373 FieldAccessCallingConventionARM calling_convention;
4374 codegen_->GenerateUnresolvedFieldAccess(instruction,
4375 instruction->GetFieldType(),
4376 instruction->GetFieldIndex(),
4377 instruction->GetDexPc(),
4378 calling_convention);
4379}
4380
4381void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4382 HUnresolvedInstanceFieldSet* instruction) {
4383 FieldAccessCallingConventionARM calling_convention;
4384 codegen_->CreateUnresolvedFieldLocationSummary(
4385 instruction, instruction->GetFieldType(), calling_convention);
4386}
4387
4388void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4389 HUnresolvedInstanceFieldSet* instruction) {
4390 FieldAccessCallingConventionARM calling_convention;
4391 codegen_->GenerateUnresolvedFieldAccess(instruction,
4392 instruction->GetFieldType(),
4393 instruction->GetFieldIndex(),
4394 instruction->GetDexPc(),
4395 calling_convention);
4396}
4397
4398void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4399 HUnresolvedStaticFieldGet* instruction) {
4400 FieldAccessCallingConventionARM calling_convention;
4401 codegen_->CreateUnresolvedFieldLocationSummary(
4402 instruction, instruction->GetFieldType(), calling_convention);
4403}
4404
4405void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4406 HUnresolvedStaticFieldGet* instruction) {
4407 FieldAccessCallingConventionARM calling_convention;
4408 codegen_->GenerateUnresolvedFieldAccess(instruction,
4409 instruction->GetFieldType(),
4410 instruction->GetFieldIndex(),
4411 instruction->GetDexPc(),
4412 calling_convention);
4413}
4414
4415void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4416 HUnresolvedStaticFieldSet* instruction) {
4417 FieldAccessCallingConventionARM calling_convention;
4418 codegen_->CreateUnresolvedFieldLocationSummary(
4419 instruction, instruction->GetFieldType(), calling_convention);
4420}
4421
4422void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4423 HUnresolvedStaticFieldSet* instruction) {
4424 FieldAccessCallingConventionARM calling_convention;
4425 codegen_->GenerateUnresolvedFieldAccess(instruction,
4426 instruction->GetFieldType(),
4427 instruction->GetFieldIndex(),
4428 instruction->GetDexPc(),
4429 calling_convention);
4430}
4431
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004432void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko3b7537b2016-09-13 11:56:01 +00004433 codegen_->CreateNullCheckLocations(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004434}
4435
Calin Juravle2ae48182016-03-16 14:05:09 +00004436void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4437 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004438 return;
4439 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004440 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004441
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004442 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004443 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004444}
4445
Calin Juravle2ae48182016-03-16 14:05:09 +00004446void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004447 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004448 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004449
4450 LocationSummary* locations = instruction->GetLocations();
4451 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004452
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004453 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004454}
4455
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004456void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004457 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004458}
4459
Artem Serov6c916792016-07-11 14:02:34 +01004460static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4461 switch (type) {
4462 case Primitive::kPrimNot:
4463 return kLoadWord;
4464 case Primitive::kPrimBoolean:
4465 return kLoadUnsignedByte;
4466 case Primitive::kPrimByte:
4467 return kLoadSignedByte;
4468 case Primitive::kPrimChar:
4469 return kLoadUnsignedHalfword;
4470 case Primitive::kPrimShort:
4471 return kLoadSignedHalfword;
4472 case Primitive::kPrimInt:
4473 return kLoadWord;
4474 case Primitive::kPrimLong:
4475 return kLoadWordPair;
4476 case Primitive::kPrimFloat:
4477 return kLoadSWord;
4478 case Primitive::kPrimDouble:
4479 return kLoadDWord;
4480 default:
4481 LOG(FATAL) << "Unreachable type " << type;
4482 UNREACHABLE();
4483 }
4484}
4485
4486static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4487 switch (type) {
4488 case Primitive::kPrimNot:
4489 return kStoreWord;
4490 case Primitive::kPrimBoolean:
4491 case Primitive::kPrimByte:
4492 return kStoreByte;
4493 case Primitive::kPrimChar:
4494 case Primitive::kPrimShort:
4495 return kStoreHalfword;
4496 case Primitive::kPrimInt:
4497 return kStoreWord;
4498 case Primitive::kPrimLong:
4499 return kStoreWordPair;
4500 case Primitive::kPrimFloat:
4501 return kStoreSWord;
4502 case Primitive::kPrimDouble:
4503 return kStoreDWord;
4504 default:
4505 LOG(FATAL) << "Unreachable type " << type;
4506 UNREACHABLE();
4507 }
4508}
4509
4510void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
4511 Location out_loc,
4512 Register base,
4513 Register reg_offset,
4514 Condition cond) {
4515 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4516 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4517
4518 switch (type) {
4519 case Primitive::kPrimByte:
4520 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
4521 break;
4522 case Primitive::kPrimBoolean:
4523 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
4524 break;
4525 case Primitive::kPrimShort:
4526 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
4527 break;
4528 case Primitive::kPrimChar:
4529 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
4530 break;
4531 case Primitive::kPrimNot:
4532 case Primitive::kPrimInt:
4533 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
4534 break;
4535 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
4536 case Primitive::kPrimLong:
4537 case Primitive::kPrimFloat:
4538 case Primitive::kPrimDouble:
4539 default:
4540 LOG(FATAL) << "Unreachable type " << type;
4541 UNREACHABLE();
4542 }
4543}
4544
4545void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
4546 Location loc,
4547 Register base,
4548 Register reg_offset,
4549 Condition cond) {
4550 uint32_t shift_count = Primitive::ComponentSizeShift(type);
4551 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
4552
4553 switch (type) {
4554 case Primitive::kPrimByte:
4555 case Primitive::kPrimBoolean:
4556 __ strb(loc.AsRegister<Register>(), mem_address, cond);
4557 break;
4558 case Primitive::kPrimShort:
4559 case Primitive::kPrimChar:
4560 __ strh(loc.AsRegister<Register>(), mem_address, cond);
4561 break;
4562 case Primitive::kPrimNot:
4563 case Primitive::kPrimInt:
4564 __ str(loc.AsRegister<Register>(), mem_address, cond);
4565 break;
4566 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
4567 case Primitive::kPrimLong:
4568 case Primitive::kPrimFloat:
4569 case Primitive::kPrimDouble:
4570 default:
4571 LOG(FATAL) << "Unreachable type " << type;
4572 UNREACHABLE();
4573 }
4574}
4575
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004576void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004577 bool object_array_get_with_read_barrier =
4578 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004579 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004580 new (GetGraph()->GetArena()) LocationSummary(instruction,
4581 object_array_get_with_read_barrier ?
4582 LocationSummary::kCallOnSlowPath :
4583 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004584 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4585 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
4586 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004587 locations->SetInAt(0, Location::RequiresRegister());
4588 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004589 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4590 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4591 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00004592 // The output overlaps in the case of an object array get with
4593 // read barriers enabled: we do not want the move to overwrite the
4594 // array's location, as we need it to emit the read barrier.
4595 locations->SetOut(
4596 Location::RequiresRegister(),
4597 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004598 }
Roland Levillainc9285912015-12-18 10:38:42 +00004599 // We need a temporary register for the read barrier marking slow
4600 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
4601 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4602 locations->AddTemp(Location::RequiresRegister());
4603 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004604}
4605
4606void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
4607 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004608 Location obj_loc = locations->InAt(0);
4609 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004610 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00004611 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01004612 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00004613 Primitive::Type type = instruction->GetType();
Artem Serov328429f2016-07-06 16:23:04 +01004614 HInstruction* array_instr = instruction->GetArray();
4615 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004616 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4617 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Artem Serov6c916792016-07-11 14:02:34 +01004618
Roland Levillain4d027112015-07-01 15:41:14 +01004619 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01004620 case Primitive::kPrimBoolean:
4621 case Primitive::kPrimByte:
4622 case Primitive::kPrimShort:
4623 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00004624 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004625 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004626 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4627 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
4628
4629 LoadOperandType load_type = GetLoadOperandType(type);
4630 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004631 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004632 Register temp = IP;
4633
4634 if (has_intermediate_address) {
4635 // We do not need to compute the intermediate address from the array: the
4636 // input instruction has done it already. See the comment in
4637 // `TryExtractArrayAccessAddress()`.
4638 if (kIsDebugBuild) {
4639 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4640 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4641 }
4642 temp = obj;
4643 } else {
4644 __ add(temp, obj, ShifterOperand(data_offset));
4645 }
4646 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004647 }
4648 break;
4649 }
4650
Roland Levillainc9285912015-12-18 10:38:42 +00004651 case Primitive::kPrimNot: {
4652 static_assert(
4653 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4654 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00004655 // /* HeapReference<Object> */ out =
4656 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4657 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4658 Location temp = locations->GetTemp(0);
4659 // Note that a potential implicit null check is handled in this
4660 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
4661 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4662 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4663 } else {
4664 Register out = out_loc.AsRegister<Register>();
4665 if (index.IsConstant()) {
4666 size_t offset =
4667 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4668 __ LoadFromOffset(kLoadWord, out, obj, offset);
4669 codegen_->MaybeRecordImplicitNullCheck(instruction);
4670 // If read barriers are enabled, emit read barriers other than
4671 // Baker's using a slow path (and also unpoison the loaded
4672 // reference, if heap poisoning is enabled).
4673 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4674 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004675 Register temp = IP;
4676
4677 if (has_intermediate_address) {
4678 // We do not need to compute the intermediate address from the array: the
4679 // input instruction has done it already. See the comment in
4680 // `TryExtractArrayAccessAddress()`.
4681 if (kIsDebugBuild) {
4682 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4683 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
4684 }
4685 temp = obj;
4686 } else {
4687 __ add(temp, obj, ShifterOperand(data_offset));
4688 }
4689 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01004690
Roland Levillainc9285912015-12-18 10:38:42 +00004691 codegen_->MaybeRecordImplicitNullCheck(instruction);
4692 // If read barriers are enabled, emit read barriers other than
4693 // Baker's using a slow path (and also unpoison the loaded
4694 // reference, if heap poisoning is enabled).
4695 codegen_->MaybeGenerateReadBarrierSlow(
4696 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4697 }
4698 }
4699 break;
4700 }
4701
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004702 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004703 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004704 size_t offset =
4705 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004706 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004707 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004708 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004709 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004710 }
4711 break;
4712 }
4713
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004714 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00004715 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004716 if (index.IsConstant()) {
4717 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004718 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004719 } else {
4720 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00004721 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004722 }
4723 break;
4724 }
4725
4726 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00004727 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004728 if (index.IsConstant()) {
4729 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00004730 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004731 } else {
4732 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00004733 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004734 }
4735 break;
4736 }
4737
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004738 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01004739 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004740 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004741 }
Roland Levillain4d027112015-07-01 15:41:14 +01004742
4743 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00004744 // Potential implicit null checks, in the case of reference
4745 // arrays, are handled in the previous switch statement.
4746 } else {
4747 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01004748 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004749}
4750
4751void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004752 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004753
4754 bool needs_write_barrier =
4755 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00004756 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004757
Nicolas Geoffray39468442014-09-02 15:17:15 +01004758 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004759 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01004760 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00004761 LocationSummary::kCallOnSlowPath :
4762 LocationSummary::kNoCall);
4763
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004764 locations->SetInAt(0, Location::RequiresRegister());
4765 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4766 if (Primitive::IsFloatingPointType(value_type)) {
4767 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004768 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004769 locations->SetInAt(2, Location::RequiresRegister());
4770 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004771 if (needs_write_barrier) {
4772 // Temporary registers for the write barrier.
4773 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004774 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004775 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004776}
4777
4778void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
4779 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004780 Location array_loc = locations->InAt(0);
4781 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004782 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004783 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00004784 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004785 bool needs_write_barrier =
4786 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01004787 uint32_t data_offset =
4788 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
4789 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01004790 HInstruction* array_instr = instruction->GetArray();
4791 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Roland Levillain4a3aa572016-08-15 13:17:06 +00004792 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
4793 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004794
4795 switch (value_type) {
4796 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01004797 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004798 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01004799 case Primitive::kPrimChar:
4800 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004801 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01004802 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
4803 uint32_t full_offset =
4804 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
4805 StoreOperandType store_type = GetStoreOperandType(value_type);
4806 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004807 } else {
Artem Serov328429f2016-07-06 16:23:04 +01004808 Register temp = IP;
4809
4810 if (has_intermediate_address) {
4811 // We do not need to compute the intermediate address from the array: the
4812 // input instruction has done it already. See the comment in
4813 // `TryExtractArrayAccessAddress()`.
4814 if (kIsDebugBuild) {
4815 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
4816 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
4817 }
4818 temp = array;
4819 } else {
4820 __ add(temp, array, ShifterOperand(data_offset));
4821 }
Artem Serov6c916792016-07-11 14:02:34 +01004822 codegen_->StoreToShiftedRegOffset(value_type,
4823 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01004824 temp,
Artem Serov6c916792016-07-11 14:02:34 +01004825 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004826 }
4827 break;
4828 }
4829
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004830 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00004831 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01004832 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
4833 // See the comment in instruction_simplifier_shared.cc.
4834 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004835
4836 if (instruction->InputAt(2)->IsNullConstant()) {
4837 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004838 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004839 size_t offset =
4840 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01004841 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004842 } else {
4843 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004844 __ add(IP, array, ShifterOperand(data_offset));
4845 codegen_->StoreToShiftedRegOffset(value_type,
4846 value_loc,
4847 IP,
4848 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004849 }
Roland Levillain1407ee72016-01-08 15:56:19 +00004850 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00004851 DCHECK(!needs_write_barrier);
4852 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004853 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004854 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004855
4856 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01004857 Location temp1_loc = locations->GetTemp(0);
4858 Register temp1 = temp1_loc.AsRegister<Register>();
4859 Location temp2_loc = locations->GetTemp(1);
4860 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004861 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4862 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4863 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4864 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01004865 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004866
Roland Levillain3b359c72015-11-17 19:35:12 +00004867 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004868 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
4869 codegen_->AddSlowPath(slow_path);
4870 if (instruction->GetValueCanBeNull()) {
4871 Label non_zero;
4872 __ CompareAndBranchIfNonZero(value, &non_zero);
4873 if (index.IsConstant()) {
4874 size_t offset =
4875 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4876 __ StoreToOffset(kStoreWord, value, array, offset);
4877 } else {
4878 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004879 __ add(IP, array, ShifterOperand(data_offset));
4880 codegen_->StoreToShiftedRegOffset(value_type,
4881 value_loc,
4882 IP,
4883 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004884 }
4885 codegen_->MaybeRecordImplicitNullCheck(instruction);
4886 __ b(&done);
4887 __ Bind(&non_zero);
4888 }
4889
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004890 // Note that when read barriers are enabled, the type checks
4891 // are performed without read barriers. This is fine, even in
4892 // the case where a class object is in the from-space after
4893 // the flip, as a comparison involving such a type would not
4894 // produce a false positive; it may of course produce a false
4895 // negative, in which case we would take the ArraySet slow
4896 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01004897
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004898 // /* HeapReference<Class> */ temp1 = array->klass_
4899 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
4900 codegen_->MaybeRecordImplicitNullCheck(instruction);
4901 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01004902
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004903 // /* HeapReference<Class> */ temp1 = temp1->component_type_
4904 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
4905 // /* HeapReference<Class> */ temp2 = value->klass_
4906 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
4907 // If heap poisoning is enabled, no need to unpoison `temp1`
4908 // nor `temp2`, as we are comparing two poisoned references.
4909 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01004910
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004911 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4912 Label do_put;
4913 __ b(&do_put, EQ);
4914 // If heap poisoning is enabled, the `temp1` reference has
4915 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00004916 __ MaybeUnpoisonHeapReference(temp1);
4917
Roland Levillain9d6e1f82016-09-05 15:57:33 +01004918 // /* HeapReference<Class> */ temp1 = temp1->super_class_
4919 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
4920 // If heap poisoning is enabled, no need to unpoison
4921 // `temp1`, as we are comparing against null below.
4922 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
4923 __ Bind(&do_put);
4924 } else {
4925 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004926 }
4927 }
4928
Artem Serov6c916792016-07-11 14:02:34 +01004929 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004930 if (kPoisonHeapReferences) {
4931 // Note that in the case where `value` is a null reference,
4932 // we do not enter this block, as a null reference does not
4933 // need poisoning.
4934 DCHECK_EQ(value_type, Primitive::kPrimNot);
4935 __ Mov(temp1, value);
4936 __ PoisonHeapReference(temp1);
4937 source = temp1;
4938 }
4939
4940 if (index.IsConstant()) {
4941 size_t offset =
4942 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4943 __ StoreToOffset(kStoreWord, source, array, offset);
4944 } else {
4945 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01004946
4947 __ add(IP, array, ShifterOperand(data_offset));
4948 codegen_->StoreToShiftedRegOffset(value_type,
4949 Location::RegisterLocation(source),
4950 IP,
4951 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004952 }
4953
Roland Levillain3b359c72015-11-17 19:35:12 +00004954 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004955 codegen_->MaybeRecordImplicitNullCheck(instruction);
4956 }
4957
4958 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
4959
4960 if (done.IsLinked()) {
4961 __ Bind(&done);
4962 }
4963
4964 if (slow_path != nullptr) {
4965 __ Bind(slow_path->GetExitLabel());
4966 }
4967
4968 break;
4969 }
4970
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004971 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004972 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004973 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004974 size_t offset =
4975 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004976 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004977 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004978 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004979 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004980 }
4981 break;
4982 }
4983
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004984 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004985 Location value = locations->InAt(2);
4986 DCHECK(value.IsFpuRegister());
4987 if (index.IsConstant()) {
4988 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004989 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004990 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004991 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004992 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
4993 }
4994 break;
4995 }
4996
4997 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00004998 Location value = locations->InAt(2);
4999 DCHECK(value.IsFpuRegisterPair());
5000 if (index.IsConstant()) {
5001 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005002 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005003 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005004 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005005 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5006 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005007
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005008 break;
5009 }
5010
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005011 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005012 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005013 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005014 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005015
Roland Levillain80e67092016-01-08 16:04:55 +00005016 // Objects are handled in the switch.
5017 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005018 codegen_->MaybeRecordImplicitNullCheck(instruction);
5019 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005020}
5021
5022void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005023 LocationSummary* locations =
5024 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005025 locations->SetInAt(0, Location::RequiresRegister());
5026 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005027}
5028
5029void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5030 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005031 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005032 Register obj = locations->InAt(0).AsRegister<Register>();
5033 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005034 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005035 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005036}
5037
Artem Serov328429f2016-07-06 16:23:04 +01005038void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Roland Levillain4a3aa572016-08-15 13:17:06 +00005039 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
5040 DCHECK(!kEmitCompilerReadBarrier);
Artem Serov328429f2016-07-06 16:23:04 +01005041 LocationSummary* locations =
5042 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5043
5044 locations->SetInAt(0, Location::RequiresRegister());
5045 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5046 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5047}
5048
5049void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5050 LocationSummary* locations = instruction->GetLocations();
5051 Location out = locations->Out();
5052 Location first = locations->InAt(0);
5053 Location second = locations->InAt(1);
5054
Roland Levillain4a3aa572016-08-15 13:17:06 +00005055 // The read barrier instrumentation does not support the HIntermediateAddress instruction yet.
5056 DCHECK(!kEmitCompilerReadBarrier);
5057
Artem Serov328429f2016-07-06 16:23:04 +01005058 if (second.IsRegister()) {
5059 __ add(out.AsRegister<Register>(),
5060 first.AsRegister<Register>(),
5061 ShifterOperand(second.AsRegister<Register>()));
5062 } else {
5063 __ AddConstant(out.AsRegister<Register>(),
5064 first.AsRegister<Register>(),
5065 second.GetConstant()->AsIntConstant()->GetValue());
5066 }
5067}
5068
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005070 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5071 ? LocationSummary::kCallOnSlowPath
5072 : LocationSummary::kNoCall;
5073 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074 locations->SetInAt(0, Location::RequiresRegister());
5075 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005076 if (instruction->HasUses()) {
5077 locations->SetOut(Location::SameAsFirstInput());
5078 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005079}
5080
5081void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5082 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005083 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005084 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005085 codegen_->AddSlowPath(slow_path);
5086
Roland Levillain271ab9c2014-11-27 15:23:57 +00005087 Register index = locations->InAt(0).AsRegister<Register>();
5088 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005089
5090 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005091 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005092}
5093
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005094void CodeGeneratorARM::MarkGCCard(Register temp,
5095 Register card,
5096 Register object,
5097 Register value,
5098 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005099 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005100 if (can_be_null) {
5101 __ CompareAndBranchIfZero(value, &is_null);
5102 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005103 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5105 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005106 if (can_be_null) {
5107 __ Bind(&is_null);
5108 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005109}
5110
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005111void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005112 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005113}
5114
5115void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005116 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5117}
5118
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005119void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005120 LocationSummary* locations =
5121 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5122 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005123}
5124
5125void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005126 HBasicBlock* block = instruction->GetBlock();
5127 if (block->GetLoopInformation() != nullptr) {
5128 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5129 // The back edge will generate the suspend check.
5130 return;
5131 }
5132 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5133 // The goto will generate the suspend check.
5134 return;
5135 }
5136 GenerateSuspendCheck(instruction, nullptr);
5137}
5138
5139void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5140 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005141 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005142 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5143 if (slow_path == nullptr) {
5144 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5145 instruction->SetSlowPath(slow_path);
5146 codegen_->AddSlowPath(slow_path);
5147 if (successor != nullptr) {
5148 DCHECK(successor->IsLoopHeader());
5149 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5150 }
5151 } else {
5152 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5153 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005154
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005155 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005156 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005157 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005158 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005159 __ Bind(slow_path->GetReturnLabel());
5160 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005161 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005162 __ b(slow_path->GetEntryLabel());
5163 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005164}
5165
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005166ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5167 return codegen_->GetAssembler();
5168}
5169
5170void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005171 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005172 Location source = move->GetSource();
5173 Location destination = move->GetDestination();
5174
5175 if (source.IsRegister()) {
5176 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005177 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005178 } else if (destination.IsFpuRegister()) {
5179 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005180 } else {
5181 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005182 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005183 SP, destination.GetStackIndex());
5184 }
5185 } else if (source.IsStackSlot()) {
5186 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005187 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005188 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005189 } else if (destination.IsFpuRegister()) {
5190 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005191 } else {
5192 DCHECK(destination.IsStackSlot());
5193 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5194 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5195 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005196 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005197 if (destination.IsRegister()) {
5198 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5199 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005200 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005201 } else {
5202 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005203 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5204 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005205 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005206 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005207 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5208 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005209 } else if (destination.IsRegisterPair()) {
5210 DCHECK(ExpectedPairLayout(destination));
5211 __ LoadFromOffset(
5212 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5213 } else {
5214 DCHECK(destination.IsFpuRegisterPair()) << destination;
5215 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5216 SP,
5217 source.GetStackIndex());
5218 }
5219 } else if (source.IsRegisterPair()) {
5220 if (destination.IsRegisterPair()) {
5221 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5222 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005223 } else if (destination.IsFpuRegisterPair()) {
5224 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5225 source.AsRegisterPairLow<Register>(),
5226 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005227 } else {
5228 DCHECK(destination.IsDoubleStackSlot()) << destination;
5229 DCHECK(ExpectedPairLayout(source));
5230 __ StoreToOffset(
5231 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5232 }
5233 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005234 if (destination.IsRegisterPair()) {
5235 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5236 destination.AsRegisterPairHigh<Register>(),
5237 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5238 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005239 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5240 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5241 } else {
5242 DCHECK(destination.IsDoubleStackSlot()) << destination;
5243 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5244 SP,
5245 destination.GetStackIndex());
5246 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005247 } else {
5248 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005249 HConstant* constant = source.GetConstant();
5250 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5251 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005252 if (destination.IsRegister()) {
5253 __ LoadImmediate(destination.AsRegister<Register>(), value);
5254 } else {
5255 DCHECK(destination.IsStackSlot());
5256 __ LoadImmediate(IP, value);
5257 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5258 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005259 } else if (constant->IsLongConstant()) {
5260 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005261 if (destination.IsRegisterPair()) {
5262 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5263 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005264 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005265 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005266 __ LoadImmediate(IP, Low32Bits(value));
5267 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5268 __ LoadImmediate(IP, High32Bits(value));
5269 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5270 }
5271 } else if (constant->IsDoubleConstant()) {
5272 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005273 if (destination.IsFpuRegisterPair()) {
5274 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005275 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005276 DCHECK(destination.IsDoubleStackSlot()) << destination;
5277 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005278 __ LoadImmediate(IP, Low32Bits(int_value));
5279 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5280 __ LoadImmediate(IP, High32Bits(int_value));
5281 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5282 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005283 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005284 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005285 float value = constant->AsFloatConstant()->GetValue();
5286 if (destination.IsFpuRegister()) {
5287 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5288 } else {
5289 DCHECK(destination.IsStackSlot());
5290 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5291 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5292 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005293 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005294 }
5295}
5296
5297void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5298 __ Mov(IP, reg);
5299 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5300 __ StoreToOffset(kStoreWord, IP, SP, mem);
5301}
5302
5303void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5304 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5305 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5306 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5307 SP, mem1 + stack_offset);
5308 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5309 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5310 SP, mem2 + stack_offset);
5311 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5312}
5313
5314void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005315 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005316 Location source = move->GetSource();
5317 Location destination = move->GetDestination();
5318
5319 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005320 DCHECK_NE(source.AsRegister<Register>(), IP);
5321 DCHECK_NE(destination.AsRegister<Register>(), IP);
5322 __ Mov(IP, source.AsRegister<Register>());
5323 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5324 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005325 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005326 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005327 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005328 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005329 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5330 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005331 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005332 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005333 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005334 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005335 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005336 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005337 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005338 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005339 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5340 destination.AsRegisterPairHigh<Register>(),
5341 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005342 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005343 Register low_reg = source.IsRegisterPair()
5344 ? source.AsRegisterPairLow<Register>()
5345 : destination.AsRegisterPairLow<Register>();
5346 int mem = source.IsRegisterPair()
5347 ? destination.GetStackIndex()
5348 : source.GetStackIndex();
5349 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005350 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005351 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005352 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005353 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005354 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5355 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005356 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005357 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005358 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005359 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5360 DRegister reg = source.IsFpuRegisterPair()
5361 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5362 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5363 int mem = source.IsFpuRegisterPair()
5364 ? destination.GetStackIndex()
5365 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005366 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005367 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005368 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005369 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5370 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5371 : destination.AsFpuRegister<SRegister>();
5372 int mem = source.IsFpuRegister()
5373 ? destination.GetStackIndex()
5374 : source.GetStackIndex();
5375
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005376 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005377 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005378 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005379 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005380 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5381 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005382 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005383 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005384 }
5385}
5386
5387void ParallelMoveResolverARM::SpillScratch(int reg) {
5388 __ Push(static_cast<Register>(reg));
5389}
5390
5391void ParallelMoveResolverARM::RestoreScratch(int reg) {
5392 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005393}
5394
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005395HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5396 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005397 switch (desired_class_load_kind) {
5398 case HLoadClass::LoadKind::kReferrersClass:
5399 break;
5400 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5401 DCHECK(!GetCompilerOptions().GetCompilePic());
5402 break;
5403 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5404 DCHECK(GetCompilerOptions().GetCompilePic());
5405 break;
5406 case HLoadClass::LoadKind::kBootImageAddress:
5407 break;
5408 case HLoadClass::LoadKind::kDexCacheAddress:
5409 DCHECK(Runtime::Current()->UseJitCompilation());
5410 break;
5411 case HLoadClass::LoadKind::kDexCachePcRelative:
5412 DCHECK(!Runtime::Current()->UseJitCompilation());
5413 // We disable pc-relative load when there is an irreducible loop, as the optimization
5414 // is incompatible with it.
5415 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5416 // with irreducible loops.
5417 if (GetGraph()->HasIrreducibleLoops()) {
5418 return HLoadClass::LoadKind::kDexCacheViaMethod;
5419 }
5420 break;
5421 case HLoadClass::LoadKind::kDexCacheViaMethod:
5422 break;
5423 }
5424 return desired_class_load_kind;
5425}
5426
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005427void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005428 if (cls->NeedsAccessCheck()) {
5429 InvokeRuntimeCallingConvention calling_convention;
5430 CodeGenerator::CreateLoadClassLocationSummary(
5431 cls,
5432 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5433 Location::RegisterLocation(R0),
5434 /* code_generator_supports_read_barrier */ true);
5435 return;
5436 }
5437
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005438 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5439 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005440 ? LocationSummary::kCallOnSlowPath
5441 : LocationSummary::kNoCall;
5442 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005443 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005444 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5445 }
5446
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005447 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5448 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5449 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5450 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5451 locations->SetInAt(0, Location::RequiresRegister());
5452 }
5453 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005454}
5455
5456void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005457 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005458 if (cls->NeedsAccessCheck()) {
5459 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005460 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005461 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005462 return;
5463 }
5464
Roland Levillain3b359c72015-11-17 19:35:12 +00005465 Location out_loc = locations->Out();
5466 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005467
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005468 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005469 bool generate_null_check = false;
5470 switch (cls->GetLoadKind()) {
5471 case HLoadClass::LoadKind::kReferrersClass: {
5472 DCHECK(!cls->CanCallRuntime());
5473 DCHECK(!cls->MustGenerateClinitCheck());
5474 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5475 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005476 GenerateGcRootFieldLoad(cls,
5477 out_loc,
5478 current_method,
5479 ArtMethod::DeclaringClassOffset().Int32Value(),
5480 requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005481 break;
5482 }
5483 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005484 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005485 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
5486 cls->GetTypeIndex()));
5487 break;
5488 }
5489 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005490 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005491 CodeGeneratorARM::PcRelativePatchInfo* labels =
5492 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
5493 __ BindTrackedLabel(&labels->movw_label);
5494 __ movw(out, /* placeholder */ 0u);
5495 __ BindTrackedLabel(&labels->movt_label);
5496 __ movt(out, /* placeholder */ 0u);
5497 __ BindTrackedLabel(&labels->add_pc_label);
5498 __ add(out, out, ShifterOperand(PC));
5499 break;
5500 }
5501 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005502 DCHECK(!requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005503 DCHECK_NE(cls->GetAddress(), 0u);
5504 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5505 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5506 break;
5507 }
5508 case HLoadClass::LoadKind::kDexCacheAddress: {
5509 DCHECK_NE(cls->GetAddress(), 0u);
5510 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
5511 // 16-bit LDR immediate has a 5-bit offset multiplied by the size and that gives
5512 // a 128B range. To try and reduce the number of literals if we load multiple types,
5513 // simply split the dex cache address to a 128B aligned base loaded from a literal
5514 // and the remaining offset embedded in the load.
5515 static_assert(sizeof(GcRoot<mirror::Class>) == 4u, "Expected GC root to be 4 bytes.");
5516 DCHECK_ALIGNED(cls->GetAddress(), 4u);
5517 constexpr size_t offset_bits = /* encoded bits */ 5 + /* scale */ 2;
5518 uint32_t base_address = address & ~MaxInt<uint32_t>(offset_bits);
5519 uint32_t offset = address & MaxInt<uint32_t>(offset_bits);
5520 __ LoadLiteral(out, codegen_->DeduplicateDexCacheAddressLiteral(base_address));
5521 // /* GcRoot<mirror::Class> */ out = *(base_address + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005522 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005523 generate_null_check = !cls->IsInDexCache();
5524 break;
5525 }
5526 case HLoadClass::LoadKind::kDexCachePcRelative: {
5527 Register base_reg = locations->InAt(0).AsRegister<Register>();
5528 HArmDexCacheArraysBase* base = cls->InputAt(0)->AsArmDexCacheArraysBase();
5529 int32_t offset = cls->GetDexCacheElementOffset() - base->GetElementOffset();
5530 // /* GcRoot<mirror::Class> */ out = *(dex_cache_arrays_base + offset)
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005531 GenerateGcRootFieldLoad(cls, out_loc, base_reg, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005532 generate_null_check = !cls->IsInDexCache();
5533 break;
5534 }
5535 case HLoadClass::LoadKind::kDexCacheViaMethod: {
5536 // /* GcRoot<mirror::Class>[] */ out =
5537 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5538 Register current_method = locations->InAt(0).AsRegister<Register>();
5539 __ LoadFromOffset(kLoadWord,
5540 out,
5541 current_method,
5542 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value());
5543 // /* GcRoot<mirror::Class> */ out = out[type_index]
5544 size_t offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005545 GenerateGcRootFieldLoad(cls, out_loc, out, offset, requires_read_barrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005546 generate_null_check = !cls->IsInDexCache();
5547 }
5548 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005549
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005550 if (generate_null_check || cls->MustGenerateClinitCheck()) {
5551 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01005552 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005553 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5554 codegen_->AddSlowPath(slow_path);
5555 if (generate_null_check) {
5556 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
5557 }
5558 if (cls->MustGenerateClinitCheck()) {
5559 GenerateClassInitializationCheck(slow_path, out);
5560 } else {
5561 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005562 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005563 }
5564}
5565
5566void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
5567 LocationSummary* locations =
5568 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5569 locations->SetInAt(0, Location::RequiresRegister());
5570 if (check->HasUses()) {
5571 locations->SetOut(Location::SameAsFirstInput());
5572 }
5573}
5574
5575void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005576 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01005577 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005578 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005579 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005580 GenerateClassInitializationCheck(slow_path,
5581 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005582}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005583
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005584void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01005585 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005586 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
5587 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
5588 __ b(slow_path->GetEntryLabel(), LT);
5589 // Even if the initialized flag is set, we may be in a situation where caches are not synced
5590 // properly. Therefore, we do a memory fence.
5591 __ dmb(ISH);
5592 __ Bind(slow_path->GetExitLabel());
5593}
5594
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005595HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
5596 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005597 switch (desired_string_load_kind) {
5598 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5599 DCHECK(!GetCompilerOptions().GetCompilePic());
5600 break;
5601 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5602 DCHECK(GetCompilerOptions().GetCompilePic());
5603 break;
5604 case HLoadString::LoadKind::kBootImageAddress:
5605 break;
5606 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005607 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005608 break;
5609 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005610 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005611 // We disable pc-relative load when there is an irreducible loop, as the optimization
5612 // is incompatible with it.
5613 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
5614 // with irreducible loops.
5615 if (GetGraph()->HasIrreducibleLoops()) {
5616 return HLoadString::LoadKind::kDexCacheViaMethod;
5617 }
5618 break;
5619 case HLoadString::LoadKind::kDexCacheViaMethod:
5620 break;
5621 }
5622 return desired_string_load_kind;
5623}
5624
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005625void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005626 LocationSummary::CallKind call_kind = load->NeedsEnvironment()
5627 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005628 : LocationSummary::kNoCall;
5629 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005630
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005631 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005632 DCHECK(load_kind != HLoadString::LoadKind::kDexCachePcRelative) << "Not supported";
5633 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005634 locations->SetInAt(0, Location::RequiresRegister());
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005635 locations->SetOut(Location::RegisterLocation(R0));
5636 } else {
5637 locations->SetOut(Location::RequiresRegister());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005638 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005639}
5640
5641void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005642 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005643 Location out_loc = locations->Out();
5644 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005645 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00005646
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005647 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005648 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005649 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
5650 load->GetStringIndex()));
5651 return; // No dex cache slow path.
5652 }
5653 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005654 CodeGeneratorARM::PcRelativePatchInfo* labels =
5655 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
5656 __ BindTrackedLabel(&labels->movw_label);
5657 __ movw(out, /* placeholder */ 0u);
5658 __ BindTrackedLabel(&labels->movt_label);
5659 __ movt(out, /* placeholder */ 0u);
5660 __ BindTrackedLabel(&labels->add_pc_label);
5661 __ add(out, out, ShifterOperand(PC));
5662 return; // No dex cache slow path.
5663 }
5664 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005665 DCHECK_NE(load->GetAddress(), 0u);
5666 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
5667 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
5668 return; // No dex cache slow path.
5669 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005670 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07005671 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005672 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005673
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07005674 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
5675 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
5676 InvokeRuntimeCallingConvention calling_convention;
5677 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex());
5678 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
5679 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005680}
5681
David Brazdilcb1c0552015-08-04 16:22:25 +01005682static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07005683 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01005684}
5685
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005686void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
5687 LocationSummary* locations =
5688 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5689 locations->SetOut(Location::RequiresRegister());
5690}
5691
5692void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005693 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01005694 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
5695}
5696
5697void LocationsBuilderARM::VisitClearException(HClearException* clear) {
5698 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5699}
5700
5701void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005702 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01005703 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005704}
5705
5706void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
5707 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005708 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005709 InvokeRuntimeCallingConvention calling_convention;
5710 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5711}
5712
5713void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005714 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00005715 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005716}
5717
Roland Levillainc9285912015-12-18 10:38:42 +00005718static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5719 return kEmitCompilerReadBarrier &&
5720 (kUseBakerReadBarrier ||
5721 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5722 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5723 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5724}
5725
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005726void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005727 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00005728 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01005729 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00005730 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005731 case TypeCheckKind::kExactCheck:
5732 case TypeCheckKind::kAbstractClassCheck:
5733 case TypeCheckKind::kClassHierarchyCheck:
5734 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005735 call_kind =
5736 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01005737 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005738 break;
5739 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005740 case TypeCheckKind::kUnresolvedCheck:
5741 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005742 call_kind = LocationSummary::kCallOnSlowPath;
5743 break;
5744 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005745
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005746 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01005747 if (baker_read_barrier_slow_path) {
5748 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers.
5749 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005750 locations->SetInAt(0, Location::RequiresRegister());
5751 locations->SetInAt(1, Location::RequiresRegister());
5752 // The "out" register is used as a temporary, so it overlaps with the inputs.
5753 // Note that TypeCheckSlowPathARM uses this register too.
5754 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5755 // When read barriers are enabled, we need a temporary register for
5756 // some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005757 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005758 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005759 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005760}
5761
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005762void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005763 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005764 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005765 Location obj_loc = locations->InAt(0);
5766 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005767 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005768 Location out_loc = locations->Out();
5769 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005770 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005771 locations->GetTemp(0) :
5772 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005773 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005774 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5775 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5776 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005777 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005778 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005779
5780 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005781 // avoid null check if we know obj is not null.
5782 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00005783 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005784 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005785
Roland Levillain3b359c72015-11-17 19:35:12 +00005786 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005787 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005788
Roland Levillainc9285912015-12-18 10:38:42 +00005789 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005790 case TypeCheckKind::kExactCheck: {
5791 __ cmp(out, ShifterOperand(cls));
5792 // Classes must be equal for the instanceof to succeed.
5793 __ b(&zero, NE);
5794 __ LoadImmediate(out, 1);
5795 __ b(&done);
5796 break;
5797 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005798
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005799 case TypeCheckKind::kAbstractClassCheck: {
5800 // If the class is abstract, we eagerly fetch the super class of the
5801 // object to avoid doing a comparison we know will fail.
5802 Label loop;
5803 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005804 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005805 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005806 // If `out` is null, we use it for the result, and jump to `done`.
5807 __ CompareAndBranchIfZero(out, &done);
5808 __ cmp(out, ShifterOperand(cls));
5809 __ b(&loop, NE);
5810 __ LoadImmediate(out, 1);
5811 if (zero.IsLinked()) {
5812 __ b(&done);
5813 }
5814 break;
5815 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005816
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005817 case TypeCheckKind::kClassHierarchyCheck: {
5818 // Walk over the class hierarchy to find a match.
5819 Label loop, success;
5820 __ Bind(&loop);
5821 __ cmp(out, ShifterOperand(cls));
5822 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005823 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005824 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005825 __ CompareAndBranchIfNonZero(out, &loop);
5826 // If `out` is null, we use it for the result, and jump to `done`.
5827 __ b(&done);
5828 __ Bind(&success);
5829 __ LoadImmediate(out, 1);
5830 if (zero.IsLinked()) {
5831 __ b(&done);
5832 }
5833 break;
5834 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005835
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005836 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005837 // Do an exact check.
5838 Label exact_check;
5839 __ cmp(out, ShifterOperand(cls));
5840 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00005841 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00005842 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005843 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005844 // If `out` is null, we use it for the result, and jump to `done`.
5845 __ CompareAndBranchIfZero(out, &done);
5846 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
5847 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
5848 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005849 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005850 __ LoadImmediate(out, 1);
5851 __ b(&done);
5852 break;
5853 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005854
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005855 case TypeCheckKind::kArrayCheck: {
5856 __ cmp(out, ShifterOperand(cls));
5857 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00005858 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5859 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005860 codegen_->AddSlowPath(slow_path);
5861 __ b(slow_path->GetEntryLabel(), NE);
5862 __ LoadImmediate(out, 1);
5863 if (zero.IsLinked()) {
5864 __ b(&done);
5865 }
5866 break;
5867 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005868
Calin Juravle98893e12015-10-02 21:05:03 +01005869 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005870 case TypeCheckKind::kInterfaceCheck: {
5871 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00005872 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00005873 // cases.
5874 //
5875 // We cannot directly call the InstanceofNonTrivial runtime
5876 // entry point without resorting to a type checking slow path
5877 // here (i.e. by calling InvokeRuntime directly), as it would
5878 // require to assign fixed registers for the inputs of this
5879 // HInstanceOf instruction (following the runtime calling
5880 // convention), which might be cluttered by the potential first
5881 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00005882 //
5883 // TODO: Introduce a new runtime entry point taking the object
5884 // to test (instead of its class) as argument, and let it deal
5885 // with the read barrier issues. This will let us refactor this
5886 // case of the `switch` code as it was previously (with a direct
5887 // call to the runtime not using a type checking slow path).
5888 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00005889 DCHECK(locations->OnlyCallsOnSlowPath());
5890 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5891 /* is_fatal */ false);
5892 codegen_->AddSlowPath(slow_path);
5893 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005894 if (zero.IsLinked()) {
5895 __ b(&done);
5896 }
5897 break;
5898 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005899 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005900
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005901 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005902 __ Bind(&zero);
5903 __ LoadImmediate(out, 0);
5904 }
5905
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005906 if (done.IsLinked()) {
5907 __ Bind(&done);
5908 }
5909
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005910 if (slow_path != nullptr) {
5911 __ Bind(slow_path->GetExitLabel());
5912 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005913}
5914
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005915void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005916 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5917 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5918
Roland Levillain3b359c72015-11-17 19:35:12 +00005919 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5920 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005921 case TypeCheckKind::kExactCheck:
5922 case TypeCheckKind::kAbstractClassCheck:
5923 case TypeCheckKind::kClassHierarchyCheck:
5924 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005925 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5926 LocationSummary::kCallOnSlowPath :
5927 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005928 break;
5929 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00005930 case TypeCheckKind::kUnresolvedCheck:
5931 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005932 call_kind = LocationSummary::kCallOnSlowPath;
5933 break;
5934 }
5935
Roland Levillain3b359c72015-11-17 19:35:12 +00005936 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5937 locations->SetInAt(0, Location::RequiresRegister());
5938 locations->SetInAt(1, Location::RequiresRegister());
5939 // Note that TypeCheckSlowPathARM uses this "temp" register too.
5940 locations->AddTemp(Location::RequiresRegister());
5941 // When read barriers are enabled, we need an additional temporary
5942 // register for some cases.
Roland Levillainc9285912015-12-18 10:38:42 +00005943 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005944 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005945 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005946}
5947
5948void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00005949 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005950 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005951 Location obj_loc = locations->InAt(0);
5952 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005953 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00005954 Location temp_loc = locations->GetTemp(0);
5955 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005956 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillainc9285912015-12-18 10:38:42 +00005957 locations->GetTemp(1) :
5958 Location::NoLocation();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005959 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005960 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5961 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5962 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005963
Roland Levillain3b359c72015-11-17 19:35:12 +00005964 bool is_type_check_slow_path_fatal =
5965 (type_check_kind == TypeCheckKind::kExactCheck ||
5966 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5967 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5968 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5969 !instruction->CanThrowIntoCatchBlock();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005970 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00005971 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
5972 is_type_check_slow_path_fatal);
5973 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005974
5975 Label done;
5976 // Avoid null check if we know obj is not null.
5977 if (instruction->MustDoNullCheck()) {
5978 __ CompareAndBranchIfZero(obj, &done);
5979 }
5980
Roland Levillain3b359c72015-11-17 19:35:12 +00005981 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005982 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005983
Roland Levillain3b359c72015-11-17 19:35:12 +00005984 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005985 case TypeCheckKind::kExactCheck:
5986 case TypeCheckKind::kArrayCheck: {
5987 __ cmp(temp, ShifterOperand(cls));
5988 // Jump to slow path for throwing the exception or doing a
5989 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00005990 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005991 break;
5992 }
Roland Levillain3b359c72015-11-17 19:35:12 +00005993
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005994 case TypeCheckKind::kAbstractClassCheck: {
5995 // If the class is abstract, we eagerly fetch the super class of the
5996 // object to avoid doing a comparison we know will fail.
Roland Levillain3b359c72015-11-17 19:35:12 +00005997 Label loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005998 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00005999 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006000 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006001
6002 // If the class reference currently in `temp` is not null, jump
6003 // to the `compare_classes` label to compare it with the checked
6004 // class.
6005 __ CompareAndBranchIfNonZero(temp, &compare_classes);
6006 // Otherwise, jump to the slow path to throw the exception.
6007 //
6008 // But before, move back the object's class into `temp` before
6009 // going into the slow path, as it has been overwritten in the
6010 // meantime.
6011 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006012 GenerateReferenceLoadTwoRegisters(
6013 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006014 __ b(type_check_slow_path->GetEntryLabel());
6015
6016 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006017 __ cmp(temp, ShifterOperand(cls));
6018 __ b(&loop, NE);
6019 break;
6020 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006021
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006022 case TypeCheckKind::kClassHierarchyCheck: {
6023 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006024 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006025 __ Bind(&loop);
6026 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006027 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006028
Roland Levillain3b359c72015-11-17 19:35:12 +00006029 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006030 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006031
6032 // If the class reference currently in `temp` is not null, jump
6033 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006034 __ CompareAndBranchIfNonZero(temp, &loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006035 // Otherwise, jump to the slow path to throw the exception.
6036 //
6037 // But before, move back the object's class into `temp` before
6038 // going into the slow path, as it has been overwritten in the
6039 // meantime.
6040 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006041 GenerateReferenceLoadTwoRegisters(
6042 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006043 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006044 break;
6045 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006046
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006047 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006048 // Do an exact check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006049 Label check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006050 __ cmp(temp, ShifterOperand(cls));
6051 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006052
6053 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006054 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006055 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006056
6057 // If the component type is not null (i.e. the object is indeed
6058 // an array), jump to label `check_non_primitive_component_type`
6059 // to further check that this component type is not a primitive
6060 // type.
6061 __ CompareAndBranchIfNonZero(temp, &check_non_primitive_component_type);
6062 // Otherwise, jump to the slow path to throw the exception.
6063 //
6064 // But before, move back the object's class into `temp` before
6065 // going into the slow path, as it has been overwritten in the
6066 // meantime.
6067 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006068 GenerateReferenceLoadTwoRegisters(
6069 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006070 __ b(type_check_slow_path->GetEntryLabel());
6071
6072 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006073 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006074 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
6075 __ CompareAndBranchIfZero(temp, &done);
6076 // Same comment as above regarding `temp` and the slow path.
6077 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006078 GenerateReferenceLoadTwoRegisters(
6079 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain3b359c72015-11-17 19:35:12 +00006080 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006081 break;
6082 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006083
Calin Juravle98893e12015-10-02 21:05:03 +01006084 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006085 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006086 // We always go into the type check slow path for the unresolved
6087 // and interface check cases.
Roland Levillain3b359c72015-11-17 19:35:12 +00006088 //
6089 // We cannot directly call the CheckCast runtime entry point
6090 // without resorting to a type checking slow path here (i.e. by
6091 // calling InvokeRuntime directly), as it would require to
6092 // assign fixed registers for the inputs of this HInstanceOf
6093 // instruction (following the runtime calling convention), which
6094 // might be cluttered by the potential first read barrier
6095 // emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006096 //
6097 // TODO: Introduce a new runtime entry point taking the object
6098 // to test (instead of its class) as argument, and let it deal
6099 // with the read barrier issues. This will let us refactor this
6100 // case of the `switch` code as it was previously (with a direct
6101 // call to the runtime not using a type checking slow path).
6102 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006103 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006104 break;
6105 }
6106 __ Bind(&done);
6107
Roland Levillain3b359c72015-11-17 19:35:12 +00006108 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006109}
6110
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006111void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6112 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006113 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006114 InvokeRuntimeCallingConvention calling_convention;
6115 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6116}
6117
6118void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006119 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6120 instruction,
6121 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006122 if (instruction->IsEnter()) {
6123 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6124 } else {
6125 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6126 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006127}
6128
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006129void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6130void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6131void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006132
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006133void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006134 LocationSummary* locations =
6135 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6136 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6137 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006138 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006139 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006140 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006141 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006142}
6143
6144void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6145 HandleBitwiseOperation(instruction);
6146}
6147
6148void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6149 HandleBitwiseOperation(instruction);
6150}
6151
6152void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6153 HandleBitwiseOperation(instruction);
6154}
6155
Artem Serov7fc63502016-02-09 17:15:29 +00006156
6157void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6158 LocationSummary* locations =
6159 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6160 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6161 || instruction->GetResultType() == Primitive::kPrimLong);
6162
6163 locations->SetInAt(0, Location::RequiresRegister());
6164 locations->SetInAt(1, Location::RequiresRegister());
6165 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6166}
6167
6168void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6169 LocationSummary* locations = instruction->GetLocations();
6170 Location first = locations->InAt(0);
6171 Location second = locations->InAt(1);
6172 Location out = locations->Out();
6173
6174 if (instruction->GetResultType() == Primitive::kPrimInt) {
6175 Register first_reg = first.AsRegister<Register>();
6176 ShifterOperand second_reg(second.AsRegister<Register>());
6177 Register out_reg = out.AsRegister<Register>();
6178
6179 switch (instruction->GetOpKind()) {
6180 case HInstruction::kAnd:
6181 __ bic(out_reg, first_reg, second_reg);
6182 break;
6183 case HInstruction::kOr:
6184 __ orn(out_reg, first_reg, second_reg);
6185 break;
6186 // There is no EON on arm.
6187 case HInstruction::kXor:
6188 default:
6189 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6190 UNREACHABLE();
6191 }
6192 return;
6193
6194 } else {
6195 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6196 Register first_low = first.AsRegisterPairLow<Register>();
6197 Register first_high = first.AsRegisterPairHigh<Register>();
6198 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6199 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6200 Register out_low = out.AsRegisterPairLow<Register>();
6201 Register out_high = out.AsRegisterPairHigh<Register>();
6202
6203 switch (instruction->GetOpKind()) {
6204 case HInstruction::kAnd:
6205 __ bic(out_low, first_low, second_low);
6206 __ bic(out_high, first_high, second_high);
6207 break;
6208 case HInstruction::kOr:
6209 __ orn(out_low, first_low, second_low);
6210 __ orn(out_high, first_high, second_high);
6211 break;
6212 // There is no EON on arm.
6213 case HInstruction::kXor:
6214 default:
6215 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6216 UNREACHABLE();
6217 }
6218 }
6219}
6220
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006221void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6222 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6223 if (value == 0xffffffffu) {
6224 if (out != first) {
6225 __ mov(out, ShifterOperand(first));
6226 }
6227 return;
6228 }
6229 if (value == 0u) {
6230 __ mov(out, ShifterOperand(0));
6231 return;
6232 }
6233 ShifterOperand so;
6234 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6235 __ and_(out, first, so);
6236 } else {
6237 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6238 __ bic(out, first, ShifterOperand(~value));
6239 }
6240}
6241
6242void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6243 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6244 if (value == 0u) {
6245 if (out != first) {
6246 __ mov(out, ShifterOperand(first));
6247 }
6248 return;
6249 }
6250 if (value == 0xffffffffu) {
6251 __ mvn(out, ShifterOperand(0));
6252 return;
6253 }
6254 ShifterOperand so;
6255 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6256 __ orr(out, first, so);
6257 } else {
6258 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6259 __ orn(out, first, ShifterOperand(~value));
6260 }
6261}
6262
6263void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6264 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6265 if (value == 0u) {
6266 if (out != first) {
6267 __ mov(out, ShifterOperand(first));
6268 }
6269 return;
6270 }
6271 __ eor(out, first, ShifterOperand(value));
6272}
6273
Vladimir Marko59751a72016-08-05 14:37:27 +01006274void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
6275 Location first,
6276 uint64_t value) {
6277 Register out_low = out.AsRegisterPairLow<Register>();
6278 Register out_high = out.AsRegisterPairHigh<Register>();
6279 Register first_low = first.AsRegisterPairLow<Register>();
6280 Register first_high = first.AsRegisterPairHigh<Register>();
6281 uint32_t value_low = Low32Bits(value);
6282 uint32_t value_high = High32Bits(value);
6283 if (value_low == 0u) {
6284 if (out_low != first_low) {
6285 __ mov(out_low, ShifterOperand(first_low));
6286 }
6287 __ AddConstant(out_high, first_high, value_high);
6288 return;
6289 }
6290 __ AddConstantSetFlags(out_low, first_low, value_low);
6291 ShifterOperand so;
6292 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
6293 __ adc(out_high, first_high, so);
6294 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
6295 __ sbc(out_high, first_high, so);
6296 } else {
6297 LOG(FATAL) << "Unexpected constant " << value_high;
6298 UNREACHABLE();
6299 }
6300}
6301
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006302void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
6303 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006304 Location first = locations->InAt(0);
6305 Location second = locations->InAt(1);
6306 Location out = locations->Out();
6307
6308 if (second.IsConstant()) {
6309 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
6310 uint32_t value_low = Low32Bits(value);
6311 if (instruction->GetResultType() == Primitive::kPrimInt) {
6312 Register first_reg = first.AsRegister<Register>();
6313 Register out_reg = out.AsRegister<Register>();
6314 if (instruction->IsAnd()) {
6315 GenerateAndConst(out_reg, first_reg, value_low);
6316 } else if (instruction->IsOr()) {
6317 GenerateOrrConst(out_reg, first_reg, value_low);
6318 } else {
6319 DCHECK(instruction->IsXor());
6320 GenerateEorConst(out_reg, first_reg, value_low);
6321 }
6322 } else {
6323 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6324 uint32_t value_high = High32Bits(value);
6325 Register first_low = first.AsRegisterPairLow<Register>();
6326 Register first_high = first.AsRegisterPairHigh<Register>();
6327 Register out_low = out.AsRegisterPairLow<Register>();
6328 Register out_high = out.AsRegisterPairHigh<Register>();
6329 if (instruction->IsAnd()) {
6330 GenerateAndConst(out_low, first_low, value_low);
6331 GenerateAndConst(out_high, first_high, value_high);
6332 } else if (instruction->IsOr()) {
6333 GenerateOrrConst(out_low, first_low, value_low);
6334 GenerateOrrConst(out_high, first_high, value_high);
6335 } else {
6336 DCHECK(instruction->IsXor());
6337 GenerateEorConst(out_low, first_low, value_low);
6338 GenerateEorConst(out_high, first_high, value_high);
6339 }
6340 }
6341 return;
6342 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006343
6344 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006345 Register first_reg = first.AsRegister<Register>();
6346 ShifterOperand second_reg(second.AsRegister<Register>());
6347 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006348 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006349 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006350 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006351 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006352 } else {
6353 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006354 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006355 }
6356 } else {
6357 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006358 Register first_low = first.AsRegisterPairLow<Register>();
6359 Register first_high = first.AsRegisterPairHigh<Register>();
6360 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6361 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6362 Register out_low = out.AsRegisterPairLow<Register>();
6363 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006364 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006365 __ and_(out_low, first_low, second_low);
6366 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006367 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006368 __ orr(out_low, first_low, second_low);
6369 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006370 } else {
6371 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006372 __ eor(out_low, first_low, second_low);
6373 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006374 }
6375 }
6376}
6377
Roland Levillainc9285912015-12-18 10:38:42 +00006378void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6379 Location out,
6380 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006381 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006382 Register out_reg = out.AsRegister<Register>();
6383 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006384 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006385 if (kUseBakerReadBarrier) {
6386 // Load with fast path based Baker's read barrier.
6387 // /* HeapReference<Object> */ out = *(out + offset)
6388 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006389 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006390 } else {
6391 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006392 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00006393 // in the following move operation, as we will need it for the
6394 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006395 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00006396 // /* HeapReference<Object> */ out = *(out + offset)
6397 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006398 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00006399 }
6400 } else {
6401 // Plain load with no read barrier.
6402 // /* HeapReference<Object> */ out = *(out + offset)
6403 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6404 __ MaybeUnpoisonHeapReference(out_reg);
6405 }
6406}
6407
6408void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6409 Location out,
6410 Location obj,
6411 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006412 Location maybe_temp) {
Roland Levillainc9285912015-12-18 10:38:42 +00006413 Register out_reg = out.AsRegister<Register>();
6414 Register obj_reg = obj.AsRegister<Register>();
6415 if (kEmitCompilerReadBarrier) {
6416 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006417 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00006418 // Load with fast path based Baker's read barrier.
6419 // /* HeapReference<Object> */ out = *(obj + offset)
6420 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006421 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00006422 } else {
6423 // Load with slow path based read barrier.
6424 // /* HeapReference<Object> */ out = *(obj + offset)
6425 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6426 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6427 }
6428 } else {
6429 // Plain load with no read barrier.
6430 // /* HeapReference<Object> */ out = *(obj + offset)
6431 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6432 __ MaybeUnpoisonHeapReference(out_reg);
6433 }
6434}
6435
6436void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
6437 Location root,
6438 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006439 uint32_t offset,
6440 bool requires_read_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006441 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006442 if (requires_read_barrier) {
6443 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00006444 if (kUseBakerReadBarrier) {
6445 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6446 // Baker's read barrier are used:
6447 //
6448 // root = obj.field;
6449 // if (Thread::Current()->GetIsGcMarking()) {
6450 // root = ReadBarrier::Mark(root)
6451 // }
6452
6453 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6454 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6455 static_assert(
6456 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6457 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6458 "have different sizes.");
6459 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6460 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6461 "have different sizes.");
6462
Vladimir Marko953437b2016-08-24 08:30:46 +00006463 // Slow path marking the GC root `root`.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006464 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006465 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, root);
Roland Levillainc9285912015-12-18 10:38:42 +00006466 codegen_->AddSlowPath(slow_path);
6467
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006468 // IP = Thread::Current()->GetIsGcMarking()
Roland Levillainc9285912015-12-18 10:38:42 +00006469 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006470 kLoadWord, IP, TR, Thread::IsGcMarkingOffset<kArmPointerSize>().Int32Value());
Roland Levillainc9285912015-12-18 10:38:42 +00006471 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
6472 __ Bind(slow_path->GetExitLabel());
6473 } else {
6474 // GC root loaded through a slow path for read barriers other
6475 // than Baker's.
6476 // /* GcRoot<mirror::Object>* */ root = obj + offset
6477 __ AddConstant(root_reg, obj, offset);
6478 // /* mirror::Object* */ root = root->Read()
6479 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6480 }
6481 } else {
6482 // Plain GC root load with no read barrier.
6483 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6484 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6485 // Note that GC roots are not affected by heap poisoning, thus we
6486 // do not have to unpoison `root_reg` here.
6487 }
6488}
6489
6490void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6491 Location ref,
6492 Register obj,
6493 uint32_t offset,
6494 Location temp,
6495 bool needs_null_check) {
6496 DCHECK(kEmitCompilerReadBarrier);
6497 DCHECK(kUseBakerReadBarrier);
6498
6499 // /* HeapReference<Object> */ ref = *(obj + offset)
6500 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01006501 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00006502 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006503 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006504}
6505
6506void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6507 Location ref,
6508 Register obj,
6509 uint32_t data_offset,
6510 Location index,
6511 Location temp,
6512 bool needs_null_check) {
6513 DCHECK(kEmitCompilerReadBarrier);
6514 DCHECK(kUseBakerReadBarrier);
6515
Roland Levillainbfea3352016-06-23 13:48:47 +01006516 static_assert(
6517 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6518 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006519 // /* HeapReference<Object> */ ref =
6520 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01006521 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00006522 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01006523 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00006524}
6525
6526void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6527 Location ref,
6528 Register obj,
6529 uint32_t offset,
6530 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01006531 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00006532 Location temp,
6533 bool needs_null_check) {
6534 DCHECK(kEmitCompilerReadBarrier);
6535 DCHECK(kUseBakerReadBarrier);
6536
6537 // In slow path based read barriers, the read barrier call is
6538 // inserted after the original load. However, in fast path based
6539 // Baker's read barriers, we need to perform the load of
6540 // mirror::Object::monitor_ *before* the original reference load.
6541 // This load-load ordering is required by the read barrier.
6542 // The fast path/slow path (for Baker's algorithm) should look like:
6543 //
6544 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6545 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6546 // HeapReference<Object> ref = *src; // Original reference load.
6547 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6548 // if (is_gray) {
6549 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6550 // }
6551 //
6552 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006553 // slightly more complex as it performs additional checks that we do
6554 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00006555
6556 Register ref_reg = ref.AsRegister<Register>();
6557 Register temp_reg = temp.AsRegister<Register>();
6558 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6559
6560 // /* int32_t */ monitor = obj->monitor_
6561 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6562 if (needs_null_check) {
6563 MaybeRecordImplicitNullCheck(instruction);
6564 }
6565 // /* LockWord */ lock_word = LockWord(monitor)
6566 static_assert(sizeof(LockWord) == sizeof(int32_t),
6567 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006568
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006569 // Introduce a dependency on the lock_word including the rb_state,
6570 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00006571 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01006572 // `obj` is unchanged by this operation, but its value now depends
6573 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006574 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00006575
6576 // The actual reference load.
6577 if (index.IsValid()) {
Roland Levillainbfea3352016-06-23 13:48:47 +01006578 // Load types involving an "index": ArrayGet and
6579 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
6580 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00006581 if (index.IsConstant()) {
6582 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01006583 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006584 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6585 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01006586 // Handle the special case of the
6587 // UnsafeGetObject/UnsafeGetObjectVolatile intrinsics, which use
6588 // a register pair as index ("long offset"), of which only the low
6589 // part contains data.
6590 Register index_reg = index.IsRegisterPair()
6591 ? index.AsRegisterPairLow<Register>()
6592 : index.AsRegister<Register>();
6593 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00006594 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
6595 }
6596 } else {
6597 // /* HeapReference<Object> */ ref = *(obj + offset)
6598 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6599 }
6600
6601 // Object* ref = ref_addr->AsMirrorPtr()
6602 __ MaybeUnpoisonHeapReference(ref_reg);
6603
Vladimir Marko953437b2016-08-24 08:30:46 +00006604 // Slow path marking the object `ref` when it is gray.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006605 SlowPathCodeARM* slow_path =
Roland Levillain02b75802016-07-13 11:54:35 +01006606 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
Roland Levillainc9285912015-12-18 10:38:42 +00006607 AddSlowPath(slow_path);
6608
6609 // if (rb_state == ReadBarrier::gray_ptr_)
6610 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01006611 // Given the numeric representation, it's enough to check the low bit of the
6612 // rb_state. We do that by shifting the bit out of the lock word with LSRS
6613 // which can be a 16-bit instruction unlike the TST immediate.
6614 static_assert(ReadBarrier::white_ptr_ == 0, "Expecting white to have value 0");
6615 static_assert(ReadBarrier::gray_ptr_ == 1, "Expecting gray to have value 1");
6616 static_assert(ReadBarrier::black_ptr_ == 2, "Expecting black to have value 2");
6617 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
6618 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00006619 __ Bind(slow_path->GetExitLabel());
6620}
6621
6622void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
6623 Location out,
6624 Location ref,
6625 Location obj,
6626 uint32_t offset,
6627 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006628 DCHECK(kEmitCompilerReadBarrier);
6629
Roland Levillainc9285912015-12-18 10:38:42 +00006630 // Insert a slow path based read barrier *after* the reference load.
6631 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006632 // If heap poisoning is enabled, the unpoisoning of the loaded
6633 // reference will be carried out by the runtime within the slow
6634 // path.
6635 //
6636 // Note that `ref` currently does not get unpoisoned (when heap
6637 // poisoning is enabled), which is alright as the `ref` argument is
6638 // not used by the artReadBarrierSlow entry point.
6639 //
6640 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006641 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00006642 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
6643 AddSlowPath(slow_path);
6644
Roland Levillain3b359c72015-11-17 19:35:12 +00006645 __ b(slow_path->GetEntryLabel());
6646 __ Bind(slow_path->GetExitLabel());
6647}
6648
Roland Levillainc9285912015-12-18 10:38:42 +00006649void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6650 Location out,
6651 Location ref,
6652 Location obj,
6653 uint32_t offset,
6654 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006655 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00006656 // Baker's read barriers shall be handled by the fast path
6657 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
6658 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006659 // If heap poisoning is enabled, unpoisoning will be taken care of
6660 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00006661 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00006662 } else if (kPoisonHeapReferences) {
6663 __ UnpoisonHeapReference(out.AsRegister<Register>());
6664 }
6665}
6666
Roland Levillainc9285912015-12-18 10:38:42 +00006667void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6668 Location out,
6669 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006670 DCHECK(kEmitCompilerReadBarrier);
6671
Roland Levillainc9285912015-12-18 10:38:42 +00006672 // Insert a slow path based read barrier *after* the GC root load.
6673 //
Roland Levillain3b359c72015-11-17 19:35:12 +00006674 // Note that GC roots are not affected by heap poisoning, so we do
6675 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006676 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006677 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
6678 AddSlowPath(slow_path);
6679
Roland Levillain3b359c72015-11-17 19:35:12 +00006680 __ b(slow_path->GetEntryLabel());
6681 __ Bind(slow_path->GetExitLabel());
6682}
6683
Vladimir Markodc151b22015-10-15 18:02:30 +01006684HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
6685 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
6686 MethodReference target_method) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006687 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
6688 // We disable pc-relative load when there is an irreducible loop, as the optimization
6689 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006690 // TODO: Create as many ArmDexCacheArraysBase instructions as needed for methods
6691 // with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006692 if (GetGraph()->HasIrreducibleLoops() &&
6693 (dispatch_info.method_load_kind ==
6694 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
6695 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
6696 }
6697
6698 if (dispatch_info.code_ptr_location == HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) {
Vladimir Markodc151b22015-10-15 18:02:30 +01006699 const DexFile& outer_dex_file = GetGraph()->GetDexFile();
6700 if (&outer_dex_file != target_method.dex_file) {
6701 // Calls across dex files are more likely to exceed the available BL range,
6702 // so use absolute patch with fixup if available and kCallArtMethod otherwise.
6703 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location =
6704 (desired_dispatch_info.method_load_kind ==
6705 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup)
6706 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup
6707 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
6708 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006709 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01006710 code_ptr_location,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006711 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01006712 0u
6713 };
6714 }
6715 }
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00006716 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01006717}
6718
Vladimir Markob4536b72015-11-24 13:45:23 +00006719Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
6720 Register temp) {
6721 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
6722 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
6723 if (!invoke->GetLocations()->Intrinsified()) {
6724 return location.AsRegister<Register>();
6725 }
6726 // For intrinsics we allow any location, so it may be on the stack.
6727 if (!location.IsRegister()) {
6728 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
6729 return temp;
6730 }
6731 // For register locations, check if the register was saved. If so, get it from the stack.
6732 // Note: There is a chance that the register was saved but not overwritten, so we could
6733 // save one load. However, since this is just an intrinsic slow path we prefer this
6734 // simple and more robust approach rather that trying to determine if that's the case.
6735 SlowPathCode* slow_path = GetCurrentSlowPath();
6736 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
6737 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
6738 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
6739 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
6740 return temp;
6741 }
6742 return location.AsRegister<Register>();
6743}
6744
Nicolas Geoffray38207af2015-06-01 15:46:22 +01006745void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00006746 // For better instruction scheduling we load the direct code pointer before the method pointer.
Vladimir Marko58155012015-08-19 12:49:41 +00006747 switch (invoke->GetCodePtrLocation()) {
Vladimir Marko58155012015-08-19 12:49:41 +00006748 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6749 // LR = code address from literal pool with link-time patch.
6750 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod()));
Vladimir Marko58155012015-08-19 12:49:41 +00006751 break;
6752 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6753 // LR = invoke->GetDirectCodePtr();
6754 __ LoadImmediate(LR, invoke->GetDirectCodePtr());
Vladimir Marko58155012015-08-19 12:49:41 +00006755 break;
6756 default:
6757 break;
6758 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006759
Vladimir Marko58155012015-08-19 12:49:41 +00006760 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
6761 switch (invoke->GetMethodLoadKind()) {
6762 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
6763 // temp = thread->string_init_entrypoint
6764 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset());
6765 break;
6766 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00006767 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006768 break;
6769 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
6770 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
6771 break;
6772 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
6773 __ LoadLiteral(temp.AsRegister<Register>(),
6774 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod()));
6775 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00006776 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
6777 HArmDexCacheArraysBase* base =
6778 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
6779 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
6780 temp.AsRegister<Register>());
6781 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
6782 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
6783 break;
6784 }
Vladimir Marko58155012015-08-19 12:49:41 +00006785 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00006786 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00006787 Register method_reg;
6788 Register reg = temp.AsRegister<Register>();
6789 if (current_method.IsRegister()) {
6790 method_reg = current_method.AsRegister<Register>();
6791 } else {
6792 DCHECK(invoke->GetLocations()->Intrinsified());
6793 DCHECK(!current_method.IsValid());
6794 method_reg = reg;
6795 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
6796 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006797 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
6798 __ LoadFromOffset(kLoadWord,
6799 reg,
6800 method_reg,
6801 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01006802 // temp = temp[index_in_cache];
6803 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
6804 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00006805 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
6806 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01006807 }
Vladimir Marko58155012015-08-19 12:49:41 +00006808 }
6809
6810 switch (invoke->GetCodePtrLocation()) {
6811 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
6812 __ bl(GetFrameEntryLabel());
6813 break;
6814 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative:
Vladimir Markodc151b22015-10-15 18:02:30 +01006815 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07006816 __ BindTrackedLabel(&relative_call_patches_.back().label);
Vladimir Markodc151b22015-10-15 18:02:30 +01006817 // Arbitrarily branch to the BL itself, override at link time.
6818 __ bl(&relative_call_patches_.back().label);
6819 break;
Vladimir Marko58155012015-08-19 12:49:41 +00006820 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
6821 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
6822 // LR prepared above for better instruction scheduling.
Vladimir Marko58155012015-08-19 12:49:41 +00006823 // LR()
6824 __ blx(LR);
6825 break;
6826 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
6827 // LR = callee_method->entry_point_from_quick_compiled_code_
6828 __ LoadFromOffset(
6829 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07006830 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00006831 // LR()
6832 __ blx(LR);
6833 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006834 }
6835
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08006836 DCHECK(!IsLeafMethod());
6837}
6838
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006839void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
6840 Register temp = temp_location.AsRegister<Register>();
6841 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
6842 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006843
6844 // Use the calling convention instead of the location of the receiver, as
6845 // intrinsics may have put the receiver in a different register. In the intrinsics
6846 // slow path, the arguments have been moved to the right place, so here we are
6847 // guaranteed that the receiver is the first register of the calling convention.
6848 InvokeDexCallingConvention calling_convention;
6849 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006850 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00006851 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00006852 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006853 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00006854 // Instead of simply (possibly) unpoisoning `temp` here, we should
6855 // emit a read barrier for the previous class reference load.
6856 // However this is not required in practice, as this is an
6857 // intermediate/temporary reference and because the current
6858 // concurrent copying collector keeps the from-space memory
6859 // intact/accessible until the end of the marking phase (the
6860 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006861 __ MaybeUnpoisonHeapReference(temp);
6862 // temp = temp->GetMethodAt(method_offset);
6863 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006864 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00006865 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6866 // LR = temp->GetEntryPoint();
6867 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
6868 // LR();
6869 __ blx(LR);
6870}
6871
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006872CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
6873 const DexFile& dex_file, uint32_t string_index) {
6874 return NewPcRelativePatch(dex_file, string_index, &pc_relative_string_patches_);
6875}
6876
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006877CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
6878 const DexFile& dex_file, uint32_t type_index) {
6879 return NewPcRelativePatch(dex_file, type_index, &pc_relative_type_patches_);
6880}
6881
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006882CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
6883 const DexFile& dex_file, uint32_t element_offset) {
6884 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
6885}
6886
6887CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
6888 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
6889 patches->emplace_back(dex_file, offset_or_index);
6890 return &patches->back();
6891}
6892
6893Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
6894 uint32_t string_index) {
6895 return boot_image_string_patches_.GetOrCreate(
6896 StringReference(&dex_file, string_index),
6897 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6898}
6899
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006900Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
6901 uint32_t type_index) {
6902 return boot_image_type_patches_.GetOrCreate(
6903 TypeReference(&dex_file, type_index),
6904 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
6905}
6906
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006907Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
6908 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
6909 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
6910 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
6911}
6912
6913Literal* CodeGeneratorARM::DeduplicateDexCacheAddressLiteral(uint32_t address) {
6914 return DeduplicateUint32Literal(address, &uint32_literals_);
6915}
6916
Vladimir Marko58155012015-08-19 12:49:41 +00006917void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
6918 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00006919 size_t size =
6920 method_patches_.size() +
6921 call_patches_.size() +
6922 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006923 /* MOVW+MOVT for each base */ 2u * pc_relative_dex_cache_patches_.size() +
6924 boot_image_string_patches_.size() +
6925 /* MOVW+MOVT for each base */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006926 boot_image_type_patches_.size() +
6927 /* MOVW+MOVT for each base */ 2u * pc_relative_type_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006928 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00006929 linker_patches->reserve(size);
6930 for (const auto& entry : method_patches_) {
6931 const MethodReference& target_method = entry.first;
6932 Literal* literal = entry.second;
6933 DCHECK(literal->GetLabel()->IsBound());
6934 uint32_t literal_offset = literal->GetLabel()->Position();
6935 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
6936 target_method.dex_file,
6937 target_method.dex_method_index));
6938 }
6939 for (const auto& entry : call_patches_) {
6940 const MethodReference& target_method = entry.first;
6941 Literal* literal = entry.second;
6942 DCHECK(literal->GetLabel()->IsBound());
6943 uint32_t literal_offset = literal->GetLabel()->Position();
6944 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset,
6945 target_method.dex_file,
6946 target_method.dex_method_index));
6947 }
6948 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
6949 uint32_t literal_offset = info.label.Position();
6950 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
6951 info.target_method.dex_file,
6952 info.target_method.dex_method_index));
6953 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006954 for (const PcRelativePatchInfo& info : pc_relative_dex_cache_patches_) {
6955 const DexFile& dex_file = info.target_dex_file;
6956 size_t base_element_offset = info.offset_or_index;
6957 DCHECK(info.add_pc_label.IsBound());
6958 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006959 // Add MOVW patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006960 DCHECK(info.movw_label.IsBound());
6961 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006962 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movw_offset,
6963 &dex_file,
6964 add_pc_offset,
6965 base_element_offset));
6966 // Add MOVT patch.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006967 DCHECK(info.movt_label.IsBound());
6968 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
Vladimir Markob4536b72015-11-24 13:45:23 +00006969 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(movt_offset,
6970 &dex_file,
6971 add_pc_offset,
6972 base_element_offset));
6973 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006974 for (const auto& entry : boot_image_string_patches_) {
6975 const StringReference& target_string = entry.first;
6976 Literal* literal = entry.second;
6977 DCHECK(literal->GetLabel()->IsBound());
6978 uint32_t literal_offset = literal->GetLabel()->Position();
6979 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
6980 target_string.dex_file,
6981 target_string.string_index));
6982 }
6983 for (const PcRelativePatchInfo& info : pc_relative_string_patches_) {
6984 const DexFile& dex_file = info.target_dex_file;
6985 uint32_t string_index = info.offset_or_index;
6986 DCHECK(info.add_pc_label.IsBound());
6987 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
6988 // Add MOVW patch.
6989 DCHECK(info.movw_label.IsBound());
6990 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
6991 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movw_offset,
6992 &dex_file,
6993 add_pc_offset,
6994 string_index));
6995 // Add MOVT patch.
6996 DCHECK(info.movt_label.IsBound());
6997 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
6998 linker_patches->push_back(LinkerPatch::RelativeStringPatch(movt_offset,
6999 &dex_file,
7000 add_pc_offset,
7001 string_index));
7002 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007003 for (const auto& entry : boot_image_type_patches_) {
7004 const TypeReference& target_type = entry.first;
7005 Literal* literal = entry.second;
7006 DCHECK(literal->GetLabel()->IsBound());
7007 uint32_t literal_offset = literal->GetLabel()->Position();
7008 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7009 target_type.dex_file,
7010 target_type.type_index));
7011 }
7012 for (const PcRelativePatchInfo& info : pc_relative_type_patches_) {
7013 const DexFile& dex_file = info.target_dex_file;
7014 uint32_t type_index = info.offset_or_index;
7015 DCHECK(info.add_pc_label.IsBound());
7016 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7017 // Add MOVW patch.
7018 DCHECK(info.movw_label.IsBound());
7019 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7020 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movw_offset,
7021 &dex_file,
7022 add_pc_offset,
7023 type_index));
7024 // Add MOVT patch.
7025 DCHECK(info.movt_label.IsBound());
7026 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7027 linker_patches->push_back(LinkerPatch::RelativeTypePatch(movt_offset,
7028 &dex_file,
7029 add_pc_offset,
7030 type_index));
7031 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007032 for (const auto& entry : boot_image_address_patches_) {
7033 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7034 Literal* literal = entry.second;
7035 DCHECK(literal->GetLabel()->IsBound());
7036 uint32_t literal_offset = literal->GetLabel()->Position();
7037 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7038 }
7039}
7040
7041Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7042 return map->GetOrCreate(
7043 value,
7044 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007045}
7046
7047Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7048 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007049 return map->GetOrCreate(
7050 target_method,
7051 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007052}
7053
7054Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) {
7055 return DeduplicateMethodLiteral(target_method, &method_patches_);
7056}
7057
7058Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) {
7059 return DeduplicateMethodLiteral(target_method, &call_patches_);
7060}
7061
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007062void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7063 LocationSummary* locations =
7064 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7065 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7066 Location::RequiresRegister());
7067 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7068 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7069 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7070}
7071
7072void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7073 LocationSummary* locations = instr->GetLocations();
7074 Register res = locations->Out().AsRegister<Register>();
7075 Register accumulator =
7076 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7077 Register mul_left =
7078 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7079 Register mul_right =
7080 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7081
7082 if (instr->GetOpKind() == HInstruction::kAdd) {
7083 __ mla(res, mul_left, mul_right, accumulator);
7084 } else {
7085 __ mls(res, mul_left, mul_right, accumulator);
7086 }
7087}
7088
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007089void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007090 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007091 LOG(FATAL) << "Unreachable";
7092}
7093
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007094void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007095 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007096 LOG(FATAL) << "Unreachable";
7097}
7098
Mark Mendellfe57faa2015-09-18 09:26:15 -04007099// Simple implementation of packed switch - generate cascaded compare/jumps.
7100void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7101 LocationSummary* locations =
7102 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7103 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007104 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007105 codegen_->GetAssembler()->IsThumb()) {
7106 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7107 if (switch_instr->GetStartValue() != 0) {
7108 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7109 }
7110 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007111}
7112
7113void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7114 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007115 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007116 LocationSummary* locations = switch_instr->GetLocations();
7117 Register value_reg = locations->InAt(0).AsRegister<Register>();
7118 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7119
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007120 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007121 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007122 Register temp_reg = IP;
7123 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7124 // the immediate, because IP is used as the destination register. For the other
7125 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7126 // and they can be encoded in the instruction without making use of IP register.
7127 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7128
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007129 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007130 // Jump to successors[0] if value == lower_bound.
7131 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7132 int32_t last_index = 0;
7133 for (; num_entries - last_index > 2; last_index += 2) {
7134 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7135 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7136 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7137 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7138 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7139 }
7140 if (num_entries - last_index == 2) {
7141 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007142 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007143 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007144 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007145
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007146 // And the default for any other value.
7147 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7148 __ b(codegen_->GetLabelOf(default_block));
7149 }
7150 } else {
7151 // Create a table lookup.
7152 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7153
7154 // Materialize a pointer to the switch table
7155 std::vector<Label*> labels(num_entries);
7156 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7157 for (uint32_t i = 0; i < num_entries; i++) {
7158 labels[i] = codegen_->GetLabelOf(successors[i]);
7159 }
7160 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7161
7162 // Remove the bias.
7163 Register key_reg;
7164 if (lower_bound != 0) {
7165 key_reg = locations->GetTemp(1).AsRegister<Register>();
7166 __ AddConstant(key_reg, value_reg, -lower_bound);
7167 } else {
7168 key_reg = value_reg;
7169 }
7170
7171 // Check whether the value is in the table, jump to default block if not.
7172 __ CmpConstant(key_reg, num_entries - 1);
7173 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7174
7175 // Load the displacement from the table.
7176 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7177
7178 // Dispatch is a direct add to the PC (for Thumb2).
7179 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007180 }
7181}
7182
Vladimir Markob4536b72015-11-24 13:45:23 +00007183void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7184 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7185 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007186}
7187
7188void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7189 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007190 CodeGeneratorARM::PcRelativePatchInfo* labels =
7191 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007192 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007193 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007194 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007195 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007196 __ BindTrackedLabel(&labels->add_pc_label);
7197 __ add(base_reg, base_reg, ShifterOperand(PC));
7198}
7199
Andreas Gampe85b62f22015-09-09 13:15:38 -07007200void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7201 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007202 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007203 return;
7204 }
7205
7206 DCHECK_NE(type, Primitive::kPrimVoid);
7207
7208 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7209 if (return_loc.Equals(trg)) {
7210 return;
7211 }
7212
7213 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7214 // with the last branch.
7215 if (type == Primitive::kPrimLong) {
7216 HParallelMove parallel_move(GetGraph()->GetArena());
7217 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7218 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7219 GetMoveResolver()->EmitNativeCode(&parallel_move);
7220 } else if (type == Primitive::kPrimDouble) {
7221 HParallelMove parallel_move(GetGraph()->GetArena());
7222 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7223 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7224 GetMoveResolver()->EmitNativeCode(&parallel_move);
7225 } else {
7226 // Let the parallel move resolver take care of all of this.
7227 HParallelMove parallel_move(GetGraph()->GetArena());
7228 parallel_move.AddMove(return_loc, trg, type, nullptr);
7229 GetMoveResolver()->EmitNativeCode(&parallel_move);
7230 }
7231}
7232
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007233void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7234 LocationSummary* locations =
7235 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7236 locations->SetInAt(0, Location::RequiresRegister());
7237 locations->SetOut(Location::RequiresRegister());
7238}
7239
7240void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7241 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007242 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007243 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007244 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007245 __ LoadFromOffset(kLoadWord,
7246 locations->Out().AsRegister<Register>(),
7247 locations->InAt(0).AsRegister<Register>(),
7248 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007249 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007250 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007251 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007252 __ LoadFromOffset(kLoadWord,
7253 locations->Out().AsRegister<Register>(),
7254 locations->InAt(0).AsRegister<Register>(),
7255 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7256 __ LoadFromOffset(kLoadWord,
7257 locations->Out().AsRegister<Register>(),
7258 locations->Out().AsRegister<Register>(),
7259 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007260 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007261}
7262
Roland Levillain4d027112015-07-01 15:41:14 +01007263#undef __
7264#undef QUICK_ENTRY_POINT
7265
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007266} // namespace arm
7267} // namespace art