blob: 511bd9b7ef1ebb0b6e0e56be563e4d747da316c1 [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 Levillain35345a52017-02-27 14:32:08 +0000653// is for the GcRoot read barrier.
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 Levillain47b3ab22017-02-27 14:31:35 +0000718 int32_t entry_point_offset =
719 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
720 // This runtime call does not require a stack map.
721 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
722 }
723 __ b(GetExitLabel());
724 }
725
726 private:
727 // The location (register) of the marked object reference.
728 const Location ref_;
729
730 // The location of the entrypoint if already loaded.
731 const Location entrypoint_;
732
733 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
734};
735
736// Slow path marking an object reference `ref` during a read barrier,
737// and if needed, atomically updating the field `obj.field` in the
738// object `obj` holding this reference after marking (contrary to
739// ReadBarrierMarkSlowPathARM above, which never tries to update
740// `obj.field`).
741//
742// This means that after the execution of this slow path, both `ref`
743// and `obj.field` will be up-to-date; i.e., after the flip, both will
744// hold the same to-space reference (unless another thread installed
745// another object reference (different from `ref`) in `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000746class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
747 public:
748 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
749 Location ref,
750 Register obj,
751 Location field_offset,
752 Register temp1,
Roland Levillain35345a52017-02-27 14:32:08 +0000753 Register temp2)
Roland Levillain47b3ab22017-02-27 14:31:35 +0000754 : SlowPathCodeARM(instruction),
755 ref_(ref),
756 obj_(obj),
757 field_offset_(field_offset),
758 temp1_(temp1),
Roland Levillain35345a52017-02-27 14:32:08 +0000759 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000760 DCHECK(kEmitCompilerReadBarrier);
761 }
762
763 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
764
765 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
766 LocationSummary* locations = instruction_->GetLocations();
767 Register ref_reg = ref_.AsRegister<Register>();
768 DCHECK(locations->CanCall());
769 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
770 // This slow path is only used by the UnsafeCASObject intrinsic.
771 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
772 << "Unexpected instruction in read barrier marking and field updating slow path: "
773 << instruction_->DebugName();
774 DCHECK(instruction_->GetLocations()->Intrinsified());
775 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
776 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
777
778 __ Bind(GetEntryLabel());
779
780 // Save the old reference.
781 // Note that we cannot use IP to save the old reference, as IP is
782 // used internally by the ReadBarrierMarkRegX entry point, and we
783 // need the old reference after the call to that entry point.
784 DCHECK_NE(temp1_, IP);
785 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000786
787 // No need to save live registers; it's taken care of by the
788 // entrypoint. Also, there is no need to update the stack mask,
789 // as this runtime call will not trigger a garbage collection.
790 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
791 DCHECK_NE(ref_reg, SP);
792 DCHECK_NE(ref_reg, LR);
793 DCHECK_NE(ref_reg, PC);
794 // IP is used internally by the ReadBarrierMarkRegX entry point
795 // as a temporary, it cannot be the entry point's input/output.
796 DCHECK_NE(ref_reg, IP);
797 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
798 // "Compact" slow path, saving two moves.
799 //
800 // Instead of using the standard runtime calling convention (input
801 // and output in R0):
802 //
803 // R0 <- ref
804 // R0 <- ReadBarrierMark(R0)
805 // ref <- R0
806 //
807 // we just use rX (the register containing `ref`) as input and output
808 // of a dedicated entrypoint:
809 //
810 // rX <- ReadBarrierMarkRegX(rX)
811 //
Roland Levillain35345a52017-02-27 14:32:08 +0000812 int32_t entry_point_offset =
813 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
814 // This runtime call does not require a stack map.
815 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100816
817 // If the new reference is different from the old reference,
Roland Levillain47b3ab22017-02-27 14:31:35 +0000818 // update the field in the holder (`*(obj_ + field_offset_)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100819 //
820 // Note that this field could also hold a different object, if
821 // another thread had concurrently changed it. In that case, the
822 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
823 // (CAS) operation below would abort the CAS, leaving the field
824 // as-is.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000825 Label done;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100826 __ cmp(temp1_, ShifterOperand(ref_reg));
Roland Levillain47b3ab22017-02-27 14:31:35 +0000827 __ b(&done, EQ);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100828
829 // Update the the holder's field atomically. This may fail if
830 // mutator updates before us, but it's OK. This is achieved
831 // using a strong compare-and-set (CAS) operation with relaxed
832 // memory synchronization ordering, where the expected value is
833 // the old reference and the desired value is the new reference.
834
835 // Convenience aliases.
836 Register base = obj_;
837 // The UnsafeCASObject intrinsic uses a register pair as field
838 // offset ("long offset"), of which only the low part contains
839 // data.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000840 Register offset = field_offset_.AsRegisterPairLow<Register>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100841 Register expected = temp1_;
842 Register value = ref_reg;
843 Register tmp_ptr = IP; // Pointer to actual memory.
844 Register tmp = temp2_; // Value in memory.
845
846 __ add(tmp_ptr, base, ShifterOperand(offset));
847
848 if (kPoisonHeapReferences) {
849 __ PoisonHeapReference(expected);
850 if (value == expected) {
851 // Do not poison `value`, as it is the same register as
852 // `expected`, which has just been poisoned.
853 } else {
854 __ PoisonHeapReference(value);
855 }
856 }
857
858 // do {
859 // tmp = [r_ptr] - expected;
860 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
861
Roland Levillain24a4d112016-10-26 13:10:46 +0100862 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100863 __ Bind(&loop_head);
864
865 __ ldrex(tmp, tmp_ptr);
866
867 __ subs(tmp, tmp, ShifterOperand(expected));
868
Roland Levillain24a4d112016-10-26 13:10:46 +0100869 __ it(NE);
870 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100871
Roland Levillain24a4d112016-10-26 13:10:46 +0100872 __ b(&exit_loop, NE);
873
874 __ strex(tmp, value, tmp_ptr);
875 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100876 __ b(&loop_head, EQ);
877
Roland Levillain24a4d112016-10-26 13:10:46 +0100878 __ Bind(&exit_loop);
879
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100880 if (kPoisonHeapReferences) {
881 __ UnpoisonHeapReference(expected);
882 if (value == expected) {
883 // Do not unpoison `value`, as it is the same register as
884 // `expected`, which has just been unpoisoned.
885 } else {
886 __ UnpoisonHeapReference(value);
887 }
888 }
889
Roland Levillain47b3ab22017-02-27 14:31:35 +0000890 __ Bind(&done);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100891 __ b(GetExitLabel());
892 }
893
894 private:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000895 // The location (register) of the marked object reference.
896 const Location ref_;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100897 // The register containing the object holding the marked object reference field.
898 const Register obj_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000899 // The location of the offset of the marked reference field within `obj_`.
900 Location field_offset_;
901
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100902 const Register temp1_;
903 const Register temp2_;
904
Roland Levillain47b3ab22017-02-27 14:31:35 +0000905 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100906};
907
Roland Levillain3b359c72015-11-17 19:35:12 +0000908// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100909class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000910 public:
911 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
912 Location out,
913 Location ref,
914 Location obj,
915 uint32_t offset,
916 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100917 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000918 out_(out),
919 ref_(ref),
920 obj_(obj),
921 offset_(offset),
922 index_(index) {
923 DCHECK(kEmitCompilerReadBarrier);
924 // If `obj` is equal to `out` or `ref`, it means the initial object
925 // has been overwritten by (or after) the heap object reference load
926 // to be instrumented, e.g.:
927 //
928 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000929 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000930 //
931 // In that case, we have lost the information about the original
932 // object, and the emitted read barrier cannot work properly.
933 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
934 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
935 }
936
937 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
938 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
939 LocationSummary* locations = instruction_->GetLocations();
940 Register reg_out = out_.AsRegister<Register>();
941 DCHECK(locations->CanCall());
942 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100943 DCHECK(instruction_->IsInstanceFieldGet() ||
944 instruction_->IsStaticFieldGet() ||
945 instruction_->IsArrayGet() ||
946 instruction_->IsInstanceOf() ||
947 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100948 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000949 << "Unexpected instruction in read barrier for heap reference slow path: "
950 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000951 // The read barrier instrumentation of object ArrayGet
952 // instructions does not support the HIntermediateAddress
953 // instruction.
954 DCHECK(!(instruction_->IsArrayGet() &&
955 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +0000956
957 __ Bind(GetEntryLabel());
958 SaveLiveRegisters(codegen, locations);
959
960 // We may have to change the index's value, but as `index_` is a
961 // constant member (like other "inputs" of this slow path),
962 // introduce a copy of it, `index`.
963 Location index = index_;
964 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100965 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000966 if (instruction_->IsArrayGet()) {
967 // Compute the actual memory offset and store it in `index`.
968 Register index_reg = index_.AsRegister<Register>();
969 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
970 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
971 // We are about to change the value of `index_reg` (see the
972 // calls to art::arm::Thumb2Assembler::Lsl and
973 // art::arm::Thumb2Assembler::AddConstant below), but it has
974 // not been saved by the previous call to
975 // art::SlowPathCode::SaveLiveRegisters, as it is a
976 // callee-save register --
977 // art::SlowPathCode::SaveLiveRegisters does not consider
978 // callee-save registers, as it has been designed with the
979 // assumption that callee-save registers are supposed to be
980 // handled by the called function. So, as a callee-save
981 // register, `index_reg` _would_ eventually be saved onto
982 // the stack, but it would be too late: we would have
983 // changed its value earlier. Therefore, we manually save
984 // it here into another freely available register,
985 // `free_reg`, chosen of course among the caller-save
986 // registers (as a callee-save `free_reg` register would
987 // exhibit the same problem).
988 //
989 // Note we could have requested a temporary register from
990 // the register allocator instead; but we prefer not to, as
991 // this is a slow path, and we know we can find a
992 // caller-save register that is available.
993 Register free_reg = FindAvailableCallerSaveRegister(codegen);
994 __ Mov(free_reg, index_reg);
995 index_reg = free_reg;
996 index = Location::RegisterLocation(index_reg);
997 } else {
998 // The initial register stored in `index_` has already been
999 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1000 // (as it is not a callee-save register), so we can freely
1001 // use it.
1002 }
1003 // Shifting the index value contained in `index_reg` by the scale
1004 // factor (2) cannot overflow in practice, as the runtime is
1005 // unable to allocate object arrays with a size larger than
1006 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1007 __ Lsl(index_reg, index_reg, TIMES_4);
1008 static_assert(
1009 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1010 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1011 __ AddConstant(index_reg, index_reg, offset_);
1012 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001013 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1014 // intrinsics, `index_` is not shifted by a scale factor of 2
1015 // (as in the case of ArrayGet), as it is actually an offset
1016 // to an object field within an object.
1017 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001018 DCHECK(instruction_->GetLocations()->Intrinsified());
1019 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1020 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1021 << instruction_->AsInvoke()->GetIntrinsic();
1022 DCHECK_EQ(offset_, 0U);
1023 DCHECK(index_.IsRegisterPair());
1024 // UnsafeGet's offset location is a register pair, the low
1025 // part contains the correct offset.
1026 index = index_.ToLow();
1027 }
1028 }
1029
1030 // We're moving two or three locations to locations that could
1031 // overlap, so we need a parallel move resolver.
1032 InvokeRuntimeCallingConvention calling_convention;
1033 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1034 parallel_move.AddMove(ref_,
1035 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1036 Primitive::kPrimNot,
1037 nullptr);
1038 parallel_move.AddMove(obj_,
1039 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1040 Primitive::kPrimNot,
1041 nullptr);
1042 if (index.IsValid()) {
1043 parallel_move.AddMove(index,
1044 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1045 Primitive::kPrimInt,
1046 nullptr);
1047 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1048 } else {
1049 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1050 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1051 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001052 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001053 CheckEntrypointTypes<
1054 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1055 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1056
1057 RestoreLiveRegisters(codegen, locations);
1058 __ b(GetExitLabel());
1059 }
1060
1061 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1062
1063 private:
1064 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1065 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1066 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1067 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1068 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1069 return static_cast<Register>(i);
1070 }
1071 }
1072 // We shall never fail to find a free caller-save register, as
1073 // there are more than two core caller-save registers on ARM
1074 // (meaning it is possible to find one which is different from
1075 // `ref` and `obj`).
1076 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1077 LOG(FATAL) << "Could not find a free caller-save register";
1078 UNREACHABLE();
1079 }
1080
Roland Levillain3b359c72015-11-17 19:35:12 +00001081 const Location out_;
1082 const Location ref_;
1083 const Location obj_;
1084 const uint32_t offset_;
1085 // An additional location containing an index to an array.
1086 // Only used for HArrayGet and the UnsafeGetObject &
1087 // UnsafeGetObjectVolatile intrinsics.
1088 const Location index_;
1089
1090 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1091};
1092
1093// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001094class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001095 public:
1096 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001097 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001098 DCHECK(kEmitCompilerReadBarrier);
1099 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001100
1101 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1102 LocationSummary* locations = instruction_->GetLocations();
1103 Register reg_out = out_.AsRegister<Register>();
1104 DCHECK(locations->CanCall());
1105 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001106 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1107 << "Unexpected instruction in read barrier for GC root slow path: "
1108 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001109
1110 __ Bind(GetEntryLabel());
1111 SaveLiveRegisters(codegen, locations);
1112
1113 InvokeRuntimeCallingConvention calling_convention;
1114 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1115 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001116 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001117 instruction_,
1118 instruction_->GetDexPc(),
1119 this);
1120 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1121 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1122
1123 RestoreLiveRegisters(codegen, locations);
1124 __ b(GetExitLabel());
1125 }
1126
1127 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1128
1129 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001130 const Location out_;
1131 const Location root_;
1132
1133 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1134};
1135
Aart Bike9f37602015-10-09 11:15:55 -07001136inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001137 switch (cond) {
1138 case kCondEQ: return EQ;
1139 case kCondNE: return NE;
1140 case kCondLT: return LT;
1141 case kCondLE: return LE;
1142 case kCondGT: return GT;
1143 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001144 case kCondB: return LO;
1145 case kCondBE: return LS;
1146 case kCondA: return HI;
1147 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001148 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001149 LOG(FATAL) << "Unreachable";
1150 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001151}
1152
Aart Bike9f37602015-10-09 11:15:55 -07001153// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001154inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001155 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001156 case kCondEQ: return EQ;
1157 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001158 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001159 case kCondLT: return LO;
1160 case kCondLE: return LS;
1161 case kCondGT: return HI;
1162 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001163 // Unsigned remain unchanged.
1164 case kCondB: return LO;
1165 case kCondBE: return LS;
1166 case kCondA: return HI;
1167 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001168 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001169 LOG(FATAL) << "Unreachable";
1170 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001171}
1172
Vladimir Markod6e069b2016-01-18 11:11:01 +00001173inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1174 // The ARM condition codes can express all the necessary branches, see the
1175 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1176 // There is no dex instruction or HIR that would need the missing conditions
1177 // "equal or unordered" or "not equal".
1178 switch (cond) {
1179 case kCondEQ: return EQ;
1180 case kCondNE: return NE /* unordered */;
1181 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1182 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1183 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1184 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1185 default:
1186 LOG(FATAL) << "UNREACHABLE";
1187 UNREACHABLE();
1188 }
1189}
1190
Anton Kirilov74234da2017-01-13 14:42:47 +00001191inline Shift ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1192 switch (op_kind) {
1193 case HDataProcWithShifterOp::kASR: return ASR;
1194 case HDataProcWithShifterOp::kLSL: return LSL;
1195 case HDataProcWithShifterOp::kLSR: return LSR;
1196 default:
1197 LOG(FATAL) << "Unexpected op kind " << op_kind;
1198 UNREACHABLE();
1199 }
1200}
1201
1202static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1203 Register out,
1204 Register first,
1205 const ShifterOperand& second,
1206 CodeGeneratorARM* codegen) {
1207 if (second.IsImmediate() && second.GetImmediate() == 0) {
1208 const ShifterOperand in = kind == HInstruction::kAnd
1209 ? ShifterOperand(0)
1210 : ShifterOperand(first);
1211
1212 __ mov(out, in);
1213 } else {
1214 switch (kind) {
1215 case HInstruction::kAdd:
1216 __ add(out, first, second);
1217 break;
1218 case HInstruction::kAnd:
1219 __ and_(out, first, second);
1220 break;
1221 case HInstruction::kOr:
1222 __ orr(out, first, second);
1223 break;
1224 case HInstruction::kSub:
1225 __ sub(out, first, second);
1226 break;
1227 case HInstruction::kXor:
1228 __ eor(out, first, second);
1229 break;
1230 default:
1231 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1232 UNREACHABLE();
1233 }
1234 }
1235}
1236
1237static void GenerateDataProc(HInstruction::InstructionKind kind,
1238 const Location& out,
1239 const Location& first,
1240 const ShifterOperand& second_lo,
1241 const ShifterOperand& second_hi,
1242 CodeGeneratorARM* codegen) {
1243 const Register first_hi = first.AsRegisterPairHigh<Register>();
1244 const Register first_lo = first.AsRegisterPairLow<Register>();
1245 const Register out_hi = out.AsRegisterPairHigh<Register>();
1246 const Register out_lo = out.AsRegisterPairLow<Register>();
1247
1248 if (kind == HInstruction::kAdd) {
1249 __ adds(out_lo, first_lo, second_lo);
1250 __ adc(out_hi, first_hi, second_hi);
1251 } else if (kind == HInstruction::kSub) {
1252 __ subs(out_lo, first_lo, second_lo);
1253 __ sbc(out_hi, first_hi, second_hi);
1254 } else {
1255 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1256 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1257 }
1258}
1259
1260static ShifterOperand GetShifterOperand(Register rm, Shift shift, uint32_t shift_imm) {
1261 return shift_imm == 0 ? ShifterOperand(rm) : ShifterOperand(rm, shift, shift_imm);
1262}
1263
1264static void GenerateLongDataProc(HDataProcWithShifterOp* instruction, CodeGeneratorARM* codegen) {
1265 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1266 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1267
1268 const LocationSummary* const locations = instruction->GetLocations();
1269 const uint32_t shift_value = instruction->GetShiftAmount();
1270 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1271 const Location first = locations->InAt(0);
1272 const Location second = locations->InAt(1);
1273 const Location out = locations->Out();
1274 const Register first_hi = first.AsRegisterPairHigh<Register>();
1275 const Register first_lo = first.AsRegisterPairLow<Register>();
1276 const Register out_hi = out.AsRegisterPairHigh<Register>();
1277 const Register out_lo = out.AsRegisterPairLow<Register>();
1278 const Register second_hi = second.AsRegisterPairHigh<Register>();
1279 const Register second_lo = second.AsRegisterPairLow<Register>();
1280 const Shift shift = ShiftFromOpKind(instruction->GetOpKind());
1281
1282 if (shift_value >= 32) {
1283 if (shift == LSL) {
1284 GenerateDataProcInstruction(kind,
1285 out_hi,
1286 first_hi,
1287 ShifterOperand(second_lo, LSL, shift_value - 32),
1288 codegen);
1289 GenerateDataProcInstruction(kind,
1290 out_lo,
1291 first_lo,
1292 ShifterOperand(0),
1293 codegen);
1294 } else if (shift == ASR) {
1295 GenerateDataProc(kind,
1296 out,
1297 first,
1298 GetShifterOperand(second_hi, ASR, shift_value - 32),
1299 ShifterOperand(second_hi, ASR, 31),
1300 codegen);
1301 } else {
1302 DCHECK_EQ(shift, LSR);
1303 GenerateDataProc(kind,
1304 out,
1305 first,
1306 GetShifterOperand(second_hi, LSR, shift_value - 32),
1307 ShifterOperand(0),
1308 codegen);
1309 }
1310 } else {
1311 DCHECK_GT(shift_value, 1U);
1312 DCHECK_LT(shift_value, 32U);
1313
1314 if (shift == LSL) {
1315 // We are not doing this for HInstruction::kAdd because the output will require
1316 // Location::kOutputOverlap; not applicable to other cases.
1317 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1318 GenerateDataProcInstruction(kind,
1319 out_hi,
1320 first_hi,
1321 ShifterOperand(second_hi, LSL, shift_value),
1322 codegen);
1323 GenerateDataProcInstruction(kind,
1324 out_hi,
1325 out_hi,
1326 ShifterOperand(second_lo, LSR, 32 - shift_value),
1327 codegen);
1328 GenerateDataProcInstruction(kind,
1329 out_lo,
1330 first_lo,
1331 ShifterOperand(second_lo, LSL, shift_value),
1332 codegen);
1333 } else {
1334 __ Lsl(IP, second_hi, shift_value);
1335 __ orr(IP, IP, ShifterOperand(second_lo, LSR, 32 - shift_value));
1336 GenerateDataProc(kind,
1337 out,
1338 first,
1339 ShifterOperand(second_lo, LSL, shift_value),
1340 ShifterOperand(IP),
1341 codegen);
1342 }
1343 } else {
1344 DCHECK(shift == ASR || shift == LSR);
1345
1346 // We are not doing this for HInstruction::kAdd because the output will require
1347 // Location::kOutputOverlap; not applicable to other cases.
1348 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1349 GenerateDataProcInstruction(kind,
1350 out_lo,
1351 first_lo,
1352 ShifterOperand(second_lo, LSR, shift_value),
1353 codegen);
1354 GenerateDataProcInstruction(kind,
1355 out_lo,
1356 out_lo,
1357 ShifterOperand(second_hi, LSL, 32 - shift_value),
1358 codegen);
1359 GenerateDataProcInstruction(kind,
1360 out_hi,
1361 first_hi,
1362 ShifterOperand(second_hi, shift, shift_value),
1363 codegen);
1364 } else {
1365 __ Lsr(IP, second_lo, shift_value);
1366 __ orr(IP, IP, ShifterOperand(second_hi, LSL, 32 - shift_value));
1367 GenerateDataProc(kind,
1368 out,
1369 first,
1370 ShifterOperand(IP),
1371 ShifterOperand(second_hi, shift, shift_value),
1372 codegen);
1373 }
1374 }
1375 }
1376}
1377
1378#undef __
1379// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1380#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
1381
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001382void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001383 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001384}
1385
1386void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001387 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001388}
1389
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001390size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1391 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1392 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001393}
1394
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001395size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1396 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1397 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001398}
1399
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001400size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1401 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1402 return kArmWordSize;
1403}
1404
1405size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1406 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1407 return kArmWordSize;
1408}
1409
Calin Juravle34166012014-12-19 17:22:29 +00001410CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001411 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001412 const CompilerOptions& compiler_options,
1413 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001414 : CodeGenerator(graph,
1415 kNumberOfCoreRegisters,
1416 kNumberOfSRegisters,
1417 kNumberOfRegisterPairs,
1418 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1419 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001420 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1421 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001422 compiler_options,
1423 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001424 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001425 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001426 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001427 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001428 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001429 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001430 uint32_literals_(std::less<uint32_t>(),
1431 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001432 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1433 boot_image_string_patches_(StringReferenceValueComparator(),
1434 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1435 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001436 boot_image_type_patches_(TypeReferenceValueComparator(),
1437 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1438 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001439 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001440 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001441 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1442 jit_class_patches_(TypeReferenceValueComparator(),
1443 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001444 // Always save the LR register to mimic Quick.
1445 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001446}
1447
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001448void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1449 // Ensure that we fix up branches and literal loads and emit the literal pool.
1450 __ FinalizeCode();
1451
1452 // Adjust native pc offsets in stack maps.
1453 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001454 uint32_t old_position =
1455 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001456 uint32_t new_position = __ GetAdjustedPosition(old_position);
1457 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1458 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001459 // Adjust pc offsets for the disassembly information.
1460 if (disasm_info_ != nullptr) {
1461 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1462 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1463 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1464 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1465 it.second.start = __ GetAdjustedPosition(it.second.start);
1466 it.second.end = __ GetAdjustedPosition(it.second.end);
1467 }
1468 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1469 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1470 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1471 }
1472 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001473
1474 CodeGenerator::Finalize(allocator);
1475}
1476
David Brazdil58282f42016-01-14 12:45:10 +00001477void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001478 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001479 blocked_core_registers_[SP] = true;
1480 blocked_core_registers_[LR] = true;
1481 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001482
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001483 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001484 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001485
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001486 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001487 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001488
David Brazdil58282f42016-01-14 12:45:10 +00001489 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001490 // Stubs do not save callee-save floating point registers. If the graph
1491 // is debuggable, we need to deal with these registers differently. For
1492 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001493 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1494 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1495 }
1496 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001497}
1498
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001499InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001500 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001501 assembler_(codegen->GetAssembler()),
1502 codegen_(codegen) {}
1503
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001504void CodeGeneratorARM::ComputeSpillMask() {
1505 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1506 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001507 // There is no easy instruction to restore just the PC on thumb2. We spill and
1508 // restore another arbitrary register.
1509 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001510 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1511 // We use vpush and vpop for saving and restoring floating point registers, which take
1512 // a SRegister and the number of registers to save/restore after that SRegister. We
1513 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1514 // but in the range.
1515 if (fpu_spill_mask_ != 0) {
1516 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1517 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1518 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1519 fpu_spill_mask_ |= (1 << i);
1520 }
1521 }
1522}
1523
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001524static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001525 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001526}
1527
1528static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001529 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001530}
1531
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001532void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001533 bool skip_overflow_check =
1534 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001535 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001536 __ Bind(&frame_entry_label_);
1537
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001538 if (HasEmptyFrame()) {
1539 return;
1540 }
1541
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001542 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001543 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1544 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1545 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001546 }
1547
Andreas Gampe501fd632015-09-10 16:11:06 -07001548 __ PushList(core_spill_mask_);
1549 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1550 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001551 if (fpu_spill_mask_ != 0) {
1552 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1553 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001554 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001555 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001556 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001557
1558 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1559 // Initialize should_deoptimize flag to 0.
1560 __ mov(IP, ShifterOperand(0));
1561 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
1562 }
1563
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001564 int adjust = GetFrameSize() - FrameEntrySpillSize();
1565 __ AddConstant(SP, -adjust);
1566 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001567
1568 // Save the current method if we need it. Note that we do not
1569 // do this in HCurrentMethod, as the instruction might have been removed
1570 // in the SSA graph.
1571 if (RequiresCurrentMethod()) {
1572 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1573 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001574}
1575
1576void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001577 if (HasEmptyFrame()) {
1578 __ bx(LR);
1579 return;
1580 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001581 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001582 int adjust = GetFrameSize() - FrameEntrySpillSize();
1583 __ AddConstant(SP, adjust);
1584 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001585 if (fpu_spill_mask_ != 0) {
1586 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1587 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001588 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001589 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001590 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001591 // Pop LR into PC to return.
1592 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1593 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1594 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001595 __ cfi().RestoreState();
1596 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001597}
1598
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001599void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001600 Label* label = GetLabelOf(block);
1601 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001602}
1603
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001604Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001605 switch (type) {
1606 case Primitive::kPrimBoolean:
1607 case Primitive::kPrimByte:
1608 case Primitive::kPrimChar:
1609 case Primitive::kPrimShort:
1610 case Primitive::kPrimInt:
1611 case Primitive::kPrimNot: {
1612 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001613 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001614 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001615 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001616 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001617 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001618 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001619 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001620
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001621 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001622 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001623 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001624 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001625 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001626 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001627 if (calling_convention.GetRegisterAt(index) == R1) {
1628 // Skip R1, and use R2_R3 instead.
1629 gp_index_++;
1630 index++;
1631 }
1632 }
1633 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1634 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001635 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001636
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001637 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001638 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001639 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001640 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1641 }
1642 }
1643
1644 case Primitive::kPrimFloat: {
1645 uint32_t stack_index = stack_index_++;
1646 if (float_index_ % 2 == 0) {
1647 float_index_ = std::max(double_index_, float_index_);
1648 }
1649 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1650 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1651 } else {
1652 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1653 }
1654 }
1655
1656 case Primitive::kPrimDouble: {
1657 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1658 uint32_t stack_index = stack_index_;
1659 stack_index_ += 2;
1660 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1661 uint32_t index = double_index_;
1662 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001663 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001664 calling_convention.GetFpuRegisterAt(index),
1665 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001666 DCHECK(ExpectedPairLayout(result));
1667 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001668 } else {
1669 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001670 }
1671 }
1672
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001673 case Primitive::kPrimVoid:
1674 LOG(FATAL) << "Unexpected parameter type " << type;
1675 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001676 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001677 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001678}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001679
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001680Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001681 switch (type) {
1682 case Primitive::kPrimBoolean:
1683 case Primitive::kPrimByte:
1684 case Primitive::kPrimChar:
1685 case Primitive::kPrimShort:
1686 case Primitive::kPrimInt:
1687 case Primitive::kPrimNot: {
1688 return Location::RegisterLocation(R0);
1689 }
1690
1691 case Primitive::kPrimFloat: {
1692 return Location::FpuRegisterLocation(S0);
1693 }
1694
1695 case Primitive::kPrimLong: {
1696 return Location::RegisterPairLocation(R0, R1);
1697 }
1698
1699 case Primitive::kPrimDouble: {
1700 return Location::FpuRegisterPairLocation(S0, S1);
1701 }
1702
1703 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001704 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001705 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001706
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001707 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001708}
1709
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001710Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1711 return Location::RegisterLocation(kMethodRegisterArgument);
1712}
1713
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001714void CodeGeneratorARM::Move32(Location destination, Location source) {
1715 if (source.Equals(destination)) {
1716 return;
1717 }
1718 if (destination.IsRegister()) {
1719 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001720 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001721 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001722 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001723 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001724 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001725 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001726 } else if (destination.IsFpuRegister()) {
1727 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001728 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001729 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001730 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001731 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001732 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001733 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001734 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001735 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001736 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001737 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001738 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001739 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001740 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001741 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001742 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1743 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001744 }
1745 }
1746}
1747
1748void CodeGeneratorARM::Move64(Location destination, Location source) {
1749 if (source.Equals(destination)) {
1750 return;
1751 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001752 if (destination.IsRegisterPair()) {
1753 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001754 EmitParallelMoves(
1755 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1756 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001757 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001758 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001759 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1760 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001761 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001762 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001763 } else if (source.IsFpuRegisterPair()) {
1764 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1765 destination.AsRegisterPairHigh<Register>(),
1766 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001767 } else {
1768 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001769 DCHECK(ExpectedPairLayout(destination));
1770 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1771 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001772 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001773 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001774 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001775 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1776 SP,
1777 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001778 } else if (source.IsRegisterPair()) {
1779 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1780 source.AsRegisterPairLow<Register>(),
1781 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001782 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001783 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001784 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001785 } else {
1786 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001787 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001788 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001789 if (source.AsRegisterPairLow<Register>() == R1) {
1790 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001791 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1792 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001793 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001794 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001795 SP, destination.GetStackIndex());
1796 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001797 } else if (source.IsFpuRegisterPair()) {
1798 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1799 SP,
1800 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001801 } else {
1802 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001803 EmitParallelMoves(
1804 Location::StackSlot(source.GetStackIndex()),
1805 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001806 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001807 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001808 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1809 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001810 }
1811 }
1812}
1813
Calin Juravle175dc732015-08-25 15:42:32 +01001814void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1815 DCHECK(location.IsRegister());
1816 __ LoadImmediate(location.AsRegister<Register>(), value);
1817}
1818
Calin Juravlee460d1d2015-09-29 04:52:17 +01001819void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001820 HParallelMove move(GetGraph()->GetArena());
1821 move.AddMove(src, dst, dst_type, nullptr);
1822 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001823}
1824
1825void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1826 if (location.IsRegister()) {
1827 locations->AddTemp(location);
1828 } else if (location.IsRegisterPair()) {
1829 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1830 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1831 } else {
1832 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1833 }
1834}
1835
Calin Juravle175dc732015-08-25 15:42:32 +01001836void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1837 HInstruction* instruction,
1838 uint32_t dex_pc,
1839 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001840 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001841 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001842 if (EntrypointRequiresStackMap(entrypoint)) {
1843 RecordPcInfo(instruction, dex_pc, slow_path);
1844 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001845}
1846
Roland Levillaindec8f632016-07-22 17:10:06 +01001847void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1848 HInstruction* instruction,
1849 SlowPathCode* slow_path) {
1850 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001851 GenerateInvokeRuntime(entry_point_offset);
1852}
1853
1854void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001855 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1856 __ blx(LR);
1857}
1858
David Brazdilfc6a86a2015-06-26 10:33:45 +00001859void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001860 DCHECK(!successor->IsExitBlock());
1861
1862 HBasicBlock* block = got->GetBlock();
1863 HInstruction* previous = got->GetPrevious();
1864
1865 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001866 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001867 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1868 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1869 return;
1870 }
1871
1872 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1873 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1874 }
1875 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001876 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001877 }
1878}
1879
David Brazdilfc6a86a2015-06-26 10:33:45 +00001880void LocationsBuilderARM::VisitGoto(HGoto* got) {
1881 got->SetLocations(nullptr);
1882}
1883
1884void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1885 HandleGoto(got, got->GetSuccessor());
1886}
1887
1888void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1889 try_boundary->SetLocations(nullptr);
1890}
1891
1892void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1893 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1894 if (!successor->IsExitBlock()) {
1895 HandleGoto(try_boundary, successor);
1896 }
1897}
1898
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001899void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001900 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001901}
1902
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001903void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001904}
1905
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001906void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1907 Primitive::Type type = instruction->InputAt(0)->GetType();
1908 Location lhs_loc = instruction->GetLocations()->InAt(0);
1909 Location rhs_loc = instruction->GetLocations()->InAt(1);
1910 if (rhs_loc.IsConstant()) {
1911 // 0.0 is the only immediate that can be encoded directly in
1912 // a VCMP instruction.
1913 //
1914 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1915 // specify that in a floating-point comparison, positive zero
1916 // and negative zero are considered equal, so we can use the
1917 // literal 0.0 for both cases here.
1918 //
1919 // Note however that some methods (Float.equal, Float.compare,
1920 // Float.compareTo, Double.equal, Double.compare,
1921 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1922 // StrictMath.min) consider 0.0 to be (strictly) greater than
1923 // -0.0. So if we ever translate calls to these methods into a
1924 // HCompare instruction, we must handle the -0.0 case with
1925 // care here.
1926 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1927 if (type == Primitive::kPrimFloat) {
1928 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1929 } else {
1930 DCHECK_EQ(type, Primitive::kPrimDouble);
1931 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1932 }
1933 } else {
1934 if (type == Primitive::kPrimFloat) {
1935 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1936 } else {
1937 DCHECK_EQ(type, Primitive::kPrimDouble);
1938 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1939 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1940 }
1941 }
1942}
1943
Roland Levillain4fa13f62015-07-06 18:11:54 +01001944void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1945 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001946 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001947 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001948 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001949}
1950
1951void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1952 Label* true_label,
1953 Label* false_label) {
1954 LocationSummary* locations = cond->GetLocations();
1955 Location left = locations->InAt(0);
1956 Location right = locations->InAt(1);
1957 IfCondition if_cond = cond->GetCondition();
1958
1959 Register left_high = left.AsRegisterPairHigh<Register>();
1960 Register left_low = left.AsRegisterPairLow<Register>();
1961 IfCondition true_high_cond = if_cond;
1962 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001963 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001964
1965 // Set the conditions for the test, remembering that == needs to be
1966 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001967 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001968 switch (if_cond) {
1969 case kCondEQ:
1970 case kCondNE:
1971 // Nothing to do.
1972 break;
1973 case kCondLT:
1974 false_high_cond = kCondGT;
1975 break;
1976 case kCondLE:
1977 true_high_cond = kCondLT;
1978 break;
1979 case kCondGT:
1980 false_high_cond = kCondLT;
1981 break;
1982 case kCondGE:
1983 true_high_cond = kCondGT;
1984 break;
Aart Bike9f37602015-10-09 11:15:55 -07001985 case kCondB:
1986 false_high_cond = kCondA;
1987 break;
1988 case kCondBE:
1989 true_high_cond = kCondB;
1990 break;
1991 case kCondA:
1992 false_high_cond = kCondB;
1993 break;
1994 case kCondAE:
1995 true_high_cond = kCondA;
1996 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001997 }
1998 if (right.IsConstant()) {
1999 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
2000 int32_t val_low = Low32Bits(value);
2001 int32_t val_high = High32Bits(value);
2002
Vladimir Markoac6ac102015-12-17 12:14:00 +00002003 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002004 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002005 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002006 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002007 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002008 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002009 __ b(true_label, ARMCondition(true_high_cond));
2010 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002011 }
2012 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00002013 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002014 } else {
2015 Register right_high = right.AsRegisterPairHigh<Register>();
2016 Register right_low = right.AsRegisterPairLow<Register>();
2017
2018 __ cmp(left_high, ShifterOperand(right_high));
2019 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002020 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002021 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002022 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002023 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002024 __ b(true_label, ARMCondition(true_high_cond));
2025 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002026 }
2027 // Must be equal high, so compare the lows.
2028 __ cmp(left_low, ShifterOperand(right_low));
2029 }
2030 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07002031 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01002032 __ b(true_label, final_condition);
2033}
2034
David Brazdil0debae72015-11-12 18:37:00 +00002035void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
2036 Label* true_target_in,
2037 Label* false_target_in) {
2038 // Generated branching requires both targets to be explicit. If either of the
2039 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2040 Label fallthrough_target;
2041 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
2042 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
2043
Roland Levillain4fa13f62015-07-06 18:11:54 +01002044 Primitive::Type type = condition->InputAt(0)->GetType();
2045 switch (type) {
2046 case Primitive::kPrimLong:
2047 GenerateLongComparesAndJumps(condition, true_target, false_target);
2048 break;
2049 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002050 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002051 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002052 GenerateFPJumps(condition, true_target, false_target);
2053 break;
2054 default:
2055 LOG(FATAL) << "Unexpected compare type " << type;
2056 }
2057
David Brazdil0debae72015-11-12 18:37:00 +00002058 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002059 __ b(false_target);
2060 }
David Brazdil0debae72015-11-12 18:37:00 +00002061
2062 if (fallthrough_target.IsLinked()) {
2063 __ Bind(&fallthrough_target);
2064 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002065}
2066
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002067void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002068 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002069 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002070 Label* false_target) {
2071 HInstruction* cond = instruction->InputAt(condition_input_index);
2072
2073 if (true_target == nullptr && false_target == nullptr) {
2074 // Nothing to do. The code always falls through.
2075 return;
2076 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002077 // Constant condition, statically compared against "true" (integer value 1).
2078 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002079 if (true_target != nullptr) {
2080 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002081 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002082 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002083 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002084 if (false_target != nullptr) {
2085 __ b(false_target);
2086 }
2087 }
2088 return;
2089 }
2090
2091 // The following code generates these patterns:
2092 // (1) true_target == nullptr && false_target != nullptr
2093 // - opposite condition true => branch to false_target
2094 // (2) true_target != nullptr && false_target == nullptr
2095 // - condition true => branch to true_target
2096 // (3) true_target != nullptr && false_target != nullptr
2097 // - condition true => branch to true_target
2098 // - branch to false_target
2099 if (IsBooleanValueOrMaterializedCondition(cond)) {
2100 // Condition has been materialized, compare the output to 0.
2101 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2102 DCHECK(cond_val.IsRegister());
2103 if (true_target == nullptr) {
2104 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
2105 } else {
2106 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002107 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002108 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002109 // Condition has not been materialized. Use its inputs as the comparison and
2110 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002111 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002112
2113 // If this is a long or FP comparison that has been folded into
2114 // the HCondition, generate the comparison directly.
2115 Primitive::Type type = condition->InputAt(0)->GetType();
2116 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2117 GenerateCompareTestAndBranch(condition, true_target, false_target);
2118 return;
2119 }
2120
2121 LocationSummary* locations = cond->GetLocations();
2122 DCHECK(locations->InAt(0).IsRegister());
2123 Register left = locations->InAt(0).AsRegister<Register>();
2124 Location right = locations->InAt(1);
2125 if (right.IsRegister()) {
2126 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002127 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002128 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002129 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00002130 }
2131 if (true_target == nullptr) {
2132 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
2133 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04002134 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002135 }
Dave Allison20dfc792014-06-16 20:44:29 -07002136 }
David Brazdil0debae72015-11-12 18:37:00 +00002137
2138 // If neither branch falls through (case 3), the conditional branch to `true_target`
2139 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2140 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002141 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002142 }
2143}
2144
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002145void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002146 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2147 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002148 locations->SetInAt(0, Location::RequiresRegister());
2149 }
2150}
2151
2152void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002153 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2154 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2155 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2156 nullptr : codegen_->GetLabelOf(true_successor);
2157 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2158 nullptr : codegen_->GetLabelOf(false_successor);
2159 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002160}
2161
2162void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
2163 LocationSummary* locations = new (GetGraph()->GetArena())
2164 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002165 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002166 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002167 locations->SetInAt(0, Location::RequiresRegister());
2168 }
2169}
2170
2171void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01002172 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002173 GenerateTestAndBranch(deoptimize,
2174 /* condition_input_index */ 0,
2175 slow_path->GetEntryLabel(),
2176 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002177}
Dave Allison20dfc792014-06-16 20:44:29 -07002178
Mingyao Yang063fc772016-08-02 11:02:54 -07002179void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2180 LocationSummary* locations = new (GetGraph()->GetArena())
2181 LocationSummary(flag, LocationSummary::kNoCall);
2182 locations->SetOut(Location::RequiresRegister());
2183}
2184
2185void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2186 __ LoadFromOffset(kLoadWord,
2187 flag->GetLocations()->Out().AsRegister<Register>(),
2188 SP,
2189 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2190}
2191
David Brazdil74eb1b22015-12-14 11:44:01 +00002192void LocationsBuilderARM::VisitSelect(HSelect* select) {
2193 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2194 if (Primitive::IsFloatingPointType(select->GetType())) {
2195 locations->SetInAt(0, Location::RequiresFpuRegister());
2196 locations->SetInAt(1, Location::RequiresFpuRegister());
2197 } else {
2198 locations->SetInAt(0, Location::RequiresRegister());
2199 locations->SetInAt(1, Location::RequiresRegister());
2200 }
2201 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2202 locations->SetInAt(2, Location::RequiresRegister());
2203 }
2204 locations->SetOut(Location::SameAsFirstInput());
2205}
2206
2207void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
2208 LocationSummary* locations = select->GetLocations();
2209 Label false_target;
2210 GenerateTestAndBranch(select,
2211 /* condition_input_index */ 2,
2212 /* true_target */ nullptr,
2213 &false_target);
2214 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2215 __ Bind(&false_target);
2216}
2217
David Srbecky0cf44932015-12-09 14:09:59 +00002218void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2219 new (GetGraph()->GetArena()) LocationSummary(info);
2220}
2221
David Srbeckyd28f4a02016-03-14 17:14:24 +00002222void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2223 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002224}
2225
2226void CodeGeneratorARM::GenerateNop() {
2227 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002228}
2229
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002230void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002231 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002232 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002233 // Handle the long/FP comparisons made in instruction simplification.
2234 switch (cond->InputAt(0)->GetType()) {
2235 case Primitive::kPrimLong:
2236 locations->SetInAt(0, Location::RequiresRegister());
2237 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002238 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002239 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2240 }
2241 break;
2242
2243 case Primitive::kPrimFloat:
2244 case Primitive::kPrimDouble:
2245 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002246 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002247 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002248 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2249 }
2250 break;
2251
2252 default:
2253 locations->SetInAt(0, Location::RequiresRegister());
2254 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002255 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002256 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2257 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002258 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002259}
2260
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002261void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002262 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002263 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002264 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002265
2266 LocationSummary* locations = cond->GetLocations();
2267 Location left = locations->InAt(0);
2268 Location right = locations->InAt(1);
2269 Register out = locations->Out().AsRegister<Register>();
2270 Label true_label, false_label;
2271
2272 switch (cond->InputAt(0)->GetType()) {
2273 default: {
2274 // Integer case.
2275 if (right.IsRegister()) {
2276 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2277 } else {
2278 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002279 __ CmpConstant(left.AsRegister<Register>(),
2280 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002281 }
Aart Bike9f37602015-10-09 11:15:55 -07002282 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002283 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002284 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002285 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002286 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002287 return;
2288 }
2289 case Primitive::kPrimLong:
2290 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2291 break;
2292 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002293 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002294 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002295 GenerateFPJumps(cond, &true_label, &false_label);
2296 break;
2297 }
2298
2299 // Convert the jumps into the result.
2300 Label done_label;
2301
2302 // False case: result = 0.
2303 __ Bind(&false_label);
2304 __ LoadImmediate(out, 0);
2305 __ b(&done_label);
2306
2307 // True case: result = 1.
2308 __ Bind(&true_label);
2309 __ LoadImmediate(out, 1);
2310 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002311}
2312
2313void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002314 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002315}
2316
2317void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002318 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002319}
2320
2321void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002322 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002323}
2324
2325void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002326 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002327}
2328
2329void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002330 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002331}
2332
2333void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002334 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002335}
2336
2337void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002338 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002339}
2340
2341void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002342 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002343}
2344
2345void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002346 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002347}
2348
2349void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002350 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002351}
2352
2353void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002354 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002355}
2356
2357void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002358 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002359}
2360
Aart Bike9f37602015-10-09 11:15:55 -07002361void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002362 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002363}
2364
2365void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002366 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002367}
2368
2369void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002370 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002371}
2372
2373void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002374 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002375}
2376
2377void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002378 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002379}
2380
2381void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002382 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002383}
2384
2385void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002386 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002387}
2388
2389void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002390 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002391}
2392
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002393void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002394 LocationSummary* locations =
2395 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002396 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002397}
2398
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002399void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002400 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002401}
2402
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002403void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2404 LocationSummary* locations =
2405 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2406 locations->SetOut(Location::ConstantLocation(constant));
2407}
2408
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002409void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002410 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002411}
2412
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002413void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002414 LocationSummary* locations =
2415 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002416 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002417}
2418
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002419void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002420 // Will be generated at use site.
2421}
2422
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002423void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2424 LocationSummary* locations =
2425 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2426 locations->SetOut(Location::ConstantLocation(constant));
2427}
2428
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002429void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002430 // Will be generated at use site.
2431}
2432
2433void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2434 LocationSummary* locations =
2435 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2436 locations->SetOut(Location::ConstantLocation(constant));
2437}
2438
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002439void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002440 // Will be generated at use site.
2441}
2442
Calin Juravle27df7582015-04-17 19:12:31 +01002443void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2444 memory_barrier->SetLocations(nullptr);
2445}
2446
2447void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002448 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002449}
2450
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002451void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002452 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002453}
2454
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002455void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002456 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002457}
2458
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002459void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002460 LocationSummary* locations =
2461 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002462 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002463}
2464
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002465void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002466 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002467}
2468
Calin Juravle175dc732015-08-25 15:42:32 +01002469void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2470 // The trampoline uses the same calling convention as dex calling conventions,
2471 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2472 // the method_idx.
2473 HandleInvoke(invoke);
2474}
2475
2476void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2477 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2478}
2479
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002480void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002481 // Explicit clinit checks triggered by static invokes must have been pruned by
2482 // art::PrepareForRegisterAllocation.
2483 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002484
Vladimir Marko68c981f2016-08-26 13:13:33 +01002485 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002486 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002487 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2488 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2489 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002490 return;
2491 }
2492
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002493 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002494
2495 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2496 if (invoke->HasPcRelativeDexCache()) {
2497 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2498 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002499}
2500
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002501static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2502 if (invoke->GetLocations()->Intrinsified()) {
2503 IntrinsicCodeGeneratorARM intrinsic(codegen);
2504 intrinsic.Dispatch(invoke);
2505 return true;
2506 }
2507 return false;
2508}
2509
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002510void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002511 // Explicit clinit checks triggered by static invokes must have been pruned by
2512 // art::PrepareForRegisterAllocation.
2513 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002514
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002515 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2516 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002517 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002518
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002519 LocationSummary* locations = invoke->GetLocations();
2520 codegen_->GenerateStaticOrDirectCall(
2521 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002522 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002523}
2524
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002525void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002526 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002527 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002528}
2529
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002530void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002531 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002532 if (intrinsic.TryDispatch(invoke)) {
2533 return;
2534 }
2535
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002536 HandleInvoke(invoke);
2537}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002538
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002539void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002540 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2541 return;
2542 }
2543
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002544 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002545 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002546 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002547}
2548
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002549void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2550 HandleInvoke(invoke);
2551 // Add the hidden argument.
2552 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2553}
2554
2555void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2556 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002557 LocationSummary* locations = invoke->GetLocations();
2558 Register temp = locations->GetTemp(0).AsRegister<Register>();
2559 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002560 Location receiver = locations->InAt(0);
2561 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2562
Roland Levillain3b359c72015-11-17 19:35:12 +00002563 // Set the hidden argument. This is safe to do this here, as R12
2564 // won't be modified thereafter, before the `blx` (call) instruction.
2565 DCHECK_EQ(R12, hidden_reg);
2566 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002567
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002568 if (receiver.IsStackSlot()) {
2569 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002570 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002571 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2572 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002573 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002574 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002575 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002576 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002577 // Instead of simply (possibly) unpoisoning `temp` here, we should
2578 // emit a read barrier for the previous class reference load.
2579 // However this is not required in practice, as this is an
2580 // intermediate/temporary reference and because the current
2581 // concurrent copying collector keeps the from-space memory
2582 // intact/accessible until the end of the marking phase (the
2583 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002584 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002585 __ LoadFromOffset(kLoadWord, temp, temp,
2586 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2587 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002588 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002589 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002590 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002591 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002592 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002593 // LR = temp->GetEntryPoint();
2594 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2595 // LR();
2596 __ blx(LR);
2597 DCHECK(!codegen_->IsLeafMethod());
2598 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2599}
2600
Orion Hodsonac141392017-01-13 11:53:47 +00002601void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2602 HandleInvoke(invoke);
2603}
2604
2605void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2606 codegen_->GenerateInvokePolymorphicCall(invoke);
2607}
2608
Roland Levillain88cb1752014-10-20 16:36:47 +01002609void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2610 LocationSummary* locations =
2611 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2612 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002613 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002614 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002615 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2616 break;
2617 }
2618 case Primitive::kPrimLong: {
2619 locations->SetInAt(0, Location::RequiresRegister());
2620 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002621 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002622 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002623
Roland Levillain88cb1752014-10-20 16:36:47 +01002624 case Primitive::kPrimFloat:
2625 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002626 locations->SetInAt(0, Location::RequiresFpuRegister());
2627 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002628 break;
2629
2630 default:
2631 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2632 }
2633}
2634
2635void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2636 LocationSummary* locations = neg->GetLocations();
2637 Location out = locations->Out();
2638 Location in = locations->InAt(0);
2639 switch (neg->GetResultType()) {
2640 case Primitive::kPrimInt:
2641 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002643 break;
2644
2645 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002646 DCHECK(in.IsRegisterPair());
2647 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2648 __ rsbs(out.AsRegisterPairLow<Register>(),
2649 in.AsRegisterPairLow<Register>(),
2650 ShifterOperand(0));
2651 // We cannot emit an RSC (Reverse Subtract with Carry)
2652 // instruction here, as it does not exist in the Thumb-2
2653 // instruction set. We use the following approach
2654 // using SBC and SUB instead.
2655 //
2656 // out.hi = -C
2657 __ sbc(out.AsRegisterPairHigh<Register>(),
2658 out.AsRegisterPairHigh<Register>(),
2659 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2660 // out.hi = out.hi - in.hi
2661 __ sub(out.AsRegisterPairHigh<Register>(),
2662 out.AsRegisterPairHigh<Register>(),
2663 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2664 break;
2665
Roland Levillain88cb1752014-10-20 16:36:47 +01002666 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002667 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002668 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002669 break;
2670
Roland Levillain88cb1752014-10-20 16:36:47 +01002671 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002672 DCHECK(in.IsFpuRegisterPair());
2673 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2674 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002675 break;
2676
2677 default:
2678 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2679 }
2680}
2681
Roland Levillaindff1f282014-11-05 14:15:05 +00002682void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002683 Primitive::Type result_type = conversion->GetResultType();
2684 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002685 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002686
Roland Levillain5b3ee562015-04-14 16:02:41 +01002687 // The float-to-long, double-to-long and long-to-float type conversions
2688 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002689 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002690 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2691 && result_type == Primitive::kPrimLong)
2692 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002693 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002694 : LocationSummary::kNoCall;
2695 LocationSummary* locations =
2696 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2697
David Brazdilb2bd1c52015-03-25 11:17:37 +00002698 // The Java language does not allow treating boolean as an integral type but
2699 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002700
Roland Levillaindff1f282014-11-05 14:15:05 +00002701 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002702 case Primitive::kPrimByte:
2703 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002704 case Primitive::kPrimLong:
2705 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002706 case Primitive::kPrimBoolean:
2707 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002708 case Primitive::kPrimShort:
2709 case Primitive::kPrimInt:
2710 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002711 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002712 locations->SetInAt(0, Location::RequiresRegister());
2713 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2714 break;
2715
2716 default:
2717 LOG(FATAL) << "Unexpected type conversion from " << input_type
2718 << " to " << result_type;
2719 }
2720 break;
2721
Roland Levillain01a8d712014-11-14 16:27:39 +00002722 case Primitive::kPrimShort:
2723 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002724 case Primitive::kPrimLong:
2725 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002726 case Primitive::kPrimBoolean:
2727 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002728 case Primitive::kPrimByte:
2729 case Primitive::kPrimInt:
2730 case Primitive::kPrimChar:
2731 // Processing a Dex `int-to-short' instruction.
2732 locations->SetInAt(0, Location::RequiresRegister());
2733 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2734 break;
2735
2736 default:
2737 LOG(FATAL) << "Unexpected type conversion from " << input_type
2738 << " to " << result_type;
2739 }
2740 break;
2741
Roland Levillain946e1432014-11-11 17:35:19 +00002742 case Primitive::kPrimInt:
2743 switch (input_type) {
2744 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002745 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002746 locations->SetInAt(0, Location::Any());
2747 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2748 break;
2749
2750 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002751 // Processing a Dex `float-to-int' instruction.
2752 locations->SetInAt(0, Location::RequiresFpuRegister());
2753 locations->SetOut(Location::RequiresRegister());
2754 locations->AddTemp(Location::RequiresFpuRegister());
2755 break;
2756
Roland Levillain946e1432014-11-11 17:35:19 +00002757 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002758 // Processing a Dex `double-to-int' instruction.
2759 locations->SetInAt(0, Location::RequiresFpuRegister());
2760 locations->SetOut(Location::RequiresRegister());
2761 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002762 break;
2763
2764 default:
2765 LOG(FATAL) << "Unexpected type conversion from " << input_type
2766 << " to " << result_type;
2767 }
2768 break;
2769
Roland Levillaindff1f282014-11-05 14:15:05 +00002770 case Primitive::kPrimLong:
2771 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002772 case Primitive::kPrimBoolean:
2773 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002774 case Primitive::kPrimByte:
2775 case Primitive::kPrimShort:
2776 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002777 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002778 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002779 locations->SetInAt(0, Location::RequiresRegister());
2780 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2781 break;
2782
Roland Levillain624279f2014-12-04 11:54:28 +00002783 case Primitive::kPrimFloat: {
2784 // Processing a Dex `float-to-long' instruction.
2785 InvokeRuntimeCallingConvention calling_convention;
2786 locations->SetInAt(0, Location::FpuRegisterLocation(
2787 calling_convention.GetFpuRegisterAt(0)));
2788 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2789 break;
2790 }
2791
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002792 case Primitive::kPrimDouble: {
2793 // Processing a Dex `double-to-long' instruction.
2794 InvokeRuntimeCallingConvention calling_convention;
2795 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2796 calling_convention.GetFpuRegisterAt(0),
2797 calling_convention.GetFpuRegisterAt(1)));
2798 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002799 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002800 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002801
2802 default:
2803 LOG(FATAL) << "Unexpected type conversion from " << input_type
2804 << " to " << result_type;
2805 }
2806 break;
2807
Roland Levillain981e4542014-11-14 11:47:14 +00002808 case Primitive::kPrimChar:
2809 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002810 case Primitive::kPrimLong:
2811 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002812 case Primitive::kPrimBoolean:
2813 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002814 case Primitive::kPrimByte:
2815 case Primitive::kPrimShort:
2816 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002817 // Processing a Dex `int-to-char' instruction.
2818 locations->SetInAt(0, Location::RequiresRegister());
2819 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2820 break;
2821
2822 default:
2823 LOG(FATAL) << "Unexpected type conversion from " << input_type
2824 << " to " << result_type;
2825 }
2826 break;
2827
Roland Levillaindff1f282014-11-05 14:15:05 +00002828 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002829 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002830 case Primitive::kPrimBoolean:
2831 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002832 case Primitive::kPrimByte:
2833 case Primitive::kPrimShort:
2834 case Primitive::kPrimInt:
2835 case Primitive::kPrimChar:
2836 // Processing a Dex `int-to-float' instruction.
2837 locations->SetInAt(0, Location::RequiresRegister());
2838 locations->SetOut(Location::RequiresFpuRegister());
2839 break;
2840
Roland Levillain5b3ee562015-04-14 16:02:41 +01002841 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002842 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002843 InvokeRuntimeCallingConvention calling_convention;
2844 locations->SetInAt(0, Location::RegisterPairLocation(
2845 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2846 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002847 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002848 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002849
Roland Levillaincff13742014-11-17 14:32:17 +00002850 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002851 // Processing a Dex `double-to-float' instruction.
2852 locations->SetInAt(0, Location::RequiresFpuRegister());
2853 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002854 break;
2855
2856 default:
2857 LOG(FATAL) << "Unexpected type conversion from " << input_type
2858 << " to " << result_type;
2859 };
2860 break;
2861
Roland Levillaindff1f282014-11-05 14:15:05 +00002862 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002863 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002864 case Primitive::kPrimBoolean:
2865 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002866 case Primitive::kPrimByte:
2867 case Primitive::kPrimShort:
2868 case Primitive::kPrimInt:
2869 case Primitive::kPrimChar:
2870 // Processing a Dex `int-to-double' instruction.
2871 locations->SetInAt(0, Location::RequiresRegister());
2872 locations->SetOut(Location::RequiresFpuRegister());
2873 break;
2874
2875 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002876 // Processing a Dex `long-to-double' instruction.
2877 locations->SetInAt(0, Location::RequiresRegister());
2878 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002879 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002880 locations->AddTemp(Location::RequiresFpuRegister());
2881 break;
2882
Roland Levillaincff13742014-11-17 14:32:17 +00002883 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002884 // Processing a Dex `float-to-double' instruction.
2885 locations->SetInAt(0, Location::RequiresFpuRegister());
2886 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002887 break;
2888
2889 default:
2890 LOG(FATAL) << "Unexpected type conversion from " << input_type
2891 << " to " << result_type;
2892 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002893 break;
2894
2895 default:
2896 LOG(FATAL) << "Unexpected type conversion from " << input_type
2897 << " to " << result_type;
2898 }
2899}
2900
2901void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2902 LocationSummary* locations = conversion->GetLocations();
2903 Location out = locations->Out();
2904 Location in = locations->InAt(0);
2905 Primitive::Type result_type = conversion->GetResultType();
2906 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002907 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002908 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002909 case Primitive::kPrimByte:
2910 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002911 case Primitive::kPrimLong:
2912 // Type conversion from long to byte is a result of code transformations.
2913 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2914 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002915 case Primitive::kPrimBoolean:
2916 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002917 case Primitive::kPrimShort:
2918 case Primitive::kPrimInt:
2919 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002920 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002921 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002922 break;
2923
2924 default:
2925 LOG(FATAL) << "Unexpected type conversion from " << input_type
2926 << " to " << result_type;
2927 }
2928 break;
2929
Roland Levillain01a8d712014-11-14 16:27:39 +00002930 case Primitive::kPrimShort:
2931 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002932 case Primitive::kPrimLong:
2933 // Type conversion from long to short is a result of code transformations.
2934 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2935 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002936 case Primitive::kPrimBoolean:
2937 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002938 case Primitive::kPrimByte:
2939 case Primitive::kPrimInt:
2940 case Primitive::kPrimChar:
2941 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002942 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002943 break;
2944
2945 default:
2946 LOG(FATAL) << "Unexpected type conversion from " << input_type
2947 << " to " << result_type;
2948 }
2949 break;
2950
Roland Levillain946e1432014-11-11 17:35:19 +00002951 case Primitive::kPrimInt:
2952 switch (input_type) {
2953 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002954 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002955 DCHECK(out.IsRegister());
2956 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002957 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002958 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002959 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002960 } else {
2961 DCHECK(in.IsConstant());
2962 DCHECK(in.GetConstant()->IsLongConstant());
2963 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002964 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002965 }
2966 break;
2967
Roland Levillain3f8f9362014-12-02 17:45:01 +00002968 case Primitive::kPrimFloat: {
2969 // Processing a Dex `float-to-int' instruction.
2970 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002971 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002972 __ vmovrs(out.AsRegister<Register>(), temp);
2973 break;
2974 }
2975
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002976 case Primitive::kPrimDouble: {
2977 // Processing a Dex `double-to-int' instruction.
2978 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002979 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002980 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002981 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002982 }
Roland Levillain946e1432014-11-11 17:35:19 +00002983
2984 default:
2985 LOG(FATAL) << "Unexpected type conversion from " << input_type
2986 << " to " << result_type;
2987 }
2988 break;
2989
Roland Levillaindff1f282014-11-05 14:15:05 +00002990 case Primitive::kPrimLong:
2991 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002992 case Primitive::kPrimBoolean:
2993 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002994 case Primitive::kPrimByte:
2995 case Primitive::kPrimShort:
2996 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002997 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002998 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002999 DCHECK(out.IsRegisterPair());
3000 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003001 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00003002 // Sign extension.
3003 __ Asr(out.AsRegisterPairHigh<Register>(),
3004 out.AsRegisterPairLow<Register>(),
3005 31);
3006 break;
3007
3008 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00003009 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003010 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003011 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003012 break;
3013
Roland Levillaindff1f282014-11-05 14:15:05 +00003014 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003015 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003016 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003017 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003018 break;
3019
3020 default:
3021 LOG(FATAL) << "Unexpected type conversion from " << input_type
3022 << " to " << result_type;
3023 }
3024 break;
3025
Roland Levillain981e4542014-11-14 11:47:14 +00003026 case Primitive::kPrimChar:
3027 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003028 case Primitive::kPrimLong:
3029 // Type conversion from long to char is a result of code transformations.
3030 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3031 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003032 case Primitive::kPrimBoolean:
3033 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003034 case Primitive::kPrimByte:
3035 case Primitive::kPrimShort:
3036 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003037 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003038 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00003039 break;
3040
3041 default:
3042 LOG(FATAL) << "Unexpected type conversion from " << input_type
3043 << " to " << result_type;
3044 }
3045 break;
3046
Roland Levillaindff1f282014-11-05 14:15:05 +00003047 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003048 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003049 case Primitive::kPrimBoolean:
3050 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003051 case Primitive::kPrimByte:
3052 case Primitive::kPrimShort:
3053 case Primitive::kPrimInt:
3054 case Primitive::kPrimChar: {
3055 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003056 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
3057 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003058 break;
3059 }
3060
Roland Levillain5b3ee562015-04-14 16:02:41 +01003061 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003062 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003063 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003064 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00003065 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003066
Roland Levillaincff13742014-11-17 14:32:17 +00003067 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003068 // Processing a Dex `double-to-float' instruction.
3069 __ vcvtsd(out.AsFpuRegister<SRegister>(),
3070 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00003071 break;
3072
3073 default:
3074 LOG(FATAL) << "Unexpected type conversion from " << input_type
3075 << " to " << result_type;
3076 };
3077 break;
3078
Roland Levillaindff1f282014-11-05 14:15:05 +00003079 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003080 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003081 case Primitive::kPrimBoolean:
3082 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003083 case Primitive::kPrimByte:
3084 case Primitive::kPrimShort:
3085 case Primitive::kPrimInt:
3086 case Primitive::kPrimChar: {
3087 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003088 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003089 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3090 out.AsFpuRegisterPairLow<SRegister>());
3091 break;
3092 }
3093
Roland Levillain647b9ed2014-11-27 12:06:00 +00003094 case Primitive::kPrimLong: {
3095 // Processing a Dex `long-to-double' instruction.
3096 Register low = in.AsRegisterPairLow<Register>();
3097 Register high = in.AsRegisterPairHigh<Register>();
3098 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
3099 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003100 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00003101 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003102 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
3103 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003104
Roland Levillain682393c2015-04-14 15:57:52 +01003105 // temp_d = int-to-double(high)
3106 __ vmovsr(temp_s, high);
3107 __ vcvtdi(temp_d, temp_s);
3108 // constant_d = k2Pow32EncodingForDouble
3109 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
3110 // out_d = unsigned-to-double(low)
3111 __ vmovsr(out_s, low);
3112 __ vcvtdu(out_d, out_s);
3113 // out_d += temp_d * constant_d
3114 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003115 break;
3116 }
3117
Roland Levillaincff13742014-11-17 14:32:17 +00003118 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003119 // Processing a Dex `float-to-double' instruction.
3120 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3121 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003122 break;
3123
3124 default:
3125 LOG(FATAL) << "Unexpected type conversion from " << input_type
3126 << " to " << result_type;
3127 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003128 break;
3129
3130 default:
3131 LOG(FATAL) << "Unexpected type conversion from " << input_type
3132 << " to " << result_type;
3133 }
3134}
3135
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003136void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003137 LocationSummary* locations =
3138 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003139 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003140 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003141 locations->SetInAt(0, Location::RequiresRegister());
3142 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003143 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3144 break;
3145 }
3146
3147 case Primitive::kPrimLong: {
3148 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003149 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003150 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003151 break;
3152 }
3153
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003154 case Primitive::kPrimFloat:
3155 case Primitive::kPrimDouble: {
3156 locations->SetInAt(0, Location::RequiresFpuRegister());
3157 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003158 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003159 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003160 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003161
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003162 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003163 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003164 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003165}
3166
3167void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
3168 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003169 Location out = locations->Out();
3170 Location first = locations->InAt(0);
3171 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003172 switch (add->GetResultType()) {
3173 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003174 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003175 __ add(out.AsRegister<Register>(),
3176 first.AsRegister<Register>(),
3177 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003178 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003179 __ AddConstant(out.AsRegister<Register>(),
3180 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003181 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003182 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003183 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003184
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003185 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003186 if (second.IsConstant()) {
3187 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3188 GenerateAddLongConst(out, first, value);
3189 } else {
3190 DCHECK(second.IsRegisterPair());
3191 __ adds(out.AsRegisterPairLow<Register>(),
3192 first.AsRegisterPairLow<Register>(),
3193 ShifterOperand(second.AsRegisterPairLow<Register>()));
3194 __ adc(out.AsRegisterPairHigh<Register>(),
3195 first.AsRegisterPairHigh<Register>(),
3196 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3197 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003198 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003199 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003200
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003201 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00003202 __ vadds(out.AsFpuRegister<SRegister>(),
3203 first.AsFpuRegister<SRegister>(),
3204 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003205 break;
3206
3207 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003208 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3209 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3210 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003211 break;
3212
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003213 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003214 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003215 }
3216}
3217
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003218void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003219 LocationSummary* locations =
3220 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003221 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003222 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003223 locations->SetInAt(0, Location::RequiresRegister());
3224 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003225 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3226 break;
3227 }
3228
3229 case Primitive::kPrimLong: {
3230 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003231 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003232 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003233 break;
3234 }
Calin Juravle11351682014-10-23 15:38:15 +01003235 case Primitive::kPrimFloat:
3236 case Primitive::kPrimDouble: {
3237 locations->SetInAt(0, Location::RequiresFpuRegister());
3238 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003239 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003240 break;
Calin Juravle11351682014-10-23 15:38:15 +01003241 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003242 default:
Calin Juravle11351682014-10-23 15:38:15 +01003243 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003244 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003245}
3246
3247void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3248 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003249 Location out = locations->Out();
3250 Location first = locations->InAt(0);
3251 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003252 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003253 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003254 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003255 __ sub(out.AsRegister<Register>(),
3256 first.AsRegister<Register>(),
3257 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003258 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003259 __ AddConstant(out.AsRegister<Register>(),
3260 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003261 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003262 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003263 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003264 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003265
Calin Juravle11351682014-10-23 15:38:15 +01003266 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003267 if (second.IsConstant()) {
3268 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3269 GenerateAddLongConst(out, first, -value);
3270 } else {
3271 DCHECK(second.IsRegisterPair());
3272 __ subs(out.AsRegisterPairLow<Register>(),
3273 first.AsRegisterPairLow<Register>(),
3274 ShifterOperand(second.AsRegisterPairLow<Register>()));
3275 __ sbc(out.AsRegisterPairHigh<Register>(),
3276 first.AsRegisterPairHigh<Register>(),
3277 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3278 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003279 break;
Calin Juravle11351682014-10-23 15:38:15 +01003280 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003281
Calin Juravle11351682014-10-23 15:38:15 +01003282 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003283 __ vsubs(out.AsFpuRegister<SRegister>(),
3284 first.AsFpuRegister<SRegister>(),
3285 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003286 break;
Calin Juravle11351682014-10-23 15:38:15 +01003287 }
3288
3289 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003290 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3291 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3292 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003293 break;
3294 }
3295
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003296
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003297 default:
Calin Juravle11351682014-10-23 15:38:15 +01003298 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003299 }
3300}
3301
Calin Juravle34bacdf2014-10-07 20:23:36 +01003302void LocationsBuilderARM::VisitMul(HMul* mul) {
3303 LocationSummary* locations =
3304 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3305 switch (mul->GetResultType()) {
3306 case Primitive::kPrimInt:
3307 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003308 locations->SetInAt(0, Location::RequiresRegister());
3309 locations->SetInAt(1, Location::RequiresRegister());
3310 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003311 break;
3312 }
3313
Calin Juravleb5bfa962014-10-21 18:02:24 +01003314 case Primitive::kPrimFloat:
3315 case Primitive::kPrimDouble: {
3316 locations->SetInAt(0, Location::RequiresFpuRegister());
3317 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003318 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003319 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003320 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003321
3322 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003323 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003324 }
3325}
3326
3327void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3328 LocationSummary* locations = mul->GetLocations();
3329 Location out = locations->Out();
3330 Location first = locations->InAt(0);
3331 Location second = locations->InAt(1);
3332 switch (mul->GetResultType()) {
3333 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003334 __ mul(out.AsRegister<Register>(),
3335 first.AsRegister<Register>(),
3336 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003337 break;
3338 }
3339 case Primitive::kPrimLong: {
3340 Register out_hi = out.AsRegisterPairHigh<Register>();
3341 Register out_lo = out.AsRegisterPairLow<Register>();
3342 Register in1_hi = first.AsRegisterPairHigh<Register>();
3343 Register in1_lo = first.AsRegisterPairLow<Register>();
3344 Register in2_hi = second.AsRegisterPairHigh<Register>();
3345 Register in2_lo = second.AsRegisterPairLow<Register>();
3346
3347 // Extra checks to protect caused by the existence of R1_R2.
3348 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3349 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3350 DCHECK_NE(out_hi, in1_lo);
3351 DCHECK_NE(out_hi, in2_lo);
3352
3353 // input: in1 - 64 bits, in2 - 64 bits
3354 // output: out
3355 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3356 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3357 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3358
3359 // IP <- in1.lo * in2.hi
3360 __ mul(IP, in1_lo, in2_hi);
3361 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3362 __ mla(out_hi, in1_hi, in2_lo, IP);
3363 // out.lo <- (in1.lo * in2.lo)[31:0];
3364 __ umull(out_lo, IP, in1_lo, in2_lo);
3365 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3366 __ add(out_hi, out_hi, ShifterOperand(IP));
3367 break;
3368 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003369
3370 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003371 __ vmuls(out.AsFpuRegister<SRegister>(),
3372 first.AsFpuRegister<SRegister>(),
3373 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003374 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003375 }
3376
3377 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003378 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3379 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3380 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003381 break;
3382 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003383
3384 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003385 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003386 }
3387}
3388
Zheng Xuc6667102015-05-15 16:08:45 +08003389void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3390 DCHECK(instruction->IsDiv() || instruction->IsRem());
3391 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3392
3393 LocationSummary* locations = instruction->GetLocations();
3394 Location second = locations->InAt(1);
3395 DCHECK(second.IsConstant());
3396
3397 Register out = locations->Out().AsRegister<Register>();
3398 Register dividend = locations->InAt(0).AsRegister<Register>();
3399 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3400 DCHECK(imm == 1 || imm == -1);
3401
3402 if (instruction->IsRem()) {
3403 __ LoadImmediate(out, 0);
3404 } else {
3405 if (imm == 1) {
3406 __ Mov(out, dividend);
3407 } else {
3408 __ rsb(out, dividend, ShifterOperand(0));
3409 }
3410 }
3411}
3412
3413void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3414 DCHECK(instruction->IsDiv() || instruction->IsRem());
3415 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3416
3417 LocationSummary* locations = instruction->GetLocations();
3418 Location second = locations->InAt(1);
3419 DCHECK(second.IsConstant());
3420
3421 Register out = locations->Out().AsRegister<Register>();
3422 Register dividend = locations->InAt(0).AsRegister<Register>();
3423 Register temp = locations->GetTemp(0).AsRegister<Register>();
3424 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003425 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003426 int ctz_imm = CTZ(abs_imm);
3427
3428 if (ctz_imm == 1) {
3429 __ Lsr(temp, dividend, 32 - ctz_imm);
3430 } else {
3431 __ Asr(temp, dividend, 31);
3432 __ Lsr(temp, temp, 32 - ctz_imm);
3433 }
3434 __ add(out, temp, ShifterOperand(dividend));
3435
3436 if (instruction->IsDiv()) {
3437 __ Asr(out, out, ctz_imm);
3438 if (imm < 0) {
3439 __ rsb(out, out, ShifterOperand(0));
3440 }
3441 } else {
3442 __ ubfx(out, out, 0, ctz_imm);
3443 __ sub(out, out, ShifterOperand(temp));
3444 }
3445}
3446
3447void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3448 DCHECK(instruction->IsDiv() || instruction->IsRem());
3449 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3450
3451 LocationSummary* locations = instruction->GetLocations();
3452 Location second = locations->InAt(1);
3453 DCHECK(second.IsConstant());
3454
3455 Register out = locations->Out().AsRegister<Register>();
3456 Register dividend = locations->InAt(0).AsRegister<Register>();
3457 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3458 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3459 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3460
3461 int64_t magic;
3462 int shift;
3463 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3464
3465 __ LoadImmediate(temp1, magic);
3466 __ smull(temp2, temp1, dividend, temp1);
3467
3468 if (imm > 0 && magic < 0) {
3469 __ add(temp1, temp1, ShifterOperand(dividend));
3470 } else if (imm < 0 && magic > 0) {
3471 __ sub(temp1, temp1, ShifterOperand(dividend));
3472 }
3473
3474 if (shift != 0) {
3475 __ Asr(temp1, temp1, shift);
3476 }
3477
3478 if (instruction->IsDiv()) {
3479 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3480 } else {
3481 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3482 // TODO: Strength reduction for mls.
3483 __ LoadImmediate(temp2, imm);
3484 __ mls(out, temp1, temp2, dividend);
3485 }
3486}
3487
3488void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3489 DCHECK(instruction->IsDiv() || instruction->IsRem());
3490 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3491
3492 LocationSummary* locations = instruction->GetLocations();
3493 Location second = locations->InAt(1);
3494 DCHECK(second.IsConstant());
3495
3496 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3497 if (imm == 0) {
3498 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3499 } else if (imm == 1 || imm == -1) {
3500 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003501 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003502 DivRemByPowerOfTwo(instruction);
3503 } else {
3504 DCHECK(imm <= -2 || imm >= 2);
3505 GenerateDivRemWithAnyConstant(instruction);
3506 }
3507}
3508
Calin Juravle7c4954d2014-10-28 16:57:40 +00003509void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003510 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3511 if (div->GetResultType() == Primitive::kPrimLong) {
3512 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003513 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003514 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3515 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003516 } else if (div->GetResultType() == Primitive::kPrimInt &&
3517 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3518 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003519 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003520 }
3521
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003522 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3523
Calin Juravle7c4954d2014-10-28 16:57:40 +00003524 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003525 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003526 if (div->InputAt(1)->IsConstant()) {
3527 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003528 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003529 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003530 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3531 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003532 // No temp register required.
3533 } else {
3534 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003535 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003536 locations->AddTemp(Location::RequiresRegister());
3537 }
3538 }
3539 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003540 locations->SetInAt(0, Location::RequiresRegister());
3541 locations->SetInAt(1, Location::RequiresRegister());
3542 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3543 } else {
3544 InvokeRuntimeCallingConvention calling_convention;
3545 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3546 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003547 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003548 // we only need the former.
3549 locations->SetOut(Location::RegisterLocation(R0));
3550 }
Calin Juravled0d48522014-11-04 16:40:20 +00003551 break;
3552 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003553 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003554 InvokeRuntimeCallingConvention calling_convention;
3555 locations->SetInAt(0, Location::RegisterPairLocation(
3556 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3557 locations->SetInAt(1, Location::RegisterPairLocation(
3558 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003559 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003560 break;
3561 }
3562 case Primitive::kPrimFloat:
3563 case Primitive::kPrimDouble: {
3564 locations->SetInAt(0, Location::RequiresFpuRegister());
3565 locations->SetInAt(1, Location::RequiresFpuRegister());
3566 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3567 break;
3568 }
3569
3570 default:
3571 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3572 }
3573}
3574
3575void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3576 LocationSummary* locations = div->GetLocations();
3577 Location out = locations->Out();
3578 Location first = locations->InAt(0);
3579 Location second = locations->InAt(1);
3580
3581 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003582 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003583 if (second.IsConstant()) {
3584 GenerateDivRemConstantIntegral(div);
3585 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003586 __ sdiv(out.AsRegister<Register>(),
3587 first.AsRegister<Register>(),
3588 second.AsRegister<Register>());
3589 } else {
3590 InvokeRuntimeCallingConvention calling_convention;
3591 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3592 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3593 DCHECK_EQ(R0, out.AsRegister<Register>());
3594
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003595 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003596 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003597 }
Calin Juravled0d48522014-11-04 16:40:20 +00003598 break;
3599 }
3600
Calin Juravle7c4954d2014-10-28 16:57:40 +00003601 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003602 InvokeRuntimeCallingConvention calling_convention;
3603 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3604 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3605 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3606 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3607 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003608 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003609
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003610 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003611 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003612 break;
3613 }
3614
3615 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003616 __ vdivs(out.AsFpuRegister<SRegister>(),
3617 first.AsFpuRegister<SRegister>(),
3618 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003619 break;
3620 }
3621
3622 case Primitive::kPrimDouble: {
3623 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3624 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3625 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3626 break;
3627 }
3628
3629 default:
3630 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3631 }
3632}
3633
Calin Juravlebacfec32014-11-14 15:54:36 +00003634void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003635 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003636
3637 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003638 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003639 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3640 // sdiv will be replaced by other instruction sequence.
3641 call_kind = LocationSummary::kNoCall;
3642 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3643 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003644 // Have hardware divide instruction for int, do it with three instructions.
3645 call_kind = LocationSummary::kNoCall;
3646 }
3647
Calin Juravlebacfec32014-11-14 15:54:36 +00003648 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3649
Calin Juravled2ec87d2014-12-08 14:24:46 +00003650 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003651 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003652 if (rem->InputAt(1)->IsConstant()) {
3653 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003654 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003655 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003656 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3657 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003658 // No temp register required.
3659 } else {
3660 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003661 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003662 locations->AddTemp(Location::RequiresRegister());
3663 }
3664 }
3665 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003666 locations->SetInAt(0, Location::RequiresRegister());
3667 locations->SetInAt(1, Location::RequiresRegister());
3668 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3669 locations->AddTemp(Location::RequiresRegister());
3670 } else {
3671 InvokeRuntimeCallingConvention calling_convention;
3672 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3673 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003674 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003675 // we only need the latter.
3676 locations->SetOut(Location::RegisterLocation(R1));
3677 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003678 break;
3679 }
3680 case Primitive::kPrimLong: {
3681 InvokeRuntimeCallingConvention calling_convention;
3682 locations->SetInAt(0, Location::RegisterPairLocation(
3683 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3684 locations->SetInAt(1, Location::RegisterPairLocation(
3685 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3686 // The runtime helper puts the output in R2,R3.
3687 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3688 break;
3689 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003690 case Primitive::kPrimFloat: {
3691 InvokeRuntimeCallingConvention calling_convention;
3692 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3693 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3694 locations->SetOut(Location::FpuRegisterLocation(S0));
3695 break;
3696 }
3697
Calin Juravlebacfec32014-11-14 15:54:36 +00003698 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003699 InvokeRuntimeCallingConvention calling_convention;
3700 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3701 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3702 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3703 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3704 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003705 break;
3706 }
3707
3708 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003709 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003710 }
3711}
3712
3713void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3714 LocationSummary* locations = rem->GetLocations();
3715 Location out = locations->Out();
3716 Location first = locations->InAt(0);
3717 Location second = locations->InAt(1);
3718
Calin Juravled2ec87d2014-12-08 14:24:46 +00003719 Primitive::Type type = rem->GetResultType();
3720 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003721 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003722 if (second.IsConstant()) {
3723 GenerateDivRemConstantIntegral(rem);
3724 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003725 Register reg1 = first.AsRegister<Register>();
3726 Register reg2 = second.AsRegister<Register>();
3727 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003728
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003729 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003730 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003731 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003732 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003733 } else {
3734 InvokeRuntimeCallingConvention calling_convention;
3735 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3736 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3737 DCHECK_EQ(R1, out.AsRegister<Register>());
3738
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003739 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003740 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003741 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003742 break;
3743 }
3744
3745 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003746 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003747 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003748 break;
3749 }
3750
Calin Juravled2ec87d2014-12-08 14:24:46 +00003751 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003752 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003753 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003754 break;
3755 }
3756
Calin Juravlebacfec32014-11-14 15:54:36 +00003757 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003758 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003759 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003760 break;
3761 }
3762
3763 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003764 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003765 }
3766}
3767
Calin Juravled0d48522014-11-04 16:40:20 +00003768void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003769 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003770 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003771}
3772
3773void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003774 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003775 codegen_->AddSlowPath(slow_path);
3776
3777 LocationSummary* locations = instruction->GetLocations();
3778 Location value = locations->InAt(0);
3779
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003780 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003781 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003782 case Primitive::kPrimByte:
3783 case Primitive::kPrimChar:
3784 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003785 case Primitive::kPrimInt: {
3786 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003787 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003788 } else {
3789 DCHECK(value.IsConstant()) << value;
3790 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3791 __ b(slow_path->GetEntryLabel());
3792 }
3793 }
3794 break;
3795 }
3796 case Primitive::kPrimLong: {
3797 if (value.IsRegisterPair()) {
3798 __ orrs(IP,
3799 value.AsRegisterPairLow<Register>(),
3800 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3801 __ b(slow_path->GetEntryLabel(), EQ);
3802 } else {
3803 DCHECK(value.IsConstant()) << value;
3804 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3805 __ b(slow_path->GetEntryLabel());
3806 }
3807 }
3808 break;
3809 default:
3810 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3811 }
3812 }
Calin Juravled0d48522014-11-04 16:40:20 +00003813}
3814
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003815void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3816 Register in = locations->InAt(0).AsRegister<Register>();
3817 Location rhs = locations->InAt(1);
3818 Register out = locations->Out().AsRegister<Register>();
3819
3820 if (rhs.IsConstant()) {
3821 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3822 // so map all rotations to a +ve. equivalent in that range.
3823 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3824 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3825 if (rot) {
3826 // Rotate, mapping left rotations to right equivalents if necessary.
3827 // (e.g. left by 2 bits == right by 30.)
3828 __ Ror(out, in, rot);
3829 } else if (out != in) {
3830 __ Mov(out, in);
3831 }
3832 } else {
3833 __ Ror(out, in, rhs.AsRegister<Register>());
3834 }
3835}
3836
3837// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3838// rotates by swapping input regs (effectively rotating by the first 32-bits of
3839// a larger rotation) or flipping direction (thus treating larger right/left
3840// rotations as sub-word sized rotations in the other direction) as appropriate.
3841void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3842 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3843 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3844 Location rhs = locations->InAt(1);
3845 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3846 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3847
3848 if (rhs.IsConstant()) {
3849 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3850 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003851 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003852 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3853 // logic below to a simple pair of binary orr.
3854 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3855 if (rot >= kArmBitsPerWord) {
3856 rot -= kArmBitsPerWord;
3857 std::swap(in_reg_hi, in_reg_lo);
3858 }
3859 // Rotate, or mov to out for zero or word size rotations.
3860 if (rot != 0u) {
3861 __ Lsr(out_reg_hi, in_reg_hi, rot);
3862 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3863 __ Lsr(out_reg_lo, in_reg_lo, rot);
3864 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3865 } else {
3866 __ Mov(out_reg_lo, in_reg_lo);
3867 __ Mov(out_reg_hi, in_reg_hi);
3868 }
3869 } else {
3870 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3871 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3872 Label end;
3873 Label shift_by_32_plus_shift_right;
3874
3875 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3876 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3877 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3878 __ b(&shift_by_32_plus_shift_right, CC);
3879
3880 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3881 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3882 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3883 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3884 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3885 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3886 __ Lsr(shift_left, in_reg_hi, shift_right);
3887 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3888 __ b(&end);
3889
3890 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3891 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3892 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3893 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3894 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3895 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3896 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3897 __ Lsl(shift_right, in_reg_hi, shift_left);
3898 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3899
3900 __ Bind(&end);
3901 }
3902}
Roland Levillain22c49222016-03-18 14:04:28 +00003903
3904void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003905 LocationSummary* locations =
3906 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3907 switch (ror->GetResultType()) {
3908 case Primitive::kPrimInt: {
3909 locations->SetInAt(0, Location::RequiresRegister());
3910 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3911 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3912 break;
3913 }
3914 case Primitive::kPrimLong: {
3915 locations->SetInAt(0, Location::RequiresRegister());
3916 if (ror->InputAt(1)->IsConstant()) {
3917 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3918 } else {
3919 locations->SetInAt(1, Location::RequiresRegister());
3920 locations->AddTemp(Location::RequiresRegister());
3921 locations->AddTemp(Location::RequiresRegister());
3922 }
3923 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3924 break;
3925 }
3926 default:
3927 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3928 }
3929}
3930
Roland Levillain22c49222016-03-18 14:04:28 +00003931void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003932 LocationSummary* locations = ror->GetLocations();
3933 Primitive::Type type = ror->GetResultType();
3934 switch (type) {
3935 case Primitive::kPrimInt: {
3936 HandleIntegerRotate(locations);
3937 break;
3938 }
3939 case Primitive::kPrimLong: {
3940 HandleLongRotate(locations);
3941 break;
3942 }
3943 default:
3944 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003945 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003946 }
3947}
3948
Calin Juravle9aec02f2014-11-18 23:06:35 +00003949void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3950 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3951
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003952 LocationSummary* locations =
3953 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003954
3955 switch (op->GetResultType()) {
3956 case Primitive::kPrimInt: {
3957 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003958 if (op->InputAt(1)->IsConstant()) {
3959 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3960 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3961 } else {
3962 locations->SetInAt(1, Location::RequiresRegister());
3963 // Make the output overlap, as it will be used to hold the masked
3964 // second input.
3965 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3966 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003967 break;
3968 }
3969 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003970 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003971 if (op->InputAt(1)->IsConstant()) {
3972 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3973 // For simplicity, use kOutputOverlap even though we only require that low registers
3974 // don't clash with high registers which the register allocator currently guarantees.
3975 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3976 } else {
3977 locations->SetInAt(1, Location::RequiresRegister());
3978 locations->AddTemp(Location::RequiresRegister());
3979 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3980 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003981 break;
3982 }
3983 default:
3984 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3985 }
3986}
3987
3988void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3989 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3990
3991 LocationSummary* locations = op->GetLocations();
3992 Location out = locations->Out();
3993 Location first = locations->InAt(0);
3994 Location second = locations->InAt(1);
3995
3996 Primitive::Type type = op->GetResultType();
3997 switch (type) {
3998 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003999 Register out_reg = out.AsRegister<Register>();
4000 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004001 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004002 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00004003 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004004 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004005 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004006 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004007 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004008 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004009 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004010 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004011 }
4012 } else {
4013 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004014 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00004015 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00004016 __ Mov(out_reg, first_reg);
4017 } else if (op->IsShl()) {
4018 __ Lsl(out_reg, first_reg, shift_value);
4019 } else if (op->IsShr()) {
4020 __ Asr(out_reg, first_reg, shift_value);
4021 } else {
4022 __ Lsr(out_reg, first_reg, shift_value);
4023 }
4024 }
4025 break;
4026 }
4027 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004028 Register o_h = out.AsRegisterPairHigh<Register>();
4029 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004030
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004031 Register high = first.AsRegisterPairHigh<Register>();
4032 Register low = first.AsRegisterPairLow<Register>();
4033
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004034 if (second.IsRegister()) {
4035 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004036
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004037 Register second_reg = second.AsRegister<Register>();
4038
4039 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004040 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004041 // Shift the high part
4042 __ Lsl(o_h, high, o_l);
4043 // Shift the low part and `or` what overflew on the high part
4044 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
4045 __ Lsr(temp, low, temp);
4046 __ orr(o_h, o_h, ShifterOperand(temp));
4047 // If the shift is > 32 bits, override the high part
4048 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
4049 __ it(PL);
4050 __ Lsl(o_h, low, temp, PL);
4051 // Shift the low part
4052 __ Lsl(o_l, low, o_l);
4053 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004054 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004055 // Shift the low part
4056 __ Lsr(o_l, low, o_h);
4057 // Shift the high part and `or` what underflew on the low part
4058 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4059 __ Lsl(temp, high, temp);
4060 __ orr(o_l, o_l, ShifterOperand(temp));
4061 // If the shift is > 32 bits, override the low part
4062 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4063 __ it(PL);
4064 __ Asr(o_l, high, temp, PL);
4065 // Shift the high part
4066 __ Asr(o_h, high, o_h);
4067 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004068 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004069 // same as Shr except we use `Lsr`s and not `Asr`s
4070 __ Lsr(o_l, low, o_h);
4071 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4072 __ Lsl(temp, high, temp);
4073 __ orr(o_l, o_l, ShifterOperand(temp));
4074 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4075 __ it(PL);
4076 __ Lsr(o_l, high, temp, PL);
4077 __ Lsr(o_h, high, o_h);
4078 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004079 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004080 // Register allocator doesn't create partial overlap.
4081 DCHECK_NE(o_l, high);
4082 DCHECK_NE(o_h, low);
4083 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004084 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004085 if (shift_value > 32) {
4086 if (op->IsShl()) {
4087 __ Lsl(o_h, low, shift_value - 32);
4088 __ LoadImmediate(o_l, 0);
4089 } else if (op->IsShr()) {
4090 __ Asr(o_l, high, shift_value - 32);
4091 __ Asr(o_h, high, 31);
4092 } else {
4093 __ Lsr(o_l, high, shift_value - 32);
4094 __ LoadImmediate(o_h, 0);
4095 }
4096 } else if (shift_value == 32) {
4097 if (op->IsShl()) {
4098 __ mov(o_h, ShifterOperand(low));
4099 __ LoadImmediate(o_l, 0);
4100 } else if (op->IsShr()) {
4101 __ mov(o_l, ShifterOperand(high));
4102 __ Asr(o_h, high, 31);
4103 } else {
4104 __ mov(o_l, ShifterOperand(high));
4105 __ LoadImmediate(o_h, 0);
4106 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00004107 } else if (shift_value == 1) {
4108 if (op->IsShl()) {
4109 __ Lsls(o_l, low, 1);
4110 __ adc(o_h, high, ShifterOperand(high));
4111 } else if (op->IsShr()) {
4112 __ Asrs(o_h, high, 1);
4113 __ Rrx(o_l, low);
4114 } else {
4115 __ Lsrs(o_h, high, 1);
4116 __ Rrx(o_l, low);
4117 }
4118 } else {
4119 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004120 if (op->IsShl()) {
4121 __ Lsl(o_h, high, shift_value);
4122 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
4123 __ Lsl(o_l, low, shift_value);
4124 } else if (op->IsShr()) {
4125 __ Lsr(o_l, low, shift_value);
4126 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4127 __ Asr(o_h, high, shift_value);
4128 } else {
4129 __ Lsr(o_l, low, shift_value);
4130 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4131 __ Lsr(o_h, high, shift_value);
4132 }
4133 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004134 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004135 break;
4136 }
4137 default:
4138 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004139 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004140 }
4141}
4142
4143void LocationsBuilderARM::VisitShl(HShl* shl) {
4144 HandleShift(shl);
4145}
4146
4147void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
4148 HandleShift(shl);
4149}
4150
4151void LocationsBuilderARM::VisitShr(HShr* shr) {
4152 HandleShift(shr);
4153}
4154
4155void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
4156 HandleShift(shr);
4157}
4158
4159void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
4160 HandleShift(ushr);
4161}
4162
4163void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
4164 HandleShift(ushr);
4165}
4166
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004167void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004168 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004169 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00004170 if (instruction->IsStringAlloc()) {
4171 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4172 } else {
4173 InvokeRuntimeCallingConvention calling_convention;
4174 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004175 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004176 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004177}
4178
4179void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004180 // Note: if heap poisoning is enabled, the entry point takes cares
4181 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004182 if (instruction->IsStringAlloc()) {
4183 // String is allocated through StringFactory. Call NewEmptyString entry point.
4184 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004185 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004186 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4187 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
4188 __ blx(LR);
4189 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4190 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004191 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004192 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004193 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004194}
4195
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004196void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
4197 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004198 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004199 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004200 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004201 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4202 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004203}
4204
4205void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004206 // Note: if heap poisoning is enabled, the entry point takes cares
4207 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004208 QuickEntrypointEnum entrypoint =
4209 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4210 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004211 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004212 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004213}
4214
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004215void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004216 LocationSummary* locations =
4217 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004218 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4219 if (location.IsStackSlot()) {
4220 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4221 } else if (location.IsDoubleStackSlot()) {
4222 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004223 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004224 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004225}
4226
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004227void InstructionCodeGeneratorARM::VisitParameterValue(
4228 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004229 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004230}
4231
4232void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4233 LocationSummary* locations =
4234 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4235 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4236}
4237
4238void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4239 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004240}
4241
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004242void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004243 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004244 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004245 locations->SetInAt(0, Location::RequiresRegister());
4246 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004247}
4248
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004249void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4250 LocationSummary* locations = not_->GetLocations();
4251 Location out = locations->Out();
4252 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004253 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004254 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004255 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004256 break;
4257
4258 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004259 __ mvn(out.AsRegisterPairLow<Register>(),
4260 ShifterOperand(in.AsRegisterPairLow<Register>()));
4261 __ mvn(out.AsRegisterPairHigh<Register>(),
4262 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004263 break;
4264
4265 default:
4266 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4267 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004268}
4269
David Brazdil66d126e2015-04-03 16:02:44 +01004270void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4271 LocationSummary* locations =
4272 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4273 locations->SetInAt(0, Location::RequiresRegister());
4274 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4275}
4276
4277void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004278 LocationSummary* locations = bool_not->GetLocations();
4279 Location out = locations->Out();
4280 Location in = locations->InAt(0);
4281 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4282}
4283
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004284void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004285 LocationSummary* locations =
4286 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004287 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004288 case Primitive::kPrimBoolean:
4289 case Primitive::kPrimByte:
4290 case Primitive::kPrimShort:
4291 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004292 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004293 case Primitive::kPrimLong: {
4294 locations->SetInAt(0, Location::RequiresRegister());
4295 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004296 // Output overlaps because it is written before doing the low comparison.
4297 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004298 break;
4299 }
4300 case Primitive::kPrimFloat:
4301 case Primitive::kPrimDouble: {
4302 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004303 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004304 locations->SetOut(Location::RequiresRegister());
4305 break;
4306 }
4307 default:
4308 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4309 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004310}
4311
4312void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004313 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004314 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004315 Location left = locations->InAt(0);
4316 Location right = locations->InAt(1);
4317
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004318 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004319 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004320 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004321 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004322 case Primitive::kPrimBoolean:
4323 case Primitive::kPrimByte:
4324 case Primitive::kPrimShort:
4325 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004326 case Primitive::kPrimInt: {
4327 __ LoadImmediate(out, 0);
4328 __ cmp(left.AsRegister<Register>(),
4329 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4330 less_cond = LT;
4331 break;
4332 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004333 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004334 __ cmp(left.AsRegisterPairHigh<Register>(),
4335 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004336 __ b(&less, LT);
4337 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004338 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004339 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004340 __ cmp(left.AsRegisterPairLow<Register>(),
4341 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004342 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004343 break;
4344 }
4345 case Primitive::kPrimFloat:
4346 case Primitive::kPrimDouble: {
4347 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004348 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004349 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004350 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004351 break;
4352 }
4353 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004354 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004355 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004356 }
Aart Bika19616e2016-02-01 18:57:58 -08004357
Calin Juravleddb7df22014-11-25 20:56:51 +00004358 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004359 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004360
4361 __ Bind(&greater);
4362 __ LoadImmediate(out, 1);
4363 __ b(&done);
4364
4365 __ Bind(&less);
4366 __ LoadImmediate(out, -1);
4367
4368 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004369}
4370
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004371void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004372 LocationSummary* locations =
4373 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004374 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004375 locations->SetInAt(i, Location::Any());
4376 }
4377 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004378}
4379
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004380void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004381 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004382}
4383
Roland Levillainc9285912015-12-18 10:38:42 +00004384void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4385 // TODO (ported from quick): revisit ARM barrier kinds.
4386 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004387 switch (kind) {
4388 case MemBarrierKind::kAnyStore:
4389 case MemBarrierKind::kLoadAny:
4390 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004391 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004392 break;
4393 }
4394 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004395 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004396 break;
4397 }
4398 default:
4399 LOG(FATAL) << "Unexpected memory barrier " << kind;
4400 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004401 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004402}
4403
4404void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4405 uint32_t offset,
4406 Register out_lo,
4407 Register out_hi) {
4408 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004409 // Ensure `out_lo` is different from `addr`, so that loading
4410 // `offset` into `out_lo` does not clutter `addr`.
4411 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004412 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004413 __ add(IP, addr, ShifterOperand(out_lo));
4414 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004415 }
4416 __ ldrexd(out_lo, out_hi, addr);
4417}
4418
4419void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4420 uint32_t offset,
4421 Register value_lo,
4422 Register value_hi,
4423 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004424 Register temp2,
4425 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004426 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004427 if (offset != 0) {
4428 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004429 __ add(IP, addr, ShifterOperand(temp1));
4430 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004431 }
4432 __ Bind(&fail);
4433 // We need a load followed by store. (The address used in a STREX instruction must
4434 // be the same as the address in the most recently executed LDREX instruction.)
4435 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004436 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004437 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004438 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004439}
4440
4441void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4442 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4443
Nicolas Geoffray39468442014-09-02 15:17:15 +01004444 LocationSummary* locations =
4445 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004446 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004447
Calin Juravle52c48962014-12-16 17:02:57 +00004448 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004449 if (Primitive::IsFloatingPointType(field_type)) {
4450 locations->SetInAt(1, Location::RequiresFpuRegister());
4451 } else {
4452 locations->SetInAt(1, Location::RequiresRegister());
4453 }
4454
Calin Juravle52c48962014-12-16 17:02:57 +00004455 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004456 bool generate_volatile = field_info.IsVolatile()
4457 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004458 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004459 bool needs_write_barrier =
4460 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004461 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004462 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004463 if (needs_write_barrier) {
4464 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004465 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004466 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004467 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004468 // - registers need to be consecutive
4469 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004470 // We don't test for ARM yet, and the assertion makes sure that we
4471 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004472 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4473
4474 locations->AddTemp(Location::RequiresRegister());
4475 locations->AddTemp(Location::RequiresRegister());
4476 if (field_type == Primitive::kPrimDouble) {
4477 // For doubles we need two more registers to copy the value.
4478 locations->AddTemp(Location::RegisterLocation(R2));
4479 locations->AddTemp(Location::RegisterLocation(R3));
4480 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004481 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004482}
4483
Calin Juravle52c48962014-12-16 17:02:57 +00004484void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004485 const FieldInfo& field_info,
4486 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004487 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4488
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004489 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004490 Register base = locations->InAt(0).AsRegister<Register>();
4491 Location value = locations->InAt(1);
4492
4493 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004494 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004495 Primitive::Type field_type = field_info.GetFieldType();
4496 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004497 bool needs_write_barrier =
4498 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004499
4500 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004501 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004502 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004503
4504 switch (field_type) {
4505 case Primitive::kPrimBoolean:
4506 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004507 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004508 break;
4509 }
4510
4511 case Primitive::kPrimShort:
4512 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004513 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004514 break;
4515 }
4516
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004517 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004518 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004519 if (kPoisonHeapReferences && needs_write_barrier) {
4520 // Note that in the case where `value` is a null reference,
4521 // we do not enter this block, as a null reference does not
4522 // need poisoning.
4523 DCHECK_EQ(field_type, Primitive::kPrimNot);
4524 Register temp = locations->GetTemp(0).AsRegister<Register>();
4525 __ Mov(temp, value.AsRegister<Register>());
4526 __ PoisonHeapReference(temp);
4527 __ StoreToOffset(kStoreWord, temp, base, offset);
4528 } else {
4529 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4530 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004531 break;
4532 }
4533
4534 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004535 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004536 GenerateWideAtomicStore(base, offset,
4537 value.AsRegisterPairLow<Register>(),
4538 value.AsRegisterPairHigh<Register>(),
4539 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004540 locations->GetTemp(1).AsRegister<Register>(),
4541 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004542 } else {
4543 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004544 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004545 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004546 break;
4547 }
4548
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004549 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004550 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004551 break;
4552 }
4553
4554 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004555 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004556 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004557 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4558 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4559
4560 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4561
4562 GenerateWideAtomicStore(base, offset,
4563 value_reg_lo,
4564 value_reg_hi,
4565 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004566 locations->GetTemp(3).AsRegister<Register>(),
4567 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004568 } else {
4569 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004570 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004571 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004572 break;
4573 }
4574
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004575 case Primitive::kPrimVoid:
4576 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004577 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004578 }
Calin Juravle52c48962014-12-16 17:02:57 +00004579
Calin Juravle77520bc2015-01-12 18:45:46 +00004580 // Longs and doubles are handled in the switch.
4581 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4582 codegen_->MaybeRecordImplicitNullCheck(instruction);
4583 }
4584
4585 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4586 Register temp = locations->GetTemp(0).AsRegister<Register>();
4587 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004588 codegen_->MarkGCCard(
4589 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004590 }
4591
Calin Juravle52c48962014-12-16 17:02:57 +00004592 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004593 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004594 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004595}
4596
Calin Juravle52c48962014-12-16 17:02:57 +00004597void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4598 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004599
4600 bool object_field_get_with_read_barrier =
4601 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004602 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004603 new (GetGraph()->GetArena()) LocationSummary(instruction,
4604 object_field_get_with_read_barrier ?
4605 LocationSummary::kCallOnSlowPath :
4606 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004607 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004608 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004609 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004610 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004611
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004612 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004613 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004614 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004615 // The output overlaps in case of volatile long: we don't want the
4616 // code generated by GenerateWideAtomicLoad to overwrite the
4617 // object's location. Likewise, in the case of an object field get
4618 // with read barriers enabled, we do not want the load to overwrite
4619 // the object's location, as we need it to emit the read barrier.
4620 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4621 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004622
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004623 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4624 locations->SetOut(Location::RequiresFpuRegister());
4625 } else {
4626 locations->SetOut(Location::RequiresRegister(),
4627 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4628 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004629 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004630 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004631 // - registers need to be consecutive
4632 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004633 // We don't test for ARM yet, and the assertion makes sure that we
4634 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004635 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4636 locations->AddTemp(Location::RequiresRegister());
4637 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004638 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4639 // We need a temporary register for the read barrier marking slow
4640 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4641 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004642 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004643}
4644
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004645Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4646 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4647 << input->GetType();
4648 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4649 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4650 return Location::ConstantLocation(input->AsConstant());
4651 } else {
4652 return Location::RequiresFpuRegister();
4653 }
4654}
4655
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004656Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4657 Opcode opcode) {
4658 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4659 if (constant->IsConstant() &&
4660 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4661 return Location::ConstantLocation(constant->AsConstant());
4662 }
4663 return Location::RequiresRegister();
4664}
4665
4666bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4667 Opcode opcode) {
4668 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4669 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004670 Opcode high_opcode = opcode;
4671 SetCc low_set_cc = kCcDontCare;
4672 switch (opcode) {
4673 case SUB:
4674 // Flip the operation to an ADD.
4675 value = -value;
4676 opcode = ADD;
4677 FALLTHROUGH_INTENDED;
4678 case ADD:
4679 if (Low32Bits(value) == 0u) {
4680 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4681 }
4682 high_opcode = ADC;
4683 low_set_cc = kCcSet;
4684 break;
4685 default:
4686 break;
4687 }
4688 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4689 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004690 } else {
4691 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4692 }
4693}
4694
Vladimir Marko59751a72016-08-05 14:37:27 +01004695bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4696 Opcode opcode,
4697 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004698 ShifterOperand so;
4699 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004700 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004701 return true;
4702 }
4703 Opcode neg_opcode = kNoOperand;
4704 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004705 case AND: neg_opcode = BIC; value = ~value; break;
4706 case ORR: neg_opcode = ORN; value = ~value; break;
4707 case ADD: neg_opcode = SUB; value = -value; break;
4708 case ADC: neg_opcode = SBC; value = ~value; break;
4709 case SUB: neg_opcode = ADD; value = -value; break;
4710 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004711 default:
4712 return false;
4713 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004714 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004715}
4716
Calin Juravle52c48962014-12-16 17:02:57 +00004717void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4718 const FieldInfo& field_info) {
4719 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004720
Calin Juravle52c48962014-12-16 17:02:57 +00004721 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004722 Location base_loc = locations->InAt(0);
4723 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004724 Location out = locations->Out();
4725 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004726 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004727 Primitive::Type field_type = field_info.GetFieldType();
4728 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4729
4730 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004731 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004732 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004733 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004734
Roland Levillainc9285912015-12-18 10:38:42 +00004735 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004736 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004737 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004738
Roland Levillainc9285912015-12-18 10:38:42 +00004739 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004740 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004741 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004742
Roland Levillainc9285912015-12-18 10:38:42 +00004743 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004744 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004745 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004746
4747 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004748 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004749 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004750
4751 case Primitive::kPrimNot: {
4752 // /* HeapReference<Object> */ out = *(base + offset)
4753 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4754 Location temp_loc = locations->GetTemp(0);
4755 // Note that a potential implicit null check is handled in this
4756 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4757 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4758 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4759 if (is_volatile) {
4760 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4761 }
4762 } else {
4763 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4764 codegen_->MaybeRecordImplicitNullCheck(instruction);
4765 if (is_volatile) {
4766 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4767 }
4768 // If read barriers are enabled, emit read barriers other than
4769 // Baker's using a slow path (and also unpoison the loaded
4770 // reference, if heap poisoning is enabled).
4771 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4772 }
4773 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004774 }
4775
Roland Levillainc9285912015-12-18 10:38:42 +00004776 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004777 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004778 GenerateWideAtomicLoad(base, offset,
4779 out.AsRegisterPairLow<Register>(),
4780 out.AsRegisterPairHigh<Register>());
4781 } else {
4782 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4783 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004784 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004785
Roland Levillainc9285912015-12-18 10:38:42 +00004786 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004787 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004788 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004789
4790 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004791 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004792 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004793 Register lo = locations->GetTemp(0).AsRegister<Register>();
4794 Register hi = locations->GetTemp(1).AsRegister<Register>();
4795 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004796 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004797 __ vmovdrr(out_reg, lo, hi);
4798 } else {
4799 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004800 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004801 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004802 break;
4803 }
4804
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004805 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004806 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004807 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004808 }
Calin Juravle52c48962014-12-16 17:02:57 +00004809
Roland Levillainc9285912015-12-18 10:38:42 +00004810 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4811 // Potential implicit null checks, in the case of reference or
4812 // double fields, are handled in the previous switch statement.
4813 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004814 codegen_->MaybeRecordImplicitNullCheck(instruction);
4815 }
4816
Calin Juravle52c48962014-12-16 17:02:57 +00004817 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004818 if (field_type == Primitive::kPrimNot) {
4819 // Memory barriers, in the case of references, are also handled
4820 // in the previous switch statement.
4821 } else {
4822 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4823 }
Roland Levillain4d027112015-07-01 15:41:14 +01004824 }
Calin Juravle52c48962014-12-16 17:02:57 +00004825}
4826
4827void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4828 HandleFieldSet(instruction, instruction->GetFieldInfo());
4829}
4830
4831void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004832 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004833}
4834
4835void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4836 HandleFieldGet(instruction, instruction->GetFieldInfo());
4837}
4838
4839void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4840 HandleFieldGet(instruction, instruction->GetFieldInfo());
4841}
4842
4843void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4844 HandleFieldGet(instruction, instruction->GetFieldInfo());
4845}
4846
4847void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4848 HandleFieldGet(instruction, instruction->GetFieldInfo());
4849}
4850
4851void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4852 HandleFieldSet(instruction, instruction->GetFieldInfo());
4853}
4854
4855void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004856 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004857}
4858
Calin Juravlee460d1d2015-09-29 04:52:17 +01004859void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4860 HUnresolvedInstanceFieldGet* instruction) {
4861 FieldAccessCallingConventionARM calling_convention;
4862 codegen_->CreateUnresolvedFieldLocationSummary(
4863 instruction, instruction->GetFieldType(), calling_convention);
4864}
4865
4866void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4867 HUnresolvedInstanceFieldGet* instruction) {
4868 FieldAccessCallingConventionARM calling_convention;
4869 codegen_->GenerateUnresolvedFieldAccess(instruction,
4870 instruction->GetFieldType(),
4871 instruction->GetFieldIndex(),
4872 instruction->GetDexPc(),
4873 calling_convention);
4874}
4875
4876void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4877 HUnresolvedInstanceFieldSet* instruction) {
4878 FieldAccessCallingConventionARM calling_convention;
4879 codegen_->CreateUnresolvedFieldLocationSummary(
4880 instruction, instruction->GetFieldType(), calling_convention);
4881}
4882
4883void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4884 HUnresolvedInstanceFieldSet* instruction) {
4885 FieldAccessCallingConventionARM calling_convention;
4886 codegen_->GenerateUnresolvedFieldAccess(instruction,
4887 instruction->GetFieldType(),
4888 instruction->GetFieldIndex(),
4889 instruction->GetDexPc(),
4890 calling_convention);
4891}
4892
4893void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4894 HUnresolvedStaticFieldGet* instruction) {
4895 FieldAccessCallingConventionARM calling_convention;
4896 codegen_->CreateUnresolvedFieldLocationSummary(
4897 instruction, instruction->GetFieldType(), calling_convention);
4898}
4899
4900void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4901 HUnresolvedStaticFieldGet* instruction) {
4902 FieldAccessCallingConventionARM calling_convention;
4903 codegen_->GenerateUnresolvedFieldAccess(instruction,
4904 instruction->GetFieldType(),
4905 instruction->GetFieldIndex(),
4906 instruction->GetDexPc(),
4907 calling_convention);
4908}
4909
4910void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4911 HUnresolvedStaticFieldSet* instruction) {
4912 FieldAccessCallingConventionARM calling_convention;
4913 codegen_->CreateUnresolvedFieldLocationSummary(
4914 instruction, instruction->GetFieldType(), calling_convention);
4915}
4916
4917void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4918 HUnresolvedStaticFieldSet* instruction) {
4919 FieldAccessCallingConventionARM calling_convention;
4920 codegen_->GenerateUnresolvedFieldAccess(instruction,
4921 instruction->GetFieldType(),
4922 instruction->GetFieldIndex(),
4923 instruction->GetDexPc(),
4924 calling_convention);
4925}
4926
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004927void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004928 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4929 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004930}
4931
Calin Juravle2ae48182016-03-16 14:05:09 +00004932void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4933 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004934 return;
4935 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004936 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004937
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004938 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004939 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004940}
4941
Calin Juravle2ae48182016-03-16 14:05:09 +00004942void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004943 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004944 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004945
4946 LocationSummary* locations = instruction->GetLocations();
4947 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004948
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004949 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004950}
4951
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004952void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004953 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004954}
4955
Artem Serov6c916792016-07-11 14:02:34 +01004956static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4957 switch (type) {
4958 case Primitive::kPrimNot:
4959 return kLoadWord;
4960 case Primitive::kPrimBoolean:
4961 return kLoadUnsignedByte;
4962 case Primitive::kPrimByte:
4963 return kLoadSignedByte;
4964 case Primitive::kPrimChar:
4965 return kLoadUnsignedHalfword;
4966 case Primitive::kPrimShort:
4967 return kLoadSignedHalfword;
4968 case Primitive::kPrimInt:
4969 return kLoadWord;
4970 case Primitive::kPrimLong:
4971 return kLoadWordPair;
4972 case Primitive::kPrimFloat:
4973 return kLoadSWord;
4974 case Primitive::kPrimDouble:
4975 return kLoadDWord;
4976 default:
4977 LOG(FATAL) << "Unreachable type " << type;
4978 UNREACHABLE();
4979 }
4980}
4981
4982static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4983 switch (type) {
4984 case Primitive::kPrimNot:
4985 return kStoreWord;
4986 case Primitive::kPrimBoolean:
4987 case Primitive::kPrimByte:
4988 return kStoreByte;
4989 case Primitive::kPrimChar:
4990 case Primitive::kPrimShort:
4991 return kStoreHalfword;
4992 case Primitive::kPrimInt:
4993 return kStoreWord;
4994 case Primitive::kPrimLong:
4995 return kStoreWordPair;
4996 case Primitive::kPrimFloat:
4997 return kStoreSWord;
4998 case Primitive::kPrimDouble:
4999 return kStoreDWord;
5000 default:
5001 LOG(FATAL) << "Unreachable type " << type;
5002 UNREACHABLE();
5003 }
5004}
5005
5006void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
5007 Location out_loc,
5008 Register base,
5009 Register reg_offset,
5010 Condition cond) {
5011 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5012 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5013
5014 switch (type) {
5015 case Primitive::kPrimByte:
5016 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
5017 break;
5018 case Primitive::kPrimBoolean:
5019 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
5020 break;
5021 case Primitive::kPrimShort:
5022 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
5023 break;
5024 case Primitive::kPrimChar:
5025 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
5026 break;
5027 case Primitive::kPrimNot:
5028 case Primitive::kPrimInt:
5029 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
5030 break;
5031 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5032 case Primitive::kPrimLong:
5033 case Primitive::kPrimFloat:
5034 case Primitive::kPrimDouble:
5035 default:
5036 LOG(FATAL) << "Unreachable type " << type;
5037 UNREACHABLE();
5038 }
5039}
5040
5041void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
5042 Location loc,
5043 Register base,
5044 Register reg_offset,
5045 Condition cond) {
5046 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5047 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5048
5049 switch (type) {
5050 case Primitive::kPrimByte:
5051 case Primitive::kPrimBoolean:
5052 __ strb(loc.AsRegister<Register>(), mem_address, cond);
5053 break;
5054 case Primitive::kPrimShort:
5055 case Primitive::kPrimChar:
5056 __ strh(loc.AsRegister<Register>(), mem_address, cond);
5057 break;
5058 case Primitive::kPrimNot:
5059 case Primitive::kPrimInt:
5060 __ str(loc.AsRegister<Register>(), mem_address, cond);
5061 break;
5062 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5063 case Primitive::kPrimLong:
5064 case Primitive::kPrimFloat:
5065 case Primitive::kPrimDouble:
5066 default:
5067 LOG(FATAL) << "Unreachable type " << type;
5068 UNREACHABLE();
5069 }
5070}
5071
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005072void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005073 bool object_array_get_with_read_barrier =
5074 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005075 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005076 new (GetGraph()->GetArena()) LocationSummary(instruction,
5077 object_array_get_with_read_barrier ?
5078 LocationSummary::kCallOnSlowPath :
5079 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005080 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005081 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005082 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005083 locations->SetInAt(0, Location::RequiresRegister());
5084 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005085 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5086 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5087 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005088 // The output overlaps in the case of an object array get with
5089 // read barriers enabled: we do not want the move to overwrite the
5090 // array's location, as we need it to emit the read barrier.
5091 locations->SetOut(
5092 Location::RequiresRegister(),
5093 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005094 }
Roland Levillainc9285912015-12-18 10:38:42 +00005095 // We need a temporary register for the read barrier marking slow
5096 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07005097 // Also need for String compression feature.
5098 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
5099 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00005100 locations->AddTemp(Location::RequiresRegister());
5101 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005102}
5103
5104void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
5105 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005106 Location obj_loc = locations->InAt(0);
5107 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005108 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00005109 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005110 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00005111 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07005112 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5113 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01005114 HInstruction* array_instr = instruction->GetArray();
5115 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01005116
Roland Levillain4d027112015-07-01 15:41:14 +01005117 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01005118 case Primitive::kPrimBoolean:
5119 case Primitive::kPrimByte:
5120 case Primitive::kPrimShort:
5121 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00005122 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005123 Register length;
5124 if (maybe_compressed_char_at) {
5125 length = locations->GetTemp(0).AsRegister<Register>();
5126 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5127 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
5128 codegen_->MaybeRecordImplicitNullCheck(instruction);
5129 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005130 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005131 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07005132 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07005133 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005134 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5135 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5136 "Expecting 0=compressed, 1=uncompressed");
5137 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005138 __ LoadFromOffset(kLoadUnsignedByte,
5139 out_loc.AsRegister<Register>(),
5140 obj,
5141 data_offset + const_index);
5142 __ b(&done);
5143 __ Bind(&uncompressed_load);
5144 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5145 out_loc.AsRegister<Register>(),
5146 obj,
5147 data_offset + (const_index << 1));
5148 __ Bind(&done);
5149 } else {
5150 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01005151
jessicahandojo05765752016-09-09 19:01:32 -07005152 LoadOperandType load_type = GetLoadOperandType(type);
5153 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
5154 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005155 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005156 Register temp = IP;
5157
5158 if (has_intermediate_address) {
5159 // We do not need to compute the intermediate address from the array: the
5160 // input instruction has done it already. See the comment in
5161 // `TryExtractArrayAccessAddress()`.
5162 if (kIsDebugBuild) {
5163 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5164 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5165 }
5166 temp = obj;
5167 } else {
5168 __ add(temp, obj, ShifterOperand(data_offset));
5169 }
jessicahandojo05765752016-09-09 19:01:32 -07005170 if (maybe_compressed_char_at) {
5171 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005172 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5173 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5174 "Expecting 0=compressed, 1=uncompressed");
5175 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005176 __ ldrb(out_loc.AsRegister<Register>(),
5177 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
5178 __ b(&done);
5179 __ Bind(&uncompressed_load);
5180 __ ldrh(out_loc.AsRegister<Register>(),
5181 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
5182 __ Bind(&done);
5183 } else {
5184 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
5185 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005186 }
5187 break;
5188 }
5189
Roland Levillainc9285912015-12-18 10:38:42 +00005190 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00005191 // The read barrier instrumentation of object ArrayGet
5192 // instructions does not support the HIntermediateAddress
5193 // instruction.
5194 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5195
Roland Levillainc9285912015-12-18 10:38:42 +00005196 static_assert(
5197 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5198 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00005199 // /* HeapReference<Object> */ out =
5200 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5201 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5202 Location temp = locations->GetTemp(0);
5203 // Note that a potential implicit null check is handled in this
5204 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
5205 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5206 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5207 } else {
5208 Register out = out_loc.AsRegister<Register>();
5209 if (index.IsConstant()) {
5210 size_t offset =
5211 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5212 __ LoadFromOffset(kLoadWord, out, obj, offset);
5213 codegen_->MaybeRecordImplicitNullCheck(instruction);
5214 // If read barriers are enabled, emit read barriers other than
5215 // Baker's using a slow path (and also unpoison the loaded
5216 // reference, if heap poisoning is enabled).
5217 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5218 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005219 Register temp = IP;
5220
5221 if (has_intermediate_address) {
5222 // We do not need to compute the intermediate address from the array: the
5223 // input instruction has done it already. See the comment in
5224 // `TryExtractArrayAccessAddress()`.
5225 if (kIsDebugBuild) {
5226 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5227 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5228 }
5229 temp = obj;
5230 } else {
5231 __ add(temp, obj, ShifterOperand(data_offset));
5232 }
5233 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005234
Roland Levillainc9285912015-12-18 10:38:42 +00005235 codegen_->MaybeRecordImplicitNullCheck(instruction);
5236 // If read barriers are enabled, emit read barriers other than
5237 // Baker's using a slow path (and also unpoison the loaded
5238 // reference, if heap poisoning is enabled).
5239 codegen_->MaybeGenerateReadBarrierSlow(
5240 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5241 }
5242 }
5243 break;
5244 }
5245
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005246 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005247 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005248 size_t offset =
5249 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005250 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005251 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005252 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005253 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 }
5255 break;
5256 }
5257
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005258 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005259 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005260 if (index.IsConstant()) {
5261 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005262 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005263 } else {
5264 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005265 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005266 }
5267 break;
5268 }
5269
5270 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005271 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005272 if (index.IsConstant()) {
5273 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005274 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005275 } else {
5276 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005277 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005278 }
5279 break;
5280 }
5281
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005283 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005284 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005285 }
Roland Levillain4d027112015-07-01 15:41:14 +01005286
5287 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005288 // Potential implicit null checks, in the case of reference
5289 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005290 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005291 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005292 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293}
5294
5295void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005296 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005297
5298 bool needs_write_barrier =
5299 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005300 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005301
Nicolas Geoffray39468442014-09-02 15:17:15 +01005302 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005303 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005304 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005305 LocationSummary::kCallOnSlowPath :
5306 LocationSummary::kNoCall);
5307
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005308 locations->SetInAt(0, Location::RequiresRegister());
5309 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5310 if (Primitive::IsFloatingPointType(value_type)) {
5311 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005312 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005313 locations->SetInAt(2, Location::RequiresRegister());
5314 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005315 if (needs_write_barrier) {
5316 // Temporary registers for the write barrier.
5317 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005318 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005319 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005320}
5321
5322void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5323 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005324 Location array_loc = locations->InAt(0);
5325 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005327 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005328 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005329 bool needs_write_barrier =
5330 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005331 uint32_t data_offset =
5332 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5333 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005334 HInstruction* array_instr = instruction->GetArray();
5335 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005336
5337 switch (value_type) {
5338 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005339 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005341 case Primitive::kPrimChar:
5342 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005343 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005344 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5345 uint32_t full_offset =
5346 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5347 StoreOperandType store_type = GetStoreOperandType(value_type);
5348 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005350 Register temp = IP;
5351
5352 if (has_intermediate_address) {
5353 // We do not need to compute the intermediate address from the array: the
5354 // input instruction has done it already. See the comment in
5355 // `TryExtractArrayAccessAddress()`.
5356 if (kIsDebugBuild) {
5357 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5358 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5359 }
5360 temp = array;
5361 } else {
5362 __ add(temp, array, ShifterOperand(data_offset));
5363 }
Artem Serov6c916792016-07-11 14:02:34 +01005364 codegen_->StoreToShiftedRegOffset(value_type,
5365 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005366 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005367 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368 }
5369 break;
5370 }
5371
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005372 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005373 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005374 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5375 // See the comment in instruction_simplifier_shared.cc.
5376 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005377
5378 if (instruction->InputAt(2)->IsNullConstant()) {
5379 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005380 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005381 size_t offset =
5382 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005383 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005384 } else {
5385 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005386 __ add(IP, array, ShifterOperand(data_offset));
5387 codegen_->StoreToShiftedRegOffset(value_type,
5388 value_loc,
5389 IP,
5390 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005391 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005392 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005393 DCHECK(!needs_write_barrier);
5394 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005395 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005396 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005397
5398 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005399 Location temp1_loc = locations->GetTemp(0);
5400 Register temp1 = temp1_loc.AsRegister<Register>();
5401 Location temp2_loc = locations->GetTemp(1);
5402 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005403 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5404 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5405 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5406 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005407 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005408
Roland Levillain3b359c72015-11-17 19:35:12 +00005409 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005410 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5411 codegen_->AddSlowPath(slow_path);
5412 if (instruction->GetValueCanBeNull()) {
5413 Label non_zero;
5414 __ CompareAndBranchIfNonZero(value, &non_zero);
5415 if (index.IsConstant()) {
5416 size_t offset =
5417 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5418 __ StoreToOffset(kStoreWord, value, array, offset);
5419 } else {
5420 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005421 __ add(IP, array, ShifterOperand(data_offset));
5422 codegen_->StoreToShiftedRegOffset(value_type,
5423 value_loc,
5424 IP,
5425 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005426 }
5427 codegen_->MaybeRecordImplicitNullCheck(instruction);
5428 __ b(&done);
5429 __ Bind(&non_zero);
5430 }
5431
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005432 // Note that when read barriers are enabled, the type checks
5433 // are performed without read barriers. This is fine, even in
5434 // the case where a class object is in the from-space after
5435 // the flip, as a comparison involving such a type would not
5436 // produce a false positive; it may of course produce a false
5437 // negative, in which case we would take the ArraySet slow
5438 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005439
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005440 // /* HeapReference<Class> */ temp1 = array->klass_
5441 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5442 codegen_->MaybeRecordImplicitNullCheck(instruction);
5443 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005444
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005445 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5446 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5447 // /* HeapReference<Class> */ temp2 = value->klass_
5448 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5449 // If heap poisoning is enabled, no need to unpoison `temp1`
5450 // nor `temp2`, as we are comparing two poisoned references.
5451 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005452
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005453 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5454 Label do_put;
5455 __ b(&do_put, EQ);
5456 // If heap poisoning is enabled, the `temp1` reference has
5457 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005458 __ MaybeUnpoisonHeapReference(temp1);
5459
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005460 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5461 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5462 // If heap poisoning is enabled, no need to unpoison
5463 // `temp1`, as we are comparing against null below.
5464 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5465 __ Bind(&do_put);
5466 } else {
5467 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005468 }
5469 }
5470
Artem Serov6c916792016-07-11 14:02:34 +01005471 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005472 if (kPoisonHeapReferences) {
5473 // Note that in the case where `value` is a null reference,
5474 // we do not enter this block, as a null reference does not
5475 // need poisoning.
5476 DCHECK_EQ(value_type, Primitive::kPrimNot);
5477 __ Mov(temp1, value);
5478 __ PoisonHeapReference(temp1);
5479 source = temp1;
5480 }
5481
5482 if (index.IsConstant()) {
5483 size_t offset =
5484 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5485 __ StoreToOffset(kStoreWord, source, array, offset);
5486 } else {
5487 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005488
5489 __ add(IP, array, ShifterOperand(data_offset));
5490 codegen_->StoreToShiftedRegOffset(value_type,
5491 Location::RegisterLocation(source),
5492 IP,
5493 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005494 }
5495
Roland Levillain3b359c72015-11-17 19:35:12 +00005496 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005497 codegen_->MaybeRecordImplicitNullCheck(instruction);
5498 }
5499
5500 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5501
5502 if (done.IsLinked()) {
5503 __ Bind(&done);
5504 }
5505
5506 if (slow_path != nullptr) {
5507 __ Bind(slow_path->GetExitLabel());
5508 }
5509
5510 break;
5511 }
5512
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005513 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005514 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005515 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005516 size_t offset =
5517 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005518 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005519 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005520 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005521 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005522 }
5523 break;
5524 }
5525
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005526 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005527 Location value = locations->InAt(2);
5528 DCHECK(value.IsFpuRegister());
5529 if (index.IsConstant()) {
5530 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005531 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005532 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005533 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005534 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5535 }
5536 break;
5537 }
5538
5539 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005540 Location value = locations->InAt(2);
5541 DCHECK(value.IsFpuRegisterPair());
5542 if (index.IsConstant()) {
5543 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005544 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005545 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005546 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005547 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5548 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005549
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005550 break;
5551 }
5552
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005553 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005554 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005555 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005556 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005557
Roland Levillain80e67092016-01-08 16:04:55 +00005558 // Objects are handled in the switch.
5559 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005560 codegen_->MaybeRecordImplicitNullCheck(instruction);
5561 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005562}
5563
5564void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005565 LocationSummary* locations =
5566 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005567 locations->SetInAt(0, Location::RequiresRegister());
5568 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005569}
5570
5571void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5572 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005573 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005574 Register obj = locations->InAt(0).AsRegister<Register>();
5575 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005576 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005577 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005578 // Mask out compression flag from String's array length.
5579 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005580 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07005581 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005582}
5583
Artem Serov328429f2016-07-06 16:23:04 +01005584void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005585 LocationSummary* locations =
5586 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5587
5588 locations->SetInAt(0, Location::RequiresRegister());
5589 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5590 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5591}
5592
5593void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5594 LocationSummary* locations = instruction->GetLocations();
5595 Location out = locations->Out();
5596 Location first = locations->InAt(0);
5597 Location second = locations->InAt(1);
5598
Artem Serov328429f2016-07-06 16:23:04 +01005599 if (second.IsRegister()) {
5600 __ add(out.AsRegister<Register>(),
5601 first.AsRegister<Register>(),
5602 ShifterOperand(second.AsRegister<Register>()));
5603 } else {
5604 __ AddConstant(out.AsRegister<Register>(),
5605 first.AsRegister<Register>(),
5606 second.GetConstant()->AsIntConstant()->GetValue());
5607 }
5608}
5609
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005610void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005611 RegisterSet caller_saves = RegisterSet::Empty();
5612 InvokeRuntimeCallingConvention calling_convention;
5613 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5614 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5615 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005616 locations->SetInAt(0, Location::RequiresRegister());
5617 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005618}
5619
5620void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5621 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005622 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005623 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005624 codegen_->AddSlowPath(slow_path);
5625
Roland Levillain271ab9c2014-11-27 15:23:57 +00005626 Register index = locations->InAt(0).AsRegister<Register>();
5627 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005628
5629 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005630 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005631}
5632
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005633void CodeGeneratorARM::MarkGCCard(Register temp,
5634 Register card,
5635 Register object,
5636 Register value,
5637 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005638 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005639 if (can_be_null) {
5640 __ CompareAndBranchIfZero(value, &is_null);
5641 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005642 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005643 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5644 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005645 if (can_be_null) {
5646 __ Bind(&is_null);
5647 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005648}
5649
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005650void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005651 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005652}
5653
5654void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005655 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5656}
5657
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005658void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005659 LocationSummary* locations =
5660 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005661 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005662}
5663
5664void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005665 HBasicBlock* block = instruction->GetBlock();
5666 if (block->GetLoopInformation() != nullptr) {
5667 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5668 // The back edge will generate the suspend check.
5669 return;
5670 }
5671 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5672 // The goto will generate the suspend check.
5673 return;
5674 }
5675 GenerateSuspendCheck(instruction, nullptr);
5676}
5677
5678void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5679 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005680 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005681 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5682 if (slow_path == nullptr) {
5683 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5684 instruction->SetSlowPath(slow_path);
5685 codegen_->AddSlowPath(slow_path);
5686 if (successor != nullptr) {
5687 DCHECK(successor->IsLoopHeader());
5688 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5689 }
5690 } else {
5691 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5692 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005693
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005694 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005695 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005696 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005697 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005698 __ Bind(slow_path->GetReturnLabel());
5699 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005700 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005701 __ b(slow_path->GetEntryLabel());
5702 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005703}
5704
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005705ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5706 return codegen_->GetAssembler();
5707}
5708
5709void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005710 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005711 Location source = move->GetSource();
5712 Location destination = move->GetDestination();
5713
5714 if (source.IsRegister()) {
5715 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005716 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005717 } else if (destination.IsFpuRegister()) {
5718 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005719 } else {
5720 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005721 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005722 SP, destination.GetStackIndex());
5723 }
5724 } else if (source.IsStackSlot()) {
5725 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005726 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005727 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005728 } else if (destination.IsFpuRegister()) {
5729 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005730 } else {
5731 DCHECK(destination.IsStackSlot());
5732 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5733 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5734 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005735 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005736 if (destination.IsRegister()) {
5737 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5738 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005739 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005740 } else {
5741 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005742 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5743 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005744 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005745 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005746 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5747 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005748 } else if (destination.IsRegisterPair()) {
5749 DCHECK(ExpectedPairLayout(destination));
5750 __ LoadFromOffset(
5751 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5752 } else {
5753 DCHECK(destination.IsFpuRegisterPair()) << destination;
5754 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5755 SP,
5756 source.GetStackIndex());
5757 }
5758 } else if (source.IsRegisterPair()) {
5759 if (destination.IsRegisterPair()) {
5760 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5761 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005762 } else if (destination.IsFpuRegisterPair()) {
5763 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5764 source.AsRegisterPairLow<Register>(),
5765 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005766 } else {
5767 DCHECK(destination.IsDoubleStackSlot()) << destination;
5768 DCHECK(ExpectedPairLayout(source));
5769 __ StoreToOffset(
5770 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5771 }
5772 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005773 if (destination.IsRegisterPair()) {
5774 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5775 destination.AsRegisterPairHigh<Register>(),
5776 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5777 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005778 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5779 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5780 } else {
5781 DCHECK(destination.IsDoubleStackSlot()) << destination;
5782 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5783 SP,
5784 destination.GetStackIndex());
5785 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005786 } else {
5787 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005788 HConstant* constant = source.GetConstant();
5789 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5790 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005791 if (destination.IsRegister()) {
5792 __ LoadImmediate(destination.AsRegister<Register>(), value);
5793 } else {
5794 DCHECK(destination.IsStackSlot());
5795 __ LoadImmediate(IP, value);
5796 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5797 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005798 } else if (constant->IsLongConstant()) {
5799 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005800 if (destination.IsRegisterPair()) {
5801 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5802 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005803 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005804 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005805 __ LoadImmediate(IP, Low32Bits(value));
5806 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5807 __ LoadImmediate(IP, High32Bits(value));
5808 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5809 }
5810 } else if (constant->IsDoubleConstant()) {
5811 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005812 if (destination.IsFpuRegisterPair()) {
5813 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005814 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005815 DCHECK(destination.IsDoubleStackSlot()) << destination;
5816 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005817 __ LoadImmediate(IP, Low32Bits(int_value));
5818 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5819 __ LoadImmediate(IP, High32Bits(int_value));
5820 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5821 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005822 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005823 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005824 float value = constant->AsFloatConstant()->GetValue();
5825 if (destination.IsFpuRegister()) {
5826 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5827 } else {
5828 DCHECK(destination.IsStackSlot());
5829 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5830 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5831 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005832 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005833 }
5834}
5835
5836void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5837 __ Mov(IP, reg);
5838 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5839 __ StoreToOffset(kStoreWord, IP, SP, mem);
5840}
5841
5842void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5843 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5844 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5845 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5846 SP, mem1 + stack_offset);
5847 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5848 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5849 SP, mem2 + stack_offset);
5850 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5851}
5852
5853void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005854 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005855 Location source = move->GetSource();
5856 Location destination = move->GetDestination();
5857
5858 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005859 DCHECK_NE(source.AsRegister<Register>(), IP);
5860 DCHECK_NE(destination.AsRegister<Register>(), IP);
5861 __ Mov(IP, source.AsRegister<Register>());
5862 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5863 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005864 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005865 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005866 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005867 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005868 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5869 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005870 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005871 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005872 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005873 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005874 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005875 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005876 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005877 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005878 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5879 destination.AsRegisterPairHigh<Register>(),
5880 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005881 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005882 Register low_reg = source.IsRegisterPair()
5883 ? source.AsRegisterPairLow<Register>()
5884 : destination.AsRegisterPairLow<Register>();
5885 int mem = source.IsRegisterPair()
5886 ? destination.GetStackIndex()
5887 : source.GetStackIndex();
5888 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005889 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005890 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005891 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005892 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005893 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5894 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005895 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005896 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005897 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005898 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5899 DRegister reg = source.IsFpuRegisterPair()
5900 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5901 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5902 int mem = source.IsFpuRegisterPair()
5903 ? destination.GetStackIndex()
5904 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005905 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005906 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005907 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005908 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5909 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5910 : destination.AsFpuRegister<SRegister>();
5911 int mem = source.IsFpuRegister()
5912 ? destination.GetStackIndex()
5913 : source.GetStackIndex();
5914
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005915 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005916 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005917 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005918 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005919 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5920 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005921 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005922 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005923 }
5924}
5925
5926void ParallelMoveResolverARM::SpillScratch(int reg) {
5927 __ Push(static_cast<Register>(reg));
5928}
5929
5930void ParallelMoveResolverARM::RestoreScratch(int reg) {
5931 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005932}
5933
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005934HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5935 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005936 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005937 case HLoadClass::LoadKind::kInvalid:
5938 LOG(FATAL) << "UNREACHABLE";
5939 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005940 case HLoadClass::LoadKind::kReferrersClass:
5941 break;
5942 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5943 DCHECK(!GetCompilerOptions().GetCompilePic());
5944 break;
5945 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5946 DCHECK(GetCompilerOptions().GetCompilePic());
5947 break;
5948 case HLoadClass::LoadKind::kBootImageAddress:
5949 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005950 case HLoadClass::LoadKind::kBssEntry:
5951 DCHECK(!Runtime::Current()->UseJitCompilation());
5952 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005953 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005954 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005955 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005956 case HLoadClass::LoadKind::kDexCacheViaMethod:
5957 break;
5958 }
5959 return desired_class_load_kind;
5960}
5961
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005962void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005963 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5964 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005965 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005966 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005967 cls,
5968 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005969 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005970 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005971 return;
5972 }
Vladimir Marko41559982017-01-06 14:04:23 +00005973 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005974
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005975 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5976 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005977 ? LocationSummary::kCallOnSlowPath
5978 : LocationSummary::kNoCall;
5979 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005980 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005981 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005982 }
5983
Vladimir Marko41559982017-01-06 14:04:23 +00005984 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005985 locations->SetInAt(0, Location::RequiresRegister());
5986 }
5987 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005988 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5989 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5990 // Rely on the type resolution or initialization and marking to save everything we need.
5991 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
5992 // to the custom calling convention) or by marking, so we request a different temp.
5993 locations->AddTemp(Location::RequiresRegister());
5994 RegisterSet caller_saves = RegisterSet::Empty();
5995 InvokeRuntimeCallingConvention calling_convention;
5996 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5997 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
5998 // that the the kPrimNot result register is the same as the first argument register.
5999 locations->SetCustomSlowPathCallerSaves(caller_saves);
6000 } else {
6001 // For non-Baker read barrier we have a temp-clobbering call.
6002 }
6003 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006004}
6005
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006006// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6007// move.
6008void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006009 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6010 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6011 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006012 return;
6013 }
Vladimir Marko41559982017-01-06 14:04:23 +00006014 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006015
Vladimir Marko41559982017-01-06 14:04:23 +00006016 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006017 Location out_loc = locations->Out();
6018 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006019
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006020 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6021 ? kWithoutReadBarrier
6022 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006023 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006024 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006025 case HLoadClass::LoadKind::kReferrersClass: {
6026 DCHECK(!cls->CanCallRuntime());
6027 DCHECK(!cls->MustGenerateClinitCheck());
6028 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6029 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006030 GenerateGcRootFieldLoad(cls,
6031 out_loc,
6032 current_method,
6033 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006034 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006035 break;
6036 }
6037 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006038 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006039 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006040 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6041 cls->GetTypeIndex()));
6042 break;
6043 }
6044 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006045 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006046 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006047 CodeGeneratorARM::PcRelativePatchInfo* labels =
6048 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6049 __ BindTrackedLabel(&labels->movw_label);
6050 __ movw(out, /* placeholder */ 0u);
6051 __ BindTrackedLabel(&labels->movt_label);
6052 __ movt(out, /* placeholder */ 0u);
6053 __ BindTrackedLabel(&labels->add_pc_label);
6054 __ add(out, out, ShifterOperand(PC));
6055 break;
6056 }
6057 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006058 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006059 uint32_t address = dchecked_integral_cast<uint32_t>(
6060 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6061 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006062 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6063 break;
6064 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006065 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006066 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6067 ? locations->GetTemp(0).AsRegister<Register>()
6068 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006069 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006070 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006071 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006072 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006073 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006074 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006075 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006076 __ add(temp, temp, ShifterOperand(PC));
6077 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006078 generate_null_check = true;
6079 break;
6080 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006081 case HLoadClass::LoadKind::kJitTableAddress: {
6082 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6083 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006084 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006085 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006086 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006087 break;
6088 }
Vladimir Marko41559982017-01-06 14:04:23 +00006089 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006090 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006091 LOG(FATAL) << "UNREACHABLE";
6092 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006093 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006094
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006095 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6096 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01006097 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006098 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6099 codegen_->AddSlowPath(slow_path);
6100 if (generate_null_check) {
6101 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6102 }
6103 if (cls->MustGenerateClinitCheck()) {
6104 GenerateClassInitializationCheck(slow_path, out);
6105 } else {
6106 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006107 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006108 }
6109}
6110
6111void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
6112 LocationSummary* locations =
6113 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6114 locations->SetInAt(0, Location::RequiresRegister());
6115 if (check->HasUses()) {
6116 locations->SetOut(Location::SameAsFirstInput());
6117 }
6118}
6119
6120void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006121 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006122 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006123 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006124 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006125 GenerateClassInitializationCheck(slow_path,
6126 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006127}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006128
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006129void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01006130 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006131 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
6132 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
6133 __ b(slow_path->GetEntryLabel(), LT);
6134 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6135 // properly. Therefore, we do a memory fence.
6136 __ dmb(ISH);
6137 __ Bind(slow_path->GetExitLabel());
6138}
6139
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006140HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
6141 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006142 switch (desired_string_load_kind) {
6143 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6144 DCHECK(!GetCompilerOptions().GetCompilePic());
6145 break;
6146 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6147 DCHECK(GetCompilerOptions().GetCompilePic());
6148 break;
6149 case HLoadString::LoadKind::kBootImageAddress:
6150 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006151 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006152 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006153 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006154 case HLoadString::LoadKind::kJitTableAddress:
6155 DCHECK(Runtime::Current()->UseJitCompilation());
6156 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006157 case HLoadString::LoadKind::kDexCacheViaMethod:
6158 break;
6159 }
6160 return desired_string_load_kind;
6161}
6162
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006163void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006164 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006165 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006166 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006167 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006168 locations->SetOut(Location::RegisterLocation(R0));
6169 } else {
6170 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006171 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6172 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006173 // Rely on the pResolveString and marking to save everything we need, including temps.
6174 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6175 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006176 locations->AddTemp(Location::RequiresRegister());
6177 RegisterSet caller_saves = RegisterSet::Empty();
6178 InvokeRuntimeCallingConvention calling_convention;
6179 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6180 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6181 // that the the kPrimNot result register is the same as the first argument register.
6182 locations->SetCustomSlowPathCallerSaves(caller_saves);
6183 } else {
6184 // For non-Baker read barrier we have a temp-clobbering call.
6185 }
6186 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006187 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006188}
6189
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006190// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6191// move.
6192void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006193 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006194 Location out_loc = locations->Out();
6195 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006196 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00006197
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006198 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006199 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006200 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006201 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6202 load->GetStringIndex()));
6203 return; // No dex cache slow path.
6204 }
6205 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006206 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006207 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006208 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006209 __ BindTrackedLabel(&labels->movw_label);
6210 __ movw(out, /* placeholder */ 0u);
6211 __ BindTrackedLabel(&labels->movt_label);
6212 __ movt(out, /* placeholder */ 0u);
6213 __ BindTrackedLabel(&labels->add_pc_label);
6214 __ add(out, out, ShifterOperand(PC));
6215 return; // No dex cache slow path.
6216 }
6217 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006218 uint32_t address = dchecked_integral_cast<uint32_t>(
6219 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6220 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006221 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6222 return; // No dex cache slow path.
6223 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006224 case HLoadString::LoadKind::kBssEntry: {
6225 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006226 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6227 ? locations->GetTemp(0).AsRegister<Register>()
6228 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006229 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006230 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006231 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006232 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006233 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006234 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006235 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006236 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006237 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006238 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
6239 codegen_->AddSlowPath(slow_path);
6240 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6241 __ Bind(slow_path->GetExitLabel());
6242 return;
6243 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006244 case HLoadString::LoadKind::kJitTableAddress: {
6245 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006246 load->GetStringIndex(),
6247 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006248 // /* GcRoot<mirror::String> */ out = *out
6249 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6250 return;
6251 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006252 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006253 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006254 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006255
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006256 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
6257 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6258 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006259 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006260 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006261 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6262 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006263}
6264
David Brazdilcb1c0552015-08-04 16:22:25 +01006265static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006266 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006267}
6268
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006269void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6270 LocationSummary* locations =
6271 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6272 locations->SetOut(Location::RequiresRegister());
6273}
6274
6275void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006276 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006277 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6278}
6279
6280void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6281 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6282}
6283
6284void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006285 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006286 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006287}
6288
6289void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6290 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006291 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006292 InvokeRuntimeCallingConvention calling_convention;
6293 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6294}
6295
6296void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006297 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006298 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006299}
6300
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006301// Temp is used for read barrier.
6302static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6303 if (kEmitCompilerReadBarrier &&
6304 (kUseBakerReadBarrier ||
6305 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6306 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6307 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6308 return 1;
6309 }
6310 return 0;
6311}
6312
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006313// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006314// interface pointer, one for loading the current interface.
6315// The other checks have one temp for loading the object's class.
6316static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6317 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6318 return 3;
6319 }
6320 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006321}
6322
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006323void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006324 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006325 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006326 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006327 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 case TypeCheckKind::kExactCheck:
6329 case TypeCheckKind::kAbstractClassCheck:
6330 case TypeCheckKind::kClassHierarchyCheck:
6331 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006332 call_kind =
6333 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006334 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006335 break;
6336 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006337 case TypeCheckKind::kUnresolvedCheck:
6338 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006339 call_kind = LocationSummary::kCallOnSlowPath;
6340 break;
6341 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006342
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006343 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006344 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006345 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006346 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006347 locations->SetInAt(0, Location::RequiresRegister());
6348 locations->SetInAt(1, Location::RequiresRegister());
6349 // The "out" register is used as a temporary, so it overlaps with the inputs.
6350 // Note that TypeCheckSlowPathARM uses this register too.
6351 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006352 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006353}
6354
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006355void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006356 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006357 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006358 Location obj_loc = locations->InAt(0);
6359 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006360 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006361 Location out_loc = locations->Out();
6362 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006363 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6364 DCHECK_LE(num_temps, 1u);
6365 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006366 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006367 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6368 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6369 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006370 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006371 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006372
6373 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006374 // avoid null check if we know obj is not null.
6375 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006376 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006377 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006378
Roland Levillainc9285912015-12-18 10:38:42 +00006379 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006380 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006381 // /* HeapReference<Class> */ out = obj->klass_
6382 GenerateReferenceLoadTwoRegisters(instruction,
6383 out_loc,
6384 obj_loc,
6385 class_offset,
6386 maybe_temp_loc,
6387 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006388 __ cmp(out, ShifterOperand(cls));
6389 // Classes must be equal for the instanceof to succeed.
6390 __ b(&zero, NE);
6391 __ LoadImmediate(out, 1);
6392 __ b(&done);
6393 break;
6394 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006395
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006396 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006397 // /* HeapReference<Class> */ out = obj->klass_
6398 GenerateReferenceLoadTwoRegisters(instruction,
6399 out_loc,
6400 obj_loc,
6401 class_offset,
6402 maybe_temp_loc,
6403 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006404 // If the class is abstract, we eagerly fetch the super class of the
6405 // object to avoid doing a comparison we know will fail.
6406 Label loop;
6407 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006408 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006409 GenerateReferenceLoadOneRegister(instruction,
6410 out_loc,
6411 super_offset,
6412 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006413 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006414 // If `out` is null, we use it for the result, and jump to `done`.
6415 __ CompareAndBranchIfZero(out, &done);
6416 __ cmp(out, ShifterOperand(cls));
6417 __ b(&loop, NE);
6418 __ LoadImmediate(out, 1);
6419 if (zero.IsLinked()) {
6420 __ b(&done);
6421 }
6422 break;
6423 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006424
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006425 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006426 // /* HeapReference<Class> */ out = obj->klass_
6427 GenerateReferenceLoadTwoRegisters(instruction,
6428 out_loc,
6429 obj_loc,
6430 class_offset,
6431 maybe_temp_loc,
6432 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006433 // Walk over the class hierarchy to find a match.
6434 Label loop, success;
6435 __ Bind(&loop);
6436 __ cmp(out, ShifterOperand(cls));
6437 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006438 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006439 GenerateReferenceLoadOneRegister(instruction,
6440 out_loc,
6441 super_offset,
6442 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006443 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006444 __ CompareAndBranchIfNonZero(out, &loop);
6445 // If `out` is null, we use it for the result, and jump to `done`.
6446 __ b(&done);
6447 __ Bind(&success);
6448 __ LoadImmediate(out, 1);
6449 if (zero.IsLinked()) {
6450 __ b(&done);
6451 }
6452 break;
6453 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006454
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006455 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006456 // /* HeapReference<Class> */ out = obj->klass_
6457 GenerateReferenceLoadTwoRegisters(instruction,
6458 out_loc,
6459 obj_loc,
6460 class_offset,
6461 maybe_temp_loc,
6462 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006463 // Do an exact check.
6464 Label exact_check;
6465 __ cmp(out, ShifterOperand(cls));
6466 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006467 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006468 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006469 GenerateReferenceLoadOneRegister(instruction,
6470 out_loc,
6471 component_offset,
6472 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006473 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006474 // If `out` is null, we use it for the result, and jump to `done`.
6475 __ CompareAndBranchIfZero(out, &done);
6476 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6477 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6478 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006479 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006480 __ LoadImmediate(out, 1);
6481 __ b(&done);
6482 break;
6483 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006484
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006485 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006486 // No read barrier since the slow path will retry upon failure.
6487 // /* HeapReference<Class> */ out = obj->klass_
6488 GenerateReferenceLoadTwoRegisters(instruction,
6489 out_loc,
6490 obj_loc,
6491 class_offset,
6492 maybe_temp_loc,
6493 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006494 __ cmp(out, ShifterOperand(cls));
6495 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006496 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6497 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006498 codegen_->AddSlowPath(slow_path);
6499 __ b(slow_path->GetEntryLabel(), NE);
6500 __ LoadImmediate(out, 1);
6501 if (zero.IsLinked()) {
6502 __ b(&done);
6503 }
6504 break;
6505 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006506
Calin Juravle98893e12015-10-02 21:05:03 +01006507 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006508 case TypeCheckKind::kInterfaceCheck: {
6509 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006510 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006511 // cases.
6512 //
6513 // We cannot directly call the InstanceofNonTrivial runtime
6514 // entry point without resorting to a type checking slow path
6515 // here (i.e. by calling InvokeRuntime directly), as it would
6516 // require to assign fixed registers for the inputs of this
6517 // HInstanceOf instruction (following the runtime calling
6518 // convention), which might be cluttered by the potential first
6519 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006520 //
6521 // TODO: Introduce a new runtime entry point taking the object
6522 // to test (instead of its class) as argument, and let it deal
6523 // with the read barrier issues. This will let us refactor this
6524 // case of the `switch` code as it was previously (with a direct
6525 // call to the runtime not using a type checking slow path).
6526 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006527 DCHECK(locations->OnlyCallsOnSlowPath());
6528 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6529 /* is_fatal */ false);
6530 codegen_->AddSlowPath(slow_path);
6531 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006532 if (zero.IsLinked()) {
6533 __ b(&done);
6534 }
6535 break;
6536 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006537 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006538
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006539 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006540 __ Bind(&zero);
6541 __ LoadImmediate(out, 0);
6542 }
6543
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006544 if (done.IsLinked()) {
6545 __ Bind(&done);
6546 }
6547
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006548 if (slow_path != nullptr) {
6549 __ Bind(slow_path->GetExitLabel());
6550 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006551}
6552
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006553void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006554 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6555 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6556
Roland Levillain3b359c72015-11-17 19:35:12 +00006557 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6558 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006559 case TypeCheckKind::kExactCheck:
6560 case TypeCheckKind::kAbstractClassCheck:
6561 case TypeCheckKind::kClassHierarchyCheck:
6562 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006563 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6564 LocationSummary::kCallOnSlowPath :
6565 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006566 break;
6567 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006568 case TypeCheckKind::kUnresolvedCheck:
6569 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006570 call_kind = LocationSummary::kCallOnSlowPath;
6571 break;
6572 }
6573
Roland Levillain3b359c72015-11-17 19:35:12 +00006574 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6575 locations->SetInAt(0, Location::RequiresRegister());
6576 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006577 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006578}
6579
6580void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006581 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006582 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006583 Location obj_loc = locations->InAt(0);
6584 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006585 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006586 Location temp_loc = locations->GetTemp(0);
6587 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006588 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6589 DCHECK_LE(num_temps, 3u);
6590 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6591 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6592 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6593 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6594 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6595 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6596 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6597 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6598 const uint32_t object_array_data_offset =
6599 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006600
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006601 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6602 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6603 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006604 bool is_type_check_slow_path_fatal = false;
6605 if (!kEmitCompilerReadBarrier) {
6606 is_type_check_slow_path_fatal =
6607 (type_check_kind == TypeCheckKind::kExactCheck ||
6608 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6609 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6610 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6611 !instruction->CanThrowIntoCatchBlock();
6612 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01006613 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006614 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6615 is_type_check_slow_path_fatal);
6616 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006617
6618 Label done;
6619 // Avoid null check if we know obj is not null.
6620 if (instruction->MustDoNullCheck()) {
6621 __ CompareAndBranchIfZero(obj, &done);
6622 }
6623
Roland Levillain3b359c72015-11-17 19:35:12 +00006624 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006625 case TypeCheckKind::kExactCheck:
6626 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006627 // /* HeapReference<Class> */ temp = obj->klass_
6628 GenerateReferenceLoadTwoRegisters(instruction,
6629 temp_loc,
6630 obj_loc,
6631 class_offset,
6632 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006633 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006634
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006635 __ cmp(temp, ShifterOperand(cls));
6636 // Jump to slow path for throwing the exception or doing a
6637 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006638 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006639 break;
6640 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006641
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006642 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006643 // /* HeapReference<Class> */ temp = obj->klass_
6644 GenerateReferenceLoadTwoRegisters(instruction,
6645 temp_loc,
6646 obj_loc,
6647 class_offset,
6648 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006649 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006650
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006651 // If the class is abstract, we eagerly fetch the super class of the
6652 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006653 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006654 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006655 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006656 GenerateReferenceLoadOneRegister(instruction,
6657 temp_loc,
6658 super_offset,
6659 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006660 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006661
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006662 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6663 // exception.
6664 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00006665
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006666 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006667 __ cmp(temp, ShifterOperand(cls));
6668 __ b(&loop, NE);
6669 break;
6670 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006671
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006672 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006673 // /* HeapReference<Class> */ temp = obj->klass_
6674 GenerateReferenceLoadTwoRegisters(instruction,
6675 temp_loc,
6676 obj_loc,
6677 class_offset,
6678 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006679 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006680
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006681 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006682 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006683 __ Bind(&loop);
6684 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006685 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006686
Roland Levillain3b359c72015-11-17 19:35:12 +00006687 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006688 GenerateReferenceLoadOneRegister(instruction,
6689 temp_loc,
6690 super_offset,
6691 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006692 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006693
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006694 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6695 // exception.
6696 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6697 // Otherwise, jump to the beginning of the loop.
6698 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006699 break;
6700 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006701
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006702 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006703 // /* HeapReference<Class> */ temp = obj->klass_
6704 GenerateReferenceLoadTwoRegisters(instruction,
6705 temp_loc,
6706 obj_loc,
6707 class_offset,
6708 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006709 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006710
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006711 // Do an exact check.
6712 __ cmp(temp, ShifterOperand(cls));
6713 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006714
6715 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006716 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006717 GenerateReferenceLoadOneRegister(instruction,
6718 temp_loc,
6719 component_offset,
6720 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006721 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006722 // If the component type is null, jump to the slow path to throw the exception.
6723 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6724 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6725 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006726 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006727 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006728 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006729 break;
6730 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006731
Calin Juravle98893e12015-10-02 21:05:03 +01006732 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006733 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00006734 // We cannot directly call the CheckCast runtime entry point
6735 // without resorting to a type checking slow path here (i.e. by
6736 // calling InvokeRuntime directly), as it would require to
6737 // assign fixed registers for the inputs of this HInstanceOf
6738 // instruction (following the runtime calling convention), which
6739 // might be cluttered by the potential first read barrier
6740 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006741
Roland Levillain3b359c72015-11-17 19:35:12 +00006742 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006743 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006744
6745 case TypeCheckKind::kInterfaceCheck: {
6746 // Avoid read barriers to improve performance of the fast path. We can not get false
6747 // positives by doing this.
6748 // /* HeapReference<Class> */ temp = obj->klass_
6749 GenerateReferenceLoadTwoRegisters(instruction,
6750 temp_loc,
6751 obj_loc,
6752 class_offset,
6753 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006754 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006755
6756 // /* HeapReference<Class> */ temp = temp->iftable_
6757 GenerateReferenceLoadTwoRegisters(instruction,
6758 temp_loc,
6759 temp_loc,
6760 iftable_offset,
6761 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006762 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006763 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006764 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006765 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006766 Label start_loop;
6767 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006768 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
6769 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006770 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
6771 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006772 // Go to next interface.
6773 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
6774 __ sub(maybe_temp2_loc.AsRegister<Register>(),
6775 maybe_temp2_loc.AsRegister<Register>(),
6776 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006777 // Compare the classes and continue the loop if they do not match.
6778 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
6779 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006780 break;
6781 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006782 }
6783 __ Bind(&done);
6784
Roland Levillain3b359c72015-11-17 19:35:12 +00006785 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006786}
6787
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006788void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6789 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006790 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006791 InvokeRuntimeCallingConvention calling_convention;
6792 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6793}
6794
6795void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006796 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6797 instruction,
6798 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006799 if (instruction->IsEnter()) {
6800 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6801 } else {
6802 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6803 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006804}
6805
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006806void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6807void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6808void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006809
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006810void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006811 LocationSummary* locations =
6812 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6813 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6814 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006815 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006816 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006817 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006818 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006819}
6820
6821void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6822 HandleBitwiseOperation(instruction);
6823}
6824
6825void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6826 HandleBitwiseOperation(instruction);
6827}
6828
6829void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6830 HandleBitwiseOperation(instruction);
6831}
6832
Artem Serov7fc63502016-02-09 17:15:29 +00006833
6834void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6835 LocationSummary* locations =
6836 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6837 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6838 || instruction->GetResultType() == Primitive::kPrimLong);
6839
6840 locations->SetInAt(0, Location::RequiresRegister());
6841 locations->SetInAt(1, Location::RequiresRegister());
6842 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6843}
6844
6845void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6846 LocationSummary* locations = instruction->GetLocations();
6847 Location first = locations->InAt(0);
6848 Location second = locations->InAt(1);
6849 Location out = locations->Out();
6850
6851 if (instruction->GetResultType() == Primitive::kPrimInt) {
6852 Register first_reg = first.AsRegister<Register>();
6853 ShifterOperand second_reg(second.AsRegister<Register>());
6854 Register out_reg = out.AsRegister<Register>();
6855
6856 switch (instruction->GetOpKind()) {
6857 case HInstruction::kAnd:
6858 __ bic(out_reg, first_reg, second_reg);
6859 break;
6860 case HInstruction::kOr:
6861 __ orn(out_reg, first_reg, second_reg);
6862 break;
6863 // There is no EON on arm.
6864 case HInstruction::kXor:
6865 default:
6866 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6867 UNREACHABLE();
6868 }
6869 return;
6870
6871 } else {
6872 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6873 Register first_low = first.AsRegisterPairLow<Register>();
6874 Register first_high = first.AsRegisterPairHigh<Register>();
6875 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6876 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6877 Register out_low = out.AsRegisterPairLow<Register>();
6878 Register out_high = out.AsRegisterPairHigh<Register>();
6879
6880 switch (instruction->GetOpKind()) {
6881 case HInstruction::kAnd:
6882 __ bic(out_low, first_low, second_low);
6883 __ bic(out_high, first_high, second_high);
6884 break;
6885 case HInstruction::kOr:
6886 __ orn(out_low, first_low, second_low);
6887 __ orn(out_high, first_high, second_high);
6888 break;
6889 // There is no EON on arm.
6890 case HInstruction::kXor:
6891 default:
6892 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6893 UNREACHABLE();
6894 }
6895 }
6896}
6897
Anton Kirilov74234da2017-01-13 14:42:47 +00006898void LocationsBuilderARM::VisitDataProcWithShifterOp(
6899 HDataProcWithShifterOp* instruction) {
6900 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
6901 instruction->GetType() == Primitive::kPrimLong);
6902 LocationSummary* locations =
6903 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6904 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
6905 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
6906
6907 locations->SetInAt(0, Location::RequiresRegister());
6908 locations->SetInAt(1, Location::RequiresRegister());
6909 locations->SetOut(Location::RequiresRegister(),
6910 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6911}
6912
6913void InstructionCodeGeneratorARM::VisitDataProcWithShifterOp(
6914 HDataProcWithShifterOp* instruction) {
6915 const LocationSummary* const locations = instruction->GetLocations();
6916 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
6917 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
6918 const Location left = locations->InAt(0);
6919 const Location right = locations->InAt(1);
6920 const Location out = locations->Out();
6921
6922 if (instruction->GetType() == Primitive::kPrimInt) {
6923 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
6924
6925 const Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
6926 ? right.AsRegisterPairLow<Register>()
6927 : right.AsRegister<Register>();
6928
6929 GenerateDataProcInstruction(kind,
6930 out.AsRegister<Register>(),
6931 left.AsRegister<Register>(),
6932 ShifterOperand(second,
6933 ShiftFromOpKind(op_kind),
6934 instruction->GetShiftAmount()),
6935 codegen_);
6936 } else {
6937 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
6938
6939 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
6940 const Register second = right.AsRegister<Register>();
6941
6942 DCHECK_NE(out.AsRegisterPairLow<Register>(), second);
6943 GenerateDataProc(kind,
6944 out,
6945 left,
6946 ShifterOperand(second),
6947 ShifterOperand(second, ASR, 31),
6948 codegen_);
6949 } else {
6950 GenerateLongDataProc(instruction, codegen_);
6951 }
6952 }
6953}
6954
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006955void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6956 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6957 if (value == 0xffffffffu) {
6958 if (out != first) {
6959 __ mov(out, ShifterOperand(first));
6960 }
6961 return;
6962 }
6963 if (value == 0u) {
6964 __ mov(out, ShifterOperand(0));
6965 return;
6966 }
6967 ShifterOperand so;
6968 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6969 __ and_(out, first, so);
6970 } else {
6971 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6972 __ bic(out, first, ShifterOperand(~value));
6973 }
6974}
6975
6976void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6977 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6978 if (value == 0u) {
6979 if (out != first) {
6980 __ mov(out, ShifterOperand(first));
6981 }
6982 return;
6983 }
6984 if (value == 0xffffffffu) {
6985 __ mvn(out, ShifterOperand(0));
6986 return;
6987 }
6988 ShifterOperand so;
6989 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6990 __ orr(out, first, so);
6991 } else {
6992 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6993 __ orn(out, first, ShifterOperand(~value));
6994 }
6995}
6996
6997void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
6998 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
6999 if (value == 0u) {
7000 if (out != first) {
7001 __ mov(out, ShifterOperand(first));
7002 }
7003 return;
7004 }
7005 __ eor(out, first, ShifterOperand(value));
7006}
7007
Vladimir Marko59751a72016-08-05 14:37:27 +01007008void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
7009 Location first,
7010 uint64_t value) {
7011 Register out_low = out.AsRegisterPairLow<Register>();
7012 Register out_high = out.AsRegisterPairHigh<Register>();
7013 Register first_low = first.AsRegisterPairLow<Register>();
7014 Register first_high = first.AsRegisterPairHigh<Register>();
7015 uint32_t value_low = Low32Bits(value);
7016 uint32_t value_high = High32Bits(value);
7017 if (value_low == 0u) {
7018 if (out_low != first_low) {
7019 __ mov(out_low, ShifterOperand(first_low));
7020 }
7021 __ AddConstant(out_high, first_high, value_high);
7022 return;
7023 }
7024 __ AddConstantSetFlags(out_low, first_low, value_low);
7025 ShifterOperand so;
7026 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
7027 __ adc(out_high, first_high, so);
7028 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
7029 __ sbc(out_high, first_high, so);
7030 } else {
7031 LOG(FATAL) << "Unexpected constant " << value_high;
7032 UNREACHABLE();
7033 }
7034}
7035
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007036void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
7037 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007038 Location first = locations->InAt(0);
7039 Location second = locations->InAt(1);
7040 Location out = locations->Out();
7041
7042 if (second.IsConstant()) {
7043 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7044 uint32_t value_low = Low32Bits(value);
7045 if (instruction->GetResultType() == Primitive::kPrimInt) {
7046 Register first_reg = first.AsRegister<Register>();
7047 Register out_reg = out.AsRegister<Register>();
7048 if (instruction->IsAnd()) {
7049 GenerateAndConst(out_reg, first_reg, value_low);
7050 } else if (instruction->IsOr()) {
7051 GenerateOrrConst(out_reg, first_reg, value_low);
7052 } else {
7053 DCHECK(instruction->IsXor());
7054 GenerateEorConst(out_reg, first_reg, value_low);
7055 }
7056 } else {
7057 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7058 uint32_t value_high = High32Bits(value);
7059 Register first_low = first.AsRegisterPairLow<Register>();
7060 Register first_high = first.AsRegisterPairHigh<Register>();
7061 Register out_low = out.AsRegisterPairLow<Register>();
7062 Register out_high = out.AsRegisterPairHigh<Register>();
7063 if (instruction->IsAnd()) {
7064 GenerateAndConst(out_low, first_low, value_low);
7065 GenerateAndConst(out_high, first_high, value_high);
7066 } else if (instruction->IsOr()) {
7067 GenerateOrrConst(out_low, first_low, value_low);
7068 GenerateOrrConst(out_high, first_high, value_high);
7069 } else {
7070 DCHECK(instruction->IsXor());
7071 GenerateEorConst(out_low, first_low, value_low);
7072 GenerateEorConst(out_high, first_high, value_high);
7073 }
7074 }
7075 return;
7076 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007077
7078 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007079 Register first_reg = first.AsRegister<Register>();
7080 ShifterOperand second_reg(second.AsRegister<Register>());
7081 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007082 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007083 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007084 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007085 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007086 } else {
7087 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007088 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007089 }
7090 } else {
7091 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007092 Register first_low = first.AsRegisterPairLow<Register>();
7093 Register first_high = first.AsRegisterPairHigh<Register>();
7094 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7095 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7096 Register out_low = out.AsRegisterPairLow<Register>();
7097 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007098 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007099 __ and_(out_low, first_low, second_low);
7100 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007101 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007102 __ orr(out_low, first_low, second_low);
7103 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007104 } else {
7105 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007106 __ eor(out_low, first_low, second_low);
7107 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007108 }
7109 }
7110}
7111
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007112void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
7113 HInstruction* instruction,
7114 Location out,
7115 uint32_t offset,
7116 Location maybe_temp,
7117 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007118 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007119 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007120 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007121 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007122 if (kUseBakerReadBarrier) {
7123 // Load with fast path based Baker's read barrier.
7124 // /* HeapReference<Object> */ out = *(out + offset)
7125 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007126 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007127 } else {
7128 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007129 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00007130 // in the following move operation, as we will need it for the
7131 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007132 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00007133 // /* HeapReference<Object> */ out = *(out + offset)
7134 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007135 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00007136 }
7137 } else {
7138 // Plain load with no read barrier.
7139 // /* HeapReference<Object> */ out = *(out + offset)
7140 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7141 __ MaybeUnpoisonHeapReference(out_reg);
7142 }
7143}
7144
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007145void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
7146 HInstruction* instruction,
7147 Location out,
7148 Location obj,
7149 uint32_t offset,
7150 Location maybe_temp,
7151 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007152 Register out_reg = out.AsRegister<Register>();
7153 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007154 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007155 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007156 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007157 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007158 // Load with fast path based Baker's read barrier.
7159 // /* HeapReference<Object> */ out = *(obj + offset)
7160 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007161 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007162 } else {
7163 // Load with slow path based read barrier.
7164 // /* HeapReference<Object> */ out = *(obj + offset)
7165 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7166 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7167 }
7168 } else {
7169 // Plain load with no read barrier.
7170 // /* HeapReference<Object> */ out = *(obj + offset)
7171 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7172 __ MaybeUnpoisonHeapReference(out_reg);
7173 }
7174}
7175
7176void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
7177 Location root,
7178 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007179 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007180 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007181 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007182 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007183 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007184 if (kUseBakerReadBarrier) {
7185 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillain35345a52017-02-27 14:32:08 +00007186 // Baker's read barrier are used:
Roland Levillainc9285912015-12-18 10:38:42 +00007187 //
Roland Levillain35345a52017-02-27 14:32:08 +00007188 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007189 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillain35345a52017-02-27 14:32:08 +00007190 // if (temp != null) {
7191 // root = temp(root)
Roland Levillainc9285912015-12-18 10:38:42 +00007192 // }
7193
7194 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7195 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7196 static_assert(
7197 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7198 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7199 "have different sizes.");
7200 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7201 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7202 "have different sizes.");
7203
Roland Levillain35345a52017-02-27 14:32:08 +00007204 // Slow path marking the GC root `root`.
7205 Location temp = Location::RegisterLocation(LR);
7206 SlowPathCodeARM* slow_path =
7207 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
7208 instruction,
7209 root,
7210 /*entrypoint*/ temp);
7211 codegen_->AddSlowPath(slow_path);
7212
7213 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7214 const int32_t entry_point_offset =
7215 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
7216 // Loading the entrypoint does not require a load acquire since it is only changed when
7217 // threads are suspended or running a checkpoint.
7218 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007219 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7220 // checking GetIsGcMarking.
7221 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00007222 __ Bind(slow_path->GetExitLabel());
7223 } else {
7224 // GC root loaded through a slow path for read barriers other
7225 // than Baker's.
7226 // /* GcRoot<mirror::Object>* */ root = obj + offset
7227 __ AddConstant(root_reg, obj, offset);
7228 // /* mirror::Object* */ root = root->Read()
7229 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7230 }
7231 } else {
7232 // Plain GC root load with no read barrier.
7233 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7234 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7235 // Note that GC roots are not affected by heap poisoning, thus we
7236 // do not have to unpoison `root_reg` here.
7237 }
7238}
7239
7240void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7241 Location ref,
7242 Register obj,
7243 uint32_t offset,
7244 Location temp,
7245 bool needs_null_check) {
7246 DCHECK(kEmitCompilerReadBarrier);
7247 DCHECK(kUseBakerReadBarrier);
7248
7249 // /* HeapReference<Object> */ ref = *(obj + offset)
7250 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01007251 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00007252 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007253 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007254}
7255
7256void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7257 Location ref,
7258 Register obj,
7259 uint32_t data_offset,
7260 Location index,
7261 Location temp,
7262 bool needs_null_check) {
7263 DCHECK(kEmitCompilerReadBarrier);
7264 DCHECK(kUseBakerReadBarrier);
7265
Roland Levillainbfea3352016-06-23 13:48:47 +01007266 static_assert(
7267 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7268 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007269 // /* HeapReference<Object> */ ref =
7270 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01007271 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00007272 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007273 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007274}
7275
7276void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7277 Location ref,
7278 Register obj,
7279 uint32_t offset,
7280 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01007281 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00007282 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007283 bool needs_null_check,
7284 bool always_update_field,
7285 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00007286 DCHECK(kEmitCompilerReadBarrier);
7287 DCHECK(kUseBakerReadBarrier);
7288
Roland Levillain35345a52017-02-27 14:32:08 +00007289 // In slow path based read barriers, the read barrier call is
7290 // inserted after the original load. However, in fast path based
7291 // Baker's read barriers, we need to perform the load of
7292 // mirror::Object::monitor_ *before* the original reference load.
7293 // This load-load ordering is required by the read barrier.
7294 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillainc9285912015-12-18 10:38:42 +00007295 //
Roland Levillain35345a52017-02-27 14:32:08 +00007296 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7297 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7298 // HeapReference<Object> ref = *src; // Original reference load.
7299 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7300 // if (is_gray) {
7301 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007302 // }
Roland Levillain35345a52017-02-27 14:32:08 +00007303 //
7304 // Note: the original implementation in ReadBarrier::Barrier is
7305 // slightly more complex as it performs additional checks that we do
7306 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00007307
Roland Levillain1372c9f2017-01-13 11:47:39 +00007308 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain35345a52017-02-27 14:32:08 +00007309 Register temp_reg = temp.AsRegister<Register>();
7310 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Roland Levillain1372c9f2017-01-13 11:47:39 +00007311
Roland Levillain35345a52017-02-27 14:32:08 +00007312 // /* int32_t */ monitor = obj->monitor_
7313 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7314 if (needs_null_check) {
7315 MaybeRecordImplicitNullCheck(instruction);
7316 }
7317 // /* LockWord */ lock_word = LockWord(monitor)
7318 static_assert(sizeof(LockWord) == sizeof(int32_t),
7319 "art::LockWord and int32_t have different sizes.");
7320
7321 // Introduce a dependency on the lock_word including the rb_state,
7322 // which shall prevent load-load reordering without using
7323 // a memory barrier (which would be more expensive).
7324 // `obj` is unchanged by this operation, but its value now depends
7325 // on `temp_reg`.
7326 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
7327
7328 // The actual reference load.
Roland Levillainc9285912015-12-18 10:38:42 +00007329 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007330 // Load types involving an "index": ArrayGet,
7331 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7332 // intrinsics.
Roland Levillain35345a52017-02-27 14:32:08 +00007333 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00007334 if (index.IsConstant()) {
7335 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01007336 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00007337 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7338 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01007339 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007340 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7341 // intrinsics, which use a register pair as index ("long
7342 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01007343 Register index_reg = index.IsRegisterPair()
7344 ? index.AsRegisterPairLow<Register>()
7345 : index.AsRegister<Register>();
7346 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00007347 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
7348 }
7349 } else {
Roland Levillain35345a52017-02-27 14:32:08 +00007350 // /* HeapReference<Object> */ ref = *(obj + offset)
Roland Levillainc9285912015-12-18 10:38:42 +00007351 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7352 }
7353
7354 // Object* ref = ref_addr->AsMirrorPtr()
7355 __ MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain35345a52017-02-27 14:32:08 +00007356
7357 // Slow path marking the object `ref` when it is gray.
7358 SlowPathCodeARM* slow_path;
7359 if (always_update_field) {
7360 DCHECK(temp2 != nullptr);
7361 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
7362 // of the form `obj + field_offset`, where `obj` is a register and
7363 // `field_offset` is a register pair (of which only the lower half
7364 // is used). Thus `offset` and `scale_factor` above are expected
7365 // to be null in this code path.
7366 DCHECK_EQ(offset, 0u);
7367 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7368 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
7369 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7370 } else {
7371 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
7372 }
7373 AddSlowPath(slow_path);
7374
7375 // if (rb_state == ReadBarrier::GrayState())
7376 // ref = ReadBarrier::Mark(ref);
7377 // Given the numeric representation, it's enough to check the low bit of the
7378 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7379 // which can be a 16-bit instruction unlike the TST immediate.
7380 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7381 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7382 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7383 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
7384 __ Bind(slow_path->GetExitLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00007385}
7386
7387void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
7388 Location out,
7389 Location ref,
7390 Location obj,
7391 uint32_t offset,
7392 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007393 DCHECK(kEmitCompilerReadBarrier);
7394
Roland Levillainc9285912015-12-18 10:38:42 +00007395 // Insert a slow path based read barrier *after* the reference load.
7396 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007397 // If heap poisoning is enabled, the unpoisoning of the loaded
7398 // reference will be carried out by the runtime within the slow
7399 // path.
7400 //
7401 // Note that `ref` currently does not get unpoisoned (when heap
7402 // poisoning is enabled), which is alright as the `ref` argument is
7403 // not used by the artReadBarrierSlow entry point.
7404 //
7405 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007406 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00007407 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
7408 AddSlowPath(slow_path);
7409
Roland Levillain3b359c72015-11-17 19:35:12 +00007410 __ b(slow_path->GetEntryLabel());
7411 __ Bind(slow_path->GetExitLabel());
7412}
7413
Roland Levillainc9285912015-12-18 10:38:42 +00007414void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(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 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00007421 // Baker's read barriers shall be handled by the fast path
7422 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
7423 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007424 // If heap poisoning is enabled, unpoisoning will be taken care of
7425 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007426 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00007427 } else if (kPoisonHeapReferences) {
7428 __ UnpoisonHeapReference(out.AsRegister<Register>());
7429 }
7430}
7431
Roland Levillainc9285912015-12-18 10:38:42 +00007432void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7433 Location out,
7434 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007435 DCHECK(kEmitCompilerReadBarrier);
7436
Roland Levillainc9285912015-12-18 10:38:42 +00007437 // Insert a slow path based read barrier *after* the GC root load.
7438 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007439 // Note that GC roots are not affected by heap poisoning, so we do
7440 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007441 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007442 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
7443 AddSlowPath(slow_path);
7444
Roland Levillain3b359c72015-11-17 19:35:12 +00007445 __ b(slow_path->GetEntryLabel());
7446 __ Bind(slow_path->GetExitLabel());
7447}
7448
Vladimir Markodc151b22015-10-15 18:02:30 +01007449HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7450 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007451 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00007452 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007453}
7454
Vladimir Markob4536b72015-11-24 13:45:23 +00007455Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7456 Register temp) {
7457 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7458 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7459 if (!invoke->GetLocations()->Intrinsified()) {
7460 return location.AsRegister<Register>();
7461 }
7462 // For intrinsics we allow any location, so it may be on the stack.
7463 if (!location.IsRegister()) {
7464 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7465 return temp;
7466 }
7467 // For register locations, check if the register was saved. If so, get it from the stack.
7468 // Note: There is a chance that the register was saved but not overwritten, so we could
7469 // save one load. However, since this is just an intrinsic slow path we prefer this
7470 // simple and more robust approach rather that trying to determine if that's the case.
7471 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08007472 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00007473 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7474 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7475 return temp;
7476 }
7477 return location.AsRegister<Register>();
7478}
7479
TatWai Chongd8c052a2016-11-02 16:12:48 +08007480Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
7481 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007482 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7483 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007484 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7485 uint32_t offset =
7486 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007487 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007488 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007489 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007490 }
Vladimir Marko58155012015-08-19 12:49:41 +00007491 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007492 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007493 break;
7494 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7495 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7496 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007497 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7498 HArmDexCacheArraysBase* base =
7499 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7500 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7501 temp.AsRegister<Register>());
7502 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7503 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7504 break;
7505 }
Vladimir Marko58155012015-08-19 12:49:41 +00007506 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007507 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007508 Register method_reg;
7509 Register reg = temp.AsRegister<Register>();
7510 if (current_method.IsRegister()) {
7511 method_reg = current_method.AsRegister<Register>();
7512 } else {
7513 DCHECK(invoke->GetLocations()->Intrinsified());
7514 DCHECK(!current_method.IsValid());
7515 method_reg = reg;
7516 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7517 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007518 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7519 __ LoadFromOffset(kLoadWord,
7520 reg,
7521 method_reg,
7522 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007523 // temp = temp[index_in_cache];
7524 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7525 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007526 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7527 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007528 }
Vladimir Marko58155012015-08-19 12:49:41 +00007529 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08007530 return callee_method;
7531}
7532
7533void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
7534 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00007535
7536 switch (invoke->GetCodePtrLocation()) {
7537 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7538 __ bl(GetFrameEntryLabel());
7539 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007540 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7541 // LR = callee_method->entry_point_from_quick_compiled_code_
7542 __ LoadFromOffset(
7543 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007544 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007545 // LR()
7546 __ blx(LR);
7547 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007548 }
7549
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007550 DCHECK(!IsLeafMethod());
7551}
7552
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007553void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7554 Register temp = temp_location.AsRegister<Register>();
7555 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7556 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007557
7558 // Use the calling convention instead of the location of the receiver, as
7559 // intrinsics may have put the receiver in a different register. In the intrinsics
7560 // slow path, the arguments have been moved to the right place, so here we are
7561 // guaranteed that the receiver is the first register of the calling convention.
7562 InvokeDexCallingConvention calling_convention;
7563 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007564 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007565 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007566 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007567 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007568 // Instead of simply (possibly) unpoisoning `temp` here, we should
7569 // emit a read barrier for the previous class reference load.
7570 // However this is not required in practice, as this is an
7571 // intermediate/temporary reference and because the current
7572 // concurrent copying collector keeps the from-space memory
7573 // intact/accessible until the end of the marking phase (the
7574 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007575 __ MaybeUnpoisonHeapReference(temp);
7576 // temp = temp->GetMethodAt(method_offset);
7577 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007578 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007579 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7580 // LR = temp->GetEntryPoint();
7581 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7582 // LR();
7583 __ blx(LR);
7584}
7585
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007586CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007587 const DexFile& dex_file, dex::StringIndex string_index) {
7588 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007589}
7590
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007591CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08007592 const DexFile& dex_file, dex::TypeIndex type_index) {
7593 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007594}
7595
Vladimir Marko1998cd02017-01-13 13:02:58 +00007596CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
7597 const DexFile& dex_file, dex::TypeIndex type_index) {
7598 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
7599}
7600
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007601CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7602 const DexFile& dex_file, uint32_t element_offset) {
7603 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7604}
7605
7606CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7607 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7608 patches->emplace_back(dex_file, offset_or_index);
7609 return &patches->back();
7610}
7611
7612Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007613 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007614 return boot_image_string_patches_.GetOrCreate(
7615 StringReference(&dex_file, string_index),
7616 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7617}
7618
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007619Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007620 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007621 return boot_image_type_patches_.GetOrCreate(
7622 TypeReference(&dex_file, type_index),
7623 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7624}
7625
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007626Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00007627 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007628}
7629
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007630Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007631 dex::StringIndex string_index,
7632 Handle<mirror::String> handle) {
7633 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7634 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007635 return jit_string_patches_.GetOrCreate(
7636 StringReference(&dex_file, string_index),
7637 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7638}
7639
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007640Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
7641 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007642 Handle<mirror::Class> handle) {
7643 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
7644 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007645 return jit_class_patches_.GetOrCreate(
7646 TypeReference(&dex_file, type_index),
7647 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7648}
7649
Vladimir Markoaad75c62016-10-03 08:46:48 +00007650template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7651inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7652 const ArenaDeque<PcRelativePatchInfo>& infos,
7653 ArenaVector<LinkerPatch>* linker_patches) {
7654 for (const PcRelativePatchInfo& info : infos) {
7655 const DexFile& dex_file = info.target_dex_file;
7656 size_t offset_or_index = info.offset_or_index;
7657 DCHECK(info.add_pc_label.IsBound());
7658 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7659 // Add MOVW patch.
7660 DCHECK(info.movw_label.IsBound());
7661 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7662 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7663 // Add MOVT patch.
7664 DCHECK(info.movt_label.IsBound());
7665 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7666 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7667 }
7668}
7669
Vladimir Marko58155012015-08-19 12:49:41 +00007670void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7671 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007672 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00007673 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007674 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007675 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007676 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007677 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00007678 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007679 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007680 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7681 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007682 for (const auto& entry : boot_image_string_patches_) {
7683 const StringReference& target_string = entry.first;
7684 Literal* literal = entry.second;
7685 DCHECK(literal->GetLabel()->IsBound());
7686 uint32_t literal_offset = literal->GetLabel()->Position();
7687 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7688 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007689 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007690 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007691 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00007692 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007693 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7694 linker_patches);
7695 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007696 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7697 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007698 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7699 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007700 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007701 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
7702 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007703 for (const auto& entry : boot_image_type_patches_) {
7704 const TypeReference& target_type = entry.first;
7705 Literal* literal = entry.second;
7706 DCHECK(literal->GetLabel()->IsBound());
7707 uint32_t literal_offset = literal->GetLabel()->Position();
7708 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7709 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007710 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007711 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007712 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007713}
7714
7715Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7716 return map->GetOrCreate(
7717 value,
7718 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007719}
7720
7721Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7722 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007723 return map->GetOrCreate(
7724 target_method,
7725 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007726}
7727
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007728void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7729 LocationSummary* locations =
7730 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7731 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7732 Location::RequiresRegister());
7733 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7734 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7735 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7736}
7737
7738void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7739 LocationSummary* locations = instr->GetLocations();
7740 Register res = locations->Out().AsRegister<Register>();
7741 Register accumulator =
7742 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7743 Register mul_left =
7744 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7745 Register mul_right =
7746 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7747
7748 if (instr->GetOpKind() == HInstruction::kAdd) {
7749 __ mla(res, mul_left, mul_right, accumulator);
7750 } else {
7751 __ mls(res, mul_left, mul_right, accumulator);
7752 }
7753}
7754
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007755void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007756 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007757 LOG(FATAL) << "Unreachable";
7758}
7759
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007760void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007761 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007762 LOG(FATAL) << "Unreachable";
7763}
7764
Mark Mendellfe57faa2015-09-18 09:26:15 -04007765// Simple implementation of packed switch - generate cascaded compare/jumps.
7766void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7767 LocationSummary* locations =
7768 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7769 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007770 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007771 codegen_->GetAssembler()->IsThumb()) {
7772 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7773 if (switch_instr->GetStartValue() != 0) {
7774 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7775 }
7776 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007777}
7778
7779void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7780 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007781 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007782 LocationSummary* locations = switch_instr->GetLocations();
7783 Register value_reg = locations->InAt(0).AsRegister<Register>();
7784 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7785
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007786 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007787 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007788 Register temp_reg = IP;
7789 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7790 // the immediate, because IP is used as the destination register. For the other
7791 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7792 // and they can be encoded in the instruction without making use of IP register.
7793 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7794
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007795 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007796 // Jump to successors[0] if value == lower_bound.
7797 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7798 int32_t last_index = 0;
7799 for (; num_entries - last_index > 2; last_index += 2) {
7800 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7801 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7802 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7803 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7804 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7805 }
7806 if (num_entries - last_index == 2) {
7807 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007808 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007809 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007810 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007811
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007812 // And the default for any other value.
7813 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7814 __ b(codegen_->GetLabelOf(default_block));
7815 }
7816 } else {
7817 // Create a table lookup.
7818 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7819
7820 // Materialize a pointer to the switch table
7821 std::vector<Label*> labels(num_entries);
7822 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7823 for (uint32_t i = 0; i < num_entries; i++) {
7824 labels[i] = codegen_->GetLabelOf(successors[i]);
7825 }
7826 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7827
7828 // Remove the bias.
7829 Register key_reg;
7830 if (lower_bound != 0) {
7831 key_reg = locations->GetTemp(1).AsRegister<Register>();
7832 __ AddConstant(key_reg, value_reg, -lower_bound);
7833 } else {
7834 key_reg = value_reg;
7835 }
7836
7837 // Check whether the value is in the table, jump to default block if not.
7838 __ CmpConstant(key_reg, num_entries - 1);
7839 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7840
7841 // Load the displacement from the table.
7842 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7843
7844 // Dispatch is a direct add to the PC (for Thumb2).
7845 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007846 }
7847}
7848
Vladimir Markob4536b72015-11-24 13:45:23 +00007849void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7850 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7851 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007852}
7853
7854void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7855 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007856 CodeGeneratorARM::PcRelativePatchInfo* labels =
7857 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007858 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007859 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007860 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007861 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007862 __ BindTrackedLabel(&labels->add_pc_label);
7863 __ add(base_reg, base_reg, ShifterOperand(PC));
7864}
7865
Andreas Gampe85b62f22015-09-09 13:15:38 -07007866void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7867 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007868 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007869 return;
7870 }
7871
7872 DCHECK_NE(type, Primitive::kPrimVoid);
7873
7874 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7875 if (return_loc.Equals(trg)) {
7876 return;
7877 }
7878
7879 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7880 // with the last branch.
7881 if (type == Primitive::kPrimLong) {
7882 HParallelMove parallel_move(GetGraph()->GetArena());
7883 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7884 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7885 GetMoveResolver()->EmitNativeCode(&parallel_move);
7886 } else if (type == Primitive::kPrimDouble) {
7887 HParallelMove parallel_move(GetGraph()->GetArena());
7888 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7889 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7890 GetMoveResolver()->EmitNativeCode(&parallel_move);
7891 } else {
7892 // Let the parallel move resolver take care of all of this.
7893 HParallelMove parallel_move(GetGraph()->GetArena());
7894 parallel_move.AddMove(return_loc, trg, type, nullptr);
7895 GetMoveResolver()->EmitNativeCode(&parallel_move);
7896 }
7897}
7898
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007899void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7900 LocationSummary* locations =
7901 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7902 locations->SetInAt(0, Location::RequiresRegister());
7903 locations->SetOut(Location::RequiresRegister());
7904}
7905
7906void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7907 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007908 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007909 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007910 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007911 __ LoadFromOffset(kLoadWord,
7912 locations->Out().AsRegister<Register>(),
7913 locations->InAt(0).AsRegister<Register>(),
7914 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007915 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007916 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007917 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007918 __ LoadFromOffset(kLoadWord,
7919 locations->Out().AsRegister<Register>(),
7920 locations->InAt(0).AsRegister<Register>(),
7921 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7922 __ LoadFromOffset(kLoadWord,
7923 locations->Out().AsRegister<Register>(),
7924 locations->Out().AsRegister<Register>(),
7925 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007926 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007927}
7928
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007929static void PatchJitRootUse(uint8_t* code,
7930 const uint8_t* roots_data,
7931 Literal* literal,
7932 uint64_t index_in_table) {
7933 DCHECK(literal->GetLabel()->IsBound());
7934 uint32_t literal_offset = literal->GetLabel()->Position();
7935 uintptr_t address =
7936 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7937 uint8_t* data = code + literal_offset;
7938 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7939}
7940
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007941void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7942 for (const auto& entry : jit_string_patches_) {
7943 const auto& it = jit_string_roots_.find(entry.first);
7944 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007945 PatchJitRootUse(code, roots_data, entry.second, it->second);
7946 }
7947 for (const auto& entry : jit_class_patches_) {
7948 const auto& it = jit_class_roots_.find(entry.first);
7949 DCHECK(it != jit_class_roots_.end());
7950 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007951 }
7952}
7953
Roland Levillain4d027112015-07-01 15:41:14 +01007954#undef __
7955#undef QUICK_ENTRY_POINT
7956
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007957} // namespace arm
7958} // namespace art