blob: ebd578c5cd9ed329aa029226db02eb78ae91ae1a [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 Levillain54f869e2017-03-06 13:54:11 +0000639// Abstract base class for read barrier slow paths marking a reference
640// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000641//
Roland Levillain54f869e2017-03-06 13:54:11 +0000642// Argument `entrypoint` must be a register location holding the read
643// barrier marking runtime entry point to be invoked.
644class ReadBarrierMarkSlowPathBaseARM : public SlowPathCodeARM {
645 protected:
646 ReadBarrierMarkSlowPathBaseARM(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000647 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
648 DCHECK(kEmitCompilerReadBarrier);
649 }
650
Roland Levillain54f869e2017-03-06 13:54:11 +0000651 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000652
Roland Levillain54f869e2017-03-06 13:54:11 +0000653 // Generate assembly code calling the read barrier marking runtime
654 // entry point (ReadBarrierMarkRegX).
655 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000656 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain47b3ab22017-02-27 14:31:35 +0000657
Roland Levillain47b3ab22017-02-27 14:31:35 +0000658 // No need to save live registers; it's taken care of by the
659 // entrypoint. Also, there is no need to update the stack mask,
660 // as this runtime call will not trigger a garbage collection.
661 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
662 DCHECK_NE(ref_reg, SP);
663 DCHECK_NE(ref_reg, LR);
664 DCHECK_NE(ref_reg, PC);
665 // IP is used internally by the ReadBarrierMarkRegX entry point
666 // as a temporary, it cannot be the entry point's input/output.
667 DCHECK_NE(ref_reg, IP);
668 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
669 // "Compact" slow path, saving two moves.
670 //
671 // Instead of using the standard runtime calling convention (input
672 // and output in R0):
673 //
674 // R0 <- ref
675 // R0 <- ReadBarrierMark(R0)
676 // ref <- R0
677 //
678 // we just use rX (the register containing `ref`) as input and output
679 // of a dedicated entrypoint:
680 //
681 // rX <- ReadBarrierMarkRegX(rX)
682 //
683 if (entrypoint_.IsValid()) {
684 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
685 __ blx(entrypoint_.AsRegister<Register>());
686 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000687 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000688 int32_t entry_point_offset =
689 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
690 // This runtime call does not require a stack map.
691 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
692 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000693 }
694
695 // The location (register) of the marked object reference.
696 const Location ref_;
697
698 // The location of the entrypoint if it is already loaded.
699 const Location entrypoint_;
700
701 private:
702 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM);
703};
704
Dave Allison20dfc792014-06-16 20:44:29 -0700705// Slow path marking an object reference `ref` during a read
706// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000707// reference does not get updated by this slow path after marking.
Dave Allison20dfc792014-06-16 20:44:29 -0700708//
709// This means that after the execution of this slow path, `ref` will
710// always be up-to-date, but `obj.field` may not; i.e., after the
711// flip, `ref` will be a to-space reference, but `obj.field` will
712// probably still be a from-space reference (unless it gets updated by
713// another thread, or if another thread installed another object
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000714// reference (different from `ref`) in `obj.field`).
715//
716// If `entrypoint` is a valid location it is assumed to already be
717// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000718// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000719class ReadBarrierMarkSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000720 public:
721 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
722 Location ref,
723 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000724 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000725 DCHECK(kEmitCompilerReadBarrier);
726 }
727
728 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
729
730 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
731 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000732 DCHECK(locations->CanCall());
733 if (kIsDebugBuild) {
734 Register ref_reg = ref_.AsRegister<Register>();
735 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
736 }
737 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
738 << "Unexpected instruction in read barrier marking slow path: "
739 << instruction_->DebugName();
740
741 __ Bind(GetEntryLabel());
742 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000743 __ b(GetExitLabel());
744 }
745
746 private:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000747 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
748};
749
Roland Levillain54f869e2017-03-06 13:54:11 +0000750// Slow path loading `obj`'s lock word, loading a reference from
751// object `*(obj + offset + (index << scale_factor))` into `ref`, and
752// marking `ref` if `obj` is gray according to the lock word (Baker
753// read barrier). The field `obj.field` in the object `obj` holding
754// this reference does not get updated by this slow path after marking
755// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
756// below for that).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000757//
Roland Levillain54f869e2017-03-06 13:54:11 +0000758// This means that after the execution of this slow path, `ref` will
759// always be up-to-date, but `obj.field` may not; i.e., after the
760// flip, `ref` will be a to-space reference, but `obj.field` will
761// probably still be a from-space reference (unless it gets updated by
762// another thread, or if another thread installed another object
763// reference (different from `ref`) in `obj.field`).
764//
765// Argument `entrypoint` must be a register location holding the read
766// barrier marking runtime entry point to be invoked.
767class LoadReferenceWithBakerReadBarrierSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000768 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000769 LoadReferenceWithBakerReadBarrierSlowPathARM(HInstruction* instruction,
770 Location ref,
771 Register obj,
772 uint32_t offset,
773 Location index,
774 ScaleFactor scale_factor,
775 bool needs_null_check,
776 Register temp,
777 Location entrypoint)
778 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000779 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000780 offset_(offset),
781 index_(index),
782 scale_factor_(scale_factor),
783 needs_null_check_(needs_null_check),
784 temp_(temp) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000785 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000786 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000787 }
788
Roland Levillain54f869e2017-03-06 13:54:11 +0000789 const char* GetDescription() const OVERRIDE {
790 return "LoadReferenceWithBakerReadBarrierSlowPathARM";
791 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000792
793 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
794 LocationSummary* locations = instruction_->GetLocations();
795 Register ref_reg = ref_.AsRegister<Register>();
796 DCHECK(locations->CanCall());
797 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000798 DCHECK_NE(ref_reg, temp_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000799 DCHECK(instruction_->IsInstanceFieldGet() ||
800 instruction_->IsStaticFieldGet() ||
801 instruction_->IsArrayGet() ||
802 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000803 instruction_->IsInstanceOf() ||
804 instruction_->IsCheckCast() ||
805 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
806 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
807 << "Unexpected instruction in read barrier marking slow path: "
808 << instruction_->DebugName();
809 // The read barrier instrumentation of object ArrayGet
810 // instructions does not support the HIntermediateAddress
811 // instruction.
812 DCHECK(!(instruction_->IsArrayGet() &&
813 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
814
815 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000816
817 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
818 // inserted after the original load. However, in fast path based
819 // Baker's read barriers, we need to perform the load of
820 // mirror::Object::monitor_ *before* the original reference load.
821 // This load-load ordering is required by the read barrier.
822 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000823 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000824 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
825 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
826 // HeapReference<mirror::Object> ref = *src; // Original reference load.
827 // bool is_gray = (rb_state == ReadBarrier::GrayState());
828 // if (is_gray) {
829 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
830 // }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000831 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000832 // Note: the original implementation in ReadBarrier::Barrier is
833 // slightly more complex as it performs additional checks that we do
834 // not do here for performance reasons.
835
836 // /* int32_t */ monitor = obj->monitor_
837 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
838 __ LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
839 if (needs_null_check_) {
840 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000841 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000842 // /* LockWord */ lock_word = LockWord(monitor)
843 static_assert(sizeof(LockWord) == sizeof(int32_t),
844 "art::LockWord and int32_t have different sizes.");
845
846 // Introduce a dependency on the lock_word including the rb_state,
847 // which shall prevent load-load reordering without using
848 // a memory barrier (which would be more expensive).
849 // `obj` is unchanged by this operation, but its value now depends
850 // on `temp`.
851 __ add(obj_, obj_, ShifterOperand(temp_, LSR, 32));
852
853 // The actual reference load.
854 // A possible implicit null check has already been handled above.
855 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
856 arm_codegen->GenerateRawReferenceLoad(
857 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
858
859 // Mark the object `ref` when `obj` is gray.
860 //
861 // if (rb_state == ReadBarrier::GrayState())
862 // ref = ReadBarrier::Mark(ref);
863 //
864 // Given the numeric representation, it's enough to check the low bit of the
865 // rb_state. We do that by shifting the bit out of the lock word with LSRS
866 // which can be a 16-bit instruction unlike the TST immediate.
867 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
868 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
869 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
870 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
871 GenerateReadBarrierMarkRuntimeCall(codegen);
872
Roland Levillain47b3ab22017-02-27 14:31:35 +0000873 __ b(GetExitLabel());
874 }
875
876 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000877 // The register containing the object holding the marked object reference field.
878 Register obj_;
879 // The offset, index and scale factor to access the reference in `obj_`.
880 uint32_t offset_;
881 Location index_;
882 ScaleFactor scale_factor_;
883 // Is a null check required?
884 bool needs_null_check_;
885 // A temporary register used to hold the lock word of `obj_`.
886 Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000887
Roland Levillain54f869e2017-03-06 13:54:11 +0000888 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000889};
890
Roland Levillain54f869e2017-03-06 13:54:11 +0000891// Slow path loading `obj`'s lock word, loading a reference from
892// object `*(obj + offset + (index << scale_factor))` into `ref`, and
893// marking `ref` if `obj` is gray according to the lock word (Baker
894// read barrier). If needed, this slow path also atomically updates
895// the field `obj.field` in the object `obj` holding this reference
896// after marking (contrary to
897// LoadReferenceWithBakerReadBarrierSlowPathARM above, which never
898// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000899//
900// This means that after the execution of this slow path, both `ref`
901// and `obj.field` will be up-to-date; i.e., after the flip, both will
902// hold the same to-space reference (unless another thread installed
903// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000904//
Roland Levillain54f869e2017-03-06 13:54:11 +0000905// Argument `entrypoint` must be a register location holding the read
906// barrier marking runtime entry point to be invoked.
907class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
908 : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000909 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000910 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(HInstruction* instruction,
911 Location ref,
912 Register obj,
913 uint32_t offset,
914 Location index,
915 ScaleFactor scale_factor,
916 bool needs_null_check,
917 Register temp1,
918 Register temp2,
919 Location entrypoint)
920 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000921 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000922 offset_(offset),
923 index_(index),
924 scale_factor_(scale_factor),
925 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000926 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +0000927 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000928 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000929 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000930 }
931
Roland Levillain54f869e2017-03-06 13:54:11 +0000932 const char* GetDescription() const OVERRIDE {
933 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM";
934 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000935
936 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
937 LocationSummary* locations = instruction_->GetLocations();
938 Register ref_reg = ref_.AsRegister<Register>();
939 DCHECK(locations->CanCall());
940 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000941 DCHECK_NE(ref_reg, temp1_);
942
943 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000944 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
945 << "Unexpected instruction in read barrier marking and field updating slow path: "
946 << instruction_->DebugName();
947 DCHECK(instruction_->GetLocations()->Intrinsified());
948 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000949 DCHECK_EQ(offset_, 0u);
950 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
951 // The location of the offset of the marked reference field within `obj_`.
952 Location field_offset = index_;
953 DCHECK(field_offset.IsRegisterPair()) << field_offset;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000954
955 __ Bind(GetEntryLabel());
956
Roland Levillain54f869e2017-03-06 13:54:11 +0000957 // /* int32_t */ monitor = obj->monitor_
958 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
959 __ LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
960 if (needs_null_check_) {
961 codegen->MaybeRecordImplicitNullCheck(instruction_);
962 }
963 // /* LockWord */ lock_word = LockWord(monitor)
964 static_assert(sizeof(LockWord) == sizeof(int32_t),
965 "art::LockWord and int32_t have different sizes.");
966
967 // Introduce a dependency on the lock_word including the rb_state,
968 // which shall prevent load-load reordering without using
969 // a memory barrier (which would be more expensive).
970 // `obj` is unchanged by this operation, but its value now depends
971 // on `temp1`.
972 __ add(obj_, obj_, ShifterOperand(temp1_, LSR, 32));
973
974 // The actual reference load.
975 // A possible implicit null check has already been handled above.
976 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
977 arm_codegen->GenerateRawReferenceLoad(
978 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
979
980 // Mark the object `ref` when `obj` is gray.
981 //
982 // if (rb_state == ReadBarrier::GrayState())
983 // ref = ReadBarrier::Mark(ref);
984 //
985 // Given the numeric representation, it's enough to check the low bit of the
986 // rb_state. We do that by shifting the bit out of the lock word with LSRS
987 // which can be a 16-bit instruction unlike the TST immediate.
988 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
989 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
990 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
991 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
992
993 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000994 // Note that we cannot use IP to save the old reference, as IP is
995 // used internally by the ReadBarrierMarkRegX entry point, and we
996 // need the old reference after the call to that entry point.
997 DCHECK_NE(temp1_, IP);
998 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000999
Roland Levillain54f869e2017-03-06 13:54:11 +00001000 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001001
1002 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001003 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001004 //
1005 // Note that this field could also hold a different object, if
1006 // another thread had concurrently changed it. In that case, the
1007 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1008 // (CAS) operation below would abort the CAS, leaving the field
1009 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001010 __ cmp(temp1_, ShifterOperand(ref_reg));
Roland Levillain54f869e2017-03-06 13:54:11 +00001011 __ b(GetExitLabel(), EQ);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001012
1013 // Update the the holder's field atomically. This may fail if
1014 // mutator updates before us, but it's OK. This is achieved
1015 // using a strong compare-and-set (CAS) operation with relaxed
1016 // memory synchronization ordering, where the expected value is
1017 // the old reference and the desired value is the new reference.
1018
1019 // Convenience aliases.
1020 Register base = obj_;
1021 // The UnsafeCASObject intrinsic uses a register pair as field
1022 // offset ("long offset"), of which only the low part contains
1023 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001024 Register offset = field_offset.AsRegisterPairLow<Register>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001025 Register expected = temp1_;
1026 Register value = ref_reg;
1027 Register tmp_ptr = IP; // Pointer to actual memory.
1028 Register tmp = temp2_; // Value in memory.
1029
1030 __ add(tmp_ptr, base, ShifterOperand(offset));
1031
1032 if (kPoisonHeapReferences) {
1033 __ PoisonHeapReference(expected);
1034 if (value == expected) {
1035 // Do not poison `value`, as it is the same register as
1036 // `expected`, which has just been poisoned.
1037 } else {
1038 __ PoisonHeapReference(value);
1039 }
1040 }
1041
1042 // do {
1043 // tmp = [r_ptr] - expected;
1044 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1045
Roland Levillain24a4d112016-10-26 13:10:46 +01001046 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001047 __ Bind(&loop_head);
1048
1049 __ ldrex(tmp, tmp_ptr);
1050
1051 __ subs(tmp, tmp, ShifterOperand(expected));
1052
Roland Levillain24a4d112016-10-26 13:10:46 +01001053 __ it(NE);
1054 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001055
Roland Levillain24a4d112016-10-26 13:10:46 +01001056 __ b(&exit_loop, NE);
1057
1058 __ strex(tmp, value, tmp_ptr);
1059 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001060 __ b(&loop_head, EQ);
1061
Roland Levillain24a4d112016-10-26 13:10:46 +01001062 __ Bind(&exit_loop);
1063
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001064 if (kPoisonHeapReferences) {
1065 __ UnpoisonHeapReference(expected);
1066 if (value == expected) {
1067 // Do not unpoison `value`, as it is the same register as
1068 // `expected`, which has just been unpoisoned.
1069 } else {
1070 __ UnpoisonHeapReference(value);
1071 }
1072 }
1073
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001074 __ b(GetExitLabel());
1075 }
1076
1077 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001078 // The register containing the object holding the marked object reference field.
1079 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001080 // The offset, index and scale factor to access the reference in `obj_`.
1081 uint32_t offset_;
1082 Location index_;
1083 ScaleFactor scale_factor_;
1084 // Is a null check required?
1085 bool needs_null_check_;
1086 // A temporary register used to hold the lock word of `obj_`; and
1087 // also to hold the original reference value, when the reference is
1088 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001089 const Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001090 // A temporary register used in the implementation of the CAS, to
1091 // update the object's reference field.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001092 const Register temp2_;
1093
Roland Levillain54f869e2017-03-06 13:54:11 +00001094 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001095};
1096
Roland Levillain3b359c72015-11-17 19:35:12 +00001097// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001098class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001099 public:
1100 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
1101 Location out,
1102 Location ref,
1103 Location obj,
1104 uint32_t offset,
1105 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001106 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +00001107 out_(out),
1108 ref_(ref),
1109 obj_(obj),
1110 offset_(offset),
1111 index_(index) {
1112 DCHECK(kEmitCompilerReadBarrier);
1113 // If `obj` is equal to `out` or `ref`, it means the initial object
1114 // has been overwritten by (or after) the heap object reference load
1115 // to be instrumented, e.g.:
1116 //
1117 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00001118 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001119 //
1120 // In that case, we have lost the information about the original
1121 // object, and the emitted read barrier cannot work properly.
1122 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1123 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1124 }
1125
1126 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1127 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1128 LocationSummary* locations = instruction_->GetLocations();
1129 Register reg_out = out_.AsRegister<Register>();
1130 DCHECK(locations->CanCall());
1131 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +01001132 DCHECK(instruction_->IsInstanceFieldGet() ||
1133 instruction_->IsStaticFieldGet() ||
1134 instruction_->IsArrayGet() ||
1135 instruction_->IsInstanceOf() ||
1136 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001137 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +00001138 << "Unexpected instruction in read barrier for heap reference slow path: "
1139 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001140 // The read barrier instrumentation of object ArrayGet
1141 // instructions does not support the HIntermediateAddress
1142 // instruction.
1143 DCHECK(!(instruction_->IsArrayGet() &&
1144 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +00001145
1146 __ Bind(GetEntryLabel());
1147 SaveLiveRegisters(codegen, locations);
1148
1149 // We may have to change the index's value, but as `index_` is a
1150 // constant member (like other "inputs" of this slow path),
1151 // introduce a copy of it, `index`.
1152 Location index = index_;
1153 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001154 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +00001155 if (instruction_->IsArrayGet()) {
1156 // Compute the actual memory offset and store it in `index`.
1157 Register index_reg = index_.AsRegister<Register>();
1158 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
1159 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
1160 // We are about to change the value of `index_reg` (see the
1161 // calls to art::arm::Thumb2Assembler::Lsl and
1162 // art::arm::Thumb2Assembler::AddConstant below), but it has
1163 // not been saved by the previous call to
1164 // art::SlowPathCode::SaveLiveRegisters, as it is a
1165 // callee-save register --
1166 // art::SlowPathCode::SaveLiveRegisters does not consider
1167 // callee-save registers, as it has been designed with the
1168 // assumption that callee-save registers are supposed to be
1169 // handled by the called function. So, as a callee-save
1170 // register, `index_reg` _would_ eventually be saved onto
1171 // the stack, but it would be too late: we would have
1172 // changed its value earlier. Therefore, we manually save
1173 // it here into another freely available register,
1174 // `free_reg`, chosen of course among the caller-save
1175 // registers (as a callee-save `free_reg` register would
1176 // exhibit the same problem).
1177 //
1178 // Note we could have requested a temporary register from
1179 // the register allocator instead; but we prefer not to, as
1180 // this is a slow path, and we know we can find a
1181 // caller-save register that is available.
1182 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1183 __ Mov(free_reg, index_reg);
1184 index_reg = free_reg;
1185 index = Location::RegisterLocation(index_reg);
1186 } else {
1187 // The initial register stored in `index_` has already been
1188 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1189 // (as it is not a callee-save register), so we can freely
1190 // use it.
1191 }
1192 // Shifting the index value contained in `index_reg` by the scale
1193 // factor (2) cannot overflow in practice, as the runtime is
1194 // unable to allocate object arrays with a size larger than
1195 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1196 __ Lsl(index_reg, index_reg, TIMES_4);
1197 static_assert(
1198 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1199 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1200 __ AddConstant(index_reg, index_reg, offset_);
1201 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001202 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1203 // intrinsics, `index_` is not shifted by a scale factor of 2
1204 // (as in the case of ArrayGet), as it is actually an offset
1205 // to an object field within an object.
1206 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001207 DCHECK(instruction_->GetLocations()->Intrinsified());
1208 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1209 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1210 << instruction_->AsInvoke()->GetIntrinsic();
1211 DCHECK_EQ(offset_, 0U);
1212 DCHECK(index_.IsRegisterPair());
1213 // UnsafeGet's offset location is a register pair, the low
1214 // part contains the correct offset.
1215 index = index_.ToLow();
1216 }
1217 }
1218
1219 // We're moving two or three locations to locations that could
1220 // overlap, so we need a parallel move resolver.
1221 InvokeRuntimeCallingConvention calling_convention;
1222 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1223 parallel_move.AddMove(ref_,
1224 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1225 Primitive::kPrimNot,
1226 nullptr);
1227 parallel_move.AddMove(obj_,
1228 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1229 Primitive::kPrimNot,
1230 nullptr);
1231 if (index.IsValid()) {
1232 parallel_move.AddMove(index,
1233 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1234 Primitive::kPrimInt,
1235 nullptr);
1236 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1237 } else {
1238 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1239 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1240 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001241 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001242 CheckEntrypointTypes<
1243 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1244 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1245
1246 RestoreLiveRegisters(codegen, locations);
1247 __ b(GetExitLabel());
1248 }
1249
1250 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1251
1252 private:
1253 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1254 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1255 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1256 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1257 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1258 return static_cast<Register>(i);
1259 }
1260 }
1261 // We shall never fail to find a free caller-save register, as
1262 // there are more than two core caller-save registers on ARM
1263 // (meaning it is possible to find one which is different from
1264 // `ref` and `obj`).
1265 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1266 LOG(FATAL) << "Could not find a free caller-save register";
1267 UNREACHABLE();
1268 }
1269
Roland Levillain3b359c72015-11-17 19:35:12 +00001270 const Location out_;
1271 const Location ref_;
1272 const Location obj_;
1273 const uint32_t offset_;
1274 // An additional location containing an index to an array.
1275 // Only used for HArrayGet and the UnsafeGetObject &
1276 // UnsafeGetObjectVolatile intrinsics.
1277 const Location index_;
1278
1279 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1280};
1281
1282// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001283class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001284 public:
1285 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001286 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001287 DCHECK(kEmitCompilerReadBarrier);
1288 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001289
1290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1291 LocationSummary* locations = instruction_->GetLocations();
1292 Register reg_out = out_.AsRegister<Register>();
1293 DCHECK(locations->CanCall());
1294 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001295 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1296 << "Unexpected instruction in read barrier for GC root slow path: "
1297 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001298
1299 __ Bind(GetEntryLabel());
1300 SaveLiveRegisters(codegen, locations);
1301
1302 InvokeRuntimeCallingConvention calling_convention;
1303 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1304 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001305 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001306 instruction_,
1307 instruction_->GetDexPc(),
1308 this);
1309 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1310 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1311
1312 RestoreLiveRegisters(codegen, locations);
1313 __ b(GetExitLabel());
1314 }
1315
1316 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1317
1318 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001319 const Location out_;
1320 const Location root_;
1321
1322 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1323};
1324
Aart Bike9f37602015-10-09 11:15:55 -07001325inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001326 switch (cond) {
1327 case kCondEQ: return EQ;
1328 case kCondNE: return NE;
1329 case kCondLT: return LT;
1330 case kCondLE: return LE;
1331 case kCondGT: return GT;
1332 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001333 case kCondB: return LO;
1334 case kCondBE: return LS;
1335 case kCondA: return HI;
1336 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001337 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001338 LOG(FATAL) << "Unreachable";
1339 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001340}
1341
Aart Bike9f37602015-10-09 11:15:55 -07001342// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001343inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001344 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001345 case kCondEQ: return EQ;
1346 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001347 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001348 case kCondLT: return LO;
1349 case kCondLE: return LS;
1350 case kCondGT: return HI;
1351 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001352 // Unsigned remain unchanged.
1353 case kCondB: return LO;
1354 case kCondBE: return LS;
1355 case kCondA: return HI;
1356 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001357 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001358 LOG(FATAL) << "Unreachable";
1359 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001360}
1361
Vladimir Markod6e069b2016-01-18 11:11:01 +00001362inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1363 // The ARM condition codes can express all the necessary branches, see the
1364 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1365 // There is no dex instruction or HIR that would need the missing conditions
1366 // "equal or unordered" or "not equal".
1367 switch (cond) {
1368 case kCondEQ: return EQ;
1369 case kCondNE: return NE /* unordered */;
1370 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1371 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1372 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1373 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1374 default:
1375 LOG(FATAL) << "UNREACHABLE";
1376 UNREACHABLE();
1377 }
1378}
1379
Anton Kirilov74234da2017-01-13 14:42:47 +00001380inline Shift ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1381 switch (op_kind) {
1382 case HDataProcWithShifterOp::kASR: return ASR;
1383 case HDataProcWithShifterOp::kLSL: return LSL;
1384 case HDataProcWithShifterOp::kLSR: return LSR;
1385 default:
1386 LOG(FATAL) << "Unexpected op kind " << op_kind;
1387 UNREACHABLE();
1388 }
1389}
1390
1391static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1392 Register out,
1393 Register first,
1394 const ShifterOperand& second,
1395 CodeGeneratorARM* codegen) {
1396 if (second.IsImmediate() && second.GetImmediate() == 0) {
1397 const ShifterOperand in = kind == HInstruction::kAnd
1398 ? ShifterOperand(0)
1399 : ShifterOperand(first);
1400
1401 __ mov(out, in);
1402 } else {
1403 switch (kind) {
1404 case HInstruction::kAdd:
1405 __ add(out, first, second);
1406 break;
1407 case HInstruction::kAnd:
1408 __ and_(out, first, second);
1409 break;
1410 case HInstruction::kOr:
1411 __ orr(out, first, second);
1412 break;
1413 case HInstruction::kSub:
1414 __ sub(out, first, second);
1415 break;
1416 case HInstruction::kXor:
1417 __ eor(out, first, second);
1418 break;
1419 default:
1420 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1421 UNREACHABLE();
1422 }
1423 }
1424}
1425
1426static void GenerateDataProc(HInstruction::InstructionKind kind,
1427 const Location& out,
1428 const Location& first,
1429 const ShifterOperand& second_lo,
1430 const ShifterOperand& second_hi,
1431 CodeGeneratorARM* codegen) {
1432 const Register first_hi = first.AsRegisterPairHigh<Register>();
1433 const Register first_lo = first.AsRegisterPairLow<Register>();
1434 const Register out_hi = out.AsRegisterPairHigh<Register>();
1435 const Register out_lo = out.AsRegisterPairLow<Register>();
1436
1437 if (kind == HInstruction::kAdd) {
1438 __ adds(out_lo, first_lo, second_lo);
1439 __ adc(out_hi, first_hi, second_hi);
1440 } else if (kind == HInstruction::kSub) {
1441 __ subs(out_lo, first_lo, second_lo);
1442 __ sbc(out_hi, first_hi, second_hi);
1443 } else {
1444 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1445 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1446 }
1447}
1448
1449static ShifterOperand GetShifterOperand(Register rm, Shift shift, uint32_t shift_imm) {
1450 return shift_imm == 0 ? ShifterOperand(rm) : ShifterOperand(rm, shift, shift_imm);
1451}
1452
1453static void GenerateLongDataProc(HDataProcWithShifterOp* instruction, CodeGeneratorARM* codegen) {
1454 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1455 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1456
1457 const LocationSummary* const locations = instruction->GetLocations();
1458 const uint32_t shift_value = instruction->GetShiftAmount();
1459 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1460 const Location first = locations->InAt(0);
1461 const Location second = locations->InAt(1);
1462 const Location out = locations->Out();
1463 const Register first_hi = first.AsRegisterPairHigh<Register>();
1464 const Register first_lo = first.AsRegisterPairLow<Register>();
1465 const Register out_hi = out.AsRegisterPairHigh<Register>();
1466 const Register out_lo = out.AsRegisterPairLow<Register>();
1467 const Register second_hi = second.AsRegisterPairHigh<Register>();
1468 const Register second_lo = second.AsRegisterPairLow<Register>();
1469 const Shift shift = ShiftFromOpKind(instruction->GetOpKind());
1470
1471 if (shift_value >= 32) {
1472 if (shift == LSL) {
1473 GenerateDataProcInstruction(kind,
1474 out_hi,
1475 first_hi,
1476 ShifterOperand(second_lo, LSL, shift_value - 32),
1477 codegen);
1478 GenerateDataProcInstruction(kind,
1479 out_lo,
1480 first_lo,
1481 ShifterOperand(0),
1482 codegen);
1483 } else if (shift == ASR) {
1484 GenerateDataProc(kind,
1485 out,
1486 first,
1487 GetShifterOperand(second_hi, ASR, shift_value - 32),
1488 ShifterOperand(second_hi, ASR, 31),
1489 codegen);
1490 } else {
1491 DCHECK_EQ(shift, LSR);
1492 GenerateDataProc(kind,
1493 out,
1494 first,
1495 GetShifterOperand(second_hi, LSR, shift_value - 32),
1496 ShifterOperand(0),
1497 codegen);
1498 }
1499 } else {
1500 DCHECK_GT(shift_value, 1U);
1501 DCHECK_LT(shift_value, 32U);
1502
1503 if (shift == LSL) {
1504 // We are not doing this for HInstruction::kAdd because the output will require
1505 // Location::kOutputOverlap; not applicable to other cases.
1506 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1507 GenerateDataProcInstruction(kind,
1508 out_hi,
1509 first_hi,
1510 ShifterOperand(second_hi, LSL, shift_value),
1511 codegen);
1512 GenerateDataProcInstruction(kind,
1513 out_hi,
1514 out_hi,
1515 ShifterOperand(second_lo, LSR, 32 - shift_value),
1516 codegen);
1517 GenerateDataProcInstruction(kind,
1518 out_lo,
1519 first_lo,
1520 ShifterOperand(second_lo, LSL, shift_value),
1521 codegen);
1522 } else {
1523 __ Lsl(IP, second_hi, shift_value);
1524 __ orr(IP, IP, ShifterOperand(second_lo, LSR, 32 - shift_value));
1525 GenerateDataProc(kind,
1526 out,
1527 first,
1528 ShifterOperand(second_lo, LSL, shift_value),
1529 ShifterOperand(IP),
1530 codegen);
1531 }
1532 } else {
1533 DCHECK(shift == ASR || shift == LSR);
1534
1535 // We are not doing this for HInstruction::kAdd because the output will require
1536 // Location::kOutputOverlap; not applicable to other cases.
1537 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1538 GenerateDataProcInstruction(kind,
1539 out_lo,
1540 first_lo,
1541 ShifterOperand(second_lo, LSR, shift_value),
1542 codegen);
1543 GenerateDataProcInstruction(kind,
1544 out_lo,
1545 out_lo,
1546 ShifterOperand(second_hi, LSL, 32 - shift_value),
1547 codegen);
1548 GenerateDataProcInstruction(kind,
1549 out_hi,
1550 first_hi,
1551 ShifterOperand(second_hi, shift, shift_value),
1552 codegen);
1553 } else {
1554 __ Lsr(IP, second_lo, shift_value);
1555 __ orr(IP, IP, ShifterOperand(second_hi, LSL, 32 - shift_value));
1556 GenerateDataProc(kind,
1557 out,
1558 first,
1559 ShifterOperand(IP),
1560 ShifterOperand(second_hi, shift, shift_value),
1561 codegen);
1562 }
1563 }
1564 }
1565}
1566
Donghui Bai426b49c2016-11-08 14:55:38 +08001567static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARM* codegen) {
1568 Primitive::Type type = instruction->InputAt(0)->GetType();
1569 Location lhs_loc = instruction->GetLocations()->InAt(0);
1570 Location rhs_loc = instruction->GetLocations()->InAt(1);
1571 if (rhs_loc.IsConstant()) {
1572 // 0.0 is the only immediate that can be encoded directly in
1573 // a VCMP instruction.
1574 //
1575 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1576 // specify that in a floating-point comparison, positive zero
1577 // and negative zero are considered equal, so we can use the
1578 // literal 0.0 for both cases here.
1579 //
1580 // Note however that some methods (Float.equal, Float.compare,
1581 // Float.compareTo, Double.equal, Double.compare,
1582 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1583 // StrictMath.min) consider 0.0 to be (strictly) greater than
1584 // -0.0. So if we ever translate calls to these methods into a
1585 // HCompare instruction, we must handle the -0.0 case with
1586 // care here.
1587 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1588 if (type == Primitive::kPrimFloat) {
1589 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1590 } else {
1591 DCHECK_EQ(type, Primitive::kPrimDouble);
1592 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1593 }
1594 } else {
1595 if (type == Primitive::kPrimFloat) {
1596 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1597 } else {
1598 DCHECK_EQ(type, Primitive::kPrimDouble);
1599 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1600 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1601 }
1602 }
1603}
1604
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001605static std::pair<Condition, Condition> GenerateLongTestConstant(HCondition* condition,
1606 bool invert,
1607 CodeGeneratorARM* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001608 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1609
1610 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001611 IfCondition cond = condition->GetCondition();
1612 IfCondition opposite = condition->GetOppositeCondition();
1613
1614 if (invert) {
1615 std::swap(cond, opposite);
1616 }
1617
1618 std::pair<Condition, Condition> ret;
Donghui Bai426b49c2016-11-08 14:55:38 +08001619 const Location left = locations->InAt(0);
1620 const Location right = locations->InAt(1);
1621
1622 DCHECK(right.IsConstant());
1623
1624 const Register left_high = left.AsRegisterPairHigh<Register>();
1625 const Register left_low = left.AsRegisterPairLow<Register>();
1626 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1627
1628 switch (cond) {
1629 case kCondEQ:
1630 case kCondNE:
1631 case kCondB:
1632 case kCondBE:
1633 case kCondA:
1634 case kCondAE:
1635 __ CmpConstant(left_high, High32Bits(value));
1636 __ it(EQ);
1637 __ cmp(left_low, ShifterOperand(Low32Bits(value)), EQ);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001638 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001639 break;
1640 case kCondLE:
1641 case kCondGT:
1642 // Trivially true or false.
1643 if (value == std::numeric_limits<int64_t>::max()) {
1644 __ cmp(left_low, ShifterOperand(left_low));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001645 ret = cond == kCondLE ? std::make_pair(EQ, NE) : std::make_pair(NE, EQ);
Donghui Bai426b49c2016-11-08 14:55:38 +08001646 break;
1647 }
1648
1649 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001650 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001651 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001652 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001653 } else {
1654 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001655 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001656 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001657 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001658 }
1659
1660 value++;
1661 FALLTHROUGH_INTENDED;
1662 case kCondGE:
1663 case kCondLT:
1664 __ CmpConstant(left_low, Low32Bits(value));
1665 __ sbcs(IP, left_high, ShifterOperand(High32Bits(value)));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001666 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001667 break;
1668 default:
1669 LOG(FATAL) << "Unreachable";
1670 UNREACHABLE();
1671 }
1672
1673 return ret;
1674}
1675
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001676static std::pair<Condition, Condition> GenerateLongTest(HCondition* condition,
1677 bool invert,
1678 CodeGeneratorARM* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001679 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1680
1681 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001682 IfCondition cond = condition->GetCondition();
1683 IfCondition opposite = condition->GetOppositeCondition();
1684
1685 if (invert) {
1686 std::swap(cond, opposite);
1687 }
1688
1689 std::pair<Condition, Condition> ret;
Donghui Bai426b49c2016-11-08 14:55:38 +08001690 Location left = locations->InAt(0);
1691 Location right = locations->InAt(1);
1692
1693 DCHECK(right.IsRegisterPair());
1694
1695 switch (cond) {
1696 case kCondEQ:
1697 case kCondNE:
1698 case kCondB:
1699 case kCondBE:
1700 case kCondA:
1701 case kCondAE:
1702 __ cmp(left.AsRegisterPairHigh<Register>(),
1703 ShifterOperand(right.AsRegisterPairHigh<Register>()));
1704 __ it(EQ);
1705 __ cmp(left.AsRegisterPairLow<Register>(),
1706 ShifterOperand(right.AsRegisterPairLow<Register>()),
1707 EQ);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001708 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001709 break;
1710 case kCondLE:
1711 case kCondGT:
1712 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001713 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001714 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001715 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001716 } else {
1717 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001718 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001719 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001720 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001721 }
1722
1723 std::swap(left, right);
1724 FALLTHROUGH_INTENDED;
1725 case kCondGE:
1726 case kCondLT:
1727 __ cmp(left.AsRegisterPairLow<Register>(),
1728 ShifterOperand(right.AsRegisterPairLow<Register>()));
1729 __ sbcs(IP,
1730 left.AsRegisterPairHigh<Register>(),
1731 ShifterOperand(right.AsRegisterPairHigh<Register>()));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001732 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001733 break;
1734 default:
1735 LOG(FATAL) << "Unreachable";
1736 UNREACHABLE();
1737 }
1738
1739 return ret;
1740}
1741
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001742static std::pair<Condition, Condition> GenerateTest(HCondition* condition,
1743 bool invert,
1744 CodeGeneratorARM* codegen) {
1745 const LocationSummary* const locations = condition->GetLocations();
1746 const Primitive::Type type = condition->GetLeft()->GetType();
1747 IfCondition cond = condition->GetCondition();
1748 IfCondition opposite = condition->GetOppositeCondition();
1749 std::pair<Condition, Condition> ret;
1750 const Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08001751
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001752 if (invert) {
1753 std::swap(cond, opposite);
1754 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001755
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001756 if (type == Primitive::kPrimLong) {
1757 ret = locations->InAt(1).IsConstant()
1758 ? GenerateLongTestConstant(condition, invert, codegen)
1759 : GenerateLongTest(condition, invert, codegen);
1760 } else if (Primitive::IsFloatingPointType(type)) {
1761 GenerateVcmp(condition, codegen);
1762 __ vmstat();
1763 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1764 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001765 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001766 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08001767
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001768 const Register left = locations->InAt(0).AsRegister<Register>();
1769
1770 if (right.IsRegister()) {
1771 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001772 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001773 DCHECK(right.IsConstant());
1774 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001775 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001776
1777 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001778 }
1779
1780 return ret;
1781}
1782
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001783static bool CanGenerateTest(HCondition* condition, ArmAssembler* assembler) {
1784 if (condition->GetLeft()->GetType() == Primitive::kPrimLong) {
1785 const LocationSummary* const locations = condition->GetLocations();
1786 const IfCondition c = condition->GetCondition();
Donghui Bai426b49c2016-11-08 14:55:38 +08001787
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001788 if (locations->InAt(1).IsConstant()) {
1789 const int64_t value = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
1790 ShifterOperand so;
Donghui Bai426b49c2016-11-08 14:55:38 +08001791
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001792 if (c < kCondLT || c > kCondGE) {
1793 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1794 // we check that the least significant half of the first input to be compared
1795 // is in a low register (the other half is read outside an IT block), and
1796 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
1797 // encoding can be used.
1798 if (!ArmAssembler::IsLowRegister(locations->InAt(0).AsRegisterPairLow<Register>()) ||
1799 !IsUint<8>(Low32Bits(value))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001800 return false;
1801 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001802 } else if (c == kCondLE || c == kCondGT) {
1803 if (value < std::numeric_limits<int64_t>::max() &&
1804 !assembler->ShifterOperandCanHold(kNoRegister,
1805 kNoRegister,
1806 SBC,
1807 High32Bits(value + 1),
1808 kCcSet,
1809 &so)) {
1810 return false;
1811 }
1812 } else if (!assembler->ShifterOperandCanHold(kNoRegister,
1813 kNoRegister,
1814 SBC,
1815 High32Bits(value),
1816 kCcSet,
1817 &so)) {
1818 return false;
Donghui Bai426b49c2016-11-08 14:55:38 +08001819 }
1820 }
1821 }
1822
1823 return true;
1824}
1825
1826static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
1827 const Primitive::Type type = constant->GetType();
1828 bool ret = false;
1829
1830 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1831
1832 if (type == Primitive::kPrimLong) {
1833 const uint64_t value = constant->AsLongConstant()->GetValueAsUint64();
1834
1835 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1836 } else {
1837 ret = IsUint<8>(CodeGenerator::GetInt32ValueOf(constant));
1838 }
1839
1840 return ret;
1841}
1842
1843static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
1844 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
1845
1846 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1847 return Location::ConstantLocation(constant->AsConstant());
1848 }
1849
1850 return Location::RequiresRegister();
1851}
1852
1853static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1854 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1855 // we check that we are not dealing with floating-point output (there is no
1856 // 16-bit VMOV encoding).
1857 if (!out.IsRegister() && !out.IsRegisterPair()) {
1858 return false;
1859 }
1860
1861 // For constants, we also check that the output is in one or two low registers,
1862 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1863 // MOV encoding can be used.
1864 if (src.IsConstant()) {
1865 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1866 return false;
1867 }
1868
1869 if (out.IsRegister()) {
1870 if (!ArmAssembler::IsLowRegister(out.AsRegister<Register>())) {
1871 return false;
1872 }
1873 } else {
1874 DCHECK(out.IsRegisterPair());
1875
1876 if (!ArmAssembler::IsLowRegister(out.AsRegisterPairHigh<Register>())) {
1877 return false;
1878 }
1879 }
1880 }
1881
1882 return true;
1883}
1884
Anton Kirilov74234da2017-01-13 14:42:47 +00001885#undef __
1886// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1887#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
1888
Donghui Bai426b49c2016-11-08 14:55:38 +08001889Label* CodeGeneratorARM::GetFinalLabel(HInstruction* instruction, Label* final_label) {
1890 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001891 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001892
1893 const HBasicBlock* const block = instruction->GetBlock();
1894 const HLoopInformation* const info = block->GetLoopInformation();
1895 HInstruction* const next = instruction->GetNext();
1896
1897 // Avoid a branch to a branch.
1898 if (next->IsGoto() && (info == nullptr ||
1899 !info->IsBackEdge(*block) ||
1900 !info->HasSuspendCheck())) {
1901 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1902 }
1903
1904 return final_label;
1905}
1906
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001907void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001908 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001909}
1910
1911void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001912 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001913}
1914
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001915size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1916 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1917 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001918}
1919
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001920size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1921 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1922 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001923}
1924
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001925size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1926 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1927 return kArmWordSize;
1928}
1929
1930size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1931 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1932 return kArmWordSize;
1933}
1934
Calin Juravle34166012014-12-19 17:22:29 +00001935CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001936 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001937 const CompilerOptions& compiler_options,
1938 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001939 : CodeGenerator(graph,
1940 kNumberOfCoreRegisters,
1941 kNumberOfSRegisters,
1942 kNumberOfRegisterPairs,
1943 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1944 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001945 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1946 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001947 compiler_options,
1948 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001949 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001950 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001951 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001952 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001953 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001954 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001955 uint32_literals_(std::less<uint32_t>(),
1956 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001957 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1958 boot_image_string_patches_(StringReferenceValueComparator(),
1959 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1960 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001961 boot_image_type_patches_(TypeReferenceValueComparator(),
1962 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1963 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001964 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001965 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001966 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1967 jit_class_patches_(TypeReferenceValueComparator(),
1968 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001969 // Always save the LR register to mimic Quick.
1970 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001971}
1972
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001973void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1974 // Ensure that we fix up branches and literal loads and emit the literal pool.
1975 __ FinalizeCode();
1976
1977 // Adjust native pc offsets in stack maps.
1978 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001979 uint32_t old_position =
1980 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001981 uint32_t new_position = __ GetAdjustedPosition(old_position);
1982 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1983 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001984 // Adjust pc offsets for the disassembly information.
1985 if (disasm_info_ != nullptr) {
1986 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1987 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1988 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1989 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1990 it.second.start = __ GetAdjustedPosition(it.second.start);
1991 it.second.end = __ GetAdjustedPosition(it.second.end);
1992 }
1993 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1994 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1995 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1996 }
1997 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001998
1999 CodeGenerator::Finalize(allocator);
2000}
2001
David Brazdil58282f42016-01-14 12:45:10 +00002002void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002003 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002004 blocked_core_registers_[SP] = true;
2005 blocked_core_registers_[LR] = true;
2006 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002007
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002008 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002009 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002010
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002011 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002012 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002013
David Brazdil58282f42016-01-14 12:45:10 +00002014 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01002015 // Stubs do not save callee-save floating point registers. If the graph
2016 // is debuggable, we need to deal with these registers differently. For
2017 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002018 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
2019 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
2020 }
2021 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002022}
2023
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002024InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002025 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002026 assembler_(codegen->GetAssembler()),
2027 codegen_(codegen) {}
2028
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002029void CodeGeneratorARM::ComputeSpillMask() {
2030 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2031 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00002032 // There is no easy instruction to restore just the PC on thumb2. We spill and
2033 // restore another arbitrary register.
2034 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002035 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2036 // We use vpush and vpop for saving and restoring floating point registers, which take
2037 // a SRegister and the number of registers to save/restore after that SRegister. We
2038 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2039 // but in the range.
2040 if (fpu_spill_mask_ != 0) {
2041 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2042 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2043 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2044 fpu_spill_mask_ |= (1 << i);
2045 }
2046 }
2047}
2048
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002049static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002050 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002051}
2052
2053static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002054 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002055}
2056
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002057void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00002058 bool skip_overflow_check =
2059 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002060 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002061 __ Bind(&frame_entry_label_);
2062
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002063 if (HasEmptyFrame()) {
2064 return;
2065 }
2066
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002067 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002068 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
2069 __ LoadFromOffset(kLoadWord, IP, IP, 0);
2070 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002071 }
2072
Andreas Gampe501fd632015-09-10 16:11:06 -07002073 __ PushList(core_spill_mask_);
2074 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2075 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002076 if (fpu_spill_mask_ != 0) {
2077 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2078 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002079 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01002080 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002081 }
Mingyao Yang063fc772016-08-02 11:02:54 -07002082
2083 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2084 // Initialize should_deoptimize flag to 0.
2085 __ mov(IP, ShifterOperand(0));
2086 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
2087 }
2088
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002089 int adjust = GetFrameSize() - FrameEntrySpillSize();
2090 __ AddConstant(SP, -adjust);
2091 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002092
2093 // Save the current method if we need it. Note that we do not
2094 // do this in HCurrentMethod, as the instruction might have been removed
2095 // in the SSA graph.
2096 if (RequiresCurrentMethod()) {
2097 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
2098 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002099}
2100
2101void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002102 if (HasEmptyFrame()) {
2103 __ bx(LR);
2104 return;
2105 }
David Srbeckyc34dc932015-04-12 09:27:43 +01002106 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002107 int adjust = GetFrameSize() - FrameEntrySpillSize();
2108 __ AddConstant(SP, adjust);
2109 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002110 if (fpu_spill_mask_ != 0) {
2111 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2112 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07002113 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002114 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002115 }
Andreas Gampe501fd632015-09-10 16:11:06 -07002116 // Pop LR into PC to return.
2117 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
2118 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
2119 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01002120 __ cfi().RestoreState();
2121 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002122}
2123
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002124void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07002125 Label* label = GetLabelOf(block);
2126 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002127}
2128
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002129Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002130 switch (type) {
2131 case Primitive::kPrimBoolean:
2132 case Primitive::kPrimByte:
2133 case Primitive::kPrimChar:
2134 case Primitive::kPrimShort:
2135 case Primitive::kPrimInt:
2136 case Primitive::kPrimNot: {
2137 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002138 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002139 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002140 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002141 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002142 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002143 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002144 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002145
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002146 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002147 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002148 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002149 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002150 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002151 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002152 if (calling_convention.GetRegisterAt(index) == R1) {
2153 // Skip R1, and use R2_R3 instead.
2154 gp_index_++;
2155 index++;
2156 }
2157 }
2158 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2159 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002160 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01002161
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002162 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002163 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002164 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002165 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2166 }
2167 }
2168
2169 case Primitive::kPrimFloat: {
2170 uint32_t stack_index = stack_index_++;
2171 if (float_index_ % 2 == 0) {
2172 float_index_ = std::max(double_index_, float_index_);
2173 }
2174 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2175 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
2176 } else {
2177 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2178 }
2179 }
2180
2181 case Primitive::kPrimDouble: {
2182 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2183 uint32_t stack_index = stack_index_;
2184 stack_index_ += 2;
2185 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2186 uint32_t index = double_index_;
2187 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002188 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002189 calling_convention.GetFpuRegisterAt(index),
2190 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002191 DCHECK(ExpectedPairLayout(result));
2192 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002193 } else {
2194 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002195 }
2196 }
2197
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002198 case Primitive::kPrimVoid:
2199 LOG(FATAL) << "Unexpected parameter type " << type;
2200 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002201 }
Roland Levillain3b359c72015-11-17 19:35:12 +00002202 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002203}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002204
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002205Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002206 switch (type) {
2207 case Primitive::kPrimBoolean:
2208 case Primitive::kPrimByte:
2209 case Primitive::kPrimChar:
2210 case Primitive::kPrimShort:
2211 case Primitive::kPrimInt:
2212 case Primitive::kPrimNot: {
2213 return Location::RegisterLocation(R0);
2214 }
2215
2216 case Primitive::kPrimFloat: {
2217 return Location::FpuRegisterLocation(S0);
2218 }
2219
2220 case Primitive::kPrimLong: {
2221 return Location::RegisterPairLocation(R0, R1);
2222 }
2223
2224 case Primitive::kPrimDouble: {
2225 return Location::FpuRegisterPairLocation(S0, S1);
2226 }
2227
2228 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00002229 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002230 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002231
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002232 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002233}
2234
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002235Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
2236 return Location::RegisterLocation(kMethodRegisterArgument);
2237}
2238
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002239void CodeGeneratorARM::Move32(Location destination, Location source) {
2240 if (source.Equals(destination)) {
2241 return;
2242 }
2243 if (destination.IsRegister()) {
2244 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002245 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002246 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002247 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002248 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002249 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002250 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002251 } else if (destination.IsFpuRegister()) {
2252 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002253 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002254 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002255 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002256 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002257 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002258 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002259 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002260 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002261 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002262 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002263 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002264 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002265 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002266 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002267 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
2268 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002269 }
2270 }
2271}
2272
2273void CodeGeneratorARM::Move64(Location destination, Location source) {
2274 if (source.Equals(destination)) {
2275 return;
2276 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002277 if (destination.IsRegisterPair()) {
2278 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002279 EmitParallelMoves(
2280 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
2281 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002282 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002283 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002284 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
2285 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002286 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002287 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002288 } else if (source.IsFpuRegisterPair()) {
2289 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
2290 destination.AsRegisterPairHigh<Register>(),
2291 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002292 } else {
2293 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002294 DCHECK(ExpectedPairLayout(destination));
2295 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
2296 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002297 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002298 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002299 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002300 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2301 SP,
2302 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01002303 } else if (source.IsRegisterPair()) {
2304 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2305 source.AsRegisterPairLow<Register>(),
2306 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002307 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002308 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002309 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002310 } else {
2311 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002312 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002313 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002314 if (source.AsRegisterPairLow<Register>() == R1) {
2315 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002316 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
2317 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002318 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002319 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002320 SP, destination.GetStackIndex());
2321 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002322 } else if (source.IsFpuRegisterPair()) {
2323 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
2324 SP,
2325 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002326 } else {
2327 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002328 EmitParallelMoves(
2329 Location::StackSlot(source.GetStackIndex()),
2330 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002331 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002332 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002333 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
2334 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002335 }
2336 }
2337}
2338
Calin Juravle175dc732015-08-25 15:42:32 +01002339void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
2340 DCHECK(location.IsRegister());
2341 __ LoadImmediate(location.AsRegister<Register>(), value);
2342}
2343
Calin Juravlee460d1d2015-09-29 04:52:17 +01002344void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002345 HParallelMove move(GetGraph()->GetArena());
2346 move.AddMove(src, dst, dst_type, nullptr);
2347 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002348}
2349
2350void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
2351 if (location.IsRegister()) {
2352 locations->AddTemp(location);
2353 } else if (location.IsRegisterPair()) {
2354 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
2355 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
2356 } else {
2357 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2358 }
2359}
2360
Calin Juravle175dc732015-08-25 15:42:32 +01002361void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
2362 HInstruction* instruction,
2363 uint32_t dex_pc,
2364 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002365 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002366 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002367 if (EntrypointRequiresStackMap(entrypoint)) {
2368 RecordPcInfo(instruction, dex_pc, slow_path);
2369 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002370}
2371
Roland Levillaindec8f632016-07-22 17:10:06 +01002372void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2373 HInstruction* instruction,
2374 SlowPathCode* slow_path) {
2375 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002376 GenerateInvokeRuntime(entry_point_offset);
2377}
2378
2379void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01002380 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2381 __ blx(LR);
2382}
2383
David Brazdilfc6a86a2015-06-26 10:33:45 +00002384void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002385 DCHECK(!successor->IsExitBlock());
2386
2387 HBasicBlock* block = got->GetBlock();
2388 HInstruction* previous = got->GetPrevious();
2389
2390 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00002391 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002392 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2393 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2394 return;
2395 }
2396
2397 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2398 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2399 }
2400 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002401 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002402 }
2403}
2404
David Brazdilfc6a86a2015-06-26 10:33:45 +00002405void LocationsBuilderARM::VisitGoto(HGoto* got) {
2406 got->SetLocations(nullptr);
2407}
2408
2409void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
2410 HandleGoto(got, got->GetSuccessor());
2411}
2412
2413void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2414 try_boundary->SetLocations(nullptr);
2415}
2416
2417void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2418 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2419 if (!successor->IsExitBlock()) {
2420 HandleGoto(try_boundary, successor);
2421 }
2422}
2423
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002424void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002425 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002426}
2427
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002428void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002429}
2430
Roland Levillain4fa13f62015-07-06 18:11:54 +01002431void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
2432 Label* true_label,
2433 Label* false_label) {
2434 LocationSummary* locations = cond->GetLocations();
2435 Location left = locations->InAt(0);
2436 Location right = locations->InAt(1);
2437 IfCondition if_cond = cond->GetCondition();
2438
2439 Register left_high = left.AsRegisterPairHigh<Register>();
2440 Register left_low = left.AsRegisterPairLow<Register>();
2441 IfCondition true_high_cond = if_cond;
2442 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07002443 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01002444
2445 // Set the conditions for the test, remembering that == needs to be
2446 // decided using the low words.
2447 switch (if_cond) {
2448 case kCondEQ:
2449 case kCondNE:
2450 // Nothing to do.
2451 break;
2452 case kCondLT:
2453 false_high_cond = kCondGT;
2454 break;
2455 case kCondLE:
2456 true_high_cond = kCondLT;
2457 break;
2458 case kCondGT:
2459 false_high_cond = kCondLT;
2460 break;
2461 case kCondGE:
2462 true_high_cond = kCondGT;
2463 break;
Aart Bike9f37602015-10-09 11:15:55 -07002464 case kCondB:
2465 false_high_cond = kCondA;
2466 break;
2467 case kCondBE:
2468 true_high_cond = kCondB;
2469 break;
2470 case kCondA:
2471 false_high_cond = kCondB;
2472 break;
2473 case kCondAE:
2474 true_high_cond = kCondA;
2475 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002476 }
2477 if (right.IsConstant()) {
2478 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
2479 int32_t val_low = Low32Bits(value);
2480 int32_t val_high = High32Bits(value);
2481
Vladimir Markoac6ac102015-12-17 12:14:00 +00002482 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002483 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002484 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002485 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002486 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002487 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002488 __ b(true_label, ARMCondition(true_high_cond));
2489 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002490 }
2491 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00002492 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002493 } else {
2494 Register right_high = right.AsRegisterPairHigh<Register>();
2495 Register right_low = right.AsRegisterPairLow<Register>();
2496
2497 __ cmp(left_high, ShifterOperand(right_high));
2498 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002499 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002500 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002501 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002502 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002503 __ b(true_label, ARMCondition(true_high_cond));
2504 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002505 }
2506 // Must be equal high, so compare the lows.
2507 __ cmp(left_low, ShifterOperand(right_low));
2508 }
2509 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07002510 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01002511 __ b(true_label, final_condition);
2512}
2513
David Brazdil0debae72015-11-12 18:37:00 +00002514void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
2515 Label* true_target_in,
2516 Label* false_target_in) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002517 if (CanGenerateTest(condition, codegen_->GetAssembler())) {
2518 Label* non_fallthrough_target;
2519 bool invert;
2520
2521 if (true_target_in == nullptr) {
2522 DCHECK(false_target_in != nullptr);
2523 non_fallthrough_target = false_target_in;
2524 invert = true;
2525 } else {
2526 non_fallthrough_target = true_target_in;
2527 invert = false;
2528 }
2529
2530 const auto cond = GenerateTest(condition, invert, codegen_);
2531
2532 __ b(non_fallthrough_target, cond.first);
2533
2534 if (false_target_in != nullptr && false_target_in != non_fallthrough_target) {
2535 __ b(false_target_in);
2536 }
2537
2538 return;
2539 }
2540
David Brazdil0debae72015-11-12 18:37:00 +00002541 // Generated branching requires both targets to be explicit. If either of the
2542 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2543 Label fallthrough_target;
2544 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
2545 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
2546
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002547 DCHECK_EQ(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
2548 GenerateLongComparesAndJumps(condition, true_target, false_target);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002549
David Brazdil0debae72015-11-12 18:37:00 +00002550 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002551 __ b(false_target);
2552 }
David Brazdil0debae72015-11-12 18:37:00 +00002553
2554 if (fallthrough_target.IsLinked()) {
2555 __ Bind(&fallthrough_target);
2556 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002557}
2558
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002559void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002560 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002561 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002562 Label* false_target) {
2563 HInstruction* cond = instruction->InputAt(condition_input_index);
2564
2565 if (true_target == nullptr && false_target == nullptr) {
2566 // Nothing to do. The code always falls through.
2567 return;
2568 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002569 // Constant condition, statically compared against "true" (integer value 1).
2570 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002571 if (true_target != nullptr) {
2572 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002573 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002574 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002575 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002576 if (false_target != nullptr) {
2577 __ b(false_target);
2578 }
2579 }
2580 return;
2581 }
2582
2583 // The following code generates these patterns:
2584 // (1) true_target == nullptr && false_target != nullptr
2585 // - opposite condition true => branch to false_target
2586 // (2) true_target != nullptr && false_target == nullptr
2587 // - condition true => branch to true_target
2588 // (3) true_target != nullptr && false_target != nullptr
2589 // - condition true => branch to true_target
2590 // - branch to false_target
2591 if (IsBooleanValueOrMaterializedCondition(cond)) {
2592 // Condition has been materialized, compare the output to 0.
2593 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2594 DCHECK(cond_val.IsRegister());
2595 if (true_target == nullptr) {
2596 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
2597 } else {
2598 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002599 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002600 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002601 // Condition has not been materialized. Use its inputs as the comparison and
2602 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002603 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002604
2605 // If this is a long or FP comparison that has been folded into
2606 // the HCondition, generate the comparison directly.
2607 Primitive::Type type = condition->InputAt(0)->GetType();
2608 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2609 GenerateCompareTestAndBranch(condition, true_target, false_target);
2610 return;
2611 }
2612
Donghui Bai426b49c2016-11-08 14:55:38 +08002613 Label* non_fallthrough_target;
2614 Condition arm_cond;
David Brazdil0debae72015-11-12 18:37:00 +00002615 LocationSummary* locations = cond->GetLocations();
2616 DCHECK(locations->InAt(0).IsRegister());
2617 Register left = locations->InAt(0).AsRegister<Register>();
2618 Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08002619
David Brazdil0debae72015-11-12 18:37:00 +00002620 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002621 arm_cond = ARMCondition(condition->GetOppositeCondition());
2622 non_fallthrough_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002623 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002624 arm_cond = ARMCondition(condition->GetCondition());
2625 non_fallthrough_target = true_target;
2626 }
2627
2628 if (right.IsConstant() && (arm_cond == NE || arm_cond == EQ) &&
2629 CodeGenerator::GetInt32ValueOf(right.GetConstant()) == 0) {
2630 if (arm_cond == EQ) {
2631 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2632 } else {
2633 DCHECK_EQ(arm_cond, NE);
2634 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2635 }
2636 } else {
2637 if (right.IsRegister()) {
2638 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
2639 } else {
2640 DCHECK(right.IsConstant());
2641 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2642 }
2643
2644 __ b(non_fallthrough_target, arm_cond);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002645 }
Dave Allison20dfc792014-06-16 20:44:29 -07002646 }
David Brazdil0debae72015-11-12 18:37:00 +00002647
2648 // If neither branch falls through (case 3), the conditional branch to `true_target`
2649 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2650 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002651 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002652 }
2653}
2654
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002655void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002656 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2657 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002658 locations->SetInAt(0, Location::RequiresRegister());
2659 }
2660}
2661
2662void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002663 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2664 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2665 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2666 nullptr : codegen_->GetLabelOf(true_successor);
2667 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2668 nullptr : codegen_->GetLabelOf(false_successor);
2669 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002670}
2671
2672void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
2673 LocationSummary* locations = new (GetGraph()->GetArena())
2674 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002675 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002676 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002677 locations->SetInAt(0, Location::RequiresRegister());
2678 }
2679}
2680
2681void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01002682 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002683 GenerateTestAndBranch(deoptimize,
2684 /* condition_input_index */ 0,
2685 slow_path->GetEntryLabel(),
2686 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002687}
Dave Allison20dfc792014-06-16 20:44:29 -07002688
Mingyao Yang063fc772016-08-02 11:02:54 -07002689void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2690 LocationSummary* locations = new (GetGraph()->GetArena())
2691 LocationSummary(flag, LocationSummary::kNoCall);
2692 locations->SetOut(Location::RequiresRegister());
2693}
2694
2695void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2696 __ LoadFromOffset(kLoadWord,
2697 flag->GetLocations()->Out().AsRegister<Register>(),
2698 SP,
2699 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2700}
2701
David Brazdil74eb1b22015-12-14 11:44:01 +00002702void LocationsBuilderARM::VisitSelect(HSelect* select) {
2703 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08002704 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
2705
2706 if (is_floating_point) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002707 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002708 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002709 } else {
2710 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002711 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002712 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002713
David Brazdil74eb1b22015-12-14 11:44:01 +00002714 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002715 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2716 // The code generator handles overlap with the values, but not with the condition.
2717 locations->SetOut(Location::SameAsFirstInput());
2718 } else if (is_floating_point) {
2719 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2720 } else {
2721 if (!locations->InAt(1).IsConstant()) {
2722 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2723 }
2724
2725 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00002726 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002727}
2728
2729void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002730 HInstruction* const condition = select->GetCondition();
2731 const LocationSummary* const locations = select->GetLocations();
2732 const Primitive::Type type = select->GetType();
2733 const Location first = locations->InAt(0);
2734 const Location out = locations->Out();
2735 const Location second = locations->InAt(1);
2736 Location src;
2737
2738 if (condition->IsIntConstant()) {
2739 if (condition->AsIntConstant()->IsFalse()) {
2740 src = first;
2741 } else {
2742 src = second;
2743 }
2744
2745 codegen_->MoveLocation(out, src, type);
2746 return;
2747 }
2748
2749 if (!Primitive::IsFloatingPointType(type) &&
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002750 (IsBooleanValueOrMaterializedCondition(condition) ||
2751 CanGenerateTest(condition->AsCondition(), codegen_->GetAssembler()))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002752 bool invert = false;
2753
2754 if (out.Equals(second)) {
2755 src = first;
2756 invert = true;
2757 } else if (out.Equals(first)) {
2758 src = second;
2759 } else if (second.IsConstant()) {
2760 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2761 src = second;
2762 } else if (first.IsConstant()) {
2763 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2764 src = first;
2765 invert = true;
2766 } else {
2767 src = second;
2768 }
2769
2770 if (CanGenerateConditionalMove(out, src)) {
2771 if (!out.Equals(first) && !out.Equals(second)) {
2772 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2773 }
2774
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002775 std::pair<Condition, Condition> cond;
2776
2777 if (IsBooleanValueOrMaterializedCondition(condition)) {
2778 __ CmpConstant(locations->InAt(2).AsRegister<Register>(), 0);
2779 cond = invert ? std::make_pair(EQ, NE) : std::make_pair(NE, EQ);
2780 } else {
2781 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2782 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002783
2784 if (out.IsRegister()) {
2785 ShifterOperand operand;
2786
2787 if (src.IsConstant()) {
2788 operand = ShifterOperand(CodeGenerator::GetInt32ValueOf(src.GetConstant()));
2789 } else {
2790 DCHECK(src.IsRegister());
2791 operand = ShifterOperand(src.AsRegister<Register>());
2792 }
2793
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002794 __ it(cond.first);
2795 __ mov(out.AsRegister<Register>(), operand, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08002796 } else {
2797 DCHECK(out.IsRegisterPair());
2798
2799 ShifterOperand operand_high;
2800 ShifterOperand operand_low;
2801
2802 if (src.IsConstant()) {
2803 const int64_t value = src.GetConstant()->AsLongConstant()->GetValue();
2804
2805 operand_high = ShifterOperand(High32Bits(value));
2806 operand_low = ShifterOperand(Low32Bits(value));
2807 } else {
2808 DCHECK(src.IsRegisterPair());
2809 operand_high = ShifterOperand(src.AsRegisterPairHigh<Register>());
2810 operand_low = ShifterOperand(src.AsRegisterPairLow<Register>());
2811 }
2812
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002813 __ it(cond.first);
2814 __ mov(out.AsRegisterPairLow<Register>(), operand_low, cond.first);
2815 __ it(cond.first);
2816 __ mov(out.AsRegisterPairHigh<Register>(), operand_high, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08002817 }
2818
2819 return;
2820 }
2821 }
2822
2823 Label* false_target = nullptr;
2824 Label* true_target = nullptr;
2825 Label select_end;
2826 Label* target = codegen_->GetFinalLabel(select, &select_end);
2827
2828 if (out.Equals(second)) {
2829 true_target = target;
2830 src = first;
2831 } else {
2832 false_target = target;
2833 src = second;
2834
2835 if (!out.Equals(first)) {
2836 codegen_->MoveLocation(out, first, type);
2837 }
2838 }
2839
2840 GenerateTestAndBranch(select, 2, true_target, false_target);
2841 codegen_->MoveLocation(out, src, type);
2842
2843 if (select_end.IsLinked()) {
2844 __ Bind(&select_end);
2845 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002846}
2847
David Srbecky0cf44932015-12-09 14:09:59 +00002848void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2849 new (GetGraph()->GetArena()) LocationSummary(info);
2850}
2851
David Srbeckyd28f4a02016-03-14 17:14:24 +00002852void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2853 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002854}
2855
2856void CodeGeneratorARM::GenerateNop() {
2857 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002858}
2859
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002860void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002861 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002862 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002863 // Handle the long/FP comparisons made in instruction simplification.
2864 switch (cond->InputAt(0)->GetType()) {
2865 case Primitive::kPrimLong:
2866 locations->SetInAt(0, Location::RequiresRegister());
2867 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002868 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002869 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002870 }
2871 break;
2872
2873 case Primitive::kPrimFloat:
2874 case Primitive::kPrimDouble:
2875 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002876 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002877 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002878 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2879 }
2880 break;
2881
2882 default:
2883 locations->SetInAt(0, Location::RequiresRegister());
2884 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002885 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2887 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002888 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002889}
2890
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002891void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002892 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002893 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002894 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002895
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002896 const Register out = cond->GetLocations()->Out().AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002897
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002898 if (ArmAssembler::IsLowRegister(out) && CanGenerateTest(cond, codegen_->GetAssembler())) {
2899 const auto condition = GenerateTest(cond, false, codegen_);
2900
2901 __ it(condition.first);
2902 __ mov(out, ShifterOperand(1), condition.first);
2903 __ it(condition.second);
2904 __ mov(out, ShifterOperand(0), condition.second);
2905 return;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002906 }
2907
2908 // Convert the jumps into the result.
2909 Label done_label;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002910 Label* const final_label = codegen_->GetFinalLabel(cond, &done_label);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002911
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002912 if (cond->InputAt(0)->GetType() == Primitive::kPrimLong) {
2913 Label true_label, false_label;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002914
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002915 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2916
2917 // False case: result = 0.
2918 __ Bind(&false_label);
2919 __ LoadImmediate(out, 0);
2920 __ b(final_label);
2921
2922 // True case: result = 1.
2923 __ Bind(&true_label);
2924 __ LoadImmediate(out, 1);
2925 } else {
2926 DCHECK(CanGenerateTest(cond, codegen_->GetAssembler()));
2927
2928 const auto condition = GenerateTest(cond, false, codegen_);
2929
2930 __ mov(out, ShifterOperand(0), AL, kCcKeep);
2931 __ b(final_label, condition.second);
2932 __ LoadImmediate(out, 1);
2933 }
Anton Kirilov6f644202017-02-27 18:29:45 +00002934
2935 if (done_label.IsLinked()) {
2936 __ Bind(&done_label);
2937 }
Dave Allison20dfc792014-06-16 20:44:29 -07002938}
2939
2940void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002941 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002942}
2943
2944void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002945 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002946}
2947
2948void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002949 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002950}
2951
2952void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002953 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002954}
2955
2956void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002957 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002958}
2959
2960void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002961 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002962}
2963
2964void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002965 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002966}
2967
2968void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002969 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002970}
2971
2972void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002973 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002974}
2975
2976void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002977 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002978}
2979
2980void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002981 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002982}
2983
2984void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002985 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002986}
2987
Aart Bike9f37602015-10-09 11:15:55 -07002988void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002989 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002990}
2991
2992void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002993 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002994}
2995
2996void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002997 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002998}
2999
3000void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003001 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003002}
3003
3004void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003005 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003006}
3007
3008void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003009 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003010}
3011
3012void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003013 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003014}
3015
3016void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003017 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003018}
3019
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003020void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003021 LocationSummary* locations =
3022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003023 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003024}
3025
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003026void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01003027 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003028}
3029
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003030void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
3031 LocationSummary* locations =
3032 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3033 locations->SetOut(Location::ConstantLocation(constant));
3034}
3035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003036void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003037 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003038}
3039
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003040void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003041 LocationSummary* locations =
3042 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003043 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003044}
3045
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003046void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003047 // Will be generated at use site.
3048}
3049
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003050void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
3051 LocationSummary* locations =
3052 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3053 locations->SetOut(Location::ConstantLocation(constant));
3054}
3055
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003056void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003057 // Will be generated at use site.
3058}
3059
3060void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
3061 LocationSummary* locations =
3062 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3063 locations->SetOut(Location::ConstantLocation(constant));
3064}
3065
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003066void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003067 // Will be generated at use site.
3068}
3069
Igor Murashkind01745e2017-04-05 16:40:31 -07003070void LocationsBuilderARM::VisitConstructorFence(HConstructorFence* constructor_fence) {
3071 constructor_fence->SetLocations(nullptr);
3072}
3073
3074void InstructionCodeGeneratorARM::VisitConstructorFence(
3075 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3076 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3077}
3078
Calin Juravle27df7582015-04-17 19:12:31 +01003079void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3080 memory_barrier->SetLocations(nullptr);
3081}
3082
3083void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00003084 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01003085}
3086
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003087void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003088 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003089}
3090
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003091void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003092 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003093}
3094
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003095void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003096 LocationSummary* locations =
3097 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003098 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003099}
3100
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003101void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003102 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003103}
3104
Calin Juravle175dc732015-08-25 15:42:32 +01003105void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3106 // The trampoline uses the same calling convention as dex calling conventions,
3107 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3108 // the method_idx.
3109 HandleInvoke(invoke);
3110}
3111
3112void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3113 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3114}
3115
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003116void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003117 // Explicit clinit checks triggered by static invokes must have been pruned by
3118 // art::PrepareForRegisterAllocation.
3119 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003120
Vladimir Marko68c981f2016-08-26 13:13:33 +01003121 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003122 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00003123 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3124 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3125 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003126 return;
3127 }
3128
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003129 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00003130
3131 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3132 if (invoke->HasPcRelativeDexCache()) {
3133 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3134 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003135}
3136
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003137static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
3138 if (invoke->GetLocations()->Intrinsified()) {
3139 IntrinsicCodeGeneratorARM intrinsic(codegen);
3140 intrinsic.Dispatch(invoke);
3141 return true;
3142 }
3143 return false;
3144}
3145
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003146void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003147 // Explicit clinit checks triggered by static invokes must have been pruned by
3148 // art::PrepareForRegisterAllocation.
3149 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003150
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003151 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3152 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003153 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003154
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003155 LocationSummary* locations = invoke->GetLocations();
3156 codegen_->GenerateStaticOrDirectCall(
3157 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003158 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003159}
3160
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003161void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003162 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003163 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003164}
3165
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003166void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01003167 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003168 if (intrinsic.TryDispatch(invoke)) {
3169 return;
3170 }
3171
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003172 HandleInvoke(invoke);
3173}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003174
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003175void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003176 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3177 return;
3178 }
3179
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003180 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003181 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003182 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003183}
3184
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003185void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3186 HandleInvoke(invoke);
3187 // Add the hidden argument.
3188 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
3189}
3190
3191void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3192 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00003193 LocationSummary* locations = invoke->GetLocations();
3194 Register temp = locations->GetTemp(0).AsRegister<Register>();
3195 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003196 Location receiver = locations->InAt(0);
3197 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3198
Roland Levillain3b359c72015-11-17 19:35:12 +00003199 // Set the hidden argument. This is safe to do this here, as R12
3200 // won't be modified thereafter, before the `blx` (call) instruction.
3201 DCHECK_EQ(R12, hidden_reg);
3202 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003203
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003204 if (receiver.IsStackSlot()) {
3205 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00003206 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003207 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3208 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00003209 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00003210 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003211 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003212 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00003213 // Instead of simply (possibly) unpoisoning `temp` here, we should
3214 // emit a read barrier for the previous class reference load.
3215 // However this is not required in practice, as this is an
3216 // intermediate/temporary reference and because the current
3217 // concurrent copying collector keeps the from-space memory
3218 // intact/accessible until the end of the marking phase (the
3219 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003220 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003221 __ LoadFromOffset(kLoadWord, temp, temp,
3222 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3223 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003224 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003225 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003226 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00003227 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07003228 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003229 // LR = temp->GetEntryPoint();
3230 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
3231 // LR();
3232 __ blx(LR);
3233 DCHECK(!codegen_->IsLeafMethod());
3234 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3235}
3236
Orion Hodsonac141392017-01-13 11:53:47 +00003237void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3238 HandleInvoke(invoke);
3239}
3240
3241void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3242 codegen_->GenerateInvokePolymorphicCall(invoke);
3243}
3244
Roland Levillain88cb1752014-10-20 16:36:47 +01003245void LocationsBuilderARM::VisitNeg(HNeg* neg) {
3246 LocationSummary* locations =
3247 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3248 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003249 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01003250 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003251 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3252 break;
3253 }
3254 case Primitive::kPrimLong: {
3255 locations->SetInAt(0, Location::RequiresRegister());
3256 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003257 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003258 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003259
Roland Levillain88cb1752014-10-20 16:36:47 +01003260 case Primitive::kPrimFloat:
3261 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003262 locations->SetInAt(0, Location::RequiresFpuRegister());
3263 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003264 break;
3265
3266 default:
3267 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3268 }
3269}
3270
3271void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
3272 LocationSummary* locations = neg->GetLocations();
3273 Location out = locations->Out();
3274 Location in = locations->InAt(0);
3275 switch (neg->GetResultType()) {
3276 case Primitive::kPrimInt:
3277 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003278 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01003279 break;
3280
3281 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003282 DCHECK(in.IsRegisterPair());
3283 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3284 __ rsbs(out.AsRegisterPairLow<Register>(),
3285 in.AsRegisterPairLow<Register>(),
3286 ShifterOperand(0));
3287 // We cannot emit an RSC (Reverse Subtract with Carry)
3288 // instruction here, as it does not exist in the Thumb-2
3289 // instruction set. We use the following approach
3290 // using SBC and SUB instead.
3291 //
3292 // out.hi = -C
3293 __ sbc(out.AsRegisterPairHigh<Register>(),
3294 out.AsRegisterPairHigh<Register>(),
3295 ShifterOperand(out.AsRegisterPairHigh<Register>()));
3296 // out.hi = out.hi - in.hi
3297 __ sub(out.AsRegisterPairHigh<Register>(),
3298 out.AsRegisterPairHigh<Register>(),
3299 ShifterOperand(in.AsRegisterPairHigh<Register>()));
3300 break;
3301
Roland Levillain88cb1752014-10-20 16:36:47 +01003302 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003303 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003304 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00003305 break;
3306
Roland Levillain88cb1752014-10-20 16:36:47 +01003307 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003308 DCHECK(in.IsFpuRegisterPair());
3309 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3310 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01003311 break;
3312
3313 default:
3314 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3315 }
3316}
3317
Roland Levillaindff1f282014-11-05 14:15:05 +00003318void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003319 Primitive::Type result_type = conversion->GetResultType();
3320 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003321 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00003322
Roland Levillain5b3ee562015-04-14 16:02:41 +01003323 // The float-to-long, double-to-long and long-to-float type conversions
3324 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003325 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01003326 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3327 && result_type == Primitive::kPrimLong)
3328 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003329 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00003330 : LocationSummary::kNoCall;
3331 LocationSummary* locations =
3332 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3333
David Brazdilb2bd1c52015-03-25 11:17:37 +00003334 // The Java language does not allow treating boolean as an integral type but
3335 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00003336
Roland Levillaindff1f282014-11-05 14:15:05 +00003337 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003338 case Primitive::kPrimByte:
3339 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003340 case Primitive::kPrimLong:
3341 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003342 case Primitive::kPrimBoolean:
3343 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003344 case Primitive::kPrimShort:
3345 case Primitive::kPrimInt:
3346 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003347 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003348 locations->SetInAt(0, Location::RequiresRegister());
3349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3350 break;
3351
3352 default:
3353 LOG(FATAL) << "Unexpected type conversion from " << input_type
3354 << " to " << result_type;
3355 }
3356 break;
3357
Roland Levillain01a8d712014-11-14 16:27:39 +00003358 case Primitive::kPrimShort:
3359 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003360 case Primitive::kPrimLong:
3361 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003362 case Primitive::kPrimBoolean:
3363 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003364 case Primitive::kPrimByte:
3365 case Primitive::kPrimInt:
3366 case Primitive::kPrimChar:
3367 // Processing a Dex `int-to-short' instruction.
3368 locations->SetInAt(0, Location::RequiresRegister());
3369 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3370 break;
3371
3372 default:
3373 LOG(FATAL) << "Unexpected type conversion from " << input_type
3374 << " to " << result_type;
3375 }
3376 break;
3377
Roland Levillain946e1432014-11-11 17:35:19 +00003378 case Primitive::kPrimInt:
3379 switch (input_type) {
3380 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003381 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003382 locations->SetInAt(0, Location::Any());
3383 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3384 break;
3385
3386 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00003387 // Processing a Dex `float-to-int' instruction.
3388 locations->SetInAt(0, Location::RequiresFpuRegister());
3389 locations->SetOut(Location::RequiresRegister());
3390 locations->AddTemp(Location::RequiresFpuRegister());
3391 break;
3392
Roland Levillain946e1432014-11-11 17:35:19 +00003393 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003394 // Processing a Dex `double-to-int' instruction.
3395 locations->SetInAt(0, Location::RequiresFpuRegister());
3396 locations->SetOut(Location::RequiresRegister());
3397 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00003398 break;
3399
3400 default:
3401 LOG(FATAL) << "Unexpected type conversion from " << input_type
3402 << " to " << result_type;
3403 }
3404 break;
3405
Roland Levillaindff1f282014-11-05 14:15:05 +00003406 case Primitive::kPrimLong:
3407 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003408 case Primitive::kPrimBoolean:
3409 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003410 case Primitive::kPrimByte:
3411 case Primitive::kPrimShort:
3412 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003413 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003414 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003415 locations->SetInAt(0, Location::RequiresRegister());
3416 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3417 break;
3418
Roland Levillain624279f2014-12-04 11:54:28 +00003419 case Primitive::kPrimFloat: {
3420 // Processing a Dex `float-to-long' instruction.
3421 InvokeRuntimeCallingConvention calling_convention;
3422 locations->SetInAt(0, Location::FpuRegisterLocation(
3423 calling_convention.GetFpuRegisterAt(0)));
3424 locations->SetOut(Location::RegisterPairLocation(R0, R1));
3425 break;
3426 }
3427
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003428 case Primitive::kPrimDouble: {
3429 // Processing a Dex `double-to-long' instruction.
3430 InvokeRuntimeCallingConvention calling_convention;
3431 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3432 calling_convention.GetFpuRegisterAt(0),
3433 calling_convention.GetFpuRegisterAt(1)));
3434 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00003435 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003436 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003437
3438 default:
3439 LOG(FATAL) << "Unexpected type conversion from " << input_type
3440 << " to " << result_type;
3441 }
3442 break;
3443
Roland Levillain981e4542014-11-14 11:47:14 +00003444 case Primitive::kPrimChar:
3445 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003446 case Primitive::kPrimLong:
3447 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003448 case Primitive::kPrimBoolean:
3449 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003450 case Primitive::kPrimByte:
3451 case Primitive::kPrimShort:
3452 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003453 // Processing a Dex `int-to-char' instruction.
3454 locations->SetInAt(0, Location::RequiresRegister());
3455 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3456 break;
3457
3458 default:
3459 LOG(FATAL) << "Unexpected type conversion from " << input_type
3460 << " to " << result_type;
3461 }
3462 break;
3463
Roland Levillaindff1f282014-11-05 14:15:05 +00003464 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003465 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003466 case Primitive::kPrimBoolean:
3467 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003468 case Primitive::kPrimByte:
3469 case Primitive::kPrimShort:
3470 case Primitive::kPrimInt:
3471 case Primitive::kPrimChar:
3472 // Processing a Dex `int-to-float' instruction.
3473 locations->SetInAt(0, Location::RequiresRegister());
3474 locations->SetOut(Location::RequiresFpuRegister());
3475 break;
3476
Roland Levillain5b3ee562015-04-14 16:02:41 +01003477 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00003478 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01003479 InvokeRuntimeCallingConvention calling_convention;
3480 locations->SetInAt(0, Location::RegisterPairLocation(
3481 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3482 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00003483 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01003484 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003485
Roland Levillaincff13742014-11-17 14:32:17 +00003486 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003487 // Processing a Dex `double-to-float' instruction.
3488 locations->SetInAt(0, Location::RequiresFpuRegister());
3489 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003490 break;
3491
3492 default:
3493 LOG(FATAL) << "Unexpected type conversion from " << input_type
3494 << " to " << result_type;
3495 };
3496 break;
3497
Roland Levillaindff1f282014-11-05 14:15:05 +00003498 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003499 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003500 case Primitive::kPrimBoolean:
3501 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003502 case Primitive::kPrimByte:
3503 case Primitive::kPrimShort:
3504 case Primitive::kPrimInt:
3505 case Primitive::kPrimChar:
3506 // Processing a Dex `int-to-double' instruction.
3507 locations->SetInAt(0, Location::RequiresRegister());
3508 locations->SetOut(Location::RequiresFpuRegister());
3509 break;
3510
3511 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003512 // Processing a Dex `long-to-double' instruction.
3513 locations->SetInAt(0, Location::RequiresRegister());
3514 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01003515 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00003516 locations->AddTemp(Location::RequiresFpuRegister());
3517 break;
3518
Roland Levillaincff13742014-11-17 14:32:17 +00003519 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003520 // Processing a Dex `float-to-double' instruction.
3521 locations->SetInAt(0, Location::RequiresFpuRegister());
3522 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003523 break;
3524
3525 default:
3526 LOG(FATAL) << "Unexpected type conversion from " << input_type
3527 << " to " << result_type;
3528 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003529 break;
3530
3531 default:
3532 LOG(FATAL) << "Unexpected type conversion from " << input_type
3533 << " to " << result_type;
3534 }
3535}
3536
3537void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
3538 LocationSummary* locations = conversion->GetLocations();
3539 Location out = locations->Out();
3540 Location in = locations->InAt(0);
3541 Primitive::Type result_type = conversion->GetResultType();
3542 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003543 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00003544 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003545 case Primitive::kPrimByte:
3546 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003547 case Primitive::kPrimLong:
3548 // Type conversion from long to byte is a result of code transformations.
3549 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
3550 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003551 case Primitive::kPrimBoolean:
3552 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003553 case Primitive::kPrimShort:
3554 case Primitive::kPrimInt:
3555 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003556 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003557 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00003558 break;
3559
3560 default:
3561 LOG(FATAL) << "Unexpected type conversion from " << input_type
3562 << " to " << result_type;
3563 }
3564 break;
3565
Roland Levillain01a8d712014-11-14 16:27:39 +00003566 case Primitive::kPrimShort:
3567 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003568 case Primitive::kPrimLong:
3569 // Type conversion from long to short is a result of code transformations.
3570 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3571 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003572 case Primitive::kPrimBoolean:
3573 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003574 case Primitive::kPrimByte:
3575 case Primitive::kPrimInt:
3576 case Primitive::kPrimChar:
3577 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003578 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00003579 break;
3580
3581 default:
3582 LOG(FATAL) << "Unexpected type conversion from " << input_type
3583 << " to " << result_type;
3584 }
3585 break;
3586
Roland Levillain946e1432014-11-11 17:35:19 +00003587 case Primitive::kPrimInt:
3588 switch (input_type) {
3589 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003590 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003591 DCHECK(out.IsRegister());
3592 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003593 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003594 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003595 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00003596 } else {
3597 DCHECK(in.IsConstant());
3598 DCHECK(in.GetConstant()->IsLongConstant());
3599 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003600 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00003601 }
3602 break;
3603
Roland Levillain3f8f9362014-12-02 17:45:01 +00003604 case Primitive::kPrimFloat: {
3605 // Processing a Dex `float-to-int' instruction.
3606 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003607 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00003608 __ vmovrs(out.AsRegister<Register>(), temp);
3609 break;
3610 }
3611
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003612 case Primitive::kPrimDouble: {
3613 // Processing a Dex `double-to-int' instruction.
3614 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003615 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003616 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00003617 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003618 }
Roland Levillain946e1432014-11-11 17:35:19 +00003619
3620 default:
3621 LOG(FATAL) << "Unexpected type conversion from " << input_type
3622 << " to " << result_type;
3623 }
3624 break;
3625
Roland Levillaindff1f282014-11-05 14:15:05 +00003626 case Primitive::kPrimLong:
3627 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003628 case Primitive::kPrimBoolean:
3629 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003630 case Primitive::kPrimByte:
3631 case Primitive::kPrimShort:
3632 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003633 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003634 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003635 DCHECK(out.IsRegisterPair());
3636 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003637 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00003638 // Sign extension.
3639 __ Asr(out.AsRegisterPairHigh<Register>(),
3640 out.AsRegisterPairLow<Register>(),
3641 31);
3642 break;
3643
3644 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00003645 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003646 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003647 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003648 break;
3649
Roland Levillaindff1f282014-11-05 14:15:05 +00003650 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003651 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003652 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003653 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003654 break;
3655
3656 default:
3657 LOG(FATAL) << "Unexpected type conversion from " << input_type
3658 << " to " << result_type;
3659 }
3660 break;
3661
Roland Levillain981e4542014-11-14 11:47:14 +00003662 case Primitive::kPrimChar:
3663 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003664 case Primitive::kPrimLong:
3665 // Type conversion from long to char is a result of code transformations.
3666 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3667 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003668 case Primitive::kPrimBoolean:
3669 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003670 case Primitive::kPrimByte:
3671 case Primitive::kPrimShort:
3672 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003673 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003674 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00003675 break;
3676
3677 default:
3678 LOG(FATAL) << "Unexpected type conversion from " << input_type
3679 << " to " << result_type;
3680 }
3681 break;
3682
Roland Levillaindff1f282014-11-05 14:15:05 +00003683 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003684 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003685 case Primitive::kPrimBoolean:
3686 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003687 case Primitive::kPrimByte:
3688 case Primitive::kPrimShort:
3689 case Primitive::kPrimInt:
3690 case Primitive::kPrimChar: {
3691 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003692 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
3693 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003694 break;
3695 }
3696
Roland Levillain5b3ee562015-04-14 16:02:41 +01003697 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003698 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003699 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003700 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00003701 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003702
Roland Levillaincff13742014-11-17 14:32:17 +00003703 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003704 // Processing a Dex `double-to-float' instruction.
3705 __ vcvtsd(out.AsFpuRegister<SRegister>(),
3706 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00003707 break;
3708
3709 default:
3710 LOG(FATAL) << "Unexpected type conversion from " << input_type
3711 << " to " << result_type;
3712 };
3713 break;
3714
Roland Levillaindff1f282014-11-05 14:15:05 +00003715 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003716 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003717 case Primitive::kPrimBoolean:
3718 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003719 case Primitive::kPrimByte:
3720 case Primitive::kPrimShort:
3721 case Primitive::kPrimInt:
3722 case Primitive::kPrimChar: {
3723 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003724 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003725 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3726 out.AsFpuRegisterPairLow<SRegister>());
3727 break;
3728 }
3729
Roland Levillain647b9ed2014-11-27 12:06:00 +00003730 case Primitive::kPrimLong: {
3731 // Processing a Dex `long-to-double' instruction.
3732 Register low = in.AsRegisterPairLow<Register>();
3733 Register high = in.AsRegisterPairHigh<Register>();
3734 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
3735 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003736 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00003737 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003738 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
3739 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003740
Roland Levillain682393c2015-04-14 15:57:52 +01003741 // temp_d = int-to-double(high)
3742 __ vmovsr(temp_s, high);
3743 __ vcvtdi(temp_d, temp_s);
3744 // constant_d = k2Pow32EncodingForDouble
3745 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
3746 // out_d = unsigned-to-double(low)
3747 __ vmovsr(out_s, low);
3748 __ vcvtdu(out_d, out_s);
3749 // out_d += temp_d * constant_d
3750 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003751 break;
3752 }
3753
Roland Levillaincff13742014-11-17 14:32:17 +00003754 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003755 // Processing a Dex `float-to-double' instruction.
3756 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3757 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003758 break;
3759
3760 default:
3761 LOG(FATAL) << "Unexpected type conversion from " << input_type
3762 << " to " << result_type;
3763 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003764 break;
3765
3766 default:
3767 LOG(FATAL) << "Unexpected type conversion from " << input_type
3768 << " to " << result_type;
3769 }
3770}
3771
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003772void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003773 LocationSummary* locations =
3774 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003775 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003776 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003777 locations->SetInAt(0, Location::RequiresRegister());
3778 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003779 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3780 break;
3781 }
3782
3783 case Primitive::kPrimLong: {
3784 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003785 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003786 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003787 break;
3788 }
3789
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003790 case Primitive::kPrimFloat:
3791 case Primitive::kPrimDouble: {
3792 locations->SetInAt(0, Location::RequiresFpuRegister());
3793 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003794 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003795 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003796 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003797
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003798 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003799 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003800 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003801}
3802
3803void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
3804 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003805 Location out = locations->Out();
3806 Location first = locations->InAt(0);
3807 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003808 switch (add->GetResultType()) {
3809 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003810 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003811 __ add(out.AsRegister<Register>(),
3812 first.AsRegister<Register>(),
3813 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003814 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003815 __ AddConstant(out.AsRegister<Register>(),
3816 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003817 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003818 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003819 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003820
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003821 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003822 if (second.IsConstant()) {
3823 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3824 GenerateAddLongConst(out, first, value);
3825 } else {
3826 DCHECK(second.IsRegisterPair());
3827 __ adds(out.AsRegisterPairLow<Register>(),
3828 first.AsRegisterPairLow<Register>(),
3829 ShifterOperand(second.AsRegisterPairLow<Register>()));
3830 __ adc(out.AsRegisterPairHigh<Register>(),
3831 first.AsRegisterPairHigh<Register>(),
3832 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3833 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003834 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003835 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003836
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003837 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00003838 __ vadds(out.AsFpuRegister<SRegister>(),
3839 first.AsFpuRegister<SRegister>(),
3840 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003841 break;
3842
3843 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003844 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3845 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3846 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003847 break;
3848
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003849 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003850 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003851 }
3852}
3853
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003854void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003855 LocationSummary* locations =
3856 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003857 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003858 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003859 locations->SetInAt(0, Location::RequiresRegister());
3860 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003861 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3862 break;
3863 }
3864
3865 case Primitive::kPrimLong: {
3866 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003867 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003868 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003869 break;
3870 }
Calin Juravle11351682014-10-23 15:38:15 +01003871 case Primitive::kPrimFloat:
3872 case Primitive::kPrimDouble: {
3873 locations->SetInAt(0, Location::RequiresFpuRegister());
3874 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003875 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003876 break;
Calin Juravle11351682014-10-23 15:38:15 +01003877 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003878 default:
Calin Juravle11351682014-10-23 15:38:15 +01003879 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003880 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003881}
3882
3883void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3884 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003885 Location out = locations->Out();
3886 Location first = locations->InAt(0);
3887 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003888 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003889 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003890 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003891 __ sub(out.AsRegister<Register>(),
3892 first.AsRegister<Register>(),
3893 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003894 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003895 __ AddConstant(out.AsRegister<Register>(),
3896 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003897 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003898 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003899 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003900 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003901
Calin Juravle11351682014-10-23 15:38:15 +01003902 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003903 if (second.IsConstant()) {
3904 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3905 GenerateAddLongConst(out, first, -value);
3906 } else {
3907 DCHECK(second.IsRegisterPair());
3908 __ subs(out.AsRegisterPairLow<Register>(),
3909 first.AsRegisterPairLow<Register>(),
3910 ShifterOperand(second.AsRegisterPairLow<Register>()));
3911 __ sbc(out.AsRegisterPairHigh<Register>(),
3912 first.AsRegisterPairHigh<Register>(),
3913 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3914 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003915 break;
Calin Juravle11351682014-10-23 15:38:15 +01003916 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003917
Calin Juravle11351682014-10-23 15:38:15 +01003918 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003919 __ vsubs(out.AsFpuRegister<SRegister>(),
3920 first.AsFpuRegister<SRegister>(),
3921 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003922 break;
Calin Juravle11351682014-10-23 15:38:15 +01003923 }
3924
3925 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003926 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3927 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3928 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003929 break;
3930 }
3931
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003932
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003933 default:
Calin Juravle11351682014-10-23 15:38:15 +01003934 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003935 }
3936}
3937
Calin Juravle34bacdf2014-10-07 20:23:36 +01003938void LocationsBuilderARM::VisitMul(HMul* mul) {
3939 LocationSummary* locations =
3940 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3941 switch (mul->GetResultType()) {
3942 case Primitive::kPrimInt:
3943 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003944 locations->SetInAt(0, Location::RequiresRegister());
3945 locations->SetInAt(1, Location::RequiresRegister());
3946 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003947 break;
3948 }
3949
Calin Juravleb5bfa962014-10-21 18:02:24 +01003950 case Primitive::kPrimFloat:
3951 case Primitive::kPrimDouble: {
3952 locations->SetInAt(0, Location::RequiresFpuRegister());
3953 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003954 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003955 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003956 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003957
3958 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003959 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003960 }
3961}
3962
3963void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3964 LocationSummary* locations = mul->GetLocations();
3965 Location out = locations->Out();
3966 Location first = locations->InAt(0);
3967 Location second = locations->InAt(1);
3968 switch (mul->GetResultType()) {
3969 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003970 __ mul(out.AsRegister<Register>(),
3971 first.AsRegister<Register>(),
3972 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003973 break;
3974 }
3975 case Primitive::kPrimLong: {
3976 Register out_hi = out.AsRegisterPairHigh<Register>();
3977 Register out_lo = out.AsRegisterPairLow<Register>();
3978 Register in1_hi = first.AsRegisterPairHigh<Register>();
3979 Register in1_lo = first.AsRegisterPairLow<Register>();
3980 Register in2_hi = second.AsRegisterPairHigh<Register>();
3981 Register in2_lo = second.AsRegisterPairLow<Register>();
3982
3983 // Extra checks to protect caused by the existence of R1_R2.
3984 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3985 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3986 DCHECK_NE(out_hi, in1_lo);
3987 DCHECK_NE(out_hi, in2_lo);
3988
3989 // input: in1 - 64 bits, in2 - 64 bits
3990 // output: out
3991 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3992 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3993 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3994
3995 // IP <- in1.lo * in2.hi
3996 __ mul(IP, in1_lo, in2_hi);
3997 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3998 __ mla(out_hi, in1_hi, in2_lo, IP);
3999 // out.lo <- (in1.lo * in2.lo)[31:0];
4000 __ umull(out_lo, IP, in1_lo, in2_lo);
4001 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
4002 __ add(out_hi, out_hi, ShifterOperand(IP));
4003 break;
4004 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01004005
4006 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004007 __ vmuls(out.AsFpuRegister<SRegister>(),
4008 first.AsFpuRegister<SRegister>(),
4009 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004010 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004011 }
4012
4013 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004014 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4015 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4016 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01004017 break;
4018 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004019
4020 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004021 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004022 }
4023}
4024
Zheng Xuc6667102015-05-15 16:08:45 +08004025void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4026 DCHECK(instruction->IsDiv() || instruction->IsRem());
4027 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4028
4029 LocationSummary* locations = instruction->GetLocations();
4030 Location second = locations->InAt(1);
4031 DCHECK(second.IsConstant());
4032
4033 Register out = locations->Out().AsRegister<Register>();
4034 Register dividend = locations->InAt(0).AsRegister<Register>();
4035 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4036 DCHECK(imm == 1 || imm == -1);
4037
4038 if (instruction->IsRem()) {
4039 __ LoadImmediate(out, 0);
4040 } else {
4041 if (imm == 1) {
4042 __ Mov(out, dividend);
4043 } else {
4044 __ rsb(out, dividend, ShifterOperand(0));
4045 }
4046 }
4047}
4048
4049void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4050 DCHECK(instruction->IsDiv() || instruction->IsRem());
4051 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4052
4053 LocationSummary* locations = instruction->GetLocations();
4054 Location second = locations->InAt(1);
4055 DCHECK(second.IsConstant());
4056
4057 Register out = locations->Out().AsRegister<Register>();
4058 Register dividend = locations->InAt(0).AsRegister<Register>();
4059 Register temp = locations->GetTemp(0).AsRegister<Register>();
4060 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004061 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08004062 int ctz_imm = CTZ(abs_imm);
4063
4064 if (ctz_imm == 1) {
4065 __ Lsr(temp, dividend, 32 - ctz_imm);
4066 } else {
4067 __ Asr(temp, dividend, 31);
4068 __ Lsr(temp, temp, 32 - ctz_imm);
4069 }
4070 __ add(out, temp, ShifterOperand(dividend));
4071
4072 if (instruction->IsDiv()) {
4073 __ Asr(out, out, ctz_imm);
4074 if (imm < 0) {
4075 __ rsb(out, out, ShifterOperand(0));
4076 }
4077 } else {
4078 __ ubfx(out, out, 0, ctz_imm);
4079 __ sub(out, out, ShifterOperand(temp));
4080 }
4081}
4082
4083void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4084 DCHECK(instruction->IsDiv() || instruction->IsRem());
4085 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4086
4087 LocationSummary* locations = instruction->GetLocations();
4088 Location second = locations->InAt(1);
4089 DCHECK(second.IsConstant());
4090
4091 Register out = locations->Out().AsRegister<Register>();
4092 Register dividend = locations->InAt(0).AsRegister<Register>();
4093 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4094 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4095 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4096
4097 int64_t magic;
4098 int shift;
4099 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4100
4101 __ LoadImmediate(temp1, magic);
4102 __ smull(temp2, temp1, dividend, temp1);
4103
4104 if (imm > 0 && magic < 0) {
4105 __ add(temp1, temp1, ShifterOperand(dividend));
4106 } else if (imm < 0 && magic > 0) {
4107 __ sub(temp1, temp1, ShifterOperand(dividend));
4108 }
4109
4110 if (shift != 0) {
4111 __ Asr(temp1, temp1, shift);
4112 }
4113
4114 if (instruction->IsDiv()) {
4115 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
4116 } else {
4117 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
4118 // TODO: Strength reduction for mls.
4119 __ LoadImmediate(temp2, imm);
4120 __ mls(out, temp1, temp2, dividend);
4121 }
4122}
4123
4124void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
4125 DCHECK(instruction->IsDiv() || instruction->IsRem());
4126 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4127
4128 LocationSummary* locations = instruction->GetLocations();
4129 Location second = locations->InAt(1);
4130 DCHECK(second.IsConstant());
4131
4132 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4133 if (imm == 0) {
4134 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4135 } else if (imm == 1 || imm == -1) {
4136 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004137 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004138 DivRemByPowerOfTwo(instruction);
4139 } else {
4140 DCHECK(imm <= -2 || imm >= 2);
4141 GenerateDivRemWithAnyConstant(instruction);
4142 }
4143}
4144
Calin Juravle7c4954d2014-10-28 16:57:40 +00004145void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004146 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4147 if (div->GetResultType() == Primitive::kPrimLong) {
4148 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004149 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004150 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4151 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004152 } else if (div->GetResultType() == Primitive::kPrimInt &&
4153 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4154 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004155 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004156 }
4157
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004158 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4159
Calin Juravle7c4954d2014-10-28 16:57:40 +00004160 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004161 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004162 if (div->InputAt(1)->IsConstant()) {
4163 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004164 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004165 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004166 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
4167 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004168 // No temp register required.
4169 } else {
4170 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004171 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004172 locations->AddTemp(Location::RequiresRegister());
4173 }
4174 }
4175 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004176 locations->SetInAt(0, Location::RequiresRegister());
4177 locations->SetInAt(1, Location::RequiresRegister());
4178 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4179 } else {
4180 InvokeRuntimeCallingConvention calling_convention;
4181 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4182 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004183 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004184 // we only need the former.
4185 locations->SetOut(Location::RegisterLocation(R0));
4186 }
Calin Juravled0d48522014-11-04 16:40:20 +00004187 break;
4188 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004189 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004190 InvokeRuntimeCallingConvention calling_convention;
4191 locations->SetInAt(0, Location::RegisterPairLocation(
4192 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4193 locations->SetInAt(1, Location::RegisterPairLocation(
4194 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004195 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004196 break;
4197 }
4198 case Primitive::kPrimFloat:
4199 case Primitive::kPrimDouble: {
4200 locations->SetInAt(0, Location::RequiresFpuRegister());
4201 locations->SetInAt(1, Location::RequiresFpuRegister());
4202 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4203 break;
4204 }
4205
4206 default:
4207 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4208 }
4209}
4210
4211void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
4212 LocationSummary* locations = div->GetLocations();
4213 Location out = locations->Out();
4214 Location first = locations->InAt(0);
4215 Location second = locations->InAt(1);
4216
4217 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004218 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004219 if (second.IsConstant()) {
4220 GenerateDivRemConstantIntegral(div);
4221 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004222 __ sdiv(out.AsRegister<Register>(),
4223 first.AsRegister<Register>(),
4224 second.AsRegister<Register>());
4225 } else {
4226 InvokeRuntimeCallingConvention calling_convention;
4227 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4228 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4229 DCHECK_EQ(R0, out.AsRegister<Register>());
4230
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004231 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004232 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004233 }
Calin Juravled0d48522014-11-04 16:40:20 +00004234 break;
4235 }
4236
Calin Juravle7c4954d2014-10-28 16:57:40 +00004237 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004238 InvokeRuntimeCallingConvention calling_convention;
4239 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4240 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4241 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4242 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4243 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004244 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004245
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004246 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004247 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00004248 break;
4249 }
4250
4251 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004252 __ vdivs(out.AsFpuRegister<SRegister>(),
4253 first.AsFpuRegister<SRegister>(),
4254 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004255 break;
4256 }
4257
4258 case Primitive::kPrimDouble: {
4259 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4260 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4261 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
4262 break;
4263 }
4264
4265 default:
4266 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4267 }
4268}
4269
Calin Juravlebacfec32014-11-14 15:54:36 +00004270void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004271 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004272
4273 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004274 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004275 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4276 // sdiv will be replaced by other instruction sequence.
4277 call_kind = LocationSummary::kNoCall;
4278 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4279 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004280 // Have hardware divide instruction for int, do it with three instructions.
4281 call_kind = LocationSummary::kNoCall;
4282 }
4283
Calin Juravlebacfec32014-11-14 15:54:36 +00004284 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4285
Calin Juravled2ec87d2014-12-08 14:24:46 +00004286 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004287 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004288 if (rem->InputAt(1)->IsConstant()) {
4289 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004290 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004291 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004292 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
4293 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004294 // No temp register required.
4295 } else {
4296 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004297 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004298 locations->AddTemp(Location::RequiresRegister());
4299 }
4300 }
4301 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004302 locations->SetInAt(0, Location::RequiresRegister());
4303 locations->SetInAt(1, Location::RequiresRegister());
4304 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4305 locations->AddTemp(Location::RequiresRegister());
4306 } else {
4307 InvokeRuntimeCallingConvention calling_convention;
4308 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4309 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004310 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004311 // we only need the latter.
4312 locations->SetOut(Location::RegisterLocation(R1));
4313 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004314 break;
4315 }
4316 case Primitive::kPrimLong: {
4317 InvokeRuntimeCallingConvention calling_convention;
4318 locations->SetInAt(0, Location::RegisterPairLocation(
4319 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4320 locations->SetInAt(1, Location::RegisterPairLocation(
4321 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4322 // The runtime helper puts the output in R2,R3.
4323 locations->SetOut(Location::RegisterPairLocation(R2, R3));
4324 break;
4325 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00004326 case Primitive::kPrimFloat: {
4327 InvokeRuntimeCallingConvention calling_convention;
4328 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4329 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4330 locations->SetOut(Location::FpuRegisterLocation(S0));
4331 break;
4332 }
4333
Calin Juravlebacfec32014-11-14 15:54:36 +00004334 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004335 InvokeRuntimeCallingConvention calling_convention;
4336 locations->SetInAt(0, Location::FpuRegisterPairLocation(
4337 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4338 locations->SetInAt(1, Location::FpuRegisterPairLocation(
4339 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4340 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00004341 break;
4342 }
4343
4344 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004345 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004346 }
4347}
4348
4349void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
4350 LocationSummary* locations = rem->GetLocations();
4351 Location out = locations->Out();
4352 Location first = locations->InAt(0);
4353 Location second = locations->InAt(1);
4354
Calin Juravled2ec87d2014-12-08 14:24:46 +00004355 Primitive::Type type = rem->GetResultType();
4356 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004357 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004358 if (second.IsConstant()) {
4359 GenerateDivRemConstantIntegral(rem);
4360 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004361 Register reg1 = first.AsRegister<Register>();
4362 Register reg2 = second.AsRegister<Register>();
4363 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004364
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004365 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004366 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004367 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004368 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004369 } else {
4370 InvokeRuntimeCallingConvention calling_convention;
4371 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4372 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4373 DCHECK_EQ(R1, out.AsRegister<Register>());
4374
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004375 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004376 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004377 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004378 break;
4379 }
4380
4381 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004382 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004383 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004384 break;
4385 }
4386
Calin Juravled2ec87d2014-12-08 14:24:46 +00004387 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004388 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004389 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00004390 break;
4391 }
4392
Calin Juravlebacfec32014-11-14 15:54:36 +00004393 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004394 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004395 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004396 break;
4397 }
4398
4399 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004400 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004401 }
4402}
4403
Calin Juravled0d48522014-11-04 16:40:20 +00004404void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004405 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004406 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00004407}
4408
4409void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004410 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004411 codegen_->AddSlowPath(slow_path);
4412
4413 LocationSummary* locations = instruction->GetLocations();
4414 Location value = locations->InAt(0);
4415
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004416 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00004417 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004418 case Primitive::kPrimByte:
4419 case Primitive::kPrimChar:
4420 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004421 case Primitive::kPrimInt: {
4422 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004423 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004424 } else {
4425 DCHECK(value.IsConstant()) << value;
4426 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4427 __ b(slow_path->GetEntryLabel());
4428 }
4429 }
4430 break;
4431 }
4432 case Primitive::kPrimLong: {
4433 if (value.IsRegisterPair()) {
4434 __ orrs(IP,
4435 value.AsRegisterPairLow<Register>(),
4436 ShifterOperand(value.AsRegisterPairHigh<Register>()));
4437 __ b(slow_path->GetEntryLabel(), EQ);
4438 } else {
4439 DCHECK(value.IsConstant()) << value;
4440 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4441 __ b(slow_path->GetEntryLabel());
4442 }
4443 }
4444 break;
4445 default:
4446 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4447 }
4448 }
Calin Juravled0d48522014-11-04 16:40:20 +00004449}
4450
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004451void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
4452 Register in = locations->InAt(0).AsRegister<Register>();
4453 Location rhs = locations->InAt(1);
4454 Register out = locations->Out().AsRegister<Register>();
4455
4456 if (rhs.IsConstant()) {
4457 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4458 // so map all rotations to a +ve. equivalent in that range.
4459 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4460 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4461 if (rot) {
4462 // Rotate, mapping left rotations to right equivalents if necessary.
4463 // (e.g. left by 2 bits == right by 30.)
4464 __ Ror(out, in, rot);
4465 } else if (out != in) {
4466 __ Mov(out, in);
4467 }
4468 } else {
4469 __ Ror(out, in, rhs.AsRegister<Register>());
4470 }
4471}
4472
4473// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4474// rotates by swapping input regs (effectively rotating by the first 32-bits of
4475// a larger rotation) or flipping direction (thus treating larger right/left
4476// rotations as sub-word sized rotations in the other direction) as appropriate.
Anton Kirilov6f644202017-02-27 18:29:45 +00004477void InstructionCodeGeneratorARM::HandleLongRotate(HRor* ror) {
4478 LocationSummary* locations = ror->GetLocations();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004479 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
4480 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
4481 Location rhs = locations->InAt(1);
4482 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
4483 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
4484
4485 if (rhs.IsConstant()) {
4486 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4487 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00004488 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004489 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4490 // logic below to a simple pair of binary orr.
4491 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4492 if (rot >= kArmBitsPerWord) {
4493 rot -= kArmBitsPerWord;
4494 std::swap(in_reg_hi, in_reg_lo);
4495 }
4496 // Rotate, or mov to out for zero or word size rotations.
4497 if (rot != 0u) {
4498 __ Lsr(out_reg_hi, in_reg_hi, rot);
4499 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
4500 __ Lsr(out_reg_lo, in_reg_lo, rot);
4501 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
4502 } else {
4503 __ Mov(out_reg_lo, in_reg_lo);
4504 __ Mov(out_reg_hi, in_reg_hi);
4505 }
4506 } else {
4507 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
4508 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
4509 Label end;
4510 Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004511 Label* final_label = codegen_->GetFinalLabel(ror, &end);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004512
4513 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
4514 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
4515 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
4516 __ b(&shift_by_32_plus_shift_right, CC);
4517
4518 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4519 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4520 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4521 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4522 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4523 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4524 __ Lsr(shift_left, in_reg_hi, shift_right);
4525 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
Anton Kirilov6f644202017-02-27 18:29:45 +00004526 __ b(final_label);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004527
4528 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4529 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4530 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4531 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4532 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4533 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4534 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4535 __ Lsl(shift_right, in_reg_hi, shift_left);
4536 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
4537
Anton Kirilov6f644202017-02-27 18:29:45 +00004538 if (end.IsLinked()) {
4539 __ Bind(&end);
4540 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004541 }
4542}
Roland Levillain22c49222016-03-18 14:04:28 +00004543
4544void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004545 LocationSummary* locations =
4546 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4547 switch (ror->GetResultType()) {
4548 case Primitive::kPrimInt: {
4549 locations->SetInAt(0, Location::RequiresRegister());
4550 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4551 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4552 break;
4553 }
4554 case Primitive::kPrimLong: {
4555 locations->SetInAt(0, Location::RequiresRegister());
4556 if (ror->InputAt(1)->IsConstant()) {
4557 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4558 } else {
4559 locations->SetInAt(1, Location::RequiresRegister());
4560 locations->AddTemp(Location::RequiresRegister());
4561 locations->AddTemp(Location::RequiresRegister());
4562 }
4563 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4564 break;
4565 }
4566 default:
4567 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4568 }
4569}
4570
Roland Levillain22c49222016-03-18 14:04:28 +00004571void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004572 LocationSummary* locations = ror->GetLocations();
4573 Primitive::Type type = ror->GetResultType();
4574 switch (type) {
4575 case Primitive::kPrimInt: {
4576 HandleIntegerRotate(locations);
4577 break;
4578 }
4579 case Primitive::kPrimLong: {
Anton Kirilov6f644202017-02-27 18:29:45 +00004580 HandleLongRotate(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004581 break;
4582 }
4583 default:
4584 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004585 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004586 }
4587}
4588
Calin Juravle9aec02f2014-11-18 23:06:35 +00004589void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
4590 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4591
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004592 LocationSummary* locations =
4593 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004594
4595 switch (op->GetResultType()) {
4596 case Primitive::kPrimInt: {
4597 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004598 if (op->InputAt(1)->IsConstant()) {
4599 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4600 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4601 } else {
4602 locations->SetInAt(1, Location::RequiresRegister());
4603 // Make the output overlap, as it will be used to hold the masked
4604 // second input.
4605 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4606 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004607 break;
4608 }
4609 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004610 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004611 if (op->InputAt(1)->IsConstant()) {
4612 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4613 // For simplicity, use kOutputOverlap even though we only require that low registers
4614 // don't clash with high registers which the register allocator currently guarantees.
4615 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4616 } else {
4617 locations->SetInAt(1, Location::RequiresRegister());
4618 locations->AddTemp(Location::RequiresRegister());
4619 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4620 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004621 break;
4622 }
4623 default:
4624 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4625 }
4626}
4627
4628void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
4629 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4630
4631 LocationSummary* locations = op->GetLocations();
4632 Location out = locations->Out();
4633 Location first = locations->InAt(0);
4634 Location second = locations->InAt(1);
4635
4636 Primitive::Type type = op->GetResultType();
4637 switch (type) {
4638 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004639 Register out_reg = out.AsRegister<Register>();
4640 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004641 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004642 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00004643 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004644 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004645 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004646 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004647 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004648 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004649 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004650 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004651 }
4652 } else {
4653 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004654 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00004655 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00004656 __ Mov(out_reg, first_reg);
4657 } else if (op->IsShl()) {
4658 __ Lsl(out_reg, first_reg, shift_value);
4659 } else if (op->IsShr()) {
4660 __ Asr(out_reg, first_reg, shift_value);
4661 } else {
4662 __ Lsr(out_reg, first_reg, shift_value);
4663 }
4664 }
4665 break;
4666 }
4667 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004668 Register o_h = out.AsRegisterPairHigh<Register>();
4669 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004670
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004671 Register high = first.AsRegisterPairHigh<Register>();
4672 Register low = first.AsRegisterPairLow<Register>();
4673
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004674 if (second.IsRegister()) {
4675 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004676
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004677 Register second_reg = second.AsRegister<Register>();
4678
4679 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004680 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004681 // Shift the high part
4682 __ Lsl(o_h, high, o_l);
4683 // Shift the low part and `or` what overflew on the high part
4684 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
4685 __ Lsr(temp, low, temp);
4686 __ orr(o_h, o_h, ShifterOperand(temp));
4687 // If the shift is > 32 bits, override the high part
4688 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
4689 __ it(PL);
4690 __ Lsl(o_h, low, temp, PL);
4691 // Shift the low part
4692 __ Lsl(o_l, low, o_l);
4693 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004694 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004695 // Shift the low part
4696 __ Lsr(o_l, low, o_h);
4697 // Shift the high part and `or` what underflew on the low part
4698 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4699 __ Lsl(temp, high, temp);
4700 __ orr(o_l, o_l, ShifterOperand(temp));
4701 // If the shift is > 32 bits, override the low part
4702 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4703 __ it(PL);
4704 __ Asr(o_l, high, temp, PL);
4705 // Shift the high part
4706 __ Asr(o_h, high, o_h);
4707 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004708 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004709 // same as Shr except we use `Lsr`s and not `Asr`s
4710 __ Lsr(o_l, low, o_h);
4711 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4712 __ Lsl(temp, high, temp);
4713 __ orr(o_l, o_l, ShifterOperand(temp));
4714 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4715 __ it(PL);
4716 __ Lsr(o_l, high, temp, PL);
4717 __ Lsr(o_h, high, o_h);
4718 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004719 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004720 // Register allocator doesn't create partial overlap.
4721 DCHECK_NE(o_l, high);
4722 DCHECK_NE(o_h, low);
4723 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004724 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004725 if (shift_value > 32) {
4726 if (op->IsShl()) {
4727 __ Lsl(o_h, low, shift_value - 32);
4728 __ LoadImmediate(o_l, 0);
4729 } else if (op->IsShr()) {
4730 __ Asr(o_l, high, shift_value - 32);
4731 __ Asr(o_h, high, 31);
4732 } else {
4733 __ Lsr(o_l, high, shift_value - 32);
4734 __ LoadImmediate(o_h, 0);
4735 }
4736 } else if (shift_value == 32) {
4737 if (op->IsShl()) {
4738 __ mov(o_h, ShifterOperand(low));
4739 __ LoadImmediate(o_l, 0);
4740 } else if (op->IsShr()) {
4741 __ mov(o_l, ShifterOperand(high));
4742 __ Asr(o_h, high, 31);
4743 } else {
4744 __ mov(o_l, ShifterOperand(high));
4745 __ LoadImmediate(o_h, 0);
4746 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00004747 } else if (shift_value == 1) {
4748 if (op->IsShl()) {
4749 __ Lsls(o_l, low, 1);
4750 __ adc(o_h, high, ShifterOperand(high));
4751 } else if (op->IsShr()) {
4752 __ Asrs(o_h, high, 1);
4753 __ Rrx(o_l, low);
4754 } else {
4755 __ Lsrs(o_h, high, 1);
4756 __ Rrx(o_l, low);
4757 }
4758 } else {
4759 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004760 if (op->IsShl()) {
4761 __ Lsl(o_h, high, shift_value);
4762 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
4763 __ Lsl(o_l, low, shift_value);
4764 } else if (op->IsShr()) {
4765 __ Lsr(o_l, low, shift_value);
4766 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4767 __ Asr(o_h, high, shift_value);
4768 } else {
4769 __ Lsr(o_l, low, shift_value);
4770 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4771 __ Lsr(o_h, high, shift_value);
4772 }
4773 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004774 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004775 break;
4776 }
4777 default:
4778 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004779 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004780 }
4781}
4782
4783void LocationsBuilderARM::VisitShl(HShl* shl) {
4784 HandleShift(shl);
4785}
4786
4787void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
4788 HandleShift(shl);
4789}
4790
4791void LocationsBuilderARM::VisitShr(HShr* shr) {
4792 HandleShift(shr);
4793}
4794
4795void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
4796 HandleShift(shr);
4797}
4798
4799void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
4800 HandleShift(ushr);
4801}
4802
4803void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
4804 HandleShift(ushr);
4805}
4806
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004807void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004808 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004809 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00004810 if (instruction->IsStringAlloc()) {
4811 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4812 } else {
4813 InvokeRuntimeCallingConvention calling_convention;
4814 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004815 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004816 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004817}
4818
4819void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004820 // Note: if heap poisoning is enabled, the entry point takes cares
4821 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004822 if (instruction->IsStringAlloc()) {
4823 // String is allocated through StringFactory. Call NewEmptyString entry point.
4824 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004825 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004826 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4827 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
4828 __ blx(LR);
4829 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4830 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004831 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004832 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004833 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004834}
4835
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004836void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
4837 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004838 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004839 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004840 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004841 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4842 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004843}
4844
4845void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004846 // Note: if heap poisoning is enabled, the entry point takes cares
4847 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004848 QuickEntrypointEnum entrypoint =
4849 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4850 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004851 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004852 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004853}
4854
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004855void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004856 LocationSummary* locations =
4857 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004858 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4859 if (location.IsStackSlot()) {
4860 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4861 } else if (location.IsDoubleStackSlot()) {
4862 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004863 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004864 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004865}
4866
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004867void InstructionCodeGeneratorARM::VisitParameterValue(
4868 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004869 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004870}
4871
4872void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4873 LocationSummary* locations =
4874 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4875 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4876}
4877
4878void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4879 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004880}
4881
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004882void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004883 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004884 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004885 locations->SetInAt(0, Location::RequiresRegister());
4886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004887}
4888
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004889void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4890 LocationSummary* locations = not_->GetLocations();
4891 Location out = locations->Out();
4892 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004893 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004894 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004895 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004896 break;
4897
4898 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004899 __ mvn(out.AsRegisterPairLow<Register>(),
4900 ShifterOperand(in.AsRegisterPairLow<Register>()));
4901 __ mvn(out.AsRegisterPairHigh<Register>(),
4902 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004903 break;
4904
4905 default:
4906 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4907 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004908}
4909
David Brazdil66d126e2015-04-03 16:02:44 +01004910void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4911 LocationSummary* locations =
4912 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4913 locations->SetInAt(0, Location::RequiresRegister());
4914 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4915}
4916
4917void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004918 LocationSummary* locations = bool_not->GetLocations();
4919 Location out = locations->Out();
4920 Location in = locations->InAt(0);
4921 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4922}
4923
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004924void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004925 LocationSummary* locations =
4926 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004927 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004928 case Primitive::kPrimBoolean:
4929 case Primitive::kPrimByte:
4930 case Primitive::kPrimShort:
4931 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004932 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004933 case Primitive::kPrimLong: {
4934 locations->SetInAt(0, Location::RequiresRegister());
4935 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004936 // Output overlaps because it is written before doing the low comparison.
4937 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004938 break;
4939 }
4940 case Primitive::kPrimFloat:
4941 case Primitive::kPrimDouble: {
4942 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004943 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004944 locations->SetOut(Location::RequiresRegister());
4945 break;
4946 }
4947 default:
4948 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4949 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004950}
4951
4952void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004953 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004954 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004955 Location left = locations->InAt(0);
4956 Location right = locations->InAt(1);
4957
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004958 Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00004959 Label* final_label = codegen_->GetFinalLabel(compare, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00004960 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004961 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004962 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004963 case Primitive::kPrimBoolean:
4964 case Primitive::kPrimByte:
4965 case Primitive::kPrimShort:
4966 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004967 case Primitive::kPrimInt: {
4968 __ LoadImmediate(out, 0);
4969 __ cmp(left.AsRegister<Register>(),
4970 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4971 less_cond = LT;
4972 break;
4973 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004974 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004975 __ cmp(left.AsRegisterPairHigh<Register>(),
4976 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004977 __ b(&less, LT);
4978 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004979 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004980 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004981 __ cmp(left.AsRegisterPairLow<Register>(),
4982 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004983 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004984 break;
4985 }
4986 case Primitive::kPrimFloat:
4987 case Primitive::kPrimDouble: {
4988 __ LoadImmediate(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08004989 GenerateVcmp(compare, codegen_);
Calin Juravleddb7df22014-11-25 20:56:51 +00004990 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004991 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004992 break;
4993 }
4994 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004995 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004996 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004997 }
Aart Bika19616e2016-02-01 18:57:58 -08004998
Anton Kirilov6f644202017-02-27 18:29:45 +00004999 __ b(final_label, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00005000 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00005001
5002 __ Bind(&greater);
5003 __ LoadImmediate(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005004 __ b(final_label);
Calin Juravleddb7df22014-11-25 20:56:51 +00005005
5006 __ Bind(&less);
5007 __ LoadImmediate(out, -1);
5008
Anton Kirilov6f644202017-02-27 18:29:45 +00005009 if (done.IsLinked()) {
5010 __ Bind(&done);
5011 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005012}
5013
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005014void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005015 LocationSummary* locations =
5016 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005017 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005018 locations->SetInAt(i, Location::Any());
5019 }
5020 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005021}
5022
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005023void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005024 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005025}
5026
Roland Levillainc9285912015-12-18 10:38:42 +00005027void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
5028 // TODO (ported from quick): revisit ARM barrier kinds.
5029 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00005030 switch (kind) {
5031 case MemBarrierKind::kAnyStore:
5032 case MemBarrierKind::kLoadAny:
5033 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005034 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00005035 break;
5036 }
5037 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005038 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00005039 break;
5040 }
5041 default:
5042 LOG(FATAL) << "Unexpected memory barrier " << kind;
5043 }
Kenny Root1d8199d2015-06-02 11:01:10 -07005044 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00005045}
5046
5047void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
5048 uint32_t offset,
5049 Register out_lo,
5050 Register out_hi) {
5051 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005052 // Ensure `out_lo` is different from `addr`, so that loading
5053 // `offset` into `out_lo` does not clutter `addr`.
5054 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00005055 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005056 __ add(IP, addr, ShifterOperand(out_lo));
5057 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005058 }
5059 __ ldrexd(out_lo, out_hi, addr);
5060}
5061
5062void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
5063 uint32_t offset,
5064 Register value_lo,
5065 Register value_hi,
5066 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00005067 Register temp2,
5068 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005069 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00005070 if (offset != 0) {
5071 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005072 __ add(IP, addr, ShifterOperand(temp1));
5073 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005074 }
5075 __ Bind(&fail);
5076 // We need a load followed by store. (The address used in a STREX instruction must
5077 // be the same as the address in the most recently executed LDREX instruction.)
5078 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00005079 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005080 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005081 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00005082}
5083
5084void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5085 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5086
Nicolas Geoffray39468442014-09-02 15:17:15 +01005087 LocationSummary* locations =
5088 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005089 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005090
Calin Juravle52c48962014-12-16 17:02:57 +00005091 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005092 if (Primitive::IsFloatingPointType(field_type)) {
5093 locations->SetInAt(1, Location::RequiresFpuRegister());
5094 } else {
5095 locations->SetInAt(1, Location::RequiresRegister());
5096 }
5097
Calin Juravle52c48962014-12-16 17:02:57 +00005098 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00005099 bool generate_volatile = field_info.IsVolatile()
5100 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005101 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01005102 bool needs_write_barrier =
5103 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005104 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00005105 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01005106 if (needs_write_barrier) {
5107 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005108 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005109 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005110 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005111 // - registers need to be consecutive
5112 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005113 // We don't test for ARM yet, and the assertion makes sure that we
5114 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005115 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5116
5117 locations->AddTemp(Location::RequiresRegister());
5118 locations->AddTemp(Location::RequiresRegister());
5119 if (field_type == Primitive::kPrimDouble) {
5120 // For doubles we need two more registers to copy the value.
5121 locations->AddTemp(Location::RegisterLocation(R2));
5122 locations->AddTemp(Location::RegisterLocation(R3));
5123 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005124 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005125}
5126
Calin Juravle52c48962014-12-16 17:02:57 +00005127void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005128 const FieldInfo& field_info,
5129 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005130 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5131
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005132 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00005133 Register base = locations->InAt(0).AsRegister<Register>();
5134 Location value = locations->InAt(1);
5135
5136 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005137 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005138 Primitive::Type field_type = field_info.GetFieldType();
5139 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005140 bool needs_write_barrier =
5141 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005142
5143 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005144 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005145 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005146
5147 switch (field_type) {
5148 case Primitive::kPrimBoolean:
5149 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00005150 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005151 break;
5152 }
5153
5154 case Primitive::kPrimShort:
5155 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00005156 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005157 break;
5158 }
5159
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005160 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005161 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01005162 if (kPoisonHeapReferences && needs_write_barrier) {
5163 // Note that in the case where `value` is a null reference,
5164 // we do not enter this block, as a null reference does not
5165 // need poisoning.
5166 DCHECK_EQ(field_type, Primitive::kPrimNot);
5167 Register temp = locations->GetTemp(0).AsRegister<Register>();
5168 __ Mov(temp, value.AsRegister<Register>());
5169 __ PoisonHeapReference(temp);
5170 __ StoreToOffset(kStoreWord, temp, base, offset);
5171 } else {
5172 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
5173 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005174 break;
5175 }
5176
5177 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00005178 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005179 GenerateWideAtomicStore(base, offset,
5180 value.AsRegisterPairLow<Register>(),
5181 value.AsRegisterPairHigh<Register>(),
5182 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005183 locations->GetTemp(1).AsRegister<Register>(),
5184 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005185 } else {
5186 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005187 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005188 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005189 break;
5190 }
5191
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005192 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00005193 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005194 break;
5195 }
5196
5197 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005198 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005199 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005200 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
5201 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
5202
5203 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
5204
5205 GenerateWideAtomicStore(base, offset,
5206 value_reg_lo,
5207 value_reg_hi,
5208 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005209 locations->GetTemp(3).AsRegister<Register>(),
5210 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005211 } else {
5212 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005213 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005214 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005215 break;
5216 }
5217
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005218 case Primitive::kPrimVoid:
5219 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005220 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005221 }
Calin Juravle52c48962014-12-16 17:02:57 +00005222
Calin Juravle77520bc2015-01-12 18:45:46 +00005223 // Longs and doubles are handled in the switch.
5224 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
5225 codegen_->MaybeRecordImplicitNullCheck(instruction);
5226 }
5227
5228 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5229 Register temp = locations->GetTemp(0).AsRegister<Register>();
5230 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005231 codegen_->MarkGCCard(
5232 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005233 }
5234
Calin Juravle52c48962014-12-16 17:02:57 +00005235 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005236 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005237 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005238}
5239
Calin Juravle52c48962014-12-16 17:02:57 +00005240void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5241 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00005242
5243 bool object_field_get_with_read_barrier =
5244 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005245 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005246 new (GetGraph()->GetArena()) LocationSummary(instruction,
5247 object_field_get_with_read_barrier ?
5248 LocationSummary::kCallOnSlowPath :
5249 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005250 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005251 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005252 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005253 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00005254
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005255 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00005256 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005257 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00005258 // The output overlaps in case of volatile long: we don't want the
5259 // code generated by GenerateWideAtomicLoad to overwrite the
5260 // object's location. Likewise, in the case of an object field get
5261 // with read barriers enabled, we do not want the load to overwrite
5262 // the object's location, as we need it to emit the read barrier.
5263 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5264 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01005265
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005266 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5267 locations->SetOut(Location::RequiresFpuRegister());
5268 } else {
5269 locations->SetOut(Location::RequiresRegister(),
5270 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5271 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005272 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00005273 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005274 // - registers need to be consecutive
5275 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005276 // We don't test for ARM yet, and the assertion makes sure that we
5277 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005278 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5279 locations->AddTemp(Location::RequiresRegister());
5280 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00005281 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5282 // We need a temporary register for the read barrier marking slow
5283 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
5284 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00005285 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005286}
5287
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005288Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5289 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
5290 << input->GetType();
5291 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5292 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5293 return Location::ConstantLocation(input->AsConstant());
5294 } else {
5295 return Location::RequiresFpuRegister();
5296 }
5297}
5298
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005299Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
5300 Opcode opcode) {
5301 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5302 if (constant->IsConstant() &&
5303 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5304 return Location::ConstantLocation(constant->AsConstant());
5305 }
5306 return Location::RequiresRegister();
5307}
5308
5309bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
5310 Opcode opcode) {
5311 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5312 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01005313 Opcode high_opcode = opcode;
5314 SetCc low_set_cc = kCcDontCare;
5315 switch (opcode) {
5316 case SUB:
5317 // Flip the operation to an ADD.
5318 value = -value;
5319 opcode = ADD;
5320 FALLTHROUGH_INTENDED;
5321 case ADD:
5322 if (Low32Bits(value) == 0u) {
5323 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5324 }
5325 high_opcode = ADC;
5326 low_set_cc = kCcSet;
5327 break;
5328 default:
5329 break;
5330 }
5331 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5332 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005333 } else {
5334 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5335 }
5336}
5337
Vladimir Marko59751a72016-08-05 14:37:27 +01005338bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
5339 Opcode opcode,
5340 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005341 ShifterOperand so;
5342 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01005343 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005344 return true;
5345 }
5346 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005347 uint32_t neg_value = 0;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005348 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005349 case AND: neg_opcode = BIC; neg_value = ~value; break;
5350 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5351 case ADD: neg_opcode = SUB; neg_value = -value; break;
5352 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5353 case SUB: neg_opcode = ADD; neg_value = -value; break;
5354 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5355 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005356 default:
5357 return false;
5358 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005359
5360 if (assembler->ShifterOperandCanHold(kNoRegister,
5361 kNoRegister,
5362 neg_opcode,
5363 neg_value,
5364 set_cc,
5365 &so)) {
5366 return true;
5367 }
5368
5369 return opcode == AND && IsPowerOfTwo(value + 1);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005370}
5371
Calin Juravle52c48962014-12-16 17:02:57 +00005372void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
5373 const FieldInfo& field_info) {
5374 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005375
Calin Juravle52c48962014-12-16 17:02:57 +00005376 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005377 Location base_loc = locations->InAt(0);
5378 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005379 Location out = locations->Out();
5380 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005381 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005382 Primitive::Type field_type = field_info.GetFieldType();
5383 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5384
5385 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00005386 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00005387 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005388 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005389
Roland Levillainc9285912015-12-18 10:38:42 +00005390 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00005391 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005392 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005393
Roland Levillainc9285912015-12-18 10:38:42 +00005394 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00005395 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005396 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005397
Roland Levillainc9285912015-12-18 10:38:42 +00005398 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00005399 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005400 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005401
5402 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00005403 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005404 break;
Roland Levillainc9285912015-12-18 10:38:42 +00005405
5406 case Primitive::kPrimNot: {
5407 // /* HeapReference<Object> */ out = *(base + offset)
5408 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5409 Location temp_loc = locations->GetTemp(0);
5410 // Note that a potential implicit null check is handled in this
5411 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
5412 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5413 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5414 if (is_volatile) {
5415 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5416 }
5417 } else {
5418 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
5419 codegen_->MaybeRecordImplicitNullCheck(instruction);
5420 if (is_volatile) {
5421 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5422 }
5423 // If read barriers are enabled, emit read barriers other than
5424 // Baker's using a slow path (and also unpoison the loaded
5425 // reference, if heap poisoning is enabled).
5426 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5427 }
5428 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005429 }
5430
Roland Levillainc9285912015-12-18 10:38:42 +00005431 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00005432 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005433 GenerateWideAtomicLoad(base, offset,
5434 out.AsRegisterPairLow<Register>(),
5435 out.AsRegisterPairHigh<Register>());
5436 } else {
5437 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
5438 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005439 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005440
Roland Levillainc9285912015-12-18 10:38:42 +00005441 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00005442 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005443 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005444
5445 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005446 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005447 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005448 Register lo = locations->GetTemp(0).AsRegister<Register>();
5449 Register hi = locations->GetTemp(1).AsRegister<Register>();
5450 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00005451 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005452 __ vmovdrr(out_reg, lo, hi);
5453 } else {
5454 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005455 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005456 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005457 break;
5458 }
5459
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005460 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005461 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005462 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005463 }
Calin Juravle52c48962014-12-16 17:02:57 +00005464
Roland Levillainc9285912015-12-18 10:38:42 +00005465 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5466 // Potential implicit null checks, in the case of reference or
5467 // double fields, are handled in the previous switch statement.
5468 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005469 codegen_->MaybeRecordImplicitNullCheck(instruction);
5470 }
5471
Calin Juravle52c48962014-12-16 17:02:57 +00005472 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005473 if (field_type == Primitive::kPrimNot) {
5474 // Memory barriers, in the case of references, are also handled
5475 // in the previous switch statement.
5476 } else {
5477 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5478 }
Roland Levillain4d027112015-07-01 15:41:14 +01005479 }
Calin Juravle52c48962014-12-16 17:02:57 +00005480}
5481
5482void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5483 HandleFieldSet(instruction, instruction->GetFieldInfo());
5484}
5485
5486void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005487 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005488}
5489
5490void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5491 HandleFieldGet(instruction, instruction->GetFieldInfo());
5492}
5493
5494void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5495 HandleFieldGet(instruction, instruction->GetFieldInfo());
5496}
5497
5498void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5499 HandleFieldGet(instruction, instruction->GetFieldInfo());
5500}
5501
5502void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5503 HandleFieldGet(instruction, instruction->GetFieldInfo());
5504}
5505
5506void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5507 HandleFieldSet(instruction, instruction->GetFieldInfo());
5508}
5509
5510void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005511 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005512}
5513
Calin Juravlee460d1d2015-09-29 04:52:17 +01005514void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
5515 HUnresolvedInstanceFieldGet* instruction) {
5516 FieldAccessCallingConventionARM calling_convention;
5517 codegen_->CreateUnresolvedFieldLocationSummary(
5518 instruction, instruction->GetFieldType(), calling_convention);
5519}
5520
5521void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
5522 HUnresolvedInstanceFieldGet* instruction) {
5523 FieldAccessCallingConventionARM calling_convention;
5524 codegen_->GenerateUnresolvedFieldAccess(instruction,
5525 instruction->GetFieldType(),
5526 instruction->GetFieldIndex(),
5527 instruction->GetDexPc(),
5528 calling_convention);
5529}
5530
5531void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
5532 HUnresolvedInstanceFieldSet* instruction) {
5533 FieldAccessCallingConventionARM calling_convention;
5534 codegen_->CreateUnresolvedFieldLocationSummary(
5535 instruction, instruction->GetFieldType(), calling_convention);
5536}
5537
5538void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
5539 HUnresolvedInstanceFieldSet* instruction) {
5540 FieldAccessCallingConventionARM calling_convention;
5541 codegen_->GenerateUnresolvedFieldAccess(instruction,
5542 instruction->GetFieldType(),
5543 instruction->GetFieldIndex(),
5544 instruction->GetDexPc(),
5545 calling_convention);
5546}
5547
5548void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
5549 HUnresolvedStaticFieldGet* instruction) {
5550 FieldAccessCallingConventionARM calling_convention;
5551 codegen_->CreateUnresolvedFieldLocationSummary(
5552 instruction, instruction->GetFieldType(), calling_convention);
5553}
5554
5555void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
5556 HUnresolvedStaticFieldGet* instruction) {
5557 FieldAccessCallingConventionARM calling_convention;
5558 codegen_->GenerateUnresolvedFieldAccess(instruction,
5559 instruction->GetFieldType(),
5560 instruction->GetFieldIndex(),
5561 instruction->GetDexPc(),
5562 calling_convention);
5563}
5564
5565void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
5566 HUnresolvedStaticFieldSet* instruction) {
5567 FieldAccessCallingConventionARM calling_convention;
5568 codegen_->CreateUnresolvedFieldLocationSummary(
5569 instruction, instruction->GetFieldType(), calling_convention);
5570}
5571
5572void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
5573 HUnresolvedStaticFieldSet* instruction) {
5574 FieldAccessCallingConventionARM calling_convention;
5575 codegen_->GenerateUnresolvedFieldAccess(instruction,
5576 instruction->GetFieldType(),
5577 instruction->GetFieldIndex(),
5578 instruction->GetDexPc(),
5579 calling_convention);
5580}
5581
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005582void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005583 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5584 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005585}
5586
Calin Juravle2ae48182016-03-16 14:05:09 +00005587void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
5588 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005589 return;
5590 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005591 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005592
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005593 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005594 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005595}
5596
Calin Juravle2ae48182016-03-16 14:05:09 +00005597void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01005598 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005599 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005600
5601 LocationSummary* locations = instruction->GetLocations();
5602 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005603
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005604 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005605}
5606
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005607void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005608 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005609}
5610
Artem Serov6c916792016-07-11 14:02:34 +01005611static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5612 switch (type) {
5613 case Primitive::kPrimNot:
5614 return kLoadWord;
5615 case Primitive::kPrimBoolean:
5616 return kLoadUnsignedByte;
5617 case Primitive::kPrimByte:
5618 return kLoadSignedByte;
5619 case Primitive::kPrimChar:
5620 return kLoadUnsignedHalfword;
5621 case Primitive::kPrimShort:
5622 return kLoadSignedHalfword;
5623 case Primitive::kPrimInt:
5624 return kLoadWord;
5625 case Primitive::kPrimLong:
5626 return kLoadWordPair;
5627 case Primitive::kPrimFloat:
5628 return kLoadSWord;
5629 case Primitive::kPrimDouble:
5630 return kLoadDWord;
5631 default:
5632 LOG(FATAL) << "Unreachable type " << type;
5633 UNREACHABLE();
5634 }
5635}
5636
5637static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5638 switch (type) {
5639 case Primitive::kPrimNot:
5640 return kStoreWord;
5641 case Primitive::kPrimBoolean:
5642 case Primitive::kPrimByte:
5643 return kStoreByte;
5644 case Primitive::kPrimChar:
5645 case Primitive::kPrimShort:
5646 return kStoreHalfword;
5647 case Primitive::kPrimInt:
5648 return kStoreWord;
5649 case Primitive::kPrimLong:
5650 return kStoreWordPair;
5651 case Primitive::kPrimFloat:
5652 return kStoreSWord;
5653 case Primitive::kPrimDouble:
5654 return kStoreDWord;
5655 default:
5656 LOG(FATAL) << "Unreachable type " << type;
5657 UNREACHABLE();
5658 }
5659}
5660
5661void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
5662 Location out_loc,
5663 Register base,
5664 Register reg_offset,
5665 Condition cond) {
5666 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5667 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5668
5669 switch (type) {
5670 case Primitive::kPrimByte:
5671 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
5672 break;
5673 case Primitive::kPrimBoolean:
5674 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
5675 break;
5676 case Primitive::kPrimShort:
5677 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
5678 break;
5679 case Primitive::kPrimChar:
5680 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
5681 break;
5682 case Primitive::kPrimNot:
5683 case Primitive::kPrimInt:
5684 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
5685 break;
5686 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5687 case Primitive::kPrimLong:
5688 case Primitive::kPrimFloat:
5689 case Primitive::kPrimDouble:
5690 default:
5691 LOG(FATAL) << "Unreachable type " << type;
5692 UNREACHABLE();
5693 }
5694}
5695
5696void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
5697 Location loc,
5698 Register base,
5699 Register reg_offset,
5700 Condition cond) {
5701 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5702 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5703
5704 switch (type) {
5705 case Primitive::kPrimByte:
5706 case Primitive::kPrimBoolean:
5707 __ strb(loc.AsRegister<Register>(), mem_address, cond);
5708 break;
5709 case Primitive::kPrimShort:
5710 case Primitive::kPrimChar:
5711 __ strh(loc.AsRegister<Register>(), mem_address, cond);
5712 break;
5713 case Primitive::kPrimNot:
5714 case Primitive::kPrimInt:
5715 __ str(loc.AsRegister<Register>(), mem_address, cond);
5716 break;
5717 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5718 case Primitive::kPrimLong:
5719 case Primitive::kPrimFloat:
5720 case Primitive::kPrimDouble:
5721 default:
5722 LOG(FATAL) << "Unreachable type " << type;
5723 UNREACHABLE();
5724 }
5725}
5726
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005727void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005728 bool object_array_get_with_read_barrier =
5729 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005730 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005731 new (GetGraph()->GetArena()) LocationSummary(instruction,
5732 object_array_get_with_read_barrier ?
5733 LocationSummary::kCallOnSlowPath :
5734 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005735 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005736 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005737 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005738 locations->SetInAt(0, Location::RequiresRegister());
5739 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005740 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5741 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5742 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005743 // The output overlaps in the case of an object array get with
5744 // read barriers enabled: we do not want the move to overwrite the
5745 // array's location, as we need it to emit the read barrier.
5746 locations->SetOut(
5747 Location::RequiresRegister(),
5748 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005749 }
Roland Levillainc9285912015-12-18 10:38:42 +00005750 // We need a temporary register for the read barrier marking slow
5751 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07005752 // Also need for String compression feature.
5753 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
5754 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00005755 locations->AddTemp(Location::RequiresRegister());
5756 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005757}
5758
5759void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
5760 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005761 Location obj_loc = locations->InAt(0);
5762 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005763 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00005764 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005765 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00005766 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07005767 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5768 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01005769 HInstruction* array_instr = instruction->GetArray();
5770 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01005771
Roland Levillain4d027112015-07-01 15:41:14 +01005772 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01005773 case Primitive::kPrimBoolean:
5774 case Primitive::kPrimByte:
5775 case Primitive::kPrimShort:
5776 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00005777 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005778 Register length;
5779 if (maybe_compressed_char_at) {
5780 length = locations->GetTemp(0).AsRegister<Register>();
5781 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5782 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
5783 codegen_->MaybeRecordImplicitNullCheck(instruction);
5784 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005785 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005786 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07005787 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07005788 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005789 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005790 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5791 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5792 "Expecting 0=compressed, 1=uncompressed");
5793 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005794 __ LoadFromOffset(kLoadUnsignedByte,
5795 out_loc.AsRegister<Register>(),
5796 obj,
5797 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00005798 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07005799 __ Bind(&uncompressed_load);
5800 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5801 out_loc.AsRegister<Register>(),
5802 obj,
5803 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005804 if (done.IsLinked()) {
5805 __ Bind(&done);
5806 }
jessicahandojo05765752016-09-09 19:01:32 -07005807 } else {
5808 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01005809
jessicahandojo05765752016-09-09 19:01:32 -07005810 LoadOperandType load_type = GetLoadOperandType(type);
5811 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
5812 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005813 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005814 Register temp = IP;
5815
5816 if (has_intermediate_address) {
5817 // We do not need to compute the intermediate address from the array: the
5818 // input instruction has done it already. See the comment in
5819 // `TryExtractArrayAccessAddress()`.
5820 if (kIsDebugBuild) {
5821 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5822 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5823 }
5824 temp = obj;
5825 } else {
5826 __ add(temp, obj, ShifterOperand(data_offset));
5827 }
jessicahandojo05765752016-09-09 19:01:32 -07005828 if (maybe_compressed_char_at) {
5829 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005830 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005831 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5832 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5833 "Expecting 0=compressed, 1=uncompressed");
5834 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005835 __ ldrb(out_loc.AsRegister<Register>(),
5836 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00005837 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07005838 __ Bind(&uncompressed_load);
5839 __ ldrh(out_loc.AsRegister<Register>(),
5840 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005841 if (done.IsLinked()) {
5842 __ Bind(&done);
5843 }
jessicahandojo05765752016-09-09 19:01:32 -07005844 } else {
5845 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
5846 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005847 }
5848 break;
5849 }
5850
Roland Levillainc9285912015-12-18 10:38:42 +00005851 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00005852 // The read barrier instrumentation of object ArrayGet
5853 // instructions does not support the HIntermediateAddress
5854 // instruction.
5855 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5856
Roland Levillainc9285912015-12-18 10:38:42 +00005857 static_assert(
5858 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5859 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00005860 // /* HeapReference<Object> */ out =
5861 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5862 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5863 Location temp = locations->GetTemp(0);
5864 // Note that a potential implicit null check is handled in this
5865 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
5866 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5867 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5868 } else {
5869 Register out = out_loc.AsRegister<Register>();
5870 if (index.IsConstant()) {
5871 size_t offset =
5872 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5873 __ LoadFromOffset(kLoadWord, out, obj, offset);
5874 codegen_->MaybeRecordImplicitNullCheck(instruction);
5875 // If read barriers are enabled, emit read barriers other than
5876 // Baker's using a slow path (and also unpoison the loaded
5877 // reference, if heap poisoning is enabled).
5878 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5879 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005880 Register temp = IP;
5881
5882 if (has_intermediate_address) {
5883 // We do not need to compute the intermediate address from the array: the
5884 // input instruction has done it already. See the comment in
5885 // `TryExtractArrayAccessAddress()`.
5886 if (kIsDebugBuild) {
5887 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5888 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5889 }
5890 temp = obj;
5891 } else {
5892 __ add(temp, obj, ShifterOperand(data_offset));
5893 }
5894 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005895
Roland Levillainc9285912015-12-18 10:38:42 +00005896 codegen_->MaybeRecordImplicitNullCheck(instruction);
5897 // If read barriers are enabled, emit read barriers other than
5898 // Baker's using a slow path (and also unpoison the loaded
5899 // reference, if heap poisoning is enabled).
5900 codegen_->MaybeGenerateReadBarrierSlow(
5901 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5902 }
5903 }
5904 break;
5905 }
5906
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005907 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005908 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005909 size_t offset =
5910 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005911 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005912 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005913 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005914 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005915 }
5916 break;
5917 }
5918
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005919 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005920 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005921 if (index.IsConstant()) {
5922 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005923 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005924 } else {
5925 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005926 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005927 }
5928 break;
5929 }
5930
5931 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005932 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005933 if (index.IsConstant()) {
5934 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005935 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005936 } else {
5937 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005938 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005939 }
5940 break;
5941 }
5942
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005943 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005944 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005945 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005946 }
Roland Levillain4d027112015-07-01 15:41:14 +01005947
5948 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005949 // Potential implicit null checks, in the case of reference
5950 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005951 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005952 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005953 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005954}
5955
5956void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005957 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005958
5959 bool needs_write_barrier =
5960 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005961 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005962
Nicolas Geoffray39468442014-09-02 15:17:15 +01005963 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005964 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005965 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005966 LocationSummary::kCallOnSlowPath :
5967 LocationSummary::kNoCall);
5968
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005969 locations->SetInAt(0, Location::RequiresRegister());
5970 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5971 if (Primitive::IsFloatingPointType(value_type)) {
5972 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005973 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005974 locations->SetInAt(2, Location::RequiresRegister());
5975 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005976 if (needs_write_barrier) {
5977 // Temporary registers for the write barrier.
5978 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005979 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005980 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005981}
5982
5983void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5984 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005985 Location array_loc = locations->InAt(0);
5986 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005987 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005988 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005989 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005990 bool needs_write_barrier =
5991 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005992 uint32_t data_offset =
5993 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5994 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005995 HInstruction* array_instr = instruction->GetArray();
5996 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005997
5998 switch (value_type) {
5999 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01006000 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006001 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01006002 case Primitive::kPrimChar:
6003 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006004 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01006005 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
6006 uint32_t full_offset =
6007 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6008 StoreOperandType store_type = GetStoreOperandType(value_type);
6009 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006010 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006011 Register temp = IP;
6012
6013 if (has_intermediate_address) {
6014 // We do not need to compute the intermediate address from the array: the
6015 // input instruction has done it already. See the comment in
6016 // `TryExtractArrayAccessAddress()`.
6017 if (kIsDebugBuild) {
6018 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6019 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
6020 }
6021 temp = array;
6022 } else {
6023 __ add(temp, array, ShifterOperand(data_offset));
6024 }
Artem Serov6c916792016-07-11 14:02:34 +01006025 codegen_->StoreToShiftedRegOffset(value_type,
6026 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01006027 temp,
Artem Serov6c916792016-07-11 14:02:34 +01006028 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006029 }
6030 break;
6031 }
6032
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006033 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00006034 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01006035 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6036 // See the comment in instruction_simplifier_shared.cc.
6037 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006038
6039 if (instruction->InputAt(2)->IsNullConstant()) {
6040 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006041 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006042 size_t offset =
6043 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01006044 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006045 } else {
6046 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006047 __ add(IP, array, ShifterOperand(data_offset));
6048 codegen_->StoreToShiftedRegOffset(value_type,
6049 value_loc,
6050 IP,
6051 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006052 }
Roland Levillain1407ee72016-01-08 15:56:19 +00006053 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00006054 DCHECK(!needs_write_barrier);
6055 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006056 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006057 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006058
6059 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01006060 Location temp1_loc = locations->GetTemp(0);
6061 Register temp1 = temp1_loc.AsRegister<Register>();
6062 Location temp2_loc = locations->GetTemp(1);
6063 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006064 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6065 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6066 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6067 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006068 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006069 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006070
Roland Levillain3b359c72015-11-17 19:35:12 +00006071 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006072 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
6073 codegen_->AddSlowPath(slow_path);
6074 if (instruction->GetValueCanBeNull()) {
6075 Label non_zero;
6076 __ CompareAndBranchIfNonZero(value, &non_zero);
6077 if (index.IsConstant()) {
6078 size_t offset =
6079 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6080 __ StoreToOffset(kStoreWord, value, array, offset);
6081 } else {
6082 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006083 __ add(IP, array, ShifterOperand(data_offset));
6084 codegen_->StoreToShiftedRegOffset(value_type,
6085 value_loc,
6086 IP,
6087 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006088 }
6089 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006090 __ b(final_label);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006091 __ Bind(&non_zero);
6092 }
6093
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006094 // Note that when read barriers are enabled, the type checks
6095 // are performed without read barriers. This is fine, even in
6096 // the case where a class object is in the from-space after
6097 // the flip, as a comparison involving such a type would not
6098 // produce a false positive; it may of course produce a false
6099 // negative, in which case we would take the ArraySet slow
6100 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006101
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006102 // /* HeapReference<Class> */ temp1 = array->klass_
6103 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
6104 codegen_->MaybeRecordImplicitNullCheck(instruction);
6105 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01006106
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006107 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6108 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6109 // /* HeapReference<Class> */ temp2 = value->klass_
6110 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
6111 // If heap poisoning is enabled, no need to unpoison `temp1`
6112 // nor `temp2`, as we are comparing two poisoned references.
6113 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01006114
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006115 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6116 Label do_put;
6117 __ b(&do_put, EQ);
6118 // If heap poisoning is enabled, the `temp1` reference has
6119 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00006120 __ MaybeUnpoisonHeapReference(temp1);
6121
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006122 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6123 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6124 // If heap poisoning is enabled, no need to unpoison
6125 // `temp1`, as we are comparing against null below.
6126 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
6127 __ Bind(&do_put);
6128 } else {
6129 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006130 }
6131 }
6132
Artem Serov6c916792016-07-11 14:02:34 +01006133 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006134 if (kPoisonHeapReferences) {
6135 // Note that in the case where `value` is a null reference,
6136 // we do not enter this block, as a null reference does not
6137 // need poisoning.
6138 DCHECK_EQ(value_type, Primitive::kPrimNot);
6139 __ Mov(temp1, value);
6140 __ PoisonHeapReference(temp1);
6141 source = temp1;
6142 }
6143
6144 if (index.IsConstant()) {
6145 size_t offset =
6146 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6147 __ StoreToOffset(kStoreWord, source, array, offset);
6148 } else {
6149 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006150
6151 __ add(IP, array, ShifterOperand(data_offset));
6152 codegen_->StoreToShiftedRegOffset(value_type,
6153 Location::RegisterLocation(source),
6154 IP,
6155 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006156 }
6157
Roland Levillain3b359c72015-11-17 19:35:12 +00006158 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006159 codegen_->MaybeRecordImplicitNullCheck(instruction);
6160 }
6161
6162 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6163
6164 if (done.IsLinked()) {
6165 __ Bind(&done);
6166 }
6167
6168 if (slow_path != nullptr) {
6169 __ Bind(slow_path->GetExitLabel());
6170 }
6171
6172 break;
6173 }
6174
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006175 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006176 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006177 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006178 size_t offset =
6179 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006180 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006181 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006182 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006183 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006184 }
6185 break;
6186 }
6187
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006188 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006189 Location value = locations->InAt(2);
6190 DCHECK(value.IsFpuRegister());
6191 if (index.IsConstant()) {
6192 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006193 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006194 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006195 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006196 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
6197 }
6198 break;
6199 }
6200
6201 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006202 Location value = locations->InAt(2);
6203 DCHECK(value.IsFpuRegisterPair());
6204 if (index.IsConstant()) {
6205 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006206 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006207 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006208 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006209 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
6210 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006211
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006212 break;
6213 }
6214
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006215 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006216 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006217 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006218 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006219
Roland Levillain80e67092016-01-08 16:04:55 +00006220 // Objects are handled in the switch.
6221 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006222 codegen_->MaybeRecordImplicitNullCheck(instruction);
6223 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006224}
6225
6226void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006227 LocationSummary* locations =
6228 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006229 locations->SetInAt(0, Location::RequiresRegister());
6230 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006231}
6232
6233void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
6234 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006235 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006236 Register obj = locations->InAt(0).AsRegister<Register>();
6237 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006238 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00006239 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07006240 // Mask out compression flag from String's array length.
6241 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006242 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07006243 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006244}
6245
Artem Serov328429f2016-07-06 16:23:04 +01006246void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01006247 LocationSummary* locations =
6248 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6249
6250 locations->SetInAt(0, Location::RequiresRegister());
6251 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6252 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6253}
6254
6255void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6256 LocationSummary* locations = instruction->GetLocations();
6257 Location out = locations->Out();
6258 Location first = locations->InAt(0);
6259 Location second = locations->InAt(1);
6260
Artem Serov328429f2016-07-06 16:23:04 +01006261 if (second.IsRegister()) {
6262 __ add(out.AsRegister<Register>(),
6263 first.AsRegister<Register>(),
6264 ShifterOperand(second.AsRegister<Register>()));
6265 } else {
6266 __ AddConstant(out.AsRegister<Register>(),
6267 first.AsRegister<Register>(),
6268 second.GetConstant()->AsIntConstant()->GetValue());
6269 }
6270}
6271
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006272void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006273 RegisterSet caller_saves = RegisterSet::Empty();
6274 InvokeRuntimeCallingConvention calling_convention;
6275 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6276 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6277 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006278
6279 HInstruction* index = instruction->InputAt(0);
6280 HInstruction* length = instruction->InputAt(1);
6281 // If both index and length are constants we can statically check the bounds. But if at least one
6282 // of them is not encodable ArmEncodableConstantOrRegister will create
6283 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6284 // locations.
6285 bool both_const = index->IsConstant() && length->IsConstant();
6286 locations->SetInAt(0, both_const
6287 ? Location::ConstantLocation(index->AsConstant())
6288 : ArmEncodableConstantOrRegister(index, CMP));
6289 locations->SetInAt(1, both_const
6290 ? Location::ConstantLocation(length->AsConstant())
6291 : ArmEncodableConstantOrRegister(length, CMP));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006292}
6293
6294void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
6295 LocationSummary* locations = instruction->GetLocations();
Artem Serov2dd053d2017-03-08 14:54:06 +00006296 Location index_loc = locations->InAt(0);
6297 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006298
Artem Serov2dd053d2017-03-08 14:54:06 +00006299 if (length_loc.IsConstant()) {
6300 int32_t length = helpers::Int32ConstantFrom(length_loc);
6301 if (index_loc.IsConstant()) {
6302 // BCE will remove the bounds check if we are guaranteed to pass.
6303 int32_t index = helpers::Int32ConstantFrom(index_loc);
6304 if (index < 0 || index >= length) {
6305 SlowPathCodeARM* slow_path =
6306 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6307 codegen_->AddSlowPath(slow_path);
6308 __ b(slow_path->GetEntryLabel());
6309 } else {
6310 // Some optimization after BCE may have generated this, and we should not
6311 // generate a bounds check if it is a valid range.
6312 }
6313 return;
6314 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006315
Artem Serov2dd053d2017-03-08 14:54:06 +00006316 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6317 __ cmp(index_loc.AsRegister<Register>(), ShifterOperand(length));
6318 codegen_->AddSlowPath(slow_path);
6319 __ b(slow_path->GetEntryLabel(), HS);
6320 } else {
6321 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6322 if (index_loc.IsConstant()) {
6323 int32_t index = helpers::Int32ConstantFrom(index_loc);
6324 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index));
6325 } else {
6326 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index_loc.AsRegister<Register>()));
6327 }
6328 codegen_->AddSlowPath(slow_path);
6329 __ b(slow_path->GetEntryLabel(), LS);
6330 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006331}
6332
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006333void CodeGeneratorARM::MarkGCCard(Register temp,
6334 Register card,
6335 Register object,
6336 Register value,
6337 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006338 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006339 if (can_be_null) {
6340 __ CompareAndBranchIfZero(value, &is_null);
6341 }
Andreas Gampe542451c2016-07-26 09:02:02 -07006342 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006343 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
6344 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006345 if (can_be_null) {
6346 __ Bind(&is_null);
6347 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006348}
6349
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006350void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006351 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006352}
6353
6354void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006355 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6356}
6357
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006358void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006359 LocationSummary* locations =
6360 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006361 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006362}
6363
6364void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006365 HBasicBlock* block = instruction->GetBlock();
6366 if (block->GetLoopInformation() != nullptr) {
6367 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6368 // The back edge will generate the suspend check.
6369 return;
6370 }
6371 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6372 // The goto will generate the suspend check.
6373 return;
6374 }
6375 GenerateSuspendCheck(instruction, nullptr);
6376}
6377
6378void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
6379 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006380 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006381 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
6382 if (slow_path == nullptr) {
6383 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
6384 instruction->SetSlowPath(slow_path);
6385 codegen_->AddSlowPath(slow_path);
6386 if (successor != nullptr) {
6387 DCHECK(successor->IsLoopHeader());
6388 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6389 }
6390 } else {
6391 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6392 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006393
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00006394 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006395 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006396 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006397 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006398 __ Bind(slow_path->GetReturnLabel());
6399 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006400 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006401 __ b(slow_path->GetEntryLabel());
6402 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006403}
6404
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006405ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
6406 return codegen_->GetAssembler();
6407}
6408
6409void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006410 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006411 Location source = move->GetSource();
6412 Location destination = move->GetDestination();
6413
6414 if (source.IsRegister()) {
6415 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006416 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006417 } else if (destination.IsFpuRegister()) {
6418 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006419 } else {
6420 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006421 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006422 SP, destination.GetStackIndex());
6423 }
6424 } else if (source.IsStackSlot()) {
6425 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006426 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006427 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006428 } else if (destination.IsFpuRegister()) {
6429 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006430 } else {
6431 DCHECK(destination.IsStackSlot());
6432 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
6433 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6434 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006435 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006436 if (destination.IsRegister()) {
6437 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
6438 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006439 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006440 } else {
6441 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006442 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
6443 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006444 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006445 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006446 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
6447 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006448 } else if (destination.IsRegisterPair()) {
6449 DCHECK(ExpectedPairLayout(destination));
6450 __ LoadFromOffset(
6451 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
6452 } else {
6453 DCHECK(destination.IsFpuRegisterPair()) << destination;
6454 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6455 SP,
6456 source.GetStackIndex());
6457 }
6458 } else if (source.IsRegisterPair()) {
6459 if (destination.IsRegisterPair()) {
6460 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6461 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006462 } else if (destination.IsFpuRegisterPair()) {
6463 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6464 source.AsRegisterPairLow<Register>(),
6465 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006466 } else {
6467 DCHECK(destination.IsDoubleStackSlot()) << destination;
6468 DCHECK(ExpectedPairLayout(source));
6469 __ StoreToOffset(
6470 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
6471 }
6472 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006473 if (destination.IsRegisterPair()) {
6474 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6475 destination.AsRegisterPairHigh<Register>(),
6476 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6477 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006478 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6479 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6480 } else {
6481 DCHECK(destination.IsDoubleStackSlot()) << destination;
6482 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
6483 SP,
6484 destination.GetStackIndex());
6485 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006486 } else {
6487 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006488 HConstant* constant = source.GetConstant();
6489 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6490 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006491 if (destination.IsRegister()) {
6492 __ LoadImmediate(destination.AsRegister<Register>(), value);
6493 } else {
6494 DCHECK(destination.IsStackSlot());
6495 __ LoadImmediate(IP, value);
6496 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6497 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006498 } else if (constant->IsLongConstant()) {
6499 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006500 if (destination.IsRegisterPair()) {
6501 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
6502 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006503 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006504 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006505 __ LoadImmediate(IP, Low32Bits(value));
6506 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6507 __ LoadImmediate(IP, High32Bits(value));
6508 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6509 }
6510 } else if (constant->IsDoubleConstant()) {
6511 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006512 if (destination.IsFpuRegisterPair()) {
6513 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006514 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006515 DCHECK(destination.IsDoubleStackSlot()) << destination;
6516 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006517 __ LoadImmediate(IP, Low32Bits(int_value));
6518 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6519 __ LoadImmediate(IP, High32Bits(int_value));
6520 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6521 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006522 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006523 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006524 float value = constant->AsFloatConstant()->GetValue();
6525 if (destination.IsFpuRegister()) {
6526 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
6527 } else {
6528 DCHECK(destination.IsStackSlot());
6529 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
6530 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6531 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006532 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006533 }
6534}
6535
6536void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
6537 __ Mov(IP, reg);
6538 __ LoadFromOffset(kLoadWord, reg, SP, mem);
6539 __ StoreToOffset(kStoreWord, IP, SP, mem);
6540}
6541
6542void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
6543 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
6544 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6545 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
6546 SP, mem1 + stack_offset);
6547 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
6548 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
6549 SP, mem2 + stack_offset);
6550 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
6551}
6552
6553void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006554 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006555 Location source = move->GetSource();
6556 Location destination = move->GetDestination();
6557
6558 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006559 DCHECK_NE(source.AsRegister<Register>(), IP);
6560 DCHECK_NE(destination.AsRegister<Register>(), IP);
6561 __ Mov(IP, source.AsRegister<Register>());
6562 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
6563 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006564 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006565 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006566 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006567 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006568 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
6569 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006570 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006571 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006572 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006573 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006574 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006575 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006576 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006577 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006578 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6579 destination.AsRegisterPairHigh<Register>(),
6580 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006581 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006582 Register low_reg = source.IsRegisterPair()
6583 ? source.AsRegisterPairLow<Register>()
6584 : destination.AsRegisterPairLow<Register>();
6585 int mem = source.IsRegisterPair()
6586 ? destination.GetStackIndex()
6587 : source.GetStackIndex();
6588 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006589 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006590 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006591 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006592 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006593 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
6594 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006595 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006596 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006597 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006598 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
6599 DRegister reg = source.IsFpuRegisterPair()
6600 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
6601 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
6602 int mem = source.IsFpuRegisterPair()
6603 ? destination.GetStackIndex()
6604 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006605 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006606 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006607 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006608 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
6609 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
6610 : destination.AsFpuRegister<SRegister>();
6611 int mem = source.IsFpuRegister()
6612 ? destination.GetStackIndex()
6613 : source.GetStackIndex();
6614
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006615 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006616 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006617 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006618 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006619 Exchange(source.GetStackIndex(), destination.GetStackIndex());
6620 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006621 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006622 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006623 }
6624}
6625
6626void ParallelMoveResolverARM::SpillScratch(int reg) {
6627 __ Push(static_cast<Register>(reg));
6628}
6629
6630void ParallelMoveResolverARM::RestoreScratch(int reg) {
6631 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006632}
6633
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006634HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
6635 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006636 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006637 case HLoadClass::LoadKind::kInvalid:
6638 LOG(FATAL) << "UNREACHABLE";
6639 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006640 case HLoadClass::LoadKind::kReferrersClass:
6641 break;
6642 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6643 DCHECK(!GetCompilerOptions().GetCompilePic());
6644 break;
6645 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6646 DCHECK(GetCompilerOptions().GetCompilePic());
6647 break;
6648 case HLoadClass::LoadKind::kBootImageAddress:
6649 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006650 case HLoadClass::LoadKind::kBssEntry:
6651 DCHECK(!Runtime::Current()->UseJitCompilation());
6652 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006653 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006654 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006655 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006656 case HLoadClass::LoadKind::kDexCacheViaMethod:
6657 break;
6658 }
6659 return desired_class_load_kind;
6660}
6661
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006662void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006663 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6664 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006665 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006666 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006667 cls,
6668 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006669 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006670 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006671 return;
6672 }
Vladimir Marko41559982017-01-06 14:04:23 +00006673 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006674
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006675 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6676 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006677 ? LocationSummary::kCallOnSlowPath
6678 : LocationSummary::kNoCall;
6679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006680 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006681 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006682 }
6683
Vladimir Marko41559982017-01-06 14:04:23 +00006684 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006685 locations->SetInAt(0, Location::RequiresRegister());
6686 }
6687 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006688 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6689 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6690 // Rely on the type resolution or initialization and marking to save everything we need.
6691 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6692 // to the custom calling convention) or by marking, so we request a different temp.
6693 locations->AddTemp(Location::RequiresRegister());
6694 RegisterSet caller_saves = RegisterSet::Empty();
6695 InvokeRuntimeCallingConvention calling_convention;
6696 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6697 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6698 // that the the kPrimNot result register is the same as the first argument register.
6699 locations->SetCustomSlowPathCallerSaves(caller_saves);
6700 } else {
6701 // For non-Baker read barrier we have a temp-clobbering call.
6702 }
6703 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006704}
6705
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006706// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6707// move.
6708void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006709 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6710 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6711 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006712 return;
6713 }
Vladimir Marko41559982017-01-06 14:04:23 +00006714 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006715
Vladimir Marko41559982017-01-06 14:04:23 +00006716 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006717 Location out_loc = locations->Out();
6718 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006719
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006720 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6721 ? kWithoutReadBarrier
6722 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006723 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006724 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006725 case HLoadClass::LoadKind::kReferrersClass: {
6726 DCHECK(!cls->CanCallRuntime());
6727 DCHECK(!cls->MustGenerateClinitCheck());
6728 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6729 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006730 GenerateGcRootFieldLoad(cls,
6731 out_loc,
6732 current_method,
6733 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006734 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006735 break;
6736 }
6737 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006738 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006739 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006740 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6741 cls->GetTypeIndex()));
6742 break;
6743 }
6744 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006745 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006746 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006747 CodeGeneratorARM::PcRelativePatchInfo* labels =
6748 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6749 __ BindTrackedLabel(&labels->movw_label);
6750 __ movw(out, /* placeholder */ 0u);
6751 __ BindTrackedLabel(&labels->movt_label);
6752 __ movt(out, /* placeholder */ 0u);
6753 __ BindTrackedLabel(&labels->add_pc_label);
6754 __ add(out, out, ShifterOperand(PC));
6755 break;
6756 }
6757 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006758 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006759 uint32_t address = dchecked_integral_cast<uint32_t>(
6760 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6761 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006762 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6763 break;
6764 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006765 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006766 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6767 ? locations->GetTemp(0).AsRegister<Register>()
6768 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006769 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006770 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006771 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006772 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006773 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006774 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006775 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006776 __ add(temp, temp, ShifterOperand(PC));
6777 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006778 generate_null_check = true;
6779 break;
6780 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006781 case HLoadClass::LoadKind::kJitTableAddress: {
6782 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6783 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006784 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006785 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006786 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006787 break;
6788 }
Vladimir Marko41559982017-01-06 14:04:23 +00006789 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006790 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006791 LOG(FATAL) << "UNREACHABLE";
6792 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006793 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006794
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006795 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6796 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01006797 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006798 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6799 codegen_->AddSlowPath(slow_path);
6800 if (generate_null_check) {
6801 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6802 }
6803 if (cls->MustGenerateClinitCheck()) {
6804 GenerateClassInitializationCheck(slow_path, out);
6805 } else {
6806 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006807 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006808 }
6809}
6810
6811void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
6812 LocationSummary* locations =
6813 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6814 locations->SetInAt(0, Location::RequiresRegister());
6815 if (check->HasUses()) {
6816 locations->SetOut(Location::SameAsFirstInput());
6817 }
6818}
6819
6820void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006821 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006822 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006823 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006824 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006825 GenerateClassInitializationCheck(slow_path,
6826 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006827}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006828
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006829void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01006830 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006831 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
6832 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
6833 __ b(slow_path->GetEntryLabel(), LT);
6834 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6835 // properly. Therefore, we do a memory fence.
6836 __ dmb(ISH);
6837 __ Bind(slow_path->GetExitLabel());
6838}
6839
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006840HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
6841 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006842 switch (desired_string_load_kind) {
6843 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6844 DCHECK(!GetCompilerOptions().GetCompilePic());
6845 break;
6846 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6847 DCHECK(GetCompilerOptions().GetCompilePic());
6848 break;
6849 case HLoadString::LoadKind::kBootImageAddress:
6850 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006851 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006852 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006853 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006854 case HLoadString::LoadKind::kJitTableAddress:
6855 DCHECK(Runtime::Current()->UseJitCompilation());
6856 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006857 case HLoadString::LoadKind::kDexCacheViaMethod:
6858 break;
6859 }
6860 return desired_string_load_kind;
6861}
6862
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006863void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006864 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006865 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006866 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006867 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006868 locations->SetOut(Location::RegisterLocation(R0));
6869 } else {
6870 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006871 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6872 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006873 // Rely on the pResolveString and marking to save everything we need, including temps.
6874 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6875 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006876 locations->AddTemp(Location::RequiresRegister());
6877 RegisterSet caller_saves = RegisterSet::Empty();
6878 InvokeRuntimeCallingConvention calling_convention;
6879 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6880 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6881 // that the the kPrimNot result register is the same as the first argument register.
6882 locations->SetCustomSlowPathCallerSaves(caller_saves);
6883 } else {
6884 // For non-Baker read barrier we have a temp-clobbering call.
6885 }
6886 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006887 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006888}
6889
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006890// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6891// move.
6892void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006893 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006894 Location out_loc = locations->Out();
6895 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006896 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00006897
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006898 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006899 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006900 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006901 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6902 load->GetStringIndex()));
6903 return; // No dex cache slow path.
6904 }
6905 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006906 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006907 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006908 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006909 __ BindTrackedLabel(&labels->movw_label);
6910 __ movw(out, /* placeholder */ 0u);
6911 __ BindTrackedLabel(&labels->movt_label);
6912 __ movt(out, /* placeholder */ 0u);
6913 __ BindTrackedLabel(&labels->add_pc_label);
6914 __ add(out, out, ShifterOperand(PC));
6915 return; // No dex cache slow path.
6916 }
6917 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006918 uint32_t address = dchecked_integral_cast<uint32_t>(
6919 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6920 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006921 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6922 return; // No dex cache slow path.
6923 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006924 case HLoadString::LoadKind::kBssEntry: {
6925 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006926 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6927 ? locations->GetTemp(0).AsRegister<Register>()
6928 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006929 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006930 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006931 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006932 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006933 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006934 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006935 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006936 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006937 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006938 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
6939 codegen_->AddSlowPath(slow_path);
6940 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6941 __ Bind(slow_path->GetExitLabel());
6942 return;
6943 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006944 case HLoadString::LoadKind::kJitTableAddress: {
6945 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006946 load->GetStringIndex(),
6947 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006948 // /* GcRoot<mirror::String> */ out = *out
6949 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6950 return;
6951 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006952 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006953 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006954 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006955
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006956 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
6957 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6958 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006959 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006960 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006961 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6962 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006963}
6964
David Brazdilcb1c0552015-08-04 16:22:25 +01006965static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006966 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006967}
6968
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006969void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6970 LocationSummary* locations =
6971 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6972 locations->SetOut(Location::RequiresRegister());
6973}
6974
6975void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006976 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006977 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6978}
6979
6980void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6981 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6982}
6983
6984void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006985 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006986 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006987}
6988
6989void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6990 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006991 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006992 InvokeRuntimeCallingConvention calling_convention;
6993 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6994}
6995
6996void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006997 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006998 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006999}
7000
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007001// Temp is used for read barrier.
7002static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7003 if (kEmitCompilerReadBarrier &&
7004 (kUseBakerReadBarrier ||
7005 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7006 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7007 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7008 return 1;
7009 }
7010 return 0;
7011}
7012
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007013// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007014// interface pointer, one for loading the current interface.
7015// The other checks have one temp for loading the object's class.
7016static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7017 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7018 return 3;
7019 }
7020 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00007021}
7022
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007023void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007024 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00007025 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007026 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00007027 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007028 case TypeCheckKind::kExactCheck:
7029 case TypeCheckKind::kAbstractClassCheck:
7030 case TypeCheckKind::kClassHierarchyCheck:
7031 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007032 call_kind =
7033 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01007034 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007035 break;
7036 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007037 case TypeCheckKind::kUnresolvedCheck:
7038 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007039 call_kind = LocationSummary::kCallOnSlowPath;
7040 break;
7041 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007042
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007043 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007044 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007045 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007046 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007047 locations->SetInAt(0, Location::RequiresRegister());
7048 locations->SetInAt(1, Location::RequiresRegister());
7049 // The "out" register is used as a temporary, so it overlaps with the inputs.
7050 // Note that TypeCheckSlowPathARM uses this register too.
7051 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007052 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007053}
7054
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007055void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007056 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007057 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007058 Location obj_loc = locations->InAt(0);
7059 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007060 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007061 Location out_loc = locations->Out();
7062 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007063 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7064 DCHECK_LE(num_temps, 1u);
7065 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007066 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007067 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7068 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7069 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007070 Label done;
7071 Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01007072 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007073
7074 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007075 // avoid null check if we know obj is not null.
7076 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007077 DCHECK_NE(out, obj);
7078 __ LoadImmediate(out, 0);
7079 __ CompareAndBranchIfZero(obj, final_label);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007080 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007081
Roland Levillainc9285912015-12-18 10:38:42 +00007082 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007083 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007084 // /* HeapReference<Class> */ out = obj->klass_
7085 GenerateReferenceLoadTwoRegisters(instruction,
7086 out_loc,
7087 obj_loc,
7088 class_offset,
7089 maybe_temp_loc,
7090 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007091 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007092 __ cmp(out, ShifterOperand(cls));
7093 // We speculatively set the result to false without changing the condition
7094 // flags, which allows us to avoid some branching later.
7095 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7096
7097 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7098 // we check that the output is in a low register, so that a 16-bit MOV
7099 // encoding can be used.
7100 if (ArmAssembler::IsLowRegister(out)) {
7101 __ it(EQ);
7102 __ mov(out, ShifterOperand(1), EQ);
7103 } else {
7104 __ b(final_label, NE);
7105 __ LoadImmediate(out, 1);
7106 }
7107
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007108 break;
7109 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007110
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007111 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007112 // /* HeapReference<Class> */ out = obj->klass_
7113 GenerateReferenceLoadTwoRegisters(instruction,
7114 out_loc,
7115 obj_loc,
7116 class_offset,
7117 maybe_temp_loc,
7118 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007119 // If the class is abstract, we eagerly fetch the super class of the
7120 // object to avoid doing a comparison we know will fail.
7121 Label loop;
7122 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007123 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007124 GenerateReferenceLoadOneRegister(instruction,
7125 out_loc,
7126 super_offset,
7127 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007128 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007129 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007130 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007131 __ cmp(out, ShifterOperand(cls));
7132 __ b(&loop, NE);
7133 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007134 break;
7135 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007136
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007137 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007138 // /* HeapReference<Class> */ out = obj->klass_
7139 GenerateReferenceLoadTwoRegisters(instruction,
7140 out_loc,
7141 obj_loc,
7142 class_offset,
7143 maybe_temp_loc,
7144 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007145 // Walk over the class hierarchy to find a match.
7146 Label loop, success;
7147 __ Bind(&loop);
7148 __ cmp(out, ShifterOperand(cls));
7149 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007150 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007151 GenerateReferenceLoadOneRegister(instruction,
7152 out_loc,
7153 super_offset,
7154 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007155 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007156 // This is essentially a null check, but it sets the condition flags to the
7157 // proper value for the code that follows the loop, i.e. not `EQ`.
7158 __ cmp(out, ShifterOperand(1));
7159 __ b(&loop, HS);
7160
7161 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7162 // we check that the output is in a low register, so that a 16-bit MOV
7163 // encoding can be used.
7164 if (ArmAssembler::IsLowRegister(out)) {
7165 // If `out` is null, we use it for the result, and the condition flags
7166 // have already been set to `NE`, so the IT block that comes afterwards
7167 // (and which handles the successful case) turns into a NOP (instead of
7168 // overwriting `out`).
7169 __ Bind(&success);
7170 // There is only one branch to the `success` label (which is bound to this
7171 // IT block), and it has the same condition, `EQ`, so in that case the MOV
7172 // is executed.
7173 __ it(EQ);
7174 __ mov(out, ShifterOperand(1), EQ);
7175 } else {
7176 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007177 __ b(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007178 __ Bind(&success);
7179 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007180 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007181
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007182 break;
7183 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007184
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007185 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007186 // /* HeapReference<Class> */ out = obj->klass_
7187 GenerateReferenceLoadTwoRegisters(instruction,
7188 out_loc,
7189 obj_loc,
7190 class_offset,
7191 maybe_temp_loc,
7192 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007193 // Do an exact check.
7194 Label exact_check;
7195 __ cmp(out, ShifterOperand(cls));
7196 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007197 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007198 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007199 GenerateReferenceLoadOneRegister(instruction,
7200 out_loc,
7201 component_offset,
7202 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007203 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007204 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007205 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007206 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7207 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007208 __ cmp(out, ShifterOperand(0));
7209 // We speculatively set the result to false without changing the condition
7210 // flags, which allows us to avoid some branching later.
7211 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7212
7213 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7214 // we check that the output is in a low register, so that a 16-bit MOV
7215 // encoding can be used.
7216 if (ArmAssembler::IsLowRegister(out)) {
7217 __ Bind(&exact_check);
7218 __ it(EQ);
7219 __ mov(out, ShifterOperand(1), EQ);
7220 } else {
7221 __ b(final_label, NE);
7222 __ Bind(&exact_check);
7223 __ LoadImmediate(out, 1);
7224 }
7225
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007226 break;
7227 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007228
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007229 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007230 // No read barrier since the slow path will retry upon failure.
7231 // /* HeapReference<Class> */ out = obj->klass_
7232 GenerateReferenceLoadTwoRegisters(instruction,
7233 out_loc,
7234 obj_loc,
7235 class_offset,
7236 maybe_temp_loc,
7237 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007238 __ cmp(out, ShifterOperand(cls));
7239 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00007240 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7241 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007242 codegen_->AddSlowPath(slow_path);
7243 __ b(slow_path->GetEntryLabel(), NE);
7244 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007245 break;
7246 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007247
Calin Juravle98893e12015-10-02 21:05:03 +01007248 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007249 case TypeCheckKind::kInterfaceCheck: {
7250 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007251 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00007252 // cases.
7253 //
7254 // We cannot directly call the InstanceofNonTrivial runtime
7255 // entry point without resorting to a type checking slow path
7256 // here (i.e. by calling InvokeRuntime directly), as it would
7257 // require to assign fixed registers for the inputs of this
7258 // HInstanceOf instruction (following the runtime calling
7259 // convention), which might be cluttered by the potential first
7260 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00007261 //
7262 // TODO: Introduce a new runtime entry point taking the object
7263 // to test (instead of its class) as argument, and let it deal
7264 // with the read barrier issues. This will let us refactor this
7265 // case of the `switch` code as it was previously (with a direct
7266 // call to the runtime not using a type checking slow path).
7267 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00007268 DCHECK(locations->OnlyCallsOnSlowPath());
7269 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7270 /* is_fatal */ false);
7271 codegen_->AddSlowPath(slow_path);
7272 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007273 break;
7274 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007275 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007276
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007277 if (done.IsLinked()) {
7278 __ Bind(&done);
7279 }
7280
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007281 if (slow_path != nullptr) {
7282 __ Bind(slow_path->GetExitLabel());
7283 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007284}
7285
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007286void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007287 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7288 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7289
Roland Levillain3b359c72015-11-17 19:35:12 +00007290 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7291 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007292 case TypeCheckKind::kExactCheck:
7293 case TypeCheckKind::kAbstractClassCheck:
7294 case TypeCheckKind::kClassHierarchyCheck:
7295 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007296 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7297 LocationSummary::kCallOnSlowPath :
7298 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007299 break;
7300 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007301 case TypeCheckKind::kUnresolvedCheck:
7302 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007303 call_kind = LocationSummary::kCallOnSlowPath;
7304 break;
7305 }
7306
Roland Levillain3b359c72015-11-17 19:35:12 +00007307 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7308 locations->SetInAt(0, Location::RequiresRegister());
7309 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007310 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007311}
7312
7313void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007314 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007315 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007316 Location obj_loc = locations->InAt(0);
7317 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007318 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007319 Location temp_loc = locations->GetTemp(0);
7320 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007321 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7322 DCHECK_LE(num_temps, 3u);
7323 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7324 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7325 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7326 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7327 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7328 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7329 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7330 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7331 const uint32_t object_array_data_offset =
7332 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007333
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007334 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7335 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7336 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007337 bool is_type_check_slow_path_fatal = false;
7338 if (!kEmitCompilerReadBarrier) {
7339 is_type_check_slow_path_fatal =
7340 (type_check_kind == TypeCheckKind::kExactCheck ||
7341 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7342 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7343 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7344 !instruction->CanThrowIntoCatchBlock();
7345 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01007346 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007347 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7348 is_type_check_slow_path_fatal);
7349 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007350
7351 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007352 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007353 // Avoid null check if we know obj is not null.
7354 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007355 __ CompareAndBranchIfZero(obj, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007356 }
7357
Roland Levillain3b359c72015-11-17 19:35:12 +00007358 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007359 case TypeCheckKind::kExactCheck:
7360 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007361 // /* HeapReference<Class> */ temp = obj->klass_
7362 GenerateReferenceLoadTwoRegisters(instruction,
7363 temp_loc,
7364 obj_loc,
7365 class_offset,
7366 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007367 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007368
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007369 __ cmp(temp, ShifterOperand(cls));
7370 // Jump to slow path for throwing the exception or doing a
7371 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00007372 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007373 break;
7374 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007375
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007376 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007377 // /* HeapReference<Class> */ temp = obj->klass_
7378 GenerateReferenceLoadTwoRegisters(instruction,
7379 temp_loc,
7380 obj_loc,
7381 class_offset,
7382 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007383 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007384
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007385 // If the class is abstract, we eagerly fetch the super class of the
7386 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007387 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007388 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007389 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007390 GenerateReferenceLoadOneRegister(instruction,
7391 temp_loc,
7392 super_offset,
7393 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007394 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007395
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007396 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7397 // exception.
7398 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00007399
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007400 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007401 __ cmp(temp, ShifterOperand(cls));
7402 __ b(&loop, NE);
7403 break;
7404 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007405
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007406 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007407 // /* HeapReference<Class> */ temp = obj->klass_
7408 GenerateReferenceLoadTwoRegisters(instruction,
7409 temp_loc,
7410 obj_loc,
7411 class_offset,
7412 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007413 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007414
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007415 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007416 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007417 __ Bind(&loop);
7418 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007419 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007420
Roland Levillain3b359c72015-11-17 19:35:12 +00007421 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007422 GenerateReferenceLoadOneRegister(instruction,
7423 temp_loc,
7424 super_offset,
7425 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007426 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007427
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007428 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7429 // exception.
7430 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7431 // Otherwise, jump to the beginning of the loop.
7432 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007433 break;
7434 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007435
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007436 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007437 // /* HeapReference<Class> */ temp = obj->klass_
7438 GenerateReferenceLoadTwoRegisters(instruction,
7439 temp_loc,
7440 obj_loc,
7441 class_offset,
7442 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007443 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007444
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007445 // Do an exact check.
7446 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007447 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007448
7449 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007450 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007451 GenerateReferenceLoadOneRegister(instruction,
7452 temp_loc,
7453 component_offset,
7454 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007455 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007456 // If the component type is null, jump to the slow path to throw the exception.
7457 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7458 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7459 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007460 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00007461 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007462 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007463 break;
7464 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007465
Calin Juravle98893e12015-10-02 21:05:03 +01007466 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007467 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00007468 // We cannot directly call the CheckCast runtime entry point
7469 // without resorting to a type checking slow path here (i.e. by
7470 // calling InvokeRuntime directly), as it would require to
7471 // assign fixed registers for the inputs of this HInstanceOf
7472 // instruction (following the runtime calling convention), which
7473 // might be cluttered by the potential first read barrier
7474 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007475
Roland Levillain3b359c72015-11-17 19:35:12 +00007476 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007477 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007478
7479 case TypeCheckKind::kInterfaceCheck: {
7480 // Avoid read barriers to improve performance of the fast path. We can not get false
7481 // positives by doing this.
7482 // /* HeapReference<Class> */ temp = obj->klass_
7483 GenerateReferenceLoadTwoRegisters(instruction,
7484 temp_loc,
7485 obj_loc,
7486 class_offset,
7487 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007488 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007489
7490 // /* HeapReference<Class> */ temp = temp->iftable_
7491 GenerateReferenceLoadTwoRegisters(instruction,
7492 temp_loc,
7493 temp_loc,
7494 iftable_offset,
7495 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007496 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007497 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007498 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08007499 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007500 Label start_loop;
7501 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007502 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
7503 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007504 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
7505 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007506 // Go to next interface.
7507 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
7508 __ sub(maybe_temp2_loc.AsRegister<Register>(),
7509 maybe_temp2_loc.AsRegister<Register>(),
7510 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007511 // Compare the classes and continue the loop if they do not match.
7512 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
7513 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007514 break;
7515 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007516 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007517
7518 if (done.IsLinked()) {
7519 __ Bind(&done);
7520 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007521
Roland Levillain3b359c72015-11-17 19:35:12 +00007522 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007523}
7524
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007525void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
7526 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007527 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007528 InvokeRuntimeCallingConvention calling_convention;
7529 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7530}
7531
7532void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007533 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7534 instruction,
7535 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007536 if (instruction->IsEnter()) {
7537 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7538 } else {
7539 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7540 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007541}
7542
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007543void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
7544void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
7545void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007546
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007547void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007548 LocationSummary* locations =
7549 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7550 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7551 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007552 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007553 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007554 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00007555 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007556}
7557
7558void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
7559 HandleBitwiseOperation(instruction);
7560}
7561
7562void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
7563 HandleBitwiseOperation(instruction);
7564}
7565
7566void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
7567 HandleBitwiseOperation(instruction);
7568}
7569
Artem Serov7fc63502016-02-09 17:15:29 +00007570
7571void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7572 LocationSummary* locations =
7573 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7574 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7575 || instruction->GetResultType() == Primitive::kPrimLong);
7576
7577 locations->SetInAt(0, Location::RequiresRegister());
7578 locations->SetInAt(1, Location::RequiresRegister());
7579 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7580}
7581
7582void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7583 LocationSummary* locations = instruction->GetLocations();
7584 Location first = locations->InAt(0);
7585 Location second = locations->InAt(1);
7586 Location out = locations->Out();
7587
7588 if (instruction->GetResultType() == Primitive::kPrimInt) {
7589 Register first_reg = first.AsRegister<Register>();
7590 ShifterOperand second_reg(second.AsRegister<Register>());
7591 Register out_reg = out.AsRegister<Register>();
7592
7593 switch (instruction->GetOpKind()) {
7594 case HInstruction::kAnd:
7595 __ bic(out_reg, first_reg, second_reg);
7596 break;
7597 case HInstruction::kOr:
7598 __ orn(out_reg, first_reg, second_reg);
7599 break;
7600 // There is no EON on arm.
7601 case HInstruction::kXor:
7602 default:
7603 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7604 UNREACHABLE();
7605 }
7606 return;
7607
7608 } else {
7609 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7610 Register first_low = first.AsRegisterPairLow<Register>();
7611 Register first_high = first.AsRegisterPairHigh<Register>();
7612 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7613 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7614 Register out_low = out.AsRegisterPairLow<Register>();
7615 Register out_high = out.AsRegisterPairHigh<Register>();
7616
7617 switch (instruction->GetOpKind()) {
7618 case HInstruction::kAnd:
7619 __ bic(out_low, first_low, second_low);
7620 __ bic(out_high, first_high, second_high);
7621 break;
7622 case HInstruction::kOr:
7623 __ orn(out_low, first_low, second_low);
7624 __ orn(out_high, first_high, second_high);
7625 break;
7626 // There is no EON on arm.
7627 case HInstruction::kXor:
7628 default:
7629 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7630 UNREACHABLE();
7631 }
7632 }
7633}
7634
Anton Kirilov74234da2017-01-13 14:42:47 +00007635void LocationsBuilderARM::VisitDataProcWithShifterOp(
7636 HDataProcWithShifterOp* instruction) {
7637 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
7638 instruction->GetType() == Primitive::kPrimLong);
7639 LocationSummary* locations =
7640 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7641 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
7642 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7643
7644 locations->SetInAt(0, Location::RequiresRegister());
7645 locations->SetInAt(1, Location::RequiresRegister());
7646 locations->SetOut(Location::RequiresRegister(),
7647 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7648}
7649
7650void InstructionCodeGeneratorARM::VisitDataProcWithShifterOp(
7651 HDataProcWithShifterOp* instruction) {
7652 const LocationSummary* const locations = instruction->GetLocations();
7653 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
7654 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
7655 const Location left = locations->InAt(0);
7656 const Location right = locations->InAt(1);
7657 const Location out = locations->Out();
7658
7659 if (instruction->GetType() == Primitive::kPrimInt) {
7660 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
7661
7662 const Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
7663 ? right.AsRegisterPairLow<Register>()
7664 : right.AsRegister<Register>();
7665
7666 GenerateDataProcInstruction(kind,
7667 out.AsRegister<Register>(),
7668 left.AsRegister<Register>(),
7669 ShifterOperand(second,
7670 ShiftFromOpKind(op_kind),
7671 instruction->GetShiftAmount()),
7672 codegen_);
7673 } else {
7674 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7675
7676 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7677 const Register second = right.AsRegister<Register>();
7678
7679 DCHECK_NE(out.AsRegisterPairLow<Register>(), second);
7680 GenerateDataProc(kind,
7681 out,
7682 left,
7683 ShifterOperand(second),
7684 ShifterOperand(second, ASR, 31),
7685 codegen_);
7686 } else {
7687 GenerateLongDataProc(instruction, codegen_);
7688 }
7689 }
7690}
7691
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007692void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
7693 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7694 if (value == 0xffffffffu) {
7695 if (out != first) {
7696 __ mov(out, ShifterOperand(first));
7697 }
7698 return;
7699 }
7700 if (value == 0u) {
7701 __ mov(out, ShifterOperand(0));
7702 return;
7703 }
7704 ShifterOperand so;
7705 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
7706 __ and_(out, first, so);
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007707 } else if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007708 __ bic(out, first, ShifterOperand(~value));
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007709 } else {
7710 DCHECK(IsPowerOfTwo(value + 1));
7711 __ ubfx(out, first, 0, WhichPowerOf2(value + 1));
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007712 }
7713}
7714
7715void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
7716 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7717 if (value == 0u) {
7718 if (out != first) {
7719 __ mov(out, ShifterOperand(first));
7720 }
7721 return;
7722 }
7723 if (value == 0xffffffffu) {
7724 __ mvn(out, ShifterOperand(0));
7725 return;
7726 }
7727 ShifterOperand so;
7728 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
7729 __ orr(out, first, so);
7730 } else {
7731 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
7732 __ orn(out, first, ShifterOperand(~value));
7733 }
7734}
7735
7736void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
7737 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7738 if (value == 0u) {
7739 if (out != first) {
7740 __ mov(out, ShifterOperand(first));
7741 }
7742 return;
7743 }
7744 __ eor(out, first, ShifterOperand(value));
7745}
7746
Vladimir Marko59751a72016-08-05 14:37:27 +01007747void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
7748 Location first,
7749 uint64_t value) {
7750 Register out_low = out.AsRegisterPairLow<Register>();
7751 Register out_high = out.AsRegisterPairHigh<Register>();
7752 Register first_low = first.AsRegisterPairLow<Register>();
7753 Register first_high = first.AsRegisterPairHigh<Register>();
7754 uint32_t value_low = Low32Bits(value);
7755 uint32_t value_high = High32Bits(value);
7756 if (value_low == 0u) {
7757 if (out_low != first_low) {
7758 __ mov(out_low, ShifterOperand(first_low));
7759 }
7760 __ AddConstant(out_high, first_high, value_high);
7761 return;
7762 }
7763 __ AddConstantSetFlags(out_low, first_low, value_low);
7764 ShifterOperand so;
7765 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
7766 __ adc(out_high, first_high, so);
7767 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
7768 __ sbc(out_high, first_high, so);
7769 } else {
7770 LOG(FATAL) << "Unexpected constant " << value_high;
7771 UNREACHABLE();
7772 }
7773}
7774
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007775void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
7776 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007777 Location first = locations->InAt(0);
7778 Location second = locations->InAt(1);
7779 Location out = locations->Out();
7780
7781 if (second.IsConstant()) {
7782 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7783 uint32_t value_low = Low32Bits(value);
7784 if (instruction->GetResultType() == Primitive::kPrimInt) {
7785 Register first_reg = first.AsRegister<Register>();
7786 Register out_reg = out.AsRegister<Register>();
7787 if (instruction->IsAnd()) {
7788 GenerateAndConst(out_reg, first_reg, value_low);
7789 } else if (instruction->IsOr()) {
7790 GenerateOrrConst(out_reg, first_reg, value_low);
7791 } else {
7792 DCHECK(instruction->IsXor());
7793 GenerateEorConst(out_reg, first_reg, value_low);
7794 }
7795 } else {
7796 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7797 uint32_t value_high = High32Bits(value);
7798 Register first_low = first.AsRegisterPairLow<Register>();
7799 Register first_high = first.AsRegisterPairHigh<Register>();
7800 Register out_low = out.AsRegisterPairLow<Register>();
7801 Register out_high = out.AsRegisterPairHigh<Register>();
7802 if (instruction->IsAnd()) {
7803 GenerateAndConst(out_low, first_low, value_low);
7804 GenerateAndConst(out_high, first_high, value_high);
7805 } else if (instruction->IsOr()) {
7806 GenerateOrrConst(out_low, first_low, value_low);
7807 GenerateOrrConst(out_high, first_high, value_high);
7808 } else {
7809 DCHECK(instruction->IsXor());
7810 GenerateEorConst(out_low, first_low, value_low);
7811 GenerateEorConst(out_high, first_high, value_high);
7812 }
7813 }
7814 return;
7815 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007816
7817 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007818 Register first_reg = first.AsRegister<Register>();
7819 ShifterOperand second_reg(second.AsRegister<Register>());
7820 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007821 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007822 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007823 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007824 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007825 } else {
7826 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007827 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007828 }
7829 } else {
7830 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007831 Register first_low = first.AsRegisterPairLow<Register>();
7832 Register first_high = first.AsRegisterPairHigh<Register>();
7833 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7834 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7835 Register out_low = out.AsRegisterPairLow<Register>();
7836 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007837 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007838 __ and_(out_low, first_low, second_low);
7839 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007840 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007841 __ orr(out_low, first_low, second_low);
7842 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007843 } else {
7844 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007845 __ eor(out_low, first_low, second_low);
7846 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007847 }
7848 }
7849}
7850
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007851void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
7852 HInstruction* instruction,
7853 Location out,
7854 uint32_t offset,
7855 Location maybe_temp,
7856 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007857 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007858 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007859 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007860 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007861 if (kUseBakerReadBarrier) {
7862 // Load with fast path based Baker's read barrier.
7863 // /* HeapReference<Object> */ out = *(out + offset)
7864 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007865 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007866 } else {
7867 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007868 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00007869 // in the following move operation, as we will need it for the
7870 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007871 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00007872 // /* HeapReference<Object> */ out = *(out + offset)
7873 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007874 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00007875 }
7876 } else {
7877 // Plain load with no read barrier.
7878 // /* HeapReference<Object> */ out = *(out + offset)
7879 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7880 __ MaybeUnpoisonHeapReference(out_reg);
7881 }
7882}
7883
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007884void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
7885 HInstruction* instruction,
7886 Location out,
7887 Location obj,
7888 uint32_t offset,
7889 Location maybe_temp,
7890 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007891 Register out_reg = out.AsRegister<Register>();
7892 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007893 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007894 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007895 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007896 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007897 // Load with fast path based Baker's read barrier.
7898 // /* HeapReference<Object> */ out = *(obj + offset)
7899 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007900 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007901 } else {
7902 // Load with slow path based read barrier.
7903 // /* HeapReference<Object> */ out = *(obj + offset)
7904 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7905 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7906 }
7907 } else {
7908 // Plain load with no read barrier.
7909 // /* HeapReference<Object> */ out = *(obj + offset)
7910 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7911 __ MaybeUnpoisonHeapReference(out_reg);
7912 }
7913}
7914
7915void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
7916 Location root,
7917 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007918 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007919 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007920 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007921 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007922 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007923 if (kUseBakerReadBarrier) {
7924 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00007925 // Baker's read barrier are used.
Roland Levillainc9285912015-12-18 10:38:42 +00007926 //
Roland Levillainba650a42017-03-06 13:52:32 +00007927 // Note that we do not actually check the value of
7928 // `GetIsGcMarking()` to decide whether to mark the loaded GC
7929 // root or not. Instead, we load into `temp` the read barrier
7930 // mark entry point corresponding to register `root`. If `temp`
7931 // is null, it means that `GetIsGcMarking()` is false, and vice
7932 // versa.
7933 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007934 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00007935 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7936 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
7937 // // Slow path.
7938 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
Roland Levillainc9285912015-12-18 10:38:42 +00007939 // }
7940
Roland Levillainba650a42017-03-06 13:52:32 +00007941 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
7942 Location temp = Location::RegisterLocation(LR);
7943 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
7944 instruction, root, /* entrypoint */ temp);
7945 codegen_->AddSlowPath(slow_path);
7946
7947 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7948 const int32_t entry_point_offset =
7949 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
7950 // Loading the entrypoint does not require a load acquire since it is only changed when
7951 // threads are suspended or running a checkpoint.
7952 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
7953
Roland Levillainc9285912015-12-18 10:38:42 +00007954 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7955 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7956 static_assert(
7957 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7958 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7959 "have different sizes.");
7960 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7961 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7962 "have different sizes.");
7963
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007964 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7965 // checking GetIsGcMarking.
7966 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00007967 __ Bind(slow_path->GetExitLabel());
7968 } else {
7969 // GC root loaded through a slow path for read barriers other
7970 // than Baker's.
7971 // /* GcRoot<mirror::Object>* */ root = obj + offset
7972 __ AddConstant(root_reg, obj, offset);
7973 // /* mirror::Object* */ root = root->Read()
7974 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7975 }
7976 } else {
7977 // Plain GC root load with no read barrier.
7978 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7979 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7980 // Note that GC roots are not affected by heap poisoning, thus we
7981 // do not have to unpoison `root_reg` here.
7982 }
7983}
7984
7985void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7986 Location ref,
7987 Register obj,
7988 uint32_t offset,
7989 Location temp,
7990 bool needs_null_check) {
7991 DCHECK(kEmitCompilerReadBarrier);
7992 DCHECK(kUseBakerReadBarrier);
7993
7994 // /* HeapReference<Object> */ ref = *(obj + offset)
7995 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01007996 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00007997 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007998 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007999}
8000
8001void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8002 Location ref,
8003 Register obj,
8004 uint32_t data_offset,
8005 Location index,
8006 Location temp,
8007 bool needs_null_check) {
8008 DCHECK(kEmitCompilerReadBarrier);
8009 DCHECK(kUseBakerReadBarrier);
8010
Roland Levillainbfea3352016-06-23 13:48:47 +01008011 static_assert(
8012 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8013 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00008014 // /* HeapReference<Object> */ ref =
8015 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01008016 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00008017 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008018 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008019}
8020
8021void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8022 Location ref,
8023 Register obj,
8024 uint32_t offset,
8025 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01008026 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00008027 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008028 bool needs_null_check,
8029 bool always_update_field,
8030 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00008031 DCHECK(kEmitCompilerReadBarrier);
8032 DCHECK(kUseBakerReadBarrier);
8033
Roland Levillain54f869e2017-03-06 13:54:11 +00008034 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8035 // whether we need to enter the slow path to mark the reference.
8036 // Then, in the slow path, check the gray bit in the lock word of
8037 // the reference's holder (`obj`) to decide whether to mark `ref` or
8038 // not.
Roland Levillainc9285912015-12-18 10:38:42 +00008039 //
Roland Levillainba650a42017-03-06 13:52:32 +00008040 // Note that we do not actually check the value of `GetIsGcMarking()`;
8041 // instead, we load into `temp3` the read barrier mark entry point
8042 // corresponding to register `ref`. If `temp3` is null, it means
8043 // that `GetIsGcMarking()` is false, and vice versa.
8044 //
8045 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008046 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8047 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008048 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8049 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8050 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8051 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8052 // if (is_gray) {
8053 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8054 // }
8055 // } else {
8056 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillainc9285912015-12-18 10:38:42 +00008057 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008058
Roland Levillain35345a52017-02-27 14:32:08 +00008059 Register temp_reg = temp.AsRegister<Register>();
Roland Levillain1372c9f2017-01-13 11:47:39 +00008060
Roland Levillainba650a42017-03-06 13:52:32 +00008061 // Slow path marking the object `ref` when the GC is marking. The
8062 // entrypoint will already be loaded in `temp3`.
8063 Location temp3 = Location::RegisterLocation(LR);
8064 SlowPathCodeARM* slow_path;
8065 if (always_update_field) {
8066 DCHECK(temp2 != nullptr);
Roland Levillain54f869e2017-03-06 13:54:11 +00008067 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM only
8068 // supports address of the form `obj + field_offset`, where `obj`
8069 // is a register and `field_offset` is a register pair (of which
8070 // only the lower half is used). Thus `offset` and `scale_factor`
8071 // above are expected to be null in this code path.
Roland Levillainba650a42017-03-06 13:52:32 +00008072 DCHECK_EQ(offset, 0u);
8073 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Roland Levillain54f869e2017-03-06 13:54:11 +00008074 Location field_offset = index;
8075 slow_path =
8076 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(
8077 instruction,
8078 ref,
8079 obj,
8080 offset,
8081 /* index */ field_offset,
8082 scale_factor,
8083 needs_null_check,
8084 temp_reg,
8085 *temp2,
8086 /* entrypoint */ temp3);
Roland Levillainba650a42017-03-06 13:52:32 +00008087 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00008088 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM(
8089 instruction,
8090 ref,
8091 obj,
8092 offset,
8093 index,
8094 scale_factor,
8095 needs_null_check,
8096 temp_reg,
8097 /* entrypoint */ temp3);
Roland Levillain35345a52017-02-27 14:32:08 +00008098 }
Roland Levillainba650a42017-03-06 13:52:32 +00008099 AddSlowPath(slow_path);
Roland Levillain35345a52017-02-27 14:32:08 +00008100
Roland Levillainba650a42017-03-06 13:52:32 +00008101 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8102 const int32_t entry_point_offset =
8103 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8104 // Loading the entrypoint does not require a load acquire since it is only changed when
8105 // threads are suspended or running a checkpoint.
8106 __ LoadFromOffset(kLoadWord, temp3.AsRegister<Register>(), TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008107 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8108 // checking GetIsGcMarking.
8109 __ CompareAndBranchIfNonZero(temp3.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00008110 // Fast path: just load the reference.
8111 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
Roland Levillainba650a42017-03-06 13:52:32 +00008112 __ Bind(slow_path->GetExitLabel());
8113}
Roland Levillain35345a52017-02-27 14:32:08 +00008114
Roland Levillainba650a42017-03-06 13:52:32 +00008115void CodeGeneratorARM::GenerateRawReferenceLoad(HInstruction* instruction,
8116 Location ref,
8117 Register obj,
8118 uint32_t offset,
8119 Location index,
8120 ScaleFactor scale_factor,
8121 bool needs_null_check) {
8122 Register ref_reg = ref.AsRegister<Register>();
8123
Roland Levillainc9285912015-12-18 10:38:42 +00008124 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008125 // Load types involving an "index": ArrayGet,
8126 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8127 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008128 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00008129 if (index.IsConstant()) {
8130 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01008131 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00008132 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
8133 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01008134 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008135 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8136 // intrinsics, which use a register pair as index ("long
8137 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01008138 Register index_reg = index.IsRegisterPair()
8139 ? index.AsRegisterPairLow<Register>()
8140 : index.AsRegister<Register>();
8141 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00008142 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
8143 }
8144 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008145 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillainc9285912015-12-18 10:38:42 +00008146 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
8147 }
8148
Roland Levillainba650a42017-03-06 13:52:32 +00008149 if (needs_null_check) {
8150 MaybeRecordImplicitNullCheck(instruction);
8151 }
8152
Roland Levillainc9285912015-12-18 10:38:42 +00008153 // Object* ref = ref_addr->AsMirrorPtr()
8154 __ MaybeUnpoisonHeapReference(ref_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00008155}
8156
8157void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
8158 Location out,
8159 Location ref,
8160 Location obj,
8161 uint32_t offset,
8162 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008163 DCHECK(kEmitCompilerReadBarrier);
8164
Roland Levillainc9285912015-12-18 10:38:42 +00008165 // Insert a slow path based read barrier *after* the reference load.
8166 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008167 // If heap poisoning is enabled, the unpoisoning of the loaded
8168 // reference will be carried out by the runtime within the slow
8169 // path.
8170 //
8171 // Note that `ref` currently does not get unpoisoned (when heap
8172 // poisoning is enabled), which is alright as the `ref` argument is
8173 // not used by the artReadBarrierSlow entry point.
8174 //
8175 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008176 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00008177 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
8178 AddSlowPath(slow_path);
8179
Roland Levillain3b359c72015-11-17 19:35:12 +00008180 __ b(slow_path->GetEntryLabel());
8181 __ Bind(slow_path->GetExitLabel());
8182}
8183
Roland Levillainc9285912015-12-18 10:38:42 +00008184void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8185 Location out,
8186 Location ref,
8187 Location obj,
8188 uint32_t offset,
8189 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008190 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00008191 // Baker's read barriers shall be handled by the fast path
8192 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
8193 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00008194 // If heap poisoning is enabled, unpoisoning will be taken care of
8195 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00008196 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00008197 } else if (kPoisonHeapReferences) {
8198 __ UnpoisonHeapReference(out.AsRegister<Register>());
8199 }
8200}
8201
Roland Levillainc9285912015-12-18 10:38:42 +00008202void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8203 Location out,
8204 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008205 DCHECK(kEmitCompilerReadBarrier);
8206
Roland Levillainc9285912015-12-18 10:38:42 +00008207 // Insert a slow path based read barrier *after* the GC root load.
8208 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008209 // Note that GC roots are not affected by heap poisoning, so we do
8210 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008211 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00008212 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
8213 AddSlowPath(slow_path);
8214
Roland Levillain3b359c72015-11-17 19:35:12 +00008215 __ b(slow_path->GetEntryLabel());
8216 __ Bind(slow_path->GetExitLabel());
8217}
8218
Vladimir Markodc151b22015-10-15 18:02:30 +01008219HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
8220 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008221 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008222 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01008223}
8224
Vladimir Markob4536b72015-11-24 13:45:23 +00008225Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
8226 Register temp) {
8227 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8228 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8229 if (!invoke->GetLocations()->Intrinsified()) {
8230 return location.AsRegister<Register>();
8231 }
8232 // For intrinsics we allow any location, so it may be on the stack.
8233 if (!location.IsRegister()) {
8234 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
8235 return temp;
8236 }
8237 // For register locations, check if the register was saved. If so, get it from the stack.
8238 // Note: There is a chance that the register was saved but not overwritten, so we could
8239 // save one load. However, since this is just an intrinsic slow path we prefer this
8240 // simple and more robust approach rather that trying to determine if that's the case.
8241 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08008242 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00008243 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
8244 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
8245 return temp;
8246 }
8247 return location.AsRegister<Register>();
8248}
8249
TatWai Chongd8c052a2016-11-02 16:12:48 +08008250Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
8251 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00008252 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
8253 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008254 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8255 uint32_t offset =
8256 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00008257 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008258 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00008259 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008260 }
Vladimir Marko58155012015-08-19 12:49:41 +00008261 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00008262 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008263 break;
8264 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8265 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
8266 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00008267 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
8268 HArmDexCacheArraysBase* base =
8269 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
8270 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
8271 temp.AsRegister<Register>());
8272 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
8273 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
8274 break;
8275 }
Vladimir Marko58155012015-08-19 12:49:41 +00008276 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00008277 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008278 Register method_reg;
8279 Register reg = temp.AsRegister<Register>();
8280 if (current_method.IsRegister()) {
8281 method_reg = current_method.AsRegister<Register>();
8282 } else {
8283 DCHECK(invoke->GetLocations()->Intrinsified());
8284 DCHECK(!current_method.IsValid());
8285 method_reg = reg;
8286 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
8287 }
Roland Levillain3b359c72015-11-17 19:35:12 +00008288 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
8289 __ LoadFromOffset(kLoadWord,
8290 reg,
8291 method_reg,
8292 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01008293 // temp = temp[index_in_cache];
8294 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
8295 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00008296 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
8297 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01008298 }
Vladimir Marko58155012015-08-19 12:49:41 +00008299 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08008300 return callee_method;
8301}
8302
8303void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
8304 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00008305
8306 switch (invoke->GetCodePtrLocation()) {
8307 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
8308 __ bl(GetFrameEntryLabel());
8309 break;
Vladimir Marko58155012015-08-19 12:49:41 +00008310 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8311 // LR = callee_method->entry_point_from_quick_compiled_code_
8312 __ LoadFromOffset(
8313 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07008314 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00008315 // LR()
8316 __ blx(LR);
8317 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08008318 }
8319
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08008320 DCHECK(!IsLeafMethod());
8321}
8322
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008323void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
8324 Register temp = temp_location.AsRegister<Register>();
8325 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8326 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00008327
8328 // Use the calling convention instead of the location of the receiver, as
8329 // intrinsics may have put the receiver in a different register. In the intrinsics
8330 // slow path, the arguments have been moved to the right place, so here we are
8331 // guaranteed that the receiver is the first register of the calling convention.
8332 InvokeDexCallingConvention calling_convention;
8333 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008334 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00008335 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00008336 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008337 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00008338 // Instead of simply (possibly) unpoisoning `temp` here, we should
8339 // emit a read barrier for the previous class reference load.
8340 // However this is not required in practice, as this is an
8341 // intermediate/temporary reference and because the current
8342 // concurrent copying collector keeps the from-space memory
8343 // intact/accessible until the end of the marking phase (the
8344 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008345 __ MaybeUnpoisonHeapReference(temp);
8346 // temp = temp->GetMethodAt(method_offset);
8347 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07008348 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008349 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
8350 // LR = temp->GetEntryPoint();
8351 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
8352 // LR();
8353 __ blx(LR);
8354}
8355
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008356CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008357 const DexFile& dex_file, dex::StringIndex string_index) {
8358 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008359}
8360
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008361CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08008362 const DexFile& dex_file, dex::TypeIndex type_index) {
8363 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008364}
8365
Vladimir Marko1998cd02017-01-13 13:02:58 +00008366CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
8367 const DexFile& dex_file, dex::TypeIndex type_index) {
8368 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8369}
8370
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008371CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
8372 const DexFile& dex_file, uint32_t element_offset) {
8373 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
8374}
8375
8376CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
8377 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8378 patches->emplace_back(dex_file, offset_or_index);
8379 return &patches->back();
8380}
8381
8382Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008383 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008384 return boot_image_string_patches_.GetOrCreate(
8385 StringReference(&dex_file, string_index),
8386 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8387}
8388
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008389Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08008390 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008391 return boot_image_type_patches_.GetOrCreate(
8392 TypeReference(&dex_file, type_index),
8393 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8394}
8395
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008396Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00008397 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008398}
8399
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008400Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008401 dex::StringIndex string_index,
8402 Handle<mirror::String> handle) {
8403 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
8404 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008405 return jit_string_patches_.GetOrCreate(
8406 StringReference(&dex_file, string_index),
8407 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8408}
8409
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008410Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
8411 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008412 Handle<mirror::Class> handle) {
8413 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
8414 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008415 return jit_class_patches_.GetOrCreate(
8416 TypeReference(&dex_file, type_index),
8417 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8418}
8419
Vladimir Markoaad75c62016-10-03 08:46:48 +00008420template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
8421inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
8422 const ArenaDeque<PcRelativePatchInfo>& infos,
8423 ArenaVector<LinkerPatch>* linker_patches) {
8424 for (const PcRelativePatchInfo& info : infos) {
8425 const DexFile& dex_file = info.target_dex_file;
8426 size_t offset_or_index = info.offset_or_index;
8427 DCHECK(info.add_pc_label.IsBound());
8428 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
8429 // Add MOVW patch.
8430 DCHECK(info.movw_label.IsBound());
8431 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
8432 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
8433 // Add MOVT patch.
8434 DCHECK(info.movt_label.IsBound());
8435 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
8436 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
8437 }
8438}
8439
Vladimir Marko58155012015-08-19 12:49:41 +00008440void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
8441 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00008442 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00008443 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008444 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008445 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008446 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008447 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00008448 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00008449 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008450 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
8451 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008452 for (const auto& entry : boot_image_string_patches_) {
8453 const StringReference& target_string = entry.first;
8454 Literal* literal = entry.second;
8455 DCHECK(literal->GetLabel()->IsBound());
8456 uint32_t literal_offset = literal->GetLabel()->Position();
8457 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
8458 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008459 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008460 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00008461 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00008462 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00008463 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
8464 linker_patches);
8465 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008466 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
8467 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008468 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
8469 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008470 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008471 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
8472 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008473 for (const auto& entry : boot_image_type_patches_) {
8474 const TypeReference& target_type = entry.first;
8475 Literal* literal = entry.second;
8476 DCHECK(literal->GetLabel()->IsBound());
8477 uint32_t literal_offset = literal->GetLabel()->Position();
8478 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
8479 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08008480 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008481 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008482 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008483}
8484
8485Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
8486 return map->GetOrCreate(
8487 value,
8488 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00008489}
8490
8491Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
8492 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008493 return map->GetOrCreate(
8494 target_method,
8495 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00008496}
8497
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03008498void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8499 LocationSummary* locations =
8500 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
8501 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
8502 Location::RequiresRegister());
8503 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
8504 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
8505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8506}
8507
8508void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8509 LocationSummary* locations = instr->GetLocations();
8510 Register res = locations->Out().AsRegister<Register>();
8511 Register accumulator =
8512 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
8513 Register mul_left =
8514 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
8515 Register mul_right =
8516 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
8517
8518 if (instr->GetOpKind() == HInstruction::kAdd) {
8519 __ mla(res, mul_left, mul_right, accumulator);
8520 } else {
8521 __ mls(res, mul_left, mul_right, accumulator);
8522 }
8523}
8524
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008525void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008526 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008527 LOG(FATAL) << "Unreachable";
8528}
8529
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008530void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008531 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008532 LOG(FATAL) << "Unreachable";
8533}
8534
Mark Mendellfe57faa2015-09-18 09:26:15 -04008535// Simple implementation of packed switch - generate cascaded compare/jumps.
8536void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8537 LocationSummary* locations =
8538 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8539 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008540 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008541 codegen_->GetAssembler()->IsThumb()) {
8542 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
8543 if (switch_instr->GetStartValue() != 0) {
8544 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
8545 }
8546 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008547}
8548
8549void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8550 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008551 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04008552 LocationSummary* locations = switch_instr->GetLocations();
8553 Register value_reg = locations->InAt(0).AsRegister<Register>();
8554 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8555
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008556 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008557 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008558 Register temp_reg = IP;
8559 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
8560 // the immediate, because IP is used as the destination register. For the other
8561 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
8562 // and they can be encoded in the instruction without making use of IP register.
8563 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
8564
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008565 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008566 // Jump to successors[0] if value == lower_bound.
8567 __ b(codegen_->GetLabelOf(successors[0]), EQ);
8568 int32_t last_index = 0;
8569 for (; num_entries - last_index > 2; last_index += 2) {
8570 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
8571 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8572 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
8573 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8574 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
8575 }
8576 if (num_entries - last_index == 2) {
8577 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00008578 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008579 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008580 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008581
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008582 // And the default for any other value.
8583 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
8584 __ b(codegen_->GetLabelOf(default_block));
8585 }
8586 } else {
8587 // Create a table lookup.
8588 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8589
8590 // Materialize a pointer to the switch table
8591 std::vector<Label*> labels(num_entries);
8592 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
8593 for (uint32_t i = 0; i < num_entries; i++) {
8594 labels[i] = codegen_->GetLabelOf(successors[i]);
8595 }
8596 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
8597
8598 // Remove the bias.
8599 Register key_reg;
8600 if (lower_bound != 0) {
8601 key_reg = locations->GetTemp(1).AsRegister<Register>();
8602 __ AddConstant(key_reg, value_reg, -lower_bound);
8603 } else {
8604 key_reg = value_reg;
8605 }
8606
8607 // Check whether the value is in the table, jump to default block if not.
8608 __ CmpConstant(key_reg, num_entries - 1);
8609 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
8610
8611 // Load the displacement from the table.
8612 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
8613
8614 // Dispatch is a direct add to the PC (for Thumb2).
8615 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008616 }
8617}
8618
Vladimir Markob4536b72015-11-24 13:45:23 +00008619void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8620 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8621 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00008622}
8623
8624void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8625 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008626 CodeGeneratorARM::PcRelativePatchInfo* labels =
8627 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00008628 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008629 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008630 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008631 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008632 __ BindTrackedLabel(&labels->add_pc_label);
8633 __ add(base_reg, base_reg, ShifterOperand(PC));
8634}
8635
Andreas Gampe85b62f22015-09-09 13:15:38 -07008636void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
8637 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00008638 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008639 return;
8640 }
8641
8642 DCHECK_NE(type, Primitive::kPrimVoid);
8643
8644 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
8645 if (return_loc.Equals(trg)) {
8646 return;
8647 }
8648
8649 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8650 // with the last branch.
8651 if (type == Primitive::kPrimLong) {
8652 HParallelMove parallel_move(GetGraph()->GetArena());
8653 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
8654 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
8655 GetMoveResolver()->EmitNativeCode(&parallel_move);
8656 } else if (type == Primitive::kPrimDouble) {
8657 HParallelMove parallel_move(GetGraph()->GetArena());
8658 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
8659 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
8660 GetMoveResolver()->EmitNativeCode(&parallel_move);
8661 } else {
8662 // Let the parallel move resolver take care of all of this.
8663 HParallelMove parallel_move(GetGraph()->GetArena());
8664 parallel_move.AddMove(return_loc, trg, type, nullptr);
8665 GetMoveResolver()->EmitNativeCode(&parallel_move);
8666 }
8667}
8668
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008669void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
8670 LocationSummary* locations =
8671 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8672 locations->SetInAt(0, Location::RequiresRegister());
8673 locations->SetOut(Location::RequiresRegister());
8674}
8675
8676void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
8677 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008678 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008679 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008680 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008681 __ LoadFromOffset(kLoadWord,
8682 locations->Out().AsRegister<Register>(),
8683 locations->InAt(0).AsRegister<Register>(),
8684 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008685 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008686 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008687 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008688 __ LoadFromOffset(kLoadWord,
8689 locations->Out().AsRegister<Register>(),
8690 locations->InAt(0).AsRegister<Register>(),
8691 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
8692 __ LoadFromOffset(kLoadWord,
8693 locations->Out().AsRegister<Register>(),
8694 locations->Out().AsRegister<Register>(),
8695 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008696 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008697}
8698
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008699static void PatchJitRootUse(uint8_t* code,
8700 const uint8_t* roots_data,
8701 Literal* literal,
8702 uint64_t index_in_table) {
8703 DCHECK(literal->GetLabel()->IsBound());
8704 uint32_t literal_offset = literal->GetLabel()->Position();
8705 uintptr_t address =
8706 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8707 uint8_t* data = code + literal_offset;
8708 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
8709}
8710
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008711void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8712 for (const auto& entry : jit_string_patches_) {
8713 const auto& it = jit_string_roots_.find(entry.first);
8714 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008715 PatchJitRootUse(code, roots_data, entry.second, it->second);
8716 }
8717 for (const auto& entry : jit_class_patches_) {
8718 const auto& it = jit_class_roots_.find(entry.first);
8719 DCHECK(it != jit_class_roots_.end());
8720 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008721 }
8722}
8723
Roland Levillain4d027112015-07-01 15:41:14 +01008724#undef __
8725#undef QUICK_ENTRY_POINT
8726
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008727} // namespace arm
8728} // namespace art