blob: 7b84ef83cdf43da0a7085276c5e24cbd3b4d703b [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 Levillaina1aa3b12016-10-26 13:03:38 +0100639// 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).
643//
644// 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`).
Roland Levillaind966ce72017-02-09 16:20:14 +0000650//
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
653// is for the GcRoot read barrier.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100654class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
Roland Levillainc9285912015-12-18 10:38:42 +0000655 public:
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800656 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
657 Location ref,
658 Location entrypoint = Location::NoLocation())
659 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
Roland Levillainc9285912015-12-18 10:38:42 +0000660 DCHECK(kEmitCompilerReadBarrier);
661 }
662
663 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
664
665 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
666 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100667 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +0000668 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100669 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillainc9285912015-12-18 10:38:42 +0000670 DCHECK(instruction_->IsInstanceFieldGet() ||
671 instruction_->IsStaticFieldGet() ||
672 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100673 instruction_->IsArraySet() ||
Roland Levillainc9285912015-12-18 10:38:42 +0000674 instruction_->IsLoadClass() ||
675 instruction_->IsLoadString() ||
676 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100677 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100678 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
679 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +0000680 << "Unexpected instruction in read barrier marking slow path: "
681 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000682 // 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()));
Roland Levillainc9285912015-12-18 10:38:42 +0000687
688 __ Bind(GetEntryLabel());
Roland Levillain4359e612016-07-20 11:32:19 +0100689 // 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.
Roland Levillainc9285912015-12-18 10:38:42 +0000692 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100693 DCHECK_NE(ref_reg, SP);
694 DCHECK_NE(ref_reg, LR);
695 DCHECK_NE(ref_reg, PC);
Roland Levillain0b671c02016-08-19 12:02:34 +0100696 // IP is used internally by the ReadBarrierMarkRegX entry point
697 // as a temporary, it cannot be the entry point's input/output.
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100698 DCHECK_NE(ref_reg, IP);
699 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100700 // "Compact" slow path, saving two moves.
701 //
702 // Instead of using the standard runtime calling convention (input
703 // and output in R0):
704 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100705 // R0 <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100706 // R0 <- ReadBarrierMark(R0)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100707 // ref <- R0
Roland Levillain02b75802016-07-13 11:54:35 +0100708 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100709 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100710 // of a dedicated entrypoint:
711 //
712 // rX <- ReadBarrierMarkRegX(rX)
713 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800714 if (entrypoint_.IsValid()) {
715 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
716 __ blx(entrypoint_.AsRegister<Register>());
717 } else {
718 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 }
Roland Levillainc9285912015-12-18 10:38:42 +0000723 __ b(GetExitLabel());
724 }
725
726 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100727 // The location (register) of the marked object reference.
728 const Location ref_;
Roland Levillainc9285912015-12-18 10:38:42 +0000729
Mathieu Chartierfe814e82016-11-09 14:32:49 -0800730 // The location of the entrypoint if already loaded.
731 const Location entrypoint_;
732
Roland Levillainc9285912015-12-18 10:38:42 +0000733 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
734};
735
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100736// 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`).
746class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
747 public:
748 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
749 Location ref,
750 Register obj,
751 Location field_offset,
752 Register temp1,
753 Register temp2)
754 : SlowPathCodeARM(instruction),
755 ref_(ref),
756 obj_(obj),
757 field_offset_(field_offset),
758 temp1_(temp1),
759 temp2_(temp2) {
760 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);
786
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 //
812 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);
816
817 // If the new reference is different from the old reference,
818 // update the field in the holder (`*(obj_ + field_offset_)`).
819 //
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.
825 Label done;
826 __ cmp(temp1_, ShifterOperand(ref_reg));
827 __ b(&done, EQ);
828
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.
840 Register offset = field_offset_.AsRegisterPairLow<Register>();
841 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
890 __ Bind(&done);
891 __ b(GetExitLabel());
892 }
893
894 private:
895 // The location (register) of the marked object reference.
896 const Location ref_;
897 // The register containing the object holding the marked object reference field.
898 const Register obj_;
899 // The location of the offset of the marked reference field within `obj_`.
900 Location field_offset_;
901
902 const Register temp1_;
903 const Register temp2_;
904
905 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
906};
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)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001440 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001441 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1442 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001443 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1444 jit_class_patches_(TypeReferenceValueComparator(),
1445 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001446 // Always save the LR register to mimic Quick.
1447 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001448}
1449
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001450void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1451 // Ensure that we fix up branches and literal loads and emit the literal pool.
1452 __ FinalizeCode();
1453
1454 // Adjust native pc offsets in stack maps.
1455 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001456 uint32_t old_position =
1457 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001458 uint32_t new_position = __ GetAdjustedPosition(old_position);
1459 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1460 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001461 // Adjust pc offsets for the disassembly information.
1462 if (disasm_info_ != nullptr) {
1463 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1464 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1465 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1466 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1467 it.second.start = __ GetAdjustedPosition(it.second.start);
1468 it.second.end = __ GetAdjustedPosition(it.second.end);
1469 }
1470 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1471 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1472 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1473 }
1474 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001475
1476 CodeGenerator::Finalize(allocator);
1477}
1478
David Brazdil58282f42016-01-14 12:45:10 +00001479void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001480 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001481 blocked_core_registers_[SP] = true;
1482 blocked_core_registers_[LR] = true;
1483 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001484
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001485 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001486 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001487
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001488 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001489 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001490
David Brazdil58282f42016-01-14 12:45:10 +00001491 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001492 // Stubs do not save callee-save floating point registers. If the graph
1493 // is debuggable, we need to deal with these registers differently. For
1494 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001495 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1496 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1497 }
1498 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001499}
1500
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001501InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001502 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001503 assembler_(codegen->GetAssembler()),
1504 codegen_(codegen) {}
1505
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001506void CodeGeneratorARM::ComputeSpillMask() {
1507 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1508 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001509 // There is no easy instruction to restore just the PC on thumb2. We spill and
1510 // restore another arbitrary register.
1511 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001512 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1513 // We use vpush and vpop for saving and restoring floating point registers, which take
1514 // a SRegister and the number of registers to save/restore after that SRegister. We
1515 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1516 // but in the range.
1517 if (fpu_spill_mask_ != 0) {
1518 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1519 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1520 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1521 fpu_spill_mask_ |= (1 << i);
1522 }
1523 }
1524}
1525
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001526static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001527 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001528}
1529
1530static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001531 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001532}
1533
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001534void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001535 bool skip_overflow_check =
1536 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001537 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001538 __ Bind(&frame_entry_label_);
1539
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001540 if (HasEmptyFrame()) {
1541 return;
1542 }
1543
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001544 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001545 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1546 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1547 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001548 }
1549
Andreas Gampe501fd632015-09-10 16:11:06 -07001550 __ PushList(core_spill_mask_);
1551 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1552 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001553 if (fpu_spill_mask_ != 0) {
1554 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1555 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001556 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001557 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001558 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001559
1560 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1561 // Initialize should_deoptimize flag to 0.
1562 __ mov(IP, ShifterOperand(0));
1563 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
1564 }
1565
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001566 int adjust = GetFrameSize() - FrameEntrySpillSize();
1567 __ AddConstant(SP, -adjust);
1568 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001569
1570 // Save the current method if we need it. Note that we do not
1571 // do this in HCurrentMethod, as the instruction might have been removed
1572 // in the SSA graph.
1573 if (RequiresCurrentMethod()) {
1574 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1575 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001576}
1577
1578void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001579 if (HasEmptyFrame()) {
1580 __ bx(LR);
1581 return;
1582 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001583 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001584 int adjust = GetFrameSize() - FrameEntrySpillSize();
1585 __ AddConstant(SP, adjust);
1586 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001587 if (fpu_spill_mask_ != 0) {
1588 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1589 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001590 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001591 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001592 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001593 // Pop LR into PC to return.
1594 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1595 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1596 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001597 __ cfi().RestoreState();
1598 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001599}
1600
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001601void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001602 Label* label = GetLabelOf(block);
1603 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001604}
1605
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001606Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001607 switch (type) {
1608 case Primitive::kPrimBoolean:
1609 case Primitive::kPrimByte:
1610 case Primitive::kPrimChar:
1611 case Primitive::kPrimShort:
1612 case Primitive::kPrimInt:
1613 case Primitive::kPrimNot: {
1614 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001615 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001616 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001617 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001618 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001619 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001620 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001621 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001622
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001623 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001624 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001625 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001626 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001627 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001628 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001629 if (calling_convention.GetRegisterAt(index) == R1) {
1630 // Skip R1, and use R2_R3 instead.
1631 gp_index_++;
1632 index++;
1633 }
1634 }
1635 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1636 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001637 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001638
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001639 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001640 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001641 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001642 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1643 }
1644 }
1645
1646 case Primitive::kPrimFloat: {
1647 uint32_t stack_index = stack_index_++;
1648 if (float_index_ % 2 == 0) {
1649 float_index_ = std::max(double_index_, float_index_);
1650 }
1651 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1652 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1653 } else {
1654 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1655 }
1656 }
1657
1658 case Primitive::kPrimDouble: {
1659 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1660 uint32_t stack_index = stack_index_;
1661 stack_index_ += 2;
1662 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1663 uint32_t index = double_index_;
1664 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001665 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001666 calling_convention.GetFpuRegisterAt(index),
1667 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001668 DCHECK(ExpectedPairLayout(result));
1669 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001670 } else {
1671 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001672 }
1673 }
1674
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001675 case Primitive::kPrimVoid:
1676 LOG(FATAL) << "Unexpected parameter type " << type;
1677 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001678 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001679 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001680}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001681
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001682Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001683 switch (type) {
1684 case Primitive::kPrimBoolean:
1685 case Primitive::kPrimByte:
1686 case Primitive::kPrimChar:
1687 case Primitive::kPrimShort:
1688 case Primitive::kPrimInt:
1689 case Primitive::kPrimNot: {
1690 return Location::RegisterLocation(R0);
1691 }
1692
1693 case Primitive::kPrimFloat: {
1694 return Location::FpuRegisterLocation(S0);
1695 }
1696
1697 case Primitive::kPrimLong: {
1698 return Location::RegisterPairLocation(R0, R1);
1699 }
1700
1701 case Primitive::kPrimDouble: {
1702 return Location::FpuRegisterPairLocation(S0, S1);
1703 }
1704
1705 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00001706 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001707 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001708
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001709 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001710}
1711
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001712Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
1713 return Location::RegisterLocation(kMethodRegisterArgument);
1714}
1715
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001716void CodeGeneratorARM::Move32(Location destination, Location source) {
1717 if (source.Equals(destination)) {
1718 return;
1719 }
1720 if (destination.IsRegister()) {
1721 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001722 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001723 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001724 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001725 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001726 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001727 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001728 } else if (destination.IsFpuRegister()) {
1729 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001730 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001731 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001732 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001733 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001734 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001735 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001736 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001737 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001738 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001739 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001740 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001741 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001742 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00001743 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001744 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
1745 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001746 }
1747 }
1748}
1749
1750void CodeGeneratorARM::Move64(Location destination, Location source) {
1751 if (source.Equals(destination)) {
1752 return;
1753 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001754 if (destination.IsRegisterPair()) {
1755 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001756 EmitParallelMoves(
1757 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1758 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001759 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001760 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001761 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1762 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001763 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001764 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001765 } else if (source.IsFpuRegisterPair()) {
1766 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
1767 destination.AsRegisterPairHigh<Register>(),
1768 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001769 } else {
1770 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001771 DCHECK(ExpectedPairLayout(destination));
1772 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
1773 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001774 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001775 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001776 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001777 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1778 SP,
1779 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01001780 } else if (source.IsRegisterPair()) {
1781 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
1782 source.AsRegisterPairLow<Register>(),
1783 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001784 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001785 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001786 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001787 } else {
1788 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001789 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001790 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001791 if (source.AsRegisterPairLow<Register>() == R1) {
1792 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001793 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
1794 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001795 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001796 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001797 SP, destination.GetStackIndex());
1798 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001799 } else if (source.IsFpuRegisterPair()) {
1800 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
1801 SP,
1802 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001803 } else {
1804 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001805 EmitParallelMoves(
1806 Location::StackSlot(source.GetStackIndex()),
1807 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001808 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001809 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001810 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
1811 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001812 }
1813 }
1814}
1815
Calin Juravle175dc732015-08-25 15:42:32 +01001816void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
1817 DCHECK(location.IsRegister());
1818 __ LoadImmediate(location.AsRegister<Register>(), value);
1819}
1820
Calin Juravlee460d1d2015-09-29 04:52:17 +01001821void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001822 HParallelMove move(GetGraph()->GetArena());
1823 move.AddMove(src, dst, dst_type, nullptr);
1824 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001825}
1826
1827void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
1828 if (location.IsRegister()) {
1829 locations->AddTemp(location);
1830 } else if (location.IsRegisterPair()) {
1831 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1832 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1833 } else {
1834 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1835 }
1836}
1837
Calin Juravle175dc732015-08-25 15:42:32 +01001838void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
1839 HInstruction* instruction,
1840 uint32_t dex_pc,
1841 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001842 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001843 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00001844 if (EntrypointRequiresStackMap(entrypoint)) {
1845 RecordPcInfo(instruction, dex_pc, slow_path);
1846 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01001847}
1848
Roland Levillaindec8f632016-07-22 17:10:06 +01001849void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1850 HInstruction* instruction,
1851 SlowPathCode* slow_path) {
1852 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001853 GenerateInvokeRuntime(entry_point_offset);
1854}
1855
1856void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01001857 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
1858 __ blx(LR);
1859}
1860
David Brazdilfc6a86a2015-06-26 10:33:45 +00001861void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001862 DCHECK(!successor->IsExitBlock());
1863
1864 HBasicBlock* block = got->GetBlock();
1865 HInstruction* previous = got->GetPrevious();
1866
1867 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001868 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001869 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
1870 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1871 return;
1872 }
1873
1874 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1875 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1876 }
1877 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001878 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001879 }
1880}
1881
David Brazdilfc6a86a2015-06-26 10:33:45 +00001882void LocationsBuilderARM::VisitGoto(HGoto* got) {
1883 got->SetLocations(nullptr);
1884}
1885
1886void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
1887 HandleGoto(got, got->GetSuccessor());
1888}
1889
1890void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1891 try_boundary->SetLocations(nullptr);
1892}
1893
1894void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
1895 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1896 if (!successor->IsExitBlock()) {
1897 HandleGoto(try_boundary, successor);
1898 }
1899}
1900
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001901void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001902 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001903}
1904
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001905void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001906}
1907
Vladimir Marko37dd80d2016-08-01 17:41:45 +01001908void InstructionCodeGeneratorARM::GenerateVcmp(HInstruction* instruction) {
1909 Primitive::Type type = instruction->InputAt(0)->GetType();
1910 Location lhs_loc = instruction->GetLocations()->InAt(0);
1911 Location rhs_loc = instruction->GetLocations()->InAt(1);
1912 if (rhs_loc.IsConstant()) {
1913 // 0.0 is the only immediate that can be encoded directly in
1914 // a VCMP instruction.
1915 //
1916 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1917 // specify that in a floating-point comparison, positive zero
1918 // and negative zero are considered equal, so we can use the
1919 // literal 0.0 for both cases here.
1920 //
1921 // Note however that some methods (Float.equal, Float.compare,
1922 // Float.compareTo, Double.equal, Double.compare,
1923 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1924 // StrictMath.min) consider 0.0 to be (strictly) greater than
1925 // -0.0. So if we ever translate calls to these methods into a
1926 // HCompare instruction, we must handle the -0.0 case with
1927 // care here.
1928 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1929 if (type == Primitive::kPrimFloat) {
1930 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1931 } else {
1932 DCHECK_EQ(type, Primitive::kPrimDouble);
1933 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1934 }
1935 } else {
1936 if (type == Primitive::kPrimFloat) {
1937 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1938 } else {
1939 DCHECK_EQ(type, Primitive::kPrimDouble);
1940 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1941 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1942 }
1943 }
1944}
1945
Roland Levillain4fa13f62015-07-06 18:11:54 +01001946void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
1947 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00001948 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001949 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00001950 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01001951}
1952
1953void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
1954 Label* true_label,
1955 Label* false_label) {
1956 LocationSummary* locations = cond->GetLocations();
1957 Location left = locations->InAt(0);
1958 Location right = locations->InAt(1);
1959 IfCondition if_cond = cond->GetCondition();
1960
1961 Register left_high = left.AsRegisterPairHigh<Register>();
1962 Register left_low = left.AsRegisterPairLow<Register>();
1963 IfCondition true_high_cond = if_cond;
1964 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001965 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01001966
1967 // Set the conditions for the test, remembering that == needs to be
1968 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07001969 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01001970 switch (if_cond) {
1971 case kCondEQ:
1972 case kCondNE:
1973 // Nothing to do.
1974 break;
1975 case kCondLT:
1976 false_high_cond = kCondGT;
1977 break;
1978 case kCondLE:
1979 true_high_cond = kCondLT;
1980 break;
1981 case kCondGT:
1982 false_high_cond = kCondLT;
1983 break;
1984 case kCondGE:
1985 true_high_cond = kCondGT;
1986 break;
Aart Bike9f37602015-10-09 11:15:55 -07001987 case kCondB:
1988 false_high_cond = kCondA;
1989 break;
1990 case kCondBE:
1991 true_high_cond = kCondB;
1992 break;
1993 case kCondA:
1994 false_high_cond = kCondB;
1995 break;
1996 case kCondAE:
1997 true_high_cond = kCondA;
1998 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01001999 }
2000 if (right.IsConstant()) {
2001 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
2002 int32_t val_low = Low32Bits(value);
2003 int32_t val_high = High32Bits(value);
2004
Vladimir Markoac6ac102015-12-17 12:14:00 +00002005 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002006 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002007 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002008 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002009 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002010 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002011 __ b(true_label, ARMCondition(true_high_cond));
2012 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002013 }
2014 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00002015 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002016 } else {
2017 Register right_high = right.AsRegisterPairHigh<Register>();
2018 Register right_low = right.AsRegisterPairLow<Register>();
2019
2020 __ cmp(left_high, ShifterOperand(right_high));
2021 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002022 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002023 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002024 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002025 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002026 __ b(true_label, ARMCondition(true_high_cond));
2027 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002028 }
2029 // Must be equal high, so compare the lows.
2030 __ cmp(left_low, ShifterOperand(right_low));
2031 }
2032 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07002033 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01002034 __ b(true_label, final_condition);
2035}
2036
David Brazdil0debae72015-11-12 18:37:00 +00002037void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
2038 Label* true_target_in,
2039 Label* false_target_in) {
2040 // Generated branching requires both targets to be explicit. If either of the
2041 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2042 Label fallthrough_target;
2043 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
2044 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
2045
Roland Levillain4fa13f62015-07-06 18:11:54 +01002046 Primitive::Type type = condition->InputAt(0)->GetType();
2047 switch (type) {
2048 case Primitive::kPrimLong:
2049 GenerateLongComparesAndJumps(condition, true_target, false_target);
2050 break;
2051 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002052 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002053 GenerateVcmp(condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002054 GenerateFPJumps(condition, true_target, false_target);
2055 break;
2056 default:
2057 LOG(FATAL) << "Unexpected compare type " << type;
2058 }
2059
David Brazdil0debae72015-11-12 18:37:00 +00002060 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002061 __ b(false_target);
2062 }
David Brazdil0debae72015-11-12 18:37:00 +00002063
2064 if (fallthrough_target.IsLinked()) {
2065 __ Bind(&fallthrough_target);
2066 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002067}
2068
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002069void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002070 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002071 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002072 Label* false_target) {
2073 HInstruction* cond = instruction->InputAt(condition_input_index);
2074
2075 if (true_target == nullptr && false_target == nullptr) {
2076 // Nothing to do. The code always falls through.
2077 return;
2078 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002079 // Constant condition, statically compared against "true" (integer value 1).
2080 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002081 if (true_target != nullptr) {
2082 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002083 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002084 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002085 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002086 if (false_target != nullptr) {
2087 __ b(false_target);
2088 }
2089 }
2090 return;
2091 }
2092
2093 // The following code generates these patterns:
2094 // (1) true_target == nullptr && false_target != nullptr
2095 // - opposite condition true => branch to false_target
2096 // (2) true_target != nullptr && false_target == nullptr
2097 // - condition true => branch to true_target
2098 // (3) true_target != nullptr && false_target != nullptr
2099 // - condition true => branch to true_target
2100 // - branch to false_target
2101 if (IsBooleanValueOrMaterializedCondition(cond)) {
2102 // Condition has been materialized, compare the output to 0.
2103 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2104 DCHECK(cond_val.IsRegister());
2105 if (true_target == nullptr) {
2106 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
2107 } else {
2108 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002109 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002110 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002111 // Condition has not been materialized. Use its inputs as the comparison and
2112 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002113 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002114
2115 // If this is a long or FP comparison that has been folded into
2116 // the HCondition, generate the comparison directly.
2117 Primitive::Type type = condition->InputAt(0)->GetType();
2118 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2119 GenerateCompareTestAndBranch(condition, true_target, false_target);
2120 return;
2121 }
2122
2123 LocationSummary* locations = cond->GetLocations();
2124 DCHECK(locations->InAt(0).IsRegister());
2125 Register left = locations->InAt(0).AsRegister<Register>();
2126 Location right = locations->InAt(1);
2127 if (right.IsRegister()) {
2128 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002129 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002130 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002131 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
David Brazdil0debae72015-11-12 18:37:00 +00002132 }
2133 if (true_target == nullptr) {
2134 __ b(false_target, ARMCondition(condition->GetOppositeCondition()));
2135 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04002136 __ b(true_target, ARMCondition(condition->GetCondition()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002137 }
Dave Allison20dfc792014-06-16 20:44:29 -07002138 }
David Brazdil0debae72015-11-12 18:37:00 +00002139
2140 // If neither branch falls through (case 3), the conditional branch to `true_target`
2141 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2142 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002143 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002144 }
2145}
2146
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002147void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002148 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2149 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002150 locations->SetInAt(0, Location::RequiresRegister());
2151 }
2152}
2153
2154void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002155 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2156 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2157 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2158 nullptr : codegen_->GetLabelOf(true_successor);
2159 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2160 nullptr : codegen_->GetLabelOf(false_successor);
2161 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002162}
2163
2164void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
2165 LocationSummary* locations = new (GetGraph()->GetArena())
2166 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002167 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002168 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002169 locations->SetInAt(0, Location::RequiresRegister());
2170 }
2171}
2172
2173void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01002174 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002175 GenerateTestAndBranch(deoptimize,
2176 /* condition_input_index */ 0,
2177 slow_path->GetEntryLabel(),
2178 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002179}
Dave Allison20dfc792014-06-16 20:44:29 -07002180
Mingyao Yang063fc772016-08-02 11:02:54 -07002181void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2182 LocationSummary* locations = new (GetGraph()->GetArena())
2183 LocationSummary(flag, LocationSummary::kNoCall);
2184 locations->SetOut(Location::RequiresRegister());
2185}
2186
2187void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2188 __ LoadFromOffset(kLoadWord,
2189 flag->GetLocations()->Out().AsRegister<Register>(),
2190 SP,
2191 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2192}
2193
David Brazdil74eb1b22015-12-14 11:44:01 +00002194void LocationsBuilderARM::VisitSelect(HSelect* select) {
2195 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
2196 if (Primitive::IsFloatingPointType(select->GetType())) {
2197 locations->SetInAt(0, Location::RequiresFpuRegister());
2198 locations->SetInAt(1, Location::RequiresFpuRegister());
2199 } else {
2200 locations->SetInAt(0, Location::RequiresRegister());
2201 locations->SetInAt(1, Location::RequiresRegister());
2202 }
2203 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
2204 locations->SetInAt(2, Location::RequiresRegister());
2205 }
2206 locations->SetOut(Location::SameAsFirstInput());
2207}
2208
2209void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
2210 LocationSummary* locations = select->GetLocations();
2211 Label false_target;
2212 GenerateTestAndBranch(select,
2213 /* condition_input_index */ 2,
2214 /* true_target */ nullptr,
2215 &false_target);
2216 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
2217 __ Bind(&false_target);
2218}
2219
David Srbecky0cf44932015-12-09 14:09:59 +00002220void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2221 new (GetGraph()->GetArena()) LocationSummary(info);
2222}
2223
David Srbeckyd28f4a02016-03-14 17:14:24 +00002224void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2225 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002226}
2227
2228void CodeGeneratorARM::GenerateNop() {
2229 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002230}
2231
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002232void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002233 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002234 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002235 // Handle the long/FP comparisons made in instruction simplification.
2236 switch (cond->InputAt(0)->GetType()) {
2237 case Primitive::kPrimLong:
2238 locations->SetInAt(0, Location::RequiresRegister());
2239 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002240 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002241 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2242 }
2243 break;
2244
2245 case Primitive::kPrimFloat:
2246 case Primitive::kPrimDouble:
2247 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002248 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002249 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002250 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2251 }
2252 break;
2253
2254 default:
2255 locations->SetInAt(0, Location::RequiresRegister());
2256 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002257 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2259 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002260 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002261}
2262
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002263void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002264 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002265 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002266 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002267
2268 LocationSummary* locations = cond->GetLocations();
2269 Location left = locations->InAt(0);
2270 Location right = locations->InAt(1);
2271 Register out = locations->Out().AsRegister<Register>();
2272 Label true_label, false_label;
2273
2274 switch (cond->InputAt(0)->GetType()) {
2275 default: {
2276 // Integer case.
2277 if (right.IsRegister()) {
2278 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2279 } else {
2280 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002281 __ CmpConstant(left.AsRegister<Register>(),
2282 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002283 }
Aart Bike9f37602015-10-09 11:15:55 -07002284 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002285 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002286 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002287 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002288 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002289 return;
2290 }
2291 case Primitive::kPrimLong:
2292 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2293 break;
2294 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002295 case Primitive::kPrimDouble:
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002296 GenerateVcmp(cond);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002297 GenerateFPJumps(cond, &true_label, &false_label);
2298 break;
2299 }
2300
2301 // Convert the jumps into the result.
2302 Label done_label;
2303
2304 // False case: result = 0.
2305 __ Bind(&false_label);
2306 __ LoadImmediate(out, 0);
2307 __ b(&done_label);
2308
2309 // True case: result = 1.
2310 __ Bind(&true_label);
2311 __ LoadImmediate(out, 1);
2312 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002313}
2314
2315void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002316 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002317}
2318
2319void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002320 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002321}
2322
2323void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002324 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002325}
2326
2327void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002328 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002329}
2330
2331void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002332 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002333}
2334
2335void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002336 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002337}
2338
2339void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002340 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002341}
2342
2343void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002344 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002345}
2346
2347void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002348 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002349}
2350
2351void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002352 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002353}
2354
2355void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002356 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002357}
2358
2359void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002360 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002361}
2362
Aart Bike9f37602015-10-09 11:15:55 -07002363void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002364 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002365}
2366
2367void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002368 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002369}
2370
2371void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002372 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002373}
2374
2375void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002376 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002377}
2378
2379void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002380 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002381}
2382
2383void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002384 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002385}
2386
2387void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002388 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002389}
2390
2391void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002392 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002393}
2394
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002395void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002396 LocationSummary* locations =
2397 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002398 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002399}
2400
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002401void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002402 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002403}
2404
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002405void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2406 LocationSummary* locations =
2407 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2408 locations->SetOut(Location::ConstantLocation(constant));
2409}
2410
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002411void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002412 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002413}
2414
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002415void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002416 LocationSummary* locations =
2417 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002418 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002419}
2420
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002421void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002422 // Will be generated at use site.
2423}
2424
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002425void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2426 LocationSummary* locations =
2427 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2428 locations->SetOut(Location::ConstantLocation(constant));
2429}
2430
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002431void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002432 // Will be generated at use site.
2433}
2434
2435void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2436 LocationSummary* locations =
2437 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2438 locations->SetOut(Location::ConstantLocation(constant));
2439}
2440
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002441void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002442 // Will be generated at use site.
2443}
2444
Calin Juravle27df7582015-04-17 19:12:31 +01002445void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2446 memory_barrier->SetLocations(nullptr);
2447}
2448
2449void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002450 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002451}
2452
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002453void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002454 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002455}
2456
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002457void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002458 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002459}
2460
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002461void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002462 LocationSummary* locations =
2463 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002464 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002465}
2466
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002467void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002468 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002469}
2470
Calin Juravle175dc732015-08-25 15:42:32 +01002471void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2472 // The trampoline uses the same calling convention as dex calling conventions,
2473 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2474 // the method_idx.
2475 HandleInvoke(invoke);
2476}
2477
2478void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2479 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2480}
2481
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002482void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002483 // Explicit clinit checks triggered by static invokes must have been pruned by
2484 // art::PrepareForRegisterAllocation.
2485 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002486
Vladimir Marko68c981f2016-08-26 13:13:33 +01002487 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002488 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002489 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2490 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2491 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002492 return;
2493 }
2494
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002495 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002496
2497 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2498 if (invoke->HasPcRelativeDexCache()) {
2499 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2500 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002501}
2502
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002503static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2504 if (invoke->GetLocations()->Intrinsified()) {
2505 IntrinsicCodeGeneratorARM intrinsic(codegen);
2506 intrinsic.Dispatch(invoke);
2507 return true;
2508 }
2509 return false;
2510}
2511
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002512void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002513 // Explicit clinit checks triggered by static invokes must have been pruned by
2514 // art::PrepareForRegisterAllocation.
2515 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002516
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002517 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2518 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002519 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002520
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002521 LocationSummary* locations = invoke->GetLocations();
2522 codegen_->GenerateStaticOrDirectCall(
2523 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002524 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002525}
2526
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002527void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002528 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002529 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002530}
2531
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002532void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002533 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002534 if (intrinsic.TryDispatch(invoke)) {
2535 return;
2536 }
2537
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002538 HandleInvoke(invoke);
2539}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002540
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002541void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002542 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2543 return;
2544 }
2545
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002546 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002547 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002548 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002549}
2550
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002551void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2552 HandleInvoke(invoke);
2553 // Add the hidden argument.
2554 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2555}
2556
2557void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2558 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002559 LocationSummary* locations = invoke->GetLocations();
2560 Register temp = locations->GetTemp(0).AsRegister<Register>();
2561 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002562 Location receiver = locations->InAt(0);
2563 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2564
Roland Levillain3b359c72015-11-17 19:35:12 +00002565 // Set the hidden argument. This is safe to do this here, as R12
2566 // won't be modified thereafter, before the `blx` (call) instruction.
2567 DCHECK_EQ(R12, hidden_reg);
2568 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002569
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002570 if (receiver.IsStackSlot()) {
2571 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002572 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002573 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2574 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002575 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002576 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002577 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002578 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002579 // Instead of simply (possibly) unpoisoning `temp` here, we should
2580 // emit a read barrier for the previous class reference load.
2581 // However this is not required in practice, as this is an
2582 // intermediate/temporary reference and because the current
2583 // concurrent copying collector keeps the from-space memory
2584 // intact/accessible until the end of the marking phase (the
2585 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002586 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002587 __ LoadFromOffset(kLoadWord, temp, temp,
2588 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
2589 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002590 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002591 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002592 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00002593 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07002594 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002595 // LR = temp->GetEntryPoint();
2596 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
2597 // LR();
2598 __ blx(LR);
2599 DCHECK(!codegen_->IsLeafMethod());
2600 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2601}
2602
Orion Hodsonac141392017-01-13 11:53:47 +00002603void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2604 HandleInvoke(invoke);
2605}
2606
2607void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
2608 codegen_->GenerateInvokePolymorphicCall(invoke);
2609}
2610
Roland Levillain88cb1752014-10-20 16:36:47 +01002611void LocationsBuilderARM::VisitNeg(HNeg* neg) {
2612 LocationSummary* locations =
2613 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2614 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002615 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01002616 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00002617 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2618 break;
2619 }
2620 case Primitive::kPrimLong: {
2621 locations->SetInAt(0, Location::RequiresRegister());
2622 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002623 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002624 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002625
Roland Levillain88cb1752014-10-20 16:36:47 +01002626 case Primitive::kPrimFloat:
2627 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002628 locations->SetInAt(0, Location::RequiresFpuRegister());
2629 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01002630 break;
2631
2632 default:
2633 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2634 }
2635}
2636
2637void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
2638 LocationSummary* locations = neg->GetLocations();
2639 Location out = locations->Out();
2640 Location in = locations->InAt(0);
2641 switch (neg->GetResultType()) {
2642 case Primitive::kPrimInt:
2643 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002644 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01002645 break;
2646
2647 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002648 DCHECK(in.IsRegisterPair());
2649 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
2650 __ rsbs(out.AsRegisterPairLow<Register>(),
2651 in.AsRegisterPairLow<Register>(),
2652 ShifterOperand(0));
2653 // We cannot emit an RSC (Reverse Subtract with Carry)
2654 // instruction here, as it does not exist in the Thumb-2
2655 // instruction set. We use the following approach
2656 // using SBC and SUB instead.
2657 //
2658 // out.hi = -C
2659 __ sbc(out.AsRegisterPairHigh<Register>(),
2660 out.AsRegisterPairHigh<Register>(),
2661 ShifterOperand(out.AsRegisterPairHigh<Register>()));
2662 // out.hi = out.hi - in.hi
2663 __ sub(out.AsRegisterPairHigh<Register>(),
2664 out.AsRegisterPairHigh<Register>(),
2665 ShifterOperand(in.AsRegisterPairHigh<Register>()));
2666 break;
2667
Roland Levillain88cb1752014-10-20 16:36:47 +01002668 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002669 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00002670 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002671 break;
2672
Roland Levillain88cb1752014-10-20 16:36:47 +01002673 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002674 DCHECK(in.IsFpuRegisterPair());
2675 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
2676 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01002677 break;
2678
2679 default:
2680 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2681 }
2682}
2683
Roland Levillaindff1f282014-11-05 14:15:05 +00002684void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002685 Primitive::Type result_type = conversion->GetResultType();
2686 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002687 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002688
Roland Levillain5b3ee562015-04-14 16:02:41 +01002689 // The float-to-long, double-to-long and long-to-float type conversions
2690 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002691 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01002692 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2693 && result_type == Primitive::kPrimLong)
2694 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002695 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002696 : LocationSummary::kNoCall;
2697 LocationSummary* locations =
2698 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2699
David Brazdilb2bd1c52015-03-25 11:17:37 +00002700 // The Java language does not allow treating boolean as an integral type but
2701 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002702
Roland Levillaindff1f282014-11-05 14:15:05 +00002703 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002704 case Primitive::kPrimByte:
2705 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002706 case Primitive::kPrimLong:
2707 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002708 case Primitive::kPrimBoolean:
2709 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002710 case Primitive::kPrimShort:
2711 case Primitive::kPrimInt:
2712 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002713 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002714 locations->SetInAt(0, Location::RequiresRegister());
2715 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2716 break;
2717
2718 default:
2719 LOG(FATAL) << "Unexpected type conversion from " << input_type
2720 << " to " << result_type;
2721 }
2722 break;
2723
Roland Levillain01a8d712014-11-14 16:27:39 +00002724 case Primitive::kPrimShort:
2725 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002726 case Primitive::kPrimLong:
2727 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002728 case Primitive::kPrimBoolean:
2729 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002730 case Primitive::kPrimByte:
2731 case Primitive::kPrimInt:
2732 case Primitive::kPrimChar:
2733 // Processing a Dex `int-to-short' instruction.
2734 locations->SetInAt(0, Location::RequiresRegister());
2735 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2736 break;
2737
2738 default:
2739 LOG(FATAL) << "Unexpected type conversion from " << input_type
2740 << " to " << result_type;
2741 }
2742 break;
2743
Roland Levillain946e1432014-11-11 17:35:19 +00002744 case Primitive::kPrimInt:
2745 switch (input_type) {
2746 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002747 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002748 locations->SetInAt(0, Location::Any());
2749 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2750 break;
2751
2752 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002753 // Processing a Dex `float-to-int' instruction.
2754 locations->SetInAt(0, Location::RequiresFpuRegister());
2755 locations->SetOut(Location::RequiresRegister());
2756 locations->AddTemp(Location::RequiresFpuRegister());
2757 break;
2758
Roland Levillain946e1432014-11-11 17:35:19 +00002759 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002760 // Processing a Dex `double-to-int' instruction.
2761 locations->SetInAt(0, Location::RequiresFpuRegister());
2762 locations->SetOut(Location::RequiresRegister());
2763 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002764 break;
2765
2766 default:
2767 LOG(FATAL) << "Unexpected type conversion from " << input_type
2768 << " to " << result_type;
2769 }
2770 break;
2771
Roland Levillaindff1f282014-11-05 14:15:05 +00002772 case Primitive::kPrimLong:
2773 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002774 case Primitive::kPrimBoolean:
2775 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002776 case Primitive::kPrimByte:
2777 case Primitive::kPrimShort:
2778 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002779 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002780 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002781 locations->SetInAt(0, Location::RequiresRegister());
2782 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2783 break;
2784
Roland Levillain624279f2014-12-04 11:54:28 +00002785 case Primitive::kPrimFloat: {
2786 // Processing a Dex `float-to-long' instruction.
2787 InvokeRuntimeCallingConvention calling_convention;
2788 locations->SetInAt(0, Location::FpuRegisterLocation(
2789 calling_convention.GetFpuRegisterAt(0)));
2790 locations->SetOut(Location::RegisterPairLocation(R0, R1));
2791 break;
2792 }
2793
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002794 case Primitive::kPrimDouble: {
2795 // Processing a Dex `double-to-long' instruction.
2796 InvokeRuntimeCallingConvention calling_convention;
2797 locations->SetInAt(0, Location::FpuRegisterPairLocation(
2798 calling_convention.GetFpuRegisterAt(0),
2799 calling_convention.GetFpuRegisterAt(1)));
2800 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00002801 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002802 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002803
2804 default:
2805 LOG(FATAL) << "Unexpected type conversion from " << input_type
2806 << " to " << result_type;
2807 }
2808 break;
2809
Roland Levillain981e4542014-11-14 11:47:14 +00002810 case Primitive::kPrimChar:
2811 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002812 case Primitive::kPrimLong:
2813 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002814 case Primitive::kPrimBoolean:
2815 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002816 case Primitive::kPrimByte:
2817 case Primitive::kPrimShort:
2818 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002819 // Processing a Dex `int-to-char' instruction.
2820 locations->SetInAt(0, Location::RequiresRegister());
2821 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2822 break;
2823
2824 default:
2825 LOG(FATAL) << "Unexpected type conversion from " << input_type
2826 << " to " << result_type;
2827 }
2828 break;
2829
Roland Levillaindff1f282014-11-05 14:15:05 +00002830 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002831 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002832 case Primitive::kPrimBoolean:
2833 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002834 case Primitive::kPrimByte:
2835 case Primitive::kPrimShort:
2836 case Primitive::kPrimInt:
2837 case Primitive::kPrimChar:
2838 // Processing a Dex `int-to-float' instruction.
2839 locations->SetInAt(0, Location::RequiresRegister());
2840 locations->SetOut(Location::RequiresFpuRegister());
2841 break;
2842
Roland Levillain5b3ee562015-04-14 16:02:41 +01002843 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00002844 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01002845 InvokeRuntimeCallingConvention calling_convention;
2846 locations->SetInAt(0, Location::RegisterPairLocation(
2847 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
2848 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00002849 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01002850 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002851
Roland Levillaincff13742014-11-17 14:32:17 +00002852 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002853 // Processing a Dex `double-to-float' instruction.
2854 locations->SetInAt(0, Location::RequiresFpuRegister());
2855 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002856 break;
2857
2858 default:
2859 LOG(FATAL) << "Unexpected type conversion from " << input_type
2860 << " to " << result_type;
2861 };
2862 break;
2863
Roland Levillaindff1f282014-11-05 14:15:05 +00002864 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002865 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002866 case Primitive::kPrimBoolean:
2867 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002868 case Primitive::kPrimByte:
2869 case Primitive::kPrimShort:
2870 case Primitive::kPrimInt:
2871 case Primitive::kPrimChar:
2872 // Processing a Dex `int-to-double' instruction.
2873 locations->SetInAt(0, Location::RequiresRegister());
2874 locations->SetOut(Location::RequiresFpuRegister());
2875 break;
2876
2877 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002878 // Processing a Dex `long-to-double' instruction.
2879 locations->SetInAt(0, Location::RequiresRegister());
2880 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01002881 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002882 locations->AddTemp(Location::RequiresFpuRegister());
2883 break;
2884
Roland Levillaincff13742014-11-17 14:32:17 +00002885 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002886 // Processing a Dex `float-to-double' instruction.
2887 locations->SetInAt(0, Location::RequiresFpuRegister());
2888 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002889 break;
2890
2891 default:
2892 LOG(FATAL) << "Unexpected type conversion from " << input_type
2893 << " to " << result_type;
2894 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002895 break;
2896
2897 default:
2898 LOG(FATAL) << "Unexpected type conversion from " << input_type
2899 << " to " << result_type;
2900 }
2901}
2902
2903void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
2904 LocationSummary* locations = conversion->GetLocations();
2905 Location out = locations->Out();
2906 Location in = locations->InAt(0);
2907 Primitive::Type result_type = conversion->GetResultType();
2908 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002909 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002910 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002911 case Primitive::kPrimByte:
2912 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002913 case Primitive::kPrimLong:
2914 // Type conversion from long to byte is a result of code transformations.
2915 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
2916 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002917 case Primitive::kPrimBoolean:
2918 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002919 case Primitive::kPrimShort:
2920 case Primitive::kPrimInt:
2921 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002922 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002923 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002924 break;
2925
2926 default:
2927 LOG(FATAL) << "Unexpected type conversion from " << input_type
2928 << " to " << result_type;
2929 }
2930 break;
2931
Roland Levillain01a8d712014-11-14 16:27:39 +00002932 case Primitive::kPrimShort:
2933 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002934 case Primitive::kPrimLong:
2935 // Type conversion from long to short is a result of code transformations.
2936 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
2937 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002938 case Primitive::kPrimBoolean:
2939 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002940 case Primitive::kPrimByte:
2941 case Primitive::kPrimInt:
2942 case Primitive::kPrimChar:
2943 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002944 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00002945 break;
2946
2947 default:
2948 LOG(FATAL) << "Unexpected type conversion from " << input_type
2949 << " to " << result_type;
2950 }
2951 break;
2952
Roland Levillain946e1432014-11-11 17:35:19 +00002953 case Primitive::kPrimInt:
2954 switch (input_type) {
2955 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002956 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002957 DCHECK(out.IsRegister());
2958 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002959 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002960 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002961 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00002962 } else {
2963 DCHECK(in.IsConstant());
2964 DCHECK(in.GetConstant()->IsLongConstant());
2965 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002966 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00002967 }
2968 break;
2969
Roland Levillain3f8f9362014-12-02 17:45:01 +00002970 case Primitive::kPrimFloat: {
2971 // Processing a Dex `float-to-int' instruction.
2972 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002973 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00002974 __ vmovrs(out.AsRegister<Register>(), temp);
2975 break;
2976 }
2977
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002978 case Primitive::kPrimDouble: {
2979 // Processing a Dex `double-to-int' instruction.
2980 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01002981 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002982 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00002983 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002984 }
Roland Levillain946e1432014-11-11 17:35:19 +00002985
2986 default:
2987 LOG(FATAL) << "Unexpected type conversion from " << input_type
2988 << " to " << result_type;
2989 }
2990 break;
2991
Roland Levillaindff1f282014-11-05 14:15:05 +00002992 case Primitive::kPrimLong:
2993 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002994 case Primitive::kPrimBoolean:
2995 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002996 case Primitive::kPrimByte:
2997 case Primitive::kPrimShort:
2998 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002999 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003000 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003001 DCHECK(out.IsRegisterPair());
3002 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003003 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00003004 // Sign extension.
3005 __ Asr(out.AsRegisterPairHigh<Register>(),
3006 out.AsRegisterPairLow<Register>(),
3007 31);
3008 break;
3009
3010 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00003011 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003012 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003013 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003014 break;
3015
Roland Levillaindff1f282014-11-05 14:15:05 +00003016 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003017 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003018 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003019 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003020 break;
3021
3022 default:
3023 LOG(FATAL) << "Unexpected type conversion from " << input_type
3024 << " to " << result_type;
3025 }
3026 break;
3027
Roland Levillain981e4542014-11-14 11:47:14 +00003028 case Primitive::kPrimChar:
3029 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003030 case Primitive::kPrimLong:
3031 // Type conversion from long to char is a result of code transformations.
3032 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3033 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003034 case Primitive::kPrimBoolean:
3035 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003036 case Primitive::kPrimByte:
3037 case Primitive::kPrimShort:
3038 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003039 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003040 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00003041 break;
3042
3043 default:
3044 LOG(FATAL) << "Unexpected type conversion from " << input_type
3045 << " to " << result_type;
3046 }
3047 break;
3048
Roland Levillaindff1f282014-11-05 14:15:05 +00003049 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003050 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003051 case Primitive::kPrimBoolean:
3052 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003053 case Primitive::kPrimByte:
3054 case Primitive::kPrimShort:
3055 case Primitive::kPrimInt:
3056 case Primitive::kPrimChar: {
3057 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003058 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
3059 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003060 break;
3061 }
3062
Roland Levillain5b3ee562015-04-14 16:02:41 +01003063 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003064 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003065 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003066 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00003067 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003068
Roland Levillaincff13742014-11-17 14:32:17 +00003069 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003070 // Processing a Dex `double-to-float' instruction.
3071 __ vcvtsd(out.AsFpuRegister<SRegister>(),
3072 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00003073 break;
3074
3075 default:
3076 LOG(FATAL) << "Unexpected type conversion from " << input_type
3077 << " to " << result_type;
3078 };
3079 break;
3080
Roland Levillaindff1f282014-11-05 14:15:05 +00003081 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003082 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003083 case Primitive::kPrimBoolean:
3084 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003085 case Primitive::kPrimByte:
3086 case Primitive::kPrimShort:
3087 case Primitive::kPrimInt:
3088 case Primitive::kPrimChar: {
3089 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003090 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003091 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3092 out.AsFpuRegisterPairLow<SRegister>());
3093 break;
3094 }
3095
Roland Levillain647b9ed2014-11-27 12:06:00 +00003096 case Primitive::kPrimLong: {
3097 // Processing a Dex `long-to-double' instruction.
3098 Register low = in.AsRegisterPairLow<Register>();
3099 Register high = in.AsRegisterPairHigh<Register>();
3100 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
3101 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003102 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00003103 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003104 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
3105 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003106
Roland Levillain682393c2015-04-14 15:57:52 +01003107 // temp_d = int-to-double(high)
3108 __ vmovsr(temp_s, high);
3109 __ vcvtdi(temp_d, temp_s);
3110 // constant_d = k2Pow32EncodingForDouble
3111 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
3112 // out_d = unsigned-to-double(low)
3113 __ vmovsr(out_s, low);
3114 __ vcvtdu(out_d, out_s);
3115 // out_d += temp_d * constant_d
3116 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003117 break;
3118 }
3119
Roland Levillaincff13742014-11-17 14:32:17 +00003120 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003121 // Processing a Dex `float-to-double' instruction.
3122 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3123 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003124 break;
3125
3126 default:
3127 LOG(FATAL) << "Unexpected type conversion from " << input_type
3128 << " to " << result_type;
3129 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003130 break;
3131
3132 default:
3133 LOG(FATAL) << "Unexpected type conversion from " << input_type
3134 << " to " << result_type;
3135 }
3136}
3137
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003138void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003139 LocationSummary* locations =
3140 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003141 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003142 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003143 locations->SetInAt(0, Location::RequiresRegister());
3144 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003145 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3146 break;
3147 }
3148
3149 case Primitive::kPrimLong: {
3150 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003151 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003152 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003153 break;
3154 }
3155
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003156 case Primitive::kPrimFloat:
3157 case Primitive::kPrimDouble: {
3158 locations->SetInAt(0, Location::RequiresFpuRegister());
3159 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003160 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003161 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003162 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003163
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003164 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003165 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003166 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003167}
3168
3169void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
3170 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003171 Location out = locations->Out();
3172 Location first = locations->InAt(0);
3173 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003174 switch (add->GetResultType()) {
3175 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003176 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003177 __ add(out.AsRegister<Register>(),
3178 first.AsRegister<Register>(),
3179 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003180 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003181 __ AddConstant(out.AsRegister<Register>(),
3182 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003183 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003184 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003185 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003186
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003187 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003188 if (second.IsConstant()) {
3189 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3190 GenerateAddLongConst(out, first, value);
3191 } else {
3192 DCHECK(second.IsRegisterPair());
3193 __ adds(out.AsRegisterPairLow<Register>(),
3194 first.AsRegisterPairLow<Register>(),
3195 ShifterOperand(second.AsRegisterPairLow<Register>()));
3196 __ adc(out.AsRegisterPairHigh<Register>(),
3197 first.AsRegisterPairHigh<Register>(),
3198 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3199 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003200 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003201 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003202
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003203 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00003204 __ vadds(out.AsFpuRegister<SRegister>(),
3205 first.AsFpuRegister<SRegister>(),
3206 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003207 break;
3208
3209 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003210 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3211 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3212 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003213 break;
3214
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003215 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003216 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003217 }
3218}
3219
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003220void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003221 LocationSummary* locations =
3222 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003223 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003224 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003225 locations->SetInAt(0, Location::RequiresRegister());
3226 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003227 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3228 break;
3229 }
3230
3231 case Primitive::kPrimLong: {
3232 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003233 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003234 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003235 break;
3236 }
Calin Juravle11351682014-10-23 15:38:15 +01003237 case Primitive::kPrimFloat:
3238 case Primitive::kPrimDouble: {
3239 locations->SetInAt(0, Location::RequiresFpuRegister());
3240 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003241 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003242 break;
Calin Juravle11351682014-10-23 15:38:15 +01003243 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003244 default:
Calin Juravle11351682014-10-23 15:38:15 +01003245 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003246 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003247}
3248
3249void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3250 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003251 Location out = locations->Out();
3252 Location first = locations->InAt(0);
3253 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003254 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003255 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003256 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003257 __ sub(out.AsRegister<Register>(),
3258 first.AsRegister<Register>(),
3259 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003260 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003261 __ AddConstant(out.AsRegister<Register>(),
3262 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003263 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003264 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003265 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003266 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003267
Calin Juravle11351682014-10-23 15:38:15 +01003268 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003269 if (second.IsConstant()) {
3270 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3271 GenerateAddLongConst(out, first, -value);
3272 } else {
3273 DCHECK(second.IsRegisterPair());
3274 __ subs(out.AsRegisterPairLow<Register>(),
3275 first.AsRegisterPairLow<Register>(),
3276 ShifterOperand(second.AsRegisterPairLow<Register>()));
3277 __ sbc(out.AsRegisterPairHigh<Register>(),
3278 first.AsRegisterPairHigh<Register>(),
3279 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3280 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003281 break;
Calin Juravle11351682014-10-23 15:38:15 +01003282 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003283
Calin Juravle11351682014-10-23 15:38:15 +01003284 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003285 __ vsubs(out.AsFpuRegister<SRegister>(),
3286 first.AsFpuRegister<SRegister>(),
3287 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003288 break;
Calin Juravle11351682014-10-23 15:38:15 +01003289 }
3290
3291 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003292 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3293 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3294 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003295 break;
3296 }
3297
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003298
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003299 default:
Calin Juravle11351682014-10-23 15:38:15 +01003300 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003301 }
3302}
3303
Calin Juravle34bacdf2014-10-07 20:23:36 +01003304void LocationsBuilderARM::VisitMul(HMul* mul) {
3305 LocationSummary* locations =
3306 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3307 switch (mul->GetResultType()) {
3308 case Primitive::kPrimInt:
3309 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003310 locations->SetInAt(0, Location::RequiresRegister());
3311 locations->SetInAt(1, Location::RequiresRegister());
3312 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003313 break;
3314 }
3315
Calin Juravleb5bfa962014-10-21 18:02:24 +01003316 case Primitive::kPrimFloat:
3317 case Primitive::kPrimDouble: {
3318 locations->SetInAt(0, Location::RequiresFpuRegister());
3319 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003320 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003321 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003322 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003323
3324 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003325 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003326 }
3327}
3328
3329void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3330 LocationSummary* locations = mul->GetLocations();
3331 Location out = locations->Out();
3332 Location first = locations->InAt(0);
3333 Location second = locations->InAt(1);
3334 switch (mul->GetResultType()) {
3335 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003336 __ mul(out.AsRegister<Register>(),
3337 first.AsRegister<Register>(),
3338 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003339 break;
3340 }
3341 case Primitive::kPrimLong: {
3342 Register out_hi = out.AsRegisterPairHigh<Register>();
3343 Register out_lo = out.AsRegisterPairLow<Register>();
3344 Register in1_hi = first.AsRegisterPairHigh<Register>();
3345 Register in1_lo = first.AsRegisterPairLow<Register>();
3346 Register in2_hi = second.AsRegisterPairHigh<Register>();
3347 Register in2_lo = second.AsRegisterPairLow<Register>();
3348
3349 // Extra checks to protect caused by the existence of R1_R2.
3350 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3351 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3352 DCHECK_NE(out_hi, in1_lo);
3353 DCHECK_NE(out_hi, in2_lo);
3354
3355 // input: in1 - 64 bits, in2 - 64 bits
3356 // output: out
3357 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3358 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3359 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3360
3361 // IP <- in1.lo * in2.hi
3362 __ mul(IP, in1_lo, in2_hi);
3363 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3364 __ mla(out_hi, in1_hi, in2_lo, IP);
3365 // out.lo <- (in1.lo * in2.lo)[31:0];
3366 __ umull(out_lo, IP, in1_lo, in2_lo);
3367 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3368 __ add(out_hi, out_hi, ShifterOperand(IP));
3369 break;
3370 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003371
3372 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003373 __ vmuls(out.AsFpuRegister<SRegister>(),
3374 first.AsFpuRegister<SRegister>(),
3375 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003376 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003377 }
3378
3379 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003380 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3381 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3382 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003383 break;
3384 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003385
3386 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003387 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003388 }
3389}
3390
Zheng Xuc6667102015-05-15 16:08:45 +08003391void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3392 DCHECK(instruction->IsDiv() || instruction->IsRem());
3393 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3394
3395 LocationSummary* locations = instruction->GetLocations();
3396 Location second = locations->InAt(1);
3397 DCHECK(second.IsConstant());
3398
3399 Register out = locations->Out().AsRegister<Register>();
3400 Register dividend = locations->InAt(0).AsRegister<Register>();
3401 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3402 DCHECK(imm == 1 || imm == -1);
3403
3404 if (instruction->IsRem()) {
3405 __ LoadImmediate(out, 0);
3406 } else {
3407 if (imm == 1) {
3408 __ Mov(out, dividend);
3409 } else {
3410 __ rsb(out, dividend, ShifterOperand(0));
3411 }
3412 }
3413}
3414
3415void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3416 DCHECK(instruction->IsDiv() || instruction->IsRem());
3417 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3418
3419 LocationSummary* locations = instruction->GetLocations();
3420 Location second = locations->InAt(1);
3421 DCHECK(second.IsConstant());
3422
3423 Register out = locations->Out().AsRegister<Register>();
3424 Register dividend = locations->InAt(0).AsRegister<Register>();
3425 Register temp = locations->GetTemp(0).AsRegister<Register>();
3426 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003427 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003428 int ctz_imm = CTZ(abs_imm);
3429
3430 if (ctz_imm == 1) {
3431 __ Lsr(temp, dividend, 32 - ctz_imm);
3432 } else {
3433 __ Asr(temp, dividend, 31);
3434 __ Lsr(temp, temp, 32 - ctz_imm);
3435 }
3436 __ add(out, temp, ShifterOperand(dividend));
3437
3438 if (instruction->IsDiv()) {
3439 __ Asr(out, out, ctz_imm);
3440 if (imm < 0) {
3441 __ rsb(out, out, ShifterOperand(0));
3442 }
3443 } else {
3444 __ ubfx(out, out, 0, ctz_imm);
3445 __ sub(out, out, ShifterOperand(temp));
3446 }
3447}
3448
3449void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3450 DCHECK(instruction->IsDiv() || instruction->IsRem());
3451 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3452
3453 LocationSummary* locations = instruction->GetLocations();
3454 Location second = locations->InAt(1);
3455 DCHECK(second.IsConstant());
3456
3457 Register out = locations->Out().AsRegister<Register>();
3458 Register dividend = locations->InAt(0).AsRegister<Register>();
3459 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3460 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3461 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3462
3463 int64_t magic;
3464 int shift;
3465 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3466
3467 __ LoadImmediate(temp1, magic);
3468 __ smull(temp2, temp1, dividend, temp1);
3469
3470 if (imm > 0 && magic < 0) {
3471 __ add(temp1, temp1, ShifterOperand(dividend));
3472 } else if (imm < 0 && magic > 0) {
3473 __ sub(temp1, temp1, ShifterOperand(dividend));
3474 }
3475
3476 if (shift != 0) {
3477 __ Asr(temp1, temp1, shift);
3478 }
3479
3480 if (instruction->IsDiv()) {
3481 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3482 } else {
3483 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3484 // TODO: Strength reduction for mls.
3485 __ LoadImmediate(temp2, imm);
3486 __ mls(out, temp1, temp2, dividend);
3487 }
3488}
3489
3490void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3491 DCHECK(instruction->IsDiv() || instruction->IsRem());
3492 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3493
3494 LocationSummary* locations = instruction->GetLocations();
3495 Location second = locations->InAt(1);
3496 DCHECK(second.IsConstant());
3497
3498 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3499 if (imm == 0) {
3500 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3501 } else if (imm == 1 || imm == -1) {
3502 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003503 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003504 DivRemByPowerOfTwo(instruction);
3505 } else {
3506 DCHECK(imm <= -2 || imm >= 2);
3507 GenerateDivRemWithAnyConstant(instruction);
3508 }
3509}
3510
Calin Juravle7c4954d2014-10-28 16:57:40 +00003511void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003512 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3513 if (div->GetResultType() == Primitive::kPrimLong) {
3514 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003515 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003516 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3517 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003518 } else if (div->GetResultType() == Primitive::kPrimInt &&
3519 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3520 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003521 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003522 }
3523
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003524 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3525
Calin Juravle7c4954d2014-10-28 16:57:40 +00003526 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003527 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003528 if (div->InputAt(1)->IsConstant()) {
3529 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003530 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003531 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003532 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3533 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003534 // No temp register required.
3535 } else {
3536 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003537 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003538 locations->AddTemp(Location::RequiresRegister());
3539 }
3540 }
3541 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003542 locations->SetInAt(0, Location::RequiresRegister());
3543 locations->SetInAt(1, Location::RequiresRegister());
3544 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3545 } else {
3546 InvokeRuntimeCallingConvention calling_convention;
3547 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3548 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003549 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003550 // we only need the former.
3551 locations->SetOut(Location::RegisterLocation(R0));
3552 }
Calin Juravled0d48522014-11-04 16:40:20 +00003553 break;
3554 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003555 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003556 InvokeRuntimeCallingConvention calling_convention;
3557 locations->SetInAt(0, Location::RegisterPairLocation(
3558 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3559 locations->SetInAt(1, Location::RegisterPairLocation(
3560 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003561 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003562 break;
3563 }
3564 case Primitive::kPrimFloat:
3565 case Primitive::kPrimDouble: {
3566 locations->SetInAt(0, Location::RequiresFpuRegister());
3567 locations->SetInAt(1, Location::RequiresFpuRegister());
3568 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3569 break;
3570 }
3571
3572 default:
3573 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3574 }
3575}
3576
3577void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3578 LocationSummary* locations = div->GetLocations();
3579 Location out = locations->Out();
3580 Location first = locations->InAt(0);
3581 Location second = locations->InAt(1);
3582
3583 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003584 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003585 if (second.IsConstant()) {
3586 GenerateDivRemConstantIntegral(div);
3587 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003588 __ sdiv(out.AsRegister<Register>(),
3589 first.AsRegister<Register>(),
3590 second.AsRegister<Register>());
3591 } else {
3592 InvokeRuntimeCallingConvention calling_convention;
3593 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3594 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3595 DCHECK_EQ(R0, out.AsRegister<Register>());
3596
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003597 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003598 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003599 }
Calin Juravled0d48522014-11-04 16:40:20 +00003600 break;
3601 }
3602
Calin Juravle7c4954d2014-10-28 16:57:40 +00003603 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003604 InvokeRuntimeCallingConvention calling_convention;
3605 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3606 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3607 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3608 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3609 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003610 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003611
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003612 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003613 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00003614 break;
3615 }
3616
3617 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003618 __ vdivs(out.AsFpuRegister<SRegister>(),
3619 first.AsFpuRegister<SRegister>(),
3620 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003621 break;
3622 }
3623
3624 case Primitive::kPrimDouble: {
3625 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3626 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3627 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
3628 break;
3629 }
3630
3631 default:
3632 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3633 }
3634}
3635
Calin Juravlebacfec32014-11-14 15:54:36 +00003636void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003637 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003638
3639 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003640 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003641 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
3642 // sdiv will be replaced by other instruction sequence.
3643 call_kind = LocationSummary::kNoCall;
3644 } else if ((rem->GetResultType() == Primitive::kPrimInt)
3645 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003646 // Have hardware divide instruction for int, do it with three instructions.
3647 call_kind = LocationSummary::kNoCall;
3648 }
3649
Calin Juravlebacfec32014-11-14 15:54:36 +00003650 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
3651
Calin Juravled2ec87d2014-12-08 14:24:46 +00003652 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003653 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003654 if (rem->InputAt(1)->IsConstant()) {
3655 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003656 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003657 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003658 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
3659 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003660 // No temp register required.
3661 } else {
3662 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003663 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003664 locations->AddTemp(Location::RequiresRegister());
3665 }
3666 }
3667 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003668 locations->SetInAt(0, Location::RequiresRegister());
3669 locations->SetInAt(1, Location::RequiresRegister());
3670 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3671 locations->AddTemp(Location::RequiresRegister());
3672 } else {
3673 InvokeRuntimeCallingConvention calling_convention;
3674 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3675 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003676 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003677 // we only need the latter.
3678 locations->SetOut(Location::RegisterLocation(R1));
3679 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003680 break;
3681 }
3682 case Primitive::kPrimLong: {
3683 InvokeRuntimeCallingConvention calling_convention;
3684 locations->SetInAt(0, Location::RegisterPairLocation(
3685 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3686 locations->SetInAt(1, Location::RegisterPairLocation(
3687 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3688 // The runtime helper puts the output in R2,R3.
3689 locations->SetOut(Location::RegisterPairLocation(R2, R3));
3690 break;
3691 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00003692 case Primitive::kPrimFloat: {
3693 InvokeRuntimeCallingConvention calling_convention;
3694 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
3695 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
3696 locations->SetOut(Location::FpuRegisterLocation(S0));
3697 break;
3698 }
3699
Calin Juravlebacfec32014-11-14 15:54:36 +00003700 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003701 InvokeRuntimeCallingConvention calling_convention;
3702 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3703 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
3704 locations->SetInAt(1, Location::FpuRegisterPairLocation(
3705 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
3706 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00003707 break;
3708 }
3709
3710 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003711 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003712 }
3713}
3714
3715void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
3716 LocationSummary* locations = rem->GetLocations();
3717 Location out = locations->Out();
3718 Location first = locations->InAt(0);
3719 Location second = locations->InAt(1);
3720
Calin Juravled2ec87d2014-12-08 14:24:46 +00003721 Primitive::Type type = rem->GetResultType();
3722 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003723 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003724 if (second.IsConstant()) {
3725 GenerateDivRemConstantIntegral(rem);
3726 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003727 Register reg1 = first.AsRegister<Register>();
3728 Register reg2 = second.AsRegister<Register>();
3729 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003730
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003731 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003732 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003733 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01003734 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003735 } else {
3736 InvokeRuntimeCallingConvention calling_convention;
3737 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
3738 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
3739 DCHECK_EQ(R1, out.AsRegister<Register>());
3740
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003741 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003742 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003743 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003744 break;
3745 }
3746
3747 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003748 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003749 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003750 break;
3751 }
3752
Calin Juravled2ec87d2014-12-08 14:24:46 +00003753 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003754 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003755 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00003756 break;
3757 }
3758
Calin Juravlebacfec32014-11-14 15:54:36 +00003759 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003760 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003761 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003762 break;
3763 }
3764
3765 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003766 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003767 }
3768}
3769
Calin Juravled0d48522014-11-04 16:40:20 +00003770void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003771 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003772 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00003773}
3774
3775void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003776 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003777 codegen_->AddSlowPath(slow_path);
3778
3779 LocationSummary* locations = instruction->GetLocations();
3780 Location value = locations->InAt(0);
3781
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003782 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003783 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003784 case Primitive::kPrimByte:
3785 case Primitive::kPrimChar:
3786 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003787 case Primitive::kPrimInt: {
3788 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01003789 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003790 } else {
3791 DCHECK(value.IsConstant()) << value;
3792 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3793 __ b(slow_path->GetEntryLabel());
3794 }
3795 }
3796 break;
3797 }
3798 case Primitive::kPrimLong: {
3799 if (value.IsRegisterPair()) {
3800 __ orrs(IP,
3801 value.AsRegisterPairLow<Register>(),
3802 ShifterOperand(value.AsRegisterPairHigh<Register>()));
3803 __ b(slow_path->GetEntryLabel(), EQ);
3804 } else {
3805 DCHECK(value.IsConstant()) << value;
3806 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3807 __ b(slow_path->GetEntryLabel());
3808 }
3809 }
3810 break;
3811 default:
3812 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3813 }
3814 }
Calin Juravled0d48522014-11-04 16:40:20 +00003815}
3816
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003817void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
3818 Register in = locations->InAt(0).AsRegister<Register>();
3819 Location rhs = locations->InAt(1);
3820 Register out = locations->Out().AsRegister<Register>();
3821
3822 if (rhs.IsConstant()) {
3823 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
3824 // so map all rotations to a +ve. equivalent in that range.
3825 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
3826 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
3827 if (rot) {
3828 // Rotate, mapping left rotations to right equivalents if necessary.
3829 // (e.g. left by 2 bits == right by 30.)
3830 __ Ror(out, in, rot);
3831 } else if (out != in) {
3832 __ Mov(out, in);
3833 }
3834 } else {
3835 __ Ror(out, in, rhs.AsRegister<Register>());
3836 }
3837}
3838
3839// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
3840// rotates by swapping input regs (effectively rotating by the first 32-bits of
3841// a larger rotation) or flipping direction (thus treating larger right/left
3842// rotations as sub-word sized rotations in the other direction) as appropriate.
3843void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
3844 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
3845 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
3846 Location rhs = locations->InAt(1);
3847 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
3848 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
3849
3850 if (rhs.IsConstant()) {
3851 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
3852 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00003853 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003854 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
3855 // logic below to a simple pair of binary orr.
3856 // (e.g. 34 bits == in_reg swap + 2 bits right.)
3857 if (rot >= kArmBitsPerWord) {
3858 rot -= kArmBitsPerWord;
3859 std::swap(in_reg_hi, in_reg_lo);
3860 }
3861 // Rotate, or mov to out for zero or word size rotations.
3862 if (rot != 0u) {
3863 __ Lsr(out_reg_hi, in_reg_hi, rot);
3864 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
3865 __ Lsr(out_reg_lo, in_reg_lo, rot);
3866 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
3867 } else {
3868 __ Mov(out_reg_lo, in_reg_lo);
3869 __ Mov(out_reg_hi, in_reg_hi);
3870 }
3871 } else {
3872 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
3873 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
3874 Label end;
3875 Label shift_by_32_plus_shift_right;
3876
3877 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
3878 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
3879 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
3880 __ b(&shift_by_32_plus_shift_right, CC);
3881
3882 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
3883 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
3884 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
3885 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3886 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3887 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3888 __ Lsr(shift_left, in_reg_hi, shift_right);
3889 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
3890 __ b(&end);
3891
3892 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
3893 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
3894 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
3895 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
3896 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
3897 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
3898 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
3899 __ Lsl(shift_right, in_reg_hi, shift_left);
3900 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
3901
3902 __ Bind(&end);
3903 }
3904}
Roland Levillain22c49222016-03-18 14:04:28 +00003905
3906void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003907 LocationSummary* locations =
3908 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3909 switch (ror->GetResultType()) {
3910 case Primitive::kPrimInt: {
3911 locations->SetInAt(0, Location::RequiresRegister());
3912 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
3913 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3914 break;
3915 }
3916 case Primitive::kPrimLong: {
3917 locations->SetInAt(0, Location::RequiresRegister());
3918 if (ror->InputAt(1)->IsConstant()) {
3919 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
3920 } else {
3921 locations->SetInAt(1, Location::RequiresRegister());
3922 locations->AddTemp(Location::RequiresRegister());
3923 locations->AddTemp(Location::RequiresRegister());
3924 }
3925 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3926 break;
3927 }
3928 default:
3929 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3930 }
3931}
3932
Roland Levillain22c49222016-03-18 14:04:28 +00003933void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003934 LocationSummary* locations = ror->GetLocations();
3935 Primitive::Type type = ror->GetResultType();
3936 switch (type) {
3937 case Primitive::kPrimInt: {
3938 HandleIntegerRotate(locations);
3939 break;
3940 }
3941 case Primitive::kPrimLong: {
3942 HandleLongRotate(locations);
3943 break;
3944 }
3945 default:
3946 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00003947 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003948 }
3949}
3950
Calin Juravle9aec02f2014-11-18 23:06:35 +00003951void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
3952 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3953
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003954 LocationSummary* locations =
3955 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003956
3957 switch (op->GetResultType()) {
3958 case Primitive::kPrimInt: {
3959 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003960 if (op->InputAt(1)->IsConstant()) {
3961 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3962 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3963 } else {
3964 locations->SetInAt(1, Location::RequiresRegister());
3965 // Make the output overlap, as it will be used to hold the masked
3966 // second input.
3967 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3968 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003969 break;
3970 }
3971 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00003972 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00003973 if (op->InputAt(1)->IsConstant()) {
3974 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
3975 // For simplicity, use kOutputOverlap even though we only require that low registers
3976 // don't clash with high registers which the register allocator currently guarantees.
3977 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3978 } else {
3979 locations->SetInAt(1, Location::RequiresRegister());
3980 locations->AddTemp(Location::RequiresRegister());
3981 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3982 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003983 break;
3984 }
3985 default:
3986 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
3987 }
3988}
3989
3990void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
3991 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3992
3993 LocationSummary* locations = op->GetLocations();
3994 Location out = locations->Out();
3995 Location first = locations->InAt(0);
3996 Location second = locations->InAt(1);
3997
3998 Primitive::Type type = op->GetResultType();
3999 switch (type) {
4000 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004001 Register out_reg = out.AsRegister<Register>();
4002 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004003 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004004 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00004005 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004006 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004007 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004008 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004009 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004010 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004011 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004012 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004013 }
4014 } else {
4015 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004016 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00004017 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00004018 __ Mov(out_reg, first_reg);
4019 } else if (op->IsShl()) {
4020 __ Lsl(out_reg, first_reg, shift_value);
4021 } else if (op->IsShr()) {
4022 __ Asr(out_reg, first_reg, shift_value);
4023 } else {
4024 __ Lsr(out_reg, first_reg, shift_value);
4025 }
4026 }
4027 break;
4028 }
4029 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004030 Register o_h = out.AsRegisterPairHigh<Register>();
4031 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004032
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004033 Register high = first.AsRegisterPairHigh<Register>();
4034 Register low = first.AsRegisterPairLow<Register>();
4035
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004036 if (second.IsRegister()) {
4037 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004038
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004039 Register second_reg = second.AsRegister<Register>();
4040
4041 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004042 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004043 // Shift the high part
4044 __ Lsl(o_h, high, o_l);
4045 // Shift the low part and `or` what overflew on the high part
4046 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
4047 __ Lsr(temp, low, temp);
4048 __ orr(o_h, o_h, ShifterOperand(temp));
4049 // If the shift is > 32 bits, override the high part
4050 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
4051 __ it(PL);
4052 __ Lsl(o_h, low, temp, PL);
4053 // Shift the low part
4054 __ Lsl(o_l, low, o_l);
4055 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004056 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004057 // Shift the low part
4058 __ Lsr(o_l, low, o_h);
4059 // Shift the high part and `or` what underflew on the low part
4060 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4061 __ Lsl(temp, high, temp);
4062 __ orr(o_l, o_l, ShifterOperand(temp));
4063 // If the shift is > 32 bits, override the low part
4064 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4065 __ it(PL);
4066 __ Asr(o_l, high, temp, PL);
4067 // Shift the high part
4068 __ Asr(o_h, high, o_h);
4069 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004070 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004071 // same as Shr except we use `Lsr`s and not `Asr`s
4072 __ Lsr(o_l, low, o_h);
4073 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4074 __ Lsl(temp, high, temp);
4075 __ orr(o_l, o_l, ShifterOperand(temp));
4076 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4077 __ it(PL);
4078 __ Lsr(o_l, high, temp, PL);
4079 __ Lsr(o_h, high, o_h);
4080 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004081 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004082 // Register allocator doesn't create partial overlap.
4083 DCHECK_NE(o_l, high);
4084 DCHECK_NE(o_h, low);
4085 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004086 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004087 if (shift_value > 32) {
4088 if (op->IsShl()) {
4089 __ Lsl(o_h, low, shift_value - 32);
4090 __ LoadImmediate(o_l, 0);
4091 } else if (op->IsShr()) {
4092 __ Asr(o_l, high, shift_value - 32);
4093 __ Asr(o_h, high, 31);
4094 } else {
4095 __ Lsr(o_l, high, shift_value - 32);
4096 __ LoadImmediate(o_h, 0);
4097 }
4098 } else if (shift_value == 32) {
4099 if (op->IsShl()) {
4100 __ mov(o_h, ShifterOperand(low));
4101 __ LoadImmediate(o_l, 0);
4102 } else if (op->IsShr()) {
4103 __ mov(o_l, ShifterOperand(high));
4104 __ Asr(o_h, high, 31);
4105 } else {
4106 __ mov(o_l, ShifterOperand(high));
4107 __ LoadImmediate(o_h, 0);
4108 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00004109 } else if (shift_value == 1) {
4110 if (op->IsShl()) {
4111 __ Lsls(o_l, low, 1);
4112 __ adc(o_h, high, ShifterOperand(high));
4113 } else if (op->IsShr()) {
4114 __ Asrs(o_h, high, 1);
4115 __ Rrx(o_l, low);
4116 } else {
4117 __ Lsrs(o_h, high, 1);
4118 __ Rrx(o_l, low);
4119 }
4120 } else {
4121 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004122 if (op->IsShl()) {
4123 __ Lsl(o_h, high, shift_value);
4124 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
4125 __ Lsl(o_l, low, shift_value);
4126 } else if (op->IsShr()) {
4127 __ Lsr(o_l, low, shift_value);
4128 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4129 __ Asr(o_h, high, shift_value);
4130 } else {
4131 __ Lsr(o_l, low, shift_value);
4132 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4133 __ Lsr(o_h, high, shift_value);
4134 }
4135 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004136 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004137 break;
4138 }
4139 default:
4140 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004141 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004142 }
4143}
4144
4145void LocationsBuilderARM::VisitShl(HShl* shl) {
4146 HandleShift(shl);
4147}
4148
4149void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
4150 HandleShift(shl);
4151}
4152
4153void LocationsBuilderARM::VisitShr(HShr* shr) {
4154 HandleShift(shr);
4155}
4156
4157void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
4158 HandleShift(shr);
4159}
4160
4161void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
4162 HandleShift(ushr);
4163}
4164
4165void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
4166 HandleShift(ushr);
4167}
4168
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004169void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004170 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004171 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00004172 if (instruction->IsStringAlloc()) {
4173 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4174 } else {
4175 InvokeRuntimeCallingConvention calling_convention;
4176 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004177 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004178 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004179}
4180
4181void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004182 // Note: if heap poisoning is enabled, the entry point takes cares
4183 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004184 if (instruction->IsStringAlloc()) {
4185 // String is allocated through StringFactory. Call NewEmptyString entry point.
4186 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004187 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004188 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4189 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
4190 __ blx(LR);
4191 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4192 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004193 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004194 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004195 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004196}
4197
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004198void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
4199 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004200 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004201 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004202 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004203 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4204 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004205}
4206
4207void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004208 // Note: if heap poisoning is enabled, the entry point takes cares
4209 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004210 QuickEntrypointEnum entrypoint =
4211 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4212 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004213 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004214 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004215}
4216
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004217void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004218 LocationSummary* locations =
4219 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004220 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4221 if (location.IsStackSlot()) {
4222 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4223 } else if (location.IsDoubleStackSlot()) {
4224 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004225 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004226 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004227}
4228
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004229void InstructionCodeGeneratorARM::VisitParameterValue(
4230 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004231 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004232}
4233
4234void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4235 LocationSummary* locations =
4236 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4237 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4238}
4239
4240void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4241 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004242}
4243
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004244void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004245 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004246 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004247 locations->SetInAt(0, Location::RequiresRegister());
4248 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004249}
4250
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004251void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4252 LocationSummary* locations = not_->GetLocations();
4253 Location out = locations->Out();
4254 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004255 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004256 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004257 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004258 break;
4259
4260 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004261 __ mvn(out.AsRegisterPairLow<Register>(),
4262 ShifterOperand(in.AsRegisterPairLow<Register>()));
4263 __ mvn(out.AsRegisterPairHigh<Register>(),
4264 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004265 break;
4266
4267 default:
4268 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4269 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004270}
4271
David Brazdil66d126e2015-04-03 16:02:44 +01004272void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4273 LocationSummary* locations =
4274 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4275 locations->SetInAt(0, Location::RequiresRegister());
4276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4277}
4278
4279void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004280 LocationSummary* locations = bool_not->GetLocations();
4281 Location out = locations->Out();
4282 Location in = locations->InAt(0);
4283 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4284}
4285
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004286void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004287 LocationSummary* locations =
4288 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004289 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004290 case Primitive::kPrimBoolean:
4291 case Primitive::kPrimByte:
4292 case Primitive::kPrimShort:
4293 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004294 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004295 case Primitive::kPrimLong: {
4296 locations->SetInAt(0, Location::RequiresRegister());
4297 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004298 // Output overlaps because it is written before doing the low comparison.
4299 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004300 break;
4301 }
4302 case Primitive::kPrimFloat:
4303 case Primitive::kPrimDouble: {
4304 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004305 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004306 locations->SetOut(Location::RequiresRegister());
4307 break;
4308 }
4309 default:
4310 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4311 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004312}
4313
4314void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004315 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004316 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004317 Location left = locations->InAt(0);
4318 Location right = locations->InAt(1);
4319
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004320 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004321 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004322 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004323 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004324 case Primitive::kPrimBoolean:
4325 case Primitive::kPrimByte:
4326 case Primitive::kPrimShort:
4327 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004328 case Primitive::kPrimInt: {
4329 __ LoadImmediate(out, 0);
4330 __ cmp(left.AsRegister<Register>(),
4331 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4332 less_cond = LT;
4333 break;
4334 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004335 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004336 __ cmp(left.AsRegisterPairHigh<Register>(),
4337 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004338 __ b(&less, LT);
4339 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004340 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004341 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004342 __ cmp(left.AsRegisterPairLow<Register>(),
4343 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004344 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004345 break;
4346 }
4347 case Primitive::kPrimFloat:
4348 case Primitive::kPrimDouble: {
4349 __ LoadImmediate(out, 0);
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004350 GenerateVcmp(compare);
Calin Juravleddb7df22014-11-25 20:56:51 +00004351 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004352 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004353 break;
4354 }
4355 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004356 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004357 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004358 }
Aart Bika19616e2016-02-01 18:57:58 -08004359
Calin Juravleddb7df22014-11-25 20:56:51 +00004360 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004361 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004362
4363 __ Bind(&greater);
4364 __ LoadImmediate(out, 1);
4365 __ b(&done);
4366
4367 __ Bind(&less);
4368 __ LoadImmediate(out, -1);
4369
4370 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004371}
4372
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004373void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004374 LocationSummary* locations =
4375 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004376 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004377 locations->SetInAt(i, Location::Any());
4378 }
4379 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004380}
4381
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004382void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004383 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004384}
4385
Roland Levillainc9285912015-12-18 10:38:42 +00004386void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4387 // TODO (ported from quick): revisit ARM barrier kinds.
4388 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004389 switch (kind) {
4390 case MemBarrierKind::kAnyStore:
4391 case MemBarrierKind::kLoadAny:
4392 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004393 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004394 break;
4395 }
4396 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004397 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004398 break;
4399 }
4400 default:
4401 LOG(FATAL) << "Unexpected memory barrier " << kind;
4402 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004403 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004404}
4405
4406void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4407 uint32_t offset,
4408 Register out_lo,
4409 Register out_hi) {
4410 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004411 // Ensure `out_lo` is different from `addr`, so that loading
4412 // `offset` into `out_lo` does not clutter `addr`.
4413 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004414 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004415 __ add(IP, addr, ShifterOperand(out_lo));
4416 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004417 }
4418 __ ldrexd(out_lo, out_hi, addr);
4419}
4420
4421void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4422 uint32_t offset,
4423 Register value_lo,
4424 Register value_hi,
4425 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004426 Register temp2,
4427 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004428 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004429 if (offset != 0) {
4430 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004431 __ add(IP, addr, ShifterOperand(temp1));
4432 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004433 }
4434 __ Bind(&fail);
4435 // We need a load followed by store. (The address used in a STREX instruction must
4436 // be the same as the address in the most recently executed LDREX instruction.)
4437 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004438 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004439 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004440 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004441}
4442
4443void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4444 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4445
Nicolas Geoffray39468442014-09-02 15:17:15 +01004446 LocationSummary* locations =
4447 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004448 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004449
Calin Juravle52c48962014-12-16 17:02:57 +00004450 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004451 if (Primitive::IsFloatingPointType(field_type)) {
4452 locations->SetInAt(1, Location::RequiresFpuRegister());
4453 } else {
4454 locations->SetInAt(1, Location::RequiresRegister());
4455 }
4456
Calin Juravle52c48962014-12-16 17:02:57 +00004457 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004458 bool generate_volatile = field_info.IsVolatile()
4459 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004460 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004461 bool needs_write_barrier =
4462 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004463 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004464 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004465 if (needs_write_barrier) {
4466 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004467 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004468 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004469 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004470 // - registers need to be consecutive
4471 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004472 // We don't test for ARM yet, and the assertion makes sure that we
4473 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004474 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4475
4476 locations->AddTemp(Location::RequiresRegister());
4477 locations->AddTemp(Location::RequiresRegister());
4478 if (field_type == Primitive::kPrimDouble) {
4479 // For doubles we need two more registers to copy the value.
4480 locations->AddTemp(Location::RegisterLocation(R2));
4481 locations->AddTemp(Location::RegisterLocation(R3));
4482 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004483 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484}
4485
Calin Juravle52c48962014-12-16 17:02:57 +00004486void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004487 const FieldInfo& field_info,
4488 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004489 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4490
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004491 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004492 Register base = locations->InAt(0).AsRegister<Register>();
4493 Location value = locations->InAt(1);
4494
4495 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004496 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004497 Primitive::Type field_type = field_info.GetFieldType();
4498 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004499 bool needs_write_barrier =
4500 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004501
4502 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004503 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004504 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004505
4506 switch (field_type) {
4507 case Primitive::kPrimBoolean:
4508 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004509 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004510 break;
4511 }
4512
4513 case Primitive::kPrimShort:
4514 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004515 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004516 break;
4517 }
4518
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004519 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004520 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004521 if (kPoisonHeapReferences && needs_write_barrier) {
4522 // Note that in the case where `value` is a null reference,
4523 // we do not enter this block, as a null reference does not
4524 // need poisoning.
4525 DCHECK_EQ(field_type, Primitive::kPrimNot);
4526 Register temp = locations->GetTemp(0).AsRegister<Register>();
4527 __ Mov(temp, value.AsRegister<Register>());
4528 __ PoisonHeapReference(temp);
4529 __ StoreToOffset(kStoreWord, temp, base, offset);
4530 } else {
4531 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4532 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004533 break;
4534 }
4535
4536 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004537 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004538 GenerateWideAtomicStore(base, offset,
4539 value.AsRegisterPairLow<Register>(),
4540 value.AsRegisterPairHigh<Register>(),
4541 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004542 locations->GetTemp(1).AsRegister<Register>(),
4543 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004544 } else {
4545 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004546 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004547 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004548 break;
4549 }
4550
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004551 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004552 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004553 break;
4554 }
4555
4556 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004557 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004558 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004559 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4560 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4561
4562 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4563
4564 GenerateWideAtomicStore(base, offset,
4565 value_reg_lo,
4566 value_reg_hi,
4567 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004568 locations->GetTemp(3).AsRegister<Register>(),
4569 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004570 } else {
4571 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004572 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004573 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004574 break;
4575 }
4576
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004577 case Primitive::kPrimVoid:
4578 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004579 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004580 }
Calin Juravle52c48962014-12-16 17:02:57 +00004581
Calin Juravle77520bc2015-01-12 18:45:46 +00004582 // Longs and doubles are handled in the switch.
4583 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4584 codegen_->MaybeRecordImplicitNullCheck(instruction);
4585 }
4586
4587 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4588 Register temp = locations->GetTemp(0).AsRegister<Register>();
4589 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004590 codegen_->MarkGCCard(
4591 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004592 }
4593
Calin Juravle52c48962014-12-16 17:02:57 +00004594 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004595 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004596 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004597}
4598
Calin Juravle52c48962014-12-16 17:02:57 +00004599void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4600 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00004601
4602 bool object_field_get_with_read_barrier =
4603 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004604 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00004605 new (GetGraph()->GetArena()) LocationSummary(instruction,
4606 object_field_get_with_read_barrier ?
4607 LocationSummary::kCallOnSlowPath :
4608 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004609 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004610 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004611 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004612 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004613
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004614 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00004615 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004616 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00004617 // The output overlaps in case of volatile long: we don't want the
4618 // code generated by GenerateWideAtomicLoad to overwrite the
4619 // object's location. Likewise, in the case of an object field get
4620 // with read barriers enabled, we do not want the load to overwrite
4621 // the object's location, as we need it to emit the read barrier.
4622 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
4623 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01004624
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004625 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4626 locations->SetOut(Location::RequiresFpuRegister());
4627 } else {
4628 locations->SetOut(Location::RequiresRegister(),
4629 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
4630 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004631 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00004632 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004633 // - registers need to be consecutive
4634 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004635 // We don't test for ARM yet, and the assertion makes sure that we
4636 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004637 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4638 locations->AddTemp(Location::RequiresRegister());
4639 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00004640 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4641 // We need a temporary register for the read barrier marking slow
4642 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
4643 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004644 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004645}
4646
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004647Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
4648 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
4649 << input->GetType();
4650 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
4651 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
4652 return Location::ConstantLocation(input->AsConstant());
4653 } else {
4654 return Location::RequiresFpuRegister();
4655 }
4656}
4657
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004658Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
4659 Opcode opcode) {
4660 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
4661 if (constant->IsConstant() &&
4662 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
4663 return Location::ConstantLocation(constant->AsConstant());
4664 }
4665 return Location::RequiresRegister();
4666}
4667
4668bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
4669 Opcode opcode) {
4670 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
4671 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004672 Opcode high_opcode = opcode;
4673 SetCc low_set_cc = kCcDontCare;
4674 switch (opcode) {
4675 case SUB:
4676 // Flip the operation to an ADD.
4677 value = -value;
4678 opcode = ADD;
4679 FALLTHROUGH_INTENDED;
4680 case ADD:
4681 if (Low32Bits(value) == 0u) {
4682 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
4683 }
4684 high_opcode = ADC;
4685 low_set_cc = kCcSet;
4686 break;
4687 default:
4688 break;
4689 }
4690 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
4691 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004692 } else {
4693 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
4694 }
4695}
4696
Vladimir Marko59751a72016-08-05 14:37:27 +01004697bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
4698 Opcode opcode,
4699 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004700 ShifterOperand so;
4701 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01004702 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004703 return true;
4704 }
4705 Opcode neg_opcode = kNoOperand;
4706 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01004707 case AND: neg_opcode = BIC; value = ~value; break;
4708 case ORR: neg_opcode = ORN; value = ~value; break;
4709 case ADD: neg_opcode = SUB; value = -value; break;
4710 case ADC: neg_opcode = SBC; value = ~value; break;
4711 case SUB: neg_opcode = ADD; value = -value; break;
4712 case SBC: neg_opcode = ADC; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004713 default:
4714 return false;
4715 }
Vladimir Marko59751a72016-08-05 14:37:27 +01004716 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01004717}
4718
Calin Juravle52c48962014-12-16 17:02:57 +00004719void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
4720 const FieldInfo& field_info) {
4721 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004722
Calin Juravle52c48962014-12-16 17:02:57 +00004723 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00004724 Location base_loc = locations->InAt(0);
4725 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004726 Location out = locations->Out();
4727 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004728 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004729 Primitive::Type field_type = field_info.GetFieldType();
4730 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4731
4732 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00004733 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00004734 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004735 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004736
Roland Levillainc9285912015-12-18 10:38:42 +00004737 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00004738 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004739 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004740
Roland Levillainc9285912015-12-18 10:38:42 +00004741 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00004742 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004743 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004744
Roland Levillainc9285912015-12-18 10:38:42 +00004745 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00004746 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004747 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004748
4749 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004750 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004751 break;
Roland Levillainc9285912015-12-18 10:38:42 +00004752
4753 case Primitive::kPrimNot: {
4754 // /* HeapReference<Object> */ out = *(base + offset)
4755 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4756 Location temp_loc = locations->GetTemp(0);
4757 // Note that a potential implicit null check is handled in this
4758 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
4759 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4760 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4761 if (is_volatile) {
4762 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4763 }
4764 } else {
4765 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
4766 codegen_->MaybeRecordImplicitNullCheck(instruction);
4767 if (is_volatile) {
4768 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4769 }
4770 // If read barriers are enabled, emit read barriers other than
4771 // Baker's using a slow path (and also unpoison the loaded
4772 // reference, if heap poisoning is enabled).
4773 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4774 }
4775 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004776 }
4777
Roland Levillainc9285912015-12-18 10:38:42 +00004778 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00004779 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004780 GenerateWideAtomicLoad(base, offset,
4781 out.AsRegisterPairLow<Register>(),
4782 out.AsRegisterPairHigh<Register>());
4783 } else {
4784 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
4785 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004786 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004787
Roland Levillainc9285912015-12-18 10:38:42 +00004788 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00004789 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004790 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004791
4792 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004793 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004794 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004795 Register lo = locations->GetTemp(0).AsRegister<Register>();
4796 Register hi = locations->GetTemp(1).AsRegister<Register>();
4797 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00004798 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004799 __ vmovdrr(out_reg, lo, hi);
4800 } else {
4801 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004802 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004803 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004804 break;
4805 }
4806
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004807 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004808 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004809 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004810 }
Calin Juravle52c48962014-12-16 17:02:57 +00004811
Roland Levillainc9285912015-12-18 10:38:42 +00004812 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
4813 // Potential implicit null checks, in the case of reference or
4814 // double fields, are handled in the previous switch statement.
4815 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004816 codegen_->MaybeRecordImplicitNullCheck(instruction);
4817 }
4818
Calin Juravle52c48962014-12-16 17:02:57 +00004819 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004820 if (field_type == Primitive::kPrimNot) {
4821 // Memory barriers, in the case of references, are also handled
4822 // in the previous switch statement.
4823 } else {
4824 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4825 }
Roland Levillain4d027112015-07-01 15:41:14 +01004826 }
Calin Juravle52c48962014-12-16 17:02:57 +00004827}
4828
4829void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4830 HandleFieldSet(instruction, instruction->GetFieldInfo());
4831}
4832
4833void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004834 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004835}
4836
4837void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4838 HandleFieldGet(instruction, instruction->GetFieldInfo());
4839}
4840
4841void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4842 HandleFieldGet(instruction, instruction->GetFieldInfo());
4843}
4844
4845void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4846 HandleFieldGet(instruction, instruction->GetFieldInfo());
4847}
4848
4849void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4850 HandleFieldGet(instruction, instruction->GetFieldInfo());
4851}
4852
4853void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4854 HandleFieldSet(instruction, instruction->GetFieldInfo());
4855}
4856
4857void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004858 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004859}
4860
Calin Juravlee460d1d2015-09-29 04:52:17 +01004861void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
4862 HUnresolvedInstanceFieldGet* instruction) {
4863 FieldAccessCallingConventionARM calling_convention;
4864 codegen_->CreateUnresolvedFieldLocationSummary(
4865 instruction, instruction->GetFieldType(), calling_convention);
4866}
4867
4868void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
4869 HUnresolvedInstanceFieldGet* instruction) {
4870 FieldAccessCallingConventionARM calling_convention;
4871 codegen_->GenerateUnresolvedFieldAccess(instruction,
4872 instruction->GetFieldType(),
4873 instruction->GetFieldIndex(),
4874 instruction->GetDexPc(),
4875 calling_convention);
4876}
4877
4878void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
4879 HUnresolvedInstanceFieldSet* instruction) {
4880 FieldAccessCallingConventionARM calling_convention;
4881 codegen_->CreateUnresolvedFieldLocationSummary(
4882 instruction, instruction->GetFieldType(), calling_convention);
4883}
4884
4885void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
4886 HUnresolvedInstanceFieldSet* instruction) {
4887 FieldAccessCallingConventionARM calling_convention;
4888 codegen_->GenerateUnresolvedFieldAccess(instruction,
4889 instruction->GetFieldType(),
4890 instruction->GetFieldIndex(),
4891 instruction->GetDexPc(),
4892 calling_convention);
4893}
4894
4895void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
4896 HUnresolvedStaticFieldGet* instruction) {
4897 FieldAccessCallingConventionARM calling_convention;
4898 codegen_->CreateUnresolvedFieldLocationSummary(
4899 instruction, instruction->GetFieldType(), calling_convention);
4900}
4901
4902void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
4903 HUnresolvedStaticFieldGet* instruction) {
4904 FieldAccessCallingConventionARM calling_convention;
4905 codegen_->GenerateUnresolvedFieldAccess(instruction,
4906 instruction->GetFieldType(),
4907 instruction->GetFieldIndex(),
4908 instruction->GetDexPc(),
4909 calling_convention);
4910}
4911
4912void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
4913 HUnresolvedStaticFieldSet* instruction) {
4914 FieldAccessCallingConventionARM calling_convention;
4915 codegen_->CreateUnresolvedFieldLocationSummary(
4916 instruction, instruction->GetFieldType(), calling_convention);
4917}
4918
4919void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
4920 HUnresolvedStaticFieldSet* instruction) {
4921 FieldAccessCallingConventionARM calling_convention;
4922 codegen_->GenerateUnresolvedFieldAccess(instruction,
4923 instruction->GetFieldType(),
4924 instruction->GetFieldIndex(),
4925 instruction->GetDexPc(),
4926 calling_convention);
4927}
4928
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004929void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004930 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
4931 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004932}
4933
Calin Juravle2ae48182016-03-16 14:05:09 +00004934void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
4935 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004936 return;
4937 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004938 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004939
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004940 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00004941 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004942}
4943
Calin Juravle2ae48182016-03-16 14:05:09 +00004944void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004945 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004946 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004947
4948 LocationSummary* locations = instruction->GetLocations();
4949 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004950
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004951 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004952}
4953
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004954void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004955 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004956}
4957
Artem Serov6c916792016-07-11 14:02:34 +01004958static LoadOperandType GetLoadOperandType(Primitive::Type type) {
4959 switch (type) {
4960 case Primitive::kPrimNot:
4961 return kLoadWord;
4962 case Primitive::kPrimBoolean:
4963 return kLoadUnsignedByte;
4964 case Primitive::kPrimByte:
4965 return kLoadSignedByte;
4966 case Primitive::kPrimChar:
4967 return kLoadUnsignedHalfword;
4968 case Primitive::kPrimShort:
4969 return kLoadSignedHalfword;
4970 case Primitive::kPrimInt:
4971 return kLoadWord;
4972 case Primitive::kPrimLong:
4973 return kLoadWordPair;
4974 case Primitive::kPrimFloat:
4975 return kLoadSWord;
4976 case Primitive::kPrimDouble:
4977 return kLoadDWord;
4978 default:
4979 LOG(FATAL) << "Unreachable type " << type;
4980 UNREACHABLE();
4981 }
4982}
4983
4984static StoreOperandType GetStoreOperandType(Primitive::Type type) {
4985 switch (type) {
4986 case Primitive::kPrimNot:
4987 return kStoreWord;
4988 case Primitive::kPrimBoolean:
4989 case Primitive::kPrimByte:
4990 return kStoreByte;
4991 case Primitive::kPrimChar:
4992 case Primitive::kPrimShort:
4993 return kStoreHalfword;
4994 case Primitive::kPrimInt:
4995 return kStoreWord;
4996 case Primitive::kPrimLong:
4997 return kStoreWordPair;
4998 case Primitive::kPrimFloat:
4999 return kStoreSWord;
5000 case Primitive::kPrimDouble:
5001 return kStoreDWord;
5002 default:
5003 LOG(FATAL) << "Unreachable type " << type;
5004 UNREACHABLE();
5005 }
5006}
5007
5008void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
5009 Location out_loc,
5010 Register base,
5011 Register reg_offset,
5012 Condition cond) {
5013 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5014 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5015
5016 switch (type) {
5017 case Primitive::kPrimByte:
5018 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
5019 break;
5020 case Primitive::kPrimBoolean:
5021 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
5022 break;
5023 case Primitive::kPrimShort:
5024 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
5025 break;
5026 case Primitive::kPrimChar:
5027 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
5028 break;
5029 case Primitive::kPrimNot:
5030 case Primitive::kPrimInt:
5031 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
5032 break;
5033 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5034 case Primitive::kPrimLong:
5035 case Primitive::kPrimFloat:
5036 case Primitive::kPrimDouble:
5037 default:
5038 LOG(FATAL) << "Unreachable type " << type;
5039 UNREACHABLE();
5040 }
5041}
5042
5043void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
5044 Location loc,
5045 Register base,
5046 Register reg_offset,
5047 Condition cond) {
5048 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5049 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5050
5051 switch (type) {
5052 case Primitive::kPrimByte:
5053 case Primitive::kPrimBoolean:
5054 __ strb(loc.AsRegister<Register>(), mem_address, cond);
5055 break;
5056 case Primitive::kPrimShort:
5057 case Primitive::kPrimChar:
5058 __ strh(loc.AsRegister<Register>(), mem_address, cond);
5059 break;
5060 case Primitive::kPrimNot:
5061 case Primitive::kPrimInt:
5062 __ str(loc.AsRegister<Register>(), mem_address, cond);
5063 break;
5064 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5065 case Primitive::kPrimLong:
5066 case Primitive::kPrimFloat:
5067 case Primitive::kPrimDouble:
5068 default:
5069 LOG(FATAL) << "Unreachable type " << type;
5070 UNREACHABLE();
5071 }
5072}
5073
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005075 bool object_array_get_with_read_barrier =
5076 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005077 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005078 new (GetGraph()->GetArena()) LocationSummary(instruction,
5079 object_array_get_with_read_barrier ?
5080 LocationSummary::kCallOnSlowPath :
5081 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005082 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005083 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005084 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005085 locations->SetInAt(0, Location::RequiresRegister());
5086 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005087 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5088 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5089 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005090 // The output overlaps in the case of an object array get with
5091 // read barriers enabled: we do not want the move to overwrite the
5092 // array's location, as we need it to emit the read barrier.
5093 locations->SetOut(
5094 Location::RequiresRegister(),
5095 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005096 }
Roland Levillainc9285912015-12-18 10:38:42 +00005097 // We need a temporary register for the read barrier marking slow
5098 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07005099 // Also need for String compression feature.
5100 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
5101 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00005102 locations->AddTemp(Location::RequiresRegister());
5103 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005104}
5105
5106void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
5107 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005108 Location obj_loc = locations->InAt(0);
5109 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00005111 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005112 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00005113 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07005114 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5115 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01005116 HInstruction* array_instr = instruction->GetArray();
5117 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01005118
Roland Levillain4d027112015-07-01 15:41:14 +01005119 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01005120 case Primitive::kPrimBoolean:
5121 case Primitive::kPrimByte:
5122 case Primitive::kPrimShort:
5123 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00005124 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005125 Register length;
5126 if (maybe_compressed_char_at) {
5127 length = locations->GetTemp(0).AsRegister<Register>();
5128 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5129 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
5130 codegen_->MaybeRecordImplicitNullCheck(instruction);
5131 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005133 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07005134 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07005135 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005136 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5137 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5138 "Expecting 0=compressed, 1=uncompressed");
5139 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005140 __ LoadFromOffset(kLoadUnsignedByte,
5141 out_loc.AsRegister<Register>(),
5142 obj,
5143 data_offset + const_index);
5144 __ b(&done);
5145 __ Bind(&uncompressed_load);
5146 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5147 out_loc.AsRegister<Register>(),
5148 obj,
5149 data_offset + (const_index << 1));
5150 __ Bind(&done);
5151 } else {
5152 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01005153
jessicahandojo05765752016-09-09 19:01:32 -07005154 LoadOperandType load_type = GetLoadOperandType(type);
5155 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
5156 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005157 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005158 Register temp = IP;
5159
5160 if (has_intermediate_address) {
5161 // We do not need to compute the intermediate address from the array: the
5162 // input instruction has done it already. See the comment in
5163 // `TryExtractArrayAccessAddress()`.
5164 if (kIsDebugBuild) {
5165 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5166 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5167 }
5168 temp = obj;
5169 } else {
5170 __ add(temp, obj, ShifterOperand(data_offset));
5171 }
jessicahandojo05765752016-09-09 19:01:32 -07005172 if (maybe_compressed_char_at) {
5173 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005174 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5175 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5176 "Expecting 0=compressed, 1=uncompressed");
5177 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005178 __ ldrb(out_loc.AsRegister<Register>(),
5179 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
5180 __ b(&done);
5181 __ Bind(&uncompressed_load);
5182 __ ldrh(out_loc.AsRegister<Register>(),
5183 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
5184 __ Bind(&done);
5185 } else {
5186 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
5187 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005188 }
5189 break;
5190 }
5191
Roland Levillainc9285912015-12-18 10:38:42 +00005192 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00005193 // The read barrier instrumentation of object ArrayGet
5194 // instructions does not support the HIntermediateAddress
5195 // instruction.
5196 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5197
Roland Levillainc9285912015-12-18 10:38:42 +00005198 static_assert(
5199 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5200 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00005201 // /* HeapReference<Object> */ out =
5202 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5203 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5204 Location temp = locations->GetTemp(0);
5205 // Note that a potential implicit null check is handled in this
5206 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
5207 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5208 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5209 } else {
5210 Register out = out_loc.AsRegister<Register>();
5211 if (index.IsConstant()) {
5212 size_t offset =
5213 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5214 __ LoadFromOffset(kLoadWord, out, obj, offset);
5215 codegen_->MaybeRecordImplicitNullCheck(instruction);
5216 // If read barriers are enabled, emit read barriers other than
5217 // Baker's using a slow path (and also unpoison the loaded
5218 // reference, if heap poisoning is enabled).
5219 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5220 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005221 Register temp = IP;
5222
5223 if (has_intermediate_address) {
5224 // We do not need to compute the intermediate address from the array: the
5225 // input instruction has done it already. See the comment in
5226 // `TryExtractArrayAccessAddress()`.
5227 if (kIsDebugBuild) {
5228 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5229 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5230 }
5231 temp = obj;
5232 } else {
5233 __ add(temp, obj, ShifterOperand(data_offset));
5234 }
5235 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005236
Roland Levillainc9285912015-12-18 10:38:42 +00005237 codegen_->MaybeRecordImplicitNullCheck(instruction);
5238 // If read barriers are enabled, emit read barriers other than
5239 // Baker's using a slow path (and also unpoison the loaded
5240 // reference, if heap poisoning is enabled).
5241 codegen_->MaybeGenerateReadBarrierSlow(
5242 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5243 }
5244 }
5245 break;
5246 }
5247
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005248 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005249 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005250 size_t offset =
5251 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005252 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005253 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005254 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005255 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005256 }
5257 break;
5258 }
5259
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005260 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005261 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005262 if (index.IsConstant()) {
5263 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005264 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005265 } else {
5266 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005267 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005268 }
5269 break;
5270 }
5271
5272 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005273 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005274 if (index.IsConstant()) {
5275 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005276 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005277 } else {
5278 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005279 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005280 }
5281 break;
5282 }
5283
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005284 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005285 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005286 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005287 }
Roland Levillain4d027112015-07-01 15:41:14 +01005288
5289 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005290 // Potential implicit null checks, in the case of reference
5291 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005292 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005293 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005294 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005295}
5296
5297void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005298 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005299
5300 bool needs_write_barrier =
5301 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005302 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005303
Nicolas Geoffray39468442014-09-02 15:17:15 +01005304 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005305 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005306 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005307 LocationSummary::kCallOnSlowPath :
5308 LocationSummary::kNoCall);
5309
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005310 locations->SetInAt(0, Location::RequiresRegister());
5311 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5312 if (Primitive::IsFloatingPointType(value_type)) {
5313 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005314 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005315 locations->SetInAt(2, Location::RequiresRegister());
5316 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005317 if (needs_write_barrier) {
5318 // Temporary registers for the write barrier.
5319 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005320 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005321 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005322}
5323
5324void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5325 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005326 Location array_loc = locations->InAt(0);
5327 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005328 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005329 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005330 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005331 bool needs_write_barrier =
5332 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005333 uint32_t data_offset =
5334 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5335 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005336 HInstruction* array_instr = instruction->GetArray();
5337 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005338
5339 switch (value_type) {
5340 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005341 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005342 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005343 case Primitive::kPrimChar:
5344 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005345 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005346 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5347 uint32_t full_offset =
5348 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5349 StoreOperandType store_type = GetStoreOperandType(value_type);
5350 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005351 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005352 Register temp = IP;
5353
5354 if (has_intermediate_address) {
5355 // We do not need to compute the intermediate address from the array: the
5356 // input instruction has done it already. See the comment in
5357 // `TryExtractArrayAccessAddress()`.
5358 if (kIsDebugBuild) {
5359 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5360 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5361 }
5362 temp = array;
5363 } else {
5364 __ add(temp, array, ShifterOperand(data_offset));
5365 }
Artem Serov6c916792016-07-11 14:02:34 +01005366 codegen_->StoreToShiftedRegOffset(value_type,
5367 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005368 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005369 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005370 }
5371 break;
5372 }
5373
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005375 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005376 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5377 // See the comment in instruction_simplifier_shared.cc.
5378 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005379
5380 if (instruction->InputAt(2)->IsNullConstant()) {
5381 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005382 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005383 size_t offset =
5384 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005385 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005386 } else {
5387 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005388 __ add(IP, array, ShifterOperand(data_offset));
5389 codegen_->StoreToShiftedRegOffset(value_type,
5390 value_loc,
5391 IP,
5392 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005393 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005394 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005395 DCHECK(!needs_write_barrier);
5396 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005397 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005398 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005399
5400 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005401 Location temp1_loc = locations->GetTemp(0);
5402 Register temp1 = temp1_loc.AsRegister<Register>();
5403 Location temp2_loc = locations->GetTemp(1);
5404 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005405 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5406 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5407 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5408 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005409 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005410
Roland Levillain3b359c72015-11-17 19:35:12 +00005411 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005412 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5413 codegen_->AddSlowPath(slow_path);
5414 if (instruction->GetValueCanBeNull()) {
5415 Label non_zero;
5416 __ CompareAndBranchIfNonZero(value, &non_zero);
5417 if (index.IsConstant()) {
5418 size_t offset =
5419 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5420 __ StoreToOffset(kStoreWord, value, array, offset);
5421 } else {
5422 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005423 __ add(IP, array, ShifterOperand(data_offset));
5424 codegen_->StoreToShiftedRegOffset(value_type,
5425 value_loc,
5426 IP,
5427 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005428 }
5429 codegen_->MaybeRecordImplicitNullCheck(instruction);
5430 __ b(&done);
5431 __ Bind(&non_zero);
5432 }
5433
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005434 // Note that when read barriers are enabled, the type checks
5435 // are performed without read barriers. This is fine, even in
5436 // the case where a class object is in the from-space after
5437 // the flip, as a comparison involving such a type would not
5438 // produce a false positive; it may of course produce a false
5439 // negative, in which case we would take the ArraySet slow
5440 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005441
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005442 // /* HeapReference<Class> */ temp1 = array->klass_
5443 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5444 codegen_->MaybeRecordImplicitNullCheck(instruction);
5445 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005446
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005447 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5448 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5449 // /* HeapReference<Class> */ temp2 = value->klass_
5450 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5451 // If heap poisoning is enabled, no need to unpoison `temp1`
5452 // nor `temp2`, as we are comparing two poisoned references.
5453 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005454
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005455 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5456 Label do_put;
5457 __ b(&do_put, EQ);
5458 // If heap poisoning is enabled, the `temp1` reference has
5459 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005460 __ MaybeUnpoisonHeapReference(temp1);
5461
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005462 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5463 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5464 // If heap poisoning is enabled, no need to unpoison
5465 // `temp1`, as we are comparing against null below.
5466 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5467 __ Bind(&do_put);
5468 } else {
5469 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005470 }
5471 }
5472
Artem Serov6c916792016-07-11 14:02:34 +01005473 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005474 if (kPoisonHeapReferences) {
5475 // Note that in the case where `value` is a null reference,
5476 // we do not enter this block, as a null reference does not
5477 // need poisoning.
5478 DCHECK_EQ(value_type, Primitive::kPrimNot);
5479 __ Mov(temp1, value);
5480 __ PoisonHeapReference(temp1);
5481 source = temp1;
5482 }
5483
5484 if (index.IsConstant()) {
5485 size_t offset =
5486 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5487 __ StoreToOffset(kStoreWord, source, array, offset);
5488 } else {
5489 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005490
5491 __ add(IP, array, ShifterOperand(data_offset));
5492 codegen_->StoreToShiftedRegOffset(value_type,
5493 Location::RegisterLocation(source),
5494 IP,
5495 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005496 }
5497
Roland Levillain3b359c72015-11-17 19:35:12 +00005498 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005499 codegen_->MaybeRecordImplicitNullCheck(instruction);
5500 }
5501
5502 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5503
5504 if (done.IsLinked()) {
5505 __ Bind(&done);
5506 }
5507
5508 if (slow_path != nullptr) {
5509 __ Bind(slow_path->GetExitLabel());
5510 }
5511
5512 break;
5513 }
5514
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005515 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005516 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005517 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005518 size_t offset =
5519 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005520 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005521 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005522 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005523 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005524 }
5525 break;
5526 }
5527
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005528 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005529 Location value = locations->InAt(2);
5530 DCHECK(value.IsFpuRegister());
5531 if (index.IsConstant()) {
5532 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005533 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005534 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005535 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005536 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5537 }
5538 break;
5539 }
5540
5541 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005542 Location value = locations->InAt(2);
5543 DCHECK(value.IsFpuRegisterPair());
5544 if (index.IsConstant()) {
5545 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005546 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005547 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005548 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005549 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5550 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005551
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005552 break;
5553 }
5554
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005555 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005556 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005557 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005558 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005559
Roland Levillain80e67092016-01-08 16:04:55 +00005560 // Objects are handled in the switch.
5561 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005562 codegen_->MaybeRecordImplicitNullCheck(instruction);
5563 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005564}
5565
5566void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005567 LocationSummary* locations =
5568 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005569 locations->SetInAt(0, Location::RequiresRegister());
5570 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005571}
5572
5573void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5574 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005575 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005576 Register obj = locations->InAt(0).AsRegister<Register>();
5577 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005578 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005579 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005580 // Mask out compression flag from String's array length.
5581 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005582 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07005583 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005584}
5585
Artem Serov328429f2016-07-06 16:23:04 +01005586void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01005587 LocationSummary* locations =
5588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5589
5590 locations->SetInAt(0, Location::RequiresRegister());
5591 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
5592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5593}
5594
5595void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
5596 LocationSummary* locations = instruction->GetLocations();
5597 Location out = locations->Out();
5598 Location first = locations->InAt(0);
5599 Location second = locations->InAt(1);
5600
Artem Serov328429f2016-07-06 16:23:04 +01005601 if (second.IsRegister()) {
5602 __ add(out.AsRegister<Register>(),
5603 first.AsRegister<Register>(),
5604 ShifterOperand(second.AsRegister<Register>()));
5605 } else {
5606 __ AddConstant(out.AsRegister<Register>(),
5607 first.AsRegister<Register>(),
5608 second.GetConstant()->AsIntConstant()->GetValue());
5609 }
5610}
5611
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005612void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005613 RegisterSet caller_saves = RegisterSet::Empty();
5614 InvokeRuntimeCallingConvention calling_convention;
5615 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5616 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5617 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005618 locations->SetInAt(0, Location::RequiresRegister());
5619 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005620}
5621
5622void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
5623 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01005624 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005625 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005626 codegen_->AddSlowPath(slow_path);
5627
Roland Levillain271ab9c2014-11-27 15:23:57 +00005628 Register index = locations->InAt(0).AsRegister<Register>();
5629 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005630
5631 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01005632 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005633}
5634
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005635void CodeGeneratorARM::MarkGCCard(Register temp,
5636 Register card,
5637 Register object,
5638 Register value,
5639 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005640 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005641 if (can_be_null) {
5642 __ CompareAndBranchIfZero(value, &is_null);
5643 }
Andreas Gampe542451c2016-07-26 09:02:02 -07005644 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005645 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
5646 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005647 if (can_be_null) {
5648 __ Bind(&is_null);
5649 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005650}
5651
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005652void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005653 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005654}
5655
5656void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005657 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5658}
5659
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005660void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005661 LocationSummary* locations =
5662 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005663 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005664}
5665
5666void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005667 HBasicBlock* block = instruction->GetBlock();
5668 if (block->GetLoopInformation() != nullptr) {
5669 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5670 // The back edge will generate the suspend check.
5671 return;
5672 }
5673 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5674 // The goto will generate the suspend check.
5675 return;
5676 }
5677 GenerateSuspendCheck(instruction, nullptr);
5678}
5679
5680void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
5681 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005682 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005683 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
5684 if (slow_path == nullptr) {
5685 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
5686 instruction->SetSlowPath(slow_path);
5687 codegen_->AddSlowPath(slow_path);
5688 if (successor != nullptr) {
5689 DCHECK(successor->IsLoopHeader());
5690 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5691 }
5692 } else {
5693 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5694 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005695
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00005696 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07005697 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005698 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005699 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005700 __ Bind(slow_path->GetReturnLabel());
5701 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005702 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005703 __ b(slow_path->GetEntryLabel());
5704 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005705}
5706
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005707ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
5708 return codegen_->GetAssembler();
5709}
5710
5711void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005712 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005713 Location source = move->GetSource();
5714 Location destination = move->GetDestination();
5715
5716 if (source.IsRegister()) {
5717 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005718 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005719 } else if (destination.IsFpuRegister()) {
5720 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005721 } else {
5722 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005723 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005724 SP, destination.GetStackIndex());
5725 }
5726 } else if (source.IsStackSlot()) {
5727 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005728 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005729 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005730 } else if (destination.IsFpuRegister()) {
5731 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005732 } else {
5733 DCHECK(destination.IsStackSlot());
5734 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
5735 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5736 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005737 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005738 if (destination.IsRegister()) {
5739 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
5740 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005741 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005742 } else {
5743 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005744 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
5745 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005746 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005747 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005748 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
5749 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005750 } else if (destination.IsRegisterPair()) {
5751 DCHECK(ExpectedPairLayout(destination));
5752 __ LoadFromOffset(
5753 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
5754 } else {
5755 DCHECK(destination.IsFpuRegisterPair()) << destination;
5756 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5757 SP,
5758 source.GetStackIndex());
5759 }
5760 } else if (source.IsRegisterPair()) {
5761 if (destination.IsRegisterPair()) {
5762 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
5763 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005764 } else if (destination.IsFpuRegisterPair()) {
5765 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5766 source.AsRegisterPairLow<Register>(),
5767 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005768 } else {
5769 DCHECK(destination.IsDoubleStackSlot()) << destination;
5770 DCHECK(ExpectedPairLayout(source));
5771 __ StoreToOffset(
5772 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
5773 }
5774 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005775 if (destination.IsRegisterPair()) {
5776 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5777 destination.AsRegisterPairHigh<Register>(),
5778 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5779 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005780 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
5781 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
5782 } else {
5783 DCHECK(destination.IsDoubleStackSlot()) << destination;
5784 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
5785 SP,
5786 destination.GetStackIndex());
5787 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005788 } else {
5789 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005790 HConstant* constant = source.GetConstant();
5791 if (constant->IsIntConstant() || constant->IsNullConstant()) {
5792 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005793 if (destination.IsRegister()) {
5794 __ LoadImmediate(destination.AsRegister<Register>(), value);
5795 } else {
5796 DCHECK(destination.IsStackSlot());
5797 __ LoadImmediate(IP, value);
5798 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5799 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005800 } else if (constant->IsLongConstant()) {
5801 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005802 if (destination.IsRegisterPair()) {
5803 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
5804 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005805 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005806 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005807 __ LoadImmediate(IP, Low32Bits(value));
5808 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5809 __ LoadImmediate(IP, High32Bits(value));
5810 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5811 }
5812 } else if (constant->IsDoubleConstant()) {
5813 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005814 if (destination.IsFpuRegisterPair()) {
5815 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005816 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005817 DCHECK(destination.IsDoubleStackSlot()) << destination;
5818 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005819 __ LoadImmediate(IP, Low32Bits(int_value));
5820 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5821 __ LoadImmediate(IP, High32Bits(int_value));
5822 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
5823 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005824 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00005825 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005826 float value = constant->AsFloatConstant()->GetValue();
5827 if (destination.IsFpuRegister()) {
5828 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
5829 } else {
5830 DCHECK(destination.IsStackSlot());
5831 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
5832 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
5833 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005834 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005835 }
5836}
5837
5838void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
5839 __ Mov(IP, reg);
5840 __ LoadFromOffset(kLoadWord, reg, SP, mem);
5841 __ StoreToOffset(kStoreWord, IP, SP, mem);
5842}
5843
5844void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
5845 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
5846 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
5847 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
5848 SP, mem1 + stack_offset);
5849 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
5850 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
5851 SP, mem2 + stack_offset);
5852 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
5853}
5854
5855void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005856 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005857 Location source = move->GetSource();
5858 Location destination = move->GetDestination();
5859
5860 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005861 DCHECK_NE(source.AsRegister<Register>(), IP);
5862 DCHECK_NE(destination.AsRegister<Register>(), IP);
5863 __ Mov(IP, source.AsRegister<Register>());
5864 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
5865 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005866 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005867 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005868 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005869 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005870 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5871 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005872 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005873 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005874 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005875 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005876 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005877 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005878 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005879 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005880 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
5881 destination.AsRegisterPairHigh<Register>(),
5882 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005883 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005884 Register low_reg = source.IsRegisterPair()
5885 ? source.AsRegisterPairLow<Register>()
5886 : destination.AsRegisterPairLow<Register>();
5887 int mem = source.IsRegisterPair()
5888 ? destination.GetStackIndex()
5889 : source.GetStackIndex();
5890 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005891 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005892 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005893 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005894 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005895 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
5896 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005897 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005898 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005899 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005900 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
5901 DRegister reg = source.IsFpuRegisterPair()
5902 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
5903 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
5904 int mem = source.IsFpuRegisterPair()
5905 ? destination.GetStackIndex()
5906 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005907 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005908 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00005909 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005910 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
5911 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
5912 : destination.AsFpuRegister<SRegister>();
5913 int mem = source.IsFpuRegister()
5914 ? destination.GetStackIndex()
5915 : source.GetStackIndex();
5916
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005917 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00005918 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00005919 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005920 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005921 Exchange(source.GetStackIndex(), destination.GetStackIndex());
5922 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005923 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00005924 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005925 }
5926}
5927
5928void ParallelMoveResolverARM::SpillScratch(int reg) {
5929 __ Push(static_cast<Register>(reg));
5930}
5931
5932void ParallelMoveResolverARM::RestoreScratch(int reg) {
5933 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005934}
5935
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005936HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
5937 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005938 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00005939 case HLoadClass::LoadKind::kInvalid:
5940 LOG(FATAL) << "UNREACHABLE";
5941 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005942 case HLoadClass::LoadKind::kReferrersClass:
5943 break;
5944 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5945 DCHECK(!GetCompilerOptions().GetCompilePic());
5946 break;
5947 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5948 DCHECK(GetCompilerOptions().GetCompilePic());
5949 break;
5950 case HLoadClass::LoadKind::kBootImageAddress:
5951 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005952 case HLoadClass::LoadKind::kBssEntry:
5953 DCHECK(!Runtime::Current()->UseJitCompilation());
5954 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00005955 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00005956 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005957 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005958 case HLoadClass::LoadKind::kDexCacheViaMethod:
5959 break;
5960 }
5961 return desired_class_load_kind;
5962}
5963
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005964void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00005965 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5966 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005967 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00005968 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005969 cls,
5970 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00005971 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00005972 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005973 return;
5974 }
Vladimir Marko41559982017-01-06 14:04:23 +00005975 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005976
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005977 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
5978 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005979 ? LocationSummary::kCallOnSlowPath
5980 : LocationSummary::kNoCall;
5981 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07005982 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005983 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005984 }
5985
Vladimir Marko41559982017-01-06 14:04:23 +00005986 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005987 locations->SetInAt(0, Location::RequiresRegister());
5988 }
5989 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00005990 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
5991 if (!kUseReadBarrier || kUseBakerReadBarrier) {
5992 // Rely on the type resolution or initialization and marking to save everything we need.
5993 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
5994 // to the custom calling convention) or by marking, so we request a different temp.
5995 locations->AddTemp(Location::RequiresRegister());
5996 RegisterSet caller_saves = RegisterSet::Empty();
5997 InvokeRuntimeCallingConvention calling_convention;
5998 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5999 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6000 // that the the kPrimNot result register is the same as the first argument register.
6001 locations->SetCustomSlowPathCallerSaves(caller_saves);
6002 } else {
6003 // For non-Baker read barrier we have a temp-clobbering call.
6004 }
6005 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006006}
6007
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006008// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6009// move.
6010void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006011 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6012 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6013 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006014 return;
6015 }
Vladimir Marko41559982017-01-06 14:04:23 +00006016 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006017
Vladimir Marko41559982017-01-06 14:04:23 +00006018 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006019 Location out_loc = locations->Out();
6020 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006021
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006022 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6023 ? kWithoutReadBarrier
6024 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006025 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006026 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006027 case HLoadClass::LoadKind::kReferrersClass: {
6028 DCHECK(!cls->CanCallRuntime());
6029 DCHECK(!cls->MustGenerateClinitCheck());
6030 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6031 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006032 GenerateGcRootFieldLoad(cls,
6033 out_loc,
6034 current_method,
6035 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006036 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006037 break;
6038 }
6039 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006040 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006041 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006042 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6043 cls->GetTypeIndex()));
6044 break;
6045 }
6046 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006047 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006048 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006049 CodeGeneratorARM::PcRelativePatchInfo* labels =
6050 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6051 __ BindTrackedLabel(&labels->movw_label);
6052 __ movw(out, /* placeholder */ 0u);
6053 __ BindTrackedLabel(&labels->movt_label);
6054 __ movt(out, /* placeholder */ 0u);
6055 __ BindTrackedLabel(&labels->add_pc_label);
6056 __ add(out, out, ShifterOperand(PC));
6057 break;
6058 }
6059 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006060 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006061 uint32_t address = dchecked_integral_cast<uint32_t>(
6062 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6063 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006064 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6065 break;
6066 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006067 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006068 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6069 ? locations->GetTemp(0).AsRegister<Register>()
6070 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006071 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006072 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006073 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006074 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006075 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006076 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006077 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006078 __ add(temp, temp, ShifterOperand(PC));
6079 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006080 generate_null_check = true;
6081 break;
6082 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006083 case HLoadClass::LoadKind::kJitTableAddress: {
6084 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6085 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006086 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006087 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006088 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006089 break;
6090 }
Vladimir Marko41559982017-01-06 14:04:23 +00006091 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006092 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006093 LOG(FATAL) << "UNREACHABLE";
6094 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006095 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006096
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006097 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6098 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01006099 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006100 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6101 codegen_->AddSlowPath(slow_path);
6102 if (generate_null_check) {
6103 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6104 }
6105 if (cls->MustGenerateClinitCheck()) {
6106 GenerateClassInitializationCheck(slow_path, out);
6107 } else {
6108 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006109 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006110 }
6111}
6112
6113void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
6114 LocationSummary* locations =
6115 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6116 locations->SetInAt(0, Location::RequiresRegister());
6117 if (check->HasUses()) {
6118 locations->SetOut(Location::SameAsFirstInput());
6119 }
6120}
6121
6122void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006123 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006124 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006125 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006126 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006127 GenerateClassInitializationCheck(slow_path,
6128 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006129}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006130
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006131void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01006132 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006133 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
6134 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
6135 __ b(slow_path->GetEntryLabel(), LT);
6136 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6137 // properly. Therefore, we do a memory fence.
6138 __ dmb(ISH);
6139 __ Bind(slow_path->GetExitLabel());
6140}
6141
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006142HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
6143 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006144 switch (desired_string_load_kind) {
6145 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6146 DCHECK(!GetCompilerOptions().GetCompilePic());
6147 break;
6148 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6149 DCHECK(GetCompilerOptions().GetCompilePic());
6150 break;
6151 case HLoadString::LoadKind::kBootImageAddress:
6152 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006153 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006154 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006155 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006156 case HLoadString::LoadKind::kJitTableAddress:
6157 DCHECK(Runtime::Current()->UseJitCompilation());
6158 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006159 case HLoadString::LoadKind::kDexCacheViaMethod:
6160 break;
6161 }
6162 return desired_string_load_kind;
6163}
6164
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006165void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006166 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006167 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006168 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006169 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006170 locations->SetOut(Location::RegisterLocation(R0));
6171 } else {
6172 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006173 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6174 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006175 // Rely on the pResolveString and marking to save everything we need, including temps.
6176 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6177 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006178 locations->AddTemp(Location::RequiresRegister());
6179 RegisterSet caller_saves = RegisterSet::Empty();
6180 InvokeRuntimeCallingConvention calling_convention;
6181 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6182 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6183 // that the the kPrimNot result register is the same as the first argument register.
6184 locations->SetCustomSlowPathCallerSaves(caller_saves);
6185 } else {
6186 // For non-Baker read barrier we have a temp-clobbering call.
6187 }
6188 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006189 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006190}
6191
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006192// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6193// move.
6194void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006195 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006196 Location out_loc = locations->Out();
6197 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006198 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00006199
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006200 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006201 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006202 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006203 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6204 load->GetStringIndex()));
6205 return; // No dex cache slow path.
6206 }
6207 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006208 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006209 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006210 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006211 __ BindTrackedLabel(&labels->movw_label);
6212 __ movw(out, /* placeholder */ 0u);
6213 __ BindTrackedLabel(&labels->movt_label);
6214 __ movt(out, /* placeholder */ 0u);
6215 __ BindTrackedLabel(&labels->add_pc_label);
6216 __ add(out, out, ShifterOperand(PC));
6217 return; // No dex cache slow path.
6218 }
6219 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006220 uint32_t address = dchecked_integral_cast<uint32_t>(
6221 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6222 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006223 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6224 return; // No dex cache slow path.
6225 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006226 case HLoadString::LoadKind::kBssEntry: {
6227 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006228 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6229 ? locations->GetTemp(0).AsRegister<Register>()
6230 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006231 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006232 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006233 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006234 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006235 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006236 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006237 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006238 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006239 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006240 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
6241 codegen_->AddSlowPath(slow_path);
6242 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6243 __ Bind(slow_path->GetExitLabel());
6244 return;
6245 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006246 case HLoadString::LoadKind::kJitTableAddress: {
6247 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006248 load->GetStringIndex(),
6249 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006250 // /* GcRoot<mirror::String> */ out = *out
6251 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6252 return;
6253 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006254 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006255 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006256 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006257
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006258 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
6259 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6260 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006261 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006262 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006263 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6264 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006265}
6266
David Brazdilcb1c0552015-08-04 16:22:25 +01006267static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006268 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006269}
6270
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006271void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6272 LocationSummary* locations =
6273 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6274 locations->SetOut(Location::RequiresRegister());
6275}
6276
6277void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006278 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006279 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6280}
6281
6282void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6283 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6284}
6285
6286void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006287 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006288 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006289}
6290
6291void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6292 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006293 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006294 InvokeRuntimeCallingConvention calling_convention;
6295 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6296}
6297
6298void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006299 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006300 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006301}
6302
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006303// Temp is used for read barrier.
6304static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6305 if (kEmitCompilerReadBarrier &&
6306 (kUseBakerReadBarrier ||
6307 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6308 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6309 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6310 return 1;
6311 }
6312 return 0;
6313}
6314
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006315// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006316// interface pointer, one for loading the current interface.
6317// The other checks have one temp for loading the object's class.
6318static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6319 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6320 return 3;
6321 }
6322 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006323}
6324
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006325void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006326 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006327 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006328 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006329 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006330 case TypeCheckKind::kExactCheck:
6331 case TypeCheckKind::kAbstractClassCheck:
6332 case TypeCheckKind::kClassHierarchyCheck:
6333 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006334 call_kind =
6335 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006336 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006337 break;
6338 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006339 case TypeCheckKind::kUnresolvedCheck:
6340 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006341 call_kind = LocationSummary::kCallOnSlowPath;
6342 break;
6343 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006344
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006345 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006346 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006347 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006348 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006349 locations->SetInAt(0, Location::RequiresRegister());
6350 locations->SetInAt(1, Location::RequiresRegister());
6351 // The "out" register is used as a temporary, so it overlaps with the inputs.
6352 // Note that TypeCheckSlowPathARM uses this register too.
6353 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006354 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006355}
6356
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006357void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006358 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006359 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006360 Location obj_loc = locations->InAt(0);
6361 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006362 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006363 Location out_loc = locations->Out();
6364 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006365 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6366 DCHECK_LE(num_temps, 1u);
6367 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006368 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006369 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6370 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6371 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006372 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006373 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006374
6375 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006376 // avoid null check if we know obj is not null.
6377 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006378 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006379 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006380
Roland Levillainc9285912015-12-18 10:38:42 +00006381 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006382 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006383 // /* HeapReference<Class> */ out = obj->klass_
6384 GenerateReferenceLoadTwoRegisters(instruction,
6385 out_loc,
6386 obj_loc,
6387 class_offset,
6388 maybe_temp_loc,
6389 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006390 __ cmp(out, ShifterOperand(cls));
6391 // Classes must be equal for the instanceof to succeed.
6392 __ b(&zero, NE);
6393 __ LoadImmediate(out, 1);
6394 __ b(&done);
6395 break;
6396 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006397
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006398 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006399 // /* HeapReference<Class> */ out = obj->klass_
6400 GenerateReferenceLoadTwoRegisters(instruction,
6401 out_loc,
6402 obj_loc,
6403 class_offset,
6404 maybe_temp_loc,
6405 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 // If the class is abstract, we eagerly fetch the super class of the
6407 // object to avoid doing a comparison we know will fail.
6408 Label loop;
6409 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006410 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006411 GenerateReferenceLoadOneRegister(instruction,
6412 out_loc,
6413 super_offset,
6414 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006415 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006416 // If `out` is null, we use it for the result, and jump to `done`.
6417 __ CompareAndBranchIfZero(out, &done);
6418 __ cmp(out, ShifterOperand(cls));
6419 __ b(&loop, NE);
6420 __ LoadImmediate(out, 1);
6421 if (zero.IsLinked()) {
6422 __ b(&done);
6423 }
6424 break;
6425 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006426
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006427 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006428 // /* HeapReference<Class> */ out = obj->klass_
6429 GenerateReferenceLoadTwoRegisters(instruction,
6430 out_loc,
6431 obj_loc,
6432 class_offset,
6433 maybe_temp_loc,
6434 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006435 // Walk over the class hierarchy to find a match.
6436 Label loop, success;
6437 __ Bind(&loop);
6438 __ cmp(out, ShifterOperand(cls));
6439 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006440 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006441 GenerateReferenceLoadOneRegister(instruction,
6442 out_loc,
6443 super_offset,
6444 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006445 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006446 __ CompareAndBranchIfNonZero(out, &loop);
6447 // If `out` is null, we use it for the result, and jump to `done`.
6448 __ b(&done);
6449 __ Bind(&success);
6450 __ LoadImmediate(out, 1);
6451 if (zero.IsLinked()) {
6452 __ b(&done);
6453 }
6454 break;
6455 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006456
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006457 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006458 // /* HeapReference<Class> */ out = obj->klass_
6459 GenerateReferenceLoadTwoRegisters(instruction,
6460 out_loc,
6461 obj_loc,
6462 class_offset,
6463 maybe_temp_loc,
6464 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006465 // Do an exact check.
6466 Label exact_check;
6467 __ cmp(out, ShifterOperand(cls));
6468 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006469 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006470 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006471 GenerateReferenceLoadOneRegister(instruction,
6472 out_loc,
6473 component_offset,
6474 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006475 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006476 // If `out` is null, we use it for the result, and jump to `done`.
6477 __ CompareAndBranchIfZero(out, &done);
6478 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6479 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6480 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006481 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006482 __ LoadImmediate(out, 1);
6483 __ b(&done);
6484 break;
6485 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006486
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006487 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006488 // No read barrier since the slow path will retry upon failure.
6489 // /* HeapReference<Class> */ out = obj->klass_
6490 GenerateReferenceLoadTwoRegisters(instruction,
6491 out_loc,
6492 obj_loc,
6493 class_offset,
6494 maybe_temp_loc,
6495 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006496 __ cmp(out, ShifterOperand(cls));
6497 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006498 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6499 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006500 codegen_->AddSlowPath(slow_path);
6501 __ b(slow_path->GetEntryLabel(), NE);
6502 __ LoadImmediate(out, 1);
6503 if (zero.IsLinked()) {
6504 __ b(&done);
6505 }
6506 break;
6507 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006508
Calin Juravle98893e12015-10-02 21:05:03 +01006509 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006510 case TypeCheckKind::kInterfaceCheck: {
6511 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006512 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006513 // cases.
6514 //
6515 // We cannot directly call the InstanceofNonTrivial runtime
6516 // entry point without resorting to a type checking slow path
6517 // here (i.e. by calling InvokeRuntime directly), as it would
6518 // require to assign fixed registers for the inputs of this
6519 // HInstanceOf instruction (following the runtime calling
6520 // convention), which might be cluttered by the potential first
6521 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006522 //
6523 // TODO: Introduce a new runtime entry point taking the object
6524 // to test (instead of its class) as argument, and let it deal
6525 // with the read barrier issues. This will let us refactor this
6526 // case of the `switch` code as it was previously (with a direct
6527 // call to the runtime not using a type checking slow path).
6528 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006529 DCHECK(locations->OnlyCallsOnSlowPath());
6530 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6531 /* is_fatal */ false);
6532 codegen_->AddSlowPath(slow_path);
6533 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006534 if (zero.IsLinked()) {
6535 __ b(&done);
6536 }
6537 break;
6538 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006539 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006540
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006541 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006542 __ Bind(&zero);
6543 __ LoadImmediate(out, 0);
6544 }
6545
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006546 if (done.IsLinked()) {
6547 __ Bind(&done);
6548 }
6549
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006550 if (slow_path != nullptr) {
6551 __ Bind(slow_path->GetExitLabel());
6552 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006553}
6554
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006555void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006556 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6557 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6558
Roland Levillain3b359c72015-11-17 19:35:12 +00006559 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6560 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006561 case TypeCheckKind::kExactCheck:
6562 case TypeCheckKind::kAbstractClassCheck:
6563 case TypeCheckKind::kClassHierarchyCheck:
6564 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006565 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6566 LocationSummary::kCallOnSlowPath :
6567 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006568 break;
6569 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006570 case TypeCheckKind::kUnresolvedCheck:
6571 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006572 call_kind = LocationSummary::kCallOnSlowPath;
6573 break;
6574 }
6575
Roland Levillain3b359c72015-11-17 19:35:12 +00006576 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6577 locations->SetInAt(0, Location::RequiresRegister());
6578 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006579 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006580}
6581
6582void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006583 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006584 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006585 Location obj_loc = locations->InAt(0);
6586 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006587 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006588 Location temp_loc = locations->GetTemp(0);
6589 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006590 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6591 DCHECK_LE(num_temps, 3u);
6592 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6593 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
6594 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6595 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6596 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6597 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6598 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6599 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6600 const uint32_t object_array_data_offset =
6601 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006602
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006603 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6604 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6605 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006606 bool is_type_check_slow_path_fatal = false;
6607 if (!kEmitCompilerReadBarrier) {
6608 is_type_check_slow_path_fatal =
6609 (type_check_kind == TypeCheckKind::kExactCheck ||
6610 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6611 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6612 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6613 !instruction->CanThrowIntoCatchBlock();
6614 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01006615 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00006616 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6617 is_type_check_slow_path_fatal);
6618 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006619
6620 Label done;
6621 // Avoid null check if we know obj is not null.
6622 if (instruction->MustDoNullCheck()) {
6623 __ CompareAndBranchIfZero(obj, &done);
6624 }
6625
Roland Levillain3b359c72015-11-17 19:35:12 +00006626 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006627 case TypeCheckKind::kExactCheck:
6628 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006629 // /* HeapReference<Class> */ temp = obj->klass_
6630 GenerateReferenceLoadTwoRegisters(instruction,
6631 temp_loc,
6632 obj_loc,
6633 class_offset,
6634 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006635 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006636
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006637 __ cmp(temp, ShifterOperand(cls));
6638 // Jump to slow path for throwing the exception or doing a
6639 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00006640 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006641 break;
6642 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006643
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006644 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006645 // /* HeapReference<Class> */ temp = obj->klass_
6646 GenerateReferenceLoadTwoRegisters(instruction,
6647 temp_loc,
6648 obj_loc,
6649 class_offset,
6650 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006651 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006652
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006653 // If the class is abstract, we eagerly fetch the super class of the
6654 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006655 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006656 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006657 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006658 GenerateReferenceLoadOneRegister(instruction,
6659 temp_loc,
6660 super_offset,
6661 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006662 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006663
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006664 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6665 // exception.
6666 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00006667
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006668 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006669 __ cmp(temp, ShifterOperand(cls));
6670 __ b(&loop, NE);
6671 break;
6672 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006673
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006674 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006675 // /* HeapReference<Class> */ temp = obj->klass_
6676 GenerateReferenceLoadTwoRegisters(instruction,
6677 temp_loc,
6678 obj_loc,
6679 class_offset,
6680 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006681 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006682
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006683 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006684 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006685 __ Bind(&loop);
6686 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006687 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006688
Roland Levillain3b359c72015-11-17 19:35:12 +00006689 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006690 GenerateReferenceLoadOneRegister(instruction,
6691 temp_loc,
6692 super_offset,
6693 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006694 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00006695
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006696 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6697 // exception.
6698 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6699 // Otherwise, jump to the beginning of the loop.
6700 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006701 break;
6702 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006703
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006704 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006705 // /* HeapReference<Class> */ temp = obj->klass_
6706 GenerateReferenceLoadTwoRegisters(instruction,
6707 temp_loc,
6708 obj_loc,
6709 class_offset,
6710 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006711 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006712
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006713 // Do an exact check.
6714 __ cmp(temp, ShifterOperand(cls));
6715 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006716
6717 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006718 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006719 GenerateReferenceLoadOneRegister(instruction,
6720 temp_loc,
6721 component_offset,
6722 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006723 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006724 // If the component type is null, jump to the slow path to throw the exception.
6725 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
6726 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
6727 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006728 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00006729 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006730 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006731 break;
6732 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006733
Calin Juravle98893e12015-10-02 21:05:03 +01006734 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006735 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00006736 // We cannot directly call the CheckCast runtime entry point
6737 // without resorting to a type checking slow path here (i.e. by
6738 // calling InvokeRuntime directly), as it would require to
6739 // assign fixed registers for the inputs of this HInstanceOf
6740 // instruction (following the runtime calling convention), which
6741 // might be cluttered by the potential first read barrier
6742 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006743
Roland Levillain3b359c72015-11-17 19:35:12 +00006744 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006745 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006746
6747 case TypeCheckKind::kInterfaceCheck: {
6748 // Avoid read barriers to improve performance of the fast path. We can not get false
6749 // positives by doing this.
6750 // /* HeapReference<Class> */ temp = obj->klass_
6751 GenerateReferenceLoadTwoRegisters(instruction,
6752 temp_loc,
6753 obj_loc,
6754 class_offset,
6755 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006756 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006757
6758 // /* HeapReference<Class> */ temp = temp->iftable_
6759 GenerateReferenceLoadTwoRegisters(instruction,
6760 temp_loc,
6761 temp_loc,
6762 iftable_offset,
6763 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006764 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006765 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006766 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006767 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006768 Label start_loop;
6769 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006770 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
6771 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006772 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
6773 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006774 // Go to next interface.
6775 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
6776 __ sub(maybe_temp2_loc.AsRegister<Register>(),
6777 maybe_temp2_loc.AsRegister<Register>(),
6778 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08006779 // Compare the classes and continue the loop if they do not match.
6780 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
6781 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006782 break;
6783 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006784 }
6785 __ Bind(&done);
6786
Roland Levillain3b359c72015-11-17 19:35:12 +00006787 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006788}
6789
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006790void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
6791 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006792 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006793 InvokeRuntimeCallingConvention calling_convention;
6794 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6795}
6796
6797void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006798 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
6799 instruction,
6800 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006801 if (instruction->IsEnter()) {
6802 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6803 } else {
6804 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6805 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006806}
6807
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006808void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
6809void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
6810void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006811
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006812void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006813 LocationSummary* locations =
6814 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6815 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6816 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006817 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006818 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006819 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00006820 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006821}
6822
6823void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
6824 HandleBitwiseOperation(instruction);
6825}
6826
6827void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
6828 HandleBitwiseOperation(instruction);
6829}
6830
6831void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
6832 HandleBitwiseOperation(instruction);
6833}
6834
Artem Serov7fc63502016-02-09 17:15:29 +00006835
6836void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6837 LocationSummary* locations =
6838 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6839 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6840 || instruction->GetResultType() == Primitive::kPrimLong);
6841
6842 locations->SetInAt(0, Location::RequiresRegister());
6843 locations->SetInAt(1, Location::RequiresRegister());
6844 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6845}
6846
6847void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
6848 LocationSummary* locations = instruction->GetLocations();
6849 Location first = locations->InAt(0);
6850 Location second = locations->InAt(1);
6851 Location out = locations->Out();
6852
6853 if (instruction->GetResultType() == Primitive::kPrimInt) {
6854 Register first_reg = first.AsRegister<Register>();
6855 ShifterOperand second_reg(second.AsRegister<Register>());
6856 Register out_reg = out.AsRegister<Register>();
6857
6858 switch (instruction->GetOpKind()) {
6859 case HInstruction::kAnd:
6860 __ bic(out_reg, first_reg, second_reg);
6861 break;
6862 case HInstruction::kOr:
6863 __ orn(out_reg, first_reg, second_reg);
6864 break;
6865 // There is no EON on arm.
6866 case HInstruction::kXor:
6867 default:
6868 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6869 UNREACHABLE();
6870 }
6871 return;
6872
6873 } else {
6874 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6875 Register first_low = first.AsRegisterPairLow<Register>();
6876 Register first_high = first.AsRegisterPairHigh<Register>();
6877 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
6878 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
6879 Register out_low = out.AsRegisterPairLow<Register>();
6880 Register out_high = out.AsRegisterPairHigh<Register>();
6881
6882 switch (instruction->GetOpKind()) {
6883 case HInstruction::kAnd:
6884 __ bic(out_low, first_low, second_low);
6885 __ bic(out_high, first_high, second_high);
6886 break;
6887 case HInstruction::kOr:
6888 __ orn(out_low, first_low, second_low);
6889 __ orn(out_high, first_high, second_high);
6890 break;
6891 // There is no EON on arm.
6892 case HInstruction::kXor:
6893 default:
6894 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
6895 UNREACHABLE();
6896 }
6897 }
6898}
6899
Anton Kirilov74234da2017-01-13 14:42:47 +00006900void LocationsBuilderARM::VisitDataProcWithShifterOp(
6901 HDataProcWithShifterOp* instruction) {
6902 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
6903 instruction->GetType() == Primitive::kPrimLong);
6904 LocationSummary* locations =
6905 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6906 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
6907 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
6908
6909 locations->SetInAt(0, Location::RequiresRegister());
6910 locations->SetInAt(1, Location::RequiresRegister());
6911 locations->SetOut(Location::RequiresRegister(),
6912 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
6913}
6914
6915void InstructionCodeGeneratorARM::VisitDataProcWithShifterOp(
6916 HDataProcWithShifterOp* instruction) {
6917 const LocationSummary* const locations = instruction->GetLocations();
6918 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
6919 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
6920 const Location left = locations->InAt(0);
6921 const Location right = locations->InAt(1);
6922 const Location out = locations->Out();
6923
6924 if (instruction->GetType() == Primitive::kPrimInt) {
6925 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
6926
6927 const Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
6928 ? right.AsRegisterPairLow<Register>()
6929 : right.AsRegister<Register>();
6930
6931 GenerateDataProcInstruction(kind,
6932 out.AsRegister<Register>(),
6933 left.AsRegister<Register>(),
6934 ShifterOperand(second,
6935 ShiftFromOpKind(op_kind),
6936 instruction->GetShiftAmount()),
6937 codegen_);
6938 } else {
6939 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
6940
6941 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
6942 const Register second = right.AsRegister<Register>();
6943
6944 DCHECK_NE(out.AsRegisterPairLow<Register>(), second);
6945 GenerateDataProc(kind,
6946 out,
6947 left,
6948 ShifterOperand(second),
6949 ShifterOperand(second, ASR, 31),
6950 codegen_);
6951 } else {
6952 GenerateLongDataProc(instruction, codegen_);
6953 }
6954 }
6955}
6956
Vladimir Markod2b4ca22015-09-14 15:13:26 +01006957void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
6958 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
6959 if (value == 0xffffffffu) {
6960 if (out != first) {
6961 __ mov(out, ShifterOperand(first));
6962 }
6963 return;
6964 }
6965 if (value == 0u) {
6966 __ mov(out, ShifterOperand(0));
6967 return;
6968 }
6969 ShifterOperand so;
6970 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
6971 __ and_(out, first, so);
6972 } else {
6973 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
6974 __ bic(out, first, ShifterOperand(~value));
6975 }
6976}
6977
6978void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
6979 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
6980 if (value == 0u) {
6981 if (out != first) {
6982 __ mov(out, ShifterOperand(first));
6983 }
6984 return;
6985 }
6986 if (value == 0xffffffffu) {
6987 __ mvn(out, ShifterOperand(0));
6988 return;
6989 }
6990 ShifterOperand so;
6991 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
6992 __ orr(out, first, so);
6993 } else {
6994 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
6995 __ orn(out, first, ShifterOperand(~value));
6996 }
6997}
6998
6999void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
7000 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7001 if (value == 0u) {
7002 if (out != first) {
7003 __ mov(out, ShifterOperand(first));
7004 }
7005 return;
7006 }
7007 __ eor(out, first, ShifterOperand(value));
7008}
7009
Vladimir Marko59751a72016-08-05 14:37:27 +01007010void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
7011 Location first,
7012 uint64_t value) {
7013 Register out_low = out.AsRegisterPairLow<Register>();
7014 Register out_high = out.AsRegisterPairHigh<Register>();
7015 Register first_low = first.AsRegisterPairLow<Register>();
7016 Register first_high = first.AsRegisterPairHigh<Register>();
7017 uint32_t value_low = Low32Bits(value);
7018 uint32_t value_high = High32Bits(value);
7019 if (value_low == 0u) {
7020 if (out_low != first_low) {
7021 __ mov(out_low, ShifterOperand(first_low));
7022 }
7023 __ AddConstant(out_high, first_high, value_high);
7024 return;
7025 }
7026 __ AddConstantSetFlags(out_low, first_low, value_low);
7027 ShifterOperand so;
7028 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
7029 __ adc(out_high, first_high, so);
7030 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
7031 __ sbc(out_high, first_high, so);
7032 } else {
7033 LOG(FATAL) << "Unexpected constant " << value_high;
7034 UNREACHABLE();
7035 }
7036}
7037
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007038void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
7039 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007040 Location first = locations->InAt(0);
7041 Location second = locations->InAt(1);
7042 Location out = locations->Out();
7043
7044 if (second.IsConstant()) {
7045 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7046 uint32_t value_low = Low32Bits(value);
7047 if (instruction->GetResultType() == Primitive::kPrimInt) {
7048 Register first_reg = first.AsRegister<Register>();
7049 Register out_reg = out.AsRegister<Register>();
7050 if (instruction->IsAnd()) {
7051 GenerateAndConst(out_reg, first_reg, value_low);
7052 } else if (instruction->IsOr()) {
7053 GenerateOrrConst(out_reg, first_reg, value_low);
7054 } else {
7055 DCHECK(instruction->IsXor());
7056 GenerateEorConst(out_reg, first_reg, value_low);
7057 }
7058 } else {
7059 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7060 uint32_t value_high = High32Bits(value);
7061 Register first_low = first.AsRegisterPairLow<Register>();
7062 Register first_high = first.AsRegisterPairHigh<Register>();
7063 Register out_low = out.AsRegisterPairLow<Register>();
7064 Register out_high = out.AsRegisterPairHigh<Register>();
7065 if (instruction->IsAnd()) {
7066 GenerateAndConst(out_low, first_low, value_low);
7067 GenerateAndConst(out_high, first_high, value_high);
7068 } else if (instruction->IsOr()) {
7069 GenerateOrrConst(out_low, first_low, value_low);
7070 GenerateOrrConst(out_high, first_high, value_high);
7071 } else {
7072 DCHECK(instruction->IsXor());
7073 GenerateEorConst(out_low, first_low, value_low);
7074 GenerateEorConst(out_high, first_high, value_high);
7075 }
7076 }
7077 return;
7078 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007079
7080 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007081 Register first_reg = first.AsRegister<Register>();
7082 ShifterOperand second_reg(second.AsRegister<Register>());
7083 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007084 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007085 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007086 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007087 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007088 } else {
7089 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007090 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007091 }
7092 } else {
7093 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007094 Register first_low = first.AsRegisterPairLow<Register>();
7095 Register first_high = first.AsRegisterPairHigh<Register>();
7096 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7097 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7098 Register out_low = out.AsRegisterPairLow<Register>();
7099 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007100 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007101 __ and_(out_low, first_low, second_low);
7102 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007103 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007104 __ orr(out_low, first_low, second_low);
7105 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007106 } else {
7107 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007108 __ eor(out_low, first_low, second_low);
7109 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007110 }
7111 }
7112}
7113
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007114void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
7115 HInstruction* instruction,
7116 Location out,
7117 uint32_t offset,
7118 Location maybe_temp,
7119 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007120 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007121 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007122 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007123 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007124 if (kUseBakerReadBarrier) {
7125 // Load with fast path based Baker's read barrier.
7126 // /* HeapReference<Object> */ out = *(out + offset)
7127 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007128 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007129 } else {
7130 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007131 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00007132 // in the following move operation, as we will need it for the
7133 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007134 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00007135 // /* HeapReference<Object> */ out = *(out + offset)
7136 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007137 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00007138 }
7139 } else {
7140 // Plain load with no read barrier.
7141 // /* HeapReference<Object> */ out = *(out + offset)
7142 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7143 __ MaybeUnpoisonHeapReference(out_reg);
7144 }
7145}
7146
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007147void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
7148 HInstruction* instruction,
7149 Location out,
7150 Location obj,
7151 uint32_t offset,
7152 Location maybe_temp,
7153 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007154 Register out_reg = out.AsRegister<Register>();
7155 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007156 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007157 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007158 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007159 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007160 // Load with fast path based Baker's read barrier.
7161 // /* HeapReference<Object> */ out = *(obj + offset)
7162 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007163 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007164 } else {
7165 // Load with slow path based read barrier.
7166 // /* HeapReference<Object> */ out = *(obj + offset)
7167 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7168 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7169 }
7170 } else {
7171 // Plain load with no read barrier.
7172 // /* HeapReference<Object> */ out = *(obj + offset)
7173 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7174 __ MaybeUnpoisonHeapReference(out_reg);
7175 }
7176}
7177
7178void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
7179 Location root,
7180 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007181 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007182 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007183 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007184 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007185 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007186 if (kUseBakerReadBarrier) {
7187 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7188 // Baker's read barrier are used:
7189 //
7190 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007191 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7192 // if (temp != null) {
7193 // root = temp(root)
Roland Levillainc9285912015-12-18 10:38:42 +00007194 // }
7195
7196 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7197 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7198 static_assert(
7199 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7200 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7201 "have different sizes.");
7202 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7203 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7204 "have different sizes.");
7205
Vladimir Marko953437b2016-08-24 08:30:46 +00007206 // Slow path marking the GC root `root`.
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007207 Location temp = Location::RegisterLocation(LR);
Artem Serovf4d6aee2016-07-11 10:41:45 +01007208 SlowPathCodeARM* slow_path =
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007209 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
7210 instruction,
7211 root,
7212 /*entrypoint*/ temp);
Roland Levillainc9285912015-12-18 10:38:42 +00007213 codegen_->AddSlowPath(slow_path);
7214
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007215 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7216 const int32_t entry_point_offset =
7217 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
7218 // Loading the entrypoint does not require a load acquire since it is only changed when
7219 // threads are suspended or running a checkpoint.
7220 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
7221 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7222 // checking GetIsGcMarking.
7223 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00007224 __ Bind(slow_path->GetExitLabel());
7225 } else {
7226 // GC root loaded through a slow path for read barriers other
7227 // than Baker's.
7228 // /* GcRoot<mirror::Object>* */ root = obj + offset
7229 __ AddConstant(root_reg, obj, offset);
7230 // /* mirror::Object* */ root = root->Read()
7231 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7232 }
7233 } else {
7234 // Plain GC root load with no read barrier.
7235 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7236 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7237 // Note that GC roots are not affected by heap poisoning, thus we
7238 // do not have to unpoison `root_reg` here.
7239 }
7240}
7241
7242void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7243 Location ref,
7244 Register obj,
7245 uint32_t offset,
7246 Location temp,
7247 bool needs_null_check) {
7248 DCHECK(kEmitCompilerReadBarrier);
7249 DCHECK(kUseBakerReadBarrier);
7250
7251 // /* HeapReference<Object> */ ref = *(obj + offset)
7252 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01007253 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00007254 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007255 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007256}
7257
7258void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7259 Location ref,
7260 Register obj,
7261 uint32_t data_offset,
7262 Location index,
7263 Location temp,
7264 bool needs_null_check) {
7265 DCHECK(kEmitCompilerReadBarrier);
7266 DCHECK(kUseBakerReadBarrier);
7267
Roland Levillainbfea3352016-06-23 13:48:47 +01007268 static_assert(
7269 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7270 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007271 // /* HeapReference<Object> */ ref =
7272 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01007273 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00007274 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007275 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007276}
7277
7278void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7279 Location ref,
7280 Register obj,
7281 uint32_t offset,
7282 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01007283 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00007284 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007285 bool needs_null_check,
7286 bool always_update_field,
7287 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00007288 DCHECK(kEmitCompilerReadBarrier);
7289 DCHECK(kUseBakerReadBarrier);
7290
7291 // In slow path based read barriers, the read barrier call is
7292 // inserted after the original load. However, in fast path based
7293 // Baker's read barriers, we need to perform the load of
7294 // mirror::Object::monitor_ *before* the original reference load.
7295 // This load-load ordering is required by the read barrier.
7296 // The fast path/slow path (for Baker's algorithm) should look like:
7297 //
7298 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7299 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7300 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007301 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillainc9285912015-12-18 10:38:42 +00007302 // if (is_gray) {
7303 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7304 // }
7305 //
7306 // Note: the original implementation in ReadBarrier::Barrier is
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007307 // slightly more complex as it performs additional checks that we do
7308 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00007309
7310 Register ref_reg = ref.AsRegister<Register>();
7311 Register temp_reg = temp.AsRegister<Register>();
7312 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7313
7314 // /* int32_t */ monitor = obj->monitor_
7315 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7316 if (needs_null_check) {
7317 MaybeRecordImplicitNullCheck(instruction);
7318 }
7319 // /* LockWord */ lock_word = LockWord(monitor)
7320 static_assert(sizeof(LockWord) == sizeof(int32_t),
7321 "art::LockWord and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007322
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007323 // Introduce a dependency on the lock_word including the rb_state,
7324 // which shall prevent load-load reordering without using
Roland Levillainc9285912015-12-18 10:38:42 +00007325 // a memory barrier (which would be more expensive).
Roland Levillain0b671c02016-08-19 12:02:34 +01007326 // `obj` is unchanged by this operation, but its value now depends
7327 // on `temp_reg`.
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007328 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
Roland Levillainc9285912015-12-18 10:38:42 +00007329
7330 // The actual reference load.
7331 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007332 // Load types involving an "index": ArrayGet,
7333 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7334 // intrinsics.
Roland Levillainbfea3352016-06-23 13:48:47 +01007335 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00007336 if (index.IsConstant()) {
7337 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01007338 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00007339 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7340 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01007341 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007342 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7343 // intrinsics, which use a register pair as index ("long
7344 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01007345 Register index_reg = index.IsRegisterPair()
7346 ? index.AsRegisterPairLow<Register>()
7347 : index.AsRegister<Register>();
7348 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00007349 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
7350 }
7351 } else {
7352 // /* HeapReference<Object> */ ref = *(obj + offset)
7353 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7354 }
7355
7356 // Object* ref = ref_addr->AsMirrorPtr()
7357 __ MaybeUnpoisonHeapReference(ref_reg);
7358
Vladimir Marko953437b2016-08-24 08:30:46 +00007359 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007360 SlowPathCodeARM* slow_path;
7361 if (always_update_field) {
7362 DCHECK(temp2 != nullptr);
7363 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
7364 // of the form `obj + field_offset`, where `obj` is a register and
7365 // `field_offset` is a register pair (of which only the lower half
7366 // is used). Thus `offset` and `scale_factor` above are expected
7367 // to be null in this code path.
7368 DCHECK_EQ(offset, 0u);
7369 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7370 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
7371 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7372 } else {
7373 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
7374 }
Roland Levillainc9285912015-12-18 10:38:42 +00007375 AddSlowPath(slow_path);
7376
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007377 // if (rb_state == ReadBarrier::GrayState())
Roland Levillainc9285912015-12-18 10:38:42 +00007378 // ref = ReadBarrier::Mark(ref);
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007379 // Given the numeric representation, it's enough to check the low bit of the
7380 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7381 // which can be a 16-bit instruction unlike the TST immediate.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007382 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7383 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko194bcfe2016-07-11 15:52:00 +01007384 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7385 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
Roland Levillainc9285912015-12-18 10:38:42 +00007386 __ Bind(slow_path->GetExitLabel());
7387}
7388
7389void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
7390 Location out,
7391 Location ref,
7392 Location obj,
7393 uint32_t offset,
7394 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007395 DCHECK(kEmitCompilerReadBarrier);
7396
Roland Levillainc9285912015-12-18 10:38:42 +00007397 // Insert a slow path based read barrier *after* the reference load.
7398 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007399 // If heap poisoning is enabled, the unpoisoning of the loaded
7400 // reference will be carried out by the runtime within the slow
7401 // path.
7402 //
7403 // Note that `ref` currently does not get unpoisoned (when heap
7404 // poisoning is enabled), which is alright as the `ref` argument is
7405 // not used by the artReadBarrierSlow entry point.
7406 //
7407 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007408 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00007409 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
7410 AddSlowPath(slow_path);
7411
Roland Levillain3b359c72015-11-17 19:35:12 +00007412 __ b(slow_path->GetEntryLabel());
7413 __ Bind(slow_path->GetExitLabel());
7414}
7415
Roland Levillainc9285912015-12-18 10:38:42 +00007416void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7417 Location out,
7418 Location ref,
7419 Location obj,
7420 uint32_t offset,
7421 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007422 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00007423 // Baker's read barriers shall be handled by the fast path
7424 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
7425 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007426 // If heap poisoning is enabled, unpoisoning will be taken care of
7427 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007428 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00007429 } else if (kPoisonHeapReferences) {
7430 __ UnpoisonHeapReference(out.AsRegister<Register>());
7431 }
7432}
7433
Roland Levillainc9285912015-12-18 10:38:42 +00007434void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7435 Location out,
7436 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007437 DCHECK(kEmitCompilerReadBarrier);
7438
Roland Levillainc9285912015-12-18 10:38:42 +00007439 // Insert a slow path based read barrier *after* the GC root load.
7440 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007441 // Note that GC roots are not affected by heap poisoning, so we do
7442 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007443 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007444 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
7445 AddSlowPath(slow_path);
7446
Roland Levillain3b359c72015-11-17 19:35:12 +00007447 __ b(slow_path->GetEntryLabel());
7448 __ Bind(slow_path->GetExitLabel());
7449}
7450
Vladimir Markodc151b22015-10-15 18:02:30 +01007451HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7452 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007453 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00007454 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007455}
7456
Vladimir Markob4536b72015-11-24 13:45:23 +00007457Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7458 Register temp) {
7459 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7460 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7461 if (!invoke->GetLocations()->Intrinsified()) {
7462 return location.AsRegister<Register>();
7463 }
7464 // For intrinsics we allow any location, so it may be on the stack.
7465 if (!location.IsRegister()) {
7466 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7467 return temp;
7468 }
7469 // For register locations, check if the register was saved. If so, get it from the stack.
7470 // Note: There is a chance that the register was saved but not overwritten, so we could
7471 // save one load. However, since this is just an intrinsic slow path we prefer this
7472 // simple and more robust approach rather that trying to determine if that's the case.
7473 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08007474 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00007475 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7476 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7477 return temp;
7478 }
7479 return location.AsRegister<Register>();
7480}
7481
TatWai Chongd8c052a2016-11-02 16:12:48 +08007482Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
7483 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007484 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7485 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007486 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7487 uint32_t offset =
7488 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007489 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007490 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007491 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007492 }
Vladimir Marko58155012015-08-19 12:49:41 +00007493 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007494 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007495 break;
7496 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7497 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7498 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007499 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7500 HArmDexCacheArraysBase* base =
7501 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7502 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7503 temp.AsRegister<Register>());
7504 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7505 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7506 break;
7507 }
Vladimir Marko58155012015-08-19 12:49:41 +00007508 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007509 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007510 Register method_reg;
7511 Register reg = temp.AsRegister<Register>();
7512 if (current_method.IsRegister()) {
7513 method_reg = current_method.AsRegister<Register>();
7514 } else {
7515 DCHECK(invoke->GetLocations()->Intrinsified());
7516 DCHECK(!current_method.IsValid());
7517 method_reg = reg;
7518 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7519 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007520 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7521 __ LoadFromOffset(kLoadWord,
7522 reg,
7523 method_reg,
7524 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007525 // temp = temp[index_in_cache];
7526 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7527 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007528 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7529 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007530 }
Vladimir Marko58155012015-08-19 12:49:41 +00007531 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08007532 return callee_method;
7533}
7534
7535void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
7536 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00007537
7538 switch (invoke->GetCodePtrLocation()) {
7539 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7540 __ bl(GetFrameEntryLabel());
7541 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007542 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7543 // LR = callee_method->entry_point_from_quick_compiled_code_
7544 __ LoadFromOffset(
7545 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007546 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007547 // LR()
7548 __ blx(LR);
7549 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007550 }
7551
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007552 DCHECK(!IsLeafMethod());
7553}
7554
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007555void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7556 Register temp = temp_location.AsRegister<Register>();
7557 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7558 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007559
7560 // Use the calling convention instead of the location of the receiver, as
7561 // intrinsics may have put the receiver in a different register. In the intrinsics
7562 // slow path, the arguments have been moved to the right place, so here we are
7563 // guaranteed that the receiver is the first register of the calling convention.
7564 InvokeDexCallingConvention calling_convention;
7565 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007566 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007567 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007568 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007569 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007570 // Instead of simply (possibly) unpoisoning `temp` here, we should
7571 // emit a read barrier for the previous class reference load.
7572 // However this is not required in practice, as this is an
7573 // intermediate/temporary reference and because the current
7574 // concurrent copying collector keeps the from-space memory
7575 // intact/accessible until the end of the marking phase (the
7576 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007577 __ MaybeUnpoisonHeapReference(temp);
7578 // temp = temp->GetMethodAt(method_offset);
7579 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007580 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007581 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7582 // LR = temp->GetEntryPoint();
7583 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7584 // LR();
7585 __ blx(LR);
7586}
7587
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007588CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007589 const DexFile& dex_file, dex::StringIndex string_index) {
7590 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007591}
7592
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007593CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08007594 const DexFile& dex_file, dex::TypeIndex type_index) {
7595 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007596}
7597
Vladimir Marko1998cd02017-01-13 13:02:58 +00007598CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
7599 const DexFile& dex_file, dex::TypeIndex type_index) {
7600 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
7601}
7602
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007603CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
7604 const DexFile& dex_file, uint32_t element_offset) {
7605 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
7606}
7607
7608CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
7609 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
7610 patches->emplace_back(dex_file, offset_or_index);
7611 return &patches->back();
7612}
7613
7614Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007615 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007616 return boot_image_string_patches_.GetOrCreate(
7617 StringReference(&dex_file, string_index),
7618 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7619}
7620
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007621Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007622 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007623 return boot_image_type_patches_.GetOrCreate(
7624 TypeReference(&dex_file, type_index),
7625 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7626}
7627
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007628Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
7629 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
7630 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
7631 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
7632}
7633
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007634Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007635 dex::StringIndex string_index,
7636 Handle<mirror::String> handle) {
7637 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
7638 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007639 return jit_string_patches_.GetOrCreate(
7640 StringReference(&dex_file, string_index),
7641 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7642}
7643
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007644Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
7645 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007646 Handle<mirror::Class> handle) {
7647 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
7648 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007649 return jit_class_patches_.GetOrCreate(
7650 TypeReference(&dex_file, type_index),
7651 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
7652}
7653
Vladimir Markoaad75c62016-10-03 08:46:48 +00007654template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
7655inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
7656 const ArenaDeque<PcRelativePatchInfo>& infos,
7657 ArenaVector<LinkerPatch>* linker_patches) {
7658 for (const PcRelativePatchInfo& info : infos) {
7659 const DexFile& dex_file = info.target_dex_file;
7660 size_t offset_or_index = info.offset_or_index;
7661 DCHECK(info.add_pc_label.IsBound());
7662 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
7663 // Add MOVW patch.
7664 DCHECK(info.movw_label.IsBound());
7665 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
7666 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
7667 // Add MOVT patch.
7668 DCHECK(info.movt_label.IsBound());
7669 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
7670 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
7671 }
7672}
7673
Vladimir Marko58155012015-08-19 12:49:41 +00007674void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
7675 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00007676 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00007677 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007678 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007679 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007680 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00007681 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00007682 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007683 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00007684 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007685 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
7686 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007687 for (const auto& entry : boot_image_string_patches_) {
7688 const StringReference& target_string = entry.first;
7689 Literal* literal = entry.second;
7690 DCHECK(literal->GetLabel()->IsBound());
7691 uint32_t literal_offset = literal->GetLabel()->Position();
7692 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
7693 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007694 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007695 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007696 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00007697 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007698 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
7699 linker_patches);
7700 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007701 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
7702 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007703 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
7704 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007705 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007706 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
7707 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007708 for (const auto& entry : boot_image_type_patches_) {
7709 const TypeReference& target_type = entry.first;
7710 Literal* literal = entry.second;
7711 DCHECK(literal->GetLabel()->IsBound());
7712 uint32_t literal_offset = literal->GetLabel()->Position();
7713 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
7714 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08007715 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007716 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007717 for (const auto& entry : boot_image_address_patches_) {
7718 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
7719 Literal* literal = entry.second;
7720 DCHECK(literal->GetLabel()->IsBound());
7721 uint32_t literal_offset = literal->GetLabel()->Position();
7722 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
7723 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00007724 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007725}
7726
7727Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
7728 return map->GetOrCreate(
7729 value,
7730 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00007731}
7732
7733Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
7734 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007735 return map->GetOrCreate(
7736 target_method,
7737 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00007738}
7739
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03007740void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7741 LocationSummary* locations =
7742 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
7743 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
7744 Location::RequiresRegister());
7745 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
7746 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
7747 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7748}
7749
7750void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
7751 LocationSummary* locations = instr->GetLocations();
7752 Register res = locations->Out().AsRegister<Register>();
7753 Register accumulator =
7754 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
7755 Register mul_left =
7756 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
7757 Register mul_right =
7758 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
7759
7760 if (instr->GetOpKind() == HInstruction::kAdd) {
7761 __ mla(res, mul_left, mul_right, accumulator);
7762 } else {
7763 __ mls(res, mul_left, mul_right, accumulator);
7764 }
7765}
7766
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007767void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007768 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007769 LOG(FATAL) << "Unreachable";
7770}
7771
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007772void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007773 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007774 LOG(FATAL) << "Unreachable";
7775}
7776
Mark Mendellfe57faa2015-09-18 09:26:15 -04007777// Simple implementation of packed switch - generate cascaded compare/jumps.
7778void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7779 LocationSummary* locations =
7780 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7781 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007782 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007783 codegen_->GetAssembler()->IsThumb()) {
7784 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
7785 if (switch_instr->GetStartValue() != 0) {
7786 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
7787 }
7788 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007789}
7790
7791void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7792 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007793 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04007794 LocationSummary* locations = switch_instr->GetLocations();
7795 Register value_reg = locations->InAt(0).AsRegister<Register>();
7796 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7797
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007798 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007799 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007800 Register temp_reg = IP;
7801 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
7802 // the immediate, because IP is used as the destination register. For the other
7803 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
7804 // and they can be encoded in the instruction without making use of IP register.
7805 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
7806
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007807 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007808 // Jump to successors[0] if value == lower_bound.
7809 __ b(codegen_->GetLabelOf(successors[0]), EQ);
7810 int32_t last_index = 0;
7811 for (; num_entries - last_index > 2; last_index += 2) {
7812 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
7813 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
7814 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
7815 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
7816 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
7817 }
7818 if (num_entries - last_index == 2) {
7819 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00007820 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007821 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007822 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04007823
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07007824 // And the default for any other value.
7825 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
7826 __ b(codegen_->GetLabelOf(default_block));
7827 }
7828 } else {
7829 // Create a table lookup.
7830 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7831
7832 // Materialize a pointer to the switch table
7833 std::vector<Label*> labels(num_entries);
7834 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
7835 for (uint32_t i = 0; i < num_entries; i++) {
7836 labels[i] = codegen_->GetLabelOf(successors[i]);
7837 }
7838 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
7839
7840 // Remove the bias.
7841 Register key_reg;
7842 if (lower_bound != 0) {
7843 key_reg = locations->GetTemp(1).AsRegister<Register>();
7844 __ AddConstant(key_reg, value_reg, -lower_bound);
7845 } else {
7846 key_reg = value_reg;
7847 }
7848
7849 // Check whether the value is in the table, jump to default block if not.
7850 __ CmpConstant(key_reg, num_entries - 1);
7851 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
7852
7853 // Load the displacement from the table.
7854 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
7855
7856 // Dispatch is a direct add to the PC (for Thumb2).
7857 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04007858 }
7859}
7860
Vladimir Markob4536b72015-11-24 13:45:23 +00007861void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7862 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
7863 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00007864}
7865
7866void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
7867 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007868 CodeGeneratorARM::PcRelativePatchInfo* labels =
7869 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00007870 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007871 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007872 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007873 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00007874 __ BindTrackedLabel(&labels->add_pc_label);
7875 __ add(base_reg, base_reg, ShifterOperand(PC));
7876}
7877
Andreas Gampe85b62f22015-09-09 13:15:38 -07007878void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
7879 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00007880 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007881 return;
7882 }
7883
7884 DCHECK_NE(type, Primitive::kPrimVoid);
7885
7886 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
7887 if (return_loc.Equals(trg)) {
7888 return;
7889 }
7890
7891 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7892 // with the last branch.
7893 if (type == Primitive::kPrimLong) {
7894 HParallelMove parallel_move(GetGraph()->GetArena());
7895 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
7896 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
7897 GetMoveResolver()->EmitNativeCode(&parallel_move);
7898 } else if (type == Primitive::kPrimDouble) {
7899 HParallelMove parallel_move(GetGraph()->GetArena());
7900 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
7901 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
7902 GetMoveResolver()->EmitNativeCode(&parallel_move);
7903 } else {
7904 // Let the parallel move resolver take care of all of this.
7905 HParallelMove parallel_move(GetGraph()->GetArena());
7906 parallel_move.AddMove(return_loc, trg, type, nullptr);
7907 GetMoveResolver()->EmitNativeCode(&parallel_move);
7908 }
7909}
7910
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007911void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
7912 LocationSummary* locations =
7913 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7914 locations->SetInAt(0, Location::RequiresRegister());
7915 locations->SetOut(Location::RequiresRegister());
7916}
7917
7918void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
7919 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00007920 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007921 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007922 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007923 __ LoadFromOffset(kLoadWord,
7924 locations->Out().AsRegister<Register>(),
7925 locations->InAt(0).AsRegister<Register>(),
7926 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007927 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007928 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007929 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01007930 __ LoadFromOffset(kLoadWord,
7931 locations->Out().AsRegister<Register>(),
7932 locations->InAt(0).AsRegister<Register>(),
7933 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
7934 __ LoadFromOffset(kLoadWord,
7935 locations->Out().AsRegister<Register>(),
7936 locations->Out().AsRegister<Register>(),
7937 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007938 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00007939}
7940
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007941static void PatchJitRootUse(uint8_t* code,
7942 const uint8_t* roots_data,
7943 Literal* literal,
7944 uint64_t index_in_table) {
7945 DCHECK(literal->GetLabel()->IsBound());
7946 uint32_t literal_offset = literal->GetLabel()->Position();
7947 uintptr_t address =
7948 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7949 uint8_t* data = code + literal_offset;
7950 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
7951}
7952
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007953void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7954 for (const auto& entry : jit_string_patches_) {
7955 const auto& it = jit_string_roots_.find(entry.first);
7956 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007957 PatchJitRootUse(code, roots_data, entry.second, it->second);
7958 }
7959 for (const auto& entry : jit_class_patches_) {
7960 const auto& it = jit_class_roots_.find(entry.first);
7961 DCHECK(it != jit_class_roots_.end());
7962 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007963 }
7964}
7965
Roland Levillain4d027112015-07-01 15:41:14 +01007966#undef __
7967#undef QUICK_ENTRY_POINT
7968
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007969} // namespace arm
7970} // namespace art