blob: 710ca7ad455d85ccaf96e44f182e043e7dfa0621 [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"
Anton Kirilov74234da2017-01-13 14:42:47 +000022#include "common_arm.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070024#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080026#include "intrinsics.h"
27#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070030#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031#include "utils/arm/assembler_arm.h"
32#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000033#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037
Roland Levillain3b359c72015-11-17 19:35:12 +000038template<class MirrorType>
39class GcRoot;
40
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041namespace arm {
42
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000043static bool ExpectedPairLayout(Location location) {
44 // We expected this for both core and fpu register pairs.
45 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
46}
47
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010049static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050
David Brazdil58282f42016-01-14 12:45:10 +000051static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000052static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070053 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000054static constexpr SRegister kFpuCalleeSaves[] =
55 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000057// D31 cannot be split into two S registers, and the register allocator only works on
58// S registers. Therefore there is no need to block it.
59static constexpr DRegister DTMP = D31;
60
Vladimir Markof3e0ee22015-12-17 15:23:13 +000061static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070062
Roland Levillain7cbd27f2016-08-11 23:53:33 +010063// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
64#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070065#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066
Artem Serovf4d6aee2016-07-11 10:41:45 +010067static constexpr int kRegListThreshold = 4;
68
Artem Serovd300d8f2016-07-15 14:00:56 +010069// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
70// for each live D registers they treat two corresponding S registers as live ones.
71//
72// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
73// from a list of contiguous S registers a list of contiguous D registers (processing first/last
74// S registers corner cases) and save/restore this new list treating them as D registers.
75// - decreasing code size
76// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
77// restored and then used in regular non SlowPath code as D register.
78//
79// For the following example (v means the S register is live):
80// D names: | D0 | D1 | D2 | D4 | ...
81// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
82// Live? | | v | v | v | v | v | v | | ...
83//
84// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
85// as D registers.
86static size_t SaveContiguousSRegisterList(size_t first,
87 size_t last,
88 CodeGenerator* codegen,
89 size_t stack_offset) {
90 DCHECK_LE(first, last);
91 if ((first == last) && (first == 0)) {
92 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first);
93 return stack_offset;
94 }
95 if (first % 2 == 1) {
96 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first++);
97 }
98
99 bool save_last = false;
100 if (last % 2 == 0) {
101 save_last = true;
102 --last;
103 }
104
105 if (first < last) {
106 DRegister d_reg = static_cast<DRegister>(first / 2);
107 DCHECK_EQ((last - first + 1) % 2, 0u);
108 size_t number_of_d_regs = (last - first + 1) / 2;
109
110 if (number_of_d_regs == 1) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100111 __ StoreDToOffset(d_reg, SP, stack_offset);
Artem Serovd300d8f2016-07-15 14:00:56 +0100112 } else if (number_of_d_regs > 1) {
113 __ add(IP, SP, ShifterOperand(stack_offset));
114 __ vstmiad(IP, d_reg, number_of_d_regs);
115 }
116 stack_offset += number_of_d_regs * kArmWordSize * 2;
117 }
118
119 if (save_last) {
120 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, last + 1);
121 }
122
123 return stack_offset;
124}
125
126static size_t RestoreContiguousSRegisterList(size_t first,
127 size_t last,
128 CodeGenerator* codegen,
129 size_t stack_offset) {
130 DCHECK_LE(first, last);
131 if ((first == last) && (first == 0)) {
132 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first);
133 return stack_offset;
134 }
135 if (first % 2 == 1) {
136 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first++);
137 }
138
139 bool restore_last = false;
140 if (last % 2 == 0) {
141 restore_last = true;
142 --last;
143 }
144
145 if (first < last) {
146 DRegister d_reg = static_cast<DRegister>(first / 2);
147 DCHECK_EQ((last - first + 1) % 2, 0u);
148 size_t number_of_d_regs = (last - first + 1) / 2;
149 if (number_of_d_regs == 1) {
150 __ LoadDFromOffset(d_reg, SP, stack_offset);
151 } else if (number_of_d_regs > 1) {
152 __ add(IP, SP, ShifterOperand(stack_offset));
153 __ vldmiad(IP, d_reg, number_of_d_regs);
154 }
155 stack_offset += number_of_d_regs * kArmWordSize * 2;
156 }
157
158 if (restore_last) {
159 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, last + 1);
160 }
161
162 return stack_offset;
163}
164
Artem Serovf4d6aee2016-07-11 10:41:45 +0100165void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
166 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
167 size_t orig_offset = stack_offset;
168
169 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
170 for (uint32_t i : LowToHighBits(core_spills)) {
171 // If the register holds an object, update the stack mask.
172 if (locations->RegisterContainsObject(i)) {
173 locations->SetStackBit(stack_offset / kVRegSize);
174 }
175 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
176 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
177 saved_core_stack_offsets_[i] = stack_offset;
178 stack_offset += kArmWordSize;
179 }
180
181 int reg_num = POPCOUNT(core_spills);
182 if (reg_num != 0) {
183 if (reg_num > kRegListThreshold) {
184 __ StoreList(RegList(core_spills), orig_offset);
185 } else {
186 stack_offset = orig_offset;
187 for (uint32_t i : LowToHighBits(core_spills)) {
188 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
189 }
190 }
191 }
192
Artem Serovd300d8f2016-07-15 14:00:56 +0100193 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
194 orig_offset = stack_offset;
Vladimir Marko804b03f2016-09-14 16:26:36 +0100195 for (uint32_t i : LowToHighBits(fp_spills)) {
Artem Serovf4d6aee2016-07-11 10:41:45 +0100196 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
197 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serovd300d8f2016-07-15 14:00:56 +0100198 stack_offset += kArmWordSize;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100199 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100200
201 stack_offset = orig_offset;
202 while (fp_spills != 0u) {
203 uint32_t begin = CTZ(fp_spills);
204 uint32_t tmp = fp_spills + (1u << begin);
205 fp_spills &= tmp; // Clear the contiguous range of 1s.
206 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
207 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
208 }
209 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100210}
211
212void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
213 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
214 size_t orig_offset = stack_offset;
215
216 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
217 for (uint32_t i : LowToHighBits(core_spills)) {
218 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
219 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
220 stack_offset += kArmWordSize;
221 }
222
223 int reg_num = POPCOUNT(core_spills);
224 if (reg_num != 0) {
225 if (reg_num > kRegListThreshold) {
226 __ LoadList(RegList(core_spills), orig_offset);
227 } else {
228 stack_offset = orig_offset;
229 for (uint32_t i : LowToHighBits(core_spills)) {
230 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
231 }
232 }
233 }
234
Artem Serovd300d8f2016-07-15 14:00:56 +0100235 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
236 while (fp_spills != 0u) {
237 uint32_t begin = CTZ(fp_spills);
238 uint32_t tmp = fp_spills + (1u << begin);
239 fp_spills &= tmp; // Clear the contiguous range of 1s.
240 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
241 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
Artem Serovf4d6aee2016-07-11 10:41:45 +0100242 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100243 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100244}
245
246class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100247 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100248 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100249
Alexandre Rames67555f72014-11-18 10:55:16 +0000250 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100251 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100252 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000253 if (instruction_->CanThrowIntoCatchBlock()) {
254 // Live registers will be restored in the catch block if caught.
255 SaveLiveRegisters(codegen, instruction_->GetLocations());
256 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100257 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
258 instruction_,
259 instruction_->GetDexPc(),
260 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000261 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100262 }
263
Alexandre Rames8158f282015-08-07 10:26:17 +0100264 bool IsFatal() const OVERRIDE { return true; }
265
Alexandre Rames9931f312015-06-19 14:47:01 +0100266 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
267
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100268 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100269 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
270};
271
Artem Serovf4d6aee2016-07-11 10:41:45 +0100272class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000273 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100274 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000275
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000277 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
278 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100279 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000280 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000281 }
282
Alexandre Rames8158f282015-08-07 10:26:17 +0100283 bool IsFatal() const OVERRIDE { return true; }
284
Alexandre Rames9931f312015-06-19 14:47:01 +0100285 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
286
Calin Juravled0d48522014-11-04 16:40:20 +0000287 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000288 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
289};
290
Artem Serovf4d6aee2016-07-11 10:41:45 +0100291class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000292 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000293 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100294 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000295
Alexandre Rames67555f72014-11-18 10:55:16 +0000296 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100297 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000298 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100299 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000300 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100301 if (successor_ == nullptr) {
302 __ b(GetReturnLabel());
303 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100304 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100305 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000306 }
307
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100308 Label* GetReturnLabel() {
309 DCHECK(successor_ == nullptr);
310 return &return_label_;
311 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000312
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100313 HBasicBlock* GetSuccessor() const {
314 return successor_;
315 }
316
Alexandre Rames9931f312015-06-19 14:47:01 +0100317 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
318
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000319 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100320 // If not null, the block to branch to after the suspend check.
321 HBasicBlock* const successor_;
322
323 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000324 Label return_label_;
325
326 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
327};
328
Artem Serovf4d6aee2016-07-11 10:41:45 +0100329class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100330 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100331 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100332 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100333
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100335 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100336 LocationSummary* locations = instruction_->GetLocations();
337
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100338 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000339 if (instruction_->CanThrowIntoCatchBlock()) {
340 // Live registers will be restored in the catch block if caught.
341 SaveLiveRegisters(codegen, instruction_->GetLocations());
342 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000343 // We're moving two locations to locations that could overlap, so we need a parallel
344 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100345 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000346 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000348 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100349 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100350 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100351 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
352 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100353 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
354 ? kQuickThrowStringBounds
355 : kQuickThrowArrayBounds;
356 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100357 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000358 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100359 }
360
Alexandre Rames8158f282015-08-07 10:26:17 +0100361 bool IsFatal() const OVERRIDE { return true; }
362
Alexandre Rames9931f312015-06-19 14:47:01 +0100363 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
364
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100365 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100366 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
367};
368
Artem Serovf4d6aee2016-07-11 10:41:45 +0100369class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100370 public:
Vladimir Markoea4c1262017-02-06 19:59:33 +0000371 LoadClassSlowPathARM(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000372 : SlowPathCodeARM(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000373 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
374 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100375
Alexandre Rames67555f72014-11-18 10:55:16 +0000376 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000377 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000378 Location out = locations->Out();
379 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000380
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100381 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
382 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000383 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100385 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000386 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
387 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
388 bool is_load_class_bss_entry =
389 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
390 Register entry_address = kNoRegister;
391 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
392 Register temp = locations->GetTemp(0).AsRegister<Register>();
393 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
394 // the kSaveEverything call.
395 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
396 entry_address = temp_is_r0 ? out.AsRegister<Register>() : temp;
397 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
398 if (temp_is_r0) {
399 __ mov(entry_address, ShifterOperand(temp));
400 }
401 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000402 dex::TypeIndex type_index = cls_->GetTypeIndex();
403 __ LoadImmediate(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100404 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
405 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000406 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000407 if (do_clinit_) {
408 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
409 } else {
410 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
411 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000412
Vladimir Markoea4c1262017-02-06 19:59:33 +0000413 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
414 if (is_load_class_bss_entry) {
415 if (call_saves_everything_except_r0) {
416 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
417 __ str(R0, Address(entry_address));
418 } else {
419 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
420 Register temp = IP;
421 CodeGeneratorARM::PcRelativePatchInfo* labels =
422 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
423 __ BindTrackedLabel(&labels->movw_label);
424 __ movw(temp, /* placeholder */ 0u);
425 __ BindTrackedLabel(&labels->movt_label);
426 __ movt(temp, /* placeholder */ 0u);
427 __ BindTrackedLabel(&labels->add_pc_label);
428 __ add(temp, temp, ShifterOperand(PC));
429 __ str(R0, Address(temp));
430 }
431 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000432 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000433 if (out.IsValid()) {
434 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000435 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
436 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000437 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100438 __ b(GetExitLabel());
439 }
440
Alexandre Rames9931f312015-06-19 14:47:01 +0100441 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
442
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100443 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000444 // The class this slow path will load.
445 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100446
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000447 // The dex PC of `at_`.
448 const uint32_t dex_pc_;
449
450 // Whether to initialize the class.
451 const bool do_clinit_;
452
453 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100454};
455
Vladimir Markoaad75c62016-10-03 08:46:48 +0000456class LoadStringSlowPathARM : public SlowPathCodeARM {
457 public:
458 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
459
460 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000461 DCHECK(instruction_->IsLoadString());
462 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000463 LocationSummary* locations = instruction_->GetLocations();
464 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100465 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000466 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100467 Register out = locations->Out().AsRegister<Register>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100468 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000469
470 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
471 __ Bind(GetEntryLabel());
472 SaveLiveRegisters(codegen, locations);
473
474 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100475 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000476 // the kSaveEverything call.
477 Register entry_address = kNoRegister;
478 if (call_saves_everything_except_r0) {
479 Register temp = locations->GetTemp(0).AsRegister<Register>();
480 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
481 entry_address = temp_is_r0 ? out : temp;
482 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
483 if (temp_is_r0) {
484 __ mov(entry_address, ShifterOperand(temp));
485 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100486 }
487
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000488 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000489 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
490 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100491
492 // Store the resolved String to the .bss entry.
493 if (call_saves_everything_except_r0) {
494 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
495 __ str(R0, Address(entry_address));
496 } else {
497 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000498 Register temp = IP;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100499 CodeGeneratorARM::PcRelativePatchInfo* labels =
500 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
501 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000502 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100503 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000504 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100505 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000506 __ add(temp, temp, ShifterOperand(PC));
507 __ str(R0, Address(temp));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100508 }
509
Vladimir Markoaad75c62016-10-03 08:46:48 +0000510 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000511 RestoreLiveRegisters(codegen, locations);
512
Vladimir Markoaad75c62016-10-03 08:46:48 +0000513 __ b(GetExitLabel());
514 }
515
516 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
517
518 private:
519 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
520};
521
Artem Serovf4d6aee2016-07-11 10:41:45 +0100522class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000523 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000524 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100525 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000526
Alexandre Rames67555f72014-11-18 10:55:16 +0000527 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000528 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000529 DCHECK(instruction_->IsCheckCast()
530 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000531
532 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
533 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000534
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000535 if (!is_fatal_) {
536 SaveLiveRegisters(codegen, locations);
537 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000538
539 // We're moving two locations to locations that could overlap, so we need a parallel
540 // move resolver.
541 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800542 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800543 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
544 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800545 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800546 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
547 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000548 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100549 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100550 instruction_,
551 instruction_->GetDexPc(),
552 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800553 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000554 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
555 } else {
556 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800557 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
558 instruction_,
559 instruction_->GetDexPc(),
560 this);
561 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000562 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000563
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000564 if (!is_fatal_) {
565 RestoreLiveRegisters(codegen, locations);
566 __ b(GetExitLabel());
567 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000568 }
569
Alexandre Rames9931f312015-06-19 14:47:01 +0100570 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
571
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000572 bool IsFatal() const OVERRIDE { return is_fatal_; }
573
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000574 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000575 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000576
577 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
578};
579
Artem Serovf4d6aee2016-07-11 10:41:45 +0100580class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700581 public:
Aart Bik42249c32016-01-07 15:33:50 -0800582 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100583 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700584
585 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800586 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700587 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100588 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000589 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700590 }
591
Alexandre Rames9931f312015-06-19 14:47:01 +0100592 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
593
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700594 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700595 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
596};
597
Artem Serovf4d6aee2016-07-11 10:41:45 +0100598class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100599 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100600 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100601
602 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
603 LocationSummary* locations = instruction_->GetLocations();
604 __ Bind(GetEntryLabel());
605 SaveLiveRegisters(codegen, locations);
606
607 InvokeRuntimeCallingConvention calling_convention;
608 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
609 parallel_move.AddMove(
610 locations->InAt(0),
611 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
612 Primitive::kPrimNot,
613 nullptr);
614 parallel_move.AddMove(
615 locations->InAt(1),
616 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
617 Primitive::kPrimInt,
618 nullptr);
619 parallel_move.AddMove(
620 locations->InAt(2),
621 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
622 Primitive::kPrimNot,
623 nullptr);
624 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
625
626 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100627 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000628 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100629 RestoreLiveRegisters(codegen, locations);
630 __ b(GetExitLabel());
631 }
632
633 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
634
635 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100636 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
637};
638
Roland Levillain47b3ab22017-02-27 14:31:35 +0000639// Slow path marking an object reference `ref` during a read
640// barrier. The field `obj.field` in the object `obj` holding this
641// reference does not get updated by this slow path after marking (see
642// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000643//
Roland Levillain47b3ab22017-02-27 14:31:35 +0000644// This means that after the execution of this slow path, `ref` will
645// always be up-to-date, but `obj.field` may not; i.e., after the
646// flip, `ref` will be a to-space reference, but `obj.field` will
647// probably still be a from-space reference (unless it gets updated by
648// another thread, or if another thread installed another object
649// reference (different from `ref`) in `obj.field`).
650//
651// If `entrypoint` is a valid location it is assumed to already be
652// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000653// is when the decision to mark is based on whether the GC is marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000654class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
655 public:
656 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
657 Location ref,
658 Location entrypoint = Location::NoLocation())
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000659 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
660 DCHECK(kEmitCompilerReadBarrier);
661 }
662
Roland Levillain47b3ab22017-02-27 14:31:35 +0000663 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000664
Roland Levillain47b3ab22017-02-27 14:31:35 +0000665 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
666 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000667 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain47b3ab22017-02-27 14:31:35 +0000668 DCHECK(locations->CanCall());
669 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
670 DCHECK(instruction_->IsInstanceFieldGet() ||
671 instruction_->IsStaticFieldGet() ||
672 instruction_->IsArrayGet() ||
673 instruction_->IsArraySet() ||
674 instruction_->IsLoadClass() ||
675 instruction_->IsLoadString() ||
676 instruction_->IsInstanceOf() ||
677 instruction_->IsCheckCast() ||
678 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
679 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
680 << "Unexpected instruction in read barrier marking slow path: "
681 << instruction_->DebugName();
682 // The read barrier instrumentation of object ArrayGet
683 // instructions does not support the HIntermediateAddress
684 // instruction.
685 DCHECK(!(instruction_->IsArrayGet() &&
686 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
687
688 __ Bind(GetEntryLabel());
689 // No need to save live registers; it's taken care of by the
690 // entrypoint. Also, there is no need to update the stack mask,
691 // as this runtime call will not trigger a garbage collection.
692 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
693 DCHECK_NE(ref_reg, SP);
694 DCHECK_NE(ref_reg, LR);
695 DCHECK_NE(ref_reg, PC);
696 // IP is used internally by the ReadBarrierMarkRegX entry point
697 // as a temporary, it cannot be the entry point's input/output.
698 DCHECK_NE(ref_reg, IP);
699 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
700 // "Compact" slow path, saving two moves.
701 //
702 // Instead of using the standard runtime calling convention (input
703 // and output in R0):
704 //
705 // R0 <- ref
706 // R0 <- ReadBarrierMark(R0)
707 // ref <- R0
708 //
709 // we just use rX (the register containing `ref`) as input and output
710 // of a dedicated entrypoint:
711 //
712 // rX <- ReadBarrierMarkRegX(rX)
713 //
714 if (entrypoint_.IsValid()) {
715 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
716 __ blx(entrypoint_.AsRegister<Register>());
717 } else {
Roland Levillainba650a42017-03-06 13:52:32 +0000718 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000719 int32_t entry_point_offset =
720 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
721 // This runtime call does not require a stack map.
722 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
723 }
724 __ b(GetExitLabel());
725 }
726
727 private:
728 // The location (register) of the marked object reference.
729 const Location ref_;
730
731 // The location of the entrypoint if already loaded.
732 const Location entrypoint_;
733
734 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
735};
736
737// Slow path marking an object reference `ref` during a read barrier,
738// and if needed, atomically updating the field `obj.field` in the
739// object `obj` holding this reference after marking (contrary to
740// ReadBarrierMarkSlowPathARM above, which never tries to update
741// `obj.field`).
742//
743// This means that after the execution of this slow path, both `ref`
744// and `obj.field` will be up-to-date; i.e., after the flip, both will
745// hold the same to-space reference (unless another thread installed
746// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000747//
748// If `entrypoint` is a valid location it is assumed to already be
749// holding the entrypoint. The case where the entrypoint is passed in
750// is when the decision to mark is based on whether the GC is marking.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000751class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
752 public:
753 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
754 Location ref,
755 Register obj,
756 Location field_offset,
757 Register temp1,
Roland Levillainba650a42017-03-06 13:52:32 +0000758 Register temp2,
759 Location entrypoint = Location::NoLocation())
Roland Levillain47b3ab22017-02-27 14:31:35 +0000760 : SlowPathCodeARM(instruction),
761 ref_(ref),
762 obj_(obj),
763 field_offset_(field_offset),
764 temp1_(temp1),
Roland Levillainba650a42017-03-06 13:52:32 +0000765 temp2_(temp2),
766 entrypoint_(entrypoint) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000767 DCHECK(kEmitCompilerReadBarrier);
768 }
769
770 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
771
772 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
773 LocationSummary* locations = instruction_->GetLocations();
774 Register ref_reg = ref_.AsRegister<Register>();
775 DCHECK(locations->CanCall());
776 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
777 // This slow path is only used by the UnsafeCASObject intrinsic.
778 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
779 << "Unexpected instruction in read barrier marking and field updating slow path: "
780 << instruction_->DebugName();
781 DCHECK(instruction_->GetLocations()->Intrinsified());
782 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
783 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
784
785 __ Bind(GetEntryLabel());
786
787 // Save the old reference.
788 // Note that we cannot use IP to save the old reference, as IP is
789 // used internally by the ReadBarrierMarkRegX entry point, and we
790 // need the old reference after the call to that entry point.
791 DCHECK_NE(temp1_, IP);
792 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000793
794 // No need to save live registers; it's taken care of by the
795 // entrypoint. Also, there is no need to update the stack mask,
796 // as this runtime call will not trigger a garbage collection.
797 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
798 DCHECK_NE(ref_reg, SP);
799 DCHECK_NE(ref_reg, LR);
800 DCHECK_NE(ref_reg, PC);
801 // IP is used internally by the ReadBarrierMarkRegX entry point
802 // as a temporary, it cannot be the entry point's input/output.
803 DCHECK_NE(ref_reg, IP);
804 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
805 // "Compact" slow path, saving two moves.
806 //
807 // Instead of using the standard runtime calling convention (input
808 // and output in R0):
809 //
810 // R0 <- ref
811 // R0 <- ReadBarrierMark(R0)
812 // ref <- R0
813 //
814 // we just use rX (the register containing `ref`) as input and output
815 // of a dedicated entrypoint:
816 //
817 // rX <- ReadBarrierMarkRegX(rX)
818 //
Roland Levillainba650a42017-03-06 13:52:32 +0000819 if (entrypoint_.IsValid()) {
820 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
821 __ blx(entrypoint_.AsRegister<Register>());
822 } else {
823 // Entrypoint is not already loaded, load from the thread.
824 int32_t entry_point_offset =
825 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
826 // This runtime call does not require a stack map.
827 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
828 }
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100829
830 // If the new reference is different from the old reference,
Roland Levillain47b3ab22017-02-27 14:31:35 +0000831 // update the field in the holder (`*(obj_ + field_offset_)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100832 //
833 // Note that this field could also hold a different object, if
834 // another thread had concurrently changed it. In that case, the
835 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
836 // (CAS) operation below would abort the CAS, leaving the field
837 // as-is.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000838 Label done;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100839 __ cmp(temp1_, ShifterOperand(ref_reg));
Roland Levillain47b3ab22017-02-27 14:31:35 +0000840 __ b(&done, EQ);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100841
842 // Update the the holder's field atomically. This may fail if
843 // mutator updates before us, but it's OK. This is achieved
844 // using a strong compare-and-set (CAS) operation with relaxed
845 // memory synchronization ordering, where the expected value is
846 // the old reference and the desired value is the new reference.
847
848 // Convenience aliases.
849 Register base = obj_;
850 // The UnsafeCASObject intrinsic uses a register pair as field
851 // offset ("long offset"), of which only the low part contains
852 // data.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000853 Register offset = field_offset_.AsRegisterPairLow<Register>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100854 Register expected = temp1_;
855 Register value = ref_reg;
856 Register tmp_ptr = IP; // Pointer to actual memory.
857 Register tmp = temp2_; // Value in memory.
858
859 __ add(tmp_ptr, base, ShifterOperand(offset));
860
861 if (kPoisonHeapReferences) {
862 __ PoisonHeapReference(expected);
863 if (value == expected) {
864 // Do not poison `value`, as it is the same register as
865 // `expected`, which has just been poisoned.
866 } else {
867 __ PoisonHeapReference(value);
868 }
869 }
870
871 // do {
872 // tmp = [r_ptr] - expected;
873 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
874
Roland Levillain24a4d112016-10-26 13:10:46 +0100875 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100876 __ Bind(&loop_head);
877
878 __ ldrex(tmp, tmp_ptr);
879
880 __ subs(tmp, tmp, ShifterOperand(expected));
881
Roland Levillain24a4d112016-10-26 13:10:46 +0100882 __ it(NE);
883 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100884
Roland Levillain24a4d112016-10-26 13:10:46 +0100885 __ b(&exit_loop, NE);
886
887 __ strex(tmp, value, tmp_ptr);
888 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100889 __ b(&loop_head, EQ);
890
Roland Levillain24a4d112016-10-26 13:10:46 +0100891 __ Bind(&exit_loop);
892
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100893 if (kPoisonHeapReferences) {
894 __ UnpoisonHeapReference(expected);
895 if (value == expected) {
896 // Do not unpoison `value`, as it is the same register as
897 // `expected`, which has just been unpoisoned.
898 } else {
899 __ UnpoisonHeapReference(value);
900 }
901 }
902
Roland Levillain47b3ab22017-02-27 14:31:35 +0000903 __ Bind(&done);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100904 __ b(GetExitLabel());
905 }
906
907 private:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000908 // The location (register) of the marked object reference.
909 const Location ref_;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100910 // The register containing the object holding the marked object reference field.
911 const Register obj_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000912 // The location of the offset of the marked reference field within `obj_`.
913 Location field_offset_;
914
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100915 const Register temp1_;
916 const Register temp2_;
917
Roland Levillainba650a42017-03-06 13:52:32 +0000918 // The location of the entrypoint if already loaded.
919 const Location entrypoint_;
920
Roland Levillain47b3ab22017-02-27 14:31:35 +0000921 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100922};
923
Roland Levillain3b359c72015-11-17 19:35:12 +0000924// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100925class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000926 public:
927 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
928 Location out,
929 Location ref,
930 Location obj,
931 uint32_t offset,
932 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100933 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000934 out_(out),
935 ref_(ref),
936 obj_(obj),
937 offset_(offset),
938 index_(index) {
939 DCHECK(kEmitCompilerReadBarrier);
940 // If `obj` is equal to `out` or `ref`, it means the initial object
941 // has been overwritten by (or after) the heap object reference load
942 // to be instrumented, e.g.:
943 //
944 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000945 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000946 //
947 // In that case, we have lost the information about the original
948 // object, and the emitted read barrier cannot work properly.
949 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
950 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
951 }
952
953 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
954 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
955 LocationSummary* locations = instruction_->GetLocations();
956 Register reg_out = out_.AsRegister<Register>();
957 DCHECK(locations->CanCall());
958 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100959 DCHECK(instruction_->IsInstanceFieldGet() ||
960 instruction_->IsStaticFieldGet() ||
961 instruction_->IsArrayGet() ||
962 instruction_->IsInstanceOf() ||
963 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100964 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000965 << "Unexpected instruction in read barrier for heap reference slow path: "
966 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000967 // The read barrier instrumentation of object ArrayGet
968 // instructions does not support the HIntermediateAddress
969 // instruction.
970 DCHECK(!(instruction_->IsArrayGet() &&
971 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +0000972
973 __ Bind(GetEntryLabel());
974 SaveLiveRegisters(codegen, locations);
975
976 // We may have to change the index's value, but as `index_` is a
977 // constant member (like other "inputs" of this slow path),
978 // introduce a copy of it, `index`.
979 Location index = index_;
980 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100981 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000982 if (instruction_->IsArrayGet()) {
983 // Compute the actual memory offset and store it in `index`.
984 Register index_reg = index_.AsRegister<Register>();
985 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
986 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
987 // We are about to change the value of `index_reg` (see the
988 // calls to art::arm::Thumb2Assembler::Lsl and
989 // art::arm::Thumb2Assembler::AddConstant below), but it has
990 // not been saved by the previous call to
991 // art::SlowPathCode::SaveLiveRegisters, as it is a
992 // callee-save register --
993 // art::SlowPathCode::SaveLiveRegisters does not consider
994 // callee-save registers, as it has been designed with the
995 // assumption that callee-save registers are supposed to be
996 // handled by the called function. So, as a callee-save
997 // register, `index_reg` _would_ eventually be saved onto
998 // the stack, but it would be too late: we would have
999 // changed its value earlier. Therefore, we manually save
1000 // it here into another freely available register,
1001 // `free_reg`, chosen of course among the caller-save
1002 // registers (as a callee-save `free_reg` register would
1003 // exhibit the same problem).
1004 //
1005 // Note we could have requested a temporary register from
1006 // the register allocator instead; but we prefer not to, as
1007 // this is a slow path, and we know we can find a
1008 // caller-save register that is available.
1009 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1010 __ Mov(free_reg, index_reg);
1011 index_reg = free_reg;
1012 index = Location::RegisterLocation(index_reg);
1013 } else {
1014 // The initial register stored in `index_` has already been
1015 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1016 // (as it is not a callee-save register), so we can freely
1017 // use it.
1018 }
1019 // Shifting the index value contained in `index_reg` by the scale
1020 // factor (2) cannot overflow in practice, as the runtime is
1021 // unable to allocate object arrays with a size larger than
1022 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1023 __ Lsl(index_reg, index_reg, TIMES_4);
1024 static_assert(
1025 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1026 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1027 __ AddConstant(index_reg, index_reg, offset_);
1028 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001029 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1030 // intrinsics, `index_` is not shifted by a scale factor of 2
1031 // (as in the case of ArrayGet), as it is actually an offset
1032 // to an object field within an object.
1033 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001034 DCHECK(instruction_->GetLocations()->Intrinsified());
1035 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1036 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1037 << instruction_->AsInvoke()->GetIntrinsic();
1038 DCHECK_EQ(offset_, 0U);
1039 DCHECK(index_.IsRegisterPair());
1040 // UnsafeGet's offset location is a register pair, the low
1041 // part contains the correct offset.
1042 index = index_.ToLow();
1043 }
1044 }
1045
1046 // We're moving two or three locations to locations that could
1047 // overlap, so we need a parallel move resolver.
1048 InvokeRuntimeCallingConvention calling_convention;
1049 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1050 parallel_move.AddMove(ref_,
1051 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1052 Primitive::kPrimNot,
1053 nullptr);
1054 parallel_move.AddMove(obj_,
1055 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1056 Primitive::kPrimNot,
1057 nullptr);
1058 if (index.IsValid()) {
1059 parallel_move.AddMove(index,
1060 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1061 Primitive::kPrimInt,
1062 nullptr);
1063 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1064 } else {
1065 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1066 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1067 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001068 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001069 CheckEntrypointTypes<
1070 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1071 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1072
1073 RestoreLiveRegisters(codegen, locations);
1074 __ b(GetExitLabel());
1075 }
1076
1077 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1078
1079 private:
1080 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1081 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1082 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1083 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1084 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1085 return static_cast<Register>(i);
1086 }
1087 }
1088 // We shall never fail to find a free caller-save register, as
1089 // there are more than two core caller-save registers on ARM
1090 // (meaning it is possible to find one which is different from
1091 // `ref` and `obj`).
1092 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1093 LOG(FATAL) << "Could not find a free caller-save register";
1094 UNREACHABLE();
1095 }
1096
Roland Levillain3b359c72015-11-17 19:35:12 +00001097 const Location out_;
1098 const Location ref_;
1099 const Location obj_;
1100 const uint32_t offset_;
1101 // An additional location containing an index to an array.
1102 // Only used for HArrayGet and the UnsafeGetObject &
1103 // UnsafeGetObjectVolatile intrinsics.
1104 const Location index_;
1105
1106 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1107};
1108
1109// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001110class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001111 public:
1112 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001113 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001114 DCHECK(kEmitCompilerReadBarrier);
1115 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001116
1117 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1118 LocationSummary* locations = instruction_->GetLocations();
1119 Register reg_out = out_.AsRegister<Register>();
1120 DCHECK(locations->CanCall());
1121 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001122 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1123 << "Unexpected instruction in read barrier for GC root slow path: "
1124 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001125
1126 __ Bind(GetEntryLabel());
1127 SaveLiveRegisters(codegen, locations);
1128
1129 InvokeRuntimeCallingConvention calling_convention;
1130 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1131 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001132 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001133 instruction_,
1134 instruction_->GetDexPc(),
1135 this);
1136 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1137 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1138
1139 RestoreLiveRegisters(codegen, locations);
1140 __ b(GetExitLabel());
1141 }
1142
1143 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1144
1145 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001146 const Location out_;
1147 const Location root_;
1148
1149 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1150};
1151
Aart Bike9f37602015-10-09 11:15:55 -07001152inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001153 switch (cond) {
1154 case kCondEQ: return EQ;
1155 case kCondNE: return NE;
1156 case kCondLT: return LT;
1157 case kCondLE: return LE;
1158 case kCondGT: return GT;
1159 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001160 case kCondB: return LO;
1161 case kCondBE: return LS;
1162 case kCondA: return HI;
1163 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001164 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001165 LOG(FATAL) << "Unreachable";
1166 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001167}
1168
Aart Bike9f37602015-10-09 11:15:55 -07001169// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001170inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001171 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001172 case kCondEQ: return EQ;
1173 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001174 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001175 case kCondLT: return LO;
1176 case kCondLE: return LS;
1177 case kCondGT: return HI;
1178 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001179 // Unsigned remain unchanged.
1180 case kCondB: return LO;
1181 case kCondBE: return LS;
1182 case kCondA: return HI;
1183 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001184 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001185 LOG(FATAL) << "Unreachable";
1186 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001187}
1188
Vladimir Markod6e069b2016-01-18 11:11:01 +00001189inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1190 // The ARM condition codes can express all the necessary branches, see the
1191 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1192 // There is no dex instruction or HIR that would need the missing conditions
1193 // "equal or unordered" or "not equal".
1194 switch (cond) {
1195 case kCondEQ: return EQ;
1196 case kCondNE: return NE /* unordered */;
1197 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1198 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1199 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1200 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1201 default:
1202 LOG(FATAL) << "UNREACHABLE";
1203 UNREACHABLE();
1204 }
1205}
1206
Anton Kirilov74234da2017-01-13 14:42:47 +00001207inline Shift ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1208 switch (op_kind) {
1209 case HDataProcWithShifterOp::kASR: return ASR;
1210 case HDataProcWithShifterOp::kLSL: return LSL;
1211 case HDataProcWithShifterOp::kLSR: return LSR;
1212 default:
1213 LOG(FATAL) << "Unexpected op kind " << op_kind;
1214 UNREACHABLE();
1215 }
1216}
1217
1218static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1219 Register out,
1220 Register first,
1221 const ShifterOperand& second,
1222 CodeGeneratorARM* codegen) {
1223 if (second.IsImmediate() && second.GetImmediate() == 0) {
1224 const ShifterOperand in = kind == HInstruction::kAnd
1225 ? ShifterOperand(0)
1226 : ShifterOperand(first);
1227
1228 __ mov(out, in);
1229 } else {
1230 switch (kind) {
1231 case HInstruction::kAdd:
1232 __ add(out, first, second);
1233 break;
1234 case HInstruction::kAnd:
1235 __ and_(out, first, second);
1236 break;
1237 case HInstruction::kOr:
1238 __ orr(out, first, second);
1239 break;
1240 case HInstruction::kSub:
1241 __ sub(out, first, second);
1242 break;
1243 case HInstruction::kXor:
1244 __ eor(out, first, second);
1245 break;
1246 default:
1247 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1248 UNREACHABLE();
1249 }
1250 }
1251}
1252
1253static void GenerateDataProc(HInstruction::InstructionKind kind,
1254 const Location& out,
1255 const Location& first,
1256 const ShifterOperand& second_lo,
1257 const ShifterOperand& second_hi,
1258 CodeGeneratorARM* codegen) {
1259 const Register first_hi = first.AsRegisterPairHigh<Register>();
1260 const Register first_lo = first.AsRegisterPairLow<Register>();
1261 const Register out_hi = out.AsRegisterPairHigh<Register>();
1262 const Register out_lo = out.AsRegisterPairLow<Register>();
1263
1264 if (kind == HInstruction::kAdd) {
1265 __ adds(out_lo, first_lo, second_lo);
1266 __ adc(out_hi, first_hi, second_hi);
1267 } else if (kind == HInstruction::kSub) {
1268 __ subs(out_lo, first_lo, second_lo);
1269 __ sbc(out_hi, first_hi, second_hi);
1270 } else {
1271 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1272 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1273 }
1274}
1275
1276static ShifterOperand GetShifterOperand(Register rm, Shift shift, uint32_t shift_imm) {
1277 return shift_imm == 0 ? ShifterOperand(rm) : ShifterOperand(rm, shift, shift_imm);
1278}
1279
1280static void GenerateLongDataProc(HDataProcWithShifterOp* instruction, CodeGeneratorARM* codegen) {
1281 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1282 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1283
1284 const LocationSummary* const locations = instruction->GetLocations();
1285 const uint32_t shift_value = instruction->GetShiftAmount();
1286 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1287 const Location first = locations->InAt(0);
1288 const Location second = locations->InAt(1);
1289 const Location out = locations->Out();
1290 const Register first_hi = first.AsRegisterPairHigh<Register>();
1291 const Register first_lo = first.AsRegisterPairLow<Register>();
1292 const Register out_hi = out.AsRegisterPairHigh<Register>();
1293 const Register out_lo = out.AsRegisterPairLow<Register>();
1294 const Register second_hi = second.AsRegisterPairHigh<Register>();
1295 const Register second_lo = second.AsRegisterPairLow<Register>();
1296 const Shift shift = ShiftFromOpKind(instruction->GetOpKind());
1297
1298 if (shift_value >= 32) {
1299 if (shift == LSL) {
1300 GenerateDataProcInstruction(kind,
1301 out_hi,
1302 first_hi,
1303 ShifterOperand(second_lo, LSL, shift_value - 32),
1304 codegen);
1305 GenerateDataProcInstruction(kind,
1306 out_lo,
1307 first_lo,
1308 ShifterOperand(0),
1309 codegen);
1310 } else if (shift == ASR) {
1311 GenerateDataProc(kind,
1312 out,
1313 first,
1314 GetShifterOperand(second_hi, ASR, shift_value - 32),
1315 ShifterOperand(second_hi, ASR, 31),
1316 codegen);
1317 } else {
1318 DCHECK_EQ(shift, LSR);
1319 GenerateDataProc(kind,
1320 out,
1321 first,
1322 GetShifterOperand(second_hi, LSR, shift_value - 32),
1323 ShifterOperand(0),
1324 codegen);
1325 }
1326 } else {
1327 DCHECK_GT(shift_value, 1U);
1328 DCHECK_LT(shift_value, 32U);
1329
1330 if (shift == LSL) {
1331 // We are not doing this for HInstruction::kAdd because the output will require
1332 // Location::kOutputOverlap; not applicable to other cases.
1333 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1334 GenerateDataProcInstruction(kind,
1335 out_hi,
1336 first_hi,
1337 ShifterOperand(second_hi, LSL, shift_value),
1338 codegen);
1339 GenerateDataProcInstruction(kind,
1340 out_hi,
1341 out_hi,
1342 ShifterOperand(second_lo, LSR, 32 - shift_value),
1343 codegen);
1344 GenerateDataProcInstruction(kind,
1345 out_lo,
1346 first_lo,
1347 ShifterOperand(second_lo, LSL, shift_value),
1348 codegen);
1349 } else {
1350 __ Lsl(IP, second_hi, shift_value);
1351 __ orr(IP, IP, ShifterOperand(second_lo, LSR, 32 - shift_value));
1352 GenerateDataProc(kind,
1353 out,
1354 first,
1355 ShifterOperand(second_lo, LSL, shift_value),
1356 ShifterOperand(IP),
1357 codegen);
1358 }
1359 } else {
1360 DCHECK(shift == ASR || shift == LSR);
1361
1362 // We are not doing this for HInstruction::kAdd because the output will require
1363 // Location::kOutputOverlap; not applicable to other cases.
1364 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1365 GenerateDataProcInstruction(kind,
1366 out_lo,
1367 first_lo,
1368 ShifterOperand(second_lo, LSR, shift_value),
1369 codegen);
1370 GenerateDataProcInstruction(kind,
1371 out_lo,
1372 out_lo,
1373 ShifterOperand(second_hi, LSL, 32 - shift_value),
1374 codegen);
1375 GenerateDataProcInstruction(kind,
1376 out_hi,
1377 first_hi,
1378 ShifterOperand(second_hi, shift, shift_value),
1379 codegen);
1380 } else {
1381 __ Lsr(IP, second_lo, shift_value);
1382 __ orr(IP, IP, ShifterOperand(second_hi, LSL, 32 - shift_value));
1383 GenerateDataProc(kind,
1384 out,
1385 first,
1386 ShifterOperand(IP),
1387 ShifterOperand(second_hi, shift, shift_value),
1388 codegen);
1389 }
1390 }
1391 }
1392}
1393
1394#undef __
1395// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1396#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
1397
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001398void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001399 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001400}
1401
1402void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001403 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001404}
1405
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001406size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1407 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1408 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001409}
1410
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001411size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1412 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1413 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001414}
1415
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001416size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1417 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1418 return kArmWordSize;
1419}
1420
1421size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1422 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1423 return kArmWordSize;
1424}
1425
Calin Juravle34166012014-12-19 17:22:29 +00001426CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001427 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001428 const CompilerOptions& compiler_options,
1429 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001430 : CodeGenerator(graph,
1431 kNumberOfCoreRegisters,
1432 kNumberOfSRegisters,
1433 kNumberOfRegisterPairs,
1434 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1435 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001436 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1437 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001438 compiler_options,
1439 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001440 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001441 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001442 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001443 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001444 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001445 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001446 uint32_literals_(std::less<uint32_t>(),
1447 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001448 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1449 boot_image_string_patches_(StringReferenceValueComparator(),
1450 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1451 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001452 boot_image_type_patches_(TypeReferenceValueComparator(),
1453 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1454 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001455 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001456 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001457 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1458 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001459 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1460 jit_class_patches_(TypeReferenceValueComparator(),
1461 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001462 // Always save the LR register to mimic Quick.
1463 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001464}
1465
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001466void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1467 // Ensure that we fix up branches and literal loads and emit the literal pool.
1468 __ FinalizeCode();
1469
1470 // Adjust native pc offsets in stack maps.
1471 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001472 uint32_t old_position =
1473 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001474 uint32_t new_position = __ GetAdjustedPosition(old_position);
1475 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1476 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001477 // Adjust pc offsets for the disassembly information.
1478 if (disasm_info_ != nullptr) {
1479 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1480 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1481 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1482 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1483 it.second.start = __ GetAdjustedPosition(it.second.start);
1484 it.second.end = __ GetAdjustedPosition(it.second.end);
1485 }
1486 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1487 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1488 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1489 }
1490 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001491
1492 CodeGenerator::Finalize(allocator);
1493}
1494
David Brazdil58282f42016-01-14 12:45:10 +00001495void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001496 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001497 blocked_core_registers_[SP] = true;
1498 blocked_core_registers_[LR] = true;
1499 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001500
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001501 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001502 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001503
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001504 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001505 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001506
David Brazdil58282f42016-01-14 12:45:10 +00001507 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001508 // Stubs do not save callee-save floating point registers. If the graph
1509 // is debuggable, we need to deal with these registers differently. For
1510 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001511 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1512 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1513 }
1514 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001515}
1516
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001517InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001518 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001519 assembler_(codegen->GetAssembler()),
1520 codegen_(codegen) {}
1521
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001522void CodeGeneratorARM::ComputeSpillMask() {
1523 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1524 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001525 // There is no easy instruction to restore just the PC on thumb2. We spill and
1526 // restore another arbitrary register.
1527 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001528 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1529 // We use vpush and vpop for saving and restoring floating point registers, which take
1530 // a SRegister and the number of registers to save/restore after that SRegister. We
1531 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1532 // but in the range.
1533 if (fpu_spill_mask_ != 0) {
1534 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1535 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1536 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1537 fpu_spill_mask_ |= (1 << i);
1538 }
1539 }
1540}
1541
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001542static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001543 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001544}
1545
1546static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001547 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001548}
1549
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001550void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001551 bool skip_overflow_check =
1552 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001553 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001554 __ Bind(&frame_entry_label_);
1555
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001556 if (HasEmptyFrame()) {
1557 return;
1558 }
1559
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001560 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001561 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1562 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1563 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001564 }
1565
Andreas Gampe501fd632015-09-10 16:11:06 -07001566 __ PushList(core_spill_mask_);
1567 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1568 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001569 if (fpu_spill_mask_ != 0) {
1570 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1571 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001572 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001573 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001574 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001575
1576 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1577 // Initialize should_deoptimize flag to 0.
1578 __ mov(IP, ShifterOperand(0));
1579 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
1580 }
1581
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001582 int adjust = GetFrameSize() - FrameEntrySpillSize();
1583 __ AddConstant(SP, -adjust);
1584 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001585
1586 // Save the current method if we need it. Note that we do not
1587 // do this in HCurrentMethod, as the instruction might have been removed
1588 // in the SSA graph.
1589 if (RequiresCurrentMethod()) {
1590 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1591 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001592}
1593
1594void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001595 if (HasEmptyFrame()) {
1596 __ bx(LR);
1597 return;
1598 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001599 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001600 int adjust = GetFrameSize() - FrameEntrySpillSize();
1601 __ AddConstant(SP, adjust);
1602 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001603 if (fpu_spill_mask_ != 0) {
1604 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1605 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001606 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001607 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001608 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001609 // Pop LR into PC to return.
1610 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1611 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1612 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001613 __ cfi().RestoreState();
1614 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001615}
1616
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001617void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001618 Label* label = GetLabelOf(block);
1619 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001620}
1621
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001622Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001623 switch (type) {
1624 case Primitive::kPrimBoolean:
1625 case Primitive::kPrimByte:
1626 case Primitive::kPrimChar:
1627 case Primitive::kPrimShort:
1628 case Primitive::kPrimInt:
1629 case Primitive::kPrimNot: {
1630 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001631 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001632 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001633 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001634 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001635 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001636 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001637 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001638
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001639 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001640 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001641 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001642 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001643 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001644 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001645 if (calling_convention.GetRegisterAt(index) == R1) {
1646 // Skip R1, and use R2_R3 instead.
1647 gp_index_++;
1648 index++;
1649 }
1650 }
1651 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1652 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001653 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001654
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001655 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001656 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001657 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001658 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1659 }
1660 }
1661
1662 case Primitive::kPrimFloat: {
1663 uint32_t stack_index = stack_index_++;
1664 if (float_index_ % 2 == 0) {
1665 float_index_ = std::max(double_index_, float_index_);
1666 }
1667 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1668 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1669 } else {
1670 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1671 }
1672 }
1673
1674 case Primitive::kPrimDouble: {
1675 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1676 uint32_t stack_index = stack_index_;
1677 stack_index_ += 2;
1678 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1679 uint32_t index = double_index_;
1680 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001681 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001682 calling_convention.GetFpuRegisterAt(index),
1683 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001684 DCHECK(ExpectedPairLayout(result));
1685 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001686 } else {
1687 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001688 }
1689 }
1690
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001691 case Primitive::kPrimVoid:
1692 LOG(FATAL) << "Unexpected parameter type " << type;
1693 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001694 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001695 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001696}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001697
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001698Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001699 switch (type) {
1700 case Primitive::kPrimBoolean:
1701 case Primitive::kPrimByte:
1702 case Primitive::kPrimChar:
1703 case Primitive::kPrimShort:
1704 case Primitive::kPrimInt:
1705 case Primitive::kPrimNot: {
1706 return Location::RegisterLocation(R0);
1707 }
1708
1709 case Primitive::kPrimFloat: {
1710 return Location::FpuRegisterLocation(S0);
1711 }
1712
1713 case Primitive::kPrimLong: {
1714 return Location::RegisterPairLocation(R0, R1);
1715 }
1716
1717 case Primitive::kPrimDouble: {
1718 return Location::FpuRegisterPairLocation(S0, S1);
1719 }
1720
1721 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001722 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001723 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001724
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001725 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001726}
1727
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001728Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1729 return Location::RegisterLocation(kMethodRegisterArgument);
1730}
1731
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001732void CodeGeneratorARM::Move32(Location destination, Location source) {
1733 if (source.Equals(destination)) {
1734 return;
1735 }
1736 if (destination.IsRegister()) {
1737 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001738 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001739 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001740 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001741 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001742 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001743 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001744 } else if (destination.IsFpuRegister()) {
1745 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001746 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001747 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001748 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001749 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001750 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001751 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001752 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001753 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001754 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001755 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001756 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001757 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001758 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001759 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001760 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1761 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001762 }
1763 }
1764}
1765
1766void CodeGeneratorARM::Move64(Location destination, Location source) {
1767 if (source.Equals(destination)) {
1768 return;
1769 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001770 if (destination.IsRegisterPair()) {
1771 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001772 EmitParallelMoves(
1773 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1774 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001775 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001776 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001777 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1778 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001779 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001780 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001781 } else if (source.IsFpuRegisterPair()) {
1782 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1783 destination.AsRegisterPairHigh<Register>(),
1784 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001785 } else {
1786 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001787 DCHECK(ExpectedPairLayout(destination));
1788 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1789 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001790 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001791 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001792 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001793 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1794 SP,
1795 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001796 } else if (source.IsRegisterPair()) {
1797 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1798 source.AsRegisterPairLow<Register>(),
1799 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001800 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001801 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001802 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001803 } else {
1804 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001805 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001806 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001807 if (source.AsRegisterPairLow<Register>() == R1) {
1808 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001809 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1810 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001811 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001812 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001813 SP, destination.GetStackIndex());
1814 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001815 } else if (source.IsFpuRegisterPair()) {
1816 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1817 SP,
1818 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001819 } else {
1820 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001821 EmitParallelMoves(
1822 Location::StackSlot(source.GetStackIndex()),
1823 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001824 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001825 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001826 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1827 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001828 }
1829 }
1830}
1831
Calin Juravle175dc732015-08-25 15:42:32 +01001832void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1833 DCHECK(location.IsRegister());
1834 __ LoadImmediate(location.AsRegister<Register>(), value);
1835}
1836
Calin Juravlee460d1d2015-09-29 04:52:17 +01001837void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001838 HParallelMove move(GetGraph()->GetArena());
1839 move.AddMove(src, dst, dst_type, nullptr);
1840 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001841}
1842
1843void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1844 if (location.IsRegister()) {
1845 locations->AddTemp(location);
1846 } else if (location.IsRegisterPair()) {
1847 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1848 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1849 } else {
1850 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1851 }
1852}
1853
Calin Juravle175dc732015-08-25 15:42:32 +01001854void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1855 HInstruction* instruction,
1856 uint32_t dex_pc,
1857 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001858 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001859 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001860 if (EntrypointRequiresStackMap(entrypoint)) {
1861 RecordPcInfo(instruction, dex_pc, slow_path);
1862 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001863}
1864
Roland Levillaindec8f632016-07-22 17:10:06 +01001865void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1866 HInstruction* instruction,
1867 SlowPathCode* slow_path) {
1868 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001869 GenerateInvokeRuntime(entry_point_offset);
1870}
1871
1872void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001873 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1874 __ blx(LR);
1875}
1876
David Brazdilfc6a86a2015-06-26 10:33:45 +00001877void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001878 DCHECK(!successor->IsExitBlock());
1879
1880 HBasicBlock* block = got->GetBlock();
1881 HInstruction* previous = got->GetPrevious();
1882
1883 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001884 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001885 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1886 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1887 return;
1888 }
1889
1890 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1891 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1892 }
1893 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001894 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001895 }
1896}
1897
David Brazdilfc6a86a2015-06-26 10:33:45 +00001898void LocationsBuilderARM::VisitGoto(HGoto* got) {
1899 got->SetLocations(nullptr);
1900}
1901
1902void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1903 HandleGoto(got, got->GetSuccessor());
1904}
1905
1906void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1907 try_boundary->SetLocations(nullptr);
1908}
1909
1910void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1911 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1912 if (!successor->IsExitBlock()) {
1913 HandleGoto(try_boundary, successor);
1914 }
1915}
1916
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001917void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001918 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001919}
1920
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001921void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001922}
1923
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001924void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1925 Primitive::Type type = instruction->InputAt(0)->GetType();
1926 Location lhs_loc = instruction->GetLocations()->InAt(0);
1927 Location rhs_loc = instruction->GetLocations()->InAt(1);
1928 if (rhs_loc.IsConstant()) {
1929 // 0.0 is the only immediate that can be encoded directly in
1930 // a VCMP instruction.
1931 //
1932 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1933 // specify that in a floating-point comparison, positive zero
1934 // and negative zero are considered equal, so we can use the
1935 // literal 0.0 for both cases here.
1936 //
1937 // Note however that some methods (Float.equal, Float.compare,
1938 // Float.compareTo, Double.equal, Double.compare,
1939 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1940 // StrictMath.min) consider 0.0 to be (strictly) greater than
1941 // -0.0. So if we ever translate calls to these methods into a
1942 // HCompare instruction, we must handle the -0.0 case with
1943 // care here.
1944 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1945 if (type == Primitive::kPrimFloat) {
1946 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1947 } else {
1948 DCHECK_EQ(type, Primitive::kPrimDouble);
1949 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1950 }
1951 } else {
1952 if (type == Primitive::kPrimFloat) {
1953 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1954 } else {
1955 DCHECK_EQ(type, Primitive::kPrimDouble);
1956 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1957 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1958 }
1959 }
1960}
1961
Roland Levillain4fa13f62015-07-06 18:11:54 +01001962void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1963 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001964 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001965 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001966 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001967}
1968
1969void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1970 Label* true_label,
1971 Label* false_label) {
1972 LocationSummary* locations = cond->GetLocations();
1973 Location left = locations->InAt(0);
1974 Location right = locations->InAt(1);
1975 IfCondition if_cond = cond->GetCondition();
1976
1977 Register left_high = left.AsRegisterPairHigh<Register>();
1978 Register left_low = left.AsRegisterPairLow<Register>();
1979 IfCondition true_high_cond = if_cond;
1980 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001981 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001982
1983 // Set the conditions for the test, remembering that == needs to be
1984 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001985 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001986 switch (if_cond) {
1987 case kCondEQ:
1988 case kCondNE:
1989 // Nothing to do.
1990 break;
1991 case kCondLT:
1992 false_high_cond = kCondGT;
1993 break;
1994 case kCondLE:
1995 true_high_cond = kCondLT;
1996 break;
1997 case kCondGT:
1998 false_high_cond = kCondLT;
1999 break;
2000 case kCondGE:
2001 true_high_cond = kCondGT;
2002 break;
Aart Bike9f37602015-10-09 11:15:55 -07002003 case kCondB:
2004 false_high_cond = kCondA;
2005 break;
2006 case kCondBE:
2007 true_high_cond = kCondB;
2008 break;
2009 case kCondA:
2010 false_high_cond = kCondB;
2011 break;
2012 case kCondAE:
2013 true_high_cond = kCondA;
2014 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002015 }
2016 if (right.IsConstant()) {
2017 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
2018 int32_t val_low = Low32Bits(value);
2019 int32_t val_high = High32Bits(value);
2020
Vladimir Markoac6ac102015-12-17 12:14:00 +00002021 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002022 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002023 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002024 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002025 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002026 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002027 __ b(true_label, ARMCondition(true_high_cond));
2028 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002029 }
2030 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00002031 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002032 } else {
2033 Register right_high = right.AsRegisterPairHigh<Register>();
2034 Register right_low = right.AsRegisterPairLow<Register>();
2035
2036 __ cmp(left_high, ShifterOperand(right_high));
2037 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002038 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002039 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002040 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002041 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002042 __ b(true_label, ARMCondition(true_high_cond));
2043 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002044 }
2045 // Must be equal high, so compare the lows.
2046 __ cmp(left_low, ShifterOperand(right_low));
2047 }
2048 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07002049 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01002050 __ b(true_label, final_condition);
2051}
2052
David Brazdil0debae72015-11-12 18:37:00 +00002053void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
2054 Label* true_target_in,
2055 Label* false_target_in) {
2056 // Generated branching requires both targets to be explicit. If either of the
2057 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2058 Label fallthrough_target;
2059 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
2060 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
2061
Roland Levillain4fa13f62015-07-06 18:11:54 +01002062 Primitive::Type type = condition->InputAt(0)->GetType();
2063 switch (type) {
2064 case Primitive::kPrimLong:
2065 GenerateLongComparesAndJumps(condition, true_target, false_target);
2066 break;
2067 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002068 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002069 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002070 GenerateFPJumps(condition, true_target, false_target);
2071 break;
2072 default:
2073 LOG(FATAL) << "Unexpected compare type " << type;
2074 }
2075
David Brazdil0debae72015-11-12 18:37:00 +00002076 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002077 __ b(false_target);
2078 }
David Brazdil0debae72015-11-12 18:37:00 +00002079
2080 if (fallthrough_target.IsLinked()) {
2081 __ Bind(&fallthrough_target);
2082 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002083}
2084
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002085void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002086 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002087 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002088 Label* false_target) {
2089 HInstruction* cond = instruction->InputAt(condition_input_index);
2090
2091 if (true_target == nullptr && false_target == nullptr) {
2092 // Nothing to do. The code always falls through.
2093 return;
2094 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002095 // Constant condition, statically compared against "true" (integer value 1).
2096 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002097 if (true_target != nullptr) {
2098 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002099 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002100 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002101 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002102 if (false_target != nullptr) {
2103 __ b(false_target);
2104 }
2105 }
2106 return;
2107 }
2108
2109 // The following code generates these patterns:
2110 // (1) true_target == nullptr && false_target != nullptr
2111 // - opposite condition true => branch to false_target
2112 // (2) true_target != nullptr && false_target == nullptr
2113 // - condition true => branch to true_target
2114 // (3) true_target != nullptr && false_target != nullptr
2115 // - condition true => branch to true_target
2116 // - branch to false_target
2117 if (IsBooleanValueOrMaterializedCondition(cond)) {
2118 // Condition has been materialized, compare the output to 0.
2119 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2120 DCHECK(cond_val.IsRegister());
2121 if (true_target == nullptr) {
2122 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
2123 } else {
2124 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002125 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002126 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002127 // Condition has not been materialized. Use its inputs as the comparison and
2128 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002129 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002130
2131 // If this is a long or FP comparison that has been folded into
2132 // the HCondition, generate the comparison directly.
2133 Primitive::Type type = condition->InputAt(0)->GetType();
2134 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2135 GenerateCompareTestAndBranch(condition, true_target, false_target);
2136 return;
2137 }
2138
2139 LocationSummary* locations = cond->GetLocations();
2140 DCHECK(locations->InAt(0).IsRegister());
2141 Register left = locations->InAt(0).AsRegister<Register>();
2142 Location right = locations->InAt(1);
2143 if (right.IsRegister()) {
2144 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002145 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002146 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002147 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00002148 }
2149 if (true_target == nullptr) {
2150 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
2151 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04002152 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002153 }
Dave Allison20dfc792014-06-16 20:44:29 -07002154 }
David Brazdil0debae72015-11-12 18:37:00 +00002155
2156 // If neither branch falls through (case 3), the conditional branch to `true_target`
2157 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2158 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002159 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002160 }
2161}
2162
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002163void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002164 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2165 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002166 locations->SetInAt(0, Location::RequiresRegister());
2167 }
2168}
2169
2170void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002171 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2172 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2173 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2174 nullptr : codegen_->GetLabelOf(true_successor);
2175 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2176 nullptr : codegen_->GetLabelOf(false_successor);
2177 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002178}
2179
2180void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
2181 LocationSummary* locations = new (GetGraph()->GetArena())
2182 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002183 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002184 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002185 locations->SetInAt(0, Location::RequiresRegister());
2186 }
2187}
2188
2189void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01002190 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002191 GenerateTestAndBranch(deoptimize,
2192 /* condition_input_index */ 0,
2193 slow_path->GetEntryLabel(),
2194 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002195}
Dave Allison20dfc792014-06-16 20:44:29 -07002196
Mingyao Yang063fc772016-08-02 11:02:54 -07002197void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2198 LocationSummary* locations = new (GetGraph()->GetArena())
2199 LocationSummary(flag, LocationSummary::kNoCall);
2200 locations->SetOut(Location::RequiresRegister());
2201}
2202
2203void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2204 __ LoadFromOffset(kLoadWord,
2205 flag->GetLocations()->Out().AsRegister<Register>(),
2206 SP,
2207 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2208}
2209
David Brazdil74eb1b22015-12-14 11:44:01 +00002210void LocationsBuilderARM::VisitSelect(HSelect* select) {
2211 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2212 if (Primitive::IsFloatingPointType(select->GetType())) {
2213 locations->SetInAt(0, Location::RequiresFpuRegister());
2214 locations->SetInAt(1, Location::RequiresFpuRegister());
2215 } else {
2216 locations->SetInAt(0, Location::RequiresRegister());
2217 locations->SetInAt(1, Location::RequiresRegister());
2218 }
2219 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2220 locations->SetInAt(2, Location::RequiresRegister());
2221 }
2222 locations->SetOut(Location::SameAsFirstInput());
2223}
2224
2225void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
2226 LocationSummary* locations = select->GetLocations();
2227 Label false_target;
2228 GenerateTestAndBranch(select,
2229 /* condition_input_index */ 2,
2230 /* true_target */ nullptr,
2231 &false_target);
2232 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2233 __ Bind(&false_target);
2234}
2235
David Srbecky0cf44932015-12-09 14:09:59 +00002236void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2237 new (GetGraph()->GetArena()) LocationSummary(info);
2238}
2239
David Srbeckyd28f4a02016-03-14 17:14:24 +00002240void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2241 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002242}
2243
2244void CodeGeneratorARM::GenerateNop() {
2245 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002246}
2247
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002248void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002249 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002250 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002251 // Handle the long/FP comparisons made in instruction simplification.
2252 switch (cond->InputAt(0)->GetType()) {
2253 case Primitive::kPrimLong:
2254 locations->SetInAt(0, Location::RequiresRegister());
2255 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002256 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002257 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2258 }
2259 break;
2260
2261 case Primitive::kPrimFloat:
2262 case Primitive::kPrimDouble:
2263 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002264 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002265 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002266 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2267 }
2268 break;
2269
2270 default:
2271 locations->SetInAt(0, Location::RequiresRegister());
2272 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002273 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2275 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002276 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002277}
2278
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002279void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002280 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002281 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002282 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002283
2284 LocationSummary* locations = cond->GetLocations();
2285 Location left = locations->InAt(0);
2286 Location right = locations->InAt(1);
2287 Register out = locations->Out().AsRegister<Register>();
2288 Label true_label, false_label;
2289
2290 switch (cond->InputAt(0)->GetType()) {
2291 default: {
2292 // Integer case.
2293 if (right.IsRegister()) {
2294 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2295 } else {
2296 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002297 __ CmpConstant(left.AsRegister<Register>(),
2298 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002299 }
Aart Bike9f37602015-10-09 11:15:55 -07002300 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002301 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002302 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002303 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002304 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002305 return;
2306 }
2307 case Primitive::kPrimLong:
2308 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2309 break;
2310 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002311 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002312 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002313 GenerateFPJumps(cond, &true_label, &false_label);
2314 break;
2315 }
2316
2317 // Convert the jumps into the result.
2318 Label done_label;
2319
2320 // False case: result = 0.
2321 __ Bind(&false_label);
2322 __ LoadImmediate(out, 0);
2323 __ b(&done_label);
2324
2325 // True case: result = 1.
2326 __ Bind(&true_label);
2327 __ LoadImmediate(out, 1);
2328 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002329}
2330
2331void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002332 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002333}
2334
2335void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002336 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002337}
2338
2339void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002340 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002341}
2342
2343void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002344 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002345}
2346
2347void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002348 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002349}
2350
2351void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002352 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002353}
2354
2355void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002356 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002357}
2358
2359void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002360 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002361}
2362
2363void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002364 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002365}
2366
2367void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002368 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002369}
2370
2371void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002372 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002373}
2374
2375void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002376 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002377}
2378
Aart Bike9f37602015-10-09 11:15:55 -07002379void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002380 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002381}
2382
2383void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002384 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002385}
2386
2387void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002388 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002389}
2390
2391void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002392 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002393}
2394
2395void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002396 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002397}
2398
2399void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002400 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002401}
2402
2403void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002404 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002405}
2406
2407void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002408 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002409}
2410
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002411void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002412 LocationSummary* locations =
2413 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002414 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002415}
2416
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002417void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002418 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002419}
2420
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002421void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2422 LocationSummary* locations =
2423 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2424 locations->SetOut(Location::ConstantLocation(constant));
2425}
2426
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002427void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002428 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002429}
2430
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002431void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002432 LocationSummary* locations =
2433 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002434 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002435}
2436
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002437void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002438 // Will be generated at use site.
2439}
2440
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002441void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2442 LocationSummary* locations =
2443 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2444 locations->SetOut(Location::ConstantLocation(constant));
2445}
2446
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002447void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002448 // Will be generated at use site.
2449}
2450
2451void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2452 LocationSummary* locations =
2453 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2454 locations->SetOut(Location::ConstantLocation(constant));
2455}
2456
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002457void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002458 // Will be generated at use site.
2459}
2460
Calin Juravle27df7582015-04-17 19:12:31 +01002461void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2462 memory_barrier->SetLocations(nullptr);
2463}
2464
2465void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002466 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002467}
2468
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002469void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002470 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002471}
2472
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002473void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002474 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002475}
2476
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002477void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002478 LocationSummary* locations =
2479 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002480 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002481}
2482
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002483void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002484 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002485}
2486
Calin Juravle175dc732015-08-25 15:42:32 +01002487void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2488 // The trampoline uses the same calling convention as dex calling conventions,
2489 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2490 // the method_idx.
2491 HandleInvoke(invoke);
2492}
2493
2494void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2495 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2496}
2497
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002498void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002499 // Explicit clinit checks triggered by static invokes must have been pruned by
2500 // art::PrepareForRegisterAllocation.
2501 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002502
Vladimir Marko68c981f2016-08-26 13:13:33 +01002503 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002504 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002505 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2506 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2507 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002508 return;
2509 }
2510
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002511 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002512
2513 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2514 if (invoke->HasPcRelativeDexCache()) {
2515 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2516 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002517}
2518
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002519static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2520 if (invoke->GetLocations()->Intrinsified()) {
2521 IntrinsicCodeGeneratorARM intrinsic(codegen);
2522 intrinsic.Dispatch(invoke);
2523 return true;
2524 }
2525 return false;
2526}
2527
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002528void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002529 // Explicit clinit checks triggered by static invokes must have been pruned by
2530 // art::PrepareForRegisterAllocation.
2531 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002532
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002533 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2534 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002535 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002536
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002537 LocationSummary* locations = invoke->GetLocations();
2538 codegen_->GenerateStaticOrDirectCall(
2539 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002540 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002541}
2542
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002543void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002544 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002545 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002546}
2547
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002548void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002549 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002550 if (intrinsic.TryDispatch(invoke)) {
2551 return;
2552 }
2553
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002554 HandleInvoke(invoke);
2555}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002556
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002557void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002558 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2559 return;
2560 }
2561
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002562 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002563 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002564 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002565}
2566
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002567void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2568 HandleInvoke(invoke);
2569 // Add the hidden argument.
2570 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2571}
2572
2573void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2574 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002575 LocationSummary* locations = invoke->GetLocations();
2576 Register temp = locations->GetTemp(0).AsRegister<Register>();
2577 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002578 Location receiver = locations->InAt(0);
2579 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2580
Roland Levillain3b359c72015-11-17 19:35:12 +00002581 // Set the hidden argument. This is safe to do this here, as R12
2582 // won't be modified thereafter, before the `blx` (call) instruction.
2583 DCHECK_EQ(R12, hidden_reg);
2584 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002585
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002586 if (receiver.IsStackSlot()) {
2587 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002588 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002589 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2590 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002591 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002592 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002593 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002594 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002595 // Instead of simply (possibly) unpoisoning `temp` here, we should
2596 // emit a read barrier for the previous class reference load.
2597 // However this is not required in practice, as this is an
2598 // intermediate/temporary reference and because the current
2599 // concurrent copying collector keeps the from-space memory
2600 // intact/accessible until the end of the marking phase (the
2601 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002602 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002603 __ LoadFromOffset(kLoadWord, temp, temp,
2604 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2605 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002606 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002607 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002608 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002609 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002610 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002611 // LR = temp->GetEntryPoint();
2612 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2613 // LR();
2614 __ blx(LR);
2615 DCHECK(!codegen_->IsLeafMethod());
2616 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2617}
2618
Orion Hodsonac141392017-01-13 11:53:47 +00002619void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2620 HandleInvoke(invoke);
2621}
2622
2623void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2624 codegen_->GenerateInvokePolymorphicCall(invoke);
2625}
2626
Roland Levillain88cb1752014-10-20 16:36:47 +01002627void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2628 LocationSummary* locations =
2629 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2630 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002631 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002632 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2634 break;
2635 }
2636 case Primitive::kPrimLong: {
2637 locations->SetInAt(0, Location::RequiresRegister());
2638 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002639 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002640 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002641
Roland Levillain88cb1752014-10-20 16:36:47 +01002642 case Primitive::kPrimFloat:
2643 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002644 locations->SetInAt(0, Location::RequiresFpuRegister());
2645 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002646 break;
2647
2648 default:
2649 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2650 }
2651}
2652
2653void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2654 LocationSummary* locations = neg->GetLocations();
2655 Location out = locations->Out();
2656 Location in = locations->InAt(0);
2657 switch (neg->GetResultType()) {
2658 case Primitive::kPrimInt:
2659 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002660 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002661 break;
2662
2663 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002664 DCHECK(in.IsRegisterPair());
2665 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2666 __ rsbs(out.AsRegisterPairLow<Register>(),
2667 in.AsRegisterPairLow<Register>(),
2668 ShifterOperand(0));
2669 // We cannot emit an RSC (Reverse Subtract with Carry)
2670 // instruction here, as it does not exist in the Thumb-2
2671 // instruction set. We use the following approach
2672 // using SBC and SUB instead.
2673 //
2674 // out.hi = -C
2675 __ sbc(out.AsRegisterPairHigh<Register>(),
2676 out.AsRegisterPairHigh<Register>(),
2677 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2678 // out.hi = out.hi - in.hi
2679 __ sub(out.AsRegisterPairHigh<Register>(),
2680 out.AsRegisterPairHigh<Register>(),
2681 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2682 break;
2683
Roland Levillain88cb1752014-10-20 16:36:47 +01002684 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002685 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002687 break;
2688
Roland Levillain88cb1752014-10-20 16:36:47 +01002689 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002690 DCHECK(in.IsFpuRegisterPair());
2691 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2692 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002693 break;
2694
2695 default:
2696 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2697 }
2698}
2699
Roland Levillaindff1f282014-11-05 14:15:05 +00002700void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002701 Primitive::Type result_type = conversion->GetResultType();
2702 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002703 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002704
Roland Levillain5b3ee562015-04-14 16:02:41 +01002705 // The float-to-long, double-to-long and long-to-float type conversions
2706 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002707 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002708 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2709 && result_type == Primitive::kPrimLong)
2710 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002711 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002712 : LocationSummary::kNoCall;
2713 LocationSummary* locations =
2714 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2715
David Brazdilb2bd1c52015-03-25 11:17:37 +00002716 // The Java language does not allow treating boolean as an integral type but
2717 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002718
Roland Levillaindff1f282014-11-05 14:15:05 +00002719 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002720 case Primitive::kPrimByte:
2721 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002722 case Primitive::kPrimLong:
2723 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002724 case Primitive::kPrimBoolean:
2725 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002726 case Primitive::kPrimShort:
2727 case Primitive::kPrimInt:
2728 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002729 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002730 locations->SetInAt(0, Location::RequiresRegister());
2731 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2732 break;
2733
2734 default:
2735 LOG(FATAL) << "Unexpected type conversion from " << input_type
2736 << " to " << result_type;
2737 }
2738 break;
2739
Roland Levillain01a8d712014-11-14 16:27:39 +00002740 case Primitive::kPrimShort:
2741 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002742 case Primitive::kPrimLong:
2743 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002744 case Primitive::kPrimBoolean:
2745 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002746 case Primitive::kPrimByte:
2747 case Primitive::kPrimInt:
2748 case Primitive::kPrimChar:
2749 // Processing a Dex `int-to-short' instruction.
2750 locations->SetInAt(0, Location::RequiresRegister());
2751 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2752 break;
2753
2754 default:
2755 LOG(FATAL) << "Unexpected type conversion from " << input_type
2756 << " to " << result_type;
2757 }
2758 break;
2759
Roland Levillain946e1432014-11-11 17:35:19 +00002760 case Primitive::kPrimInt:
2761 switch (input_type) {
2762 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002763 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002764 locations->SetInAt(0, Location::Any());
2765 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2766 break;
2767
2768 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002769 // Processing a Dex `float-to-int' instruction.
2770 locations->SetInAt(0, Location::RequiresFpuRegister());
2771 locations->SetOut(Location::RequiresRegister());
2772 locations->AddTemp(Location::RequiresFpuRegister());
2773 break;
2774
Roland Levillain946e1432014-11-11 17:35:19 +00002775 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002776 // Processing a Dex `double-to-int' instruction.
2777 locations->SetInAt(0, Location::RequiresFpuRegister());
2778 locations->SetOut(Location::RequiresRegister());
2779 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002780 break;
2781
2782 default:
2783 LOG(FATAL) << "Unexpected type conversion from " << input_type
2784 << " to " << result_type;
2785 }
2786 break;
2787
Roland Levillaindff1f282014-11-05 14:15:05 +00002788 case Primitive::kPrimLong:
2789 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002790 case Primitive::kPrimBoolean:
2791 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002792 case Primitive::kPrimByte:
2793 case Primitive::kPrimShort:
2794 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002795 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002796 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002797 locations->SetInAt(0, Location::RequiresRegister());
2798 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2799 break;
2800
Roland Levillain624279f2014-12-04 11:54:28 +00002801 case Primitive::kPrimFloat: {
2802 // Processing a Dex `float-to-long' instruction.
2803 InvokeRuntimeCallingConvention calling_convention;
2804 locations->SetInAt(0, Location::FpuRegisterLocation(
2805 calling_convention.GetFpuRegisterAt(0)));
2806 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2807 break;
2808 }
2809
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002810 case Primitive::kPrimDouble: {
2811 // Processing a Dex `double-to-long' instruction.
2812 InvokeRuntimeCallingConvention calling_convention;
2813 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2814 calling_convention.GetFpuRegisterAt(0),
2815 calling_convention.GetFpuRegisterAt(1)));
2816 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002817 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002818 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002819
2820 default:
2821 LOG(FATAL) << "Unexpected type conversion from " << input_type
2822 << " to " << result_type;
2823 }
2824 break;
2825
Roland Levillain981e4542014-11-14 11:47:14 +00002826 case Primitive::kPrimChar:
2827 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002828 case Primitive::kPrimLong:
2829 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002830 case Primitive::kPrimBoolean:
2831 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002832 case Primitive::kPrimByte:
2833 case Primitive::kPrimShort:
2834 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002835 // Processing a Dex `int-to-char' instruction.
2836 locations->SetInAt(0, Location::RequiresRegister());
2837 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2838 break;
2839
2840 default:
2841 LOG(FATAL) << "Unexpected type conversion from " << input_type
2842 << " to " << result_type;
2843 }
2844 break;
2845
Roland Levillaindff1f282014-11-05 14:15:05 +00002846 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002847 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002848 case Primitive::kPrimBoolean:
2849 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002850 case Primitive::kPrimByte:
2851 case Primitive::kPrimShort:
2852 case Primitive::kPrimInt:
2853 case Primitive::kPrimChar:
2854 // Processing a Dex `int-to-float' instruction.
2855 locations->SetInAt(0, Location::RequiresRegister());
2856 locations->SetOut(Location::RequiresFpuRegister());
2857 break;
2858
Roland Levillain5b3ee562015-04-14 16:02:41 +01002859 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002860 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002861 InvokeRuntimeCallingConvention calling_convention;
2862 locations->SetInAt(0, Location::RegisterPairLocation(
2863 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2864 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002865 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002866 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002867
Roland Levillaincff13742014-11-17 14:32:17 +00002868 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002869 // Processing a Dex `double-to-float' instruction.
2870 locations->SetInAt(0, Location::RequiresFpuRegister());
2871 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002872 break;
2873
2874 default:
2875 LOG(FATAL) << "Unexpected type conversion from " << input_type
2876 << " to " << result_type;
2877 };
2878 break;
2879
Roland Levillaindff1f282014-11-05 14:15:05 +00002880 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002881 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002882 case Primitive::kPrimBoolean:
2883 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002884 case Primitive::kPrimByte:
2885 case Primitive::kPrimShort:
2886 case Primitive::kPrimInt:
2887 case Primitive::kPrimChar:
2888 // Processing a Dex `int-to-double' instruction.
2889 locations->SetInAt(0, Location::RequiresRegister());
2890 locations->SetOut(Location::RequiresFpuRegister());
2891 break;
2892
2893 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002894 // Processing a Dex `long-to-double' instruction.
2895 locations->SetInAt(0, Location::RequiresRegister());
2896 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002897 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002898 locations->AddTemp(Location::RequiresFpuRegister());
2899 break;
2900
Roland Levillaincff13742014-11-17 14:32:17 +00002901 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002902 // Processing a Dex `float-to-double' instruction.
2903 locations->SetInAt(0, Location::RequiresFpuRegister());
2904 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002905 break;
2906
2907 default:
2908 LOG(FATAL) << "Unexpected type conversion from " << input_type
2909 << " to " << result_type;
2910 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002911 break;
2912
2913 default:
2914 LOG(FATAL) << "Unexpected type conversion from " << input_type
2915 << " to " << result_type;
2916 }
2917}
2918
2919void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2920 LocationSummary* locations = conversion->GetLocations();
2921 Location out = locations->Out();
2922 Location in = locations->InAt(0);
2923 Primitive::Type result_type = conversion->GetResultType();
2924 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002925 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002926 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002927 case Primitive::kPrimByte:
2928 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002929 case Primitive::kPrimLong:
2930 // Type conversion from long to byte is a result of code transformations.
2931 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2932 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002933 case Primitive::kPrimBoolean:
2934 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002935 case Primitive::kPrimShort:
2936 case Primitive::kPrimInt:
2937 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002938 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002939 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002940 break;
2941
2942 default:
2943 LOG(FATAL) << "Unexpected type conversion from " << input_type
2944 << " to " << result_type;
2945 }
2946 break;
2947
Roland Levillain01a8d712014-11-14 16:27:39 +00002948 case Primitive::kPrimShort:
2949 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002950 case Primitive::kPrimLong:
2951 // Type conversion from long to short is a result of code transformations.
2952 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2953 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002954 case Primitive::kPrimBoolean:
2955 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002956 case Primitive::kPrimByte:
2957 case Primitive::kPrimInt:
2958 case Primitive::kPrimChar:
2959 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002960 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002961 break;
2962
2963 default:
2964 LOG(FATAL) << "Unexpected type conversion from " << input_type
2965 << " to " << result_type;
2966 }
2967 break;
2968
Roland Levillain946e1432014-11-11 17:35:19 +00002969 case Primitive::kPrimInt:
2970 switch (input_type) {
2971 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002972 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002973 DCHECK(out.IsRegister());
2974 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002975 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002976 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002977 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002978 } else {
2979 DCHECK(in.IsConstant());
2980 DCHECK(in.GetConstant()->IsLongConstant());
2981 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002982 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002983 }
2984 break;
2985
Roland Levillain3f8f9362014-12-02 17:45:01 +00002986 case Primitive::kPrimFloat: {
2987 // Processing a Dex `float-to-int' instruction.
2988 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002989 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002990 __ vmovrs(out.AsRegister<Register>(), temp);
2991 break;
2992 }
2993
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002994 case Primitive::kPrimDouble: {
2995 // Processing a Dex `double-to-int' instruction.
2996 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002997 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002998 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002999 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003000 }
Roland Levillain946e1432014-11-11 17:35:19 +00003001
3002 default:
3003 LOG(FATAL) << "Unexpected type conversion from " << input_type
3004 << " to " << result_type;
3005 }
3006 break;
3007
Roland Levillaindff1f282014-11-05 14:15:05 +00003008 case Primitive::kPrimLong:
3009 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003010 case Primitive::kPrimBoolean:
3011 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003012 case Primitive::kPrimByte:
3013 case Primitive::kPrimShort:
3014 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003015 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003016 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003017 DCHECK(out.IsRegisterPair());
3018 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003019 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00003020 // Sign extension.
3021 __ Asr(out.AsRegisterPairHigh<Register>(),
3022 out.AsRegisterPairLow<Register>(),
3023 31);
3024 break;
3025
3026 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00003027 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003028 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003029 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003030 break;
3031
Roland Levillaindff1f282014-11-05 14:15:05 +00003032 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003033 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003034 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003035 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003036 break;
3037
3038 default:
3039 LOG(FATAL) << "Unexpected type conversion from " << input_type
3040 << " to " << result_type;
3041 }
3042 break;
3043
Roland Levillain981e4542014-11-14 11:47:14 +00003044 case Primitive::kPrimChar:
3045 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003046 case Primitive::kPrimLong:
3047 // Type conversion from long to char is a result of code transformations.
3048 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3049 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003050 case Primitive::kPrimBoolean:
3051 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003052 case Primitive::kPrimByte:
3053 case Primitive::kPrimShort:
3054 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003055 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003056 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00003057 break;
3058
3059 default:
3060 LOG(FATAL) << "Unexpected type conversion from " << input_type
3061 << " to " << result_type;
3062 }
3063 break;
3064
Roland Levillaindff1f282014-11-05 14:15:05 +00003065 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003066 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003067 case Primitive::kPrimBoolean:
3068 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003069 case Primitive::kPrimByte:
3070 case Primitive::kPrimShort:
3071 case Primitive::kPrimInt:
3072 case Primitive::kPrimChar: {
3073 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003074 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
3075 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003076 break;
3077 }
3078
Roland Levillain5b3ee562015-04-14 16:02:41 +01003079 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003080 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003081 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003082 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00003083 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003084
Roland Levillaincff13742014-11-17 14:32:17 +00003085 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003086 // Processing a Dex `double-to-float' instruction.
3087 __ vcvtsd(out.AsFpuRegister<SRegister>(),
3088 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00003089 break;
3090
3091 default:
3092 LOG(FATAL) << "Unexpected type conversion from " << input_type
3093 << " to " << result_type;
3094 };
3095 break;
3096
Roland Levillaindff1f282014-11-05 14:15:05 +00003097 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003098 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003099 case Primitive::kPrimBoolean:
3100 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003101 case Primitive::kPrimByte:
3102 case Primitive::kPrimShort:
3103 case Primitive::kPrimInt:
3104 case Primitive::kPrimChar: {
3105 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003106 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003107 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3108 out.AsFpuRegisterPairLow<SRegister>());
3109 break;
3110 }
3111
Roland Levillain647b9ed2014-11-27 12:06:00 +00003112 case Primitive::kPrimLong: {
3113 // Processing a Dex `long-to-double' instruction.
3114 Register low = in.AsRegisterPairLow<Register>();
3115 Register high = in.AsRegisterPairHigh<Register>();
3116 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
3117 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003118 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00003119 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003120 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
3121 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003122
Roland Levillain682393c2015-04-14 15:57:52 +01003123 // temp_d = int-to-double(high)
3124 __ vmovsr(temp_s, high);
3125 __ vcvtdi(temp_d, temp_s);
3126 // constant_d = k2Pow32EncodingForDouble
3127 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
3128 // out_d = unsigned-to-double(low)
3129 __ vmovsr(out_s, low);
3130 __ vcvtdu(out_d, out_s);
3131 // out_d += temp_d * constant_d
3132 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003133 break;
3134 }
3135
Roland Levillaincff13742014-11-17 14:32:17 +00003136 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003137 // Processing a Dex `float-to-double' instruction.
3138 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3139 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003140 break;
3141
3142 default:
3143 LOG(FATAL) << "Unexpected type conversion from " << input_type
3144 << " to " << result_type;
3145 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003146 break;
3147
3148 default:
3149 LOG(FATAL) << "Unexpected type conversion from " << input_type
3150 << " to " << result_type;
3151 }
3152}
3153
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003154void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003155 LocationSummary* locations =
3156 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003157 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003158 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003159 locations->SetInAt(0, Location::RequiresRegister());
3160 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003161 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3162 break;
3163 }
3164
3165 case Primitive::kPrimLong: {
3166 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003167 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003168 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003169 break;
3170 }
3171
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003172 case Primitive::kPrimFloat:
3173 case Primitive::kPrimDouble: {
3174 locations->SetInAt(0, Location::RequiresFpuRegister());
3175 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003176 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003177 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003178 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003179
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003180 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003181 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003182 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003183}
3184
3185void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
3186 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003187 Location out = locations->Out();
3188 Location first = locations->InAt(0);
3189 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003190 switch (add->GetResultType()) {
3191 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003192 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003193 __ add(out.AsRegister<Register>(),
3194 first.AsRegister<Register>(),
3195 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003196 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003197 __ AddConstant(out.AsRegister<Register>(),
3198 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003199 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003200 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003201 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003202
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003203 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003204 if (second.IsConstant()) {
3205 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3206 GenerateAddLongConst(out, first, value);
3207 } else {
3208 DCHECK(second.IsRegisterPair());
3209 __ adds(out.AsRegisterPairLow<Register>(),
3210 first.AsRegisterPairLow<Register>(),
3211 ShifterOperand(second.AsRegisterPairLow<Register>()));
3212 __ adc(out.AsRegisterPairHigh<Register>(),
3213 first.AsRegisterPairHigh<Register>(),
3214 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3215 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003216 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003217 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003218
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003219 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00003220 __ vadds(out.AsFpuRegister<SRegister>(),
3221 first.AsFpuRegister<SRegister>(),
3222 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003223 break;
3224
3225 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003226 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3227 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3228 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003229 break;
3230
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003231 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003232 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003233 }
3234}
3235
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003236void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003237 LocationSummary* locations =
3238 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003239 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003240 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003241 locations->SetInAt(0, Location::RequiresRegister());
3242 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003243 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3244 break;
3245 }
3246
3247 case Primitive::kPrimLong: {
3248 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003249 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003250 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003251 break;
3252 }
Calin Juravle11351682014-10-23 15:38:15 +01003253 case Primitive::kPrimFloat:
3254 case Primitive::kPrimDouble: {
3255 locations->SetInAt(0, Location::RequiresFpuRegister());
3256 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003257 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003258 break;
Calin Juravle11351682014-10-23 15:38:15 +01003259 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003260 default:
Calin Juravle11351682014-10-23 15:38:15 +01003261 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003262 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003263}
3264
3265void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3266 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003267 Location out = locations->Out();
3268 Location first = locations->InAt(0);
3269 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003270 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003271 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003272 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003273 __ sub(out.AsRegister<Register>(),
3274 first.AsRegister<Register>(),
3275 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003276 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003277 __ AddConstant(out.AsRegister<Register>(),
3278 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003279 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003280 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003281 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003282 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003283
Calin Juravle11351682014-10-23 15:38:15 +01003284 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003285 if (second.IsConstant()) {
3286 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3287 GenerateAddLongConst(out, first, -value);
3288 } else {
3289 DCHECK(second.IsRegisterPair());
3290 __ subs(out.AsRegisterPairLow<Register>(),
3291 first.AsRegisterPairLow<Register>(),
3292 ShifterOperand(second.AsRegisterPairLow<Register>()));
3293 __ sbc(out.AsRegisterPairHigh<Register>(),
3294 first.AsRegisterPairHigh<Register>(),
3295 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3296 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003297 break;
Calin Juravle11351682014-10-23 15:38:15 +01003298 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003299
Calin Juravle11351682014-10-23 15:38:15 +01003300 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003301 __ vsubs(out.AsFpuRegister<SRegister>(),
3302 first.AsFpuRegister<SRegister>(),
3303 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003304 break;
Calin Juravle11351682014-10-23 15:38:15 +01003305 }
3306
3307 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003308 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3309 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3310 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003311 break;
3312 }
3313
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003314
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003315 default:
Calin Juravle11351682014-10-23 15:38:15 +01003316 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003317 }
3318}
3319
Calin Juravle34bacdf2014-10-07 20:23:36 +01003320void LocationsBuilderARM::VisitMul(HMul* mul) {
3321 LocationSummary* locations =
3322 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3323 switch (mul->GetResultType()) {
3324 case Primitive::kPrimInt:
3325 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003326 locations->SetInAt(0, Location::RequiresRegister());
3327 locations->SetInAt(1, Location::RequiresRegister());
3328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003329 break;
3330 }
3331
Calin Juravleb5bfa962014-10-21 18:02:24 +01003332 case Primitive::kPrimFloat:
3333 case Primitive::kPrimDouble: {
3334 locations->SetInAt(0, Location::RequiresFpuRegister());
3335 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003336 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003337 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003338 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003339
3340 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003341 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003342 }
3343}
3344
3345void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3346 LocationSummary* locations = mul->GetLocations();
3347 Location out = locations->Out();
3348 Location first = locations->InAt(0);
3349 Location second = locations->InAt(1);
3350 switch (mul->GetResultType()) {
3351 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003352 __ mul(out.AsRegister<Register>(),
3353 first.AsRegister<Register>(),
3354 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003355 break;
3356 }
3357 case Primitive::kPrimLong: {
3358 Register out_hi = out.AsRegisterPairHigh<Register>();
3359 Register out_lo = out.AsRegisterPairLow<Register>();
3360 Register in1_hi = first.AsRegisterPairHigh<Register>();
3361 Register in1_lo = first.AsRegisterPairLow<Register>();
3362 Register in2_hi = second.AsRegisterPairHigh<Register>();
3363 Register in2_lo = second.AsRegisterPairLow<Register>();
3364
3365 // Extra checks to protect caused by the existence of R1_R2.
3366 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3367 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3368 DCHECK_NE(out_hi, in1_lo);
3369 DCHECK_NE(out_hi, in2_lo);
3370
3371 // input: in1 - 64 bits, in2 - 64 bits
3372 // output: out
3373 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3374 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3375 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3376
3377 // IP <- in1.lo * in2.hi
3378 __ mul(IP, in1_lo, in2_hi);
3379 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3380 __ mla(out_hi, in1_hi, in2_lo, IP);
3381 // out.lo <- (in1.lo * in2.lo)[31:0];
3382 __ umull(out_lo, IP, in1_lo, in2_lo);
3383 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3384 __ add(out_hi, out_hi, ShifterOperand(IP));
3385 break;
3386 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003387
3388 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003389 __ vmuls(out.AsFpuRegister<SRegister>(),
3390 first.AsFpuRegister<SRegister>(),
3391 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003392 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003393 }
3394
3395 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003396 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3397 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3398 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003399 break;
3400 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003401
3402 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003403 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003404 }
3405}
3406
Zheng Xuc6667102015-05-15 16:08:45 +08003407void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3408 DCHECK(instruction->IsDiv() || instruction->IsRem());
3409 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3410
3411 LocationSummary* locations = instruction->GetLocations();
3412 Location second = locations->InAt(1);
3413 DCHECK(second.IsConstant());
3414
3415 Register out = locations->Out().AsRegister<Register>();
3416 Register dividend = locations->InAt(0).AsRegister<Register>();
3417 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3418 DCHECK(imm == 1 || imm == -1);
3419
3420 if (instruction->IsRem()) {
3421 __ LoadImmediate(out, 0);
3422 } else {
3423 if (imm == 1) {
3424 __ Mov(out, dividend);
3425 } else {
3426 __ rsb(out, dividend, ShifterOperand(0));
3427 }
3428 }
3429}
3430
3431void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3432 DCHECK(instruction->IsDiv() || instruction->IsRem());
3433 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3434
3435 LocationSummary* locations = instruction->GetLocations();
3436 Location second = locations->InAt(1);
3437 DCHECK(second.IsConstant());
3438
3439 Register out = locations->Out().AsRegister<Register>();
3440 Register dividend = locations->InAt(0).AsRegister<Register>();
3441 Register temp = locations->GetTemp(0).AsRegister<Register>();
3442 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003443 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003444 int ctz_imm = CTZ(abs_imm);
3445
3446 if (ctz_imm == 1) {
3447 __ Lsr(temp, dividend, 32 - ctz_imm);
3448 } else {
3449 __ Asr(temp, dividend, 31);
3450 __ Lsr(temp, temp, 32 - ctz_imm);
3451 }
3452 __ add(out, temp, ShifterOperand(dividend));
3453
3454 if (instruction->IsDiv()) {
3455 __ Asr(out, out, ctz_imm);
3456 if (imm < 0) {
3457 __ rsb(out, out, ShifterOperand(0));
3458 }
3459 } else {
3460 __ ubfx(out, out, 0, ctz_imm);
3461 __ sub(out, out, ShifterOperand(temp));
3462 }
3463}
3464
3465void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3466 DCHECK(instruction->IsDiv() || instruction->IsRem());
3467 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3468
3469 LocationSummary* locations = instruction->GetLocations();
3470 Location second = locations->InAt(1);
3471 DCHECK(second.IsConstant());
3472
3473 Register out = locations->Out().AsRegister<Register>();
3474 Register dividend = locations->InAt(0).AsRegister<Register>();
3475 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3476 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3477 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3478
3479 int64_t magic;
3480 int shift;
3481 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3482
3483 __ LoadImmediate(temp1, magic);
3484 __ smull(temp2, temp1, dividend, temp1);
3485
3486 if (imm > 0 && magic < 0) {
3487 __ add(temp1, temp1, ShifterOperand(dividend));
3488 } else if (imm < 0 && magic > 0) {
3489 __ sub(temp1, temp1, ShifterOperand(dividend));
3490 }
3491
3492 if (shift != 0) {
3493 __ Asr(temp1, temp1, shift);
3494 }
3495
3496 if (instruction->IsDiv()) {
3497 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3498 } else {
3499 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3500 // TODO: Strength reduction for mls.
3501 __ LoadImmediate(temp2, imm);
3502 __ mls(out, temp1, temp2, dividend);
3503 }
3504}
3505
3506void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3507 DCHECK(instruction->IsDiv() || instruction->IsRem());
3508 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3509
3510 LocationSummary* locations = instruction->GetLocations();
3511 Location second = locations->InAt(1);
3512 DCHECK(second.IsConstant());
3513
3514 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3515 if (imm == 0) {
3516 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3517 } else if (imm == 1 || imm == -1) {
3518 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003519 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003520 DivRemByPowerOfTwo(instruction);
3521 } else {
3522 DCHECK(imm <= -2 || imm >= 2);
3523 GenerateDivRemWithAnyConstant(instruction);
3524 }
3525}
3526
Calin Juravle7c4954d2014-10-28 16:57:40 +00003527void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003528 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3529 if (div->GetResultType() == Primitive::kPrimLong) {
3530 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003531 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003532 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3533 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003534 } else if (div->GetResultType() == Primitive::kPrimInt &&
3535 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3536 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003537 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003538 }
3539
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003540 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3541
Calin Juravle7c4954d2014-10-28 16:57:40 +00003542 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003543 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003544 if (div->InputAt(1)->IsConstant()) {
3545 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003546 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003547 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003548 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3549 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003550 // No temp register required.
3551 } else {
3552 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003553 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003554 locations->AddTemp(Location::RequiresRegister());
3555 }
3556 }
3557 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003558 locations->SetInAt(0, Location::RequiresRegister());
3559 locations->SetInAt(1, Location::RequiresRegister());
3560 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3561 } else {
3562 InvokeRuntimeCallingConvention calling_convention;
3563 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3564 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003565 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003566 // we only need the former.
3567 locations->SetOut(Location::RegisterLocation(R0));
3568 }
Calin Juravled0d48522014-11-04 16:40:20 +00003569 break;
3570 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003571 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003572 InvokeRuntimeCallingConvention calling_convention;
3573 locations->SetInAt(0, Location::RegisterPairLocation(
3574 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3575 locations->SetInAt(1, Location::RegisterPairLocation(
3576 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003577 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003578 break;
3579 }
3580 case Primitive::kPrimFloat:
3581 case Primitive::kPrimDouble: {
3582 locations->SetInAt(0, Location::RequiresFpuRegister());
3583 locations->SetInAt(1, Location::RequiresFpuRegister());
3584 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3585 break;
3586 }
3587
3588 default:
3589 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3590 }
3591}
3592
3593void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3594 LocationSummary* locations = div->GetLocations();
3595 Location out = locations->Out();
3596 Location first = locations->InAt(0);
3597 Location second = locations->InAt(1);
3598
3599 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003600 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003601 if (second.IsConstant()) {
3602 GenerateDivRemConstantIntegral(div);
3603 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003604 __ sdiv(out.AsRegister<Register>(),
3605 first.AsRegister<Register>(),
3606 second.AsRegister<Register>());
3607 } else {
3608 InvokeRuntimeCallingConvention calling_convention;
3609 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3610 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3611 DCHECK_EQ(R0, out.AsRegister<Register>());
3612
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003613 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003614 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003615 }
Calin Juravled0d48522014-11-04 16:40:20 +00003616 break;
3617 }
3618
Calin Juravle7c4954d2014-10-28 16:57:40 +00003619 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003620 InvokeRuntimeCallingConvention calling_convention;
3621 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3622 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3623 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3624 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3625 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003626 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003627
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003628 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003629 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003630 break;
3631 }
3632
3633 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003634 __ vdivs(out.AsFpuRegister<SRegister>(),
3635 first.AsFpuRegister<SRegister>(),
3636 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003637 break;
3638 }
3639
3640 case Primitive::kPrimDouble: {
3641 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3642 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3643 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3644 break;
3645 }
3646
3647 default:
3648 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3649 }
3650}
3651
Calin Juravlebacfec32014-11-14 15:54:36 +00003652void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003653 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003654
3655 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003656 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003657 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3658 // sdiv will be replaced by other instruction sequence.
3659 call_kind = LocationSummary::kNoCall;
3660 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3661 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003662 // Have hardware divide instruction for int, do it with three instructions.
3663 call_kind = LocationSummary::kNoCall;
3664 }
3665
Calin Juravlebacfec32014-11-14 15:54:36 +00003666 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3667
Calin Juravled2ec87d2014-12-08 14:24:46 +00003668 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003669 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003670 if (rem->InputAt(1)->IsConstant()) {
3671 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003672 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003673 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003674 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3675 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003676 // No temp register required.
3677 } else {
3678 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003679 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003680 locations->AddTemp(Location::RequiresRegister());
3681 }
3682 }
3683 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003684 locations->SetInAt(0, Location::RequiresRegister());
3685 locations->SetInAt(1, Location::RequiresRegister());
3686 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3687 locations->AddTemp(Location::RequiresRegister());
3688 } else {
3689 InvokeRuntimeCallingConvention calling_convention;
3690 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3691 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003692 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003693 // we only need the latter.
3694 locations->SetOut(Location::RegisterLocation(R1));
3695 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003696 break;
3697 }
3698 case Primitive::kPrimLong: {
3699 InvokeRuntimeCallingConvention calling_convention;
3700 locations->SetInAt(0, Location::RegisterPairLocation(
3701 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3702 locations->SetInAt(1, Location::RegisterPairLocation(
3703 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3704 // The runtime helper puts the output in R2,R3.
3705 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3706 break;
3707 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003708 case Primitive::kPrimFloat: {
3709 InvokeRuntimeCallingConvention calling_convention;
3710 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3711 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3712 locations->SetOut(Location::FpuRegisterLocation(S0));
3713 break;
3714 }
3715
Calin Juravlebacfec32014-11-14 15:54:36 +00003716 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003717 InvokeRuntimeCallingConvention calling_convention;
3718 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3719 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3720 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3721 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3722 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003723 break;
3724 }
3725
3726 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003727 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003728 }
3729}
3730
3731void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3732 LocationSummary* locations = rem->GetLocations();
3733 Location out = locations->Out();
3734 Location first = locations->InAt(0);
3735 Location second = locations->InAt(1);
3736
Calin Juravled2ec87d2014-12-08 14:24:46 +00003737 Primitive::Type type = rem->GetResultType();
3738 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003739 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003740 if (second.IsConstant()) {
3741 GenerateDivRemConstantIntegral(rem);
3742 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003743 Register reg1 = first.AsRegister<Register>();
3744 Register reg2 = second.AsRegister<Register>();
3745 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003746
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003747 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003748 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003749 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003750 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003751 } else {
3752 InvokeRuntimeCallingConvention calling_convention;
3753 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3754 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3755 DCHECK_EQ(R1, out.AsRegister<Register>());
3756
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003757 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003758 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003759 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003760 break;
3761 }
3762
3763 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003764 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003765 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003766 break;
3767 }
3768
Calin Juravled2ec87d2014-12-08 14:24:46 +00003769 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003770 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003771 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003772 break;
3773 }
3774
Calin Juravlebacfec32014-11-14 15:54:36 +00003775 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003776 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003777 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003778 break;
3779 }
3780
3781 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003782 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003783 }
3784}
3785
Calin Juravled0d48522014-11-04 16:40:20 +00003786void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003787 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003788 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003789}
3790
3791void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003792 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003793 codegen_->AddSlowPath(slow_path);
3794
3795 LocationSummary* locations = instruction->GetLocations();
3796 Location value = locations->InAt(0);
3797
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003798 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003799 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003800 case Primitive::kPrimByte:
3801 case Primitive::kPrimChar:
3802 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003803 case Primitive::kPrimInt: {
3804 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003805 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003806 } else {
3807 DCHECK(value.IsConstant()) << value;
3808 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3809 __ b(slow_path->GetEntryLabel());
3810 }
3811 }
3812 break;
3813 }
3814 case Primitive::kPrimLong: {
3815 if (value.IsRegisterPair()) {
3816 __ orrs(IP,
3817 value.AsRegisterPairLow<Register>(),
3818 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3819 __ b(slow_path->GetEntryLabel(), EQ);
3820 } else {
3821 DCHECK(value.IsConstant()) << value;
3822 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3823 __ b(slow_path->GetEntryLabel());
3824 }
3825 }
3826 break;
3827 default:
3828 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3829 }
3830 }
Calin Juravled0d48522014-11-04 16:40:20 +00003831}
3832
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003833void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3834 Register in = locations->InAt(0).AsRegister<Register>();
3835 Location rhs = locations->InAt(1);
3836 Register out = locations->Out().AsRegister<Register>();
3837
3838 if (rhs.IsConstant()) {
3839 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3840 // so map all rotations to a +ve. equivalent in that range.
3841 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3842 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3843 if (rot) {
3844 // Rotate, mapping left rotations to right equivalents if necessary.
3845 // (e.g. left by 2 bits == right by 30.)
3846 __ Ror(out, in, rot);
3847 } else if (out != in) {
3848 __ Mov(out, in);
3849 }
3850 } else {
3851 __ Ror(out, in, rhs.AsRegister<Register>());
3852 }
3853}
3854
3855// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3856// rotates by swapping input regs (effectively rotating by the first 32-bits of
3857// a larger rotation) or flipping direction (thus treating larger right/left
3858// rotations as sub-word sized rotations in the other direction) as appropriate.
3859void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3860 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3861 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3862 Location rhs = locations->InAt(1);
3863 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3864 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3865
3866 if (rhs.IsConstant()) {
3867 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3868 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003869 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003870 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3871 // logic below to a simple pair of binary orr.
3872 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3873 if (rot >= kArmBitsPerWord) {
3874 rot -= kArmBitsPerWord;
3875 std::swap(in_reg_hi, in_reg_lo);
3876 }
3877 // Rotate, or mov to out for zero or word size rotations.
3878 if (rot != 0u) {
3879 __ Lsr(out_reg_hi, in_reg_hi, rot);
3880 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3881 __ Lsr(out_reg_lo, in_reg_lo, rot);
3882 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3883 } else {
3884 __ Mov(out_reg_lo, in_reg_lo);
3885 __ Mov(out_reg_hi, in_reg_hi);
3886 }
3887 } else {
3888 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3889 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3890 Label end;
3891 Label shift_by_32_plus_shift_right;
3892
3893 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3894 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3895 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3896 __ b(&shift_by_32_plus_shift_right, CC);
3897
3898 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3899 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3900 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3901 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3902 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3903 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3904 __ Lsr(shift_left, in_reg_hi, shift_right);
3905 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3906 __ b(&end);
3907
3908 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3909 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3910 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3911 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3912 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3913 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3914 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3915 __ Lsl(shift_right, in_reg_hi, shift_left);
3916 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3917
3918 __ Bind(&end);
3919 }
3920}
Roland Levillain22c49222016-03-18 14:04:28 +00003921
3922void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003923 LocationSummary* locations =
3924 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3925 switch (ror->GetResultType()) {
3926 case Primitive::kPrimInt: {
3927 locations->SetInAt(0, Location::RequiresRegister());
3928 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3929 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3930 break;
3931 }
3932 case Primitive::kPrimLong: {
3933 locations->SetInAt(0, Location::RequiresRegister());
3934 if (ror->InputAt(1)->IsConstant()) {
3935 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3936 } else {
3937 locations->SetInAt(1, Location::RequiresRegister());
3938 locations->AddTemp(Location::RequiresRegister());
3939 locations->AddTemp(Location::RequiresRegister());
3940 }
3941 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3942 break;
3943 }
3944 default:
3945 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3946 }
3947}
3948
Roland Levillain22c49222016-03-18 14:04:28 +00003949void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003950 LocationSummary* locations = ror->GetLocations();
3951 Primitive::Type type = ror->GetResultType();
3952 switch (type) {
3953 case Primitive::kPrimInt: {
3954 HandleIntegerRotate(locations);
3955 break;
3956 }
3957 case Primitive::kPrimLong: {
3958 HandleLongRotate(locations);
3959 break;
3960 }
3961 default:
3962 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003963 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003964 }
3965}
3966
Calin Juravle9aec02f2014-11-18 23:06:35 +00003967void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3968 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3969
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003970 LocationSummary* locations =
3971 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003972
3973 switch (op->GetResultType()) {
3974 case Primitive::kPrimInt: {
3975 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003976 if (op->InputAt(1)->IsConstant()) {
3977 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3978 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3979 } else {
3980 locations->SetInAt(1, Location::RequiresRegister());
3981 // Make the output overlap, as it will be used to hold the masked
3982 // second input.
3983 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3984 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003985 break;
3986 }
3987 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003988 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003989 if (op->InputAt(1)->IsConstant()) {
3990 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3991 // For simplicity, use kOutputOverlap even though we only require that low registers
3992 // don't clash with high registers which the register allocator currently guarantees.
3993 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3994 } else {
3995 locations->SetInAt(1, Location::RequiresRegister());
3996 locations->AddTemp(Location::RequiresRegister());
3997 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3998 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003999 break;
4000 }
4001 default:
4002 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4003 }
4004}
4005
4006void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
4007 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4008
4009 LocationSummary* locations = op->GetLocations();
4010 Location out = locations->Out();
4011 Location first = locations->InAt(0);
4012 Location second = locations->InAt(1);
4013
4014 Primitive::Type type = op->GetResultType();
4015 switch (type) {
4016 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004017 Register out_reg = out.AsRegister<Register>();
4018 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004019 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004020 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00004021 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004022 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004023 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004024 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004025 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004026 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004027 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004028 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004029 }
4030 } else {
4031 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004032 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00004033 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00004034 __ Mov(out_reg, first_reg);
4035 } else if (op->IsShl()) {
4036 __ Lsl(out_reg, first_reg, shift_value);
4037 } else if (op->IsShr()) {
4038 __ Asr(out_reg, first_reg, shift_value);
4039 } else {
4040 __ Lsr(out_reg, first_reg, shift_value);
4041 }
4042 }
4043 break;
4044 }
4045 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004046 Register o_h = out.AsRegisterPairHigh<Register>();
4047 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004048
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004049 Register high = first.AsRegisterPairHigh<Register>();
4050 Register low = first.AsRegisterPairLow<Register>();
4051
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004052 if (second.IsRegister()) {
4053 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004054
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004055 Register second_reg = second.AsRegister<Register>();
4056
4057 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004058 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004059 // Shift the high part
4060 __ Lsl(o_h, high, o_l);
4061 // Shift the low part and `or` what overflew on the high part
4062 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
4063 __ Lsr(temp, low, temp);
4064 __ orr(o_h, o_h, ShifterOperand(temp));
4065 // If the shift is > 32 bits, override the high part
4066 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
4067 __ it(PL);
4068 __ Lsl(o_h, low, temp, PL);
4069 // Shift the low part
4070 __ Lsl(o_l, low, o_l);
4071 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004072 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004073 // Shift the low part
4074 __ Lsr(o_l, low, o_h);
4075 // Shift the high part and `or` what underflew on the low part
4076 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4077 __ Lsl(temp, high, temp);
4078 __ orr(o_l, o_l, ShifterOperand(temp));
4079 // If the shift is > 32 bits, override the low part
4080 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4081 __ it(PL);
4082 __ Asr(o_l, high, temp, PL);
4083 // Shift the high part
4084 __ Asr(o_h, high, o_h);
4085 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004086 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004087 // same as Shr except we use `Lsr`s and not `Asr`s
4088 __ Lsr(o_l, low, o_h);
4089 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4090 __ Lsl(temp, high, temp);
4091 __ orr(o_l, o_l, ShifterOperand(temp));
4092 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4093 __ it(PL);
4094 __ Lsr(o_l, high, temp, PL);
4095 __ Lsr(o_h, high, o_h);
4096 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004097 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004098 // Register allocator doesn't create partial overlap.
4099 DCHECK_NE(o_l, high);
4100 DCHECK_NE(o_h, low);
4101 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004102 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004103 if (shift_value > 32) {
4104 if (op->IsShl()) {
4105 __ Lsl(o_h, low, shift_value - 32);
4106 __ LoadImmediate(o_l, 0);
4107 } else if (op->IsShr()) {
4108 __ Asr(o_l, high, shift_value - 32);
4109 __ Asr(o_h, high, 31);
4110 } else {
4111 __ Lsr(o_l, high, shift_value - 32);
4112 __ LoadImmediate(o_h, 0);
4113 }
4114 } else if (shift_value == 32) {
4115 if (op->IsShl()) {
4116 __ mov(o_h, ShifterOperand(low));
4117 __ LoadImmediate(o_l, 0);
4118 } else if (op->IsShr()) {
4119 __ mov(o_l, ShifterOperand(high));
4120 __ Asr(o_h, high, 31);
4121 } else {
4122 __ mov(o_l, ShifterOperand(high));
4123 __ LoadImmediate(o_h, 0);
4124 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00004125 } else if (shift_value == 1) {
4126 if (op->IsShl()) {
4127 __ Lsls(o_l, low, 1);
4128 __ adc(o_h, high, ShifterOperand(high));
4129 } else if (op->IsShr()) {
4130 __ Asrs(o_h, high, 1);
4131 __ Rrx(o_l, low);
4132 } else {
4133 __ Lsrs(o_h, high, 1);
4134 __ Rrx(o_l, low);
4135 }
4136 } else {
4137 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004138 if (op->IsShl()) {
4139 __ Lsl(o_h, high, shift_value);
4140 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
4141 __ Lsl(o_l, low, shift_value);
4142 } else if (op->IsShr()) {
4143 __ Lsr(o_l, low, shift_value);
4144 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4145 __ Asr(o_h, high, shift_value);
4146 } else {
4147 __ Lsr(o_l, low, shift_value);
4148 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4149 __ Lsr(o_h, high, shift_value);
4150 }
4151 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004152 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004153 break;
4154 }
4155 default:
4156 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004157 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004158 }
4159}
4160
4161void LocationsBuilderARM::VisitShl(HShl* shl) {
4162 HandleShift(shl);
4163}
4164
4165void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
4166 HandleShift(shl);
4167}
4168
4169void LocationsBuilderARM::VisitShr(HShr* shr) {
4170 HandleShift(shr);
4171}
4172
4173void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
4174 HandleShift(shr);
4175}
4176
4177void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
4178 HandleShift(ushr);
4179}
4180
4181void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
4182 HandleShift(ushr);
4183}
4184
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004185void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004186 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004187 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00004188 if (instruction->IsStringAlloc()) {
4189 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4190 } else {
4191 InvokeRuntimeCallingConvention calling_convention;
4192 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004193 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004194 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004195}
4196
4197void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004198 // Note: if heap poisoning is enabled, the entry point takes cares
4199 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004200 if (instruction->IsStringAlloc()) {
4201 // String is allocated through StringFactory. Call NewEmptyString entry point.
4202 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004203 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004204 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4205 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
4206 __ blx(LR);
4207 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4208 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004209 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004210 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004211 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004212}
4213
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004214void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
4215 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004216 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004217 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004218 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004219 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4220 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004221}
4222
4223void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004224 // Note: if heap poisoning is enabled, the entry point takes cares
4225 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004226 QuickEntrypointEnum entrypoint =
4227 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4228 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004229 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004230 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004231}
4232
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004233void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004234 LocationSummary* locations =
4235 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004236 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4237 if (location.IsStackSlot()) {
4238 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4239 } else if (location.IsDoubleStackSlot()) {
4240 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004241 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004242 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004243}
4244
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004245void InstructionCodeGeneratorARM::VisitParameterValue(
4246 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004247 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004248}
4249
4250void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4251 LocationSummary* locations =
4252 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4253 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4254}
4255
4256void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4257 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004258}
4259
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004260void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004261 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004262 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004263 locations->SetInAt(0, Location::RequiresRegister());
4264 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004265}
4266
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004267void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4268 LocationSummary* locations = not_->GetLocations();
4269 Location out = locations->Out();
4270 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004271 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004272 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004273 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004274 break;
4275
4276 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004277 __ mvn(out.AsRegisterPairLow<Register>(),
4278 ShifterOperand(in.AsRegisterPairLow<Register>()));
4279 __ mvn(out.AsRegisterPairHigh<Register>(),
4280 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004281 break;
4282
4283 default:
4284 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4285 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004286}
4287
David Brazdil66d126e2015-04-03 16:02:44 +01004288void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4289 LocationSummary* locations =
4290 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4291 locations->SetInAt(0, Location::RequiresRegister());
4292 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4293}
4294
4295void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004296 LocationSummary* locations = bool_not->GetLocations();
4297 Location out = locations->Out();
4298 Location in = locations->InAt(0);
4299 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4300}
4301
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004302void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004303 LocationSummary* locations =
4304 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004305 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004306 case Primitive::kPrimBoolean:
4307 case Primitive::kPrimByte:
4308 case Primitive::kPrimShort:
4309 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004310 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004311 case Primitive::kPrimLong: {
4312 locations->SetInAt(0, Location::RequiresRegister());
4313 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004314 // Output overlaps because it is written before doing the low comparison.
4315 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004316 break;
4317 }
4318 case Primitive::kPrimFloat:
4319 case Primitive::kPrimDouble: {
4320 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004321 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004322 locations->SetOut(Location::RequiresRegister());
4323 break;
4324 }
4325 default:
4326 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4327 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004328}
4329
4330void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004331 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004332 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004333 Location left = locations->InAt(0);
4334 Location right = locations->InAt(1);
4335
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004336 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004337 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004338 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004339 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004340 case Primitive::kPrimBoolean:
4341 case Primitive::kPrimByte:
4342 case Primitive::kPrimShort:
4343 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004344 case Primitive::kPrimInt: {
4345 __ LoadImmediate(out, 0);
4346 __ cmp(left.AsRegister<Register>(),
4347 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4348 less_cond = LT;
4349 break;
4350 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004351 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004352 __ cmp(left.AsRegisterPairHigh<Register>(),
4353 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004354 __ b(&less, LT);
4355 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004356 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004357 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004358 __ cmp(left.AsRegisterPairLow<Register>(),
4359 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004360 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004361 break;
4362 }
4363 case Primitive::kPrimFloat:
4364 case Primitive::kPrimDouble: {
4365 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004366 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004367 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004368 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004369 break;
4370 }
4371 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004372 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004373 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004374 }
Aart Bika19616e2016-02-01 18:57:58 -08004375
Calin Juravleddb7df22014-11-25 20:56:51 +00004376 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004377 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004378
4379 __ Bind(&greater);
4380 __ LoadImmediate(out, 1);
4381 __ b(&done);
4382
4383 __ Bind(&less);
4384 __ LoadImmediate(out, -1);
4385
4386 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004387}
4388
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004389void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004390 LocationSummary* locations =
4391 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004392 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004393 locations->SetInAt(i, Location::Any());
4394 }
4395 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004396}
4397
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004398void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004399 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004400}
4401
Roland Levillainc9285912015-12-18 10:38:42 +00004402void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4403 // TODO (ported from quick): revisit ARM barrier kinds.
4404 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004405 switch (kind) {
4406 case MemBarrierKind::kAnyStore:
4407 case MemBarrierKind::kLoadAny:
4408 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004409 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004410 break;
4411 }
4412 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004413 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004414 break;
4415 }
4416 default:
4417 LOG(FATAL) << "Unexpected memory barrier " << kind;
4418 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004419 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004420}
4421
4422void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4423 uint32_t offset,
4424 Register out_lo,
4425 Register out_hi) {
4426 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004427 // Ensure `out_lo` is different from `addr`, so that loading
4428 // `offset` into `out_lo` does not clutter `addr`.
4429 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004430 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004431 __ add(IP, addr, ShifterOperand(out_lo));
4432 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004433 }
4434 __ ldrexd(out_lo, out_hi, addr);
4435}
4436
4437void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4438 uint32_t offset,
4439 Register value_lo,
4440 Register value_hi,
4441 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004442 Register temp2,
4443 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004444 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004445 if (offset != 0) {
4446 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004447 __ add(IP, addr, ShifterOperand(temp1));
4448 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004449 }
4450 __ Bind(&fail);
4451 // We need a load followed by store. (The address used in a STREX instruction must
4452 // be the same as the address in the most recently executed LDREX instruction.)
4453 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004454 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004455 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004456 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004457}
4458
4459void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4460 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4461
Nicolas Geoffray39468442014-09-02 15:17:15 +01004462 LocationSummary* locations =
4463 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004464 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004465
Calin Juravle52c48962014-12-16 17:02:57 +00004466 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004467 if (Primitive::IsFloatingPointType(field_type)) {
4468 locations->SetInAt(1, Location::RequiresFpuRegister());
4469 } else {
4470 locations->SetInAt(1, Location::RequiresRegister());
4471 }
4472
Calin Juravle52c48962014-12-16 17:02:57 +00004473 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004474 bool generate_volatile = field_info.IsVolatile()
4475 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004476 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004477 bool needs_write_barrier =
4478 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004479 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004480 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004481 if (needs_write_barrier) {
4482 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004483 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004484 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004485 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004486 // - registers need to be consecutive
4487 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004488 // We don't test for ARM yet, and the assertion makes sure that we
4489 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004490 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4491
4492 locations->AddTemp(Location::RequiresRegister());
4493 locations->AddTemp(Location::RequiresRegister());
4494 if (field_type == Primitive::kPrimDouble) {
4495 // For doubles we need two more registers to copy the value.
4496 locations->AddTemp(Location::RegisterLocation(R2));
4497 locations->AddTemp(Location::RegisterLocation(R3));
4498 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004499 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004500}
4501
Calin Juravle52c48962014-12-16 17:02:57 +00004502void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004503 const FieldInfo& field_info,
4504 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004505 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4506
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004507 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004508 Register base = locations->InAt(0).AsRegister<Register>();
4509 Location value = locations->InAt(1);
4510
4511 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004512 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004513 Primitive::Type field_type = field_info.GetFieldType();
4514 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004515 bool needs_write_barrier =
4516 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004517
4518 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004519 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004520 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004521
4522 switch (field_type) {
4523 case Primitive::kPrimBoolean:
4524 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004525 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004526 break;
4527 }
4528
4529 case Primitive::kPrimShort:
4530 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004531 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004532 break;
4533 }
4534
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004535 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004536 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004537 if (kPoisonHeapReferences && needs_write_barrier) {
4538 // Note that in the case where `value` is a null reference,
4539 // we do not enter this block, as a null reference does not
4540 // need poisoning.
4541 DCHECK_EQ(field_type, Primitive::kPrimNot);
4542 Register temp = locations->GetTemp(0).AsRegister<Register>();
4543 __ Mov(temp, value.AsRegister<Register>());
4544 __ PoisonHeapReference(temp);
4545 __ StoreToOffset(kStoreWord, temp, base, offset);
4546 } else {
4547 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4548 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004549 break;
4550 }
4551
4552 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004553 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004554 GenerateWideAtomicStore(base, offset,
4555 value.AsRegisterPairLow<Register>(),
4556 value.AsRegisterPairHigh<Register>(),
4557 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004558 locations->GetTemp(1).AsRegister<Register>(),
4559 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004560 } else {
4561 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004562 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004563 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004564 break;
4565 }
4566
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004567 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004568 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004569 break;
4570 }
4571
4572 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004573 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004574 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004575 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4576 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4577
4578 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4579
4580 GenerateWideAtomicStore(base, offset,
4581 value_reg_lo,
4582 value_reg_hi,
4583 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004584 locations->GetTemp(3).AsRegister<Register>(),
4585 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004586 } else {
4587 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004588 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004589 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004590 break;
4591 }
4592
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004593 case Primitive::kPrimVoid:
4594 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004595 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004596 }
Calin Juravle52c48962014-12-16 17:02:57 +00004597
Calin Juravle77520bc2015-01-12 18:45:46 +00004598 // Longs and doubles are handled in the switch.
4599 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4600 codegen_->MaybeRecordImplicitNullCheck(instruction);
4601 }
4602
4603 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4604 Register temp = locations->GetTemp(0).AsRegister<Register>();
4605 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004606 codegen_->MarkGCCard(
4607 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004608 }
4609
Calin Juravle52c48962014-12-16 17:02:57 +00004610 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004611 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004612 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004613}
4614
Calin Juravle52c48962014-12-16 17:02:57 +00004615void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4616 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004617
4618 bool object_field_get_with_read_barrier =
4619 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004620 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004621 new (GetGraph()->GetArena()) LocationSummary(instruction,
4622 object_field_get_with_read_barrier ?
4623 LocationSummary::kCallOnSlowPath :
4624 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004625 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004626 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004627 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004628 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004629
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004630 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004631 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004632 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004633 // The output overlaps in case of volatile long: we don't want the
4634 // code generated by GenerateWideAtomicLoad to overwrite the
4635 // object's location. Likewise, in the case of an object field get
4636 // with read barriers enabled, we do not want the load to overwrite
4637 // the object's location, as we need it to emit the read barrier.
4638 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4639 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004640
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004641 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4642 locations->SetOut(Location::RequiresFpuRegister());
4643 } else {
4644 locations->SetOut(Location::RequiresRegister(),
4645 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4646 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004647 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004648 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004649 // - registers need to be consecutive
4650 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004651 // We don't test for ARM yet, and the assertion makes sure that we
4652 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004653 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4654 locations->AddTemp(Location::RequiresRegister());
4655 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004656 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4657 // We need a temporary register for the read barrier marking slow
4658 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4659 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004660 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004661}
4662
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004663Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4664 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4665 << input->GetType();
4666 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4667 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4668 return Location::ConstantLocation(input->AsConstant());
4669 } else {
4670 return Location::RequiresFpuRegister();
4671 }
4672}
4673
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004674Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4675 Opcode opcode) {
4676 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4677 if (constant->IsConstant() &&
4678 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4679 return Location::ConstantLocation(constant->AsConstant());
4680 }
4681 return Location::RequiresRegister();
4682}
4683
4684bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4685 Opcode opcode) {
4686 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4687 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004688 Opcode high_opcode = opcode;
4689 SetCc low_set_cc = kCcDontCare;
4690 switch (opcode) {
4691 case SUB:
4692 // Flip the operation to an ADD.
4693 value = -value;
4694 opcode = ADD;
4695 FALLTHROUGH_INTENDED;
4696 case ADD:
4697 if (Low32Bits(value) == 0u) {
4698 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4699 }
4700 high_opcode = ADC;
4701 low_set_cc = kCcSet;
4702 break;
4703 default:
4704 break;
4705 }
4706 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4707 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004708 } else {
4709 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4710 }
4711}
4712
Vladimir Marko59751a72016-08-05 14:37:27 +01004713bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4714 Opcode opcode,
4715 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004716 ShifterOperand so;
4717 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004718 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004719 return true;
4720 }
4721 Opcode neg_opcode = kNoOperand;
4722 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004723 case AND: neg_opcode = BIC; value = ~value; break;
4724 case ORR: neg_opcode = ORN; value = ~value; break;
4725 case ADD: neg_opcode = SUB; value = -value; break;
4726 case ADC: neg_opcode = SBC; value = ~value; break;
4727 case SUB: neg_opcode = ADD; value = -value; break;
4728 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004729 default:
4730 return false;
4731 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004732 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004733}
4734
Calin Juravle52c48962014-12-16 17:02:57 +00004735void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4736 const FieldInfo& field_info) {
4737 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004738
Calin Juravle52c48962014-12-16 17:02:57 +00004739 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004740 Location base_loc = locations->InAt(0);
4741 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004742 Location out = locations->Out();
4743 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004744 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004745 Primitive::Type field_type = field_info.GetFieldType();
4746 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4747
4748 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004749 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004750 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004751 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004752
Roland Levillainc9285912015-12-18 10:38:42 +00004753 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004754 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004755 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004756
Roland Levillainc9285912015-12-18 10:38:42 +00004757 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004758 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004759 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004760
Roland Levillainc9285912015-12-18 10:38:42 +00004761 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004762 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004763 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004764
4765 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004766 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004767 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004768
4769 case Primitive::kPrimNot: {
4770 // /* HeapReference<Object> */ out = *(base + offset)
4771 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4772 Location temp_loc = locations->GetTemp(0);
4773 // Note that a potential implicit null check is handled in this
4774 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4775 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4776 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4777 if (is_volatile) {
4778 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4779 }
4780 } else {
4781 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4782 codegen_->MaybeRecordImplicitNullCheck(instruction);
4783 if (is_volatile) {
4784 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4785 }
4786 // If read barriers are enabled, emit read barriers other than
4787 // Baker's using a slow path (and also unpoison the loaded
4788 // reference, if heap poisoning is enabled).
4789 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4790 }
4791 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004792 }
4793
Roland Levillainc9285912015-12-18 10:38:42 +00004794 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004795 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004796 GenerateWideAtomicLoad(base, offset,
4797 out.AsRegisterPairLow<Register>(),
4798 out.AsRegisterPairHigh<Register>());
4799 } else {
4800 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4801 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004802 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004803
Roland Levillainc9285912015-12-18 10:38:42 +00004804 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004805 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004806 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004807
4808 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004809 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004810 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004811 Register lo = locations->GetTemp(0).AsRegister<Register>();
4812 Register hi = locations->GetTemp(1).AsRegister<Register>();
4813 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004814 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004815 __ vmovdrr(out_reg, lo, hi);
4816 } else {
4817 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004818 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004819 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004820 break;
4821 }
4822
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004823 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004824 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004825 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004826 }
Calin Juravle52c48962014-12-16 17:02:57 +00004827
Roland Levillainc9285912015-12-18 10:38:42 +00004828 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4829 // Potential implicit null checks, in the case of reference or
4830 // double fields, are handled in the previous switch statement.
4831 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004832 codegen_->MaybeRecordImplicitNullCheck(instruction);
4833 }
4834
Calin Juravle52c48962014-12-16 17:02:57 +00004835 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004836 if (field_type == Primitive::kPrimNot) {
4837 // Memory barriers, in the case of references, are also handled
4838 // in the previous switch statement.
4839 } else {
4840 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4841 }
Roland Levillain4d027112015-07-01 15:41:14 +01004842 }
Calin Juravle52c48962014-12-16 17:02:57 +00004843}
4844
4845void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4846 HandleFieldSet(instruction, instruction->GetFieldInfo());
4847}
4848
4849void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004850 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004851}
4852
4853void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4854 HandleFieldGet(instruction, instruction->GetFieldInfo());
4855}
4856
4857void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4858 HandleFieldGet(instruction, instruction->GetFieldInfo());
4859}
4860
4861void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4862 HandleFieldGet(instruction, instruction->GetFieldInfo());
4863}
4864
4865void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4866 HandleFieldGet(instruction, instruction->GetFieldInfo());
4867}
4868
4869void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4870 HandleFieldSet(instruction, instruction->GetFieldInfo());
4871}
4872
4873void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004874 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004875}
4876
Calin Juravlee460d1d2015-09-29 04:52:17 +01004877void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4878 HUnresolvedInstanceFieldGet* instruction) {
4879 FieldAccessCallingConventionARM calling_convention;
4880 codegen_->CreateUnresolvedFieldLocationSummary(
4881 instruction, instruction->GetFieldType(), calling_convention);
4882}
4883
4884void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4885 HUnresolvedInstanceFieldGet* instruction) {
4886 FieldAccessCallingConventionARM calling_convention;
4887 codegen_->GenerateUnresolvedFieldAccess(instruction,
4888 instruction->GetFieldType(),
4889 instruction->GetFieldIndex(),
4890 instruction->GetDexPc(),
4891 calling_convention);
4892}
4893
4894void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4895 HUnresolvedInstanceFieldSet* instruction) {
4896 FieldAccessCallingConventionARM calling_convention;
4897 codegen_->CreateUnresolvedFieldLocationSummary(
4898 instruction, instruction->GetFieldType(), calling_convention);
4899}
4900
4901void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4902 HUnresolvedInstanceFieldSet* instruction) {
4903 FieldAccessCallingConventionARM calling_convention;
4904 codegen_->GenerateUnresolvedFieldAccess(instruction,
4905 instruction->GetFieldType(),
4906 instruction->GetFieldIndex(),
4907 instruction->GetDexPc(),
4908 calling_convention);
4909}
4910
4911void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4912 HUnresolvedStaticFieldGet* instruction) {
4913 FieldAccessCallingConventionARM calling_convention;
4914 codegen_->CreateUnresolvedFieldLocationSummary(
4915 instruction, instruction->GetFieldType(), calling_convention);
4916}
4917
4918void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4919 HUnresolvedStaticFieldGet* instruction) {
4920 FieldAccessCallingConventionARM calling_convention;
4921 codegen_->GenerateUnresolvedFieldAccess(instruction,
4922 instruction->GetFieldType(),
4923 instruction->GetFieldIndex(),
4924 instruction->GetDexPc(),
4925 calling_convention);
4926}
4927
4928void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4929 HUnresolvedStaticFieldSet* instruction) {
4930 FieldAccessCallingConventionARM calling_convention;
4931 codegen_->CreateUnresolvedFieldLocationSummary(
4932 instruction, instruction->GetFieldType(), calling_convention);
4933}
4934
4935void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4936 HUnresolvedStaticFieldSet* instruction) {
4937 FieldAccessCallingConventionARM calling_convention;
4938 codegen_->GenerateUnresolvedFieldAccess(instruction,
4939 instruction->GetFieldType(),
4940 instruction->GetFieldIndex(),
4941 instruction->GetDexPc(),
4942 calling_convention);
4943}
4944
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004945void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004946 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4947 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004948}
4949
Calin Juravle2ae48182016-03-16 14:05:09 +00004950void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4951 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004952 return;
4953 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004954 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004955
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004956 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004957 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004958}
4959
Calin Juravle2ae48182016-03-16 14:05:09 +00004960void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004961 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004962 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004963
4964 LocationSummary* locations = instruction->GetLocations();
4965 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004966
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004967 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004968}
4969
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004970void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004971 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004972}
4973
Artem Serov6c916792016-07-11 14:02:34 +01004974static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4975 switch (type) {
4976 case Primitive::kPrimNot:
4977 return kLoadWord;
4978 case Primitive::kPrimBoolean:
4979 return kLoadUnsignedByte;
4980 case Primitive::kPrimByte:
4981 return kLoadSignedByte;
4982 case Primitive::kPrimChar:
4983 return kLoadUnsignedHalfword;
4984 case Primitive::kPrimShort:
4985 return kLoadSignedHalfword;
4986 case Primitive::kPrimInt:
4987 return kLoadWord;
4988 case Primitive::kPrimLong:
4989 return kLoadWordPair;
4990 case Primitive::kPrimFloat:
4991 return kLoadSWord;
4992 case Primitive::kPrimDouble:
4993 return kLoadDWord;
4994 default:
4995 LOG(FATAL) << "Unreachable type " << type;
4996 UNREACHABLE();
4997 }
4998}
4999
5000static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5001 switch (type) {
5002 case Primitive::kPrimNot:
5003 return kStoreWord;
5004 case Primitive::kPrimBoolean:
5005 case Primitive::kPrimByte:
5006 return kStoreByte;
5007 case Primitive::kPrimChar:
5008 case Primitive::kPrimShort:
5009 return kStoreHalfword;
5010 case Primitive::kPrimInt:
5011 return kStoreWord;
5012 case Primitive::kPrimLong:
5013 return kStoreWordPair;
5014 case Primitive::kPrimFloat:
5015 return kStoreSWord;
5016 case Primitive::kPrimDouble:
5017 return kStoreDWord;
5018 default:
5019 LOG(FATAL) << "Unreachable type " << type;
5020 UNREACHABLE();
5021 }
5022}
5023
5024void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
5025 Location out_loc,
5026 Register base,
5027 Register reg_offset,
5028 Condition cond) {
5029 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5030 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5031
5032 switch (type) {
5033 case Primitive::kPrimByte:
5034 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
5035 break;
5036 case Primitive::kPrimBoolean:
5037 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
5038 break;
5039 case Primitive::kPrimShort:
5040 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
5041 break;
5042 case Primitive::kPrimChar:
5043 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
5044 break;
5045 case Primitive::kPrimNot:
5046 case Primitive::kPrimInt:
5047 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
5048 break;
5049 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5050 case Primitive::kPrimLong:
5051 case Primitive::kPrimFloat:
5052 case Primitive::kPrimDouble:
5053 default:
5054 LOG(FATAL) << "Unreachable type " << type;
5055 UNREACHABLE();
5056 }
5057}
5058
5059void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
5060 Location loc,
5061 Register base,
5062 Register reg_offset,
5063 Condition cond) {
5064 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5065 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5066
5067 switch (type) {
5068 case Primitive::kPrimByte:
5069 case Primitive::kPrimBoolean:
5070 __ strb(loc.AsRegister<Register>(), mem_address, cond);
5071 break;
5072 case Primitive::kPrimShort:
5073 case Primitive::kPrimChar:
5074 __ strh(loc.AsRegister<Register>(), mem_address, cond);
5075 break;
5076 case Primitive::kPrimNot:
5077 case Primitive::kPrimInt:
5078 __ str(loc.AsRegister<Register>(), mem_address, cond);
5079 break;
5080 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5081 case Primitive::kPrimLong:
5082 case Primitive::kPrimFloat:
5083 case Primitive::kPrimDouble:
5084 default:
5085 LOG(FATAL) << "Unreachable type " << type;
5086 UNREACHABLE();
5087 }
5088}
5089
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005090void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005091 bool object_array_get_with_read_barrier =
5092 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005093 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005094 new (GetGraph()->GetArena()) LocationSummary(instruction,
5095 object_array_get_with_read_barrier ?
5096 LocationSummary::kCallOnSlowPath :
5097 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005098 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005099 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005100 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005101 locations->SetInAt(0, Location::RequiresRegister());
5102 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005103 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5104 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5105 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005106 // The output overlaps in the case of an object array get with
5107 // read barriers enabled: we do not want the move to overwrite the
5108 // array's location, as we need it to emit the read barrier.
5109 locations->SetOut(
5110 Location::RequiresRegister(),
5111 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005112 }
Roland Levillainc9285912015-12-18 10:38:42 +00005113 // We need a temporary register for the read barrier marking slow
5114 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07005115 // Also need for String compression feature.
5116 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
5117 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00005118 locations->AddTemp(Location::RequiresRegister());
5119 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005120}
5121
5122void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
5123 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005124 Location obj_loc = locations->InAt(0);
5125 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00005127 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005128 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00005129 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07005130 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5131 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01005132 HInstruction* array_instr = instruction->GetArray();
5133 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01005134
Roland Levillain4d027112015-07-01 15:41:14 +01005135 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01005136 case Primitive::kPrimBoolean:
5137 case Primitive::kPrimByte:
5138 case Primitive::kPrimShort:
5139 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00005140 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005141 Register length;
5142 if (maybe_compressed_char_at) {
5143 length = locations->GetTemp(0).AsRegister<Register>();
5144 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5145 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
5146 codegen_->MaybeRecordImplicitNullCheck(instruction);
5147 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005148 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005149 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07005150 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07005151 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005152 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5153 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5154 "Expecting 0=compressed, 1=uncompressed");
5155 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005156 __ LoadFromOffset(kLoadUnsignedByte,
5157 out_loc.AsRegister<Register>(),
5158 obj,
5159 data_offset + const_index);
5160 __ b(&done);
5161 __ Bind(&uncompressed_load);
5162 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5163 out_loc.AsRegister<Register>(),
5164 obj,
5165 data_offset + (const_index << 1));
5166 __ Bind(&done);
5167 } else {
5168 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01005169
jessicahandojo05765752016-09-09 19:01:32 -07005170 LoadOperandType load_type = GetLoadOperandType(type);
5171 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
5172 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005173 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005174 Register temp = IP;
5175
5176 if (has_intermediate_address) {
5177 // We do not need to compute the intermediate address from the array: the
5178 // input instruction has done it already. See the comment in
5179 // `TryExtractArrayAccessAddress()`.
5180 if (kIsDebugBuild) {
5181 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5182 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5183 }
5184 temp = obj;
5185 } else {
5186 __ add(temp, obj, ShifterOperand(data_offset));
5187 }
jessicahandojo05765752016-09-09 19:01:32 -07005188 if (maybe_compressed_char_at) {
5189 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005190 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5191 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5192 "Expecting 0=compressed, 1=uncompressed");
5193 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005194 __ ldrb(out_loc.AsRegister<Register>(),
5195 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
5196 __ b(&done);
5197 __ Bind(&uncompressed_load);
5198 __ ldrh(out_loc.AsRegister<Register>(),
5199 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
5200 __ Bind(&done);
5201 } else {
5202 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
5203 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005204 }
5205 break;
5206 }
5207
Roland Levillainc9285912015-12-18 10:38:42 +00005208 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00005209 // The read barrier instrumentation of object ArrayGet
5210 // instructions does not support the HIntermediateAddress
5211 // instruction.
5212 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5213
Roland Levillainc9285912015-12-18 10:38:42 +00005214 static_assert(
5215 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5216 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00005217 // /* HeapReference<Object> */ out =
5218 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5219 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5220 Location temp = locations->GetTemp(0);
5221 // Note that a potential implicit null check is handled in this
5222 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
5223 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5224 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5225 } else {
5226 Register out = out_loc.AsRegister<Register>();
5227 if (index.IsConstant()) {
5228 size_t offset =
5229 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5230 __ LoadFromOffset(kLoadWord, out, obj, offset);
5231 codegen_->MaybeRecordImplicitNullCheck(instruction);
5232 // If read barriers are enabled, emit read barriers other than
5233 // Baker's using a slow path (and also unpoison the loaded
5234 // reference, if heap poisoning is enabled).
5235 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5236 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005237 Register temp = IP;
5238
5239 if (has_intermediate_address) {
5240 // We do not need to compute the intermediate address from the array: the
5241 // input instruction has done it already. See the comment in
5242 // `TryExtractArrayAccessAddress()`.
5243 if (kIsDebugBuild) {
5244 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5245 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5246 }
5247 temp = obj;
5248 } else {
5249 __ add(temp, obj, ShifterOperand(data_offset));
5250 }
5251 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005252
Roland Levillainc9285912015-12-18 10:38:42 +00005253 codegen_->MaybeRecordImplicitNullCheck(instruction);
5254 // If read barriers are enabled, emit read barriers other than
5255 // Baker's using a slow path (and also unpoison the loaded
5256 // reference, if heap poisoning is enabled).
5257 codegen_->MaybeGenerateReadBarrierSlow(
5258 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5259 }
5260 }
5261 break;
5262 }
5263
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005264 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005265 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005266 size_t offset =
5267 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005268 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005270 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005271 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005272 }
5273 break;
5274 }
5275
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005276 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005277 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005278 if (index.IsConstant()) {
5279 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005280 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005281 } else {
5282 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005283 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005284 }
5285 break;
5286 }
5287
5288 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005289 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005290 if (index.IsConstant()) {
5291 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005292 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005293 } else {
5294 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005295 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005296 }
5297 break;
5298 }
5299
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005300 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005301 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005302 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005303 }
Roland Levillain4d027112015-07-01 15:41:14 +01005304
5305 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005306 // Potential implicit null checks, in the case of reference
5307 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005308 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005309 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005310 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005311}
5312
5313void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005314 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005315
5316 bool needs_write_barrier =
5317 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005318 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005319
Nicolas Geoffray39468442014-09-02 15:17:15 +01005320 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005321 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005322 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005323 LocationSummary::kCallOnSlowPath :
5324 LocationSummary::kNoCall);
5325
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005326 locations->SetInAt(0, Location::RequiresRegister());
5327 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5328 if (Primitive::IsFloatingPointType(value_type)) {
5329 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005330 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005331 locations->SetInAt(2, Location::RequiresRegister());
5332 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005333 if (needs_write_barrier) {
5334 // Temporary registers for the write barrier.
5335 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005336 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005337 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005338}
5339
5340void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5341 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005342 Location array_loc = locations->InAt(0);
5343 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005344 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005345 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005346 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005347 bool needs_write_barrier =
5348 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005349 uint32_t data_offset =
5350 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5351 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005352 HInstruction* array_instr = instruction->GetArray();
5353 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005354
5355 switch (value_type) {
5356 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005357 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005358 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005359 case Primitive::kPrimChar:
5360 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005362 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5363 uint32_t full_offset =
5364 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5365 StoreOperandType store_type = GetStoreOperandType(value_type);
5366 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005367 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005368 Register temp = IP;
5369
5370 if (has_intermediate_address) {
5371 // We do not need to compute the intermediate address from the array: the
5372 // input instruction has done it already. See the comment in
5373 // `TryExtractArrayAccessAddress()`.
5374 if (kIsDebugBuild) {
5375 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5376 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5377 }
5378 temp = array;
5379 } else {
5380 __ add(temp, array, ShifterOperand(data_offset));
5381 }
Artem Serov6c916792016-07-11 14:02:34 +01005382 codegen_->StoreToShiftedRegOffset(value_type,
5383 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005384 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005385 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005386 }
5387 break;
5388 }
5389
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005390 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005391 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005392 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5393 // See the comment in instruction_simplifier_shared.cc.
5394 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005395
5396 if (instruction->InputAt(2)->IsNullConstant()) {
5397 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005398 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005399 size_t offset =
5400 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005401 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005402 } else {
5403 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005404 __ add(IP, array, ShifterOperand(data_offset));
5405 codegen_->StoreToShiftedRegOffset(value_type,
5406 value_loc,
5407 IP,
5408 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005409 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005410 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005411 DCHECK(!needs_write_barrier);
5412 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005413 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005414 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005415
5416 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005417 Location temp1_loc = locations->GetTemp(0);
5418 Register temp1 = temp1_loc.AsRegister<Register>();
5419 Location temp2_loc = locations->GetTemp(1);
5420 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005421 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5422 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5423 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5424 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005425 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005426
Roland Levillain3b359c72015-11-17 19:35:12 +00005427 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005428 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5429 codegen_->AddSlowPath(slow_path);
5430 if (instruction->GetValueCanBeNull()) {
5431 Label non_zero;
5432 __ CompareAndBranchIfNonZero(value, &non_zero);
5433 if (index.IsConstant()) {
5434 size_t offset =
5435 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5436 __ StoreToOffset(kStoreWord, value, array, offset);
5437 } else {
5438 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005439 __ add(IP, array, ShifterOperand(data_offset));
5440 codegen_->StoreToShiftedRegOffset(value_type,
5441 value_loc,
5442 IP,
5443 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005444 }
5445 codegen_->MaybeRecordImplicitNullCheck(instruction);
5446 __ b(&done);
5447 __ Bind(&non_zero);
5448 }
5449
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005450 // Note that when read barriers are enabled, the type checks
5451 // are performed without read barriers. This is fine, even in
5452 // the case where a class object is in the from-space after
5453 // the flip, as a comparison involving such a type would not
5454 // produce a false positive; it may of course produce a false
5455 // negative, in which case we would take the ArraySet slow
5456 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005457
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005458 // /* HeapReference<Class> */ temp1 = array->klass_
5459 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5460 codegen_->MaybeRecordImplicitNullCheck(instruction);
5461 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005462
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005463 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5464 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5465 // /* HeapReference<Class> */ temp2 = value->klass_
5466 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5467 // If heap poisoning is enabled, no need to unpoison `temp1`
5468 // nor `temp2`, as we are comparing two poisoned references.
5469 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005470
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005471 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5472 Label do_put;
5473 __ b(&do_put, EQ);
5474 // If heap poisoning is enabled, the `temp1` reference has
5475 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005476 __ MaybeUnpoisonHeapReference(temp1);
5477
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005478 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5479 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5480 // If heap poisoning is enabled, no need to unpoison
5481 // `temp1`, as we are comparing against null below.
5482 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5483 __ Bind(&do_put);
5484 } else {
5485 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005486 }
5487 }
5488
Artem Serov6c916792016-07-11 14:02:34 +01005489 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005490 if (kPoisonHeapReferences) {
5491 // Note that in the case where `value` is a null reference,
5492 // we do not enter this block, as a null reference does not
5493 // need poisoning.
5494 DCHECK_EQ(value_type, Primitive::kPrimNot);
5495 __ Mov(temp1, value);
5496 __ PoisonHeapReference(temp1);
5497 source = temp1;
5498 }
5499
5500 if (index.IsConstant()) {
5501 size_t offset =
5502 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5503 __ StoreToOffset(kStoreWord, source, array, offset);
5504 } else {
5505 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005506
5507 __ add(IP, array, ShifterOperand(data_offset));
5508 codegen_->StoreToShiftedRegOffset(value_type,
5509 Location::RegisterLocation(source),
5510 IP,
5511 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005512 }
5513
Roland Levillain3b359c72015-11-17 19:35:12 +00005514 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005515 codegen_->MaybeRecordImplicitNullCheck(instruction);
5516 }
5517
5518 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5519
5520 if (done.IsLinked()) {
5521 __ Bind(&done);
5522 }
5523
5524 if (slow_path != nullptr) {
5525 __ Bind(slow_path->GetExitLabel());
5526 }
5527
5528 break;
5529 }
5530
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005531 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005532 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005533 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005534 size_t offset =
5535 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005536 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005537 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005538 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005539 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005540 }
5541 break;
5542 }
5543
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005544 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005545 Location value = locations->InAt(2);
5546 DCHECK(value.IsFpuRegister());
5547 if (index.IsConstant()) {
5548 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005549 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005550 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005551 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005552 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5553 }
5554 break;
5555 }
5556
5557 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005558 Location value = locations->InAt(2);
5559 DCHECK(value.IsFpuRegisterPair());
5560 if (index.IsConstant()) {
5561 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005562 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005563 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005564 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005565 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5566 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005567
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005568 break;
5569 }
5570
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005571 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005572 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005573 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005574 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005575
Roland Levillain80e67092016-01-08 16:04:55 +00005576 // Objects are handled in the switch.
5577 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005578 codegen_->MaybeRecordImplicitNullCheck(instruction);
5579 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005580}
5581
5582void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005583 LocationSummary* locations =
5584 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005585 locations->SetInAt(0, Location::RequiresRegister());
5586 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005587}
5588
5589void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5590 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005591 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005592 Register obj = locations->InAt(0).AsRegister<Register>();
5593 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005594 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005595 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005596 // Mask out compression flag from String's array length.
5597 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005598 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07005599 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005600}
5601
Artem Serov328429f2016-07-06 16:23:04 +01005602void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005603 LocationSummary* locations =
5604 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5605
5606 locations->SetInAt(0, Location::RequiresRegister());
5607 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5608 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5609}
5610
5611void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5612 LocationSummary* locations = instruction->GetLocations();
5613 Location out = locations->Out();
5614 Location first = locations->InAt(0);
5615 Location second = locations->InAt(1);
5616
Artem Serov328429f2016-07-06 16:23:04 +01005617 if (second.IsRegister()) {
5618 __ add(out.AsRegister<Register>(),
5619 first.AsRegister<Register>(),
5620 ShifterOperand(second.AsRegister<Register>()));
5621 } else {
5622 __ AddConstant(out.AsRegister<Register>(),
5623 first.AsRegister<Register>(),
5624 second.GetConstant()->AsIntConstant()->GetValue());
5625 }
5626}
5627
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005628void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005629 RegisterSet caller_saves = RegisterSet::Empty();
5630 InvokeRuntimeCallingConvention calling_convention;
5631 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5632 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5633 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005634 locations->SetInAt(0, Location::RequiresRegister());
5635 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005636}
5637
5638void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5639 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005640 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005641 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005642 codegen_->AddSlowPath(slow_path);
5643
Roland Levillain271ab9c2014-11-27 15:23:57 +00005644 Register index = locations->InAt(0).AsRegister<Register>();
5645 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005646
5647 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005648 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005649}
5650
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005651void CodeGeneratorARM::MarkGCCard(Register temp,
5652 Register card,
5653 Register object,
5654 Register value,
5655 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005656 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005657 if (can_be_null) {
5658 __ CompareAndBranchIfZero(value, &is_null);
5659 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005660 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005661 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5662 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005663 if (can_be_null) {
5664 __ Bind(&is_null);
5665 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005666}
5667
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005668void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005669 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005670}
5671
5672void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005673 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5674}
5675
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005676void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005677 LocationSummary* locations =
5678 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005679 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005680}
5681
5682void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005683 HBasicBlock* block = instruction->GetBlock();
5684 if (block->GetLoopInformation() != nullptr) {
5685 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5686 // The back edge will generate the suspend check.
5687 return;
5688 }
5689 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5690 // The goto will generate the suspend check.
5691 return;
5692 }
5693 GenerateSuspendCheck(instruction, nullptr);
5694}
5695
5696void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5697 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005698 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005699 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5700 if (slow_path == nullptr) {
5701 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5702 instruction->SetSlowPath(slow_path);
5703 codegen_->AddSlowPath(slow_path);
5704 if (successor != nullptr) {
5705 DCHECK(successor->IsLoopHeader());
5706 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5707 }
5708 } else {
5709 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5710 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005711
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005712 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005713 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005714 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005715 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005716 __ Bind(slow_path->GetReturnLabel());
5717 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005718 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005719 __ b(slow_path->GetEntryLabel());
5720 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005721}
5722
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005723ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5724 return codegen_->GetAssembler();
5725}
5726
5727void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005728 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005729 Location source = move->GetSource();
5730 Location destination = move->GetDestination();
5731
5732 if (source.IsRegister()) {
5733 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005734 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005735 } else if (destination.IsFpuRegister()) {
5736 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005737 } else {
5738 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005739 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005740 SP, destination.GetStackIndex());
5741 }
5742 } else if (source.IsStackSlot()) {
5743 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005744 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005745 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005746 } else if (destination.IsFpuRegister()) {
5747 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005748 } else {
5749 DCHECK(destination.IsStackSlot());
5750 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5751 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5752 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005753 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005754 if (destination.IsRegister()) {
5755 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5756 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005757 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005758 } else {
5759 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005760 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5761 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005762 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005763 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005764 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5765 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005766 } else if (destination.IsRegisterPair()) {
5767 DCHECK(ExpectedPairLayout(destination));
5768 __ LoadFromOffset(
5769 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5770 } else {
5771 DCHECK(destination.IsFpuRegisterPair()) << destination;
5772 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5773 SP,
5774 source.GetStackIndex());
5775 }
5776 } else if (source.IsRegisterPair()) {
5777 if (destination.IsRegisterPair()) {
5778 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5779 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005780 } else if (destination.IsFpuRegisterPair()) {
5781 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5782 source.AsRegisterPairLow<Register>(),
5783 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005784 } else {
5785 DCHECK(destination.IsDoubleStackSlot()) << destination;
5786 DCHECK(ExpectedPairLayout(source));
5787 __ StoreToOffset(
5788 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5789 }
5790 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005791 if (destination.IsRegisterPair()) {
5792 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5793 destination.AsRegisterPairHigh<Register>(),
5794 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5795 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005796 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5797 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5798 } else {
5799 DCHECK(destination.IsDoubleStackSlot()) << destination;
5800 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5801 SP,
5802 destination.GetStackIndex());
5803 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005804 } else {
5805 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005806 HConstant* constant = source.GetConstant();
5807 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5808 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005809 if (destination.IsRegister()) {
5810 __ LoadImmediate(destination.AsRegister<Register>(), value);
5811 } else {
5812 DCHECK(destination.IsStackSlot());
5813 __ LoadImmediate(IP, value);
5814 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5815 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005816 } else if (constant->IsLongConstant()) {
5817 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005818 if (destination.IsRegisterPair()) {
5819 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5820 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005821 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005822 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005823 __ LoadImmediate(IP, Low32Bits(value));
5824 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5825 __ LoadImmediate(IP, High32Bits(value));
5826 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5827 }
5828 } else if (constant->IsDoubleConstant()) {
5829 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005830 if (destination.IsFpuRegisterPair()) {
5831 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005832 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005833 DCHECK(destination.IsDoubleStackSlot()) << destination;
5834 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005835 __ LoadImmediate(IP, Low32Bits(int_value));
5836 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5837 __ LoadImmediate(IP, High32Bits(int_value));
5838 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5839 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005840 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005841 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005842 float value = constant->AsFloatConstant()->GetValue();
5843 if (destination.IsFpuRegister()) {
5844 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5845 } else {
5846 DCHECK(destination.IsStackSlot());
5847 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5848 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5849 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005850 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005851 }
5852}
5853
5854void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5855 __ Mov(IP, reg);
5856 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5857 __ StoreToOffset(kStoreWord, IP, SP, mem);
5858}
5859
5860void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5861 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5862 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5863 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5864 SP, mem1 + stack_offset);
5865 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5866 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5867 SP, mem2 + stack_offset);
5868 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5869}
5870
5871void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005872 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005873 Location source = move->GetSource();
5874 Location destination = move->GetDestination();
5875
5876 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005877 DCHECK_NE(source.AsRegister<Register>(), IP);
5878 DCHECK_NE(destination.AsRegister<Register>(), IP);
5879 __ Mov(IP, source.AsRegister<Register>());
5880 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5881 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005882 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005883 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005884 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005885 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005886 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5887 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005888 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005889 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005890 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005891 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005892 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005893 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005894 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005895 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005896 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5897 destination.AsRegisterPairHigh<Register>(),
5898 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005899 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005900 Register low_reg = source.IsRegisterPair()
5901 ? source.AsRegisterPairLow<Register>()
5902 : destination.AsRegisterPairLow<Register>();
5903 int mem = source.IsRegisterPair()
5904 ? destination.GetStackIndex()
5905 : source.GetStackIndex();
5906 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005907 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005908 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005909 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005910 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005911 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5912 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005913 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005914 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005915 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005916 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5917 DRegister reg = source.IsFpuRegisterPair()
5918 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5919 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5920 int mem = source.IsFpuRegisterPair()
5921 ? destination.GetStackIndex()
5922 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005923 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005924 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005925 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005926 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5927 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5928 : destination.AsFpuRegister<SRegister>();
5929 int mem = source.IsFpuRegister()
5930 ? destination.GetStackIndex()
5931 : source.GetStackIndex();
5932
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005933 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005934 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005935 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005936 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005937 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5938 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005939 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005940 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005941 }
5942}
5943
5944void ParallelMoveResolverARM::SpillScratch(int reg) {
5945 __ Push(static_cast<Register>(reg));
5946}
5947
5948void ParallelMoveResolverARM::RestoreScratch(int reg) {
5949 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005950}
5951
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005952HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5953 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005954 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005955 case HLoadClass::LoadKind::kInvalid:
5956 LOG(FATAL) << "UNREACHABLE";
5957 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005958 case HLoadClass::LoadKind::kReferrersClass:
5959 break;
5960 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5961 DCHECK(!GetCompilerOptions().GetCompilePic());
5962 break;
5963 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5964 DCHECK(GetCompilerOptions().GetCompilePic());
5965 break;
5966 case HLoadClass::LoadKind::kBootImageAddress:
5967 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005968 case HLoadClass::LoadKind::kBssEntry:
5969 DCHECK(!Runtime::Current()->UseJitCompilation());
5970 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005971 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005972 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005973 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005974 case HLoadClass::LoadKind::kDexCacheViaMethod:
5975 break;
5976 }
5977 return desired_class_load_kind;
5978}
5979
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005980void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005981 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5982 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005983 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005984 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005985 cls,
5986 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005987 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005988 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005989 return;
5990 }
Vladimir Marko41559982017-01-06 14:04:23 +00005991 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005992
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005993 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5994 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005995 ? LocationSummary::kCallOnSlowPath
5996 : LocationSummary::kNoCall;
5997 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005998 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005999 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006000 }
6001
Vladimir Marko41559982017-01-06 14:04:23 +00006002 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006003 locations->SetInAt(0, Location::RequiresRegister());
6004 }
6005 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006006 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6007 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6008 // Rely on the type resolution or initialization and marking to save everything we need.
6009 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6010 // to the custom calling convention) or by marking, so we request a different temp.
6011 locations->AddTemp(Location::RequiresRegister());
6012 RegisterSet caller_saves = RegisterSet::Empty();
6013 InvokeRuntimeCallingConvention calling_convention;
6014 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6015 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6016 // that the the kPrimNot result register is the same as the first argument register.
6017 locations->SetCustomSlowPathCallerSaves(caller_saves);
6018 } else {
6019 // For non-Baker read barrier we have a temp-clobbering call.
6020 }
6021 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006022}
6023
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006024// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6025// move.
6026void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006027 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6028 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6029 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006030 return;
6031 }
Vladimir Marko41559982017-01-06 14:04:23 +00006032 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006033
Vladimir Marko41559982017-01-06 14:04:23 +00006034 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006035 Location out_loc = locations->Out();
6036 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006037
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006038 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6039 ? kWithoutReadBarrier
6040 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006041 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006042 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006043 case HLoadClass::LoadKind::kReferrersClass: {
6044 DCHECK(!cls->CanCallRuntime());
6045 DCHECK(!cls->MustGenerateClinitCheck());
6046 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6047 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006048 GenerateGcRootFieldLoad(cls,
6049 out_loc,
6050 current_method,
6051 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006052 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006053 break;
6054 }
6055 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006056 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006057 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006058 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6059 cls->GetTypeIndex()));
6060 break;
6061 }
6062 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006063 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006064 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006065 CodeGeneratorARM::PcRelativePatchInfo* labels =
6066 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6067 __ BindTrackedLabel(&labels->movw_label);
6068 __ movw(out, /* placeholder */ 0u);
6069 __ BindTrackedLabel(&labels->movt_label);
6070 __ movt(out, /* placeholder */ 0u);
6071 __ BindTrackedLabel(&labels->add_pc_label);
6072 __ add(out, out, ShifterOperand(PC));
6073 break;
6074 }
6075 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006076 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006077 uint32_t address = dchecked_integral_cast<uint32_t>(
6078 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6079 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006080 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6081 break;
6082 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006083 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006084 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6085 ? locations->GetTemp(0).AsRegister<Register>()
6086 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006087 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006088 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006089 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006090 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006091 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006092 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006093 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006094 __ add(temp, temp, ShifterOperand(PC));
6095 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006096 generate_null_check = true;
6097 break;
6098 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006099 case HLoadClass::LoadKind::kJitTableAddress: {
6100 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6101 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006102 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006103 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006104 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006105 break;
6106 }
Vladimir Marko41559982017-01-06 14:04:23 +00006107 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006108 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006109 LOG(FATAL) << "UNREACHABLE";
6110 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006111 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006112
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006113 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6114 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01006115 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006116 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6117 codegen_->AddSlowPath(slow_path);
6118 if (generate_null_check) {
6119 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6120 }
6121 if (cls->MustGenerateClinitCheck()) {
6122 GenerateClassInitializationCheck(slow_path, out);
6123 } else {
6124 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006125 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006126 }
6127}
6128
6129void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
6130 LocationSummary* locations =
6131 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6132 locations->SetInAt(0, Location::RequiresRegister());
6133 if (check->HasUses()) {
6134 locations->SetOut(Location::SameAsFirstInput());
6135 }
6136}
6137
6138void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006139 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006140 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006141 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006142 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006143 GenerateClassInitializationCheck(slow_path,
6144 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006145}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006146
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006147void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01006148 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006149 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
6150 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
6151 __ b(slow_path->GetEntryLabel(), LT);
6152 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6153 // properly. Therefore, we do a memory fence.
6154 __ dmb(ISH);
6155 __ Bind(slow_path->GetExitLabel());
6156}
6157
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006158HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
6159 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006160 switch (desired_string_load_kind) {
6161 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6162 DCHECK(!GetCompilerOptions().GetCompilePic());
6163 break;
6164 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6165 DCHECK(GetCompilerOptions().GetCompilePic());
6166 break;
6167 case HLoadString::LoadKind::kBootImageAddress:
6168 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006169 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006170 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006171 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006172 case HLoadString::LoadKind::kJitTableAddress:
6173 DCHECK(Runtime::Current()->UseJitCompilation());
6174 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006175 case HLoadString::LoadKind::kDexCacheViaMethod:
6176 break;
6177 }
6178 return desired_string_load_kind;
6179}
6180
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006181void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006182 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006183 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006184 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006185 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006186 locations->SetOut(Location::RegisterLocation(R0));
6187 } else {
6188 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006189 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6190 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006191 // Rely on the pResolveString and marking to save everything we need, including temps.
6192 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6193 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006194 locations->AddTemp(Location::RequiresRegister());
6195 RegisterSet caller_saves = RegisterSet::Empty();
6196 InvokeRuntimeCallingConvention calling_convention;
6197 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6198 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6199 // that the the kPrimNot result register is the same as the first argument register.
6200 locations->SetCustomSlowPathCallerSaves(caller_saves);
6201 } else {
6202 // For non-Baker read barrier we have a temp-clobbering call.
6203 }
6204 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006205 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006206}
6207
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006208// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6209// move.
6210void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006211 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006212 Location out_loc = locations->Out();
6213 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006214 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00006215
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006216 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006217 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006218 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006219 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6220 load->GetStringIndex()));
6221 return; // No dex cache slow path.
6222 }
6223 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006224 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006225 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006226 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006227 __ BindTrackedLabel(&labels->movw_label);
6228 __ movw(out, /* placeholder */ 0u);
6229 __ BindTrackedLabel(&labels->movt_label);
6230 __ movt(out, /* placeholder */ 0u);
6231 __ BindTrackedLabel(&labels->add_pc_label);
6232 __ add(out, out, ShifterOperand(PC));
6233 return; // No dex cache slow path.
6234 }
6235 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006236 uint32_t address = dchecked_integral_cast<uint32_t>(
6237 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6238 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006239 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6240 return; // No dex cache slow path.
6241 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006242 case HLoadString::LoadKind::kBssEntry: {
6243 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006244 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6245 ? locations->GetTemp(0).AsRegister<Register>()
6246 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006247 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006248 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006249 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006250 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006251 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006252 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006253 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006254 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006255 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006256 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
6257 codegen_->AddSlowPath(slow_path);
6258 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6259 __ Bind(slow_path->GetExitLabel());
6260 return;
6261 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006262 case HLoadString::LoadKind::kJitTableAddress: {
6263 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006264 load->GetStringIndex(),
6265 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006266 // /* GcRoot<mirror::String> */ out = *out
6267 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6268 return;
6269 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006270 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006271 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006272 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006273
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006274 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
6275 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6276 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006277 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006278 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006279 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6280 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006281}
6282
David Brazdilcb1c0552015-08-04 16:22:25 +01006283static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006284 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006285}
6286
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006287void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6288 LocationSummary* locations =
6289 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6290 locations->SetOut(Location::RequiresRegister());
6291}
6292
6293void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006294 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006295 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6296}
6297
6298void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6299 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6300}
6301
6302void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006303 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006304 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006305}
6306
6307void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6308 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006309 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006310 InvokeRuntimeCallingConvention calling_convention;
6311 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6312}
6313
6314void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006315 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006316 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006317}
6318
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006319// Temp is used for read barrier.
6320static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6321 if (kEmitCompilerReadBarrier &&
6322 (kUseBakerReadBarrier ||
6323 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6324 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6325 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6326 return 1;
6327 }
6328 return 0;
6329}
6330
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006331// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006332// interface pointer, one for loading the current interface.
6333// The other checks have one temp for loading the object's class.
6334static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6335 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6336 return 3;
6337 }
6338 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006339}
6340
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006341void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006342 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006343 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006344 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006345 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006346 case TypeCheckKind::kExactCheck:
6347 case TypeCheckKind::kAbstractClassCheck:
6348 case TypeCheckKind::kClassHierarchyCheck:
6349 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006350 call_kind =
6351 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006352 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006353 break;
6354 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006355 case TypeCheckKind::kUnresolvedCheck:
6356 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006357 call_kind = LocationSummary::kCallOnSlowPath;
6358 break;
6359 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006360
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006361 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006362 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006363 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006364 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006365 locations->SetInAt(0, Location::RequiresRegister());
6366 locations->SetInAt(1, Location::RequiresRegister());
6367 // The "out" register is used as a temporary, so it overlaps with the inputs.
6368 // Note that TypeCheckSlowPathARM uses this register too.
6369 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006370 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006371}
6372
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006373void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006374 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006375 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006376 Location obj_loc = locations->InAt(0);
6377 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006378 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006379 Location out_loc = locations->Out();
6380 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006381 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6382 DCHECK_LE(num_temps, 1u);
6383 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006384 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006385 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6386 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6387 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006388 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006389 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006390
6391 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006392 // avoid null check if we know obj is not null.
6393 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006394 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006395 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006396
Roland Levillainc9285912015-12-18 10:38:42 +00006397 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006398 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006399 // /* HeapReference<Class> */ out = obj->klass_
6400 GenerateReferenceLoadTwoRegisters(instruction,
6401 out_loc,
6402 obj_loc,
6403 class_offset,
6404 maybe_temp_loc,
6405 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 __ cmp(out, ShifterOperand(cls));
6407 // Classes must be equal for the instanceof to succeed.
6408 __ b(&zero, NE);
6409 __ LoadImmediate(out, 1);
6410 __ b(&done);
6411 break;
6412 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006413
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006414 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006415 // /* HeapReference<Class> */ out = obj->klass_
6416 GenerateReferenceLoadTwoRegisters(instruction,
6417 out_loc,
6418 obj_loc,
6419 class_offset,
6420 maybe_temp_loc,
6421 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006422 // If the class is abstract, we eagerly fetch the super class of the
6423 // object to avoid doing a comparison we know will fail.
6424 Label loop;
6425 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006426 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006427 GenerateReferenceLoadOneRegister(instruction,
6428 out_loc,
6429 super_offset,
6430 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006431 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006432 // If `out` is null, we use it for the result, and jump to `done`.
6433 __ CompareAndBranchIfZero(out, &done);
6434 __ cmp(out, ShifterOperand(cls));
6435 __ b(&loop, NE);
6436 __ LoadImmediate(out, 1);
6437 if (zero.IsLinked()) {
6438 __ b(&done);
6439 }
6440 break;
6441 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006442
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006443 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006444 // /* HeapReference<Class> */ out = obj->klass_
6445 GenerateReferenceLoadTwoRegisters(instruction,
6446 out_loc,
6447 obj_loc,
6448 class_offset,
6449 maybe_temp_loc,
6450 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006451 // Walk over the class hierarchy to find a match.
6452 Label loop, success;
6453 __ Bind(&loop);
6454 __ cmp(out, ShifterOperand(cls));
6455 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006456 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006457 GenerateReferenceLoadOneRegister(instruction,
6458 out_loc,
6459 super_offset,
6460 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006461 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006462 __ CompareAndBranchIfNonZero(out, &loop);
6463 // If `out` is null, we use it for the result, and jump to `done`.
6464 __ b(&done);
6465 __ Bind(&success);
6466 __ LoadImmediate(out, 1);
6467 if (zero.IsLinked()) {
6468 __ b(&done);
6469 }
6470 break;
6471 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006472
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006473 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006474 // /* HeapReference<Class> */ out = obj->klass_
6475 GenerateReferenceLoadTwoRegisters(instruction,
6476 out_loc,
6477 obj_loc,
6478 class_offset,
6479 maybe_temp_loc,
6480 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006481 // Do an exact check.
6482 Label exact_check;
6483 __ cmp(out, ShifterOperand(cls));
6484 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006485 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006486 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006487 GenerateReferenceLoadOneRegister(instruction,
6488 out_loc,
6489 component_offset,
6490 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006491 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006492 // If `out` is null, we use it for the result, and jump to `done`.
6493 __ CompareAndBranchIfZero(out, &done);
6494 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6495 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6496 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006497 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006498 __ LoadImmediate(out, 1);
6499 __ b(&done);
6500 break;
6501 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006502
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006503 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006504 // No read barrier since the slow path will retry upon failure.
6505 // /* HeapReference<Class> */ out = obj->klass_
6506 GenerateReferenceLoadTwoRegisters(instruction,
6507 out_loc,
6508 obj_loc,
6509 class_offset,
6510 maybe_temp_loc,
6511 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006512 __ cmp(out, ShifterOperand(cls));
6513 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006514 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6515 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006516 codegen_->AddSlowPath(slow_path);
6517 __ b(slow_path->GetEntryLabel(), NE);
6518 __ LoadImmediate(out, 1);
6519 if (zero.IsLinked()) {
6520 __ b(&done);
6521 }
6522 break;
6523 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006524
Calin Juravle98893e12015-10-02 21:05:03 +01006525 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006526 case TypeCheckKind::kInterfaceCheck: {
6527 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006528 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006529 // cases.
6530 //
6531 // We cannot directly call the InstanceofNonTrivial runtime
6532 // entry point without resorting to a type checking slow path
6533 // here (i.e. by calling InvokeRuntime directly), as it would
6534 // require to assign fixed registers for the inputs of this
6535 // HInstanceOf instruction (following the runtime calling
6536 // convention), which might be cluttered by the potential first
6537 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006538 //
6539 // TODO: Introduce a new runtime entry point taking the object
6540 // to test (instead of its class) as argument, and let it deal
6541 // with the read barrier issues. This will let us refactor this
6542 // case of the `switch` code as it was previously (with a direct
6543 // call to the runtime not using a type checking slow path).
6544 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006545 DCHECK(locations->OnlyCallsOnSlowPath());
6546 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6547 /* is_fatal */ false);
6548 codegen_->AddSlowPath(slow_path);
6549 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006550 if (zero.IsLinked()) {
6551 __ b(&done);
6552 }
6553 break;
6554 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006555 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006556
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006557 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006558 __ Bind(&zero);
6559 __ LoadImmediate(out, 0);
6560 }
6561
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006562 if (done.IsLinked()) {
6563 __ Bind(&done);
6564 }
6565
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006566 if (slow_path != nullptr) {
6567 __ Bind(slow_path->GetExitLabel());
6568 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006569}
6570
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006571void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006572 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6573 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6574
Roland Levillain3b359c72015-11-17 19:35:12 +00006575 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6576 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006577 case TypeCheckKind::kExactCheck:
6578 case TypeCheckKind::kAbstractClassCheck:
6579 case TypeCheckKind::kClassHierarchyCheck:
6580 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006581 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6582 LocationSummary::kCallOnSlowPath :
6583 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006584 break;
6585 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006586 case TypeCheckKind::kUnresolvedCheck:
6587 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006588 call_kind = LocationSummary::kCallOnSlowPath;
6589 break;
6590 }
6591
Roland Levillain3b359c72015-11-17 19:35:12 +00006592 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6593 locations->SetInAt(0, Location::RequiresRegister());
6594 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006595 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006596}
6597
6598void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006599 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006600 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006601 Location obj_loc = locations->InAt(0);
6602 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006603 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006604 Location temp_loc = locations->GetTemp(0);
6605 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006606 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6607 DCHECK_LE(num_temps, 3u);
6608 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6609 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6610 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6611 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6612 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6613 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6614 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6615 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6616 const uint32_t object_array_data_offset =
6617 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006618
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006619 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6620 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6621 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006622 bool is_type_check_slow_path_fatal = false;
6623 if (!kEmitCompilerReadBarrier) {
6624 is_type_check_slow_path_fatal =
6625 (type_check_kind == TypeCheckKind::kExactCheck ||
6626 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6627 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6628 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6629 !instruction->CanThrowIntoCatchBlock();
6630 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01006631 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006632 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6633 is_type_check_slow_path_fatal);
6634 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006635
6636 Label done;
6637 // Avoid null check if we know obj is not null.
6638 if (instruction->MustDoNullCheck()) {
6639 __ CompareAndBranchIfZero(obj, &done);
6640 }
6641
Roland Levillain3b359c72015-11-17 19:35:12 +00006642 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006643 case TypeCheckKind::kExactCheck:
6644 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006645 // /* HeapReference<Class> */ temp = obj->klass_
6646 GenerateReferenceLoadTwoRegisters(instruction,
6647 temp_loc,
6648 obj_loc,
6649 class_offset,
6650 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006651 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006652
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006653 __ cmp(temp, ShifterOperand(cls));
6654 // Jump to slow path for throwing the exception or doing a
6655 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006656 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006657 break;
6658 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006659
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006660 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006661 // /* HeapReference<Class> */ temp = obj->klass_
6662 GenerateReferenceLoadTwoRegisters(instruction,
6663 temp_loc,
6664 obj_loc,
6665 class_offset,
6666 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006667 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006668
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006669 // If the class is abstract, we eagerly fetch the super class of the
6670 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006671 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006672 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006673 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006674 GenerateReferenceLoadOneRegister(instruction,
6675 temp_loc,
6676 super_offset,
6677 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006678 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006679
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006680 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6681 // exception.
6682 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00006683
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006684 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006685 __ cmp(temp, ShifterOperand(cls));
6686 __ b(&loop, NE);
6687 break;
6688 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006689
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006690 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006691 // /* HeapReference<Class> */ temp = obj->klass_
6692 GenerateReferenceLoadTwoRegisters(instruction,
6693 temp_loc,
6694 obj_loc,
6695 class_offset,
6696 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006697 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006698
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006699 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006700 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006701 __ Bind(&loop);
6702 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006703 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006704
Roland Levillain3b359c72015-11-17 19:35:12 +00006705 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006706 GenerateReferenceLoadOneRegister(instruction,
6707 temp_loc,
6708 super_offset,
6709 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006710 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006711
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006712 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6713 // exception.
6714 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6715 // Otherwise, jump to the beginning of the loop.
6716 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006717 break;
6718 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006719
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006720 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006721 // /* HeapReference<Class> */ temp = obj->klass_
6722 GenerateReferenceLoadTwoRegisters(instruction,
6723 temp_loc,
6724 obj_loc,
6725 class_offset,
6726 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006727 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006728
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006729 // Do an exact check.
6730 __ cmp(temp, ShifterOperand(cls));
6731 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006732
6733 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006734 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006735 GenerateReferenceLoadOneRegister(instruction,
6736 temp_loc,
6737 component_offset,
6738 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006739 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006740 // If the component type is null, jump to the slow path to throw the exception.
6741 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6742 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6743 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006744 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006745 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006746 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006747 break;
6748 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006749
Calin Juravle98893e12015-10-02 21:05:03 +01006750 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006751 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00006752 // We cannot directly call the CheckCast runtime entry point
6753 // without resorting to a type checking slow path here (i.e. by
6754 // calling InvokeRuntime directly), as it would require to
6755 // assign fixed registers for the inputs of this HInstanceOf
6756 // instruction (following the runtime calling convention), which
6757 // might be cluttered by the potential first read barrier
6758 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006759
Roland Levillain3b359c72015-11-17 19:35:12 +00006760 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006761 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006762
6763 case TypeCheckKind::kInterfaceCheck: {
6764 // Avoid read barriers to improve performance of the fast path. We can not get false
6765 // positives by doing this.
6766 // /* HeapReference<Class> */ temp = obj->klass_
6767 GenerateReferenceLoadTwoRegisters(instruction,
6768 temp_loc,
6769 obj_loc,
6770 class_offset,
6771 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006772 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006773
6774 // /* HeapReference<Class> */ temp = temp->iftable_
6775 GenerateReferenceLoadTwoRegisters(instruction,
6776 temp_loc,
6777 temp_loc,
6778 iftable_offset,
6779 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006780 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006781 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006782 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006783 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006784 Label start_loop;
6785 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006786 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
6787 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006788 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
6789 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006790 // Go to next interface.
6791 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
6792 __ sub(maybe_temp2_loc.AsRegister<Register>(),
6793 maybe_temp2_loc.AsRegister<Register>(),
6794 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006795 // Compare the classes and continue the loop if they do not match.
6796 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
6797 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006798 break;
6799 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006800 }
6801 __ Bind(&done);
6802
Roland Levillain3b359c72015-11-17 19:35:12 +00006803 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006804}
6805
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006806void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6807 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006808 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006809 InvokeRuntimeCallingConvention calling_convention;
6810 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6811}
6812
6813void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006814 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6815 instruction,
6816 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006817 if (instruction->IsEnter()) {
6818 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6819 } else {
6820 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6821 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006822}
6823
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006824void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6825void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6826void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006827
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006828void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006829 LocationSummary* locations =
6830 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6831 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6832 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006833 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006834 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006835 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006836 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006837}
6838
6839void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6840 HandleBitwiseOperation(instruction);
6841}
6842
6843void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6844 HandleBitwiseOperation(instruction);
6845}
6846
6847void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6848 HandleBitwiseOperation(instruction);
6849}
6850
Artem Serov7fc63502016-02-09 17:15:29 +00006851
6852void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6853 LocationSummary* locations =
6854 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6855 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6856 || instruction->GetResultType() == Primitive::kPrimLong);
6857
6858 locations->SetInAt(0, Location::RequiresRegister());
6859 locations->SetInAt(1, Location::RequiresRegister());
6860 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6861}
6862
6863void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6864 LocationSummary* locations = instruction->GetLocations();
6865 Location first = locations->InAt(0);
6866 Location second = locations->InAt(1);
6867 Location out = locations->Out();
6868
6869 if (instruction->GetResultType() == Primitive::kPrimInt) {
6870 Register first_reg = first.AsRegister<Register>();
6871 ShifterOperand second_reg(second.AsRegister<Register>());
6872 Register out_reg = out.AsRegister<Register>();
6873
6874 switch (instruction->GetOpKind()) {
6875 case HInstruction::kAnd:
6876 __ bic(out_reg, first_reg, second_reg);
6877 break;
6878 case HInstruction::kOr:
6879 __ orn(out_reg, first_reg, second_reg);
6880 break;
6881 // There is no EON on arm.
6882 case HInstruction::kXor:
6883 default:
6884 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6885 UNREACHABLE();
6886 }
6887 return;
6888
6889 } else {
6890 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6891 Register first_low = first.AsRegisterPairLow<Register>();
6892 Register first_high = first.AsRegisterPairHigh<Register>();
6893 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6894 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6895 Register out_low = out.AsRegisterPairLow<Register>();
6896 Register out_high = out.AsRegisterPairHigh<Register>();
6897
6898 switch (instruction->GetOpKind()) {
6899 case HInstruction::kAnd:
6900 __ bic(out_low, first_low, second_low);
6901 __ bic(out_high, first_high, second_high);
6902 break;
6903 case HInstruction::kOr:
6904 __ orn(out_low, first_low, second_low);
6905 __ orn(out_high, first_high, second_high);
6906 break;
6907 // There is no EON on arm.
6908 case HInstruction::kXor:
6909 default:
6910 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6911 UNREACHABLE();
6912 }
6913 }
6914}
6915
Anton Kirilov74234da2017-01-13 14:42:47 +00006916void LocationsBuilderARM::VisitDataProcWithShifterOp(
6917 HDataProcWithShifterOp* instruction) {
6918 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
6919 instruction->GetType() == Primitive::kPrimLong);
6920 LocationSummary* locations =
6921 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6922 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
6923 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
6924
6925 locations->SetInAt(0, Location::RequiresRegister());
6926 locations->SetInAt(1, Location::RequiresRegister());
6927 locations->SetOut(Location::RequiresRegister(),
6928 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6929}
6930
6931void InstructionCodeGeneratorARM::VisitDataProcWithShifterOp(
6932 HDataProcWithShifterOp* instruction) {
6933 const LocationSummary* const locations = instruction->GetLocations();
6934 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
6935 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
6936 const Location left = locations->InAt(0);
6937 const Location right = locations->InAt(1);
6938 const Location out = locations->Out();
6939
6940 if (instruction->GetType() == Primitive::kPrimInt) {
6941 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
6942
6943 const Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
6944 ? right.AsRegisterPairLow<Register>()
6945 : right.AsRegister<Register>();
6946
6947 GenerateDataProcInstruction(kind,
6948 out.AsRegister<Register>(),
6949 left.AsRegister<Register>(),
6950 ShifterOperand(second,
6951 ShiftFromOpKind(op_kind),
6952 instruction->GetShiftAmount()),
6953 codegen_);
6954 } else {
6955 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
6956
6957 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
6958 const Register second = right.AsRegister<Register>();
6959
6960 DCHECK_NE(out.AsRegisterPairLow<Register>(), second);
6961 GenerateDataProc(kind,
6962 out,
6963 left,
6964 ShifterOperand(second),
6965 ShifterOperand(second, ASR, 31),
6966 codegen_);
6967 } else {
6968 GenerateLongDataProc(instruction, codegen_);
6969 }
6970 }
6971}
6972
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006973void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6974 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6975 if (value == 0xffffffffu) {
6976 if (out != first) {
6977 __ mov(out, ShifterOperand(first));
6978 }
6979 return;
6980 }
6981 if (value == 0u) {
6982 __ mov(out, ShifterOperand(0));
6983 return;
6984 }
6985 ShifterOperand so;
6986 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6987 __ and_(out, first, so);
6988 } else {
6989 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6990 __ bic(out, first, ShifterOperand(~value));
6991 }
6992}
6993
6994void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6995 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6996 if (value == 0u) {
6997 if (out != first) {
6998 __ mov(out, ShifterOperand(first));
6999 }
7000 return;
7001 }
7002 if (value == 0xffffffffu) {
7003 __ mvn(out, ShifterOperand(0));
7004 return;
7005 }
7006 ShifterOperand so;
7007 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
7008 __ orr(out, first, so);
7009 } else {
7010 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
7011 __ orn(out, first, ShifterOperand(~value));
7012 }
7013}
7014
7015void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
7016 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7017 if (value == 0u) {
7018 if (out != first) {
7019 __ mov(out, ShifterOperand(first));
7020 }
7021 return;
7022 }
7023 __ eor(out, first, ShifterOperand(value));
7024}
7025
Vladimir Marko59751a72016-08-05 14:37:27 +01007026void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
7027 Location first,
7028 uint64_t value) {
7029 Register out_low = out.AsRegisterPairLow<Register>();
7030 Register out_high = out.AsRegisterPairHigh<Register>();
7031 Register first_low = first.AsRegisterPairLow<Register>();
7032 Register first_high = first.AsRegisterPairHigh<Register>();
7033 uint32_t value_low = Low32Bits(value);
7034 uint32_t value_high = High32Bits(value);
7035 if (value_low == 0u) {
7036 if (out_low != first_low) {
7037 __ mov(out_low, ShifterOperand(first_low));
7038 }
7039 __ AddConstant(out_high, first_high, value_high);
7040 return;
7041 }
7042 __ AddConstantSetFlags(out_low, first_low, value_low);
7043 ShifterOperand so;
7044 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
7045 __ adc(out_high, first_high, so);
7046 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
7047 __ sbc(out_high, first_high, so);
7048 } else {
7049 LOG(FATAL) << "Unexpected constant " << value_high;
7050 UNREACHABLE();
7051 }
7052}
7053
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007054void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
7055 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007056 Location first = locations->InAt(0);
7057 Location second = locations->InAt(1);
7058 Location out = locations->Out();
7059
7060 if (second.IsConstant()) {
7061 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7062 uint32_t value_low = Low32Bits(value);
7063 if (instruction->GetResultType() == Primitive::kPrimInt) {
7064 Register first_reg = first.AsRegister<Register>();
7065 Register out_reg = out.AsRegister<Register>();
7066 if (instruction->IsAnd()) {
7067 GenerateAndConst(out_reg, first_reg, value_low);
7068 } else if (instruction->IsOr()) {
7069 GenerateOrrConst(out_reg, first_reg, value_low);
7070 } else {
7071 DCHECK(instruction->IsXor());
7072 GenerateEorConst(out_reg, first_reg, value_low);
7073 }
7074 } else {
7075 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7076 uint32_t value_high = High32Bits(value);
7077 Register first_low = first.AsRegisterPairLow<Register>();
7078 Register first_high = first.AsRegisterPairHigh<Register>();
7079 Register out_low = out.AsRegisterPairLow<Register>();
7080 Register out_high = out.AsRegisterPairHigh<Register>();
7081 if (instruction->IsAnd()) {
7082 GenerateAndConst(out_low, first_low, value_low);
7083 GenerateAndConst(out_high, first_high, value_high);
7084 } else if (instruction->IsOr()) {
7085 GenerateOrrConst(out_low, first_low, value_low);
7086 GenerateOrrConst(out_high, first_high, value_high);
7087 } else {
7088 DCHECK(instruction->IsXor());
7089 GenerateEorConst(out_low, first_low, value_low);
7090 GenerateEorConst(out_high, first_high, value_high);
7091 }
7092 }
7093 return;
7094 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007095
7096 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007097 Register first_reg = first.AsRegister<Register>();
7098 ShifterOperand second_reg(second.AsRegister<Register>());
7099 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007100 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007101 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007102 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007103 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007104 } else {
7105 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007106 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007107 }
7108 } else {
7109 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007110 Register first_low = first.AsRegisterPairLow<Register>();
7111 Register first_high = first.AsRegisterPairHigh<Register>();
7112 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7113 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7114 Register out_low = out.AsRegisterPairLow<Register>();
7115 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007116 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007117 __ and_(out_low, first_low, second_low);
7118 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007119 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007120 __ orr(out_low, first_low, second_low);
7121 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007122 } else {
7123 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007124 __ eor(out_low, first_low, second_low);
7125 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007126 }
7127 }
7128}
7129
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007130void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
7131 HInstruction* instruction,
7132 Location out,
7133 uint32_t offset,
7134 Location maybe_temp,
7135 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007136 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007137 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007138 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007139 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007140 if (kUseBakerReadBarrier) {
7141 // Load with fast path based Baker's read barrier.
7142 // /* HeapReference<Object> */ out = *(out + offset)
7143 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007144 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007145 } else {
7146 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007147 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00007148 // in the following move operation, as we will need it for the
7149 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007150 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00007151 // /* HeapReference<Object> */ out = *(out + offset)
7152 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007153 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00007154 }
7155 } else {
7156 // Plain load with no read barrier.
7157 // /* HeapReference<Object> */ out = *(out + offset)
7158 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7159 __ MaybeUnpoisonHeapReference(out_reg);
7160 }
7161}
7162
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007163void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
7164 HInstruction* instruction,
7165 Location out,
7166 Location obj,
7167 uint32_t offset,
7168 Location maybe_temp,
7169 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007170 Register out_reg = out.AsRegister<Register>();
7171 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007172 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007173 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007174 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007175 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007176 // Load with fast path based Baker's read barrier.
7177 // /* HeapReference<Object> */ out = *(obj + offset)
7178 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007179 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007180 } else {
7181 // Load with slow path based read barrier.
7182 // /* HeapReference<Object> */ out = *(obj + offset)
7183 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7184 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7185 }
7186 } else {
7187 // Plain load with no read barrier.
7188 // /* HeapReference<Object> */ out = *(obj + offset)
7189 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7190 __ MaybeUnpoisonHeapReference(out_reg);
7191 }
7192}
7193
7194void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
7195 Location root,
7196 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007197 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007198 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007199 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007200 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007201 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007202 if (kUseBakerReadBarrier) {
7203 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00007204 // Baker's read barrier are used.
Roland Levillainc9285912015-12-18 10:38:42 +00007205 //
Roland Levillainba650a42017-03-06 13:52:32 +00007206 // Note that we do not actually check the value of
7207 // `GetIsGcMarking()` to decide whether to mark the loaded GC
7208 // root or not. Instead, we load into `temp` the read barrier
7209 // mark entry point corresponding to register `root`. If `temp`
7210 // is null, it means that `GetIsGcMarking()` is false, and vice
7211 // versa.
7212 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007213 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00007214 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7215 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
7216 // // Slow path.
7217 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
Roland Levillainc9285912015-12-18 10:38:42 +00007218 // }
7219
Roland Levillainba650a42017-03-06 13:52:32 +00007220 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
7221 Location temp = Location::RegisterLocation(LR);
7222 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
7223 instruction, root, /* entrypoint */ temp);
7224 codegen_->AddSlowPath(slow_path);
7225
7226 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7227 const int32_t entry_point_offset =
7228 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
7229 // Loading the entrypoint does not require a load acquire since it is only changed when
7230 // threads are suspended or running a checkpoint.
7231 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
7232
Roland Levillainc9285912015-12-18 10:38:42 +00007233 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7234 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7235 static_assert(
7236 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7237 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7238 "have different sizes.");
7239 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7240 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7241 "have different sizes.");
7242
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007243 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7244 // checking GetIsGcMarking.
7245 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00007246 __ Bind(slow_path->GetExitLabel());
7247 } else {
7248 // GC root loaded through a slow path for read barriers other
7249 // than Baker's.
7250 // /* GcRoot<mirror::Object>* */ root = obj + offset
7251 __ AddConstant(root_reg, obj, offset);
7252 // /* mirror::Object* */ root = root->Read()
7253 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7254 }
7255 } else {
7256 // Plain GC root load with no read barrier.
7257 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7258 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7259 // Note that GC roots are not affected by heap poisoning, thus we
7260 // do not have to unpoison `root_reg` here.
7261 }
7262}
7263
7264void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7265 Location ref,
7266 Register obj,
7267 uint32_t offset,
7268 Location temp,
7269 bool needs_null_check) {
7270 DCHECK(kEmitCompilerReadBarrier);
7271 DCHECK(kUseBakerReadBarrier);
7272
7273 // /* HeapReference<Object> */ ref = *(obj + offset)
7274 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01007275 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00007276 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007277 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007278}
7279
7280void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7281 Location ref,
7282 Register obj,
7283 uint32_t data_offset,
7284 Location index,
7285 Location temp,
7286 bool needs_null_check) {
7287 DCHECK(kEmitCompilerReadBarrier);
7288 DCHECK(kUseBakerReadBarrier);
7289
Roland Levillainbfea3352016-06-23 13:48:47 +01007290 static_assert(
7291 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7292 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007293 // /* HeapReference<Object> */ ref =
7294 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01007295 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00007296 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007297 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007298}
7299
7300void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7301 Location ref,
7302 Register obj,
7303 uint32_t offset,
7304 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01007305 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00007306 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007307 bool needs_null_check,
7308 bool always_update_field,
7309 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00007310 DCHECK(kEmitCompilerReadBarrier);
7311 DCHECK(kUseBakerReadBarrier);
7312
Roland Levillainba650a42017-03-06 13:52:32 +00007313 // After loading the reference from `obj.field` into `ref`, query
7314 // `art::Thread::Current()->GetIsGcMarking()` to decide whether we
7315 // need to enter the slow path to mark the reference. This
7316 // optimistic strategy (we expect the GC to not be marking most of
7317 // the time) does not check `obj`'s lock word (to see if it is a
7318 // gray object or not), so may sometimes mark an already marked
7319 // object.
Roland Levillainc9285912015-12-18 10:38:42 +00007320 //
Roland Levillainba650a42017-03-06 13:52:32 +00007321 // Note that we do not actually check the value of `GetIsGcMarking()`;
7322 // instead, we load into `temp3` the read barrier mark entry point
7323 // corresponding to register `ref`. If `temp3` is null, it means
7324 // that `GetIsGcMarking()` is false, and vice versa.
7325 //
7326 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7327 // HeapReference<mirror::Object> ref = *src; // Original reference load.
7328 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
7329 // // Slow path.
7330 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainc9285912015-12-18 10:38:42 +00007331 // }
Roland Levillainc9285912015-12-18 10:38:42 +00007332
Roland Levillainba650a42017-03-06 13:52:32 +00007333 // TODO: This temp register is only necessary when
7334 // `always_update_field` is true; make it optional (like `temp2`).
Roland Levillain35345a52017-02-27 14:32:08 +00007335 Register temp_reg = temp.AsRegister<Register>();
Roland Levillain1372c9f2017-01-13 11:47:39 +00007336
Roland Levillainba650a42017-03-06 13:52:32 +00007337 // Slow path marking the object `ref` when the GC is marking. The
7338 // entrypoint will already be loaded in `temp3`.
7339 Location temp3 = Location::RegisterLocation(LR);
7340 SlowPathCodeARM* slow_path;
7341 if (always_update_field) {
7342 DCHECK(temp2 != nullptr);
7343 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
7344 // of the form `obj + field_offset`, where `obj` is a register and
7345 // `field_offset` is a register pair (of which only the lower half
7346 // is used). Thus `offset` and `scale_factor` above are expected
7347 // to be null in this code path.
7348 DCHECK_EQ(offset, 0u);
7349 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7350 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
7351 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2, /* entrypoint */ temp3);
7352 } else {
7353 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
7354 instruction, ref, /* entrypoint */ temp3);
Roland Levillain35345a52017-02-27 14:32:08 +00007355 }
Roland Levillainba650a42017-03-06 13:52:32 +00007356 AddSlowPath(slow_path);
Roland Levillain35345a52017-02-27 14:32:08 +00007357
Roland Levillainba650a42017-03-06 13:52:32 +00007358 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
7359 const int32_t entry_point_offset =
7360 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
7361 // Loading the entrypoint does not require a load acquire since it is only changed when
7362 // threads are suspended or running a checkpoint.
7363 __ LoadFromOffset(kLoadWord, temp3.AsRegister<Register>(), TR, entry_point_offset);
7364 // The reference load.
7365 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
7366 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7367 // checking GetIsGcMarking.
7368 __ CompareAndBranchIfNonZero(temp3.AsRegister<Register>(), slow_path->GetEntryLabel());
7369 __ Bind(slow_path->GetExitLabel());
7370}
Roland Levillain35345a52017-02-27 14:32:08 +00007371
Roland Levillainba650a42017-03-06 13:52:32 +00007372void CodeGeneratorARM::GenerateRawReferenceLoad(HInstruction* instruction,
7373 Location ref,
7374 Register obj,
7375 uint32_t offset,
7376 Location index,
7377 ScaleFactor scale_factor,
7378 bool needs_null_check) {
7379 Register ref_reg = ref.AsRegister<Register>();
7380
Roland Levillainc9285912015-12-18 10:38:42 +00007381 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007382 // Load types involving an "index": ArrayGet,
7383 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7384 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00007385 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00007386 if (index.IsConstant()) {
7387 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01007388 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00007389 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7390 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01007391 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007392 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7393 // intrinsics, which use a register pair as index ("long
7394 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01007395 Register index_reg = index.IsRegisterPair()
7396 ? index.AsRegisterPairLow<Register>()
7397 : index.AsRegister<Register>();
7398 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00007399 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
7400 }
7401 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00007402 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillainc9285912015-12-18 10:38:42 +00007403 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7404 }
7405
Roland Levillainba650a42017-03-06 13:52:32 +00007406 if (needs_null_check) {
7407 MaybeRecordImplicitNullCheck(instruction);
7408 }
7409
Roland Levillainc9285912015-12-18 10:38:42 +00007410 // Object* ref = ref_addr->AsMirrorPtr()
7411 __ MaybeUnpoisonHeapReference(ref_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00007412}
7413
7414void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
7415 Location out,
7416 Location ref,
7417 Location obj,
7418 uint32_t offset,
7419 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007420 DCHECK(kEmitCompilerReadBarrier);
7421
Roland Levillainc9285912015-12-18 10:38:42 +00007422 // Insert a slow path based read barrier *after* the reference load.
7423 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007424 // If heap poisoning is enabled, the unpoisoning of the loaded
7425 // reference will be carried out by the runtime within the slow
7426 // path.
7427 //
7428 // Note that `ref` currently does not get unpoisoned (when heap
7429 // poisoning is enabled), which is alright as the `ref` argument is
7430 // not used by the artReadBarrierSlow entry point.
7431 //
7432 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007433 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00007434 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
7435 AddSlowPath(slow_path);
7436
Roland Levillain3b359c72015-11-17 19:35:12 +00007437 __ b(slow_path->GetEntryLabel());
7438 __ Bind(slow_path->GetExitLabel());
7439}
7440
Roland Levillainc9285912015-12-18 10:38:42 +00007441void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7442 Location out,
7443 Location ref,
7444 Location obj,
7445 uint32_t offset,
7446 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007447 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00007448 // Baker's read barriers shall be handled by the fast path
7449 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
7450 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007451 // If heap poisoning is enabled, unpoisoning will be taken care of
7452 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007453 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00007454 } else if (kPoisonHeapReferences) {
7455 __ UnpoisonHeapReference(out.AsRegister<Register>());
7456 }
7457}
7458
Roland Levillainc9285912015-12-18 10:38:42 +00007459void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7460 Location out,
7461 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007462 DCHECK(kEmitCompilerReadBarrier);
7463
Roland Levillainc9285912015-12-18 10:38:42 +00007464 // Insert a slow path based read barrier *after* the GC root load.
7465 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007466 // Note that GC roots are not affected by heap poisoning, so we do
7467 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007468 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007469 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
7470 AddSlowPath(slow_path);
7471
Roland Levillain3b359c72015-11-17 19:35:12 +00007472 __ b(slow_path->GetEntryLabel());
7473 __ Bind(slow_path->GetExitLabel());
7474}
7475
Vladimir Markodc151b22015-10-15 18:02:30 +01007476HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7477 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007478 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00007479 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007480}
7481
Vladimir Markob4536b72015-11-24 13:45:23 +00007482Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7483 Register temp) {
7484 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7485 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7486 if (!invoke->GetLocations()->Intrinsified()) {
7487 return location.AsRegister<Register>();
7488 }
7489 // For intrinsics we allow any location, so it may be on the stack.
7490 if (!location.IsRegister()) {
7491 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7492 return temp;
7493 }
7494 // For register locations, check if the register was saved. If so, get it from the stack.
7495 // Note: There is a chance that the register was saved but not overwritten, so we could
7496 // save one load. However, since this is just an intrinsic slow path we prefer this
7497 // simple and more robust approach rather that trying to determine if that's the case.
7498 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08007499 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00007500 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7501 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7502 return temp;
7503 }
7504 return location.AsRegister<Register>();
7505}
7506
TatWai Chongd8c052a2016-11-02 16:12:48 +08007507Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
7508 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007509 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7510 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007511 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7512 uint32_t offset =
7513 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007514 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007515 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007516 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007517 }
Vladimir Marko58155012015-08-19 12:49:41 +00007518 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007519 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007520 break;
7521 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7522 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7523 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007524 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7525 HArmDexCacheArraysBase* base =
7526 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7527 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7528 temp.AsRegister<Register>());
7529 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7530 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7531 break;
7532 }
Vladimir Marko58155012015-08-19 12:49:41 +00007533 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007534 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007535 Register method_reg;
7536 Register reg = temp.AsRegister<Register>();
7537 if (current_method.IsRegister()) {
7538 method_reg = current_method.AsRegister<Register>();
7539 } else {
7540 DCHECK(invoke->GetLocations()->Intrinsified());
7541 DCHECK(!current_method.IsValid());
7542 method_reg = reg;
7543 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7544 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007545 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7546 __ LoadFromOffset(kLoadWord,
7547 reg,
7548 method_reg,
7549 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007550 // temp = temp[index_in_cache];
7551 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7552 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007553 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7554 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007555 }
Vladimir Marko58155012015-08-19 12:49:41 +00007556 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08007557 return callee_method;
7558}
7559
7560void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
7561 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00007562
7563 switch (invoke->GetCodePtrLocation()) {
7564 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7565 __ bl(GetFrameEntryLabel());
7566 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007567 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7568 // LR = callee_method->entry_point_from_quick_compiled_code_
7569 __ LoadFromOffset(
7570 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007571 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007572 // LR()
7573 __ blx(LR);
7574 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007575 }
7576
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007577 DCHECK(!IsLeafMethod());
7578}
7579
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007580void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7581 Register temp = temp_location.AsRegister<Register>();
7582 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7583 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007584
7585 // Use the calling convention instead of the location of the receiver, as
7586 // intrinsics may have put the receiver in a different register. In the intrinsics
7587 // slow path, the arguments have been moved to the right place, so here we are
7588 // guaranteed that the receiver is the first register of the calling convention.
7589 InvokeDexCallingConvention calling_convention;
7590 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007591 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007592 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007593 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007594 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007595 // Instead of simply (possibly) unpoisoning `temp` here, we should
7596 // emit a read barrier for the previous class reference load.
7597 // However this is not required in practice, as this is an
7598 // intermediate/temporary reference and because the current
7599 // concurrent copying collector keeps the from-space memory
7600 // intact/accessible until the end of the marking phase (the
7601 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007602 __ MaybeUnpoisonHeapReference(temp);
7603 // temp = temp->GetMethodAt(method_offset);
7604 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007605 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007606 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7607 // LR = temp->GetEntryPoint();
7608 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7609 // LR();
7610 __ blx(LR);
7611}
7612
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007613CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007614 const DexFile& dex_file, dex::StringIndex string_index) {
7615 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007616}
7617
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007618CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08007619 const DexFile& dex_file, dex::TypeIndex type_index) {
7620 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007621}
7622
Vladimir Marko1998cd02017-01-13 13:02:58 +00007623CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
7624 const DexFile& dex_file, dex::TypeIndex type_index) {
7625 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
7626}
7627
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007628CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7629 const DexFile& dex_file, uint32_t element_offset) {
7630 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7631}
7632
7633CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7634 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7635 patches->emplace_back(dex_file, offset_or_index);
7636 return &patches->back();
7637}
7638
7639Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007640 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007641 return boot_image_string_patches_.GetOrCreate(
7642 StringReference(&dex_file, string_index),
7643 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7644}
7645
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007646Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007647 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007648 return boot_image_type_patches_.GetOrCreate(
7649 TypeReference(&dex_file, type_index),
7650 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7651}
7652
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007653Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7654 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7655 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7656 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7657}
7658
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007659Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007660 dex::StringIndex string_index,
7661 Handle<mirror::String> handle) {
7662 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7663 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007664 return jit_string_patches_.GetOrCreate(
7665 StringReference(&dex_file, string_index),
7666 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7667}
7668
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007669Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
7670 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007671 Handle<mirror::Class> handle) {
7672 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
7673 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007674 return jit_class_patches_.GetOrCreate(
7675 TypeReference(&dex_file, type_index),
7676 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7677}
7678
Vladimir Markoaad75c62016-10-03 08:46:48 +00007679template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7680inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7681 const ArenaDeque<PcRelativePatchInfo>& infos,
7682 ArenaVector<LinkerPatch>* linker_patches) {
7683 for (const PcRelativePatchInfo& info : infos) {
7684 const DexFile& dex_file = info.target_dex_file;
7685 size_t offset_or_index = info.offset_or_index;
7686 DCHECK(info.add_pc_label.IsBound());
7687 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7688 // Add MOVW patch.
7689 DCHECK(info.movw_label.IsBound());
7690 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7691 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7692 // Add MOVT patch.
7693 DCHECK(info.movt_label.IsBound());
7694 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7695 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7696 }
7697}
7698
Vladimir Marko58155012015-08-19 12:49:41 +00007699void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7700 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007701 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00007702 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007703 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007704 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007705 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007706 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00007707 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007708 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007709 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007710 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7711 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007712 for (const auto& entry : boot_image_string_patches_) {
7713 const StringReference& target_string = entry.first;
7714 Literal* literal = entry.second;
7715 DCHECK(literal->GetLabel()->IsBound());
7716 uint32_t literal_offset = literal->GetLabel()->Position();
7717 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7718 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007719 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007720 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007721 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00007722 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007723 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7724 linker_patches);
7725 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007726 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7727 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007728 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7729 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007730 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007731 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
7732 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007733 for (const auto& entry : boot_image_type_patches_) {
7734 const TypeReference& target_type = entry.first;
7735 Literal* literal = entry.second;
7736 DCHECK(literal->GetLabel()->IsBound());
7737 uint32_t literal_offset = literal->GetLabel()->Position();
7738 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7739 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007740 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007741 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007742 for (const auto& entry : boot_image_address_patches_) {
7743 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7744 Literal* literal = entry.second;
7745 DCHECK(literal->GetLabel()->IsBound());
7746 uint32_t literal_offset = literal->GetLabel()->Position();
7747 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7748 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007749 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007750}
7751
7752Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7753 return map->GetOrCreate(
7754 value,
7755 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007756}
7757
7758Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7759 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007760 return map->GetOrCreate(
7761 target_method,
7762 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007763}
7764
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007765void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7766 LocationSummary* locations =
7767 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7768 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7769 Location::RequiresRegister());
7770 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7771 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7772 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7773}
7774
7775void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7776 LocationSummary* locations = instr->GetLocations();
7777 Register res = locations->Out().AsRegister<Register>();
7778 Register accumulator =
7779 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7780 Register mul_left =
7781 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7782 Register mul_right =
7783 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7784
7785 if (instr->GetOpKind() == HInstruction::kAdd) {
7786 __ mla(res, mul_left, mul_right, accumulator);
7787 } else {
7788 __ mls(res, mul_left, mul_right, accumulator);
7789 }
7790}
7791
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007792void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007793 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007794 LOG(FATAL) << "Unreachable";
7795}
7796
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007797void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007798 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007799 LOG(FATAL) << "Unreachable";
7800}
7801
Mark Mendellfe57faa2015-09-18 09:26:15 -04007802// Simple implementation of packed switch - generate cascaded compare/jumps.
7803void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7804 LocationSummary* locations =
7805 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7806 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007807 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007808 codegen_->GetAssembler()->IsThumb()) {
7809 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7810 if (switch_instr->GetStartValue() != 0) {
7811 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7812 }
7813 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007814}
7815
7816void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7817 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007818 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007819 LocationSummary* locations = switch_instr->GetLocations();
7820 Register value_reg = locations->InAt(0).AsRegister<Register>();
7821 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7822
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007823 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007824 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007825 Register temp_reg = IP;
7826 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7827 // the immediate, because IP is used as the destination register. For the other
7828 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7829 // and they can be encoded in the instruction without making use of IP register.
7830 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7831
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007832 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007833 // Jump to successors[0] if value == lower_bound.
7834 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7835 int32_t last_index = 0;
7836 for (; num_entries - last_index > 2; last_index += 2) {
7837 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7838 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7839 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7840 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7841 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7842 }
7843 if (num_entries - last_index == 2) {
7844 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007845 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007846 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007847 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007848
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007849 // And the default for any other value.
7850 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7851 __ b(codegen_->GetLabelOf(default_block));
7852 }
7853 } else {
7854 // Create a table lookup.
7855 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7856
7857 // Materialize a pointer to the switch table
7858 std::vector<Label*> labels(num_entries);
7859 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7860 for (uint32_t i = 0; i < num_entries; i++) {
7861 labels[i] = codegen_->GetLabelOf(successors[i]);
7862 }
7863 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7864
7865 // Remove the bias.
7866 Register key_reg;
7867 if (lower_bound != 0) {
7868 key_reg = locations->GetTemp(1).AsRegister<Register>();
7869 __ AddConstant(key_reg, value_reg, -lower_bound);
7870 } else {
7871 key_reg = value_reg;
7872 }
7873
7874 // Check whether the value is in the table, jump to default block if not.
7875 __ CmpConstant(key_reg, num_entries - 1);
7876 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7877
7878 // Load the displacement from the table.
7879 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7880
7881 // Dispatch is a direct add to the PC (for Thumb2).
7882 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007883 }
7884}
7885
Vladimir Markob4536b72015-11-24 13:45:23 +00007886void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7887 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7888 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007889}
7890
7891void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7892 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007893 CodeGeneratorARM::PcRelativePatchInfo* labels =
7894 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007895 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007896 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007897 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007898 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007899 __ BindTrackedLabel(&labels->add_pc_label);
7900 __ add(base_reg, base_reg, ShifterOperand(PC));
7901}
7902
Andreas Gampe85b62f22015-09-09 13:15:38 -07007903void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7904 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007905 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007906 return;
7907 }
7908
7909 DCHECK_NE(type, Primitive::kPrimVoid);
7910
7911 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7912 if (return_loc.Equals(trg)) {
7913 return;
7914 }
7915
7916 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7917 // with the last branch.
7918 if (type == Primitive::kPrimLong) {
7919 HParallelMove parallel_move(GetGraph()->GetArena());
7920 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7921 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7922 GetMoveResolver()->EmitNativeCode(&parallel_move);
7923 } else if (type == Primitive::kPrimDouble) {
7924 HParallelMove parallel_move(GetGraph()->GetArena());
7925 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7926 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7927 GetMoveResolver()->EmitNativeCode(&parallel_move);
7928 } else {
7929 // Let the parallel move resolver take care of all of this.
7930 HParallelMove parallel_move(GetGraph()->GetArena());
7931 parallel_move.AddMove(return_loc, trg, type, nullptr);
7932 GetMoveResolver()->EmitNativeCode(&parallel_move);
7933 }
7934}
7935
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007936void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7937 LocationSummary* locations =
7938 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7939 locations->SetInAt(0, Location::RequiresRegister());
7940 locations->SetOut(Location::RequiresRegister());
7941}
7942
7943void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7944 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007945 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007946 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007947 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007948 __ LoadFromOffset(kLoadWord,
7949 locations->Out().AsRegister<Register>(),
7950 locations->InAt(0).AsRegister<Register>(),
7951 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007952 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007953 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007954 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007955 __ LoadFromOffset(kLoadWord,
7956 locations->Out().AsRegister<Register>(),
7957 locations->InAt(0).AsRegister<Register>(),
7958 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7959 __ LoadFromOffset(kLoadWord,
7960 locations->Out().AsRegister<Register>(),
7961 locations->Out().AsRegister<Register>(),
7962 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007963 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007964}
7965
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007966static void PatchJitRootUse(uint8_t* code,
7967 const uint8_t* roots_data,
7968 Literal* literal,
7969 uint64_t index_in_table) {
7970 DCHECK(literal->GetLabel()->IsBound());
7971 uint32_t literal_offset = literal->GetLabel()->Position();
7972 uintptr_t address =
7973 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7974 uint8_t* data = code + literal_offset;
7975 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7976}
7977
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007978void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7979 for (const auto& entry : jit_string_patches_) {
7980 const auto& it = jit_string_roots_.find(entry.first);
7981 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007982 PatchJitRootUse(code, roots_data, entry.second, it->second);
7983 }
7984 for (const auto& entry : jit_class_patches_) {
7985 const auto& it = jit_class_roots_.find(entry.first);
7986 DCHECK(it != jit_class_roots_.end());
7987 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007988 }
7989}
7990
Roland Levillain4d027112015-07-01 15:41:14 +01007991#undef __
7992#undef QUICK_ENTRY_POINT
7993
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007994} // namespace arm
7995} // namespace art