blob: e916fcec8c20167bdd519c24b46236e15c1df406 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Anton Kirilov74234da2017-01-13 14:42:47 +000022#include "common_arm.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070024#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080026#include "intrinsics.h"
27#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070030#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031#include "utils/arm/assembler_arm.h"
32#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000033#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037
Roland Levillain3b359c72015-11-17 19:35:12 +000038template<class MirrorType>
39class GcRoot;
40
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041namespace arm {
42
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000043static bool ExpectedPairLayout(Location location) {
44 // We expected this for both core and fpu register pairs.
45 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
46}
47
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010049static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050
David Brazdil58282f42016-01-14 12:45:10 +000051static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000052static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070053 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000054static constexpr SRegister kFpuCalleeSaves[] =
55 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000057// D31 cannot be split into two S registers, and the register allocator only works on
58// S registers. Therefore there is no need to block it.
59static constexpr DRegister DTMP = D31;
60
Vladimir Markof3e0ee22015-12-17 15:23:13 +000061static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070062
Roland Levillain7cbd27f2016-08-11 23:53:33 +010063// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
64#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070065#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066
Artem Serovf4d6aee2016-07-11 10:41:45 +010067static constexpr int kRegListThreshold = 4;
68
Artem Serovd300d8f2016-07-15 14:00:56 +010069// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
70// for each live D registers they treat two corresponding S registers as live ones.
71//
72// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
73// from a list of contiguous S registers a list of contiguous D registers (processing first/last
74// S registers corner cases) and save/restore this new list treating them as D registers.
75// - decreasing code size
76// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
77// restored and then used in regular non SlowPath code as D register.
78//
79// For the following example (v means the S register is live):
80// D names: | D0 | D1 | D2 | D4 | ...
81// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
82// Live? | | v | v | v | v | v | v | | ...
83//
84// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
85// as D registers.
86static size_t SaveContiguousSRegisterList(size_t first,
87 size_t last,
88 CodeGenerator* codegen,
89 size_t stack_offset) {
90 DCHECK_LE(first, last);
91 if ((first == last) && (first == 0)) {
92 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first);
93 return stack_offset;
94 }
95 if (first % 2 == 1) {
96 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first++);
97 }
98
99 bool save_last = false;
100 if (last % 2 == 0) {
101 save_last = true;
102 --last;
103 }
104
105 if (first < last) {
106 DRegister d_reg = static_cast<DRegister>(first / 2);
107 DCHECK_EQ((last - first + 1) % 2, 0u);
108 size_t number_of_d_regs = (last - first + 1) / 2;
109
110 if (number_of_d_regs == 1) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100111 __ StoreDToOffset(d_reg, SP, stack_offset);
Artem Serovd300d8f2016-07-15 14:00:56 +0100112 } else if (number_of_d_regs > 1) {
113 __ add(IP, SP, ShifterOperand(stack_offset));
114 __ vstmiad(IP, d_reg, number_of_d_regs);
115 }
116 stack_offset += number_of_d_regs * kArmWordSize * 2;
117 }
118
119 if (save_last) {
120 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, last + 1);
121 }
122
123 return stack_offset;
124}
125
126static size_t RestoreContiguousSRegisterList(size_t first,
127 size_t last,
128 CodeGenerator* codegen,
129 size_t stack_offset) {
130 DCHECK_LE(first, last);
131 if ((first == last) && (first == 0)) {
132 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first);
133 return stack_offset;
134 }
135 if (first % 2 == 1) {
136 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first++);
137 }
138
139 bool restore_last = false;
140 if (last % 2 == 0) {
141 restore_last = true;
142 --last;
143 }
144
145 if (first < last) {
146 DRegister d_reg = static_cast<DRegister>(first / 2);
147 DCHECK_EQ((last - first + 1) % 2, 0u);
148 size_t number_of_d_regs = (last - first + 1) / 2;
149 if (number_of_d_regs == 1) {
150 __ LoadDFromOffset(d_reg, SP, stack_offset);
151 } else if (number_of_d_regs > 1) {
152 __ add(IP, SP, ShifterOperand(stack_offset));
153 __ vldmiad(IP, d_reg, number_of_d_regs);
154 }
155 stack_offset += number_of_d_regs * kArmWordSize * 2;
156 }
157
158 if (restore_last) {
159 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, last + 1);
160 }
161
162 return stack_offset;
163}
164
Artem Serovf4d6aee2016-07-11 10:41:45 +0100165void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
166 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
167 size_t orig_offset = stack_offset;
168
169 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
170 for (uint32_t i : LowToHighBits(core_spills)) {
171 // If the register holds an object, update the stack mask.
172 if (locations->RegisterContainsObject(i)) {
173 locations->SetStackBit(stack_offset / kVRegSize);
174 }
175 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
176 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
177 saved_core_stack_offsets_[i] = stack_offset;
178 stack_offset += kArmWordSize;
179 }
180
181 int reg_num = POPCOUNT(core_spills);
182 if (reg_num != 0) {
183 if (reg_num > kRegListThreshold) {
184 __ StoreList(RegList(core_spills), orig_offset);
185 } else {
186 stack_offset = orig_offset;
187 for (uint32_t i : LowToHighBits(core_spills)) {
188 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
189 }
190 }
191 }
192
Artem Serovd300d8f2016-07-15 14:00:56 +0100193 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
194 orig_offset = stack_offset;
Vladimir Marko804b03f2016-09-14 16:26:36 +0100195 for (uint32_t i : LowToHighBits(fp_spills)) {
Artem Serovf4d6aee2016-07-11 10:41:45 +0100196 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
197 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serovd300d8f2016-07-15 14:00:56 +0100198 stack_offset += kArmWordSize;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100199 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100200
201 stack_offset = orig_offset;
202 while (fp_spills != 0u) {
203 uint32_t begin = CTZ(fp_spills);
204 uint32_t tmp = fp_spills + (1u << begin);
205 fp_spills &= tmp; // Clear the contiguous range of 1s.
206 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
207 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
208 }
209 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100210}
211
212void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
213 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
214 size_t orig_offset = stack_offset;
215
216 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
217 for (uint32_t i : LowToHighBits(core_spills)) {
218 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
219 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
220 stack_offset += kArmWordSize;
221 }
222
223 int reg_num = POPCOUNT(core_spills);
224 if (reg_num != 0) {
225 if (reg_num > kRegListThreshold) {
226 __ LoadList(RegList(core_spills), orig_offset);
227 } else {
228 stack_offset = orig_offset;
229 for (uint32_t i : LowToHighBits(core_spills)) {
230 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
231 }
232 }
233 }
234
Artem Serovd300d8f2016-07-15 14:00:56 +0100235 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
236 while (fp_spills != 0u) {
237 uint32_t begin = CTZ(fp_spills);
238 uint32_t tmp = fp_spills + (1u << begin);
239 fp_spills &= tmp; // Clear the contiguous range of 1s.
240 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
241 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
Artem Serovf4d6aee2016-07-11 10:41:45 +0100242 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100243 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100244}
245
246class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100247 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100248 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100249
Alexandre Rames67555f72014-11-18 10:55:16 +0000250 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100251 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100252 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000253 if (instruction_->CanThrowIntoCatchBlock()) {
254 // Live registers will be restored in the catch block if caught.
255 SaveLiveRegisters(codegen, instruction_->GetLocations());
256 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100257 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
258 instruction_,
259 instruction_->GetDexPc(),
260 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000261 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100262 }
263
Alexandre Rames8158f282015-08-07 10:26:17 +0100264 bool IsFatal() const OVERRIDE { return true; }
265
Alexandre Rames9931f312015-06-19 14:47:01 +0100266 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
267
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100268 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100269 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
270};
271
Artem Serovf4d6aee2016-07-11 10:41:45 +0100272class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000273 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100274 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000275
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000277 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
278 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100279 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000280 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000281 }
282
Alexandre Rames8158f282015-08-07 10:26:17 +0100283 bool IsFatal() const OVERRIDE { return true; }
284
Alexandre Rames9931f312015-06-19 14:47:01 +0100285 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
286
Calin Juravled0d48522014-11-04 16:40:20 +0000287 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000288 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
289};
290
Artem Serovf4d6aee2016-07-11 10:41:45 +0100291class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000292 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000293 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100294 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000295
Alexandre Rames67555f72014-11-18 10:55:16 +0000296 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100297 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000298 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100299 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000300 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100301 if (successor_ == nullptr) {
302 __ b(GetReturnLabel());
303 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100304 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100305 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000306 }
307
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100308 Label* GetReturnLabel() {
309 DCHECK(successor_ == nullptr);
310 return &return_label_;
311 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000312
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100313 HBasicBlock* GetSuccessor() const {
314 return successor_;
315 }
316
Alexandre Rames9931f312015-06-19 14:47:01 +0100317 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
318
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000319 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100320 // If not null, the block to branch to after the suspend check.
321 HBasicBlock* const successor_;
322
323 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000324 Label return_label_;
325
326 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
327};
328
Artem Serovf4d6aee2016-07-11 10:41:45 +0100329class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100330 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100331 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100332 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100333
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100335 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100336 LocationSummary* locations = instruction_->GetLocations();
337
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100338 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000339 if (instruction_->CanThrowIntoCatchBlock()) {
340 // Live registers will be restored in the catch block if caught.
341 SaveLiveRegisters(codegen, instruction_->GetLocations());
342 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000343 // We're moving two locations to locations that could overlap, so we need a parallel
344 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100345 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000346 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000348 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100349 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100350 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100351 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
352 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100353 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
354 ? kQuickThrowStringBounds
355 : kQuickThrowArrayBounds;
356 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100357 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000358 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100359 }
360
Alexandre Rames8158f282015-08-07 10:26:17 +0100361 bool IsFatal() const OVERRIDE { return true; }
362
Alexandre Rames9931f312015-06-19 14:47:01 +0100363 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
364
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100365 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100366 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
367};
368
Artem Serovf4d6aee2016-07-11 10:41:45 +0100369class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100370 public:
Vladimir Markoea4c1262017-02-06 19:59:33 +0000371 LoadClassSlowPathARM(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000372 : SlowPathCodeARM(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000373 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
374 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100375
Alexandre Rames67555f72014-11-18 10:55:16 +0000376 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000377 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000378 Location out = locations->Out();
379 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000380
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100381 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
382 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000383 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100385 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000386 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
387 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
388 bool is_load_class_bss_entry =
389 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
390 Register entry_address = kNoRegister;
391 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
392 Register temp = locations->GetTemp(0).AsRegister<Register>();
393 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
394 // the kSaveEverything call.
395 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
396 entry_address = temp_is_r0 ? out.AsRegister<Register>() : temp;
397 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
398 if (temp_is_r0) {
399 __ mov(entry_address, ShifterOperand(temp));
400 }
401 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000402 dex::TypeIndex type_index = cls_->GetTypeIndex();
403 __ LoadImmediate(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100404 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
405 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000406 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000407 if (do_clinit_) {
408 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
409 } else {
410 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
411 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000412
Vladimir Markoea4c1262017-02-06 19:59:33 +0000413 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
414 if (is_load_class_bss_entry) {
415 if (call_saves_everything_except_r0) {
416 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
417 __ str(R0, Address(entry_address));
418 } else {
419 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
420 Register temp = IP;
421 CodeGeneratorARM::PcRelativePatchInfo* labels =
422 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
423 __ BindTrackedLabel(&labels->movw_label);
424 __ movw(temp, /* placeholder */ 0u);
425 __ BindTrackedLabel(&labels->movt_label);
426 __ movt(temp, /* placeholder */ 0u);
427 __ BindTrackedLabel(&labels->add_pc_label);
428 __ add(temp, temp, ShifterOperand(PC));
429 __ str(R0, Address(temp));
430 }
431 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000432 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000433 if (out.IsValid()) {
434 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000435 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
436 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000437 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100438 __ b(GetExitLabel());
439 }
440
Alexandre Rames9931f312015-06-19 14:47:01 +0100441 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
442
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100443 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000444 // The class this slow path will load.
445 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100446
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000447 // The dex PC of `at_`.
448 const uint32_t dex_pc_;
449
450 // Whether to initialize the class.
451 const bool do_clinit_;
452
453 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100454};
455
Vladimir Markoaad75c62016-10-03 08:46:48 +0000456class LoadStringSlowPathARM : public SlowPathCodeARM {
457 public:
458 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
459
460 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000461 DCHECK(instruction_->IsLoadString());
462 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000463 LocationSummary* locations = instruction_->GetLocations();
464 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100465 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000466 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100467 Register out = locations->Out().AsRegister<Register>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100468 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000469
470 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
471 __ Bind(GetEntryLabel());
472 SaveLiveRegisters(codegen, locations);
473
474 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100475 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000476 // the kSaveEverything call.
477 Register entry_address = kNoRegister;
478 if (call_saves_everything_except_r0) {
479 Register temp = locations->GetTemp(0).AsRegister<Register>();
480 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
481 entry_address = temp_is_r0 ? out : temp;
482 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
483 if (temp_is_r0) {
484 __ mov(entry_address, ShifterOperand(temp));
485 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100486 }
487
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000488 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000489 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
490 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100491
492 // Store the resolved String to the .bss entry.
493 if (call_saves_everything_except_r0) {
494 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
495 __ str(R0, Address(entry_address));
496 } else {
497 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000498 Register temp = IP;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100499 CodeGeneratorARM::PcRelativePatchInfo* labels =
500 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
501 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000502 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100503 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000504 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100505 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000506 __ add(temp, temp, ShifterOperand(PC));
507 __ str(R0, Address(temp));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100508 }
509
Vladimir Markoaad75c62016-10-03 08:46:48 +0000510 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000511 RestoreLiveRegisters(codegen, locations);
512
Vladimir Markoaad75c62016-10-03 08:46:48 +0000513 __ b(GetExitLabel());
514 }
515
516 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
517
518 private:
519 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
520};
521
Artem Serovf4d6aee2016-07-11 10:41:45 +0100522class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000523 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000524 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100525 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000526
Alexandre Rames67555f72014-11-18 10:55:16 +0000527 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000528 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000529 DCHECK(instruction_->IsCheckCast()
530 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000531
532 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
533 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000534
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000535 if (!is_fatal_) {
536 SaveLiveRegisters(codegen, locations);
537 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000538
539 // We're moving two locations to locations that could overlap, so we need a parallel
540 // move resolver.
541 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800542 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800543 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
544 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800545 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800546 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
547 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000548 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100549 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100550 instruction_,
551 instruction_->GetDexPc(),
552 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800553 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000554 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
555 } else {
556 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800557 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
558 instruction_,
559 instruction_->GetDexPc(),
560 this);
561 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000562 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000563
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000564 if (!is_fatal_) {
565 RestoreLiveRegisters(codegen, locations);
566 __ b(GetExitLabel());
567 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000568 }
569
Alexandre Rames9931f312015-06-19 14:47:01 +0100570 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
571
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000572 bool IsFatal() const OVERRIDE { return is_fatal_; }
573
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000574 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000575 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000576
577 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
578};
579
Artem Serovf4d6aee2016-07-11 10:41:45 +0100580class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700581 public:
Aart Bik42249c32016-01-07 15:33:50 -0800582 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100583 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700584
585 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800586 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700587 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100588 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000589 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700590 }
591
Alexandre Rames9931f312015-06-19 14:47:01 +0100592 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
593
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700594 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700595 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
596};
597
Artem Serovf4d6aee2016-07-11 10:41:45 +0100598class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100599 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100600 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100601
602 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
603 LocationSummary* locations = instruction_->GetLocations();
604 __ Bind(GetEntryLabel());
605 SaveLiveRegisters(codegen, locations);
606
607 InvokeRuntimeCallingConvention calling_convention;
608 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
609 parallel_move.AddMove(
610 locations->InAt(0),
611 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
612 Primitive::kPrimNot,
613 nullptr);
614 parallel_move.AddMove(
615 locations->InAt(1),
616 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
617 Primitive::kPrimInt,
618 nullptr);
619 parallel_move.AddMove(
620 locations->InAt(2),
621 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
622 Primitive::kPrimNot,
623 nullptr);
624 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
625
626 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100627 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000628 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100629 RestoreLiveRegisters(codegen, locations);
630 __ b(GetExitLabel());
631 }
632
633 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
634
635 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100636 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
637};
638
Roland Levillain47b3ab22017-02-27 14:31:35 +0000639// Slow path marking an object reference `ref` during a read
640// barrier. The field `obj.field` in the object `obj` holding this
641// reference does not get updated by this slow path after marking (see
642// ReadBarrierMarkAndUpdateFieldSlowPathARM below for that).
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000643//
Roland Levillain47b3ab22017-02-27 14:31:35 +0000644// This means that after the execution of this slow path, `ref` will
645// always be up-to-date, but `obj.field` may not; i.e., after the
646// flip, `ref` will be a to-space reference, but `obj.field` will
647// probably still be a from-space reference (unless it gets updated by
648// another thread, or if another thread installed another object
649// reference (different from `ref`) in `obj.field`).
650//
651// If `entrypoint` is a valid location it is assumed to already be
652// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillain35345a52017-02-27 14:32:08 +0000653// is for the GcRoot read barrier.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000654class ReadBarrierMarkSlowPathARM : public SlowPathCodeARM {
655 public:
656 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
657 Location ref,
658 Location entrypoint = Location::NoLocation())
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000659 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
660 DCHECK(kEmitCompilerReadBarrier);
661 }
662
Roland Levillain47b3ab22017-02-27 14:31:35 +0000663 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000664
Roland Levillain47b3ab22017-02-27 14:31:35 +0000665 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
666 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000667 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain47b3ab22017-02-27 14:31:35 +0000668 DCHECK(locations->CanCall());
669 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
670 DCHECK(instruction_->IsInstanceFieldGet() ||
671 instruction_->IsStaticFieldGet() ||
672 instruction_->IsArrayGet() ||
673 instruction_->IsArraySet() ||
674 instruction_->IsLoadClass() ||
675 instruction_->IsLoadString() ||
676 instruction_->IsInstanceOf() ||
677 instruction_->IsCheckCast() ||
678 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
679 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
680 << "Unexpected instruction in read barrier marking slow path: "
681 << instruction_->DebugName();
682 // The read barrier instrumentation of object ArrayGet
683 // instructions does not support the HIntermediateAddress
684 // instruction.
685 DCHECK(!(instruction_->IsArrayGet() &&
686 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
687
688 __ Bind(GetEntryLabel());
689 // No need to save live registers; it's taken care of by the
690 // entrypoint. Also, there is no need to update the stack mask,
691 // as this runtime call will not trigger a garbage collection.
692 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
693 DCHECK_NE(ref_reg, SP);
694 DCHECK_NE(ref_reg, LR);
695 DCHECK_NE(ref_reg, PC);
696 // IP is used internally by the ReadBarrierMarkRegX entry point
697 // as a temporary, it cannot be the entry point's input/output.
698 DCHECK_NE(ref_reg, IP);
699 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
700 // "Compact" slow path, saving two moves.
701 //
702 // Instead of using the standard runtime calling convention (input
703 // and output in R0):
704 //
705 // R0 <- ref
706 // R0 <- ReadBarrierMark(R0)
707 // ref <- R0
708 //
709 // we just use rX (the register containing `ref`) as input and output
710 // of a dedicated entrypoint:
711 //
712 // rX <- ReadBarrierMarkRegX(rX)
713 //
714 if (entrypoint_.IsValid()) {
715 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
716 __ blx(entrypoint_.AsRegister<Register>());
717 } else {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000718 int32_t entry_point_offset =
719 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
720 // This runtime call does not require a stack map.
721 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
722 }
723 __ b(GetExitLabel());
724 }
725
726 private:
727 // The location (register) of the marked object reference.
728 const Location ref_;
729
730 // The location of the entrypoint if already loaded.
731 const Location entrypoint_;
732
733 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
734};
735
736// Slow path marking an object reference `ref` during a read barrier,
737// and if needed, atomically updating the field `obj.field` in the
738// object `obj` holding this reference after marking (contrary to
739// ReadBarrierMarkSlowPathARM above, which never tries to update
740// `obj.field`).
741//
742// This means that after the execution of this slow path, both `ref`
743// and `obj.field` will be up-to-date; i.e., after the flip, both will
744// hold the same to-space reference (unless another thread installed
745// another object reference (different from `ref`) in `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000746class ReadBarrierMarkAndUpdateFieldSlowPathARM : public SlowPathCodeARM {
747 public:
748 ReadBarrierMarkAndUpdateFieldSlowPathARM(HInstruction* instruction,
749 Location ref,
750 Register obj,
751 Location field_offset,
752 Register temp1,
Roland Levillain35345a52017-02-27 14:32:08 +0000753 Register temp2)
Roland Levillain47b3ab22017-02-27 14:31:35 +0000754 : SlowPathCodeARM(instruction),
755 ref_(ref),
756 obj_(obj),
757 field_offset_(field_offset),
758 temp1_(temp1),
Roland Levillain35345a52017-02-27 14:32:08 +0000759 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000760 DCHECK(kEmitCompilerReadBarrier);
761 }
762
763 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathARM"; }
764
765 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
766 LocationSummary* locations = instruction_->GetLocations();
767 Register ref_reg = ref_.AsRegister<Register>();
768 DCHECK(locations->CanCall());
769 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
770 // This slow path is only used by the UnsafeCASObject intrinsic.
771 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
772 << "Unexpected instruction in read barrier marking and field updating slow path: "
773 << instruction_->DebugName();
774 DCHECK(instruction_->GetLocations()->Intrinsified());
775 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
776 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
777
778 __ Bind(GetEntryLabel());
779
780 // Save the old reference.
781 // Note that we cannot use IP to save the old reference, as IP is
782 // used internally by the ReadBarrierMarkRegX entry point, and we
783 // need the old reference after the call to that entry point.
784 DCHECK_NE(temp1_, IP);
785 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000786
787 // No need to save live registers; it's taken care of by the
788 // entrypoint. Also, there is no need to update the stack mask,
789 // as this runtime call will not trigger a garbage collection.
790 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
791 DCHECK_NE(ref_reg, SP);
792 DCHECK_NE(ref_reg, LR);
793 DCHECK_NE(ref_reg, PC);
794 // IP is used internally by the ReadBarrierMarkRegX entry point
795 // as a temporary, it cannot be the entry point's input/output.
796 DCHECK_NE(ref_reg, IP);
797 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
798 // "Compact" slow path, saving two moves.
799 //
800 // Instead of using the standard runtime calling convention (input
801 // and output in R0):
802 //
803 // R0 <- ref
804 // R0 <- ReadBarrierMark(R0)
805 // ref <- R0
806 //
807 // we just use rX (the register containing `ref`) as input and output
808 // of a dedicated entrypoint:
809 //
810 // rX <- ReadBarrierMarkRegX(rX)
811 //
Roland Levillain35345a52017-02-27 14:32:08 +0000812 int32_t entry_point_offset =
813 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
814 // This runtime call does not require a stack map.
815 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100816
817 // If the new reference is different from the old reference,
Roland Levillain47b3ab22017-02-27 14:31:35 +0000818 // update the field in the holder (`*(obj_ + field_offset_)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100819 //
820 // Note that this field could also hold a different object, if
821 // another thread had concurrently changed it. In that case, the
822 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
823 // (CAS) operation below would abort the CAS, leaving the field
824 // as-is.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000825 Label done;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100826 __ cmp(temp1_, ShifterOperand(ref_reg));
Roland Levillain47b3ab22017-02-27 14:31:35 +0000827 __ b(&done, EQ);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100828
829 // Update the the holder's field atomically. This may fail if
830 // mutator updates before us, but it's OK. This is achieved
831 // using a strong compare-and-set (CAS) operation with relaxed
832 // memory synchronization ordering, where the expected value is
833 // the old reference and the desired value is the new reference.
834
835 // Convenience aliases.
836 Register base = obj_;
837 // The UnsafeCASObject intrinsic uses a register pair as field
838 // offset ("long offset"), of which only the low part contains
839 // data.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000840 Register offset = field_offset_.AsRegisterPairLow<Register>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100841 Register expected = temp1_;
842 Register value = ref_reg;
843 Register tmp_ptr = IP; // Pointer to actual memory.
844 Register tmp = temp2_; // Value in memory.
845
846 __ add(tmp_ptr, base, ShifterOperand(offset));
847
848 if (kPoisonHeapReferences) {
849 __ PoisonHeapReference(expected);
850 if (value == expected) {
851 // Do not poison `value`, as it is the same register as
852 // `expected`, which has just been poisoned.
853 } else {
854 __ PoisonHeapReference(value);
855 }
856 }
857
858 // do {
859 // tmp = [r_ptr] - expected;
860 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
861
Roland Levillain24a4d112016-10-26 13:10:46 +0100862 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100863 __ Bind(&loop_head);
864
865 __ ldrex(tmp, tmp_ptr);
866
867 __ subs(tmp, tmp, ShifterOperand(expected));
868
Roland Levillain24a4d112016-10-26 13:10:46 +0100869 __ it(NE);
870 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100871
Roland Levillain24a4d112016-10-26 13:10:46 +0100872 __ b(&exit_loop, NE);
873
874 __ strex(tmp, value, tmp_ptr);
875 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100876 __ b(&loop_head, EQ);
877
Roland Levillain24a4d112016-10-26 13:10:46 +0100878 __ Bind(&exit_loop);
879
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100880 if (kPoisonHeapReferences) {
881 __ UnpoisonHeapReference(expected);
882 if (value == expected) {
883 // Do not unpoison `value`, as it is the same register as
884 // `expected`, which has just been unpoisoned.
885 } else {
886 __ UnpoisonHeapReference(value);
887 }
888 }
889
Roland Levillain47b3ab22017-02-27 14:31:35 +0000890 __ Bind(&done);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100891 __ b(GetExitLabel());
892 }
893
894 private:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000895 // The location (register) of the marked object reference.
896 const Location ref_;
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100897 // The register containing the object holding the marked object reference field.
898 const Register obj_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000899 // The location of the offset of the marked reference field within `obj_`.
900 Location field_offset_;
901
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100902 const Register temp1_;
903 const Register temp2_;
904
Roland Levillain47b3ab22017-02-27 14:31:35 +0000905 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathARM);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100906};
907
Roland Levillain3b359c72015-11-17 19:35:12 +0000908// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +0100909class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +0000910 public:
911 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
912 Location out,
913 Location ref,
914 Location obj,
915 uint32_t offset,
916 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100917 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +0000918 out_(out),
919 ref_(ref),
920 obj_(obj),
921 offset_(offset),
922 index_(index) {
923 DCHECK(kEmitCompilerReadBarrier);
924 // If `obj` is equal to `out` or `ref`, it means the initial object
925 // has been overwritten by (or after) the heap object reference load
926 // to be instrumented, e.g.:
927 //
928 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +0000929 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +0000930 //
931 // In that case, we have lost the information about the original
932 // object, and the emitted read barrier cannot work properly.
933 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
934 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
935 }
936
937 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
938 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
939 LocationSummary* locations = instruction_->GetLocations();
940 Register reg_out = out_.AsRegister<Register>();
941 DCHECK(locations->CanCall());
942 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100943 DCHECK(instruction_->IsInstanceFieldGet() ||
944 instruction_->IsStaticFieldGet() ||
945 instruction_->IsArrayGet() ||
946 instruction_->IsInstanceOf() ||
947 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100948 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +0000949 << "Unexpected instruction in read barrier for heap reference slow path: "
950 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +0000951 // The read barrier instrumentation of object ArrayGet
952 // instructions does not support the HIntermediateAddress
953 // instruction.
954 DCHECK(!(instruction_->IsArrayGet() &&
955 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +0000956
957 __ Bind(GetEntryLabel());
958 SaveLiveRegisters(codegen, locations);
959
960 // We may have to change the index's value, but as `index_` is a
961 // constant member (like other "inputs" of this slow path),
962 // introduce a copy of it, `index`.
963 Location index = index_;
964 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100965 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +0000966 if (instruction_->IsArrayGet()) {
967 // Compute the actual memory offset and store it in `index`.
968 Register index_reg = index_.AsRegister<Register>();
969 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
970 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
971 // We are about to change the value of `index_reg` (see the
972 // calls to art::arm::Thumb2Assembler::Lsl and
973 // art::arm::Thumb2Assembler::AddConstant below), but it has
974 // not been saved by the previous call to
975 // art::SlowPathCode::SaveLiveRegisters, as it is a
976 // callee-save register --
977 // art::SlowPathCode::SaveLiveRegisters does not consider
978 // callee-save registers, as it has been designed with the
979 // assumption that callee-save registers are supposed to be
980 // handled by the called function. So, as a callee-save
981 // register, `index_reg` _would_ eventually be saved onto
982 // the stack, but it would be too late: we would have
983 // changed its value earlier. Therefore, we manually save
984 // it here into another freely available register,
985 // `free_reg`, chosen of course among the caller-save
986 // registers (as a callee-save `free_reg` register would
987 // exhibit the same problem).
988 //
989 // Note we could have requested a temporary register from
990 // the register allocator instead; but we prefer not to, as
991 // this is a slow path, and we know we can find a
992 // caller-save register that is available.
993 Register free_reg = FindAvailableCallerSaveRegister(codegen);
994 __ Mov(free_reg, index_reg);
995 index_reg = free_reg;
996 index = Location::RegisterLocation(index_reg);
997 } else {
998 // The initial register stored in `index_` has already been
999 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1000 // (as it is not a callee-save register), so we can freely
1001 // use it.
1002 }
1003 // Shifting the index value contained in `index_reg` by the scale
1004 // factor (2) cannot overflow in practice, as the runtime is
1005 // unable to allocate object arrays with a size larger than
1006 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1007 __ Lsl(index_reg, index_reg, TIMES_4);
1008 static_assert(
1009 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1010 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1011 __ AddConstant(index_reg, index_reg, offset_);
1012 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001013 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1014 // intrinsics, `index_` is not shifted by a scale factor of 2
1015 // (as in the case of ArrayGet), as it is actually an offset
1016 // to an object field within an object.
1017 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001018 DCHECK(instruction_->GetLocations()->Intrinsified());
1019 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1020 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1021 << instruction_->AsInvoke()->GetIntrinsic();
1022 DCHECK_EQ(offset_, 0U);
1023 DCHECK(index_.IsRegisterPair());
1024 // UnsafeGet's offset location is a register pair, the low
1025 // part contains the correct offset.
1026 index = index_.ToLow();
1027 }
1028 }
1029
1030 // We're moving two or three locations to locations that could
1031 // overlap, so we need a parallel move resolver.
1032 InvokeRuntimeCallingConvention calling_convention;
1033 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1034 parallel_move.AddMove(ref_,
1035 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1036 Primitive::kPrimNot,
1037 nullptr);
1038 parallel_move.AddMove(obj_,
1039 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1040 Primitive::kPrimNot,
1041 nullptr);
1042 if (index.IsValid()) {
1043 parallel_move.AddMove(index,
1044 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1045 Primitive::kPrimInt,
1046 nullptr);
1047 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1048 } else {
1049 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1050 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1051 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001052 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001053 CheckEntrypointTypes<
1054 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1055 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1056
1057 RestoreLiveRegisters(codegen, locations);
1058 __ b(GetExitLabel());
1059 }
1060
1061 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1062
1063 private:
1064 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1065 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1066 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1067 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1068 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1069 return static_cast<Register>(i);
1070 }
1071 }
1072 // We shall never fail to find a free caller-save register, as
1073 // there are more than two core caller-save registers on ARM
1074 // (meaning it is possible to find one which is different from
1075 // `ref` and `obj`).
1076 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1077 LOG(FATAL) << "Could not find a free caller-save register";
1078 UNREACHABLE();
1079 }
1080
Roland Levillain3b359c72015-11-17 19:35:12 +00001081 const Location out_;
1082 const Location ref_;
1083 const Location obj_;
1084 const uint32_t offset_;
1085 // An additional location containing an index to an array.
1086 // Only used for HArrayGet and the UnsafeGetObject &
1087 // UnsafeGetObjectVolatile intrinsics.
1088 const Location index_;
1089
1090 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1091};
1092
1093// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001094class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001095 public:
1096 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001097 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001098 DCHECK(kEmitCompilerReadBarrier);
1099 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001100
1101 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1102 LocationSummary* locations = instruction_->GetLocations();
1103 Register reg_out = out_.AsRegister<Register>();
1104 DCHECK(locations->CanCall());
1105 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001106 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1107 << "Unexpected instruction in read barrier for GC root slow path: "
1108 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001109
1110 __ Bind(GetEntryLabel());
1111 SaveLiveRegisters(codegen, locations);
1112
1113 InvokeRuntimeCallingConvention calling_convention;
1114 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1115 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001116 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001117 instruction_,
1118 instruction_->GetDexPc(),
1119 this);
1120 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1121 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1122
1123 RestoreLiveRegisters(codegen, locations);
1124 __ b(GetExitLabel());
1125 }
1126
1127 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1128
1129 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001130 const Location out_;
1131 const Location root_;
1132
1133 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1134};
1135
Aart Bike9f37602015-10-09 11:15:55 -07001136inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001137 switch (cond) {
1138 case kCondEQ: return EQ;
1139 case kCondNE: return NE;
1140 case kCondLT: return LT;
1141 case kCondLE: return LE;
1142 case kCondGT: return GT;
1143 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001144 case kCondB: return LO;
1145 case kCondBE: return LS;
1146 case kCondA: return HI;
1147 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001148 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001149 LOG(FATAL) << "Unreachable";
1150 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001151}
1152
Aart Bike9f37602015-10-09 11:15:55 -07001153// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001154inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001155 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001156 case kCondEQ: return EQ;
1157 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001158 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001159 case kCondLT: return LO;
1160 case kCondLE: return LS;
1161 case kCondGT: return HI;
1162 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001163 // Unsigned remain unchanged.
1164 case kCondB: return LO;
1165 case kCondBE: return LS;
1166 case kCondA: return HI;
1167 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001168 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001169 LOG(FATAL) << "Unreachable";
1170 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001171}
1172
Vladimir Markod6e069b2016-01-18 11:11:01 +00001173inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1174 // The ARM condition codes can express all the necessary branches, see the
1175 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1176 // There is no dex instruction or HIR that would need the missing conditions
1177 // "equal or unordered" or "not equal".
1178 switch (cond) {
1179 case kCondEQ: return EQ;
1180 case kCondNE: return NE /* unordered */;
1181 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1182 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1183 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1184 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1185 default:
1186 LOG(FATAL) << "UNREACHABLE";
1187 UNREACHABLE();
1188 }
1189}
1190
Anton Kirilov74234da2017-01-13 14:42:47 +00001191inline Shift ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1192 switch (op_kind) {
1193 case HDataProcWithShifterOp::kASR: return ASR;
1194 case HDataProcWithShifterOp::kLSL: return LSL;
1195 case HDataProcWithShifterOp::kLSR: return LSR;
1196 default:
1197 LOG(FATAL) << "Unexpected op kind " << op_kind;
1198 UNREACHABLE();
1199 }
1200}
1201
1202static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1203 Register out,
1204 Register first,
1205 const ShifterOperand& second,
1206 CodeGeneratorARM* codegen) {
1207 if (second.IsImmediate() && second.GetImmediate() == 0) {
1208 const ShifterOperand in = kind == HInstruction::kAnd
1209 ? ShifterOperand(0)
1210 : ShifterOperand(first);
1211
1212 __ mov(out, in);
1213 } else {
1214 switch (kind) {
1215 case HInstruction::kAdd:
1216 __ add(out, first, second);
1217 break;
1218 case HInstruction::kAnd:
1219 __ and_(out, first, second);
1220 break;
1221 case HInstruction::kOr:
1222 __ orr(out, first, second);
1223 break;
1224 case HInstruction::kSub:
1225 __ sub(out, first, second);
1226 break;
1227 case HInstruction::kXor:
1228 __ eor(out, first, second);
1229 break;
1230 default:
1231 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1232 UNREACHABLE();
1233 }
1234 }
1235}
1236
1237static void GenerateDataProc(HInstruction::InstructionKind kind,
1238 const Location& out,
1239 const Location& first,
1240 const ShifterOperand& second_lo,
1241 const ShifterOperand& second_hi,
1242 CodeGeneratorARM* codegen) {
1243 const Register first_hi = first.AsRegisterPairHigh<Register>();
1244 const Register first_lo = first.AsRegisterPairLow<Register>();
1245 const Register out_hi = out.AsRegisterPairHigh<Register>();
1246 const Register out_lo = out.AsRegisterPairLow<Register>();
1247
1248 if (kind == HInstruction::kAdd) {
1249 __ adds(out_lo, first_lo, second_lo);
1250 __ adc(out_hi, first_hi, second_hi);
1251 } else if (kind == HInstruction::kSub) {
1252 __ subs(out_lo, first_lo, second_lo);
1253 __ sbc(out_hi, first_hi, second_hi);
1254 } else {
1255 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1256 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1257 }
1258}
1259
1260static ShifterOperand GetShifterOperand(Register rm, Shift shift, uint32_t shift_imm) {
1261 return shift_imm == 0 ? ShifterOperand(rm) : ShifterOperand(rm, shift, shift_imm);
1262}
1263
1264static void GenerateLongDataProc(HDataProcWithShifterOp* instruction, CodeGeneratorARM* codegen) {
1265 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1266 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1267
1268 const LocationSummary* const locations = instruction->GetLocations();
1269 const uint32_t shift_value = instruction->GetShiftAmount();
1270 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1271 const Location first = locations->InAt(0);
1272 const Location second = locations->InAt(1);
1273 const Location out = locations->Out();
1274 const Register first_hi = first.AsRegisterPairHigh<Register>();
1275 const Register first_lo = first.AsRegisterPairLow<Register>();
1276 const Register out_hi = out.AsRegisterPairHigh<Register>();
1277 const Register out_lo = out.AsRegisterPairLow<Register>();
1278 const Register second_hi = second.AsRegisterPairHigh<Register>();
1279 const Register second_lo = second.AsRegisterPairLow<Register>();
1280 const Shift shift = ShiftFromOpKind(instruction->GetOpKind());
1281
1282 if (shift_value >= 32) {
1283 if (shift == LSL) {
1284 GenerateDataProcInstruction(kind,
1285 out_hi,
1286 first_hi,
1287 ShifterOperand(second_lo, LSL, shift_value - 32),
1288 codegen);
1289 GenerateDataProcInstruction(kind,
1290 out_lo,
1291 first_lo,
1292 ShifterOperand(0),
1293 codegen);
1294 } else if (shift == ASR) {
1295 GenerateDataProc(kind,
1296 out,
1297 first,
1298 GetShifterOperand(second_hi, ASR, shift_value - 32),
1299 ShifterOperand(second_hi, ASR, 31),
1300 codegen);
1301 } else {
1302 DCHECK_EQ(shift, LSR);
1303 GenerateDataProc(kind,
1304 out,
1305 first,
1306 GetShifterOperand(second_hi, LSR, shift_value - 32),
1307 ShifterOperand(0),
1308 codegen);
1309 }
1310 } else {
1311 DCHECK_GT(shift_value, 1U);
1312 DCHECK_LT(shift_value, 32U);
1313
1314 if (shift == LSL) {
1315 // We are not doing this for HInstruction::kAdd because the output will require
1316 // Location::kOutputOverlap; not applicable to other cases.
1317 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1318 GenerateDataProcInstruction(kind,
1319 out_hi,
1320 first_hi,
1321 ShifterOperand(second_hi, LSL, shift_value),
1322 codegen);
1323 GenerateDataProcInstruction(kind,
1324 out_hi,
1325 out_hi,
1326 ShifterOperand(second_lo, LSR, 32 - shift_value),
1327 codegen);
1328 GenerateDataProcInstruction(kind,
1329 out_lo,
1330 first_lo,
1331 ShifterOperand(second_lo, LSL, shift_value),
1332 codegen);
1333 } else {
1334 __ Lsl(IP, second_hi, shift_value);
1335 __ orr(IP, IP, ShifterOperand(second_lo, LSR, 32 - shift_value));
1336 GenerateDataProc(kind,
1337 out,
1338 first,
1339 ShifterOperand(second_lo, LSL, shift_value),
1340 ShifterOperand(IP),
1341 codegen);
1342 }
1343 } else {
1344 DCHECK(shift == ASR || shift == LSR);
1345
1346 // We are not doing this for HInstruction::kAdd because the output will require
1347 // Location::kOutputOverlap; not applicable to other cases.
1348 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1349 GenerateDataProcInstruction(kind,
1350 out_lo,
1351 first_lo,
1352 ShifterOperand(second_lo, LSR, shift_value),
1353 codegen);
1354 GenerateDataProcInstruction(kind,
1355 out_lo,
1356 out_lo,
1357 ShifterOperand(second_hi, LSL, 32 - shift_value),
1358 codegen);
1359 GenerateDataProcInstruction(kind,
1360 out_hi,
1361 first_hi,
1362 ShifterOperand(second_hi, shift, shift_value),
1363 codegen);
1364 } else {
1365 __ Lsr(IP, second_lo, shift_value);
1366 __ orr(IP, IP, ShifterOperand(second_hi, LSL, 32 - shift_value));
1367 GenerateDataProc(kind,
1368 out,
1369 first,
1370 ShifterOperand(IP),
1371 ShifterOperand(second_hi, shift, shift_value),
1372 codegen);
1373 }
1374 }
1375 }
1376}
1377
Donghui Bai426b49c2016-11-08 14:55:38 +08001378static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARM* codegen) {
1379 Primitive::Type type = instruction->InputAt(0)->GetType();
1380 Location lhs_loc = instruction->GetLocations()->InAt(0);
1381 Location rhs_loc = instruction->GetLocations()->InAt(1);
1382 if (rhs_loc.IsConstant()) {
1383 // 0.0 is the only immediate that can be encoded directly in
1384 // a VCMP instruction.
1385 //
1386 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1387 // specify that in a floating-point comparison, positive zero
1388 // and negative zero are considered equal, so we can use the
1389 // literal 0.0 for both cases here.
1390 //
1391 // Note however that some methods (Float.equal, Float.compare,
1392 // Float.compareTo, Double.equal, Double.compare,
1393 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1394 // StrictMath.min) consider 0.0 to be (strictly) greater than
1395 // -0.0. So if we ever translate calls to these methods into a
1396 // HCompare instruction, we must handle the -0.0 case with
1397 // care here.
1398 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1399 if (type == Primitive::kPrimFloat) {
1400 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1401 } else {
1402 DCHECK_EQ(type, Primitive::kPrimDouble);
1403 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1404 }
1405 } else {
1406 if (type == Primitive::kPrimFloat) {
1407 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1408 } else {
1409 DCHECK_EQ(type, Primitive::kPrimDouble);
1410 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1411 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1412 }
1413 }
1414}
1415
1416static Condition GenerateLongTestConstant(HCondition* condition,
1417 bool invert,
1418 CodeGeneratorARM* codegen) {
1419 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1420
1421 const LocationSummary* const locations = condition->GetLocations();
1422 IfCondition cond = invert ? condition->GetOppositeCondition() : condition->GetCondition();
1423 Condition ret = EQ;
1424 const Location left = locations->InAt(0);
1425 const Location right = locations->InAt(1);
1426
1427 DCHECK(right.IsConstant());
1428
1429 const Register left_high = left.AsRegisterPairHigh<Register>();
1430 const Register left_low = left.AsRegisterPairLow<Register>();
1431 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1432
1433 switch (cond) {
1434 case kCondEQ:
1435 case kCondNE:
1436 case kCondB:
1437 case kCondBE:
1438 case kCondA:
1439 case kCondAE:
1440 __ CmpConstant(left_high, High32Bits(value));
1441 __ it(EQ);
1442 __ cmp(left_low, ShifterOperand(Low32Bits(value)), EQ);
1443 ret = ARMUnsignedCondition(cond);
1444 break;
1445 case kCondLE:
1446 case kCondGT:
1447 // Trivially true or false.
1448 if (value == std::numeric_limits<int64_t>::max()) {
1449 __ cmp(left_low, ShifterOperand(left_low));
1450 ret = cond == kCondLE ? EQ : NE;
1451 break;
1452 }
1453
1454 if (cond == kCondLE) {
1455 cond = kCondLT;
1456 } else {
1457 DCHECK_EQ(cond, kCondGT);
1458 cond = kCondGE;
1459 }
1460
1461 value++;
1462 FALLTHROUGH_INTENDED;
1463 case kCondGE:
1464 case kCondLT:
1465 __ CmpConstant(left_low, Low32Bits(value));
1466 __ sbcs(IP, left_high, ShifterOperand(High32Bits(value)));
1467 ret = ARMCondition(cond);
1468 break;
1469 default:
1470 LOG(FATAL) << "Unreachable";
1471 UNREACHABLE();
1472 }
1473
1474 return ret;
1475}
1476
1477static Condition GenerateLongTest(HCondition* condition,
1478 bool invert,
1479 CodeGeneratorARM* codegen) {
1480 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1481
1482 const LocationSummary* const locations = condition->GetLocations();
1483 IfCondition cond = invert ? condition->GetOppositeCondition() : condition->GetCondition();
1484 Condition ret = EQ;
1485 Location left = locations->InAt(0);
1486 Location right = locations->InAt(1);
1487
1488 DCHECK(right.IsRegisterPair());
1489
1490 switch (cond) {
1491 case kCondEQ:
1492 case kCondNE:
1493 case kCondB:
1494 case kCondBE:
1495 case kCondA:
1496 case kCondAE:
1497 __ cmp(left.AsRegisterPairHigh<Register>(),
1498 ShifterOperand(right.AsRegisterPairHigh<Register>()));
1499 __ it(EQ);
1500 __ cmp(left.AsRegisterPairLow<Register>(),
1501 ShifterOperand(right.AsRegisterPairLow<Register>()),
1502 EQ);
1503 ret = ARMUnsignedCondition(cond);
1504 break;
1505 case kCondLE:
1506 case kCondGT:
1507 if (cond == kCondLE) {
1508 cond = kCondGE;
1509 } else {
1510 DCHECK_EQ(cond, kCondGT);
1511 cond = kCondLT;
1512 }
1513
1514 std::swap(left, right);
1515 FALLTHROUGH_INTENDED;
1516 case kCondGE:
1517 case kCondLT:
1518 __ cmp(left.AsRegisterPairLow<Register>(),
1519 ShifterOperand(right.AsRegisterPairLow<Register>()));
1520 __ sbcs(IP,
1521 left.AsRegisterPairHigh<Register>(),
1522 ShifterOperand(right.AsRegisterPairHigh<Register>()));
1523 ret = ARMCondition(cond);
1524 break;
1525 default:
1526 LOG(FATAL) << "Unreachable";
1527 UNREACHABLE();
1528 }
1529
1530 return ret;
1531}
1532
1533static Condition GenerateTest(HInstruction* instruction,
1534 Location loc,
1535 bool invert,
1536 CodeGeneratorARM* codegen) {
1537 DCHECK(!instruction->IsConstant());
1538
1539 Condition ret = invert ? EQ : NE;
1540
1541 if (IsBooleanValueOrMaterializedCondition(instruction)) {
1542 __ CmpConstant(loc.AsRegister<Register>(), 0);
1543 } else {
1544 HCondition* const condition = instruction->AsCondition();
1545 const LocationSummary* const locations = condition->GetLocations();
1546 const Primitive::Type type = condition->GetLeft()->GetType();
1547 const IfCondition cond = invert ? condition->GetOppositeCondition() : condition->GetCondition();
1548 const Location right = locations->InAt(1);
1549
1550 if (type == Primitive::kPrimLong) {
1551 ret = condition->GetLocations()->InAt(1).IsConstant()
1552 ? GenerateLongTestConstant(condition, invert, codegen)
1553 : GenerateLongTest(condition, invert, codegen);
1554 } else if (Primitive::IsFloatingPointType(type)) {
1555 GenerateVcmp(condition, codegen);
1556 __ vmstat();
1557 ret = ARMFPCondition(cond, condition->IsGtBias());
1558 } else {
1559 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1560
1561 const Register left = locations->InAt(0).AsRegister<Register>();
1562
1563 if (right.IsRegister()) {
1564 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
1565 } else {
1566 DCHECK(right.IsConstant());
1567 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1568 }
1569
1570 ret = ARMCondition(cond);
1571 }
1572 }
1573
1574 return ret;
1575}
1576
1577static bool CanGenerateTest(HInstruction* condition, ArmAssembler* assembler) {
1578 if (!IsBooleanValueOrMaterializedCondition(condition)) {
1579 const HCondition* const cond = condition->AsCondition();
1580
1581 if (cond->GetLeft()->GetType() == Primitive::kPrimLong) {
1582 const LocationSummary* const locations = cond->GetLocations();
1583 const IfCondition c = cond->GetCondition();
1584
1585 if (locations->InAt(1).IsConstant()) {
1586 const int64_t value = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
1587 ShifterOperand so;
1588
1589 if (c < kCondLT || c > kCondGE) {
1590 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1591 // we check that the least significant half of the first input to be compared
1592 // is in a low register (the other half is read outside an IT block), and
1593 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
1594 // encoding can be used.
1595 if (!ArmAssembler::IsLowRegister(locations->InAt(0).AsRegisterPairLow<Register>()) ||
1596 !IsUint<8>(Low32Bits(value))) {
1597 return false;
1598 }
1599 } else if (c == kCondLE || c == kCondGT) {
1600 if (value < std::numeric_limits<int64_t>::max() &&
1601 !assembler->ShifterOperandCanHold(kNoRegister,
1602 kNoRegister,
1603 SBC,
1604 High32Bits(value + 1),
1605 kCcSet,
1606 &so)) {
1607 return false;
1608 }
1609 } else if (!assembler->ShifterOperandCanHold(kNoRegister,
1610 kNoRegister,
1611 SBC,
1612 High32Bits(value),
1613 kCcSet,
1614 &so)) {
1615 return false;
1616 }
1617 }
1618 }
1619 }
1620
1621 return true;
1622}
1623
1624static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
1625 const Primitive::Type type = constant->GetType();
1626 bool ret = false;
1627
1628 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1629
1630 if (type == Primitive::kPrimLong) {
1631 const uint64_t value = constant->AsLongConstant()->GetValueAsUint64();
1632
1633 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1634 } else {
1635 ret = IsUint<8>(CodeGenerator::GetInt32ValueOf(constant));
1636 }
1637
1638 return ret;
1639}
1640
1641static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
1642 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
1643
1644 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1645 return Location::ConstantLocation(constant->AsConstant());
1646 }
1647
1648 return Location::RequiresRegister();
1649}
1650
1651static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1652 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1653 // we check that we are not dealing with floating-point output (there is no
1654 // 16-bit VMOV encoding).
1655 if (!out.IsRegister() && !out.IsRegisterPair()) {
1656 return false;
1657 }
1658
1659 // For constants, we also check that the output is in one or two low registers,
1660 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1661 // MOV encoding can be used.
1662 if (src.IsConstant()) {
1663 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1664 return false;
1665 }
1666
1667 if (out.IsRegister()) {
1668 if (!ArmAssembler::IsLowRegister(out.AsRegister<Register>())) {
1669 return false;
1670 }
1671 } else {
1672 DCHECK(out.IsRegisterPair());
1673
1674 if (!ArmAssembler::IsLowRegister(out.AsRegisterPairHigh<Register>())) {
1675 return false;
1676 }
1677 }
1678 }
1679
1680 return true;
1681}
1682
Anton Kirilov74234da2017-01-13 14:42:47 +00001683#undef __
1684// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1685#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
1686
Donghui Bai426b49c2016-11-08 14:55:38 +08001687Label* CodeGeneratorARM::GetFinalLabel(HInstruction* instruction, Label* final_label) {
1688 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
1689
1690 const HBasicBlock* const block = instruction->GetBlock();
1691 const HLoopInformation* const info = block->GetLoopInformation();
1692 HInstruction* const next = instruction->GetNext();
1693
1694 // Avoid a branch to a branch.
1695 if (next->IsGoto() && (info == nullptr ||
1696 !info->IsBackEdge(*block) ||
1697 !info->HasSuspendCheck())) {
1698 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1699 }
1700
1701 return final_label;
1702}
1703
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001704void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001705 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001706}
1707
1708void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001709 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001710}
1711
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001712size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1713 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1714 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001715}
1716
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001717size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1718 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1719 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001720}
1721
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001722size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1723 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1724 return kArmWordSize;
1725}
1726
1727size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1728 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1729 return kArmWordSize;
1730}
1731
Calin Juravle34166012014-12-19 17:22:29 +00001732CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001733 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001734 const CompilerOptions& compiler_options,
1735 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001736 : CodeGenerator(graph,
1737 kNumberOfCoreRegisters,
1738 kNumberOfSRegisters,
1739 kNumberOfRegisterPairs,
1740 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1741 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001742 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1743 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001744 compiler_options,
1745 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001746 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001747 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001748 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001749 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001750 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001751 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001752 uint32_literals_(std::less<uint32_t>(),
1753 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001754 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1755 boot_image_string_patches_(StringReferenceValueComparator(),
1756 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1757 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001758 boot_image_type_patches_(TypeReferenceValueComparator(),
1759 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1760 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001761 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001762 boot_image_address_patches_(std::less<uint32_t>(),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001763 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1764 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001765 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1766 jit_class_patches_(TypeReferenceValueComparator(),
1767 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001768 // Always save the LR register to mimic Quick.
1769 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001770}
1771
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001772void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1773 // Ensure that we fix up branches and literal loads and emit the literal pool.
1774 __ FinalizeCode();
1775
1776 // Adjust native pc offsets in stack maps.
1777 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001778 uint32_t old_position =
1779 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001780 uint32_t new_position = __ GetAdjustedPosition(old_position);
1781 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1782 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001783 // Adjust pc offsets for the disassembly information.
1784 if (disasm_info_ != nullptr) {
1785 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1786 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1787 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1788 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1789 it.second.start = __ GetAdjustedPosition(it.second.start);
1790 it.second.end = __ GetAdjustedPosition(it.second.end);
1791 }
1792 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1793 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1794 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1795 }
1796 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001797
1798 CodeGenerator::Finalize(allocator);
1799}
1800
David Brazdil58282f42016-01-14 12:45:10 +00001801void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001802 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001803 blocked_core_registers_[SP] = true;
1804 blocked_core_registers_[LR] = true;
1805 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001806
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001807 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001808 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001809
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001810 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001811 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001812
David Brazdil58282f42016-01-14 12:45:10 +00001813 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01001814 // Stubs do not save callee-save floating point registers. If the graph
1815 // is debuggable, we need to deal with these registers differently. For
1816 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001817 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1818 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1819 }
1820 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001821}
1822
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001823InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001824 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001825 assembler_(codegen->GetAssembler()),
1826 codegen_(codegen) {}
1827
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001828void CodeGeneratorARM::ComputeSpillMask() {
1829 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1830 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00001831 // There is no easy instruction to restore just the PC on thumb2. We spill and
1832 // restore another arbitrary register.
1833 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001834 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1835 // We use vpush and vpop for saving and restoring floating point registers, which take
1836 // a SRegister and the number of registers to save/restore after that SRegister. We
1837 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
1838 // but in the range.
1839 if (fpu_spill_mask_ != 0) {
1840 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
1841 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
1842 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
1843 fpu_spill_mask_ |= (1 << i);
1844 }
1845 }
1846}
1847
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001848static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001849 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001850}
1851
1852static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001853 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001854}
1855
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001856void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00001857 bool skip_overflow_check =
1858 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001859 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001860 __ Bind(&frame_entry_label_);
1861
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001862 if (HasEmptyFrame()) {
1863 return;
1864 }
1865
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001866 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001867 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
1868 __ LoadFromOffset(kLoadWord, IP, IP, 0);
1869 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001870 }
1871
Andreas Gampe501fd632015-09-10 16:11:06 -07001872 __ PushList(core_spill_mask_);
1873 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
1874 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001875 if (fpu_spill_mask_ != 0) {
1876 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1877 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001878 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01001879 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001880 }
Mingyao Yang063fc772016-08-02 11:02:54 -07001881
1882 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1883 // Initialize should_deoptimize flag to 0.
1884 __ mov(IP, ShifterOperand(0));
1885 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
1886 }
1887
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001888 int adjust = GetFrameSize() - FrameEntrySpillSize();
1889 __ AddConstant(SP, -adjust);
1890 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001891
1892 // Save the current method if we need it. Note that we do not
1893 // do this in HCurrentMethod, as the instruction might have been removed
1894 // in the SSA graph.
1895 if (RequiresCurrentMethod()) {
1896 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
1897 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001898}
1899
1900void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001901 if (HasEmptyFrame()) {
1902 __ bx(LR);
1903 return;
1904 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001905 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001906 int adjust = GetFrameSize() - FrameEntrySpillSize();
1907 __ AddConstant(SP, adjust);
1908 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001909 if (fpu_spill_mask_ != 0) {
1910 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
1911 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07001912 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001913 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001914 }
Andreas Gampe501fd632015-09-10 16:11:06 -07001915 // Pop LR into PC to return.
1916 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
1917 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
1918 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01001919 __ cfi().RestoreState();
1920 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001921}
1922
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001923void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07001924 Label* label = GetLabelOf(block);
1925 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001926}
1927
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001928Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001929 switch (type) {
1930 case Primitive::kPrimBoolean:
1931 case Primitive::kPrimByte:
1932 case Primitive::kPrimChar:
1933 case Primitive::kPrimShort:
1934 case Primitive::kPrimInt:
1935 case Primitive::kPrimNot: {
1936 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001937 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001938 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001939 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001940 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001941 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001942 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001943 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001944
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001945 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001946 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001947 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001948 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001949 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001950 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001951 if (calling_convention.GetRegisterAt(index) == R1) {
1952 // Skip R1, and use R2_R3 instead.
1953 gp_index_++;
1954 index++;
1955 }
1956 }
1957 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
1958 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001959 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01001960
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00001961 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00001962 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001963 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001964 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
1965 }
1966 }
1967
1968 case Primitive::kPrimFloat: {
1969 uint32_t stack_index = stack_index_++;
1970 if (float_index_ % 2 == 0) {
1971 float_index_ = std::max(double_index_, float_index_);
1972 }
1973 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
1974 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
1975 } else {
1976 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
1977 }
1978 }
1979
1980 case Primitive::kPrimDouble: {
1981 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
1982 uint32_t stack_index = stack_index_;
1983 stack_index_ += 2;
1984 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
1985 uint32_t index = double_index_;
1986 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001987 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001988 calling_convention.GetFpuRegisterAt(index),
1989 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00001990 DCHECK(ExpectedPairLayout(result));
1991 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00001992 } else {
1993 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001994 }
1995 }
1996
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001997 case Primitive::kPrimVoid:
1998 LOG(FATAL) << "Unexpected parameter type " << type;
1999 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002000 }
Roland Levillain3b359c72015-11-17 19:35:12 +00002001 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002002}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002003
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002004Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002005 switch (type) {
2006 case Primitive::kPrimBoolean:
2007 case Primitive::kPrimByte:
2008 case Primitive::kPrimChar:
2009 case Primitive::kPrimShort:
2010 case Primitive::kPrimInt:
2011 case Primitive::kPrimNot: {
2012 return Location::RegisterLocation(R0);
2013 }
2014
2015 case Primitive::kPrimFloat: {
2016 return Location::FpuRegisterLocation(S0);
2017 }
2018
2019 case Primitive::kPrimLong: {
2020 return Location::RegisterPairLocation(R0, R1);
2021 }
2022
2023 case Primitive::kPrimDouble: {
2024 return Location::FpuRegisterPairLocation(S0, S1);
2025 }
2026
2027 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00002028 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002029 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002030
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002031 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002032}
2033
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002034Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
2035 return Location::RegisterLocation(kMethodRegisterArgument);
2036}
2037
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002038void CodeGeneratorARM::Move32(Location destination, Location source) {
2039 if (source.Equals(destination)) {
2040 return;
2041 }
2042 if (destination.IsRegister()) {
2043 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002044 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002045 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002046 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002047 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002048 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002049 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002050 } else if (destination.IsFpuRegister()) {
2051 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002052 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002053 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002054 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002055 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002056 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002057 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002058 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002059 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002060 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002061 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002062 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002063 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002064 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002065 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002066 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
2067 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002068 }
2069 }
2070}
2071
2072void CodeGeneratorARM::Move64(Location destination, Location source) {
2073 if (source.Equals(destination)) {
2074 return;
2075 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002076 if (destination.IsRegisterPair()) {
2077 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002078 EmitParallelMoves(
2079 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
2080 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002081 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002082 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002083 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
2084 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002085 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002086 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002087 } else if (source.IsFpuRegisterPair()) {
2088 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
2089 destination.AsRegisterPairHigh<Register>(),
2090 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002091 } else {
2092 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002093 DCHECK(ExpectedPairLayout(destination));
2094 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
2095 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002096 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002097 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002098 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002099 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2100 SP,
2101 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01002102 } else if (source.IsRegisterPair()) {
2103 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2104 source.AsRegisterPairLow<Register>(),
2105 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002106 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002107 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002108 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002109 } else {
2110 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002111 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002112 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002113 if (source.AsRegisterPairLow<Register>() == R1) {
2114 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002115 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
2116 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002117 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002118 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002119 SP, destination.GetStackIndex());
2120 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002121 } else if (source.IsFpuRegisterPair()) {
2122 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
2123 SP,
2124 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002125 } else {
2126 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002127 EmitParallelMoves(
2128 Location::StackSlot(source.GetStackIndex()),
2129 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002130 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002131 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002132 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
2133 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002134 }
2135 }
2136}
2137
Calin Juravle175dc732015-08-25 15:42:32 +01002138void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
2139 DCHECK(location.IsRegister());
2140 __ LoadImmediate(location.AsRegister<Register>(), value);
2141}
2142
Calin Juravlee460d1d2015-09-29 04:52:17 +01002143void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002144 HParallelMove move(GetGraph()->GetArena());
2145 move.AddMove(src, dst, dst_type, nullptr);
2146 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002147}
2148
2149void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
2150 if (location.IsRegister()) {
2151 locations->AddTemp(location);
2152 } else if (location.IsRegisterPair()) {
2153 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
2154 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
2155 } else {
2156 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2157 }
2158}
2159
Calin Juravle175dc732015-08-25 15:42:32 +01002160void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
2161 HInstruction* instruction,
2162 uint32_t dex_pc,
2163 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002164 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002165 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002166 if (EntrypointRequiresStackMap(entrypoint)) {
2167 RecordPcInfo(instruction, dex_pc, slow_path);
2168 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002169}
2170
Roland Levillaindec8f632016-07-22 17:10:06 +01002171void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2172 HInstruction* instruction,
2173 SlowPathCode* slow_path) {
2174 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002175 GenerateInvokeRuntime(entry_point_offset);
2176}
2177
2178void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01002179 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2180 __ blx(LR);
2181}
2182
David Brazdilfc6a86a2015-06-26 10:33:45 +00002183void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002184 DCHECK(!successor->IsExitBlock());
2185
2186 HBasicBlock* block = got->GetBlock();
2187 HInstruction* previous = got->GetPrevious();
2188
2189 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00002190 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002191 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2192 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2193 return;
2194 }
2195
2196 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2197 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2198 }
2199 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002200 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002201 }
2202}
2203
David Brazdilfc6a86a2015-06-26 10:33:45 +00002204void LocationsBuilderARM::VisitGoto(HGoto* got) {
2205 got->SetLocations(nullptr);
2206}
2207
2208void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
2209 HandleGoto(got, got->GetSuccessor());
2210}
2211
2212void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2213 try_boundary->SetLocations(nullptr);
2214}
2215
2216void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2217 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2218 if (!successor->IsExitBlock()) {
2219 HandleGoto(try_boundary, successor);
2220 }
2221}
2222
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002223void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002224 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002225}
2226
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002227void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002228}
2229
Roland Levillain4fa13f62015-07-06 18:11:54 +01002230void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
2231 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00002232 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002233 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00002234 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002235}
2236
2237void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
2238 Label* true_label,
2239 Label* false_label) {
2240 LocationSummary* locations = cond->GetLocations();
2241 Location left = locations->InAt(0);
2242 Location right = locations->InAt(1);
2243 IfCondition if_cond = cond->GetCondition();
2244
2245 Register left_high = left.AsRegisterPairHigh<Register>();
2246 Register left_low = left.AsRegisterPairLow<Register>();
2247 IfCondition true_high_cond = if_cond;
2248 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07002249 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01002250
2251 // Set the conditions for the test, remembering that == needs to be
2252 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07002253 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01002254 switch (if_cond) {
2255 case kCondEQ:
2256 case kCondNE:
2257 // Nothing to do.
2258 break;
2259 case kCondLT:
2260 false_high_cond = kCondGT;
2261 break;
2262 case kCondLE:
2263 true_high_cond = kCondLT;
2264 break;
2265 case kCondGT:
2266 false_high_cond = kCondLT;
2267 break;
2268 case kCondGE:
2269 true_high_cond = kCondGT;
2270 break;
Aart Bike9f37602015-10-09 11:15:55 -07002271 case kCondB:
2272 false_high_cond = kCondA;
2273 break;
2274 case kCondBE:
2275 true_high_cond = kCondB;
2276 break;
2277 case kCondA:
2278 false_high_cond = kCondB;
2279 break;
2280 case kCondAE:
2281 true_high_cond = kCondA;
2282 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002283 }
2284 if (right.IsConstant()) {
2285 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
2286 int32_t val_low = Low32Bits(value);
2287 int32_t val_high = High32Bits(value);
2288
Vladimir Markoac6ac102015-12-17 12:14:00 +00002289 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002290 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002291 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002292 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002293 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002294 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002295 __ b(true_label, ARMCondition(true_high_cond));
2296 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002297 }
2298 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00002299 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002300 } else {
2301 Register right_high = right.AsRegisterPairHigh<Register>();
2302 Register right_low = right.AsRegisterPairLow<Register>();
2303
2304 __ cmp(left_high, ShifterOperand(right_high));
2305 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002306 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002307 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002308 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002309 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002310 __ b(true_label, ARMCondition(true_high_cond));
2311 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002312 }
2313 // Must be equal high, so compare the lows.
2314 __ cmp(left_low, ShifterOperand(right_low));
2315 }
2316 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07002317 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01002318 __ b(true_label, final_condition);
2319}
2320
David Brazdil0debae72015-11-12 18:37:00 +00002321void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
2322 Label* true_target_in,
2323 Label* false_target_in) {
2324 // Generated branching requires both targets to be explicit. If either of the
2325 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2326 Label fallthrough_target;
2327 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
2328 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
2329
Roland Levillain4fa13f62015-07-06 18:11:54 +01002330 Primitive::Type type = condition->InputAt(0)->GetType();
2331 switch (type) {
2332 case Primitive::kPrimLong:
2333 GenerateLongComparesAndJumps(condition, true_target, false_target);
2334 break;
2335 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002336 case Primitive::kPrimDouble:
Donghui Bai426b49c2016-11-08 14:55:38 +08002337 GenerateVcmp(condition, codegen_);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002338 GenerateFPJumps(condition, true_target, false_target);
2339 break;
2340 default:
2341 LOG(FATAL) << "Unexpected compare type " << type;
2342 }
2343
David Brazdil0debae72015-11-12 18:37:00 +00002344 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002345 __ b(false_target);
2346 }
David Brazdil0debae72015-11-12 18:37:00 +00002347
2348 if (fallthrough_target.IsLinked()) {
2349 __ Bind(&fallthrough_target);
2350 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002351}
2352
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002353void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002354 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002355 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002356 Label* false_target) {
2357 HInstruction* cond = instruction->InputAt(condition_input_index);
2358
2359 if (true_target == nullptr && false_target == nullptr) {
2360 // Nothing to do. The code always falls through.
2361 return;
2362 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002363 // Constant condition, statically compared against "true" (integer value 1).
2364 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002365 if (true_target != nullptr) {
2366 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002367 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002368 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002369 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002370 if (false_target != nullptr) {
2371 __ b(false_target);
2372 }
2373 }
2374 return;
2375 }
2376
2377 // The following code generates these patterns:
2378 // (1) true_target == nullptr && false_target != nullptr
2379 // - opposite condition true => branch to false_target
2380 // (2) true_target != nullptr && false_target == nullptr
2381 // - condition true => branch to true_target
2382 // (3) true_target != nullptr && false_target != nullptr
2383 // - condition true => branch to true_target
2384 // - branch to false_target
2385 if (IsBooleanValueOrMaterializedCondition(cond)) {
2386 // Condition has been materialized, compare the output to 0.
2387 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2388 DCHECK(cond_val.IsRegister());
2389 if (true_target == nullptr) {
2390 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
2391 } else {
2392 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002393 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002394 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002395 // Condition has not been materialized. Use its inputs as the comparison and
2396 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002397 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002398
2399 // If this is a long or FP comparison that has been folded into
2400 // the HCondition, generate the comparison directly.
2401 Primitive::Type type = condition->InputAt(0)->GetType();
2402 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2403 GenerateCompareTestAndBranch(condition, true_target, false_target);
2404 return;
2405 }
2406
Donghui Bai426b49c2016-11-08 14:55:38 +08002407 Label* non_fallthrough_target;
2408 Condition arm_cond;
David Brazdil0debae72015-11-12 18:37:00 +00002409 LocationSummary* locations = cond->GetLocations();
2410 DCHECK(locations->InAt(0).IsRegister());
2411 Register left = locations->InAt(0).AsRegister<Register>();
2412 Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08002413
David Brazdil0debae72015-11-12 18:37:00 +00002414 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002415 arm_cond = ARMCondition(condition->GetOppositeCondition());
2416 non_fallthrough_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002417 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002418 arm_cond = ARMCondition(condition->GetCondition());
2419 non_fallthrough_target = true_target;
2420 }
2421
2422 if (right.IsConstant() && (arm_cond == NE || arm_cond == EQ) &&
2423 CodeGenerator::GetInt32ValueOf(right.GetConstant()) == 0) {
2424 if (arm_cond == EQ) {
2425 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2426 } else {
2427 DCHECK_EQ(arm_cond, NE);
2428 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2429 }
2430 } else {
2431 if (right.IsRegister()) {
2432 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
2433 } else {
2434 DCHECK(right.IsConstant());
2435 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2436 }
2437
2438 __ b(non_fallthrough_target, arm_cond);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002439 }
Dave Allison20dfc792014-06-16 20:44:29 -07002440 }
David Brazdil0debae72015-11-12 18:37:00 +00002441
2442 // If neither branch falls through (case 3), the conditional branch to `true_target`
2443 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2444 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002445 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002446 }
2447}
2448
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002449void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002450 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2451 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002452 locations->SetInAt(0, Location::RequiresRegister());
2453 }
2454}
2455
2456void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002457 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2458 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2459 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2460 nullptr : codegen_->GetLabelOf(true_successor);
2461 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2462 nullptr : codegen_->GetLabelOf(false_successor);
2463 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002464}
2465
2466void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
2467 LocationSummary* locations = new (GetGraph()->GetArena())
2468 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002469 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002470 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002471 locations->SetInAt(0, Location::RequiresRegister());
2472 }
2473}
2474
2475void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01002476 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002477 GenerateTestAndBranch(deoptimize,
2478 /* condition_input_index */ 0,
2479 slow_path->GetEntryLabel(),
2480 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002481}
Dave Allison20dfc792014-06-16 20:44:29 -07002482
Mingyao Yang063fc772016-08-02 11:02:54 -07002483void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2484 LocationSummary* locations = new (GetGraph()->GetArena())
2485 LocationSummary(flag, LocationSummary::kNoCall);
2486 locations->SetOut(Location::RequiresRegister());
2487}
2488
2489void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2490 __ LoadFromOffset(kLoadWord,
2491 flag->GetLocations()->Out().AsRegister<Register>(),
2492 SP,
2493 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2494}
2495
David Brazdil74eb1b22015-12-14 11:44:01 +00002496void LocationsBuilderARM::VisitSelect(HSelect* select) {
2497 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08002498 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
2499
2500 if (is_floating_point) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002501 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002502 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002503 } else {
2504 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002505 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002506 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002507
David Brazdil74eb1b22015-12-14 11:44:01 +00002508 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002509 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2510 // The code generator handles overlap with the values, but not with the condition.
2511 locations->SetOut(Location::SameAsFirstInput());
2512 } else if (is_floating_point) {
2513 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2514 } else {
2515 if (!locations->InAt(1).IsConstant()) {
2516 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2517 }
2518
2519 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00002520 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002521}
2522
2523void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002524 HInstruction* const condition = select->GetCondition();
2525 const LocationSummary* const locations = select->GetLocations();
2526 const Primitive::Type type = select->GetType();
2527 const Location first = locations->InAt(0);
2528 const Location out = locations->Out();
2529 const Location second = locations->InAt(1);
2530 Location src;
2531
2532 if (condition->IsIntConstant()) {
2533 if (condition->AsIntConstant()->IsFalse()) {
2534 src = first;
2535 } else {
2536 src = second;
2537 }
2538
2539 codegen_->MoveLocation(out, src, type);
2540 return;
2541 }
2542
2543 if (!Primitive::IsFloatingPointType(type) &&
2544 CanGenerateTest(condition, codegen_->GetAssembler())) {
2545 bool invert = false;
2546
2547 if (out.Equals(second)) {
2548 src = first;
2549 invert = true;
2550 } else if (out.Equals(first)) {
2551 src = second;
2552 } else if (second.IsConstant()) {
2553 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2554 src = second;
2555 } else if (first.IsConstant()) {
2556 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2557 src = first;
2558 invert = true;
2559 } else {
2560 src = second;
2561 }
2562
2563 if (CanGenerateConditionalMove(out, src)) {
2564 if (!out.Equals(first) && !out.Equals(second)) {
2565 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2566 }
2567
2568 const Condition cond = GenerateTest(condition, locations->InAt(2), invert, codegen_);
2569
2570 if (out.IsRegister()) {
2571 ShifterOperand operand;
2572
2573 if (src.IsConstant()) {
2574 operand = ShifterOperand(CodeGenerator::GetInt32ValueOf(src.GetConstant()));
2575 } else {
2576 DCHECK(src.IsRegister());
2577 operand = ShifterOperand(src.AsRegister<Register>());
2578 }
2579
2580 __ it(cond);
2581 __ mov(out.AsRegister<Register>(), operand, cond);
2582 } else {
2583 DCHECK(out.IsRegisterPair());
2584
2585 ShifterOperand operand_high;
2586 ShifterOperand operand_low;
2587
2588 if (src.IsConstant()) {
2589 const int64_t value = src.GetConstant()->AsLongConstant()->GetValue();
2590
2591 operand_high = ShifterOperand(High32Bits(value));
2592 operand_low = ShifterOperand(Low32Bits(value));
2593 } else {
2594 DCHECK(src.IsRegisterPair());
2595 operand_high = ShifterOperand(src.AsRegisterPairHigh<Register>());
2596 operand_low = ShifterOperand(src.AsRegisterPairLow<Register>());
2597 }
2598
2599 __ it(cond);
2600 __ mov(out.AsRegisterPairLow<Register>(), operand_low, cond);
2601 __ it(cond);
2602 __ mov(out.AsRegisterPairHigh<Register>(), operand_high, cond);
2603 }
2604
2605 return;
2606 }
2607 }
2608
2609 Label* false_target = nullptr;
2610 Label* true_target = nullptr;
2611 Label select_end;
2612 Label* target = codegen_->GetFinalLabel(select, &select_end);
2613
2614 if (out.Equals(second)) {
2615 true_target = target;
2616 src = first;
2617 } else {
2618 false_target = target;
2619 src = second;
2620
2621 if (!out.Equals(first)) {
2622 codegen_->MoveLocation(out, first, type);
2623 }
2624 }
2625
2626 GenerateTestAndBranch(select, 2, true_target, false_target);
2627 codegen_->MoveLocation(out, src, type);
2628
2629 if (select_end.IsLinked()) {
2630 __ Bind(&select_end);
2631 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002632}
2633
David Srbecky0cf44932015-12-09 14:09:59 +00002634void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2635 new (GetGraph()->GetArena()) LocationSummary(info);
2636}
2637
David Srbeckyd28f4a02016-03-14 17:14:24 +00002638void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2639 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002640}
2641
2642void CodeGeneratorARM::GenerateNop() {
2643 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002644}
2645
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002646void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002647 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002648 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002649 // Handle the long/FP comparisons made in instruction simplification.
2650 switch (cond->InputAt(0)->GetType()) {
2651 case Primitive::kPrimLong:
2652 locations->SetInAt(0, Location::RequiresRegister());
2653 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002654 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002655 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2656 }
2657 break;
2658
2659 case Primitive::kPrimFloat:
2660 case Primitive::kPrimDouble:
2661 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002662 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002663 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002664 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2665 }
2666 break;
2667
2668 default:
2669 locations->SetInAt(0, Location::RequiresRegister());
2670 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002671 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002672 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2673 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002674 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002675}
2676
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002677void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002678 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002679 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002680 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002681
2682 LocationSummary* locations = cond->GetLocations();
2683 Location left = locations->InAt(0);
2684 Location right = locations->InAt(1);
2685 Register out = locations->Out().AsRegister<Register>();
2686 Label true_label, false_label;
2687
2688 switch (cond->InputAt(0)->GetType()) {
2689 default: {
2690 // Integer case.
2691 if (right.IsRegister()) {
2692 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2693 } else {
2694 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002695 __ CmpConstant(left.AsRegister<Register>(),
2696 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002697 }
Aart Bike9f37602015-10-09 11:15:55 -07002698 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002699 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002700 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002701 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002702 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002703 return;
2704 }
2705 case Primitive::kPrimLong:
2706 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2707 break;
2708 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002709 case Primitive::kPrimDouble:
Donghui Bai426b49c2016-11-08 14:55:38 +08002710 GenerateVcmp(cond, codegen_);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002711 GenerateFPJumps(cond, &true_label, &false_label);
2712 break;
2713 }
2714
2715 // Convert the jumps into the result.
2716 Label done_label;
2717
2718 // False case: result = 0.
2719 __ Bind(&false_label);
2720 __ LoadImmediate(out, 0);
2721 __ b(&done_label);
2722
2723 // True case: result = 1.
2724 __ Bind(&true_label);
2725 __ LoadImmediate(out, 1);
2726 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07002727}
2728
2729void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002730 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002731}
2732
2733void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002734 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002735}
2736
2737void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002738 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002739}
2740
2741void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002742 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002743}
2744
2745void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002746 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002747}
2748
2749void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002750 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002751}
2752
2753void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002754 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002755}
2756
2757void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002758 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002759}
2760
2761void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002762 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002763}
2764
2765void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002766 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002767}
2768
2769void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002770 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002771}
2772
2773void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002774 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002775}
2776
Aart Bike9f37602015-10-09 11:15:55 -07002777void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002778 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002779}
2780
2781void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002782 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002783}
2784
2785void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002786 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002787}
2788
2789void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002790 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002791}
2792
2793void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002794 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002795}
2796
2797void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002798 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002799}
2800
2801void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002802 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002803}
2804
2805void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002806 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002807}
2808
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002809void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002810 LocationSummary* locations =
2811 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002812 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002813}
2814
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002815void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002816 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002817}
2818
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002819void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
2820 LocationSummary* locations =
2821 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2822 locations->SetOut(Location::ConstantLocation(constant));
2823}
2824
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002825void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002826 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002827}
2828
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002829void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002830 LocationSummary* locations =
2831 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002832 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002833}
2834
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002835void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002836 // Will be generated at use site.
2837}
2838
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002839void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
2840 LocationSummary* locations =
2841 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2842 locations->SetOut(Location::ConstantLocation(constant));
2843}
2844
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002845void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002846 // Will be generated at use site.
2847}
2848
2849void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
2850 LocationSummary* locations =
2851 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2852 locations->SetOut(Location::ConstantLocation(constant));
2853}
2854
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002855void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002856 // Will be generated at use site.
2857}
2858
Calin Juravle27df7582015-04-17 19:12:31 +01002859void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2860 memory_barrier->SetLocations(nullptr);
2861}
2862
2863void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00002864 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002865}
2866
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002867void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002868 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002869}
2870
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002871void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002872 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002873}
2874
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002875void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002876 LocationSummary* locations =
2877 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002878 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002879}
2880
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002881void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002882 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002883}
2884
Calin Juravle175dc732015-08-25 15:42:32 +01002885void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2886 // The trampoline uses the same calling convention as dex calling conventions,
2887 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2888 // the method_idx.
2889 HandleInvoke(invoke);
2890}
2891
2892void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2893 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2894}
2895
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002896void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002897 // Explicit clinit checks triggered by static invokes must have been pruned by
2898 // art::PrepareForRegisterAllocation.
2899 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002900
Vladimir Marko68c981f2016-08-26 13:13:33 +01002901 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002902 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002903 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
2904 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
2905 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002906 return;
2907 }
2908
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002909 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00002910
2911 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2912 if (invoke->HasPcRelativeDexCache()) {
2913 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
2914 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002915}
2916
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002917static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
2918 if (invoke->GetLocations()->Intrinsified()) {
2919 IntrinsicCodeGeneratorARM intrinsic(codegen);
2920 intrinsic.Dispatch(invoke);
2921 return true;
2922 }
2923 return false;
2924}
2925
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002926void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002927 // Explicit clinit checks triggered by static invokes must have been pruned by
2928 // art::PrepareForRegisterAllocation.
2929 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002930
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002931 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2932 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002933 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002934
Nicolas Geoffray38207af2015-06-01 15:46:22 +01002935 LocationSummary* locations = invoke->GetLocations();
2936 codegen_->GenerateStaticOrDirectCall(
2937 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00002938 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002939}
2940
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002941void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002942 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002943 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002944}
2945
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002946void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01002947 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002948 if (intrinsic.TryDispatch(invoke)) {
2949 return;
2950 }
2951
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002952 HandleInvoke(invoke);
2953}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002954
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002955void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08002956 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2957 return;
2958 }
2959
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002960 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002961 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002962 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002963}
2964
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002965void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2966 HandleInvoke(invoke);
2967 // Add the hidden argument.
2968 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
2969}
2970
2971void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
2972 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00002973 LocationSummary* locations = invoke->GetLocations();
2974 Register temp = locations->GetTemp(0).AsRegister<Register>();
2975 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002976 Location receiver = locations->InAt(0);
2977 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2978
Roland Levillain3b359c72015-11-17 19:35:12 +00002979 // Set the hidden argument. This is safe to do this here, as R12
2980 // won't be modified thereafter, before the `blx` (call) instruction.
2981 DCHECK_EQ(R12, hidden_reg);
2982 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002983
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002984 if (receiver.IsStackSlot()) {
2985 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00002986 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002987 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
2988 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00002989 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002990 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002991 }
Calin Juravle77520bc2015-01-12 18:45:46 +00002992 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00002993 // Instead of simply (possibly) unpoisoning `temp` here, we should
2994 // emit a read barrier for the previous class reference load.
2995 // However this is not required in practice, as this is an
2996 // intermediate/temporary reference and because the current
2997 // concurrent copying collector keeps the from-space memory
2998 // intact/accessible until the end of the marking phase (the
2999 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003000 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003001 __ LoadFromOffset(kLoadWord, temp, temp,
3002 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3003 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003004 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003005 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003006 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00003007 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07003008 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003009 // LR = temp->GetEntryPoint();
3010 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
3011 // LR();
3012 __ blx(LR);
3013 DCHECK(!codegen_->IsLeafMethod());
3014 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3015}
3016
Orion Hodsonac141392017-01-13 11:53:47 +00003017void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3018 HandleInvoke(invoke);
3019}
3020
3021void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3022 codegen_->GenerateInvokePolymorphicCall(invoke);
3023}
3024
Roland Levillain88cb1752014-10-20 16:36:47 +01003025void LocationsBuilderARM::VisitNeg(HNeg* neg) {
3026 LocationSummary* locations =
3027 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3028 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003029 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01003030 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003031 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3032 break;
3033 }
3034 case Primitive::kPrimLong: {
3035 locations->SetInAt(0, Location::RequiresRegister());
3036 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003037 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003038 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003039
Roland Levillain88cb1752014-10-20 16:36:47 +01003040 case Primitive::kPrimFloat:
3041 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003042 locations->SetInAt(0, Location::RequiresFpuRegister());
3043 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003044 break;
3045
3046 default:
3047 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3048 }
3049}
3050
3051void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
3052 LocationSummary* locations = neg->GetLocations();
3053 Location out = locations->Out();
3054 Location in = locations->InAt(0);
3055 switch (neg->GetResultType()) {
3056 case Primitive::kPrimInt:
3057 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003058 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01003059 break;
3060
3061 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003062 DCHECK(in.IsRegisterPair());
3063 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3064 __ rsbs(out.AsRegisterPairLow<Register>(),
3065 in.AsRegisterPairLow<Register>(),
3066 ShifterOperand(0));
3067 // We cannot emit an RSC (Reverse Subtract with Carry)
3068 // instruction here, as it does not exist in the Thumb-2
3069 // instruction set. We use the following approach
3070 // using SBC and SUB instead.
3071 //
3072 // out.hi = -C
3073 __ sbc(out.AsRegisterPairHigh<Register>(),
3074 out.AsRegisterPairHigh<Register>(),
3075 ShifterOperand(out.AsRegisterPairHigh<Register>()));
3076 // out.hi = out.hi - in.hi
3077 __ sub(out.AsRegisterPairHigh<Register>(),
3078 out.AsRegisterPairHigh<Register>(),
3079 ShifterOperand(in.AsRegisterPairHigh<Register>()));
3080 break;
3081
Roland Levillain88cb1752014-10-20 16:36:47 +01003082 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003083 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003084 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00003085 break;
3086
Roland Levillain88cb1752014-10-20 16:36:47 +01003087 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003088 DCHECK(in.IsFpuRegisterPair());
3089 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3090 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01003091 break;
3092
3093 default:
3094 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3095 }
3096}
3097
Roland Levillaindff1f282014-11-05 14:15:05 +00003098void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003099 Primitive::Type result_type = conversion->GetResultType();
3100 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003101 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00003102
Roland Levillain5b3ee562015-04-14 16:02:41 +01003103 // The float-to-long, double-to-long and long-to-float type conversions
3104 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003105 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01003106 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3107 && result_type == Primitive::kPrimLong)
3108 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003109 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00003110 : LocationSummary::kNoCall;
3111 LocationSummary* locations =
3112 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3113
David Brazdilb2bd1c52015-03-25 11:17:37 +00003114 // The Java language does not allow treating boolean as an integral type but
3115 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00003116
Roland Levillaindff1f282014-11-05 14:15:05 +00003117 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003118 case Primitive::kPrimByte:
3119 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003120 case Primitive::kPrimLong:
3121 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003122 case Primitive::kPrimBoolean:
3123 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003124 case Primitive::kPrimShort:
3125 case Primitive::kPrimInt:
3126 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003127 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003128 locations->SetInAt(0, Location::RequiresRegister());
3129 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3130 break;
3131
3132 default:
3133 LOG(FATAL) << "Unexpected type conversion from " << input_type
3134 << " to " << result_type;
3135 }
3136 break;
3137
Roland Levillain01a8d712014-11-14 16:27:39 +00003138 case Primitive::kPrimShort:
3139 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003140 case Primitive::kPrimLong:
3141 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003142 case Primitive::kPrimBoolean:
3143 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003144 case Primitive::kPrimByte:
3145 case Primitive::kPrimInt:
3146 case Primitive::kPrimChar:
3147 // Processing a Dex `int-to-short' instruction.
3148 locations->SetInAt(0, Location::RequiresRegister());
3149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3150 break;
3151
3152 default:
3153 LOG(FATAL) << "Unexpected type conversion from " << input_type
3154 << " to " << result_type;
3155 }
3156 break;
3157
Roland Levillain946e1432014-11-11 17:35:19 +00003158 case Primitive::kPrimInt:
3159 switch (input_type) {
3160 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003161 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003162 locations->SetInAt(0, Location::Any());
3163 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3164 break;
3165
3166 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00003167 // Processing a Dex `float-to-int' instruction.
3168 locations->SetInAt(0, Location::RequiresFpuRegister());
3169 locations->SetOut(Location::RequiresRegister());
3170 locations->AddTemp(Location::RequiresFpuRegister());
3171 break;
3172
Roland Levillain946e1432014-11-11 17:35:19 +00003173 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003174 // Processing a Dex `double-to-int' instruction.
3175 locations->SetInAt(0, Location::RequiresFpuRegister());
3176 locations->SetOut(Location::RequiresRegister());
3177 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00003178 break;
3179
3180 default:
3181 LOG(FATAL) << "Unexpected type conversion from " << input_type
3182 << " to " << result_type;
3183 }
3184 break;
3185
Roland Levillaindff1f282014-11-05 14:15:05 +00003186 case Primitive::kPrimLong:
3187 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003188 case Primitive::kPrimBoolean:
3189 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003190 case Primitive::kPrimByte:
3191 case Primitive::kPrimShort:
3192 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003193 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003194 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003195 locations->SetInAt(0, Location::RequiresRegister());
3196 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3197 break;
3198
Roland Levillain624279f2014-12-04 11:54:28 +00003199 case Primitive::kPrimFloat: {
3200 // Processing a Dex `float-to-long' instruction.
3201 InvokeRuntimeCallingConvention calling_convention;
3202 locations->SetInAt(0, Location::FpuRegisterLocation(
3203 calling_convention.GetFpuRegisterAt(0)));
3204 locations->SetOut(Location::RegisterPairLocation(R0, R1));
3205 break;
3206 }
3207
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003208 case Primitive::kPrimDouble: {
3209 // Processing a Dex `double-to-long' instruction.
3210 InvokeRuntimeCallingConvention calling_convention;
3211 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3212 calling_convention.GetFpuRegisterAt(0),
3213 calling_convention.GetFpuRegisterAt(1)));
3214 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00003215 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003216 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003217
3218 default:
3219 LOG(FATAL) << "Unexpected type conversion from " << input_type
3220 << " to " << result_type;
3221 }
3222 break;
3223
Roland Levillain981e4542014-11-14 11:47:14 +00003224 case Primitive::kPrimChar:
3225 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003226 case Primitive::kPrimLong:
3227 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003228 case Primitive::kPrimBoolean:
3229 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003230 case Primitive::kPrimByte:
3231 case Primitive::kPrimShort:
3232 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003233 // Processing a Dex `int-to-char' instruction.
3234 locations->SetInAt(0, Location::RequiresRegister());
3235 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3236 break;
3237
3238 default:
3239 LOG(FATAL) << "Unexpected type conversion from " << input_type
3240 << " to " << result_type;
3241 }
3242 break;
3243
Roland Levillaindff1f282014-11-05 14:15:05 +00003244 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003245 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003246 case Primitive::kPrimBoolean:
3247 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003248 case Primitive::kPrimByte:
3249 case Primitive::kPrimShort:
3250 case Primitive::kPrimInt:
3251 case Primitive::kPrimChar:
3252 // Processing a Dex `int-to-float' instruction.
3253 locations->SetInAt(0, Location::RequiresRegister());
3254 locations->SetOut(Location::RequiresFpuRegister());
3255 break;
3256
Roland Levillain5b3ee562015-04-14 16:02:41 +01003257 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00003258 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01003259 InvokeRuntimeCallingConvention calling_convention;
3260 locations->SetInAt(0, Location::RegisterPairLocation(
3261 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3262 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00003263 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01003264 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003265
Roland Levillaincff13742014-11-17 14:32:17 +00003266 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003267 // Processing a Dex `double-to-float' instruction.
3268 locations->SetInAt(0, Location::RequiresFpuRegister());
3269 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003270 break;
3271
3272 default:
3273 LOG(FATAL) << "Unexpected type conversion from " << input_type
3274 << " to " << result_type;
3275 };
3276 break;
3277
Roland Levillaindff1f282014-11-05 14:15:05 +00003278 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003279 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003280 case Primitive::kPrimBoolean:
3281 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003282 case Primitive::kPrimByte:
3283 case Primitive::kPrimShort:
3284 case Primitive::kPrimInt:
3285 case Primitive::kPrimChar:
3286 // Processing a Dex `int-to-double' instruction.
3287 locations->SetInAt(0, Location::RequiresRegister());
3288 locations->SetOut(Location::RequiresFpuRegister());
3289 break;
3290
3291 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003292 // Processing a Dex `long-to-double' instruction.
3293 locations->SetInAt(0, Location::RequiresRegister());
3294 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01003295 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00003296 locations->AddTemp(Location::RequiresFpuRegister());
3297 break;
3298
Roland Levillaincff13742014-11-17 14:32:17 +00003299 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003300 // Processing a Dex `float-to-double' instruction.
3301 locations->SetInAt(0, Location::RequiresFpuRegister());
3302 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003303 break;
3304
3305 default:
3306 LOG(FATAL) << "Unexpected type conversion from " << input_type
3307 << " to " << result_type;
3308 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003309 break;
3310
3311 default:
3312 LOG(FATAL) << "Unexpected type conversion from " << input_type
3313 << " to " << result_type;
3314 }
3315}
3316
3317void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
3318 LocationSummary* locations = conversion->GetLocations();
3319 Location out = locations->Out();
3320 Location in = locations->InAt(0);
3321 Primitive::Type result_type = conversion->GetResultType();
3322 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003323 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00003324 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003325 case Primitive::kPrimByte:
3326 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003327 case Primitive::kPrimLong:
3328 // Type conversion from long to byte is a result of code transformations.
3329 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
3330 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003331 case Primitive::kPrimBoolean:
3332 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003333 case Primitive::kPrimShort:
3334 case Primitive::kPrimInt:
3335 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003336 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003337 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00003338 break;
3339
3340 default:
3341 LOG(FATAL) << "Unexpected type conversion from " << input_type
3342 << " to " << result_type;
3343 }
3344 break;
3345
Roland Levillain01a8d712014-11-14 16:27:39 +00003346 case Primitive::kPrimShort:
3347 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003348 case Primitive::kPrimLong:
3349 // Type conversion from long to short is a result of code transformations.
3350 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3351 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003352 case Primitive::kPrimBoolean:
3353 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003354 case Primitive::kPrimByte:
3355 case Primitive::kPrimInt:
3356 case Primitive::kPrimChar:
3357 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003358 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00003359 break;
3360
3361 default:
3362 LOG(FATAL) << "Unexpected type conversion from " << input_type
3363 << " to " << result_type;
3364 }
3365 break;
3366
Roland Levillain946e1432014-11-11 17:35:19 +00003367 case Primitive::kPrimInt:
3368 switch (input_type) {
3369 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003370 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003371 DCHECK(out.IsRegister());
3372 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003373 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003374 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003375 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00003376 } else {
3377 DCHECK(in.IsConstant());
3378 DCHECK(in.GetConstant()->IsLongConstant());
3379 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003380 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00003381 }
3382 break;
3383
Roland Levillain3f8f9362014-12-02 17:45:01 +00003384 case Primitive::kPrimFloat: {
3385 // Processing a Dex `float-to-int' instruction.
3386 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003387 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00003388 __ vmovrs(out.AsRegister<Register>(), temp);
3389 break;
3390 }
3391
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003392 case Primitive::kPrimDouble: {
3393 // Processing a Dex `double-to-int' instruction.
3394 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003395 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003396 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00003397 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003398 }
Roland Levillain946e1432014-11-11 17:35:19 +00003399
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 DCHECK(out.IsRegisterPair());
3416 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003417 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00003418 // Sign extension.
3419 __ Asr(out.AsRegisterPairHigh<Register>(),
3420 out.AsRegisterPairLow<Register>(),
3421 31);
3422 break;
3423
3424 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00003425 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003426 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003427 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003428 break;
3429
Roland Levillaindff1f282014-11-05 14:15:05 +00003430 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003431 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003432 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003433 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003434 break;
3435
3436 default:
3437 LOG(FATAL) << "Unexpected type conversion from " << input_type
3438 << " to " << result_type;
3439 }
3440 break;
3441
Roland Levillain981e4542014-11-14 11:47:14 +00003442 case Primitive::kPrimChar:
3443 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003444 case Primitive::kPrimLong:
3445 // Type conversion from long to char is a result of code transformations.
3446 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3447 break;
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.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003454 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00003455 break;
3456
3457 default:
3458 LOG(FATAL) << "Unexpected type conversion from " << input_type
3459 << " to " << result_type;
3460 }
3461 break;
3462
Roland Levillaindff1f282014-11-05 14:15:05 +00003463 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003464 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003465 case Primitive::kPrimBoolean:
3466 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003467 case Primitive::kPrimByte:
3468 case Primitive::kPrimShort:
3469 case Primitive::kPrimInt:
3470 case Primitive::kPrimChar: {
3471 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003472 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
3473 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003474 break;
3475 }
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.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003479 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003480 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00003481 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003482
Roland Levillaincff13742014-11-17 14:32:17 +00003483 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003484 // Processing a Dex `double-to-float' instruction.
3485 __ vcvtsd(out.AsFpuRegister<SRegister>(),
3486 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00003487 break;
3488
3489 default:
3490 LOG(FATAL) << "Unexpected type conversion from " << input_type
3491 << " to " << result_type;
3492 };
3493 break;
3494
Roland Levillaindff1f282014-11-05 14:15:05 +00003495 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003496 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003497 case Primitive::kPrimBoolean:
3498 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003499 case Primitive::kPrimByte:
3500 case Primitive::kPrimShort:
3501 case Primitive::kPrimInt:
3502 case Primitive::kPrimChar: {
3503 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003504 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003505 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3506 out.AsFpuRegisterPairLow<SRegister>());
3507 break;
3508 }
3509
Roland Levillain647b9ed2014-11-27 12:06:00 +00003510 case Primitive::kPrimLong: {
3511 // Processing a Dex `long-to-double' instruction.
3512 Register low = in.AsRegisterPairLow<Register>();
3513 Register high = in.AsRegisterPairHigh<Register>();
3514 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
3515 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003516 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00003517 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003518 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
3519 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003520
Roland Levillain682393c2015-04-14 15:57:52 +01003521 // temp_d = int-to-double(high)
3522 __ vmovsr(temp_s, high);
3523 __ vcvtdi(temp_d, temp_s);
3524 // constant_d = k2Pow32EncodingForDouble
3525 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
3526 // out_d = unsigned-to-double(low)
3527 __ vmovsr(out_s, low);
3528 __ vcvtdu(out_d, out_s);
3529 // out_d += temp_d * constant_d
3530 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003531 break;
3532 }
3533
Roland Levillaincff13742014-11-17 14:32:17 +00003534 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003535 // Processing a Dex `float-to-double' instruction.
3536 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3537 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003538 break;
3539
3540 default:
3541 LOG(FATAL) << "Unexpected type conversion from " << input_type
3542 << " to " << result_type;
3543 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003544 break;
3545
3546 default:
3547 LOG(FATAL) << "Unexpected type conversion from " << input_type
3548 << " to " << result_type;
3549 }
3550}
3551
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003552void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003553 LocationSummary* locations =
3554 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003555 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003556 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003557 locations->SetInAt(0, Location::RequiresRegister());
3558 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003559 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3560 break;
3561 }
3562
3563 case Primitive::kPrimLong: {
3564 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003565 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003566 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003567 break;
3568 }
3569
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003570 case Primitive::kPrimFloat:
3571 case Primitive::kPrimDouble: {
3572 locations->SetInAt(0, Location::RequiresFpuRegister());
3573 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003574 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003575 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003576 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003577
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003578 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003579 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003580 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003581}
3582
3583void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
3584 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003585 Location out = locations->Out();
3586 Location first = locations->InAt(0);
3587 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003588 switch (add->GetResultType()) {
3589 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003590 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003591 __ add(out.AsRegister<Register>(),
3592 first.AsRegister<Register>(),
3593 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003594 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003595 __ AddConstant(out.AsRegister<Register>(),
3596 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003597 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003598 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003599 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003600
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003601 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003602 if (second.IsConstant()) {
3603 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3604 GenerateAddLongConst(out, first, value);
3605 } else {
3606 DCHECK(second.IsRegisterPair());
3607 __ adds(out.AsRegisterPairLow<Register>(),
3608 first.AsRegisterPairLow<Register>(),
3609 ShifterOperand(second.AsRegisterPairLow<Register>()));
3610 __ adc(out.AsRegisterPairHigh<Register>(),
3611 first.AsRegisterPairHigh<Register>(),
3612 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3613 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003614 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003615 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003616
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003617 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00003618 __ vadds(out.AsFpuRegister<SRegister>(),
3619 first.AsFpuRegister<SRegister>(),
3620 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003621 break;
3622
3623 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003624 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3625 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3626 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003627 break;
3628
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003629 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003630 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003631 }
3632}
3633
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003634void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003635 LocationSummary* locations =
3636 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003637 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003638 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003639 locations->SetInAt(0, Location::RequiresRegister());
3640 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003641 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3642 break;
3643 }
3644
3645 case Primitive::kPrimLong: {
3646 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003647 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003648 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003649 break;
3650 }
Calin Juravle11351682014-10-23 15:38:15 +01003651 case Primitive::kPrimFloat:
3652 case Primitive::kPrimDouble: {
3653 locations->SetInAt(0, Location::RequiresFpuRegister());
3654 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003655 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003656 break;
Calin Juravle11351682014-10-23 15:38:15 +01003657 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003658 default:
Calin Juravle11351682014-10-23 15:38:15 +01003659 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003660 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003661}
3662
3663void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3664 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003665 Location out = locations->Out();
3666 Location first = locations->InAt(0);
3667 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003668 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003669 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003670 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003671 __ sub(out.AsRegister<Register>(),
3672 first.AsRegister<Register>(),
3673 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003674 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003675 __ AddConstant(out.AsRegister<Register>(),
3676 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003677 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003678 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003679 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003680 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003681
Calin Juravle11351682014-10-23 15:38:15 +01003682 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003683 if (second.IsConstant()) {
3684 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3685 GenerateAddLongConst(out, first, -value);
3686 } else {
3687 DCHECK(second.IsRegisterPair());
3688 __ subs(out.AsRegisterPairLow<Register>(),
3689 first.AsRegisterPairLow<Register>(),
3690 ShifterOperand(second.AsRegisterPairLow<Register>()));
3691 __ sbc(out.AsRegisterPairHigh<Register>(),
3692 first.AsRegisterPairHigh<Register>(),
3693 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3694 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003695 break;
Calin Juravle11351682014-10-23 15:38:15 +01003696 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003697
Calin Juravle11351682014-10-23 15:38:15 +01003698 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003699 __ vsubs(out.AsFpuRegister<SRegister>(),
3700 first.AsFpuRegister<SRegister>(),
3701 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003702 break;
Calin Juravle11351682014-10-23 15:38:15 +01003703 }
3704
3705 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003706 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3707 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3708 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003709 break;
3710 }
3711
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003712
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003713 default:
Calin Juravle11351682014-10-23 15:38:15 +01003714 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003715 }
3716}
3717
Calin Juravle34bacdf2014-10-07 20:23:36 +01003718void LocationsBuilderARM::VisitMul(HMul* mul) {
3719 LocationSummary* locations =
3720 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3721 switch (mul->GetResultType()) {
3722 case Primitive::kPrimInt:
3723 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003724 locations->SetInAt(0, Location::RequiresRegister());
3725 locations->SetInAt(1, Location::RequiresRegister());
3726 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003727 break;
3728 }
3729
Calin Juravleb5bfa962014-10-21 18:02:24 +01003730 case Primitive::kPrimFloat:
3731 case Primitive::kPrimDouble: {
3732 locations->SetInAt(0, Location::RequiresFpuRegister());
3733 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003734 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003735 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003736 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003737
3738 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003739 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003740 }
3741}
3742
3743void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3744 LocationSummary* locations = mul->GetLocations();
3745 Location out = locations->Out();
3746 Location first = locations->InAt(0);
3747 Location second = locations->InAt(1);
3748 switch (mul->GetResultType()) {
3749 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003750 __ mul(out.AsRegister<Register>(),
3751 first.AsRegister<Register>(),
3752 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003753 break;
3754 }
3755 case Primitive::kPrimLong: {
3756 Register out_hi = out.AsRegisterPairHigh<Register>();
3757 Register out_lo = out.AsRegisterPairLow<Register>();
3758 Register in1_hi = first.AsRegisterPairHigh<Register>();
3759 Register in1_lo = first.AsRegisterPairLow<Register>();
3760 Register in2_hi = second.AsRegisterPairHigh<Register>();
3761 Register in2_lo = second.AsRegisterPairLow<Register>();
3762
3763 // Extra checks to protect caused by the existence of R1_R2.
3764 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3765 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3766 DCHECK_NE(out_hi, in1_lo);
3767 DCHECK_NE(out_hi, in2_lo);
3768
3769 // input: in1 - 64 bits, in2 - 64 bits
3770 // output: out
3771 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3772 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3773 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3774
3775 // IP <- in1.lo * in2.hi
3776 __ mul(IP, in1_lo, in2_hi);
3777 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3778 __ mla(out_hi, in1_hi, in2_lo, IP);
3779 // out.lo <- (in1.lo * in2.lo)[31:0];
3780 __ umull(out_lo, IP, in1_lo, in2_lo);
3781 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3782 __ add(out_hi, out_hi, ShifterOperand(IP));
3783 break;
3784 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003785
3786 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003787 __ vmuls(out.AsFpuRegister<SRegister>(),
3788 first.AsFpuRegister<SRegister>(),
3789 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003790 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003791 }
3792
3793 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003794 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3795 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3796 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003797 break;
3798 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003799
3800 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003801 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003802 }
3803}
3804
Zheng Xuc6667102015-05-15 16:08:45 +08003805void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3806 DCHECK(instruction->IsDiv() || instruction->IsRem());
3807 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3808
3809 LocationSummary* locations = instruction->GetLocations();
3810 Location second = locations->InAt(1);
3811 DCHECK(second.IsConstant());
3812
3813 Register out = locations->Out().AsRegister<Register>();
3814 Register dividend = locations->InAt(0).AsRegister<Register>();
3815 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3816 DCHECK(imm == 1 || imm == -1);
3817
3818 if (instruction->IsRem()) {
3819 __ LoadImmediate(out, 0);
3820 } else {
3821 if (imm == 1) {
3822 __ Mov(out, dividend);
3823 } else {
3824 __ rsb(out, dividend, ShifterOperand(0));
3825 }
3826 }
3827}
3828
3829void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3830 DCHECK(instruction->IsDiv() || instruction->IsRem());
3831 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3832
3833 LocationSummary* locations = instruction->GetLocations();
3834 Location second = locations->InAt(1);
3835 DCHECK(second.IsConstant());
3836
3837 Register out = locations->Out().AsRegister<Register>();
3838 Register dividend = locations->InAt(0).AsRegister<Register>();
3839 Register temp = locations->GetTemp(0).AsRegister<Register>();
3840 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003841 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08003842 int ctz_imm = CTZ(abs_imm);
3843
3844 if (ctz_imm == 1) {
3845 __ Lsr(temp, dividend, 32 - ctz_imm);
3846 } else {
3847 __ Asr(temp, dividend, 31);
3848 __ Lsr(temp, temp, 32 - ctz_imm);
3849 }
3850 __ add(out, temp, ShifterOperand(dividend));
3851
3852 if (instruction->IsDiv()) {
3853 __ Asr(out, out, ctz_imm);
3854 if (imm < 0) {
3855 __ rsb(out, out, ShifterOperand(0));
3856 }
3857 } else {
3858 __ ubfx(out, out, 0, ctz_imm);
3859 __ sub(out, out, ShifterOperand(temp));
3860 }
3861}
3862
3863void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3864 DCHECK(instruction->IsDiv() || instruction->IsRem());
3865 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3866
3867 LocationSummary* locations = instruction->GetLocations();
3868 Location second = locations->InAt(1);
3869 DCHECK(second.IsConstant());
3870
3871 Register out = locations->Out().AsRegister<Register>();
3872 Register dividend = locations->InAt(0).AsRegister<Register>();
3873 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3874 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
3875 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3876
3877 int64_t magic;
3878 int shift;
3879 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3880
3881 __ LoadImmediate(temp1, magic);
3882 __ smull(temp2, temp1, dividend, temp1);
3883
3884 if (imm > 0 && magic < 0) {
3885 __ add(temp1, temp1, ShifterOperand(dividend));
3886 } else if (imm < 0 && magic > 0) {
3887 __ sub(temp1, temp1, ShifterOperand(dividend));
3888 }
3889
3890 if (shift != 0) {
3891 __ Asr(temp1, temp1, shift);
3892 }
3893
3894 if (instruction->IsDiv()) {
3895 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
3896 } else {
3897 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
3898 // TODO: Strength reduction for mls.
3899 __ LoadImmediate(temp2, imm);
3900 __ mls(out, temp1, temp2, dividend);
3901 }
3902}
3903
3904void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
3905 DCHECK(instruction->IsDiv() || instruction->IsRem());
3906 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
3907
3908 LocationSummary* locations = instruction->GetLocations();
3909 Location second = locations->InAt(1);
3910 DCHECK(second.IsConstant());
3911
3912 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3913 if (imm == 0) {
3914 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3915 } else if (imm == 1 || imm == -1) {
3916 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003917 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003918 DivRemByPowerOfTwo(instruction);
3919 } else {
3920 DCHECK(imm <= -2 || imm >= 2);
3921 GenerateDivRemWithAnyConstant(instruction);
3922 }
3923}
3924
Calin Juravle7c4954d2014-10-28 16:57:40 +00003925void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003926 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3927 if (div->GetResultType() == Primitive::kPrimLong) {
3928 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003929 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08003930 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
3931 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003932 } else if (div->GetResultType() == Primitive::kPrimInt &&
3933 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
3934 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003935 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003936 }
3937
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003938 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3939
Calin Juravle7c4954d2014-10-28 16:57:40 +00003940 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003941 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003942 if (div->InputAt(1)->IsConstant()) {
3943 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003944 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08003945 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003946 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
3947 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08003948 // No temp register required.
3949 } else {
3950 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003951 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08003952 locations->AddTemp(Location::RequiresRegister());
3953 }
3954 }
3955 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003956 locations->SetInAt(0, Location::RequiresRegister());
3957 locations->SetInAt(1, Location::RequiresRegister());
3958 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3959 } else {
3960 InvokeRuntimeCallingConvention calling_convention;
3961 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3962 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01003963 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07003964 // we only need the former.
3965 locations->SetOut(Location::RegisterLocation(R0));
3966 }
Calin Juravled0d48522014-11-04 16:40:20 +00003967 break;
3968 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003969 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003970 InvokeRuntimeCallingConvention calling_convention;
3971 locations->SetInAt(0, Location::RegisterPairLocation(
3972 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3973 locations->SetInAt(1, Location::RegisterPairLocation(
3974 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003975 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003976 break;
3977 }
3978 case Primitive::kPrimFloat:
3979 case Primitive::kPrimDouble: {
3980 locations->SetInAt(0, Location::RequiresFpuRegister());
3981 locations->SetInAt(1, Location::RequiresFpuRegister());
3982 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3983 break;
3984 }
3985
3986 default:
3987 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3988 }
3989}
3990
3991void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
3992 LocationSummary* locations = div->GetLocations();
3993 Location out = locations->Out();
3994 Location first = locations->InAt(0);
3995 Location second = locations->InAt(1);
3996
3997 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003998 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08003999 if (second.IsConstant()) {
4000 GenerateDivRemConstantIntegral(div);
4001 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004002 __ sdiv(out.AsRegister<Register>(),
4003 first.AsRegister<Register>(),
4004 second.AsRegister<Register>());
4005 } else {
4006 InvokeRuntimeCallingConvention calling_convention;
4007 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4008 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4009 DCHECK_EQ(R0, out.AsRegister<Register>());
4010
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004011 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004012 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004013 }
Calin Juravled0d48522014-11-04 16:40:20 +00004014 break;
4015 }
4016
Calin Juravle7c4954d2014-10-28 16:57:40 +00004017 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004018 InvokeRuntimeCallingConvention calling_convention;
4019 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4020 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4021 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4022 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4023 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004024 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004025
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004026 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004027 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00004028 break;
4029 }
4030
4031 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004032 __ vdivs(out.AsFpuRegister<SRegister>(),
4033 first.AsFpuRegister<SRegister>(),
4034 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004035 break;
4036 }
4037
4038 case Primitive::kPrimDouble: {
4039 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4040 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4041 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
4042 break;
4043 }
4044
4045 default:
4046 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4047 }
4048}
4049
Calin Juravlebacfec32014-11-14 15:54:36 +00004050void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004051 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004052
4053 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004054 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004055 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4056 // sdiv will be replaced by other instruction sequence.
4057 call_kind = LocationSummary::kNoCall;
4058 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4059 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004060 // Have hardware divide instruction for int, do it with three instructions.
4061 call_kind = LocationSummary::kNoCall;
4062 }
4063
Calin Juravlebacfec32014-11-14 15:54:36 +00004064 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4065
Calin Juravled2ec87d2014-12-08 14:24:46 +00004066 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004067 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004068 if (rem->InputAt(1)->IsConstant()) {
4069 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004070 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004071 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004072 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
4073 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004074 // No temp register required.
4075 } else {
4076 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004077 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004078 locations->AddTemp(Location::RequiresRegister());
4079 }
4080 }
4081 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004082 locations->SetInAt(0, Location::RequiresRegister());
4083 locations->SetInAt(1, Location::RequiresRegister());
4084 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4085 locations->AddTemp(Location::RequiresRegister());
4086 } else {
4087 InvokeRuntimeCallingConvention calling_convention;
4088 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4089 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004090 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004091 // we only need the latter.
4092 locations->SetOut(Location::RegisterLocation(R1));
4093 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004094 break;
4095 }
4096 case Primitive::kPrimLong: {
4097 InvokeRuntimeCallingConvention calling_convention;
4098 locations->SetInAt(0, Location::RegisterPairLocation(
4099 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4100 locations->SetInAt(1, Location::RegisterPairLocation(
4101 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4102 // The runtime helper puts the output in R2,R3.
4103 locations->SetOut(Location::RegisterPairLocation(R2, R3));
4104 break;
4105 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00004106 case Primitive::kPrimFloat: {
4107 InvokeRuntimeCallingConvention calling_convention;
4108 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4109 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4110 locations->SetOut(Location::FpuRegisterLocation(S0));
4111 break;
4112 }
4113
Calin Juravlebacfec32014-11-14 15:54:36 +00004114 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004115 InvokeRuntimeCallingConvention calling_convention;
4116 locations->SetInAt(0, Location::FpuRegisterPairLocation(
4117 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4118 locations->SetInAt(1, Location::FpuRegisterPairLocation(
4119 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4120 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00004121 break;
4122 }
4123
4124 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004125 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004126 }
4127}
4128
4129void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
4130 LocationSummary* locations = rem->GetLocations();
4131 Location out = locations->Out();
4132 Location first = locations->InAt(0);
4133 Location second = locations->InAt(1);
4134
Calin Juravled2ec87d2014-12-08 14:24:46 +00004135 Primitive::Type type = rem->GetResultType();
4136 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004137 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004138 if (second.IsConstant()) {
4139 GenerateDivRemConstantIntegral(rem);
4140 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004141 Register reg1 = first.AsRegister<Register>();
4142 Register reg2 = second.AsRegister<Register>();
4143 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004144
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004145 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004146 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004147 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004148 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004149 } else {
4150 InvokeRuntimeCallingConvention calling_convention;
4151 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4152 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4153 DCHECK_EQ(R1, out.AsRegister<Register>());
4154
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004155 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004156 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004157 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004158 break;
4159 }
4160
4161 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004162 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004163 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004164 break;
4165 }
4166
Calin Juravled2ec87d2014-12-08 14:24:46 +00004167 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004168 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004169 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00004170 break;
4171 }
4172
Calin Juravlebacfec32014-11-14 15:54:36 +00004173 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004174 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004175 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004176 break;
4177 }
4178
4179 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004180 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004181 }
4182}
4183
Calin Juravled0d48522014-11-04 16:40:20 +00004184void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004185 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004186 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00004187}
4188
4189void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004190 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004191 codegen_->AddSlowPath(slow_path);
4192
4193 LocationSummary* locations = instruction->GetLocations();
4194 Location value = locations->InAt(0);
4195
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004196 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00004197 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004198 case Primitive::kPrimByte:
4199 case Primitive::kPrimChar:
4200 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004201 case Primitive::kPrimInt: {
4202 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004203 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004204 } else {
4205 DCHECK(value.IsConstant()) << value;
4206 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4207 __ b(slow_path->GetEntryLabel());
4208 }
4209 }
4210 break;
4211 }
4212 case Primitive::kPrimLong: {
4213 if (value.IsRegisterPair()) {
4214 __ orrs(IP,
4215 value.AsRegisterPairLow<Register>(),
4216 ShifterOperand(value.AsRegisterPairHigh<Register>()));
4217 __ b(slow_path->GetEntryLabel(), EQ);
4218 } else {
4219 DCHECK(value.IsConstant()) << value;
4220 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4221 __ b(slow_path->GetEntryLabel());
4222 }
4223 }
4224 break;
4225 default:
4226 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4227 }
4228 }
Calin Juravled0d48522014-11-04 16:40:20 +00004229}
4230
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004231void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
4232 Register in = locations->InAt(0).AsRegister<Register>();
4233 Location rhs = locations->InAt(1);
4234 Register out = locations->Out().AsRegister<Register>();
4235
4236 if (rhs.IsConstant()) {
4237 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4238 // so map all rotations to a +ve. equivalent in that range.
4239 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4240 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4241 if (rot) {
4242 // Rotate, mapping left rotations to right equivalents if necessary.
4243 // (e.g. left by 2 bits == right by 30.)
4244 __ Ror(out, in, rot);
4245 } else if (out != in) {
4246 __ Mov(out, in);
4247 }
4248 } else {
4249 __ Ror(out, in, rhs.AsRegister<Register>());
4250 }
4251}
4252
4253// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4254// rotates by swapping input regs (effectively rotating by the first 32-bits of
4255// a larger rotation) or flipping direction (thus treating larger right/left
4256// rotations as sub-word sized rotations in the other direction) as appropriate.
4257void InstructionCodeGeneratorARM::HandleLongRotate(LocationSummary* locations) {
4258 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
4259 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
4260 Location rhs = locations->InAt(1);
4261 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
4262 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
4263
4264 if (rhs.IsConstant()) {
4265 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4266 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00004267 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004268 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4269 // logic below to a simple pair of binary orr.
4270 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4271 if (rot >= kArmBitsPerWord) {
4272 rot -= kArmBitsPerWord;
4273 std::swap(in_reg_hi, in_reg_lo);
4274 }
4275 // Rotate, or mov to out for zero or word size rotations.
4276 if (rot != 0u) {
4277 __ Lsr(out_reg_hi, in_reg_hi, rot);
4278 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
4279 __ Lsr(out_reg_lo, in_reg_lo, rot);
4280 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
4281 } else {
4282 __ Mov(out_reg_lo, in_reg_lo);
4283 __ Mov(out_reg_hi, in_reg_hi);
4284 }
4285 } else {
4286 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
4287 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
4288 Label end;
4289 Label shift_by_32_plus_shift_right;
4290
4291 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
4292 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
4293 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
4294 __ b(&shift_by_32_plus_shift_right, CC);
4295
4296 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4297 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4298 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4299 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4300 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4301 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4302 __ Lsr(shift_left, in_reg_hi, shift_right);
4303 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
4304 __ b(&end);
4305
4306 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4307 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4308 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4309 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4310 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4311 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4312 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4313 __ Lsl(shift_right, in_reg_hi, shift_left);
4314 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
4315
4316 __ Bind(&end);
4317 }
4318}
Roland Levillain22c49222016-03-18 14:04:28 +00004319
4320void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004321 LocationSummary* locations =
4322 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4323 switch (ror->GetResultType()) {
4324 case Primitive::kPrimInt: {
4325 locations->SetInAt(0, Location::RequiresRegister());
4326 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4327 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4328 break;
4329 }
4330 case Primitive::kPrimLong: {
4331 locations->SetInAt(0, Location::RequiresRegister());
4332 if (ror->InputAt(1)->IsConstant()) {
4333 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4334 } else {
4335 locations->SetInAt(1, Location::RequiresRegister());
4336 locations->AddTemp(Location::RequiresRegister());
4337 locations->AddTemp(Location::RequiresRegister());
4338 }
4339 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4340 break;
4341 }
4342 default:
4343 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4344 }
4345}
4346
Roland Levillain22c49222016-03-18 14:04:28 +00004347void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004348 LocationSummary* locations = ror->GetLocations();
4349 Primitive::Type type = ror->GetResultType();
4350 switch (type) {
4351 case Primitive::kPrimInt: {
4352 HandleIntegerRotate(locations);
4353 break;
4354 }
4355 case Primitive::kPrimLong: {
4356 HandleLongRotate(locations);
4357 break;
4358 }
4359 default:
4360 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004361 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004362 }
4363}
4364
Calin Juravle9aec02f2014-11-18 23:06:35 +00004365void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
4366 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4367
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004368 LocationSummary* locations =
4369 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004370
4371 switch (op->GetResultType()) {
4372 case Primitive::kPrimInt: {
4373 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004374 if (op->InputAt(1)->IsConstant()) {
4375 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4376 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4377 } else {
4378 locations->SetInAt(1, Location::RequiresRegister());
4379 // Make the output overlap, as it will be used to hold the masked
4380 // second input.
4381 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4382 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004383 break;
4384 }
4385 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004386 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004387 if (op->InputAt(1)->IsConstant()) {
4388 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4389 // For simplicity, use kOutputOverlap even though we only require that low registers
4390 // don't clash with high registers which the register allocator currently guarantees.
4391 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4392 } else {
4393 locations->SetInAt(1, Location::RequiresRegister());
4394 locations->AddTemp(Location::RequiresRegister());
4395 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4396 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004397 break;
4398 }
4399 default:
4400 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4401 }
4402}
4403
4404void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
4405 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4406
4407 LocationSummary* locations = op->GetLocations();
4408 Location out = locations->Out();
4409 Location first = locations->InAt(0);
4410 Location second = locations->InAt(1);
4411
4412 Primitive::Type type = op->GetResultType();
4413 switch (type) {
4414 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004415 Register out_reg = out.AsRegister<Register>();
4416 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004417 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004418 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00004419 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004420 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004421 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004422 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004423 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004424 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004425 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004426 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004427 }
4428 } else {
4429 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004430 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00004431 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00004432 __ Mov(out_reg, first_reg);
4433 } else if (op->IsShl()) {
4434 __ Lsl(out_reg, first_reg, shift_value);
4435 } else if (op->IsShr()) {
4436 __ Asr(out_reg, first_reg, shift_value);
4437 } else {
4438 __ Lsr(out_reg, first_reg, shift_value);
4439 }
4440 }
4441 break;
4442 }
4443 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004444 Register o_h = out.AsRegisterPairHigh<Register>();
4445 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004446
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004447 Register high = first.AsRegisterPairHigh<Register>();
4448 Register low = first.AsRegisterPairLow<Register>();
4449
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004450 if (second.IsRegister()) {
4451 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004452
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004453 Register second_reg = second.AsRegister<Register>();
4454
4455 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004456 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004457 // Shift the high part
4458 __ Lsl(o_h, high, o_l);
4459 // Shift the low part and `or` what overflew on the high part
4460 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
4461 __ Lsr(temp, low, temp);
4462 __ orr(o_h, o_h, ShifterOperand(temp));
4463 // If the shift is > 32 bits, override the high part
4464 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
4465 __ it(PL);
4466 __ Lsl(o_h, low, temp, PL);
4467 // Shift the low part
4468 __ Lsl(o_l, low, o_l);
4469 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004470 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004471 // Shift the low part
4472 __ Lsr(o_l, low, o_h);
4473 // Shift the high part and `or` what underflew on the low part
4474 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4475 __ Lsl(temp, high, temp);
4476 __ orr(o_l, o_l, ShifterOperand(temp));
4477 // If the shift is > 32 bits, override the low part
4478 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4479 __ it(PL);
4480 __ Asr(o_l, high, temp, PL);
4481 // Shift the high part
4482 __ Asr(o_h, high, o_h);
4483 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004484 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004485 // same as Shr except we use `Lsr`s and not `Asr`s
4486 __ Lsr(o_l, low, o_h);
4487 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4488 __ Lsl(temp, high, temp);
4489 __ orr(o_l, o_l, ShifterOperand(temp));
4490 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4491 __ it(PL);
4492 __ Lsr(o_l, high, temp, PL);
4493 __ Lsr(o_h, high, o_h);
4494 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004495 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004496 // Register allocator doesn't create partial overlap.
4497 DCHECK_NE(o_l, high);
4498 DCHECK_NE(o_h, low);
4499 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004500 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004501 if (shift_value > 32) {
4502 if (op->IsShl()) {
4503 __ Lsl(o_h, low, shift_value - 32);
4504 __ LoadImmediate(o_l, 0);
4505 } else if (op->IsShr()) {
4506 __ Asr(o_l, high, shift_value - 32);
4507 __ Asr(o_h, high, 31);
4508 } else {
4509 __ Lsr(o_l, high, shift_value - 32);
4510 __ LoadImmediate(o_h, 0);
4511 }
4512 } else if (shift_value == 32) {
4513 if (op->IsShl()) {
4514 __ mov(o_h, ShifterOperand(low));
4515 __ LoadImmediate(o_l, 0);
4516 } else if (op->IsShr()) {
4517 __ mov(o_l, ShifterOperand(high));
4518 __ Asr(o_h, high, 31);
4519 } else {
4520 __ mov(o_l, ShifterOperand(high));
4521 __ LoadImmediate(o_h, 0);
4522 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00004523 } else if (shift_value == 1) {
4524 if (op->IsShl()) {
4525 __ Lsls(o_l, low, 1);
4526 __ adc(o_h, high, ShifterOperand(high));
4527 } else if (op->IsShr()) {
4528 __ Asrs(o_h, high, 1);
4529 __ Rrx(o_l, low);
4530 } else {
4531 __ Lsrs(o_h, high, 1);
4532 __ Rrx(o_l, low);
4533 }
4534 } else {
4535 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004536 if (op->IsShl()) {
4537 __ Lsl(o_h, high, shift_value);
4538 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
4539 __ Lsl(o_l, low, shift_value);
4540 } else if (op->IsShr()) {
4541 __ Lsr(o_l, low, shift_value);
4542 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4543 __ Asr(o_h, high, shift_value);
4544 } else {
4545 __ Lsr(o_l, low, shift_value);
4546 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4547 __ Lsr(o_h, high, shift_value);
4548 }
4549 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004550 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004551 break;
4552 }
4553 default:
4554 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004555 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004556 }
4557}
4558
4559void LocationsBuilderARM::VisitShl(HShl* shl) {
4560 HandleShift(shl);
4561}
4562
4563void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
4564 HandleShift(shl);
4565}
4566
4567void LocationsBuilderARM::VisitShr(HShr* shr) {
4568 HandleShift(shr);
4569}
4570
4571void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
4572 HandleShift(shr);
4573}
4574
4575void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
4576 HandleShift(ushr);
4577}
4578
4579void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
4580 HandleShift(ushr);
4581}
4582
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004583void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004584 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004585 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00004586 if (instruction->IsStringAlloc()) {
4587 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4588 } else {
4589 InvokeRuntimeCallingConvention calling_convention;
4590 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004591 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004592 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004593}
4594
4595void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004596 // Note: if heap poisoning is enabled, the entry point takes cares
4597 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004598 if (instruction->IsStringAlloc()) {
4599 // String is allocated through StringFactory. Call NewEmptyString entry point.
4600 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004601 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004602 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4603 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
4604 __ blx(LR);
4605 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4606 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004607 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004608 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004609 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004610}
4611
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004612void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
4613 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004614 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004615 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004616 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004617 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4618 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004619}
4620
4621void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004622 // Note: if heap poisoning is enabled, the entry point takes cares
4623 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004624 QuickEntrypointEnum entrypoint =
4625 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4626 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004627 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004628 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004629}
4630
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004631void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004632 LocationSummary* locations =
4633 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004634 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4635 if (location.IsStackSlot()) {
4636 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4637 } else if (location.IsDoubleStackSlot()) {
4638 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004639 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004640 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004641}
4642
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004643void InstructionCodeGeneratorARM::VisitParameterValue(
4644 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004645 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004646}
4647
4648void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4649 LocationSummary* locations =
4650 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4651 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4652}
4653
4654void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4655 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004656}
4657
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004658void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004659 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004660 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004661 locations->SetInAt(0, Location::RequiresRegister());
4662 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004663}
4664
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004665void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4666 LocationSummary* locations = not_->GetLocations();
4667 Location out = locations->Out();
4668 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004669 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004670 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004671 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004672 break;
4673
4674 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004675 __ mvn(out.AsRegisterPairLow<Register>(),
4676 ShifterOperand(in.AsRegisterPairLow<Register>()));
4677 __ mvn(out.AsRegisterPairHigh<Register>(),
4678 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004679 break;
4680
4681 default:
4682 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4683 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004684}
4685
David Brazdil66d126e2015-04-03 16:02:44 +01004686void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4687 LocationSummary* locations =
4688 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4689 locations->SetInAt(0, Location::RequiresRegister());
4690 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4691}
4692
4693void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004694 LocationSummary* locations = bool_not->GetLocations();
4695 Location out = locations->Out();
4696 Location in = locations->InAt(0);
4697 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4698}
4699
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004700void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004701 LocationSummary* locations =
4702 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004703 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004704 case Primitive::kPrimBoolean:
4705 case Primitive::kPrimByte:
4706 case Primitive::kPrimShort:
4707 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004708 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004709 case Primitive::kPrimLong: {
4710 locations->SetInAt(0, Location::RequiresRegister());
4711 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004712 // Output overlaps because it is written before doing the low comparison.
4713 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004714 break;
4715 }
4716 case Primitive::kPrimFloat:
4717 case Primitive::kPrimDouble: {
4718 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004719 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004720 locations->SetOut(Location::RequiresRegister());
4721 break;
4722 }
4723 default:
4724 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4725 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004726}
4727
4728void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004729 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004730 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004731 Location left = locations->InAt(0);
4732 Location right = locations->InAt(1);
4733
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004734 Label less, greater, done;
Calin Juravleddb7df22014-11-25 20:56:51 +00004735 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004736 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004737 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004738 case Primitive::kPrimBoolean:
4739 case Primitive::kPrimByte:
4740 case Primitive::kPrimShort:
4741 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004742 case Primitive::kPrimInt: {
4743 __ LoadImmediate(out, 0);
4744 __ cmp(left.AsRegister<Register>(),
4745 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4746 less_cond = LT;
4747 break;
4748 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004749 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004750 __ cmp(left.AsRegisterPairHigh<Register>(),
4751 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004752 __ b(&less, LT);
4753 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004754 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004755 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004756 __ cmp(left.AsRegisterPairLow<Register>(),
4757 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004758 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004759 break;
4760 }
4761 case Primitive::kPrimFloat:
4762 case Primitive::kPrimDouble: {
4763 __ LoadImmediate(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08004764 GenerateVcmp(compare, codegen_);
Calin Juravleddb7df22014-11-25 20:56:51 +00004765 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004766 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004767 break;
4768 }
4769 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004770 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004771 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004772 }
Aart Bika19616e2016-02-01 18:57:58 -08004773
Calin Juravleddb7df22014-11-25 20:56:51 +00004774 __ b(&done, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004775 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004776
4777 __ Bind(&greater);
4778 __ LoadImmediate(out, 1);
4779 __ b(&done);
4780
4781 __ Bind(&less);
4782 __ LoadImmediate(out, -1);
4783
4784 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004785}
4786
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004787void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004788 LocationSummary* locations =
4789 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004790 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004791 locations->SetInAt(i, Location::Any());
4792 }
4793 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004794}
4795
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004796void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004797 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004798}
4799
Roland Levillainc9285912015-12-18 10:38:42 +00004800void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
4801 // TODO (ported from quick): revisit ARM barrier kinds.
4802 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00004803 switch (kind) {
4804 case MemBarrierKind::kAnyStore:
4805 case MemBarrierKind::kLoadAny:
4806 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004807 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00004808 break;
4809 }
4810 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07004811 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00004812 break;
4813 }
4814 default:
4815 LOG(FATAL) << "Unexpected memory barrier " << kind;
4816 }
Kenny Root1d8199d2015-06-02 11:01:10 -07004817 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00004818}
4819
4820void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
4821 uint32_t offset,
4822 Register out_lo,
4823 Register out_hi) {
4824 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00004825 // Ensure `out_lo` is different from `addr`, so that loading
4826 // `offset` into `out_lo` does not clutter `addr`.
4827 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00004828 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004829 __ add(IP, addr, ShifterOperand(out_lo));
4830 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004831 }
4832 __ ldrexd(out_lo, out_hi, addr);
4833}
4834
4835void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
4836 uint32_t offset,
4837 Register value_lo,
4838 Register value_hi,
4839 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00004840 Register temp2,
4841 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004842 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00004843 if (offset != 0) {
4844 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00004845 __ add(IP, addr, ShifterOperand(temp1));
4846 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00004847 }
4848 __ Bind(&fail);
4849 // We need a load followed by store. (The address used in a STREX instruction must
4850 // be the same as the address in the most recently executed LDREX instruction.)
4851 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00004852 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004853 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004854 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00004855}
4856
4857void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4858 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4859
Nicolas Geoffray39468442014-09-02 15:17:15 +01004860 LocationSummary* locations =
4861 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004862 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004863
Calin Juravle52c48962014-12-16 17:02:57 +00004864 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004865 if (Primitive::IsFloatingPointType(field_type)) {
4866 locations->SetInAt(1, Location::RequiresFpuRegister());
4867 } else {
4868 locations->SetInAt(1, Location::RequiresRegister());
4869 }
4870
Calin Juravle52c48962014-12-16 17:02:57 +00004871 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00004872 bool generate_volatile = field_info.IsVolatile()
4873 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004874 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01004875 bool needs_write_barrier =
4876 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004877 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00004878 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01004879 if (needs_write_barrier) {
4880 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004881 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00004882 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004883 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00004884 // - registers need to be consecutive
4885 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00004886 // We don't test for ARM yet, and the assertion makes sure that we
4887 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00004888 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
4889
4890 locations->AddTemp(Location::RequiresRegister());
4891 locations->AddTemp(Location::RequiresRegister());
4892 if (field_type == Primitive::kPrimDouble) {
4893 // For doubles we need two more registers to copy the value.
4894 locations->AddTemp(Location::RegisterLocation(R2));
4895 locations->AddTemp(Location::RegisterLocation(R3));
4896 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004897 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004898}
4899
Calin Juravle52c48962014-12-16 17:02:57 +00004900void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004901 const FieldInfo& field_info,
4902 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004903 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4904
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004905 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00004906 Register base = locations->InAt(0).AsRegister<Register>();
4907 Location value = locations->InAt(1);
4908
4909 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004910 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00004911 Primitive::Type field_type = field_info.GetFieldType();
4912 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004913 bool needs_write_barrier =
4914 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004915
4916 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00004917 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004918 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004919
4920 switch (field_type) {
4921 case Primitive::kPrimBoolean:
4922 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004923 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004924 break;
4925 }
4926
4927 case Primitive::kPrimShort:
4928 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004929 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004930 break;
4931 }
4932
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004933 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004934 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004935 if (kPoisonHeapReferences && needs_write_barrier) {
4936 // Note that in the case where `value` is a null reference,
4937 // we do not enter this block, as a null reference does not
4938 // need poisoning.
4939 DCHECK_EQ(field_type, Primitive::kPrimNot);
4940 Register temp = locations->GetTemp(0).AsRegister<Register>();
4941 __ Mov(temp, value.AsRegister<Register>());
4942 __ PoisonHeapReference(temp);
4943 __ StoreToOffset(kStoreWord, temp, base, offset);
4944 } else {
4945 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
4946 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004947 break;
4948 }
4949
4950 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00004951 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004952 GenerateWideAtomicStore(base, offset,
4953 value.AsRegisterPairLow<Register>(),
4954 value.AsRegisterPairHigh<Register>(),
4955 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004956 locations->GetTemp(1).AsRegister<Register>(),
4957 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004958 } else {
4959 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004960 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004961 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004962 break;
4963 }
4964
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004965 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004966 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004967 break;
4968 }
4969
4970 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004971 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00004972 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00004973 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
4974 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
4975
4976 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
4977
4978 GenerateWideAtomicStore(base, offset,
4979 value_reg_lo,
4980 value_reg_hi,
4981 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00004982 locations->GetTemp(3).AsRegister<Register>(),
4983 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004984 } else {
4985 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00004986 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004987 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004988 break;
4989 }
4990
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004991 case Primitive::kPrimVoid:
4992 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004993 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004994 }
Calin Juravle52c48962014-12-16 17:02:57 +00004995
Calin Juravle77520bc2015-01-12 18:45:46 +00004996 // Longs and doubles are handled in the switch.
4997 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
4998 codegen_->MaybeRecordImplicitNullCheck(instruction);
4999 }
5000
5001 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5002 Register temp = locations->GetTemp(0).AsRegister<Register>();
5003 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005004 codegen_->MarkGCCard(
5005 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005006 }
5007
Calin Juravle52c48962014-12-16 17:02:57 +00005008 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005009 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005010 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005011}
5012
Calin Juravle52c48962014-12-16 17:02:57 +00005013void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5014 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00005015
5016 bool object_field_get_with_read_barrier =
5017 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005018 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005019 new (GetGraph()->GetArena()) LocationSummary(instruction,
5020 object_field_get_with_read_barrier ?
5021 LocationSummary::kCallOnSlowPath :
5022 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005023 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005024 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005025 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005026 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00005027
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005028 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00005029 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005030 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00005031 // The output overlaps in case of volatile long: we don't want the
5032 // code generated by GenerateWideAtomicLoad to overwrite the
5033 // object's location. Likewise, in the case of an object field get
5034 // with read barriers enabled, we do not want the load to overwrite
5035 // the object's location, as we need it to emit the read barrier.
5036 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5037 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01005038
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005039 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5040 locations->SetOut(Location::RequiresFpuRegister());
5041 } else {
5042 locations->SetOut(Location::RequiresRegister(),
5043 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5044 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005045 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00005046 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005047 // - registers need to be consecutive
5048 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005049 // We don't test for ARM yet, and the assertion makes sure that we
5050 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005051 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5052 locations->AddTemp(Location::RequiresRegister());
5053 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00005054 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5055 // We need a temporary register for the read barrier marking slow
5056 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
5057 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00005058 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005059}
5060
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005061Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5062 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
5063 << input->GetType();
5064 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5065 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5066 return Location::ConstantLocation(input->AsConstant());
5067 } else {
5068 return Location::RequiresFpuRegister();
5069 }
5070}
5071
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005072Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
5073 Opcode opcode) {
5074 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5075 if (constant->IsConstant() &&
5076 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5077 return Location::ConstantLocation(constant->AsConstant());
5078 }
5079 return Location::RequiresRegister();
5080}
5081
5082bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
5083 Opcode opcode) {
5084 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5085 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01005086 Opcode high_opcode = opcode;
5087 SetCc low_set_cc = kCcDontCare;
5088 switch (opcode) {
5089 case SUB:
5090 // Flip the operation to an ADD.
5091 value = -value;
5092 opcode = ADD;
5093 FALLTHROUGH_INTENDED;
5094 case ADD:
5095 if (Low32Bits(value) == 0u) {
5096 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5097 }
5098 high_opcode = ADC;
5099 low_set_cc = kCcSet;
5100 break;
5101 default:
5102 break;
5103 }
5104 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5105 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005106 } else {
5107 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5108 }
5109}
5110
Vladimir Marko59751a72016-08-05 14:37:27 +01005111bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
5112 Opcode opcode,
5113 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005114 ShifterOperand so;
5115 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01005116 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005117 return true;
5118 }
5119 Opcode neg_opcode = kNoOperand;
5120 switch (opcode) {
Vladimir Marko59751a72016-08-05 14:37:27 +01005121 case AND: neg_opcode = BIC; value = ~value; break;
5122 case ORR: neg_opcode = ORN; value = ~value; break;
5123 case ADD: neg_opcode = SUB; value = -value; break;
5124 case ADC: neg_opcode = SBC; value = ~value; break;
5125 case SUB: neg_opcode = ADD; value = -value; break;
5126 case SBC: neg_opcode = ADC; value = ~value; break;
Donghui Bai426b49c2016-11-08 14:55:38 +08005127 case MOV: neg_opcode = MVN; value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005128 default:
5129 return false;
5130 }
Vladimir Marko59751a72016-08-05 14:37:27 +01005131 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, value, set_cc, &so);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005132}
5133
Calin Juravle52c48962014-12-16 17:02:57 +00005134void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
5135 const FieldInfo& field_info) {
5136 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005137
Calin Juravle52c48962014-12-16 17:02:57 +00005138 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005139 Location base_loc = locations->InAt(0);
5140 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005141 Location out = locations->Out();
5142 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005143 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005144 Primitive::Type field_type = field_info.GetFieldType();
5145 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5146
5147 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00005148 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00005149 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005150 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005151
Roland Levillainc9285912015-12-18 10:38:42 +00005152 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00005153 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005154 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005155
Roland Levillainc9285912015-12-18 10:38:42 +00005156 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00005157 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005158 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005159
Roland Levillainc9285912015-12-18 10:38:42 +00005160 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00005161 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005162 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005163
5164 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00005165 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005166 break;
Roland Levillainc9285912015-12-18 10:38:42 +00005167
5168 case Primitive::kPrimNot: {
5169 // /* HeapReference<Object> */ out = *(base + offset)
5170 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5171 Location temp_loc = locations->GetTemp(0);
5172 // Note that a potential implicit null check is handled in this
5173 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
5174 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5175 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5176 if (is_volatile) {
5177 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5178 }
5179 } else {
5180 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
5181 codegen_->MaybeRecordImplicitNullCheck(instruction);
5182 if (is_volatile) {
5183 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5184 }
5185 // If read barriers are enabled, emit read barriers other than
5186 // Baker's using a slow path (and also unpoison the loaded
5187 // reference, if heap poisoning is enabled).
5188 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5189 }
5190 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005191 }
5192
Roland Levillainc9285912015-12-18 10:38:42 +00005193 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00005194 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005195 GenerateWideAtomicLoad(base, offset,
5196 out.AsRegisterPairLow<Register>(),
5197 out.AsRegisterPairHigh<Register>());
5198 } else {
5199 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
5200 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005201 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005202
Roland Levillainc9285912015-12-18 10:38:42 +00005203 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00005204 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005205 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005206
5207 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005208 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005209 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005210 Register lo = locations->GetTemp(0).AsRegister<Register>();
5211 Register hi = locations->GetTemp(1).AsRegister<Register>();
5212 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00005213 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005214 __ vmovdrr(out_reg, lo, hi);
5215 } else {
5216 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005217 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005218 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005219 break;
5220 }
5221
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005222 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005223 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005224 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005225 }
Calin Juravle52c48962014-12-16 17:02:57 +00005226
Roland Levillainc9285912015-12-18 10:38:42 +00005227 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5228 // Potential implicit null checks, in the case of reference or
5229 // double fields, are handled in the previous switch statement.
5230 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005231 codegen_->MaybeRecordImplicitNullCheck(instruction);
5232 }
5233
Calin Juravle52c48962014-12-16 17:02:57 +00005234 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005235 if (field_type == Primitive::kPrimNot) {
5236 // Memory barriers, in the case of references, are also handled
5237 // in the previous switch statement.
5238 } else {
5239 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5240 }
Roland Levillain4d027112015-07-01 15:41:14 +01005241 }
Calin Juravle52c48962014-12-16 17:02:57 +00005242}
5243
5244void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5245 HandleFieldSet(instruction, instruction->GetFieldInfo());
5246}
5247
5248void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005249 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005250}
5251
5252void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5253 HandleFieldGet(instruction, instruction->GetFieldInfo());
5254}
5255
5256void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5257 HandleFieldGet(instruction, instruction->GetFieldInfo());
5258}
5259
5260void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5261 HandleFieldGet(instruction, instruction->GetFieldInfo());
5262}
5263
5264void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5265 HandleFieldGet(instruction, instruction->GetFieldInfo());
5266}
5267
5268void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5269 HandleFieldSet(instruction, instruction->GetFieldInfo());
5270}
5271
5272void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005273 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005274}
5275
Calin Juravlee460d1d2015-09-29 04:52:17 +01005276void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
5277 HUnresolvedInstanceFieldGet* instruction) {
5278 FieldAccessCallingConventionARM calling_convention;
5279 codegen_->CreateUnresolvedFieldLocationSummary(
5280 instruction, instruction->GetFieldType(), calling_convention);
5281}
5282
5283void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
5284 HUnresolvedInstanceFieldGet* instruction) {
5285 FieldAccessCallingConventionARM calling_convention;
5286 codegen_->GenerateUnresolvedFieldAccess(instruction,
5287 instruction->GetFieldType(),
5288 instruction->GetFieldIndex(),
5289 instruction->GetDexPc(),
5290 calling_convention);
5291}
5292
5293void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
5294 HUnresolvedInstanceFieldSet* instruction) {
5295 FieldAccessCallingConventionARM calling_convention;
5296 codegen_->CreateUnresolvedFieldLocationSummary(
5297 instruction, instruction->GetFieldType(), calling_convention);
5298}
5299
5300void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
5301 HUnresolvedInstanceFieldSet* instruction) {
5302 FieldAccessCallingConventionARM calling_convention;
5303 codegen_->GenerateUnresolvedFieldAccess(instruction,
5304 instruction->GetFieldType(),
5305 instruction->GetFieldIndex(),
5306 instruction->GetDexPc(),
5307 calling_convention);
5308}
5309
5310void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
5311 HUnresolvedStaticFieldGet* instruction) {
5312 FieldAccessCallingConventionARM calling_convention;
5313 codegen_->CreateUnresolvedFieldLocationSummary(
5314 instruction, instruction->GetFieldType(), calling_convention);
5315}
5316
5317void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
5318 HUnresolvedStaticFieldGet* instruction) {
5319 FieldAccessCallingConventionARM calling_convention;
5320 codegen_->GenerateUnresolvedFieldAccess(instruction,
5321 instruction->GetFieldType(),
5322 instruction->GetFieldIndex(),
5323 instruction->GetDexPc(),
5324 calling_convention);
5325}
5326
5327void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
5328 HUnresolvedStaticFieldSet* instruction) {
5329 FieldAccessCallingConventionARM calling_convention;
5330 codegen_->CreateUnresolvedFieldLocationSummary(
5331 instruction, instruction->GetFieldType(), calling_convention);
5332}
5333
5334void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
5335 HUnresolvedStaticFieldSet* instruction) {
5336 FieldAccessCallingConventionARM calling_convention;
5337 codegen_->GenerateUnresolvedFieldAccess(instruction,
5338 instruction->GetFieldType(),
5339 instruction->GetFieldIndex(),
5340 instruction->GetDexPc(),
5341 calling_convention);
5342}
5343
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005344void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005345 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5346 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005347}
5348
Calin Juravle2ae48182016-03-16 14:05:09 +00005349void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
5350 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005351 return;
5352 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005353 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005354
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005355 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005356 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005357}
5358
Calin Juravle2ae48182016-03-16 14:05:09 +00005359void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01005360 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005361 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005362
5363 LocationSummary* locations = instruction->GetLocations();
5364 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005365
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005366 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005367}
5368
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005369void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005370 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005371}
5372
Artem Serov6c916792016-07-11 14:02:34 +01005373static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5374 switch (type) {
5375 case Primitive::kPrimNot:
5376 return kLoadWord;
5377 case Primitive::kPrimBoolean:
5378 return kLoadUnsignedByte;
5379 case Primitive::kPrimByte:
5380 return kLoadSignedByte;
5381 case Primitive::kPrimChar:
5382 return kLoadUnsignedHalfword;
5383 case Primitive::kPrimShort:
5384 return kLoadSignedHalfword;
5385 case Primitive::kPrimInt:
5386 return kLoadWord;
5387 case Primitive::kPrimLong:
5388 return kLoadWordPair;
5389 case Primitive::kPrimFloat:
5390 return kLoadSWord;
5391 case Primitive::kPrimDouble:
5392 return kLoadDWord;
5393 default:
5394 LOG(FATAL) << "Unreachable type " << type;
5395 UNREACHABLE();
5396 }
5397}
5398
5399static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5400 switch (type) {
5401 case Primitive::kPrimNot:
5402 return kStoreWord;
5403 case Primitive::kPrimBoolean:
5404 case Primitive::kPrimByte:
5405 return kStoreByte;
5406 case Primitive::kPrimChar:
5407 case Primitive::kPrimShort:
5408 return kStoreHalfword;
5409 case Primitive::kPrimInt:
5410 return kStoreWord;
5411 case Primitive::kPrimLong:
5412 return kStoreWordPair;
5413 case Primitive::kPrimFloat:
5414 return kStoreSWord;
5415 case Primitive::kPrimDouble:
5416 return kStoreDWord;
5417 default:
5418 LOG(FATAL) << "Unreachable type " << type;
5419 UNREACHABLE();
5420 }
5421}
5422
5423void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
5424 Location out_loc,
5425 Register base,
5426 Register reg_offset,
5427 Condition cond) {
5428 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5429 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5430
5431 switch (type) {
5432 case Primitive::kPrimByte:
5433 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
5434 break;
5435 case Primitive::kPrimBoolean:
5436 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
5437 break;
5438 case Primitive::kPrimShort:
5439 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
5440 break;
5441 case Primitive::kPrimChar:
5442 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
5443 break;
5444 case Primitive::kPrimNot:
5445 case Primitive::kPrimInt:
5446 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
5447 break;
5448 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5449 case Primitive::kPrimLong:
5450 case Primitive::kPrimFloat:
5451 case Primitive::kPrimDouble:
5452 default:
5453 LOG(FATAL) << "Unreachable type " << type;
5454 UNREACHABLE();
5455 }
5456}
5457
5458void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
5459 Location loc,
5460 Register base,
5461 Register reg_offset,
5462 Condition cond) {
5463 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5464 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5465
5466 switch (type) {
5467 case Primitive::kPrimByte:
5468 case Primitive::kPrimBoolean:
5469 __ strb(loc.AsRegister<Register>(), mem_address, cond);
5470 break;
5471 case Primitive::kPrimShort:
5472 case Primitive::kPrimChar:
5473 __ strh(loc.AsRegister<Register>(), mem_address, cond);
5474 break;
5475 case Primitive::kPrimNot:
5476 case Primitive::kPrimInt:
5477 __ str(loc.AsRegister<Register>(), mem_address, cond);
5478 break;
5479 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5480 case Primitive::kPrimLong:
5481 case Primitive::kPrimFloat:
5482 case Primitive::kPrimDouble:
5483 default:
5484 LOG(FATAL) << "Unreachable type " << type;
5485 UNREACHABLE();
5486 }
5487}
5488
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005489void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005490 bool object_array_get_with_read_barrier =
5491 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005492 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005493 new (GetGraph()->GetArena()) LocationSummary(instruction,
5494 object_array_get_with_read_barrier ?
5495 LocationSummary::kCallOnSlowPath :
5496 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005497 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005498 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005499 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005500 locations->SetInAt(0, Location::RequiresRegister());
5501 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005502 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5503 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5504 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005505 // The output overlaps in the case of an object array get with
5506 // read barriers enabled: we do not want the move to overwrite the
5507 // array's location, as we need it to emit the read barrier.
5508 locations->SetOut(
5509 Location::RequiresRegister(),
5510 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005511 }
Roland Levillainc9285912015-12-18 10:38:42 +00005512 // We need a temporary register for the read barrier marking slow
5513 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07005514 // Also need for String compression feature.
5515 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
5516 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00005517 locations->AddTemp(Location::RequiresRegister());
5518 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005519}
5520
5521void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
5522 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005523 Location obj_loc = locations->InAt(0);
5524 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005525 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00005526 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005527 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00005528 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07005529 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5530 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01005531 HInstruction* array_instr = instruction->GetArray();
5532 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01005533
Roland Levillain4d027112015-07-01 15:41:14 +01005534 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01005535 case Primitive::kPrimBoolean:
5536 case Primitive::kPrimByte:
5537 case Primitive::kPrimShort:
5538 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00005539 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005540 Register length;
5541 if (maybe_compressed_char_at) {
5542 length = locations->GetTemp(0).AsRegister<Register>();
5543 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5544 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
5545 codegen_->MaybeRecordImplicitNullCheck(instruction);
5546 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005547 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005548 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07005549 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07005550 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005551 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5552 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5553 "Expecting 0=compressed, 1=uncompressed");
5554 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005555 __ LoadFromOffset(kLoadUnsignedByte,
5556 out_loc.AsRegister<Register>(),
5557 obj,
5558 data_offset + const_index);
5559 __ b(&done);
5560 __ Bind(&uncompressed_load);
5561 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5562 out_loc.AsRegister<Register>(),
5563 obj,
5564 data_offset + (const_index << 1));
5565 __ Bind(&done);
5566 } else {
5567 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01005568
jessicahandojo05765752016-09-09 19:01:32 -07005569 LoadOperandType load_type = GetLoadOperandType(type);
5570 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
5571 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005572 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005573 Register temp = IP;
5574
5575 if (has_intermediate_address) {
5576 // We do not need to compute the intermediate address from the array: the
5577 // input instruction has done it already. See the comment in
5578 // `TryExtractArrayAccessAddress()`.
5579 if (kIsDebugBuild) {
5580 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5581 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5582 }
5583 temp = obj;
5584 } else {
5585 __ add(temp, obj, ShifterOperand(data_offset));
5586 }
jessicahandojo05765752016-09-09 19:01:32 -07005587 if (maybe_compressed_char_at) {
5588 Label uncompressed_load, done;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005589 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5590 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5591 "Expecting 0=compressed, 1=uncompressed");
5592 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005593 __ ldrb(out_loc.AsRegister<Register>(),
5594 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
5595 __ b(&done);
5596 __ Bind(&uncompressed_load);
5597 __ ldrh(out_loc.AsRegister<Register>(),
5598 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
5599 __ Bind(&done);
5600 } else {
5601 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
5602 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005603 }
5604 break;
5605 }
5606
Roland Levillainc9285912015-12-18 10:38:42 +00005607 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00005608 // The read barrier instrumentation of object ArrayGet
5609 // instructions does not support the HIntermediateAddress
5610 // instruction.
5611 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5612
Roland Levillainc9285912015-12-18 10:38:42 +00005613 static_assert(
5614 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5615 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00005616 // /* HeapReference<Object> */ out =
5617 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5618 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5619 Location temp = locations->GetTemp(0);
5620 // Note that a potential implicit null check is handled in this
5621 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
5622 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5623 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5624 } else {
5625 Register out = out_loc.AsRegister<Register>();
5626 if (index.IsConstant()) {
5627 size_t offset =
5628 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5629 __ LoadFromOffset(kLoadWord, out, obj, offset);
5630 codegen_->MaybeRecordImplicitNullCheck(instruction);
5631 // If read barriers are enabled, emit read barriers other than
5632 // Baker's using a slow path (and also unpoison the loaded
5633 // reference, if heap poisoning is enabled).
5634 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5635 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005636 Register temp = IP;
5637
5638 if (has_intermediate_address) {
5639 // We do not need to compute the intermediate address from the array: the
5640 // input instruction has done it already. See the comment in
5641 // `TryExtractArrayAccessAddress()`.
5642 if (kIsDebugBuild) {
5643 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5644 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5645 }
5646 temp = obj;
5647 } else {
5648 __ add(temp, obj, ShifterOperand(data_offset));
5649 }
5650 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005651
Roland Levillainc9285912015-12-18 10:38:42 +00005652 codegen_->MaybeRecordImplicitNullCheck(instruction);
5653 // If read barriers are enabled, emit read barriers other than
5654 // Baker's using a slow path (and also unpoison the loaded
5655 // reference, if heap poisoning is enabled).
5656 codegen_->MaybeGenerateReadBarrierSlow(
5657 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5658 }
5659 }
5660 break;
5661 }
5662
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005663 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005664 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005665 size_t offset =
5666 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005667 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005668 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005669 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005670 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005671 }
5672 break;
5673 }
5674
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005675 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005676 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005677 if (index.IsConstant()) {
5678 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005679 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005680 } else {
5681 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005682 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005683 }
5684 break;
5685 }
5686
5687 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005688 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005689 if (index.IsConstant()) {
5690 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005691 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005692 } else {
5693 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005694 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005695 }
5696 break;
5697 }
5698
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005699 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005700 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005701 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005702 }
Roland Levillain4d027112015-07-01 15:41:14 +01005703
5704 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005705 // Potential implicit null checks, in the case of reference
5706 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005707 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005708 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005709 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005710}
5711
5712void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005713 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005714
5715 bool needs_write_barrier =
5716 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005717 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005718
Nicolas Geoffray39468442014-09-02 15:17:15 +01005719 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005720 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005721 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005722 LocationSummary::kCallOnSlowPath :
5723 LocationSummary::kNoCall);
5724
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005725 locations->SetInAt(0, Location::RequiresRegister());
5726 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5727 if (Primitive::IsFloatingPointType(value_type)) {
5728 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005729 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005730 locations->SetInAt(2, Location::RequiresRegister());
5731 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005732 if (needs_write_barrier) {
5733 // Temporary registers for the write barrier.
5734 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005735 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005736 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005737}
5738
5739void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5740 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005741 Location array_loc = locations->InAt(0);
5742 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005743 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005744 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005745 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005746 bool needs_write_barrier =
5747 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005748 uint32_t data_offset =
5749 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5750 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005751 HInstruction* array_instr = instruction->GetArray();
5752 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005753
5754 switch (value_type) {
5755 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005756 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005757 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005758 case Primitive::kPrimChar:
5759 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005760 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005761 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5762 uint32_t full_offset =
5763 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5764 StoreOperandType store_type = GetStoreOperandType(value_type);
5765 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005766 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005767 Register temp = IP;
5768
5769 if (has_intermediate_address) {
5770 // We do not need to compute the intermediate address from the array: the
5771 // input instruction has done it already. See the comment in
5772 // `TryExtractArrayAccessAddress()`.
5773 if (kIsDebugBuild) {
5774 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5775 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5776 }
5777 temp = array;
5778 } else {
5779 __ add(temp, array, ShifterOperand(data_offset));
5780 }
Artem Serov6c916792016-07-11 14:02:34 +01005781 codegen_->StoreToShiftedRegOffset(value_type,
5782 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005783 temp,
Artem Serov6c916792016-07-11 14:02:34 +01005784 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005785 }
5786 break;
5787 }
5788
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005789 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00005790 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01005791 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
5792 // See the comment in instruction_simplifier_shared.cc.
5793 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005794
5795 if (instruction->InputAt(2)->IsNullConstant()) {
5796 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005797 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005798 size_t offset =
5799 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01005800 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005801 } else {
5802 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005803 __ add(IP, array, ShifterOperand(data_offset));
5804 codegen_->StoreToShiftedRegOffset(value_type,
5805 value_loc,
5806 IP,
5807 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005808 }
Roland Levillain1407ee72016-01-08 15:56:19 +00005809 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00005810 DCHECK(!needs_write_barrier);
5811 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005812 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005813 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005814
5815 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01005816 Location temp1_loc = locations->GetTemp(0);
5817 Register temp1 = temp1_loc.AsRegister<Register>();
5818 Location temp2_loc = locations->GetTemp(1);
5819 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005820 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5821 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5822 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5823 Label done;
Artem Serovf4d6aee2016-07-11 10:41:45 +01005824 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005825
Roland Levillain3b359c72015-11-17 19:35:12 +00005826 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005827 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
5828 codegen_->AddSlowPath(slow_path);
5829 if (instruction->GetValueCanBeNull()) {
5830 Label non_zero;
5831 __ CompareAndBranchIfNonZero(value, &non_zero);
5832 if (index.IsConstant()) {
5833 size_t offset =
5834 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5835 __ StoreToOffset(kStoreWord, value, array, offset);
5836 } else {
5837 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005838 __ add(IP, array, ShifterOperand(data_offset));
5839 codegen_->StoreToShiftedRegOffset(value_type,
5840 value_loc,
5841 IP,
5842 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005843 }
5844 codegen_->MaybeRecordImplicitNullCheck(instruction);
5845 __ b(&done);
5846 __ Bind(&non_zero);
5847 }
5848
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005849 // Note that when read barriers are enabled, the type checks
5850 // are performed without read barriers. This is fine, even in
5851 // the case where a class object is in the from-space after
5852 // the flip, as a comparison involving such a type would not
5853 // produce a false positive; it may of course produce a false
5854 // negative, in which case we would take the ArraySet slow
5855 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005856
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005857 // /* HeapReference<Class> */ temp1 = array->klass_
5858 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
5859 codegen_->MaybeRecordImplicitNullCheck(instruction);
5860 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01005861
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005862 // /* HeapReference<Class> */ temp1 = temp1->component_type_
5863 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
5864 // /* HeapReference<Class> */ temp2 = value->klass_
5865 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
5866 // If heap poisoning is enabled, no need to unpoison `temp1`
5867 // nor `temp2`, as we are comparing two poisoned references.
5868 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01005869
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005870 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5871 Label do_put;
5872 __ b(&do_put, EQ);
5873 // If heap poisoning is enabled, the `temp1` reference has
5874 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00005875 __ MaybeUnpoisonHeapReference(temp1);
5876
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005877 // /* HeapReference<Class> */ temp1 = temp1->super_class_
5878 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
5879 // If heap poisoning is enabled, no need to unpoison
5880 // `temp1`, as we are comparing against null below.
5881 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
5882 __ Bind(&do_put);
5883 } else {
5884 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005885 }
5886 }
5887
Artem Serov6c916792016-07-11 14:02:34 +01005888 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005889 if (kPoisonHeapReferences) {
5890 // Note that in the case where `value` is a null reference,
5891 // we do not enter this block, as a null reference does not
5892 // need poisoning.
5893 DCHECK_EQ(value_type, Primitive::kPrimNot);
5894 __ Mov(temp1, value);
5895 __ PoisonHeapReference(temp1);
5896 source = temp1;
5897 }
5898
5899 if (index.IsConstant()) {
5900 size_t offset =
5901 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5902 __ StoreToOffset(kStoreWord, source, array, offset);
5903 } else {
5904 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01005905
5906 __ add(IP, array, ShifterOperand(data_offset));
5907 codegen_->StoreToShiftedRegOffset(value_type,
5908 Location::RegisterLocation(source),
5909 IP,
5910 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005911 }
5912
Roland Levillain3b359c72015-11-17 19:35:12 +00005913 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005914 codegen_->MaybeRecordImplicitNullCheck(instruction);
5915 }
5916
5917 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
5918
5919 if (done.IsLinked()) {
5920 __ Bind(&done);
5921 }
5922
5923 if (slow_path != nullptr) {
5924 __ Bind(slow_path->GetExitLabel());
5925 }
5926
5927 break;
5928 }
5929
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005930 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005931 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005932 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005933 size_t offset =
5934 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005935 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005936 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005937 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005938 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005939 }
5940 break;
5941 }
5942
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005943 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005944 Location value = locations->InAt(2);
5945 DCHECK(value.IsFpuRegister());
5946 if (index.IsConstant()) {
5947 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005948 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005949 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005950 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005951 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
5952 }
5953 break;
5954 }
5955
5956 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005957 Location value = locations->InAt(2);
5958 DCHECK(value.IsFpuRegisterPair());
5959 if (index.IsConstant()) {
5960 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005961 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005962 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005963 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005964 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
5965 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005966
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005967 break;
5968 }
5969
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005970 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005971 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005972 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005973 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005974
Roland Levillain80e67092016-01-08 16:04:55 +00005975 // Objects are handled in the switch.
5976 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005977 codegen_->MaybeRecordImplicitNullCheck(instruction);
5978 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005979}
5980
5981void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005982 LocationSummary* locations =
5983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005984 locations->SetInAt(0, Location::RequiresRegister());
5985 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005986}
5987
5988void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
5989 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005990 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005991 Register obj = locations->InAt(0).AsRegister<Register>();
5992 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005993 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005994 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07005995 // Mask out compression flag from String's array length.
5996 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005997 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07005998 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005999}
6000
Artem Serov328429f2016-07-06 16:23:04 +01006001void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01006002 LocationSummary* locations =
6003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6004
6005 locations->SetInAt(0, Location::RequiresRegister());
6006 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6007 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6008}
6009
6010void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6011 LocationSummary* locations = instruction->GetLocations();
6012 Location out = locations->Out();
6013 Location first = locations->InAt(0);
6014 Location second = locations->InAt(1);
6015
Artem Serov328429f2016-07-06 16:23:04 +01006016 if (second.IsRegister()) {
6017 __ add(out.AsRegister<Register>(),
6018 first.AsRegister<Register>(),
6019 ShifterOperand(second.AsRegister<Register>()));
6020 } else {
6021 __ AddConstant(out.AsRegister<Register>(),
6022 first.AsRegister<Register>(),
6023 second.GetConstant()->AsIntConstant()->GetValue());
6024 }
6025}
6026
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006027void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006028 RegisterSet caller_saves = RegisterSet::Empty();
6029 InvokeRuntimeCallingConvention calling_convention;
6030 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6031 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6032 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006033 locations->SetInAt(0, Location::RequiresRegister());
6034 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006035}
6036
6037void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
6038 LocationSummary* locations = instruction->GetLocations();
Artem Serovf4d6aee2016-07-11 10:41:45 +01006039 SlowPathCodeARM* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01006040 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006041 codegen_->AddSlowPath(slow_path);
6042
Roland Levillain271ab9c2014-11-27 15:23:57 +00006043 Register index = locations->InAt(0).AsRegister<Register>();
6044 Register length = locations->InAt(1).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006045
6046 __ cmp(index, ShifterOperand(length));
Roland Levillain4fa13f62015-07-06 18:11:54 +01006047 __ b(slow_path->GetEntryLabel(), HS);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006048}
6049
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006050void CodeGeneratorARM::MarkGCCard(Register temp,
6051 Register card,
6052 Register object,
6053 Register value,
6054 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006055 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006056 if (can_be_null) {
6057 __ CompareAndBranchIfZero(value, &is_null);
6058 }
Andreas Gampe542451c2016-07-26 09:02:02 -07006059 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006060 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
6061 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006062 if (can_be_null) {
6063 __ Bind(&is_null);
6064 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006065}
6066
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006067void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006068 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006069}
6070
6071void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006072 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6073}
6074
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006075void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006076 LocationSummary* locations =
6077 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006078 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006079}
6080
6081void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006082 HBasicBlock* block = instruction->GetBlock();
6083 if (block->GetLoopInformation() != nullptr) {
6084 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6085 // The back edge will generate the suspend check.
6086 return;
6087 }
6088 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6089 // The goto will generate the suspend check.
6090 return;
6091 }
6092 GenerateSuspendCheck(instruction, nullptr);
6093}
6094
6095void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
6096 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006097 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006098 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
6099 if (slow_path == nullptr) {
6100 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
6101 instruction->SetSlowPath(slow_path);
6102 codegen_->AddSlowPath(slow_path);
6103 if (successor != nullptr) {
6104 DCHECK(successor->IsLoopHeader());
6105 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6106 }
6107 } else {
6108 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6109 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006110
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00006111 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006112 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006113 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006114 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006115 __ Bind(slow_path->GetReturnLabel());
6116 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006117 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006118 __ b(slow_path->GetEntryLabel());
6119 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006120}
6121
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006122ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
6123 return codegen_->GetAssembler();
6124}
6125
6126void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006127 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006128 Location source = move->GetSource();
6129 Location destination = move->GetDestination();
6130
6131 if (source.IsRegister()) {
6132 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006133 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006134 } else if (destination.IsFpuRegister()) {
6135 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006136 } else {
6137 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006138 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006139 SP, destination.GetStackIndex());
6140 }
6141 } else if (source.IsStackSlot()) {
6142 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006143 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006144 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006145 } else if (destination.IsFpuRegister()) {
6146 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006147 } else {
6148 DCHECK(destination.IsStackSlot());
6149 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
6150 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6151 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006152 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006153 if (destination.IsRegister()) {
6154 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
6155 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006156 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006157 } else {
6158 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006159 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
6160 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006161 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006162 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006163 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
6164 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006165 } else if (destination.IsRegisterPair()) {
6166 DCHECK(ExpectedPairLayout(destination));
6167 __ LoadFromOffset(
6168 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
6169 } else {
6170 DCHECK(destination.IsFpuRegisterPair()) << destination;
6171 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6172 SP,
6173 source.GetStackIndex());
6174 }
6175 } else if (source.IsRegisterPair()) {
6176 if (destination.IsRegisterPair()) {
6177 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6178 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006179 } else if (destination.IsFpuRegisterPair()) {
6180 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6181 source.AsRegisterPairLow<Register>(),
6182 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006183 } else {
6184 DCHECK(destination.IsDoubleStackSlot()) << destination;
6185 DCHECK(ExpectedPairLayout(source));
6186 __ StoreToOffset(
6187 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
6188 }
6189 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006190 if (destination.IsRegisterPair()) {
6191 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6192 destination.AsRegisterPairHigh<Register>(),
6193 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6194 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006195 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6196 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6197 } else {
6198 DCHECK(destination.IsDoubleStackSlot()) << destination;
6199 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
6200 SP,
6201 destination.GetStackIndex());
6202 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006203 } else {
6204 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006205 HConstant* constant = source.GetConstant();
6206 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6207 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006208 if (destination.IsRegister()) {
6209 __ LoadImmediate(destination.AsRegister<Register>(), value);
6210 } else {
6211 DCHECK(destination.IsStackSlot());
6212 __ LoadImmediate(IP, value);
6213 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6214 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006215 } else if (constant->IsLongConstant()) {
6216 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006217 if (destination.IsRegisterPair()) {
6218 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
6219 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006220 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006221 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006222 __ LoadImmediate(IP, Low32Bits(value));
6223 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6224 __ LoadImmediate(IP, High32Bits(value));
6225 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6226 }
6227 } else if (constant->IsDoubleConstant()) {
6228 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006229 if (destination.IsFpuRegisterPair()) {
6230 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006231 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006232 DCHECK(destination.IsDoubleStackSlot()) << destination;
6233 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006234 __ LoadImmediate(IP, Low32Bits(int_value));
6235 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6236 __ LoadImmediate(IP, High32Bits(int_value));
6237 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6238 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006239 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006240 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006241 float value = constant->AsFloatConstant()->GetValue();
6242 if (destination.IsFpuRegister()) {
6243 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
6244 } else {
6245 DCHECK(destination.IsStackSlot());
6246 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
6247 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6248 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006249 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006250 }
6251}
6252
6253void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
6254 __ Mov(IP, reg);
6255 __ LoadFromOffset(kLoadWord, reg, SP, mem);
6256 __ StoreToOffset(kStoreWord, IP, SP, mem);
6257}
6258
6259void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
6260 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
6261 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6262 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
6263 SP, mem1 + stack_offset);
6264 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
6265 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
6266 SP, mem2 + stack_offset);
6267 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
6268}
6269
6270void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006271 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006272 Location source = move->GetSource();
6273 Location destination = move->GetDestination();
6274
6275 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006276 DCHECK_NE(source.AsRegister<Register>(), IP);
6277 DCHECK_NE(destination.AsRegister<Register>(), IP);
6278 __ Mov(IP, source.AsRegister<Register>());
6279 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
6280 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006281 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006282 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006283 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006284 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006285 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
6286 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006287 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006288 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006289 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006290 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006291 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006292 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006293 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006294 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006295 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6296 destination.AsRegisterPairHigh<Register>(),
6297 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006298 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006299 Register low_reg = source.IsRegisterPair()
6300 ? source.AsRegisterPairLow<Register>()
6301 : destination.AsRegisterPairLow<Register>();
6302 int mem = source.IsRegisterPair()
6303 ? destination.GetStackIndex()
6304 : source.GetStackIndex();
6305 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006306 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006307 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006308 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006309 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006310 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
6311 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006312 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006313 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006314 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006315 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
6316 DRegister reg = source.IsFpuRegisterPair()
6317 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
6318 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
6319 int mem = source.IsFpuRegisterPair()
6320 ? destination.GetStackIndex()
6321 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006322 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006323 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006324 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006325 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
6326 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
6327 : destination.AsFpuRegister<SRegister>();
6328 int mem = source.IsFpuRegister()
6329 ? destination.GetStackIndex()
6330 : source.GetStackIndex();
6331
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006332 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006333 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006334 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006335 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006336 Exchange(source.GetStackIndex(), destination.GetStackIndex());
6337 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006338 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006339 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006340 }
6341}
6342
6343void ParallelMoveResolverARM::SpillScratch(int reg) {
6344 __ Push(static_cast<Register>(reg));
6345}
6346
6347void ParallelMoveResolverARM::RestoreScratch(int reg) {
6348 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006349}
6350
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006351HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
6352 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006353 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006354 case HLoadClass::LoadKind::kInvalid:
6355 LOG(FATAL) << "UNREACHABLE";
6356 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006357 case HLoadClass::LoadKind::kReferrersClass:
6358 break;
6359 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6360 DCHECK(!GetCompilerOptions().GetCompilePic());
6361 break;
6362 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6363 DCHECK(GetCompilerOptions().GetCompilePic());
6364 break;
6365 case HLoadClass::LoadKind::kBootImageAddress:
6366 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006367 case HLoadClass::LoadKind::kBssEntry:
6368 DCHECK(!Runtime::Current()->UseJitCompilation());
6369 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006370 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006371 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006372 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006373 case HLoadClass::LoadKind::kDexCacheViaMethod:
6374 break;
6375 }
6376 return desired_class_load_kind;
6377}
6378
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006379void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006380 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6381 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006382 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006383 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006384 cls,
6385 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006386 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006387 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006388 return;
6389 }
Vladimir Marko41559982017-01-06 14:04:23 +00006390 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006391
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006392 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6393 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006394 ? LocationSummary::kCallOnSlowPath
6395 : LocationSummary::kNoCall;
6396 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006397 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006398 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006399 }
6400
Vladimir Marko41559982017-01-06 14:04:23 +00006401 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006402 locations->SetInAt(0, Location::RequiresRegister());
6403 }
6404 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006405 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6406 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6407 // Rely on the type resolution or initialization and marking to save everything we need.
6408 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6409 // to the custom calling convention) or by marking, so we request a different temp.
6410 locations->AddTemp(Location::RequiresRegister());
6411 RegisterSet caller_saves = RegisterSet::Empty();
6412 InvokeRuntimeCallingConvention calling_convention;
6413 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6414 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6415 // that the the kPrimNot result register is the same as the first argument register.
6416 locations->SetCustomSlowPathCallerSaves(caller_saves);
6417 } else {
6418 // For non-Baker read barrier we have a temp-clobbering call.
6419 }
6420 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006421}
6422
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006423// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6424// move.
6425void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006426 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6427 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6428 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006429 return;
6430 }
Vladimir Marko41559982017-01-06 14:04:23 +00006431 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006432
Vladimir Marko41559982017-01-06 14:04:23 +00006433 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006434 Location out_loc = locations->Out();
6435 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006436
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006437 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6438 ? kWithoutReadBarrier
6439 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006440 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006441 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006442 case HLoadClass::LoadKind::kReferrersClass: {
6443 DCHECK(!cls->CanCallRuntime());
6444 DCHECK(!cls->MustGenerateClinitCheck());
6445 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6446 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006447 GenerateGcRootFieldLoad(cls,
6448 out_loc,
6449 current_method,
6450 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006451 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006452 break;
6453 }
6454 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006455 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006456 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006457 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6458 cls->GetTypeIndex()));
6459 break;
6460 }
6461 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006462 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006463 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006464 CodeGeneratorARM::PcRelativePatchInfo* labels =
6465 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6466 __ BindTrackedLabel(&labels->movw_label);
6467 __ movw(out, /* placeholder */ 0u);
6468 __ BindTrackedLabel(&labels->movt_label);
6469 __ movt(out, /* placeholder */ 0u);
6470 __ BindTrackedLabel(&labels->add_pc_label);
6471 __ add(out, out, ShifterOperand(PC));
6472 break;
6473 }
6474 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006475 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006476 uint32_t address = dchecked_integral_cast<uint32_t>(
6477 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6478 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006479 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6480 break;
6481 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006482 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006483 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6484 ? locations->GetTemp(0).AsRegister<Register>()
6485 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006486 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006487 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006488 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006489 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006490 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006491 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006492 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006493 __ add(temp, temp, ShifterOperand(PC));
6494 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006495 generate_null_check = true;
6496 break;
6497 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006498 case HLoadClass::LoadKind::kJitTableAddress: {
6499 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6500 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006501 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006502 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006503 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006504 break;
6505 }
Vladimir Marko41559982017-01-06 14:04:23 +00006506 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006507 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006508 LOG(FATAL) << "UNREACHABLE";
6509 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006510 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006511
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006512 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6513 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01006514 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006515 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6516 codegen_->AddSlowPath(slow_path);
6517 if (generate_null_check) {
6518 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6519 }
6520 if (cls->MustGenerateClinitCheck()) {
6521 GenerateClassInitializationCheck(slow_path, out);
6522 } else {
6523 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006524 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006525 }
6526}
6527
6528void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
6529 LocationSummary* locations =
6530 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6531 locations->SetInAt(0, Location::RequiresRegister());
6532 if (check->HasUses()) {
6533 locations->SetOut(Location::SameAsFirstInput());
6534 }
6535}
6536
6537void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006538 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006539 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006540 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006541 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006542 GenerateClassInitializationCheck(slow_path,
6543 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006544}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006545
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006546void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01006547 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006548 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
6549 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
6550 __ b(slow_path->GetEntryLabel(), LT);
6551 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6552 // properly. Therefore, we do a memory fence.
6553 __ dmb(ISH);
6554 __ Bind(slow_path->GetExitLabel());
6555}
6556
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006557HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
6558 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006559 switch (desired_string_load_kind) {
6560 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6561 DCHECK(!GetCompilerOptions().GetCompilePic());
6562 break;
6563 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6564 DCHECK(GetCompilerOptions().GetCompilePic());
6565 break;
6566 case HLoadString::LoadKind::kBootImageAddress:
6567 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006568 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006569 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006570 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006571 case HLoadString::LoadKind::kJitTableAddress:
6572 DCHECK(Runtime::Current()->UseJitCompilation());
6573 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006574 case HLoadString::LoadKind::kDexCacheViaMethod:
6575 break;
6576 }
6577 return desired_string_load_kind;
6578}
6579
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006580void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006581 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006582 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006583 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006584 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006585 locations->SetOut(Location::RegisterLocation(R0));
6586 } else {
6587 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006588 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6589 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006590 // Rely on the pResolveString and marking to save everything we need, including temps.
6591 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6592 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006593 locations->AddTemp(Location::RequiresRegister());
6594 RegisterSet caller_saves = RegisterSet::Empty();
6595 InvokeRuntimeCallingConvention calling_convention;
6596 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6597 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6598 // that the the kPrimNot result register is the same as the first argument register.
6599 locations->SetCustomSlowPathCallerSaves(caller_saves);
6600 } else {
6601 // For non-Baker read barrier we have a temp-clobbering call.
6602 }
6603 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006604 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006605}
6606
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006607// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6608// move.
6609void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006610 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006611 Location out_loc = locations->Out();
6612 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006613 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00006614
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006615 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006616 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006617 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006618 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6619 load->GetStringIndex()));
6620 return; // No dex cache slow path.
6621 }
6622 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006623 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006624 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006625 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006626 __ BindTrackedLabel(&labels->movw_label);
6627 __ movw(out, /* placeholder */ 0u);
6628 __ BindTrackedLabel(&labels->movt_label);
6629 __ movt(out, /* placeholder */ 0u);
6630 __ BindTrackedLabel(&labels->add_pc_label);
6631 __ add(out, out, ShifterOperand(PC));
6632 return; // No dex cache slow path.
6633 }
6634 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006635 uint32_t address = dchecked_integral_cast<uint32_t>(
6636 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6637 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006638 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6639 return; // No dex cache slow path.
6640 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006641 case HLoadString::LoadKind::kBssEntry: {
6642 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006643 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6644 ? locations->GetTemp(0).AsRegister<Register>()
6645 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006646 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006647 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006648 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006649 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006650 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006651 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006652 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006653 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006654 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006655 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
6656 codegen_->AddSlowPath(slow_path);
6657 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6658 __ Bind(slow_path->GetExitLabel());
6659 return;
6660 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006661 case HLoadString::LoadKind::kJitTableAddress: {
6662 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006663 load->GetStringIndex(),
6664 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006665 // /* GcRoot<mirror::String> */ out = *out
6666 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6667 return;
6668 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006669 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006670 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006671 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006672
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006673 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
6674 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6675 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006676 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006677 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006678 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6679 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006680}
6681
David Brazdilcb1c0552015-08-04 16:22:25 +01006682static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006683 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006684}
6685
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006686void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6687 LocationSummary* locations =
6688 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6689 locations->SetOut(Location::RequiresRegister());
6690}
6691
6692void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006693 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006694 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6695}
6696
6697void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6698 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6699}
6700
6701void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006702 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006703 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006704}
6705
6706void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6707 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006708 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006709 InvokeRuntimeCallingConvention calling_convention;
6710 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6711}
6712
6713void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006714 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006715 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006716}
6717
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006718// Temp is used for read barrier.
6719static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6720 if (kEmitCompilerReadBarrier &&
6721 (kUseBakerReadBarrier ||
6722 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6723 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6724 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6725 return 1;
6726 }
6727 return 0;
6728}
6729
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006730// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006731// interface pointer, one for loading the current interface.
6732// The other checks have one temp for loading the object's class.
6733static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6734 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6735 return 3;
6736 }
6737 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006738}
6739
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006740void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006741 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006742 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006743 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006744 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006745 case TypeCheckKind::kExactCheck:
6746 case TypeCheckKind::kAbstractClassCheck:
6747 case TypeCheckKind::kClassHierarchyCheck:
6748 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006749 call_kind =
6750 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006751 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006752 break;
6753 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006754 case TypeCheckKind::kUnresolvedCheck:
6755 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006756 call_kind = LocationSummary::kCallOnSlowPath;
6757 break;
6758 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006759
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006760 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006761 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006762 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006763 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006764 locations->SetInAt(0, Location::RequiresRegister());
6765 locations->SetInAt(1, Location::RequiresRegister());
6766 // The "out" register is used as a temporary, so it overlaps with the inputs.
6767 // Note that TypeCheckSlowPathARM uses this register too.
6768 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006769 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006770}
6771
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006772void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006773 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006774 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006775 Location obj_loc = locations->InAt(0);
6776 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00006777 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006778 Location out_loc = locations->Out();
6779 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006780 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6781 DCHECK_LE(num_temps, 1u);
6782 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006783 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006784 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6785 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6786 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006787 Label done, zero;
Artem Serovf4d6aee2016-07-11 10:41:45 +01006788 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006789
6790 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006791 // avoid null check if we know obj is not null.
6792 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00006793 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006794 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006795
Roland Levillainc9285912015-12-18 10:38:42 +00006796 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006797 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006798 // /* HeapReference<Class> */ out = obj->klass_
6799 GenerateReferenceLoadTwoRegisters(instruction,
6800 out_loc,
6801 obj_loc,
6802 class_offset,
6803 maybe_temp_loc,
6804 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006805 __ cmp(out, ShifterOperand(cls));
6806 // Classes must be equal for the instanceof to succeed.
6807 __ b(&zero, NE);
6808 __ LoadImmediate(out, 1);
6809 __ b(&done);
6810 break;
6811 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006812
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006813 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006814 // /* HeapReference<Class> */ out = obj->klass_
6815 GenerateReferenceLoadTwoRegisters(instruction,
6816 out_loc,
6817 obj_loc,
6818 class_offset,
6819 maybe_temp_loc,
6820 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006821 // If the class is abstract, we eagerly fetch the super class of the
6822 // object to avoid doing a comparison we know will fail.
6823 Label loop;
6824 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00006825 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006826 GenerateReferenceLoadOneRegister(instruction,
6827 out_loc,
6828 super_offset,
6829 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006830 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006831 // If `out` is null, we use it for the result, and jump to `done`.
6832 __ CompareAndBranchIfZero(out, &done);
6833 __ cmp(out, ShifterOperand(cls));
6834 __ b(&loop, NE);
6835 __ LoadImmediate(out, 1);
6836 if (zero.IsLinked()) {
6837 __ b(&done);
6838 }
6839 break;
6840 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006841
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006842 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006843 // /* HeapReference<Class> */ out = obj->klass_
6844 GenerateReferenceLoadTwoRegisters(instruction,
6845 out_loc,
6846 obj_loc,
6847 class_offset,
6848 maybe_temp_loc,
6849 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006850 // Walk over the class hierarchy to find a match.
6851 Label loop, success;
6852 __ Bind(&loop);
6853 __ cmp(out, ShifterOperand(cls));
6854 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006855 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006856 GenerateReferenceLoadOneRegister(instruction,
6857 out_loc,
6858 super_offset,
6859 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006860 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006861 __ CompareAndBranchIfNonZero(out, &loop);
6862 // If `out` is null, we use it for the result, and jump to `done`.
6863 __ b(&done);
6864 __ Bind(&success);
6865 __ LoadImmediate(out, 1);
6866 if (zero.IsLinked()) {
6867 __ b(&done);
6868 }
6869 break;
6870 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006871
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006872 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006873 // /* HeapReference<Class> */ out = obj->klass_
6874 GenerateReferenceLoadTwoRegisters(instruction,
6875 out_loc,
6876 obj_loc,
6877 class_offset,
6878 maybe_temp_loc,
6879 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006880 // Do an exact check.
6881 Label exact_check;
6882 __ cmp(out, ShifterOperand(cls));
6883 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00006884 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00006885 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006886 GenerateReferenceLoadOneRegister(instruction,
6887 out_loc,
6888 component_offset,
6889 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006890 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006891 // If `out` is null, we use it for the result, and jump to `done`.
6892 __ CompareAndBranchIfZero(out, &done);
6893 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6894 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6895 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006896 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006897 __ LoadImmediate(out, 1);
6898 __ b(&done);
6899 break;
6900 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006901
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006902 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006903 // No read barrier since the slow path will retry upon failure.
6904 // /* HeapReference<Class> */ out = obj->klass_
6905 GenerateReferenceLoadTwoRegisters(instruction,
6906 out_loc,
6907 obj_loc,
6908 class_offset,
6909 maybe_temp_loc,
6910 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006911 __ cmp(out, ShifterOperand(cls));
6912 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00006913 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6914 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006915 codegen_->AddSlowPath(slow_path);
6916 __ b(slow_path->GetEntryLabel(), NE);
6917 __ LoadImmediate(out, 1);
6918 if (zero.IsLinked()) {
6919 __ b(&done);
6920 }
6921 break;
6922 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006923
Calin Juravle98893e12015-10-02 21:05:03 +01006924 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006925 case TypeCheckKind::kInterfaceCheck: {
6926 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006927 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00006928 // cases.
6929 //
6930 // We cannot directly call the InstanceofNonTrivial runtime
6931 // entry point without resorting to a type checking slow path
6932 // here (i.e. by calling InvokeRuntime directly), as it would
6933 // require to assign fixed registers for the inputs of this
6934 // HInstanceOf instruction (following the runtime calling
6935 // convention), which might be cluttered by the potential first
6936 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00006937 //
6938 // TODO: Introduce a new runtime entry point taking the object
6939 // to test (instead of its class) as argument, and let it deal
6940 // with the read barrier issues. This will let us refactor this
6941 // case of the `switch` code as it was previously (with a direct
6942 // call to the runtime not using a type checking slow path).
6943 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00006944 DCHECK(locations->OnlyCallsOnSlowPath());
6945 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
6946 /* is_fatal */ false);
6947 codegen_->AddSlowPath(slow_path);
6948 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006949 if (zero.IsLinked()) {
6950 __ b(&done);
6951 }
6952 break;
6953 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006954 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006955
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006956 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006957 __ Bind(&zero);
6958 __ LoadImmediate(out, 0);
6959 }
6960
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006961 if (done.IsLinked()) {
6962 __ Bind(&done);
6963 }
6964
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006965 if (slow_path != nullptr) {
6966 __ Bind(slow_path->GetExitLabel());
6967 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006968}
6969
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006970void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006971 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6972 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
6973
Roland Levillain3b359c72015-11-17 19:35:12 +00006974 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6975 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006976 case TypeCheckKind::kExactCheck:
6977 case TypeCheckKind::kAbstractClassCheck:
6978 case TypeCheckKind::kClassHierarchyCheck:
6979 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006980 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6981 LocationSummary::kCallOnSlowPath :
6982 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006983 break;
6984 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00006985 case TypeCheckKind::kUnresolvedCheck:
6986 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006987 call_kind = LocationSummary::kCallOnSlowPath;
6988 break;
6989 }
6990
Roland Levillain3b359c72015-11-17 19:35:12 +00006991 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6992 locations->SetInAt(0, Location::RequiresRegister());
6993 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006994 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006995}
6996
6997void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00006998 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006999 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007000 Location obj_loc = locations->InAt(0);
7001 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007002 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007003 Location temp_loc = locations->GetTemp(0);
7004 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007005 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7006 DCHECK_LE(num_temps, 3u);
7007 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7008 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7009 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7010 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7011 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7012 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7013 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7014 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7015 const uint32_t object_array_data_offset =
7016 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007017
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007018 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7019 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7020 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007021 bool is_type_check_slow_path_fatal = false;
7022 if (!kEmitCompilerReadBarrier) {
7023 is_type_check_slow_path_fatal =
7024 (type_check_kind == TypeCheckKind::kExactCheck ||
7025 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7026 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7027 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7028 !instruction->CanThrowIntoCatchBlock();
7029 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01007030 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007031 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7032 is_type_check_slow_path_fatal);
7033 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007034
7035 Label done;
7036 // Avoid null check if we know obj is not null.
7037 if (instruction->MustDoNullCheck()) {
7038 __ CompareAndBranchIfZero(obj, &done);
7039 }
7040
Roland Levillain3b359c72015-11-17 19:35:12 +00007041 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007042 case TypeCheckKind::kExactCheck:
7043 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007044 // /* HeapReference<Class> */ temp = obj->klass_
7045 GenerateReferenceLoadTwoRegisters(instruction,
7046 temp_loc,
7047 obj_loc,
7048 class_offset,
7049 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007050 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007051
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007052 __ cmp(temp, ShifterOperand(cls));
7053 // Jump to slow path for throwing the exception or doing a
7054 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00007055 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007056 break;
7057 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007058
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007059 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007060 // /* HeapReference<Class> */ temp = obj->klass_
7061 GenerateReferenceLoadTwoRegisters(instruction,
7062 temp_loc,
7063 obj_loc,
7064 class_offset,
7065 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007066 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007067
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007068 // If the class is abstract, we eagerly fetch the super class of the
7069 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007070 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007071 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007072 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007073 GenerateReferenceLoadOneRegister(instruction,
7074 temp_loc,
7075 super_offset,
7076 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007077 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007078
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007079 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7080 // exception.
7081 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00007082
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007083 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007084 __ cmp(temp, ShifterOperand(cls));
7085 __ b(&loop, NE);
7086 break;
7087 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007088
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007089 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007090 // /* HeapReference<Class> */ temp = obj->klass_
7091 GenerateReferenceLoadTwoRegisters(instruction,
7092 temp_loc,
7093 obj_loc,
7094 class_offset,
7095 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007096 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007097
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007098 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007099 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007100 __ Bind(&loop);
7101 __ cmp(temp, ShifterOperand(cls));
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007102 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007103
Roland Levillain3b359c72015-11-17 19:35:12 +00007104 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007105 GenerateReferenceLoadOneRegister(instruction,
7106 temp_loc,
7107 super_offset,
7108 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007109 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007110
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007111 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7112 // exception.
7113 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7114 // Otherwise, jump to the beginning of the loop.
7115 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007116 break;
7117 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007118
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007119 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007120 // /* HeapReference<Class> */ temp = obj->klass_
7121 GenerateReferenceLoadTwoRegisters(instruction,
7122 temp_loc,
7123 obj_loc,
7124 class_offset,
7125 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007126 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007127
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007128 // Do an exact check.
7129 __ cmp(temp, ShifterOperand(cls));
7130 __ b(&done, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007131
7132 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007133 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007134 GenerateReferenceLoadOneRegister(instruction,
7135 temp_loc,
7136 component_offset,
7137 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007138 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007139 // If the component type is null, jump to the slow path to throw the exception.
7140 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7141 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7142 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007143 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00007144 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007145 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007146 break;
7147 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007148
Calin Juravle98893e12015-10-02 21:05:03 +01007149 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007150 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00007151 // We cannot directly call the CheckCast runtime entry point
7152 // without resorting to a type checking slow path here (i.e. by
7153 // calling InvokeRuntime directly), as it would require to
7154 // assign fixed registers for the inputs of this HInstanceOf
7155 // instruction (following the runtime calling convention), which
7156 // might be cluttered by the potential first read barrier
7157 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007158
Roland Levillain3b359c72015-11-17 19:35:12 +00007159 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007160 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007161
7162 case TypeCheckKind::kInterfaceCheck: {
7163 // Avoid read barriers to improve performance of the fast path. We can not get false
7164 // positives by doing this.
7165 // /* HeapReference<Class> */ temp = obj->klass_
7166 GenerateReferenceLoadTwoRegisters(instruction,
7167 temp_loc,
7168 obj_loc,
7169 class_offset,
7170 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007171 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007172
7173 // /* HeapReference<Class> */ temp = temp->iftable_
7174 GenerateReferenceLoadTwoRegisters(instruction,
7175 temp_loc,
7176 temp_loc,
7177 iftable_offset,
7178 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007179 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007180 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007181 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08007182 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007183 Label start_loop;
7184 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007185 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
7186 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007187 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
7188 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007189 // Go to next interface.
7190 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
7191 __ sub(maybe_temp2_loc.AsRegister<Register>(),
7192 maybe_temp2_loc.AsRegister<Register>(),
7193 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007194 // Compare the classes and continue the loop if they do not match.
7195 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
7196 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007197 break;
7198 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007199 }
7200 __ Bind(&done);
7201
Roland Levillain3b359c72015-11-17 19:35:12 +00007202 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007203}
7204
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007205void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
7206 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007207 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007208 InvokeRuntimeCallingConvention calling_convention;
7209 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7210}
7211
7212void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007213 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7214 instruction,
7215 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007216 if (instruction->IsEnter()) {
7217 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7218 } else {
7219 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7220 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007221}
7222
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007223void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
7224void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
7225void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007226
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007227void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007228 LocationSummary* locations =
7229 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7230 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7231 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007232 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007233 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007234 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00007235 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007236}
7237
7238void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
7239 HandleBitwiseOperation(instruction);
7240}
7241
7242void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
7243 HandleBitwiseOperation(instruction);
7244}
7245
7246void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
7247 HandleBitwiseOperation(instruction);
7248}
7249
Artem Serov7fc63502016-02-09 17:15:29 +00007250
7251void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7252 LocationSummary* locations =
7253 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7254 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7255 || instruction->GetResultType() == Primitive::kPrimLong);
7256
7257 locations->SetInAt(0, Location::RequiresRegister());
7258 locations->SetInAt(1, Location::RequiresRegister());
7259 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7260}
7261
7262void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7263 LocationSummary* locations = instruction->GetLocations();
7264 Location first = locations->InAt(0);
7265 Location second = locations->InAt(1);
7266 Location out = locations->Out();
7267
7268 if (instruction->GetResultType() == Primitive::kPrimInt) {
7269 Register first_reg = first.AsRegister<Register>();
7270 ShifterOperand second_reg(second.AsRegister<Register>());
7271 Register out_reg = out.AsRegister<Register>();
7272
7273 switch (instruction->GetOpKind()) {
7274 case HInstruction::kAnd:
7275 __ bic(out_reg, first_reg, second_reg);
7276 break;
7277 case HInstruction::kOr:
7278 __ orn(out_reg, first_reg, second_reg);
7279 break;
7280 // There is no EON on arm.
7281 case HInstruction::kXor:
7282 default:
7283 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7284 UNREACHABLE();
7285 }
7286 return;
7287
7288 } else {
7289 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7290 Register first_low = first.AsRegisterPairLow<Register>();
7291 Register first_high = first.AsRegisterPairHigh<Register>();
7292 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7293 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7294 Register out_low = out.AsRegisterPairLow<Register>();
7295 Register out_high = out.AsRegisterPairHigh<Register>();
7296
7297 switch (instruction->GetOpKind()) {
7298 case HInstruction::kAnd:
7299 __ bic(out_low, first_low, second_low);
7300 __ bic(out_high, first_high, second_high);
7301 break;
7302 case HInstruction::kOr:
7303 __ orn(out_low, first_low, second_low);
7304 __ orn(out_high, first_high, second_high);
7305 break;
7306 // There is no EON on arm.
7307 case HInstruction::kXor:
7308 default:
7309 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7310 UNREACHABLE();
7311 }
7312 }
7313}
7314
Anton Kirilov74234da2017-01-13 14:42:47 +00007315void LocationsBuilderARM::VisitDataProcWithShifterOp(
7316 HDataProcWithShifterOp* instruction) {
7317 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
7318 instruction->GetType() == Primitive::kPrimLong);
7319 LocationSummary* locations =
7320 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7321 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
7322 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7323
7324 locations->SetInAt(0, Location::RequiresRegister());
7325 locations->SetInAt(1, Location::RequiresRegister());
7326 locations->SetOut(Location::RequiresRegister(),
7327 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7328}
7329
7330void InstructionCodeGeneratorARM::VisitDataProcWithShifterOp(
7331 HDataProcWithShifterOp* instruction) {
7332 const LocationSummary* const locations = instruction->GetLocations();
7333 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
7334 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
7335 const Location left = locations->InAt(0);
7336 const Location right = locations->InAt(1);
7337 const Location out = locations->Out();
7338
7339 if (instruction->GetType() == Primitive::kPrimInt) {
7340 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
7341
7342 const Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
7343 ? right.AsRegisterPairLow<Register>()
7344 : right.AsRegister<Register>();
7345
7346 GenerateDataProcInstruction(kind,
7347 out.AsRegister<Register>(),
7348 left.AsRegister<Register>(),
7349 ShifterOperand(second,
7350 ShiftFromOpKind(op_kind),
7351 instruction->GetShiftAmount()),
7352 codegen_);
7353 } else {
7354 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7355
7356 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7357 const Register second = right.AsRegister<Register>();
7358
7359 DCHECK_NE(out.AsRegisterPairLow<Register>(), second);
7360 GenerateDataProc(kind,
7361 out,
7362 left,
7363 ShifterOperand(second),
7364 ShifterOperand(second, ASR, 31),
7365 codegen_);
7366 } else {
7367 GenerateLongDataProc(instruction, codegen_);
7368 }
7369 }
7370}
7371
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007372void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
7373 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7374 if (value == 0xffffffffu) {
7375 if (out != first) {
7376 __ mov(out, ShifterOperand(first));
7377 }
7378 return;
7379 }
7380 if (value == 0u) {
7381 __ mov(out, ShifterOperand(0));
7382 return;
7383 }
7384 ShifterOperand so;
7385 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
7386 __ and_(out, first, so);
7387 } else {
7388 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so));
7389 __ bic(out, first, ShifterOperand(~value));
7390 }
7391}
7392
7393void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
7394 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7395 if (value == 0u) {
7396 if (out != first) {
7397 __ mov(out, ShifterOperand(first));
7398 }
7399 return;
7400 }
7401 if (value == 0xffffffffu) {
7402 __ mvn(out, ShifterOperand(0));
7403 return;
7404 }
7405 ShifterOperand so;
7406 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
7407 __ orr(out, first, so);
7408 } else {
7409 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
7410 __ orn(out, first, ShifterOperand(~value));
7411 }
7412}
7413
7414void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
7415 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7416 if (value == 0u) {
7417 if (out != first) {
7418 __ mov(out, ShifterOperand(first));
7419 }
7420 return;
7421 }
7422 __ eor(out, first, ShifterOperand(value));
7423}
7424
Vladimir Marko59751a72016-08-05 14:37:27 +01007425void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
7426 Location first,
7427 uint64_t value) {
7428 Register out_low = out.AsRegisterPairLow<Register>();
7429 Register out_high = out.AsRegisterPairHigh<Register>();
7430 Register first_low = first.AsRegisterPairLow<Register>();
7431 Register first_high = first.AsRegisterPairHigh<Register>();
7432 uint32_t value_low = Low32Bits(value);
7433 uint32_t value_high = High32Bits(value);
7434 if (value_low == 0u) {
7435 if (out_low != first_low) {
7436 __ mov(out_low, ShifterOperand(first_low));
7437 }
7438 __ AddConstant(out_high, first_high, value_high);
7439 return;
7440 }
7441 __ AddConstantSetFlags(out_low, first_low, value_low);
7442 ShifterOperand so;
7443 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
7444 __ adc(out_high, first_high, so);
7445 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
7446 __ sbc(out_high, first_high, so);
7447 } else {
7448 LOG(FATAL) << "Unexpected constant " << value_high;
7449 UNREACHABLE();
7450 }
7451}
7452
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007453void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
7454 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007455 Location first = locations->InAt(0);
7456 Location second = locations->InAt(1);
7457 Location out = locations->Out();
7458
7459 if (second.IsConstant()) {
7460 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7461 uint32_t value_low = Low32Bits(value);
7462 if (instruction->GetResultType() == Primitive::kPrimInt) {
7463 Register first_reg = first.AsRegister<Register>();
7464 Register out_reg = out.AsRegister<Register>();
7465 if (instruction->IsAnd()) {
7466 GenerateAndConst(out_reg, first_reg, value_low);
7467 } else if (instruction->IsOr()) {
7468 GenerateOrrConst(out_reg, first_reg, value_low);
7469 } else {
7470 DCHECK(instruction->IsXor());
7471 GenerateEorConst(out_reg, first_reg, value_low);
7472 }
7473 } else {
7474 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7475 uint32_t value_high = High32Bits(value);
7476 Register first_low = first.AsRegisterPairLow<Register>();
7477 Register first_high = first.AsRegisterPairHigh<Register>();
7478 Register out_low = out.AsRegisterPairLow<Register>();
7479 Register out_high = out.AsRegisterPairHigh<Register>();
7480 if (instruction->IsAnd()) {
7481 GenerateAndConst(out_low, first_low, value_low);
7482 GenerateAndConst(out_high, first_high, value_high);
7483 } else if (instruction->IsOr()) {
7484 GenerateOrrConst(out_low, first_low, value_low);
7485 GenerateOrrConst(out_high, first_high, value_high);
7486 } else {
7487 DCHECK(instruction->IsXor());
7488 GenerateEorConst(out_low, first_low, value_low);
7489 GenerateEorConst(out_high, first_high, value_high);
7490 }
7491 }
7492 return;
7493 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007494
7495 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007496 Register first_reg = first.AsRegister<Register>();
7497 ShifterOperand second_reg(second.AsRegister<Register>());
7498 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007499 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007500 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007501 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007502 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007503 } else {
7504 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007505 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007506 }
7507 } else {
7508 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007509 Register first_low = first.AsRegisterPairLow<Register>();
7510 Register first_high = first.AsRegisterPairHigh<Register>();
7511 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7512 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7513 Register out_low = out.AsRegisterPairLow<Register>();
7514 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007515 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007516 __ and_(out_low, first_low, second_low);
7517 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007518 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007519 __ orr(out_low, first_low, second_low);
7520 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007521 } else {
7522 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007523 __ eor(out_low, first_low, second_low);
7524 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007525 }
7526 }
7527}
7528
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007529void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
7530 HInstruction* instruction,
7531 Location out,
7532 uint32_t offset,
7533 Location maybe_temp,
7534 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007535 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007536 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007537 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007538 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007539 if (kUseBakerReadBarrier) {
7540 // Load with fast path based Baker's read barrier.
7541 // /* HeapReference<Object> */ out = *(out + offset)
7542 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007543 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007544 } else {
7545 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007546 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00007547 // in the following move operation, as we will need it for the
7548 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007549 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00007550 // /* HeapReference<Object> */ out = *(out + offset)
7551 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007552 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00007553 }
7554 } else {
7555 // Plain load with no read barrier.
7556 // /* HeapReference<Object> */ out = *(out + offset)
7557 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7558 __ MaybeUnpoisonHeapReference(out_reg);
7559 }
7560}
7561
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007562void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
7563 HInstruction* instruction,
7564 Location out,
7565 Location obj,
7566 uint32_t offset,
7567 Location maybe_temp,
7568 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007569 Register out_reg = out.AsRegister<Register>();
7570 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007571 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007572 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007573 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007574 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007575 // Load with fast path based Baker's read barrier.
7576 // /* HeapReference<Object> */ out = *(obj + offset)
7577 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007578 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007579 } else {
7580 // Load with slow path based read barrier.
7581 // /* HeapReference<Object> */ out = *(obj + offset)
7582 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7583 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7584 }
7585 } else {
7586 // Plain load with no read barrier.
7587 // /* HeapReference<Object> */ out = *(obj + offset)
7588 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7589 __ MaybeUnpoisonHeapReference(out_reg);
7590 }
7591}
7592
7593void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
7594 Location root,
7595 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007596 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007597 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007598 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007599 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007600 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007601 if (kUseBakerReadBarrier) {
7602 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillain35345a52017-02-27 14:32:08 +00007603 // Baker's read barrier are used:
Roland Levillainc9285912015-12-18 10:38:42 +00007604 //
Roland Levillain35345a52017-02-27 14:32:08 +00007605 // root = obj.field;
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007606 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillain35345a52017-02-27 14:32:08 +00007607 // if (temp != null) {
7608 // root = temp(root)
Roland Levillainc9285912015-12-18 10:38:42 +00007609 // }
7610
7611 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7612 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7613 static_assert(
7614 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7615 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7616 "have different sizes.");
7617 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7618 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7619 "have different sizes.");
7620
Roland Levillain35345a52017-02-27 14:32:08 +00007621 // Slow path marking the GC root `root`.
7622 Location temp = Location::RegisterLocation(LR);
7623 SlowPathCodeARM* slow_path =
7624 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
7625 instruction,
7626 root,
7627 /*entrypoint*/ temp);
7628 codegen_->AddSlowPath(slow_path);
7629
7630 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7631 const int32_t entry_point_offset =
7632 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
7633 // Loading the entrypoint does not require a load acquire since it is only changed when
7634 // threads are suspended or running a checkpoint.
7635 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007636 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7637 // checking GetIsGcMarking.
7638 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00007639 __ Bind(slow_path->GetExitLabel());
7640 } else {
7641 // GC root loaded through a slow path for read barriers other
7642 // than Baker's.
7643 // /* GcRoot<mirror::Object>* */ root = obj + offset
7644 __ AddConstant(root_reg, obj, offset);
7645 // /* mirror::Object* */ root = root->Read()
7646 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7647 }
7648 } else {
7649 // Plain GC root load with no read barrier.
7650 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7651 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7652 // Note that GC roots are not affected by heap poisoning, thus we
7653 // do not have to unpoison `root_reg` here.
7654 }
7655}
7656
7657void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7658 Location ref,
7659 Register obj,
7660 uint32_t offset,
7661 Location temp,
7662 bool needs_null_check) {
7663 DCHECK(kEmitCompilerReadBarrier);
7664 DCHECK(kUseBakerReadBarrier);
7665
7666 // /* HeapReference<Object> */ ref = *(obj + offset)
7667 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01007668 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00007669 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007670 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007671}
7672
7673void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7674 Location ref,
7675 Register obj,
7676 uint32_t data_offset,
7677 Location index,
7678 Location temp,
7679 bool needs_null_check) {
7680 DCHECK(kEmitCompilerReadBarrier);
7681 DCHECK(kUseBakerReadBarrier);
7682
Roland Levillainbfea3352016-06-23 13:48:47 +01007683 static_assert(
7684 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7685 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007686 // /* HeapReference<Object> */ ref =
7687 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01007688 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00007689 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007690 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007691}
7692
7693void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7694 Location ref,
7695 Register obj,
7696 uint32_t offset,
7697 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01007698 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00007699 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007700 bool needs_null_check,
7701 bool always_update_field,
7702 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00007703 DCHECK(kEmitCompilerReadBarrier);
7704 DCHECK(kUseBakerReadBarrier);
7705
Roland Levillain35345a52017-02-27 14:32:08 +00007706 // In slow path based read barriers, the read barrier call is
7707 // inserted after the original load. However, in fast path based
7708 // Baker's read barriers, we need to perform the load of
7709 // mirror::Object::monitor_ *before* the original reference load.
7710 // This load-load ordering is required by the read barrier.
7711 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillainc9285912015-12-18 10:38:42 +00007712 //
Roland Levillain35345a52017-02-27 14:32:08 +00007713 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7714 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7715 // HeapReference<Object> ref = *src; // Original reference load.
7716 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7717 // if (is_gray) {
7718 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007719 // }
Roland Levillain35345a52017-02-27 14:32:08 +00007720 //
7721 // Note: the original implementation in ReadBarrier::Barrier is
7722 // slightly more complex as it performs additional checks that we do
7723 // not do here for performance reasons.
Roland Levillainc9285912015-12-18 10:38:42 +00007724
Roland Levillain1372c9f2017-01-13 11:47:39 +00007725 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain35345a52017-02-27 14:32:08 +00007726 Register temp_reg = temp.AsRegister<Register>();
7727 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
Roland Levillain1372c9f2017-01-13 11:47:39 +00007728
Roland Levillain35345a52017-02-27 14:32:08 +00007729 // /* int32_t */ monitor = obj->monitor_
7730 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7731 if (needs_null_check) {
7732 MaybeRecordImplicitNullCheck(instruction);
7733 }
7734 // /* LockWord */ lock_word = LockWord(monitor)
7735 static_assert(sizeof(LockWord) == sizeof(int32_t),
7736 "art::LockWord and int32_t have different sizes.");
7737
7738 // Introduce a dependency on the lock_word including the rb_state,
7739 // which shall prevent load-load reordering without using
7740 // a memory barrier (which would be more expensive).
7741 // `obj` is unchanged by this operation, but its value now depends
7742 // on `temp_reg`.
7743 __ add(obj, obj, ShifterOperand(temp_reg, LSR, 32));
7744
7745 // The actual reference load.
Roland Levillainc9285912015-12-18 10:38:42 +00007746 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007747 // Load types involving an "index": ArrayGet,
7748 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7749 // intrinsics.
Roland Levillain35345a52017-02-27 14:32:08 +00007750 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00007751 if (index.IsConstant()) {
7752 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01007753 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00007754 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7755 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01007756 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007757 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7758 // intrinsics, which use a register pair as index ("long
7759 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01007760 Register index_reg = index.IsRegisterPair()
7761 ? index.AsRegisterPairLow<Register>()
7762 : index.AsRegister<Register>();
7763 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00007764 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
7765 }
7766 } else {
Roland Levillain35345a52017-02-27 14:32:08 +00007767 // /* HeapReference<Object> */ ref = *(obj + offset)
Roland Levillainc9285912015-12-18 10:38:42 +00007768 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7769 }
7770
7771 // Object* ref = ref_addr->AsMirrorPtr()
7772 __ MaybeUnpoisonHeapReference(ref_reg);
Roland Levillain35345a52017-02-27 14:32:08 +00007773
7774 // Slow path marking the object `ref` when it is gray.
7775 SlowPathCodeARM* slow_path;
7776 if (always_update_field) {
7777 DCHECK(temp2 != nullptr);
7778 // ReadBarrierMarkAndUpdateFieldSlowPathARM only supports address
7779 // of the form `obj + field_offset`, where `obj` is a register and
7780 // `field_offset` is a register pair (of which only the lower half
7781 // is used). Thus `offset` and `scale_factor` above are expected
7782 // to be null in this code path.
7783 DCHECK_EQ(offset, 0u);
7784 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
7785 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathARM(
7786 instruction, ref, obj, /* field_offset */ index, temp_reg, *temp2);
7787 } else {
7788 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(instruction, ref);
7789 }
7790 AddSlowPath(slow_path);
7791
7792 // if (rb_state == ReadBarrier::GrayState())
7793 // ref = ReadBarrier::Mark(ref);
7794 // Given the numeric representation, it's enough to check the low bit of the
7795 // rb_state. We do that by shifting the bit out of the lock word with LSRS
7796 // which can be a 16-bit instruction unlike the TST immediate.
7797 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7798 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7799 __ Lsrs(temp_reg, temp_reg, LockWord::kReadBarrierStateShift + 1);
7800 __ b(slow_path->GetEntryLabel(), CS); // Carry flag is the last bit shifted out by LSRS.
7801 __ Bind(slow_path->GetExitLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00007802}
7803
7804void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
7805 Location out,
7806 Location ref,
7807 Location obj,
7808 uint32_t offset,
7809 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007810 DCHECK(kEmitCompilerReadBarrier);
7811
Roland Levillainc9285912015-12-18 10:38:42 +00007812 // Insert a slow path based read barrier *after* the reference load.
7813 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007814 // If heap poisoning is enabled, the unpoisoning of the loaded
7815 // reference will be carried out by the runtime within the slow
7816 // path.
7817 //
7818 // Note that `ref` currently does not get unpoisoned (when heap
7819 // poisoning is enabled), which is alright as the `ref` argument is
7820 // not used by the artReadBarrierSlow entry point.
7821 //
7822 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007823 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00007824 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
7825 AddSlowPath(slow_path);
7826
Roland Levillain3b359c72015-11-17 19:35:12 +00007827 __ b(slow_path->GetEntryLabel());
7828 __ Bind(slow_path->GetExitLabel());
7829}
7830
Roland Levillainc9285912015-12-18 10:38:42 +00007831void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7832 Location out,
7833 Location ref,
7834 Location obj,
7835 uint32_t offset,
7836 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007837 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00007838 // Baker's read barriers shall be handled by the fast path
7839 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
7840 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007841 // If heap poisoning is enabled, unpoisoning will be taken care of
7842 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00007843 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00007844 } else if (kPoisonHeapReferences) {
7845 __ UnpoisonHeapReference(out.AsRegister<Register>());
7846 }
7847}
7848
Roland Levillainc9285912015-12-18 10:38:42 +00007849void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7850 Location out,
7851 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00007852 DCHECK(kEmitCompilerReadBarrier);
7853
Roland Levillainc9285912015-12-18 10:38:42 +00007854 // Insert a slow path based read barrier *after* the GC root load.
7855 //
Roland Levillain3b359c72015-11-17 19:35:12 +00007856 // Note that GC roots are not affected by heap poisoning, so we do
7857 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007858 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007859 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
7860 AddSlowPath(slow_path);
7861
Roland Levillain3b359c72015-11-17 19:35:12 +00007862 __ b(slow_path->GetEntryLabel());
7863 __ Bind(slow_path->GetExitLabel());
7864}
7865
Vladimir Markodc151b22015-10-15 18:02:30 +01007866HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
7867 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00007868 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00007869 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007870}
7871
Vladimir Markob4536b72015-11-24 13:45:23 +00007872Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7873 Register temp) {
7874 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7875 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7876 if (!invoke->GetLocations()->Intrinsified()) {
7877 return location.AsRegister<Register>();
7878 }
7879 // For intrinsics we allow any location, so it may be on the stack.
7880 if (!location.IsRegister()) {
7881 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7882 return temp;
7883 }
7884 // For register locations, check if the register was saved. If so, get it from the stack.
7885 // Note: There is a chance that the register was saved but not overwritten, so we could
7886 // save one load. However, since this is just an intrinsic slow path we prefer this
7887 // simple and more robust approach rather that trying to determine if that's the case.
7888 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08007889 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00007890 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7891 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7892 return temp;
7893 }
7894 return location.AsRegister<Register>();
7895}
7896
TatWai Chongd8c052a2016-11-02 16:12:48 +08007897Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
7898 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00007899 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
7900 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007901 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
7902 uint32_t offset =
7903 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00007904 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007905 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00007906 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007907 }
Vladimir Marko58155012015-08-19 12:49:41 +00007908 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007909 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007910 break;
7911 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7912 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7913 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00007914 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
7915 HArmDexCacheArraysBase* base =
7916 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
7917 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
7918 temp.AsRegister<Register>());
7919 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
7920 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
7921 break;
7922 }
Vladimir Marko58155012015-08-19 12:49:41 +00007923 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00007924 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00007925 Register method_reg;
7926 Register reg = temp.AsRegister<Register>();
7927 if (current_method.IsRegister()) {
7928 method_reg = current_method.AsRegister<Register>();
7929 } else {
7930 DCHECK(invoke->GetLocations()->Intrinsified());
7931 DCHECK(!current_method.IsValid());
7932 method_reg = reg;
7933 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
7934 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007935 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
7936 __ LoadFromOffset(kLoadWord,
7937 reg,
7938 method_reg,
7939 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01007940 // temp = temp[index_in_cache];
7941 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
7942 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00007943 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
7944 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01007945 }
Vladimir Marko58155012015-08-19 12:49:41 +00007946 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08007947 return callee_method;
7948}
7949
7950void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
7951 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00007952
7953 switch (invoke->GetCodePtrLocation()) {
7954 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
7955 __ bl(GetFrameEntryLabel());
7956 break;
Vladimir Marko58155012015-08-19 12:49:41 +00007957 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7958 // LR = callee_method->entry_point_from_quick_compiled_code_
7959 __ LoadFromOffset(
7960 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07007961 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00007962 // LR()
7963 __ blx(LR);
7964 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007965 }
7966
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08007967 DCHECK(!IsLeafMethod());
7968}
7969
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007970void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
7971 Register temp = temp_location.AsRegister<Register>();
7972 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7973 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007974
7975 // Use the calling convention instead of the location of the receiver, as
7976 // intrinsics may have put the receiver in a different register. In the intrinsics
7977 // slow path, the arguments have been moved to the right place, so here we are
7978 // guaranteed that the receiver is the first register of the calling convention.
7979 InvokeDexCallingConvention calling_convention;
7980 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007981 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00007982 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00007983 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007984 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00007985 // Instead of simply (possibly) unpoisoning `temp` here, we should
7986 // emit a read barrier for the previous class reference load.
7987 // However this is not required in practice, as this is an
7988 // intermediate/temporary reference and because the current
7989 // concurrent copying collector keeps the from-space memory
7990 // intact/accessible until the end of the marking phase (the
7991 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007992 __ MaybeUnpoisonHeapReference(temp);
7993 // temp = temp->GetMethodAt(method_offset);
7994 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007995 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00007996 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7997 // LR = temp->GetEntryPoint();
7998 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
7999 // LR();
8000 __ blx(LR);
8001}
8002
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008003CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008004 const DexFile& dex_file, dex::StringIndex string_index) {
8005 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008006}
8007
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008008CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08008009 const DexFile& dex_file, dex::TypeIndex type_index) {
8010 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008011}
8012
Vladimir Marko1998cd02017-01-13 13:02:58 +00008013CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
8014 const DexFile& dex_file, dex::TypeIndex type_index) {
8015 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8016}
8017
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008018CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
8019 const DexFile& dex_file, uint32_t element_offset) {
8020 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
8021}
8022
8023CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
8024 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8025 patches->emplace_back(dex_file, offset_or_index);
8026 return &patches->back();
8027}
8028
8029Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008030 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008031 return boot_image_string_patches_.GetOrCreate(
8032 StringReference(&dex_file, string_index),
8033 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8034}
8035
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008036Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08008037 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008038 return boot_image_type_patches_.GetOrCreate(
8039 TypeReference(&dex_file, type_index),
8040 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8041}
8042
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008043Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
8044 bool needs_patch = GetCompilerOptions().GetIncludePatchInformation();
8045 Uint32ToLiteralMap* map = needs_patch ? &boot_image_address_patches_ : &uint32_literals_;
8046 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), map);
8047}
8048
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008049Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008050 dex::StringIndex string_index,
8051 Handle<mirror::String> handle) {
8052 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
8053 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008054 return jit_string_patches_.GetOrCreate(
8055 StringReference(&dex_file, string_index),
8056 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8057}
8058
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008059Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
8060 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008061 Handle<mirror::Class> handle) {
8062 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
8063 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008064 return jit_class_patches_.GetOrCreate(
8065 TypeReference(&dex_file, type_index),
8066 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8067}
8068
Vladimir Markoaad75c62016-10-03 08:46:48 +00008069template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
8070inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
8071 const ArenaDeque<PcRelativePatchInfo>& infos,
8072 ArenaVector<LinkerPatch>* linker_patches) {
8073 for (const PcRelativePatchInfo& info : infos) {
8074 const DexFile& dex_file = info.target_dex_file;
8075 size_t offset_or_index = info.offset_or_index;
8076 DCHECK(info.add_pc_label.IsBound());
8077 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
8078 // Add MOVW patch.
8079 DCHECK(info.movw_label.IsBound());
8080 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
8081 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
8082 // Add MOVT patch.
8083 DCHECK(info.movt_label.IsBound());
8084 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
8085 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
8086 }
8087}
8088
Vladimir Marko58155012015-08-19 12:49:41 +00008089void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
8090 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00008091 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00008092 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008093 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008094 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008095 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008096 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Marko1998cd02017-01-13 13:02:58 +00008097 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008098 boot_image_address_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00008099 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008100 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
8101 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008102 for (const auto& entry : boot_image_string_patches_) {
8103 const StringReference& target_string = entry.first;
8104 Literal* literal = entry.second;
8105 DCHECK(literal->GetLabel()->IsBound());
8106 uint32_t literal_offset = literal->GetLabel()->Position();
8107 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
8108 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008109 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008110 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00008111 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00008112 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00008113 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
8114 linker_patches);
8115 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008116 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
8117 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008118 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
8119 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008120 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008121 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
8122 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008123 for (const auto& entry : boot_image_type_patches_) {
8124 const TypeReference& target_type = entry.first;
8125 Literal* literal = entry.second;
8126 DCHECK(literal->GetLabel()->IsBound());
8127 uint32_t literal_offset = literal->GetLabel()->Position();
8128 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
8129 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08008130 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008131 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008132 for (const auto& entry : boot_image_address_patches_) {
8133 DCHECK(GetCompilerOptions().GetIncludePatchInformation());
8134 Literal* literal = entry.second;
8135 DCHECK(literal->GetLabel()->IsBound());
8136 uint32_t literal_offset = literal->GetLabel()->Position();
8137 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
8138 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008139 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008140}
8141
8142Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
8143 return map->GetOrCreate(
8144 value,
8145 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00008146}
8147
8148Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
8149 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008150 return map->GetOrCreate(
8151 target_method,
8152 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00008153}
8154
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03008155void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8156 LocationSummary* locations =
8157 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
8158 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
8159 Location::RequiresRegister());
8160 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
8161 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
8162 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8163}
8164
8165void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8166 LocationSummary* locations = instr->GetLocations();
8167 Register res = locations->Out().AsRegister<Register>();
8168 Register accumulator =
8169 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
8170 Register mul_left =
8171 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
8172 Register mul_right =
8173 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
8174
8175 if (instr->GetOpKind() == HInstruction::kAdd) {
8176 __ mla(res, mul_left, mul_right, accumulator);
8177 } else {
8178 __ mls(res, mul_left, mul_right, accumulator);
8179 }
8180}
8181
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008182void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008183 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008184 LOG(FATAL) << "Unreachable";
8185}
8186
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008187void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008188 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008189 LOG(FATAL) << "Unreachable";
8190}
8191
Mark Mendellfe57faa2015-09-18 09:26:15 -04008192// Simple implementation of packed switch - generate cascaded compare/jumps.
8193void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8194 LocationSummary* locations =
8195 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8196 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008197 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008198 codegen_->GetAssembler()->IsThumb()) {
8199 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
8200 if (switch_instr->GetStartValue() != 0) {
8201 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
8202 }
8203 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008204}
8205
8206void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8207 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008208 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04008209 LocationSummary* locations = switch_instr->GetLocations();
8210 Register value_reg = locations->InAt(0).AsRegister<Register>();
8211 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8212
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008213 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008214 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008215 Register temp_reg = IP;
8216 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
8217 // the immediate, because IP is used as the destination register. For the other
8218 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
8219 // and they can be encoded in the instruction without making use of IP register.
8220 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
8221
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008222 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008223 // Jump to successors[0] if value == lower_bound.
8224 __ b(codegen_->GetLabelOf(successors[0]), EQ);
8225 int32_t last_index = 0;
8226 for (; num_entries - last_index > 2; last_index += 2) {
8227 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
8228 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8229 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
8230 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8231 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
8232 }
8233 if (num_entries - last_index == 2) {
8234 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00008235 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008236 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008237 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008238
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008239 // And the default for any other value.
8240 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
8241 __ b(codegen_->GetLabelOf(default_block));
8242 }
8243 } else {
8244 // Create a table lookup.
8245 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8246
8247 // Materialize a pointer to the switch table
8248 std::vector<Label*> labels(num_entries);
8249 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
8250 for (uint32_t i = 0; i < num_entries; i++) {
8251 labels[i] = codegen_->GetLabelOf(successors[i]);
8252 }
8253 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
8254
8255 // Remove the bias.
8256 Register key_reg;
8257 if (lower_bound != 0) {
8258 key_reg = locations->GetTemp(1).AsRegister<Register>();
8259 __ AddConstant(key_reg, value_reg, -lower_bound);
8260 } else {
8261 key_reg = value_reg;
8262 }
8263
8264 // Check whether the value is in the table, jump to default block if not.
8265 __ CmpConstant(key_reg, num_entries - 1);
8266 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
8267
8268 // Load the displacement from the table.
8269 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
8270
8271 // Dispatch is a direct add to the PC (for Thumb2).
8272 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008273 }
8274}
8275
Vladimir Markob4536b72015-11-24 13:45:23 +00008276void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8277 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8278 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00008279}
8280
8281void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8282 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008283 CodeGeneratorARM::PcRelativePatchInfo* labels =
8284 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00008285 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008286 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008287 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008288 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008289 __ BindTrackedLabel(&labels->add_pc_label);
8290 __ add(base_reg, base_reg, ShifterOperand(PC));
8291}
8292
Andreas Gampe85b62f22015-09-09 13:15:38 -07008293void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
8294 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00008295 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008296 return;
8297 }
8298
8299 DCHECK_NE(type, Primitive::kPrimVoid);
8300
8301 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
8302 if (return_loc.Equals(trg)) {
8303 return;
8304 }
8305
8306 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8307 // with the last branch.
8308 if (type == Primitive::kPrimLong) {
8309 HParallelMove parallel_move(GetGraph()->GetArena());
8310 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
8311 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
8312 GetMoveResolver()->EmitNativeCode(&parallel_move);
8313 } else if (type == Primitive::kPrimDouble) {
8314 HParallelMove parallel_move(GetGraph()->GetArena());
8315 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
8316 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
8317 GetMoveResolver()->EmitNativeCode(&parallel_move);
8318 } else {
8319 // Let the parallel move resolver take care of all of this.
8320 HParallelMove parallel_move(GetGraph()->GetArena());
8321 parallel_move.AddMove(return_loc, trg, type, nullptr);
8322 GetMoveResolver()->EmitNativeCode(&parallel_move);
8323 }
8324}
8325
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008326void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
8327 LocationSummary* locations =
8328 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8329 locations->SetInAt(0, Location::RequiresRegister());
8330 locations->SetOut(Location::RequiresRegister());
8331}
8332
8333void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
8334 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008335 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008336 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008337 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008338 __ LoadFromOffset(kLoadWord,
8339 locations->Out().AsRegister<Register>(),
8340 locations->InAt(0).AsRegister<Register>(),
8341 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008342 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008343 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008344 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008345 __ LoadFromOffset(kLoadWord,
8346 locations->Out().AsRegister<Register>(),
8347 locations->InAt(0).AsRegister<Register>(),
8348 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
8349 __ LoadFromOffset(kLoadWord,
8350 locations->Out().AsRegister<Register>(),
8351 locations->Out().AsRegister<Register>(),
8352 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008353 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008354}
8355
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008356static void PatchJitRootUse(uint8_t* code,
8357 const uint8_t* roots_data,
8358 Literal* literal,
8359 uint64_t index_in_table) {
8360 DCHECK(literal->GetLabel()->IsBound());
8361 uint32_t literal_offset = literal->GetLabel()->Position();
8362 uintptr_t address =
8363 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8364 uint8_t* data = code + literal_offset;
8365 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
8366}
8367
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008368void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8369 for (const auto& entry : jit_string_patches_) {
8370 const auto& it = jit_string_roots_.find(entry.first);
8371 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008372 PatchJitRootUse(code, roots_data, entry.second, it->second);
8373 }
8374 for (const auto& entry : jit_class_patches_) {
8375 const auto& it = jit_class_roots_.find(entry.first);
8376 DCHECK(it != jit_class_roots_.end());
8377 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008378 }
8379}
8380
Roland Levillain4d027112015-07-01 15:41:14 +01008381#undef __
8382#undef QUICK_ENTRY_POINT
8383
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008384} // namespace arm
8385} // namespace art