blob: caea250ab622c286657fdf34c0c0a331edab4b74 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Calin Juravle34166012014-12-19 17:22:29 +000019#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070020#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080021#include "code_generator_utils.h"
Anton Kirilov74234da2017-01-13 14:42:47 +000022#include "common_arm.h"
Vladimir Marko58155012015-08-19 12:49:41 +000023#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070024#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010025#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080026#include "intrinsics.h"
27#include "intrinsics_arm.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070028#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070030#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010031#include "utils/arm/assembler_arm.h"
32#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000033#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010034#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037
Roland Levillain3b359c72015-11-17 19:35:12 +000038template<class MirrorType>
39class GcRoot;
40
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041namespace arm {
42
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000043static bool ExpectedPairLayout(Location location) {
44 // We expected this for both core and fpu register pairs.
45 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
46}
47
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010048static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010049static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050
David Brazdil58282f42016-01-14 12:45:10 +000051static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000052static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070053 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000054static constexpr SRegister kFpuCalleeSaves[] =
55 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010056
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000057// D31 cannot be split into two S registers, and the register allocator only works on
58// S registers. Therefore there is no need to block it.
59static constexpr DRegister DTMP = D31;
60
Vladimir Markof3e0ee22015-12-17 15:23:13 +000061static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070062
Roland Levillain7cbd27f2016-08-11 23:53:33 +010063// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
64#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070065#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010066
Artem Serovf4d6aee2016-07-11 10:41:45 +010067static constexpr int kRegListThreshold = 4;
68
Artem Serovd300d8f2016-07-15 14:00:56 +010069// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
70// for each live D registers they treat two corresponding S registers as live ones.
71//
72// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
73// from a list of contiguous S registers a list of contiguous D registers (processing first/last
74// S registers corner cases) and save/restore this new list treating them as D registers.
75// - decreasing code size
76// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
77// restored and then used in regular non SlowPath code as D register.
78//
79// For the following example (v means the S register is live):
80// D names: | D0 | D1 | D2 | D4 | ...
81// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
82// Live? | | v | v | v | v | v | v | | ...
83//
84// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
85// as D registers.
86static size_t SaveContiguousSRegisterList(size_t first,
87 size_t last,
88 CodeGenerator* codegen,
89 size_t stack_offset) {
90 DCHECK_LE(first, last);
91 if ((first == last) && (first == 0)) {
92 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first);
93 return stack_offset;
94 }
95 if (first % 2 == 1) {
96 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first++);
97 }
98
99 bool save_last = false;
100 if (last % 2 == 0) {
101 save_last = true;
102 --last;
103 }
104
105 if (first < last) {
106 DRegister d_reg = static_cast<DRegister>(first / 2);
107 DCHECK_EQ((last - first + 1) % 2, 0u);
108 size_t number_of_d_regs = (last - first + 1) / 2;
109
110 if (number_of_d_regs == 1) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100111 __ StoreDToOffset(d_reg, SP, stack_offset);
Artem Serovd300d8f2016-07-15 14:00:56 +0100112 } else if (number_of_d_regs > 1) {
113 __ add(IP, SP, ShifterOperand(stack_offset));
114 __ vstmiad(IP, d_reg, number_of_d_regs);
115 }
116 stack_offset += number_of_d_regs * kArmWordSize * 2;
117 }
118
119 if (save_last) {
120 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, last + 1);
121 }
122
123 return stack_offset;
124}
125
126static size_t RestoreContiguousSRegisterList(size_t first,
127 size_t last,
128 CodeGenerator* codegen,
129 size_t stack_offset) {
130 DCHECK_LE(first, last);
131 if ((first == last) && (first == 0)) {
132 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first);
133 return stack_offset;
134 }
135 if (first % 2 == 1) {
136 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first++);
137 }
138
139 bool restore_last = false;
140 if (last % 2 == 0) {
141 restore_last = true;
142 --last;
143 }
144
145 if (first < last) {
146 DRegister d_reg = static_cast<DRegister>(first / 2);
147 DCHECK_EQ((last - first + 1) % 2, 0u);
148 size_t number_of_d_regs = (last - first + 1) / 2;
149 if (number_of_d_regs == 1) {
150 __ LoadDFromOffset(d_reg, SP, stack_offset);
151 } else if (number_of_d_regs > 1) {
152 __ add(IP, SP, ShifterOperand(stack_offset));
153 __ vldmiad(IP, d_reg, number_of_d_regs);
154 }
155 stack_offset += number_of_d_regs * kArmWordSize * 2;
156 }
157
158 if (restore_last) {
159 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, last + 1);
160 }
161
162 return stack_offset;
163}
164
Artem Serovf4d6aee2016-07-11 10:41:45 +0100165void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
166 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
167 size_t orig_offset = stack_offset;
168
169 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
170 for (uint32_t i : LowToHighBits(core_spills)) {
171 // If the register holds an object, update the stack mask.
172 if (locations->RegisterContainsObject(i)) {
173 locations->SetStackBit(stack_offset / kVRegSize);
174 }
175 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
176 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
177 saved_core_stack_offsets_[i] = stack_offset;
178 stack_offset += kArmWordSize;
179 }
180
181 int reg_num = POPCOUNT(core_spills);
182 if (reg_num != 0) {
183 if (reg_num > kRegListThreshold) {
184 __ StoreList(RegList(core_spills), orig_offset);
185 } else {
186 stack_offset = orig_offset;
187 for (uint32_t i : LowToHighBits(core_spills)) {
188 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
189 }
190 }
191 }
192
Artem Serovd300d8f2016-07-15 14:00:56 +0100193 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
194 orig_offset = stack_offset;
Vladimir Marko804b03f2016-09-14 16:26:36 +0100195 for (uint32_t i : LowToHighBits(fp_spills)) {
Artem Serovf4d6aee2016-07-11 10:41:45 +0100196 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
197 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serovd300d8f2016-07-15 14:00:56 +0100198 stack_offset += kArmWordSize;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100199 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100200
201 stack_offset = orig_offset;
202 while (fp_spills != 0u) {
203 uint32_t begin = CTZ(fp_spills);
204 uint32_t tmp = fp_spills + (1u << begin);
205 fp_spills &= tmp; // Clear the contiguous range of 1s.
206 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
207 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
208 }
209 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100210}
211
212void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
213 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
214 size_t orig_offset = stack_offset;
215
216 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
217 for (uint32_t i : LowToHighBits(core_spills)) {
218 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
219 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
220 stack_offset += kArmWordSize;
221 }
222
223 int reg_num = POPCOUNT(core_spills);
224 if (reg_num != 0) {
225 if (reg_num > kRegListThreshold) {
226 __ LoadList(RegList(core_spills), orig_offset);
227 } else {
228 stack_offset = orig_offset;
229 for (uint32_t i : LowToHighBits(core_spills)) {
230 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
231 }
232 }
233 }
234
Artem Serovd300d8f2016-07-15 14:00:56 +0100235 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
236 while (fp_spills != 0u) {
237 uint32_t begin = CTZ(fp_spills);
238 uint32_t tmp = fp_spills + (1u << begin);
239 fp_spills &= tmp; // Clear the contiguous range of 1s.
240 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
241 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
Artem Serovf4d6aee2016-07-11 10:41:45 +0100242 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100243 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100244}
245
246class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100247 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100248 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100249
Alexandre Rames67555f72014-11-18 10:55:16 +0000250 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100251 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100252 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000253 if (instruction_->CanThrowIntoCatchBlock()) {
254 // Live registers will be restored in the catch block if caught.
255 SaveLiveRegisters(codegen, instruction_->GetLocations());
256 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100257 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
258 instruction_,
259 instruction_->GetDexPc(),
260 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000261 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100262 }
263
Alexandre Rames8158f282015-08-07 10:26:17 +0100264 bool IsFatal() const OVERRIDE { return true; }
265
Alexandre Rames9931f312015-06-19 14:47:01 +0100266 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
267
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100268 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100269 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
270};
271
Artem Serovf4d6aee2016-07-11 10:41:45 +0100272class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000273 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100274 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000275
Alexandre Rames67555f72014-11-18 10:55:16 +0000276 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000277 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
278 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100279 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000280 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000281 }
282
Alexandre Rames8158f282015-08-07 10:26:17 +0100283 bool IsFatal() const OVERRIDE { return true; }
284
Alexandre Rames9931f312015-06-19 14:47:01 +0100285 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
286
Calin Juravled0d48522014-11-04 16:40:20 +0000287 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000288 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
289};
290
Artem Serovf4d6aee2016-07-11 10:41:45 +0100291class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000292 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000293 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100294 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000295
Alexandre Rames67555f72014-11-18 10:55:16 +0000296 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100297 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000298 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100299 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000300 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100301 if (successor_ == nullptr) {
302 __ b(GetReturnLabel());
303 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100304 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100305 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000306 }
307
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100308 Label* GetReturnLabel() {
309 DCHECK(successor_ == nullptr);
310 return &return_label_;
311 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000312
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100313 HBasicBlock* GetSuccessor() const {
314 return successor_;
315 }
316
Alexandre Rames9931f312015-06-19 14:47:01 +0100317 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
318
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000319 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100320 // If not null, the block to branch to after the suspend check.
321 HBasicBlock* const successor_;
322
323 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000324 Label return_label_;
325
326 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
327};
328
Artem Serovf4d6aee2016-07-11 10:41:45 +0100329class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100330 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100331 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100332 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100333
Alexandre Rames67555f72014-11-18 10:55:16 +0000334 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100335 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100336 LocationSummary* locations = instruction_->GetLocations();
337
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100338 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000339 if (instruction_->CanThrowIntoCatchBlock()) {
340 // Live registers will be restored in the catch block if caught.
341 SaveLiveRegisters(codegen, instruction_->GetLocations());
342 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000343 // We're moving two locations to locations that could overlap, so we need a parallel
344 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100345 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000346 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100347 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000348 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100349 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100350 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100351 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
352 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100353 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
354 ? kQuickThrowStringBounds
355 : kQuickThrowArrayBounds;
356 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100357 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000358 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100359 }
360
Alexandre Rames8158f282015-08-07 10:26:17 +0100361 bool IsFatal() const OVERRIDE { return true; }
362
Alexandre Rames9931f312015-06-19 14:47:01 +0100363 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
364
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100365 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100366 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
367};
368
Artem Serovf4d6aee2016-07-11 10:41:45 +0100369class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100370 public:
Vladimir Markoea4c1262017-02-06 19:59:33 +0000371 LoadClassSlowPathARM(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000372 : SlowPathCodeARM(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000373 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
374 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100375
Alexandre Rames67555f72014-11-18 10:55:16 +0000376 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000377 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000378 Location out = locations->Out();
379 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000380
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100381 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
382 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000383 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100384
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100385 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000386 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
387 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
388 bool is_load_class_bss_entry =
389 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
390 Register entry_address = kNoRegister;
391 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
392 Register temp = locations->GetTemp(0).AsRegister<Register>();
393 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
394 // the kSaveEverything call.
395 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
396 entry_address = temp_is_r0 ? out.AsRegister<Register>() : temp;
397 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
398 if (temp_is_r0) {
399 __ mov(entry_address, ShifterOperand(temp));
400 }
401 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000402 dex::TypeIndex type_index = cls_->GetTypeIndex();
403 __ LoadImmediate(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100404 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
405 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000406 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000407 if (do_clinit_) {
408 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
409 } else {
410 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
411 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000412
Vladimir Markoea4c1262017-02-06 19:59:33 +0000413 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
414 if (is_load_class_bss_entry) {
415 if (call_saves_everything_except_r0) {
416 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
417 __ str(R0, Address(entry_address));
418 } else {
419 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
420 Register temp = IP;
421 CodeGeneratorARM::PcRelativePatchInfo* labels =
422 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
423 __ BindTrackedLabel(&labels->movw_label);
424 __ movw(temp, /* placeholder */ 0u);
425 __ BindTrackedLabel(&labels->movt_label);
426 __ movt(temp, /* placeholder */ 0u);
427 __ BindTrackedLabel(&labels->add_pc_label);
428 __ add(temp, temp, ShifterOperand(PC));
429 __ str(R0, Address(temp));
430 }
431 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000432 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000433 if (out.IsValid()) {
434 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000435 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
436 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000437 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100438 __ b(GetExitLabel());
439 }
440
Alexandre Rames9931f312015-06-19 14:47:01 +0100441 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
442
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100443 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000444 // The class this slow path will load.
445 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100446
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000447 // The dex PC of `at_`.
448 const uint32_t dex_pc_;
449
450 // Whether to initialize the class.
451 const bool do_clinit_;
452
453 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100454};
455
Vladimir Markoaad75c62016-10-03 08:46:48 +0000456class LoadStringSlowPathARM : public SlowPathCodeARM {
457 public:
458 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
459
460 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000461 DCHECK(instruction_->IsLoadString());
462 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000463 LocationSummary* locations = instruction_->GetLocations();
464 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100465 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000466 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100467 Register out = locations->Out().AsRegister<Register>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100468 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000469
470 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
471 __ Bind(GetEntryLabel());
472 SaveLiveRegisters(codegen, locations);
473
474 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100475 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000476 // the kSaveEverything call.
477 Register entry_address = kNoRegister;
478 if (call_saves_everything_except_r0) {
479 Register temp = locations->GetTemp(0).AsRegister<Register>();
480 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
481 entry_address = temp_is_r0 ? out : temp;
482 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
483 if (temp_is_r0) {
484 __ mov(entry_address, ShifterOperand(temp));
485 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100486 }
487
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000488 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000489 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
490 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100491
492 // Store the resolved String to the .bss entry.
493 if (call_saves_everything_except_r0) {
494 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
495 __ str(R0, Address(entry_address));
496 } else {
497 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000498 Register temp = IP;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100499 CodeGeneratorARM::PcRelativePatchInfo* labels =
500 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
501 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000502 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100503 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000504 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100505 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000506 __ add(temp, temp, ShifterOperand(PC));
507 __ str(R0, Address(temp));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100508 }
509
Vladimir Markoaad75c62016-10-03 08:46:48 +0000510 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000511 RestoreLiveRegisters(codegen, locations);
512
Vladimir Markoaad75c62016-10-03 08:46:48 +0000513 __ b(GetExitLabel());
514 }
515
516 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
517
518 private:
519 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
520};
521
Artem Serovf4d6aee2016-07-11 10:41:45 +0100522class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000523 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000524 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100525 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000526
Alexandre Rames67555f72014-11-18 10:55:16 +0000527 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000528 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000529 DCHECK(instruction_->IsCheckCast()
530 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000531
532 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
533 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000534
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000535 if (!is_fatal_) {
536 SaveLiveRegisters(codegen, locations);
537 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000538
539 // We're moving two locations to locations that could overlap, so we need a parallel
540 // move resolver.
541 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800542 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800543 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
544 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800545 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800546 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
547 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000548 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100549 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100550 instruction_,
551 instruction_->GetDexPc(),
552 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800553 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000554 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
555 } else {
556 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800557 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
558 instruction_,
559 instruction_->GetDexPc(),
560 this);
561 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000562 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000563
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000564 if (!is_fatal_) {
565 RestoreLiveRegisters(codegen, locations);
566 __ b(GetExitLabel());
567 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000568 }
569
Alexandre Rames9931f312015-06-19 14:47:01 +0100570 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
571
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000572 bool IsFatal() const OVERRIDE { return is_fatal_; }
573
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000574 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000575 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000576
577 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
578};
579
Artem Serovf4d6aee2016-07-11 10:41:45 +0100580class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700581 public:
Aart Bik42249c32016-01-07 15:33:50 -0800582 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100583 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700584
585 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800586 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700587 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100588 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000589 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700590 }
591
Alexandre Rames9931f312015-06-19 14:47:01 +0100592 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
593
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700594 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700595 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
596};
597
Artem Serovf4d6aee2016-07-11 10:41:45 +0100598class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100599 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100600 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100601
602 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
603 LocationSummary* locations = instruction_->GetLocations();
604 __ Bind(GetEntryLabel());
605 SaveLiveRegisters(codegen, locations);
606
607 InvokeRuntimeCallingConvention calling_convention;
608 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
609 parallel_move.AddMove(
610 locations->InAt(0),
611 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
612 Primitive::kPrimNot,
613 nullptr);
614 parallel_move.AddMove(
615 locations->InAt(1),
616 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
617 Primitive::kPrimInt,
618 nullptr);
619 parallel_move.AddMove(
620 locations->InAt(2),
621 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
622 Primitive::kPrimNot,
623 nullptr);
624 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
625
626 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100627 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000628 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100629 RestoreLiveRegisters(codegen, locations);
630 __ b(GetExitLabel());
631 }
632
633 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
634
635 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100636 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
637};
638
Roland Levillain54f869e2017-03-06 13:54:11 +0000639// Abstract base class for read barrier slow paths marking a reference
640// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000641//
Roland Levillain54f869e2017-03-06 13:54:11 +0000642// Argument `entrypoint` must be a register location holding the read
643// barrier marking runtime entry point to be invoked.
644class ReadBarrierMarkSlowPathBaseARM : public SlowPathCodeARM {
645 protected:
646 ReadBarrierMarkSlowPathBaseARM(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000647 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
648 DCHECK(kEmitCompilerReadBarrier);
649 }
650
Roland Levillain54f869e2017-03-06 13:54:11 +0000651 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000652
Roland Levillain54f869e2017-03-06 13:54:11 +0000653 // Generate assembly code calling the read barrier marking runtime
654 // entry point (ReadBarrierMarkRegX).
655 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000656 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain47b3ab22017-02-27 14:31:35 +0000657
Roland Levillain47b3ab22017-02-27 14:31:35 +0000658 // No need to save live registers; it's taken care of by the
659 // entrypoint. Also, there is no need to update the stack mask,
660 // as this runtime call will not trigger a garbage collection.
661 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
662 DCHECK_NE(ref_reg, SP);
663 DCHECK_NE(ref_reg, LR);
664 DCHECK_NE(ref_reg, PC);
665 // IP is used internally by the ReadBarrierMarkRegX entry point
666 // as a temporary, it cannot be the entry point's input/output.
667 DCHECK_NE(ref_reg, IP);
668 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
669 // "Compact" slow path, saving two moves.
670 //
671 // Instead of using the standard runtime calling convention (input
672 // and output in R0):
673 //
674 // R0 <- ref
675 // R0 <- ReadBarrierMark(R0)
676 // ref <- R0
677 //
678 // we just use rX (the register containing `ref`) as input and output
679 // of a dedicated entrypoint:
680 //
681 // rX <- ReadBarrierMarkRegX(rX)
682 //
683 if (entrypoint_.IsValid()) {
684 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
685 __ blx(entrypoint_.AsRegister<Register>());
686 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000687 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000688 int32_t entry_point_offset =
689 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
690 // This runtime call does not require a stack map.
691 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
692 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000693 }
694
695 // The location (register) of the marked object reference.
696 const Location ref_;
697
698 // The location of the entrypoint if it is already loaded.
699 const Location entrypoint_;
700
701 private:
702 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM);
703};
704
Dave Allison20dfc792014-06-16 20:44:29 -0700705// Slow path marking an object reference `ref` during a read
706// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000707// reference does not get updated by this slow path after marking.
Dave Allison20dfc792014-06-16 20:44:29 -0700708//
709// This means that after the execution of this slow path, `ref` will
710// always be up-to-date, but `obj.field` may not; i.e., after the
711// flip, `ref` will be a to-space reference, but `obj.field` will
712// probably still be a from-space reference (unless it gets updated by
713// another thread, or if another thread installed another object
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000714// reference (different from `ref`) in `obj.field`).
715//
716// If `entrypoint` is a valid location it is assumed to already be
717// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000718// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000719class ReadBarrierMarkSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000720 public:
721 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
722 Location ref,
723 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000724 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000725 DCHECK(kEmitCompilerReadBarrier);
726 }
727
728 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
729
730 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
731 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000732 DCHECK(locations->CanCall());
733 if (kIsDebugBuild) {
734 Register ref_reg = ref_.AsRegister<Register>();
735 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
736 }
737 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
738 << "Unexpected instruction in read barrier marking slow path: "
739 << instruction_->DebugName();
740
741 __ Bind(GetEntryLabel());
742 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000743 __ b(GetExitLabel());
744 }
745
746 private:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000747 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
748};
749
Roland Levillain54f869e2017-03-06 13:54:11 +0000750// Slow path loading `obj`'s lock word, loading a reference from
751// object `*(obj + offset + (index << scale_factor))` into `ref`, and
752// marking `ref` if `obj` is gray according to the lock word (Baker
753// read barrier). The field `obj.field` in the object `obj` holding
754// this reference does not get updated by this slow path after marking
755// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
756// below for that).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000757//
Roland Levillain54f869e2017-03-06 13:54:11 +0000758// This means that after the execution of this slow path, `ref` will
759// always be up-to-date, but `obj.field` may not; i.e., after the
760// flip, `ref` will be a to-space reference, but `obj.field` will
761// probably still be a from-space reference (unless it gets updated by
762// another thread, or if another thread installed another object
763// reference (different from `ref`) in `obj.field`).
764//
765// Argument `entrypoint` must be a register location holding the read
766// barrier marking runtime entry point to be invoked.
767class LoadReferenceWithBakerReadBarrierSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000768 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000769 LoadReferenceWithBakerReadBarrierSlowPathARM(HInstruction* instruction,
770 Location ref,
771 Register obj,
772 uint32_t offset,
773 Location index,
774 ScaleFactor scale_factor,
775 bool needs_null_check,
776 Register temp,
777 Location entrypoint)
778 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000779 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000780 offset_(offset),
781 index_(index),
782 scale_factor_(scale_factor),
783 needs_null_check_(needs_null_check),
784 temp_(temp) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000785 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000786 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000787 }
788
Roland Levillain54f869e2017-03-06 13:54:11 +0000789 const char* GetDescription() const OVERRIDE {
790 return "LoadReferenceWithBakerReadBarrierSlowPathARM";
791 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000792
793 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
794 LocationSummary* locations = instruction_->GetLocations();
795 Register ref_reg = ref_.AsRegister<Register>();
796 DCHECK(locations->CanCall());
797 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000798 DCHECK_NE(ref_reg, temp_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000799 DCHECK(instruction_->IsInstanceFieldGet() ||
800 instruction_->IsStaticFieldGet() ||
801 instruction_->IsArrayGet() ||
802 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000803 instruction_->IsInstanceOf() ||
804 instruction_->IsCheckCast() ||
805 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
806 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
807 << "Unexpected instruction in read barrier marking slow path: "
808 << instruction_->DebugName();
809 // The read barrier instrumentation of object ArrayGet
810 // instructions does not support the HIntermediateAddress
811 // instruction.
812 DCHECK(!(instruction_->IsArrayGet() &&
813 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
814
815 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000816
817 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
818 // inserted after the original load. However, in fast path based
819 // Baker's read barriers, we need to perform the load of
820 // mirror::Object::monitor_ *before* the original reference load.
821 // This load-load ordering is required by the read barrier.
822 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000823 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000824 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
825 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
826 // HeapReference<mirror::Object> ref = *src; // Original reference load.
827 // bool is_gray = (rb_state == ReadBarrier::GrayState());
828 // if (is_gray) {
829 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
830 // }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000831 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000832 // Note: the original implementation in ReadBarrier::Barrier is
833 // slightly more complex as it performs additional checks that we do
834 // not do here for performance reasons.
835
836 // /* int32_t */ monitor = obj->monitor_
837 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
838 __ LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
839 if (needs_null_check_) {
840 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000841 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000842 // /* LockWord */ lock_word = LockWord(monitor)
843 static_assert(sizeof(LockWord) == sizeof(int32_t),
844 "art::LockWord and int32_t have different sizes.");
845
846 // Introduce a dependency on the lock_word including the rb_state,
847 // which shall prevent load-load reordering without using
848 // a memory barrier (which would be more expensive).
849 // `obj` is unchanged by this operation, but its value now depends
850 // on `temp`.
851 __ add(obj_, obj_, ShifterOperand(temp_, LSR, 32));
852
853 // The actual reference load.
854 // A possible implicit null check has already been handled above.
855 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
856 arm_codegen->GenerateRawReferenceLoad(
857 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
858
859 // Mark the object `ref` when `obj` is gray.
860 //
861 // if (rb_state == ReadBarrier::GrayState())
862 // ref = ReadBarrier::Mark(ref);
863 //
864 // Given the numeric representation, it's enough to check the low bit of the
865 // rb_state. We do that by shifting the bit out of the lock word with LSRS
866 // which can be a 16-bit instruction unlike the TST immediate.
867 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
868 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
869 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
870 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
871 GenerateReadBarrierMarkRuntimeCall(codegen);
872
Roland Levillain47b3ab22017-02-27 14:31:35 +0000873 __ b(GetExitLabel());
874 }
875
876 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000877 // The register containing the object holding the marked object reference field.
878 Register obj_;
879 // The offset, index and scale factor to access the reference in `obj_`.
880 uint32_t offset_;
881 Location index_;
882 ScaleFactor scale_factor_;
883 // Is a null check required?
884 bool needs_null_check_;
885 // A temporary register used to hold the lock word of `obj_`.
886 Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000887
Roland Levillain54f869e2017-03-06 13:54:11 +0000888 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000889};
890
Roland Levillain54f869e2017-03-06 13:54:11 +0000891// Slow path loading `obj`'s lock word, loading a reference from
892// object `*(obj + offset + (index << scale_factor))` into `ref`, and
893// marking `ref` if `obj` is gray according to the lock word (Baker
894// read barrier). If needed, this slow path also atomically updates
895// the field `obj.field` in the object `obj` holding this reference
896// after marking (contrary to
897// LoadReferenceWithBakerReadBarrierSlowPathARM above, which never
898// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000899//
900// This means that after the execution of this slow path, both `ref`
901// and `obj.field` will be up-to-date; i.e., after the flip, both will
902// hold the same to-space reference (unless another thread installed
903// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000904//
Roland Levillain54f869e2017-03-06 13:54:11 +0000905// Argument `entrypoint` must be a register location holding the read
906// barrier marking runtime entry point to be invoked.
907class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
908 : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000909 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000910 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(HInstruction* instruction,
911 Location ref,
912 Register obj,
913 uint32_t offset,
914 Location index,
915 ScaleFactor scale_factor,
916 bool needs_null_check,
917 Register temp1,
918 Register temp2,
919 Location entrypoint)
920 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000921 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000922 offset_(offset),
923 index_(index),
924 scale_factor_(scale_factor),
925 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000926 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +0000927 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000928 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000929 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000930 }
931
Roland Levillain54f869e2017-03-06 13:54:11 +0000932 const char* GetDescription() const OVERRIDE {
933 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM";
934 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000935
936 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
937 LocationSummary* locations = instruction_->GetLocations();
938 Register ref_reg = ref_.AsRegister<Register>();
939 DCHECK(locations->CanCall());
940 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000941 DCHECK_NE(ref_reg, temp1_);
942
943 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000944 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
945 << "Unexpected instruction in read barrier marking and field updating slow path: "
946 << instruction_->DebugName();
947 DCHECK(instruction_->GetLocations()->Intrinsified());
948 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000949 DCHECK_EQ(offset_, 0u);
950 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
951 // The location of the offset of the marked reference field within `obj_`.
952 Location field_offset = index_;
953 DCHECK(field_offset.IsRegisterPair()) << field_offset;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000954
955 __ Bind(GetEntryLabel());
956
Roland Levillain54f869e2017-03-06 13:54:11 +0000957 // /* int32_t */ monitor = obj->monitor_
958 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
959 __ LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
960 if (needs_null_check_) {
961 codegen->MaybeRecordImplicitNullCheck(instruction_);
962 }
963 // /* LockWord */ lock_word = LockWord(monitor)
964 static_assert(sizeof(LockWord) == sizeof(int32_t),
965 "art::LockWord and int32_t have different sizes.");
966
967 // Introduce a dependency on the lock_word including the rb_state,
968 // which shall prevent load-load reordering without using
969 // a memory barrier (which would be more expensive).
970 // `obj` is unchanged by this operation, but its value now depends
971 // on `temp1`.
972 __ add(obj_, obj_, ShifterOperand(temp1_, LSR, 32));
973
974 // The actual reference load.
975 // A possible implicit null check has already been handled above.
976 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
977 arm_codegen->GenerateRawReferenceLoad(
978 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
979
980 // Mark the object `ref` when `obj` is gray.
981 //
982 // if (rb_state == ReadBarrier::GrayState())
983 // ref = ReadBarrier::Mark(ref);
984 //
985 // Given the numeric representation, it's enough to check the low bit of the
986 // rb_state. We do that by shifting the bit out of the lock word with LSRS
987 // which can be a 16-bit instruction unlike the TST immediate.
988 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
989 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
990 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
991 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
992
993 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000994 // Note that we cannot use IP to save the old reference, as IP is
995 // used internally by the ReadBarrierMarkRegX entry point, and we
996 // need the old reference after the call to that entry point.
997 DCHECK_NE(temp1_, IP);
998 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000999
Roland Levillain54f869e2017-03-06 13:54:11 +00001000 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001001
1002 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001003 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001004 //
1005 // Note that this field could also hold a different object, if
1006 // another thread had concurrently changed it. In that case, the
1007 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1008 // (CAS) operation below would abort the CAS, leaving the field
1009 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001010 __ cmp(temp1_, ShifterOperand(ref_reg));
Roland Levillain54f869e2017-03-06 13:54:11 +00001011 __ b(GetExitLabel(), EQ);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001012
1013 // Update the the holder's field atomically. This may fail if
1014 // mutator updates before us, but it's OK. This is achieved
1015 // using a strong compare-and-set (CAS) operation with relaxed
1016 // memory synchronization ordering, where the expected value is
1017 // the old reference and the desired value is the new reference.
1018
1019 // Convenience aliases.
1020 Register base = obj_;
1021 // The UnsafeCASObject intrinsic uses a register pair as field
1022 // offset ("long offset"), of which only the low part contains
1023 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001024 Register offset = field_offset.AsRegisterPairLow<Register>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001025 Register expected = temp1_;
1026 Register value = ref_reg;
1027 Register tmp_ptr = IP; // Pointer to actual memory.
1028 Register tmp = temp2_; // Value in memory.
1029
1030 __ add(tmp_ptr, base, ShifterOperand(offset));
1031
1032 if (kPoisonHeapReferences) {
1033 __ PoisonHeapReference(expected);
1034 if (value == expected) {
1035 // Do not poison `value`, as it is the same register as
1036 // `expected`, which has just been poisoned.
1037 } else {
1038 __ PoisonHeapReference(value);
1039 }
1040 }
1041
1042 // do {
1043 // tmp = [r_ptr] - expected;
1044 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1045
Roland Levillain24a4d112016-10-26 13:10:46 +01001046 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001047 __ Bind(&loop_head);
1048
1049 __ ldrex(tmp, tmp_ptr);
1050
1051 __ subs(tmp, tmp, ShifterOperand(expected));
1052
Roland Levillain24a4d112016-10-26 13:10:46 +01001053 __ it(NE);
1054 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001055
Roland Levillain24a4d112016-10-26 13:10:46 +01001056 __ b(&exit_loop, NE);
1057
1058 __ strex(tmp, value, tmp_ptr);
1059 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001060 __ b(&loop_head, EQ);
1061
Roland Levillain24a4d112016-10-26 13:10:46 +01001062 __ Bind(&exit_loop);
1063
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001064 if (kPoisonHeapReferences) {
1065 __ UnpoisonHeapReference(expected);
1066 if (value == expected) {
1067 // Do not unpoison `value`, as it is the same register as
1068 // `expected`, which has just been unpoisoned.
1069 } else {
1070 __ UnpoisonHeapReference(value);
1071 }
1072 }
1073
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001074 __ b(GetExitLabel());
1075 }
1076
1077 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001078 // The register containing the object holding the marked object reference field.
1079 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001080 // The offset, index and scale factor to access the reference in `obj_`.
1081 uint32_t offset_;
1082 Location index_;
1083 ScaleFactor scale_factor_;
1084 // Is a null check required?
1085 bool needs_null_check_;
1086 // A temporary register used to hold the lock word of `obj_`; and
1087 // also to hold the original reference value, when the reference is
1088 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001089 const Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001090 // A temporary register used in the implementation of the CAS, to
1091 // update the object's reference field.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001092 const Register temp2_;
1093
Roland Levillain54f869e2017-03-06 13:54:11 +00001094 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001095};
1096
Roland Levillain3b359c72015-11-17 19:35:12 +00001097// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001098class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001099 public:
1100 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
1101 Location out,
1102 Location ref,
1103 Location obj,
1104 uint32_t offset,
1105 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001106 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +00001107 out_(out),
1108 ref_(ref),
1109 obj_(obj),
1110 offset_(offset),
1111 index_(index) {
1112 DCHECK(kEmitCompilerReadBarrier);
1113 // If `obj` is equal to `out` or `ref`, it means the initial object
1114 // has been overwritten by (or after) the heap object reference load
1115 // to be instrumented, e.g.:
1116 //
1117 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00001118 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001119 //
1120 // In that case, we have lost the information about the original
1121 // object, and the emitted read barrier cannot work properly.
1122 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1123 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1124 }
1125
1126 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1127 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1128 LocationSummary* locations = instruction_->GetLocations();
1129 Register reg_out = out_.AsRegister<Register>();
1130 DCHECK(locations->CanCall());
1131 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +01001132 DCHECK(instruction_->IsInstanceFieldGet() ||
1133 instruction_->IsStaticFieldGet() ||
1134 instruction_->IsArrayGet() ||
1135 instruction_->IsInstanceOf() ||
1136 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +01001137 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillainc9285912015-12-18 10:38:42 +00001138 << "Unexpected instruction in read barrier for heap reference slow path: "
1139 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001140 // The read barrier instrumentation of object ArrayGet
1141 // instructions does not support the HIntermediateAddress
1142 // instruction.
1143 DCHECK(!(instruction_->IsArrayGet() &&
1144 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +00001145
1146 __ Bind(GetEntryLabel());
1147 SaveLiveRegisters(codegen, locations);
1148
1149 // We may have to change the index's value, but as `index_` is a
1150 // constant member (like other "inputs" of this slow path),
1151 // introduce a copy of it, `index`.
1152 Location index = index_;
1153 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001154 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +00001155 if (instruction_->IsArrayGet()) {
1156 // Compute the actual memory offset and store it in `index`.
1157 Register index_reg = index_.AsRegister<Register>();
1158 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
1159 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
1160 // We are about to change the value of `index_reg` (see the
1161 // calls to art::arm::Thumb2Assembler::Lsl and
1162 // art::arm::Thumb2Assembler::AddConstant below), but it has
1163 // not been saved by the previous call to
1164 // art::SlowPathCode::SaveLiveRegisters, as it is a
1165 // callee-save register --
1166 // art::SlowPathCode::SaveLiveRegisters does not consider
1167 // callee-save registers, as it has been designed with the
1168 // assumption that callee-save registers are supposed to be
1169 // handled by the called function. So, as a callee-save
1170 // register, `index_reg` _would_ eventually be saved onto
1171 // the stack, but it would be too late: we would have
1172 // changed its value earlier. Therefore, we manually save
1173 // it here into another freely available register,
1174 // `free_reg`, chosen of course among the caller-save
1175 // registers (as a callee-save `free_reg` register would
1176 // exhibit the same problem).
1177 //
1178 // Note we could have requested a temporary register from
1179 // the register allocator instead; but we prefer not to, as
1180 // this is a slow path, and we know we can find a
1181 // caller-save register that is available.
1182 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1183 __ Mov(free_reg, index_reg);
1184 index_reg = free_reg;
1185 index = Location::RegisterLocation(index_reg);
1186 } else {
1187 // The initial register stored in `index_` has already been
1188 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1189 // (as it is not a callee-save register), so we can freely
1190 // use it.
1191 }
1192 // Shifting the index value contained in `index_reg` by the scale
1193 // factor (2) cannot overflow in practice, as the runtime is
1194 // unable to allocate object arrays with a size larger than
1195 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1196 __ Lsl(index_reg, index_reg, TIMES_4);
1197 static_assert(
1198 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1199 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1200 __ AddConstant(index_reg, index_reg, offset_);
1201 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001202 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1203 // intrinsics, `index_` is not shifted by a scale factor of 2
1204 // (as in the case of ArrayGet), as it is actually an offset
1205 // to an object field within an object.
1206 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001207 DCHECK(instruction_->GetLocations()->Intrinsified());
1208 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1209 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1210 << instruction_->AsInvoke()->GetIntrinsic();
1211 DCHECK_EQ(offset_, 0U);
1212 DCHECK(index_.IsRegisterPair());
1213 // UnsafeGet's offset location is a register pair, the low
1214 // part contains the correct offset.
1215 index = index_.ToLow();
1216 }
1217 }
1218
1219 // We're moving two or three locations to locations that could
1220 // overlap, so we need a parallel move resolver.
1221 InvokeRuntimeCallingConvention calling_convention;
1222 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1223 parallel_move.AddMove(ref_,
1224 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1225 Primitive::kPrimNot,
1226 nullptr);
1227 parallel_move.AddMove(obj_,
1228 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1229 Primitive::kPrimNot,
1230 nullptr);
1231 if (index.IsValid()) {
1232 parallel_move.AddMove(index,
1233 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1234 Primitive::kPrimInt,
1235 nullptr);
1236 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1237 } else {
1238 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1239 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1240 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001241 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001242 CheckEntrypointTypes<
1243 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1244 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1245
1246 RestoreLiveRegisters(codegen, locations);
1247 __ b(GetExitLabel());
1248 }
1249
1250 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1251
1252 private:
1253 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1254 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1255 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1256 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1257 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1258 return static_cast<Register>(i);
1259 }
1260 }
1261 // We shall never fail to find a free caller-save register, as
1262 // there are more than two core caller-save registers on ARM
1263 // (meaning it is possible to find one which is different from
1264 // `ref` and `obj`).
1265 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1266 LOG(FATAL) << "Could not find a free caller-save register";
1267 UNREACHABLE();
1268 }
1269
Roland Levillain3b359c72015-11-17 19:35:12 +00001270 const Location out_;
1271 const Location ref_;
1272 const Location obj_;
1273 const uint32_t offset_;
1274 // An additional location containing an index to an array.
1275 // Only used for HArrayGet and the UnsafeGetObject &
1276 // UnsafeGetObjectVolatile intrinsics.
1277 const Location index_;
1278
1279 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1280};
1281
1282// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001283class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001284 public:
1285 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001286 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001287 DCHECK(kEmitCompilerReadBarrier);
1288 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001289
1290 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1291 LocationSummary* locations = instruction_->GetLocations();
1292 Register reg_out = out_.AsRegister<Register>();
1293 DCHECK(locations->CanCall());
1294 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001295 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1296 << "Unexpected instruction in read barrier for GC root slow path: "
1297 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001298
1299 __ Bind(GetEntryLabel());
1300 SaveLiveRegisters(codegen, locations);
1301
1302 InvokeRuntimeCallingConvention calling_convention;
1303 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1304 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001305 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001306 instruction_,
1307 instruction_->GetDexPc(),
1308 this);
1309 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1310 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1311
1312 RestoreLiveRegisters(codegen, locations);
1313 __ b(GetExitLabel());
1314 }
1315
1316 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1317
1318 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001319 const Location out_;
1320 const Location root_;
1321
1322 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1323};
1324
Aart Bike9f37602015-10-09 11:15:55 -07001325inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001326 switch (cond) {
1327 case kCondEQ: return EQ;
1328 case kCondNE: return NE;
1329 case kCondLT: return LT;
1330 case kCondLE: return LE;
1331 case kCondGT: return GT;
1332 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001333 case kCondB: return LO;
1334 case kCondBE: return LS;
1335 case kCondA: return HI;
1336 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001337 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001338 LOG(FATAL) << "Unreachable";
1339 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001340}
1341
Aart Bike9f37602015-10-09 11:15:55 -07001342// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001343inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001344 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001345 case kCondEQ: return EQ;
1346 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001347 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001348 case kCondLT: return LO;
1349 case kCondLE: return LS;
1350 case kCondGT: return HI;
1351 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001352 // Unsigned remain unchanged.
1353 case kCondB: return LO;
1354 case kCondBE: return LS;
1355 case kCondA: return HI;
1356 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001357 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001358 LOG(FATAL) << "Unreachable";
1359 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001360}
1361
Vladimir Markod6e069b2016-01-18 11:11:01 +00001362inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1363 // The ARM condition codes can express all the necessary branches, see the
1364 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1365 // There is no dex instruction or HIR that would need the missing conditions
1366 // "equal or unordered" or "not equal".
1367 switch (cond) {
1368 case kCondEQ: return EQ;
1369 case kCondNE: return NE /* unordered */;
1370 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1371 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1372 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1373 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1374 default:
1375 LOG(FATAL) << "UNREACHABLE";
1376 UNREACHABLE();
1377 }
1378}
1379
Anton Kirilov74234da2017-01-13 14:42:47 +00001380inline Shift ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1381 switch (op_kind) {
1382 case HDataProcWithShifterOp::kASR: return ASR;
1383 case HDataProcWithShifterOp::kLSL: return LSL;
1384 case HDataProcWithShifterOp::kLSR: return LSR;
1385 default:
1386 LOG(FATAL) << "Unexpected op kind " << op_kind;
1387 UNREACHABLE();
1388 }
1389}
1390
1391static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1392 Register out,
1393 Register first,
1394 const ShifterOperand& second,
1395 CodeGeneratorARM* codegen) {
1396 if (second.IsImmediate() && second.GetImmediate() == 0) {
1397 const ShifterOperand in = kind == HInstruction::kAnd
1398 ? ShifterOperand(0)
1399 : ShifterOperand(first);
1400
1401 __ mov(out, in);
1402 } else {
1403 switch (kind) {
1404 case HInstruction::kAdd:
1405 __ add(out, first, second);
1406 break;
1407 case HInstruction::kAnd:
1408 __ and_(out, first, second);
1409 break;
1410 case HInstruction::kOr:
1411 __ orr(out, first, second);
1412 break;
1413 case HInstruction::kSub:
1414 __ sub(out, first, second);
1415 break;
1416 case HInstruction::kXor:
1417 __ eor(out, first, second);
1418 break;
1419 default:
1420 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1421 UNREACHABLE();
1422 }
1423 }
1424}
1425
1426static void GenerateDataProc(HInstruction::InstructionKind kind,
1427 const Location& out,
1428 const Location& first,
1429 const ShifterOperand& second_lo,
1430 const ShifterOperand& second_hi,
1431 CodeGeneratorARM* codegen) {
1432 const Register first_hi = first.AsRegisterPairHigh<Register>();
1433 const Register first_lo = first.AsRegisterPairLow<Register>();
1434 const Register out_hi = out.AsRegisterPairHigh<Register>();
1435 const Register out_lo = out.AsRegisterPairLow<Register>();
1436
1437 if (kind == HInstruction::kAdd) {
1438 __ adds(out_lo, first_lo, second_lo);
1439 __ adc(out_hi, first_hi, second_hi);
1440 } else if (kind == HInstruction::kSub) {
1441 __ subs(out_lo, first_lo, second_lo);
1442 __ sbc(out_hi, first_hi, second_hi);
1443 } else {
1444 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1445 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1446 }
1447}
1448
1449static ShifterOperand GetShifterOperand(Register rm, Shift shift, uint32_t shift_imm) {
1450 return shift_imm == 0 ? ShifterOperand(rm) : ShifterOperand(rm, shift, shift_imm);
1451}
1452
1453static void GenerateLongDataProc(HDataProcWithShifterOp* instruction, CodeGeneratorARM* codegen) {
1454 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1455 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1456
1457 const LocationSummary* const locations = instruction->GetLocations();
1458 const uint32_t shift_value = instruction->GetShiftAmount();
1459 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1460 const Location first = locations->InAt(0);
1461 const Location second = locations->InAt(1);
1462 const Location out = locations->Out();
1463 const Register first_hi = first.AsRegisterPairHigh<Register>();
1464 const Register first_lo = first.AsRegisterPairLow<Register>();
1465 const Register out_hi = out.AsRegisterPairHigh<Register>();
1466 const Register out_lo = out.AsRegisterPairLow<Register>();
1467 const Register second_hi = second.AsRegisterPairHigh<Register>();
1468 const Register second_lo = second.AsRegisterPairLow<Register>();
1469 const Shift shift = ShiftFromOpKind(instruction->GetOpKind());
1470
1471 if (shift_value >= 32) {
1472 if (shift == LSL) {
1473 GenerateDataProcInstruction(kind,
1474 out_hi,
1475 first_hi,
1476 ShifterOperand(second_lo, LSL, shift_value - 32),
1477 codegen);
1478 GenerateDataProcInstruction(kind,
1479 out_lo,
1480 first_lo,
1481 ShifterOperand(0),
1482 codegen);
1483 } else if (shift == ASR) {
1484 GenerateDataProc(kind,
1485 out,
1486 first,
1487 GetShifterOperand(second_hi, ASR, shift_value - 32),
1488 ShifterOperand(second_hi, ASR, 31),
1489 codegen);
1490 } else {
1491 DCHECK_EQ(shift, LSR);
1492 GenerateDataProc(kind,
1493 out,
1494 first,
1495 GetShifterOperand(second_hi, LSR, shift_value - 32),
1496 ShifterOperand(0),
1497 codegen);
1498 }
1499 } else {
1500 DCHECK_GT(shift_value, 1U);
1501 DCHECK_LT(shift_value, 32U);
1502
1503 if (shift == LSL) {
1504 // We are not doing this for HInstruction::kAdd because the output will require
1505 // Location::kOutputOverlap; not applicable to other cases.
1506 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1507 GenerateDataProcInstruction(kind,
1508 out_hi,
1509 first_hi,
1510 ShifterOperand(second_hi, LSL, shift_value),
1511 codegen);
1512 GenerateDataProcInstruction(kind,
1513 out_hi,
1514 out_hi,
1515 ShifterOperand(second_lo, LSR, 32 - shift_value),
1516 codegen);
1517 GenerateDataProcInstruction(kind,
1518 out_lo,
1519 first_lo,
1520 ShifterOperand(second_lo, LSL, shift_value),
1521 codegen);
1522 } else {
1523 __ Lsl(IP, second_hi, shift_value);
1524 __ orr(IP, IP, ShifterOperand(second_lo, LSR, 32 - shift_value));
1525 GenerateDataProc(kind,
1526 out,
1527 first,
1528 ShifterOperand(second_lo, LSL, shift_value),
1529 ShifterOperand(IP),
1530 codegen);
1531 }
1532 } else {
1533 DCHECK(shift == ASR || shift == LSR);
1534
1535 // We are not doing this for HInstruction::kAdd because the output will require
1536 // Location::kOutputOverlap; not applicable to other cases.
1537 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1538 GenerateDataProcInstruction(kind,
1539 out_lo,
1540 first_lo,
1541 ShifterOperand(second_lo, LSR, shift_value),
1542 codegen);
1543 GenerateDataProcInstruction(kind,
1544 out_lo,
1545 out_lo,
1546 ShifterOperand(second_hi, LSL, 32 - shift_value),
1547 codegen);
1548 GenerateDataProcInstruction(kind,
1549 out_hi,
1550 first_hi,
1551 ShifterOperand(second_hi, shift, shift_value),
1552 codegen);
1553 } else {
1554 __ Lsr(IP, second_lo, shift_value);
1555 __ orr(IP, IP, ShifterOperand(second_hi, LSL, 32 - shift_value));
1556 GenerateDataProc(kind,
1557 out,
1558 first,
1559 ShifterOperand(IP),
1560 ShifterOperand(second_hi, shift, shift_value),
1561 codegen);
1562 }
1563 }
1564 }
1565}
1566
Donghui Bai426b49c2016-11-08 14:55:38 +08001567static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARM* codegen) {
1568 Primitive::Type type = instruction->InputAt(0)->GetType();
1569 Location lhs_loc = instruction->GetLocations()->InAt(0);
1570 Location rhs_loc = instruction->GetLocations()->InAt(1);
1571 if (rhs_loc.IsConstant()) {
1572 // 0.0 is the only immediate that can be encoded directly in
1573 // a VCMP instruction.
1574 //
1575 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1576 // specify that in a floating-point comparison, positive zero
1577 // and negative zero are considered equal, so we can use the
1578 // literal 0.0 for both cases here.
1579 //
1580 // Note however that some methods (Float.equal, Float.compare,
1581 // Float.compareTo, Double.equal, Double.compare,
1582 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1583 // StrictMath.min) consider 0.0 to be (strictly) greater than
1584 // -0.0. So if we ever translate calls to these methods into a
1585 // HCompare instruction, we must handle the -0.0 case with
1586 // care here.
1587 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1588 if (type == Primitive::kPrimFloat) {
1589 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1590 } else {
1591 DCHECK_EQ(type, Primitive::kPrimDouble);
1592 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1593 }
1594 } else {
1595 if (type == Primitive::kPrimFloat) {
1596 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1597 } else {
1598 DCHECK_EQ(type, Primitive::kPrimDouble);
1599 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1600 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1601 }
1602 }
1603}
1604
1605static Condition GenerateLongTestConstant(HCondition* condition,
1606 bool invert,
1607 CodeGeneratorARM* codegen) {
1608 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1609
1610 const LocationSummary* const locations = condition->GetLocations();
1611 IfCondition cond = invert ? condition->GetOppositeCondition() : condition->GetCondition();
1612 Condition ret = EQ;
1613 const Location left = locations->InAt(0);
1614 const Location right = locations->InAt(1);
1615
1616 DCHECK(right.IsConstant());
1617
1618 const Register left_high = left.AsRegisterPairHigh<Register>();
1619 const Register left_low = left.AsRegisterPairLow<Register>();
1620 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1621
1622 switch (cond) {
1623 case kCondEQ:
1624 case kCondNE:
1625 case kCondB:
1626 case kCondBE:
1627 case kCondA:
1628 case kCondAE:
1629 __ CmpConstant(left_high, High32Bits(value));
1630 __ it(EQ);
1631 __ cmp(left_low, ShifterOperand(Low32Bits(value)), EQ);
1632 ret = ARMUnsignedCondition(cond);
1633 break;
1634 case kCondLE:
1635 case kCondGT:
1636 // Trivially true or false.
1637 if (value == std::numeric_limits<int64_t>::max()) {
1638 __ cmp(left_low, ShifterOperand(left_low));
1639 ret = cond == kCondLE ? EQ : NE;
1640 break;
1641 }
1642
1643 if (cond == kCondLE) {
1644 cond = kCondLT;
1645 } else {
1646 DCHECK_EQ(cond, kCondGT);
1647 cond = kCondGE;
1648 }
1649
1650 value++;
1651 FALLTHROUGH_INTENDED;
1652 case kCondGE:
1653 case kCondLT:
1654 __ CmpConstant(left_low, Low32Bits(value));
1655 __ sbcs(IP, left_high, ShifterOperand(High32Bits(value)));
1656 ret = ARMCondition(cond);
1657 break;
1658 default:
1659 LOG(FATAL) << "Unreachable";
1660 UNREACHABLE();
1661 }
1662
1663 return ret;
1664}
1665
1666static Condition GenerateLongTest(HCondition* condition,
1667 bool invert,
1668 CodeGeneratorARM* codegen) {
1669 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1670
1671 const LocationSummary* const locations = condition->GetLocations();
1672 IfCondition cond = invert ? condition->GetOppositeCondition() : condition->GetCondition();
1673 Condition ret = EQ;
1674 Location left = locations->InAt(0);
1675 Location right = locations->InAt(1);
1676
1677 DCHECK(right.IsRegisterPair());
1678
1679 switch (cond) {
1680 case kCondEQ:
1681 case kCondNE:
1682 case kCondB:
1683 case kCondBE:
1684 case kCondA:
1685 case kCondAE:
1686 __ cmp(left.AsRegisterPairHigh<Register>(),
1687 ShifterOperand(right.AsRegisterPairHigh<Register>()));
1688 __ it(EQ);
1689 __ cmp(left.AsRegisterPairLow<Register>(),
1690 ShifterOperand(right.AsRegisterPairLow<Register>()),
1691 EQ);
1692 ret = ARMUnsignedCondition(cond);
1693 break;
1694 case kCondLE:
1695 case kCondGT:
1696 if (cond == kCondLE) {
1697 cond = kCondGE;
1698 } else {
1699 DCHECK_EQ(cond, kCondGT);
1700 cond = kCondLT;
1701 }
1702
1703 std::swap(left, right);
1704 FALLTHROUGH_INTENDED;
1705 case kCondGE:
1706 case kCondLT:
1707 __ cmp(left.AsRegisterPairLow<Register>(),
1708 ShifterOperand(right.AsRegisterPairLow<Register>()));
1709 __ sbcs(IP,
1710 left.AsRegisterPairHigh<Register>(),
1711 ShifterOperand(right.AsRegisterPairHigh<Register>()));
1712 ret = ARMCondition(cond);
1713 break;
1714 default:
1715 LOG(FATAL) << "Unreachable";
1716 UNREACHABLE();
1717 }
1718
1719 return ret;
1720}
1721
1722static Condition GenerateTest(HInstruction* instruction,
1723 Location loc,
1724 bool invert,
1725 CodeGeneratorARM* codegen) {
1726 DCHECK(!instruction->IsConstant());
1727
1728 Condition ret = invert ? EQ : NE;
1729
1730 if (IsBooleanValueOrMaterializedCondition(instruction)) {
1731 __ CmpConstant(loc.AsRegister<Register>(), 0);
1732 } else {
1733 HCondition* const condition = instruction->AsCondition();
1734 const LocationSummary* const locations = condition->GetLocations();
1735 const Primitive::Type type = condition->GetLeft()->GetType();
1736 const IfCondition cond = invert ? condition->GetOppositeCondition() : condition->GetCondition();
1737 const Location right = locations->InAt(1);
1738
1739 if (type == Primitive::kPrimLong) {
1740 ret = condition->GetLocations()->InAt(1).IsConstant()
1741 ? GenerateLongTestConstant(condition, invert, codegen)
1742 : GenerateLongTest(condition, invert, codegen);
1743 } else if (Primitive::IsFloatingPointType(type)) {
1744 GenerateVcmp(condition, codegen);
1745 __ vmstat();
1746 ret = ARMFPCondition(cond, condition->IsGtBias());
1747 } else {
1748 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1749
1750 const Register left = locations->InAt(0).AsRegister<Register>();
1751
1752 if (right.IsRegister()) {
1753 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
1754 } else {
1755 DCHECK(right.IsConstant());
1756 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
1757 }
1758
1759 ret = ARMCondition(cond);
1760 }
1761 }
1762
1763 return ret;
1764}
1765
1766static bool CanGenerateTest(HInstruction* condition, ArmAssembler* assembler) {
1767 if (!IsBooleanValueOrMaterializedCondition(condition)) {
1768 const HCondition* const cond = condition->AsCondition();
1769
1770 if (cond->GetLeft()->GetType() == Primitive::kPrimLong) {
1771 const LocationSummary* const locations = cond->GetLocations();
1772 const IfCondition c = cond->GetCondition();
1773
1774 if (locations->InAt(1).IsConstant()) {
1775 const int64_t value = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
1776 ShifterOperand so;
1777
1778 if (c < kCondLT || c > kCondGE) {
1779 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1780 // we check that the least significant half of the first input to be compared
1781 // is in a low register (the other half is read outside an IT block), and
1782 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
1783 // encoding can be used.
1784 if (!ArmAssembler::IsLowRegister(locations->InAt(0).AsRegisterPairLow<Register>()) ||
1785 !IsUint<8>(Low32Bits(value))) {
1786 return false;
1787 }
1788 } else if (c == kCondLE || c == kCondGT) {
1789 if (value < std::numeric_limits<int64_t>::max() &&
1790 !assembler->ShifterOperandCanHold(kNoRegister,
1791 kNoRegister,
1792 SBC,
1793 High32Bits(value + 1),
1794 kCcSet,
1795 &so)) {
1796 return false;
1797 }
1798 } else if (!assembler->ShifterOperandCanHold(kNoRegister,
1799 kNoRegister,
1800 SBC,
1801 High32Bits(value),
1802 kCcSet,
1803 &so)) {
1804 return false;
1805 }
1806 }
1807 }
1808 }
1809
1810 return true;
1811}
1812
1813static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
1814 const Primitive::Type type = constant->GetType();
1815 bool ret = false;
1816
1817 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1818
1819 if (type == Primitive::kPrimLong) {
1820 const uint64_t value = constant->AsLongConstant()->GetValueAsUint64();
1821
1822 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1823 } else {
1824 ret = IsUint<8>(CodeGenerator::GetInt32ValueOf(constant));
1825 }
1826
1827 return ret;
1828}
1829
1830static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
1831 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
1832
1833 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1834 return Location::ConstantLocation(constant->AsConstant());
1835 }
1836
1837 return Location::RequiresRegister();
1838}
1839
1840static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1841 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1842 // we check that we are not dealing with floating-point output (there is no
1843 // 16-bit VMOV encoding).
1844 if (!out.IsRegister() && !out.IsRegisterPair()) {
1845 return false;
1846 }
1847
1848 // For constants, we also check that the output is in one or two low registers,
1849 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1850 // MOV encoding can be used.
1851 if (src.IsConstant()) {
1852 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1853 return false;
1854 }
1855
1856 if (out.IsRegister()) {
1857 if (!ArmAssembler::IsLowRegister(out.AsRegister<Register>())) {
1858 return false;
1859 }
1860 } else {
1861 DCHECK(out.IsRegisterPair());
1862
1863 if (!ArmAssembler::IsLowRegister(out.AsRegisterPairHigh<Register>())) {
1864 return false;
1865 }
1866 }
1867 }
1868
1869 return true;
1870}
1871
Anton Kirilov74234da2017-01-13 14:42:47 +00001872#undef __
1873// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1874#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
1875
Donghui Bai426b49c2016-11-08 14:55:38 +08001876Label* CodeGeneratorARM::GetFinalLabel(HInstruction* instruction, Label* final_label) {
1877 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001878 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001879
1880 const HBasicBlock* const block = instruction->GetBlock();
1881 const HLoopInformation* const info = block->GetLoopInformation();
1882 HInstruction* const next = instruction->GetNext();
1883
1884 // Avoid a branch to a branch.
1885 if (next->IsGoto() && (info == nullptr ||
1886 !info->IsBackEdge(*block) ||
1887 !info->HasSuspendCheck())) {
1888 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1889 }
1890
1891 return final_label;
1892}
1893
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001894void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001895 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001896}
1897
1898void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001899 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001900}
1901
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001902size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1903 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1904 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001905}
1906
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001907size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1908 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1909 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001910}
1911
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001912size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1913 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1914 return kArmWordSize;
1915}
1916
1917size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1918 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1919 return kArmWordSize;
1920}
1921
Calin Juravle34166012014-12-19 17:22:29 +00001922CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001923 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001924 const CompilerOptions& compiler_options,
1925 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001926 : CodeGenerator(graph,
1927 kNumberOfCoreRegisters,
1928 kNumberOfSRegisters,
1929 kNumberOfRegisterPairs,
1930 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1931 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001932 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1933 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001934 compiler_options,
1935 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001936 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001937 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001938 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001939 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001940 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001941 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001942 uint32_literals_(std::less<uint32_t>(),
1943 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001944 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1945 boot_image_string_patches_(StringReferenceValueComparator(),
1946 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1947 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001948 boot_image_type_patches_(TypeReferenceValueComparator(),
1949 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1950 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001951 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001952 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001953 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1954 jit_class_patches_(TypeReferenceValueComparator(),
1955 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07001956 // Always save the LR register to mimic Quick.
1957 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01001958}
1959
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001960void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
1961 // Ensure that we fix up branches and literal loads and emit the literal pool.
1962 __ FinalizeCode();
1963
1964 // Adjust native pc offsets in stack maps.
1965 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001966 uint32_t old_position =
1967 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001968 uint32_t new_position = __ GetAdjustedPosition(old_position);
1969 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1970 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01001971 // Adjust pc offsets for the disassembly information.
1972 if (disasm_info_ != nullptr) {
1973 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1974 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1975 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1976 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1977 it.second.start = __ GetAdjustedPosition(it.second.start);
1978 it.second.end = __ GetAdjustedPosition(it.second.end);
1979 }
1980 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1981 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1982 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1983 }
1984 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00001985
1986 CodeGenerator::Finalize(allocator);
1987}
1988
David Brazdil58282f42016-01-14 12:45:10 +00001989void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001990 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001991 blocked_core_registers_[SP] = true;
1992 blocked_core_registers_[LR] = true;
1993 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001994
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001995 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001996 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001997
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001998 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001999 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002000
David Brazdil58282f42016-01-14 12:45:10 +00002001 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01002002 // Stubs do not save callee-save floating point registers. If the graph
2003 // is debuggable, we need to deal with these registers differently. For
2004 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002005 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
2006 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
2007 }
2008 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002009}
2010
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002011InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002012 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002013 assembler_(codegen->GetAssembler()),
2014 codegen_(codegen) {}
2015
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002016void CodeGeneratorARM::ComputeSpillMask() {
2017 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2018 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00002019 // There is no easy instruction to restore just the PC on thumb2. We spill and
2020 // restore another arbitrary register.
2021 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002022 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2023 // We use vpush and vpop for saving and restoring floating point registers, which take
2024 // a SRegister and the number of registers to save/restore after that SRegister. We
2025 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2026 // but in the range.
2027 if (fpu_spill_mask_ != 0) {
2028 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2029 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2030 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2031 fpu_spill_mask_ |= (1 << i);
2032 }
2033 }
2034}
2035
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002036static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002037 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002038}
2039
2040static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002041 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002042}
2043
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002044void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00002045 bool skip_overflow_check =
2046 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002047 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002048 __ Bind(&frame_entry_label_);
2049
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002050 if (HasEmptyFrame()) {
2051 return;
2052 }
2053
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002054 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002055 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
2056 __ LoadFromOffset(kLoadWord, IP, IP, 0);
2057 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002058 }
2059
Andreas Gampe501fd632015-09-10 16:11:06 -07002060 __ PushList(core_spill_mask_);
2061 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2062 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002063 if (fpu_spill_mask_ != 0) {
2064 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2065 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002066 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01002067 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002068 }
Mingyao Yang063fc772016-08-02 11:02:54 -07002069
2070 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2071 // Initialize should_deoptimize flag to 0.
2072 __ mov(IP, ShifterOperand(0));
2073 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
2074 }
2075
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002076 int adjust = GetFrameSize() - FrameEntrySpillSize();
2077 __ AddConstant(SP, -adjust);
2078 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002079
2080 // Save the current method if we need it. Note that we do not
2081 // do this in HCurrentMethod, as the instruction might have been removed
2082 // in the SSA graph.
2083 if (RequiresCurrentMethod()) {
2084 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
2085 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002086}
2087
2088void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002089 if (HasEmptyFrame()) {
2090 __ bx(LR);
2091 return;
2092 }
David Srbeckyc34dc932015-04-12 09:27:43 +01002093 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002094 int adjust = GetFrameSize() - FrameEntrySpillSize();
2095 __ AddConstant(SP, adjust);
2096 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002097 if (fpu_spill_mask_ != 0) {
2098 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2099 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07002100 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002101 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002102 }
Andreas Gampe501fd632015-09-10 16:11:06 -07002103 // Pop LR into PC to return.
2104 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
2105 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
2106 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01002107 __ cfi().RestoreState();
2108 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002109}
2110
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002111void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07002112 Label* label = GetLabelOf(block);
2113 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002114}
2115
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002116Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002117 switch (type) {
2118 case Primitive::kPrimBoolean:
2119 case Primitive::kPrimByte:
2120 case Primitive::kPrimChar:
2121 case Primitive::kPrimShort:
2122 case Primitive::kPrimInt:
2123 case Primitive::kPrimNot: {
2124 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002125 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002126 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002127 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002128 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002129 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002130 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002131 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002132
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002133 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002134 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002135 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002136 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002137 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002138 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002139 if (calling_convention.GetRegisterAt(index) == R1) {
2140 // Skip R1, and use R2_R3 instead.
2141 gp_index_++;
2142 index++;
2143 }
2144 }
2145 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2146 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002147 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01002148
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002149 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002150 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002151 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002152 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2153 }
2154 }
2155
2156 case Primitive::kPrimFloat: {
2157 uint32_t stack_index = stack_index_++;
2158 if (float_index_ % 2 == 0) {
2159 float_index_ = std::max(double_index_, float_index_);
2160 }
2161 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2162 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
2163 } else {
2164 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2165 }
2166 }
2167
2168 case Primitive::kPrimDouble: {
2169 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2170 uint32_t stack_index = stack_index_;
2171 stack_index_ += 2;
2172 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2173 uint32_t index = double_index_;
2174 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002175 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002176 calling_convention.GetFpuRegisterAt(index),
2177 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002178 DCHECK(ExpectedPairLayout(result));
2179 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002180 } else {
2181 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002182 }
2183 }
2184
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002185 case Primitive::kPrimVoid:
2186 LOG(FATAL) << "Unexpected parameter type " << type;
2187 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002188 }
Roland Levillain3b359c72015-11-17 19:35:12 +00002189 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002190}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002191
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002192Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002193 switch (type) {
2194 case Primitive::kPrimBoolean:
2195 case Primitive::kPrimByte:
2196 case Primitive::kPrimChar:
2197 case Primitive::kPrimShort:
2198 case Primitive::kPrimInt:
2199 case Primitive::kPrimNot: {
2200 return Location::RegisterLocation(R0);
2201 }
2202
2203 case Primitive::kPrimFloat: {
2204 return Location::FpuRegisterLocation(S0);
2205 }
2206
2207 case Primitive::kPrimLong: {
2208 return Location::RegisterPairLocation(R0, R1);
2209 }
2210
2211 case Primitive::kPrimDouble: {
2212 return Location::FpuRegisterPairLocation(S0, S1);
2213 }
2214
2215 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00002216 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002217 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002218
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002219 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002220}
2221
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002222Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
2223 return Location::RegisterLocation(kMethodRegisterArgument);
2224}
2225
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002226void CodeGeneratorARM::Move32(Location destination, Location source) {
2227 if (source.Equals(destination)) {
2228 return;
2229 }
2230 if (destination.IsRegister()) {
2231 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002232 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002233 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002234 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002235 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002236 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002237 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002238 } else if (destination.IsFpuRegister()) {
2239 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002240 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002241 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002242 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002243 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002244 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002245 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002246 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002247 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002248 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002249 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002250 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002251 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002252 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002253 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002254 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
2255 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002256 }
2257 }
2258}
2259
2260void CodeGeneratorARM::Move64(Location destination, Location source) {
2261 if (source.Equals(destination)) {
2262 return;
2263 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002264 if (destination.IsRegisterPair()) {
2265 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002266 EmitParallelMoves(
2267 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
2268 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002269 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002270 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002271 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
2272 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002273 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002274 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002275 } else if (source.IsFpuRegisterPair()) {
2276 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
2277 destination.AsRegisterPairHigh<Register>(),
2278 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002279 } else {
2280 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002281 DCHECK(ExpectedPairLayout(destination));
2282 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
2283 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002284 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002285 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002286 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002287 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2288 SP,
2289 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01002290 } else if (source.IsRegisterPair()) {
2291 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2292 source.AsRegisterPairLow<Register>(),
2293 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002294 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002295 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002296 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002297 } else {
2298 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002299 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002300 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002301 if (source.AsRegisterPairLow<Register>() == R1) {
2302 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002303 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
2304 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002305 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002306 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002307 SP, destination.GetStackIndex());
2308 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002309 } else if (source.IsFpuRegisterPair()) {
2310 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
2311 SP,
2312 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002313 } else {
2314 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002315 EmitParallelMoves(
2316 Location::StackSlot(source.GetStackIndex()),
2317 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002318 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002319 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002320 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
2321 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002322 }
2323 }
2324}
2325
Calin Juravle175dc732015-08-25 15:42:32 +01002326void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
2327 DCHECK(location.IsRegister());
2328 __ LoadImmediate(location.AsRegister<Register>(), value);
2329}
2330
Calin Juravlee460d1d2015-09-29 04:52:17 +01002331void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002332 HParallelMove move(GetGraph()->GetArena());
2333 move.AddMove(src, dst, dst_type, nullptr);
2334 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002335}
2336
2337void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
2338 if (location.IsRegister()) {
2339 locations->AddTemp(location);
2340 } else if (location.IsRegisterPair()) {
2341 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
2342 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
2343 } else {
2344 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2345 }
2346}
2347
Calin Juravle175dc732015-08-25 15:42:32 +01002348void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
2349 HInstruction* instruction,
2350 uint32_t dex_pc,
2351 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002352 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002353 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002354 if (EntrypointRequiresStackMap(entrypoint)) {
2355 RecordPcInfo(instruction, dex_pc, slow_path);
2356 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002357}
2358
Roland Levillaindec8f632016-07-22 17:10:06 +01002359void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2360 HInstruction* instruction,
2361 SlowPathCode* slow_path) {
2362 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002363 GenerateInvokeRuntime(entry_point_offset);
2364}
2365
2366void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01002367 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2368 __ blx(LR);
2369}
2370
David Brazdilfc6a86a2015-06-26 10:33:45 +00002371void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002372 DCHECK(!successor->IsExitBlock());
2373
2374 HBasicBlock* block = got->GetBlock();
2375 HInstruction* previous = got->GetPrevious();
2376
2377 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00002378 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002379 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2380 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2381 return;
2382 }
2383
2384 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2385 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2386 }
2387 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002388 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002389 }
2390}
2391
David Brazdilfc6a86a2015-06-26 10:33:45 +00002392void LocationsBuilderARM::VisitGoto(HGoto* got) {
2393 got->SetLocations(nullptr);
2394}
2395
2396void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
2397 HandleGoto(got, got->GetSuccessor());
2398}
2399
2400void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2401 try_boundary->SetLocations(nullptr);
2402}
2403
2404void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2405 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2406 if (!successor->IsExitBlock()) {
2407 HandleGoto(try_boundary, successor);
2408 }
2409}
2410
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002411void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002412 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002413}
2414
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002415void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002416}
2417
Roland Levillain4fa13f62015-07-06 18:11:54 +01002418void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond,
2419 Label* true_label,
Vladimir Markod6e069b2016-01-18 11:11:01 +00002420 Label* false_label ATTRIBUTE_UNUSED) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002421 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00002422 __ b(true_label, ARMFPCondition(cond->GetCondition(), cond->IsGtBias()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002423}
2424
2425void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
2426 Label* true_label,
2427 Label* false_label) {
2428 LocationSummary* locations = cond->GetLocations();
2429 Location left = locations->InAt(0);
2430 Location right = locations->InAt(1);
2431 IfCondition if_cond = cond->GetCondition();
2432
2433 Register left_high = left.AsRegisterPairHigh<Register>();
2434 Register left_low = left.AsRegisterPairLow<Register>();
2435 IfCondition true_high_cond = if_cond;
2436 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07002437 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01002438
2439 // Set the conditions for the test, remembering that == needs to be
2440 // decided using the low words.
Aart Bike9f37602015-10-09 11:15:55 -07002441 // TODO: consider avoiding jumps with temporary and CMP low+SBC high
Roland Levillain4fa13f62015-07-06 18:11:54 +01002442 switch (if_cond) {
2443 case kCondEQ:
2444 case kCondNE:
2445 // Nothing to do.
2446 break;
2447 case kCondLT:
2448 false_high_cond = kCondGT;
2449 break;
2450 case kCondLE:
2451 true_high_cond = kCondLT;
2452 break;
2453 case kCondGT:
2454 false_high_cond = kCondLT;
2455 break;
2456 case kCondGE:
2457 true_high_cond = kCondGT;
2458 break;
Aart Bike9f37602015-10-09 11:15:55 -07002459 case kCondB:
2460 false_high_cond = kCondA;
2461 break;
2462 case kCondBE:
2463 true_high_cond = kCondB;
2464 break;
2465 case kCondA:
2466 false_high_cond = kCondB;
2467 break;
2468 case kCondAE:
2469 true_high_cond = kCondA;
2470 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002471 }
2472 if (right.IsConstant()) {
2473 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
2474 int32_t val_low = Low32Bits(value);
2475 int32_t val_high = High32Bits(value);
2476
Vladimir Markoac6ac102015-12-17 12:14:00 +00002477 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002478 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002479 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002480 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002481 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002482 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002483 __ b(true_label, ARMCondition(true_high_cond));
2484 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002485 }
2486 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00002487 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002488 } else {
2489 Register right_high = right.AsRegisterPairHigh<Register>();
2490 Register right_low = right.AsRegisterPairLow<Register>();
2491
2492 __ cmp(left_high, ShifterOperand(right_high));
2493 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002494 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002495 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002496 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002497 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002498 __ b(true_label, ARMCondition(true_high_cond));
2499 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002500 }
2501 // Must be equal high, so compare the lows.
2502 __ cmp(left_low, ShifterOperand(right_low));
2503 }
2504 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07002505 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01002506 __ b(true_label, final_condition);
2507}
2508
David Brazdil0debae72015-11-12 18:37:00 +00002509void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
2510 Label* true_target_in,
2511 Label* false_target_in) {
2512 // Generated branching requires both targets to be explicit. If either of the
2513 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2514 Label fallthrough_target;
2515 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
2516 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
2517
Roland Levillain4fa13f62015-07-06 18:11:54 +01002518 Primitive::Type type = condition->InputAt(0)->GetType();
2519 switch (type) {
2520 case Primitive::kPrimLong:
2521 GenerateLongComparesAndJumps(condition, true_target, false_target);
2522 break;
2523 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002524 case Primitive::kPrimDouble:
Donghui Bai426b49c2016-11-08 14:55:38 +08002525 GenerateVcmp(condition, codegen_);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002526 GenerateFPJumps(condition, true_target, false_target);
2527 break;
2528 default:
2529 LOG(FATAL) << "Unexpected compare type " << type;
2530 }
2531
David Brazdil0debae72015-11-12 18:37:00 +00002532 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002533 __ b(false_target);
2534 }
David Brazdil0debae72015-11-12 18:37:00 +00002535
2536 if (fallthrough_target.IsLinked()) {
2537 __ Bind(&fallthrough_target);
2538 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002539}
2540
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002541void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002542 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002543 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002544 Label* false_target) {
2545 HInstruction* cond = instruction->InputAt(condition_input_index);
2546
2547 if (true_target == nullptr && false_target == nullptr) {
2548 // Nothing to do. The code always falls through.
2549 return;
2550 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002551 // Constant condition, statically compared against "true" (integer value 1).
2552 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002553 if (true_target != nullptr) {
2554 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002555 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002556 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002557 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002558 if (false_target != nullptr) {
2559 __ b(false_target);
2560 }
2561 }
2562 return;
2563 }
2564
2565 // The following code generates these patterns:
2566 // (1) true_target == nullptr && false_target != nullptr
2567 // - opposite condition true => branch to false_target
2568 // (2) true_target != nullptr && false_target == nullptr
2569 // - condition true => branch to true_target
2570 // (3) true_target != nullptr && false_target != nullptr
2571 // - condition true => branch to true_target
2572 // - branch to false_target
2573 if (IsBooleanValueOrMaterializedCondition(cond)) {
2574 // Condition has been materialized, compare the output to 0.
2575 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2576 DCHECK(cond_val.IsRegister());
2577 if (true_target == nullptr) {
2578 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
2579 } else {
2580 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002581 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002582 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002583 // Condition has not been materialized. Use its inputs as the comparison and
2584 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002585 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002586
2587 // If this is a long or FP comparison that has been folded into
2588 // the HCondition, generate the comparison directly.
2589 Primitive::Type type = condition->InputAt(0)->GetType();
2590 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2591 GenerateCompareTestAndBranch(condition, true_target, false_target);
2592 return;
2593 }
2594
Donghui Bai426b49c2016-11-08 14:55:38 +08002595 Label* non_fallthrough_target;
2596 Condition arm_cond;
David Brazdil0debae72015-11-12 18:37:00 +00002597 LocationSummary* locations = cond->GetLocations();
2598 DCHECK(locations->InAt(0).IsRegister());
2599 Register left = locations->InAt(0).AsRegister<Register>();
2600 Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08002601
David Brazdil0debae72015-11-12 18:37:00 +00002602 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002603 arm_cond = ARMCondition(condition->GetOppositeCondition());
2604 non_fallthrough_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002605 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002606 arm_cond = ARMCondition(condition->GetCondition());
2607 non_fallthrough_target = true_target;
2608 }
2609
2610 if (right.IsConstant() && (arm_cond == NE || arm_cond == EQ) &&
2611 CodeGenerator::GetInt32ValueOf(right.GetConstant()) == 0) {
2612 if (arm_cond == EQ) {
2613 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2614 } else {
2615 DCHECK_EQ(arm_cond, NE);
2616 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2617 }
2618 } else {
2619 if (right.IsRegister()) {
2620 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
2621 } else {
2622 DCHECK(right.IsConstant());
2623 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2624 }
2625
2626 __ b(non_fallthrough_target, arm_cond);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002627 }
Dave Allison20dfc792014-06-16 20:44:29 -07002628 }
David Brazdil0debae72015-11-12 18:37:00 +00002629
2630 // If neither branch falls through (case 3), the conditional branch to `true_target`
2631 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2632 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002633 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002634 }
2635}
2636
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002637void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002638 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2639 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002640 locations->SetInAt(0, Location::RequiresRegister());
2641 }
2642}
2643
2644void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002645 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2646 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2647 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2648 nullptr : codegen_->GetLabelOf(true_successor);
2649 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2650 nullptr : codegen_->GetLabelOf(false_successor);
2651 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002652}
2653
2654void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
2655 LocationSummary* locations = new (GetGraph()->GetArena())
2656 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002657 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002658 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002659 locations->SetInAt(0, Location::RequiresRegister());
2660 }
2661}
2662
2663void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01002664 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002665 GenerateTestAndBranch(deoptimize,
2666 /* condition_input_index */ 0,
2667 slow_path->GetEntryLabel(),
2668 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002669}
Dave Allison20dfc792014-06-16 20:44:29 -07002670
Mingyao Yang063fc772016-08-02 11:02:54 -07002671void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2672 LocationSummary* locations = new (GetGraph()->GetArena())
2673 LocationSummary(flag, LocationSummary::kNoCall);
2674 locations->SetOut(Location::RequiresRegister());
2675}
2676
2677void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2678 __ LoadFromOffset(kLoadWord,
2679 flag->GetLocations()->Out().AsRegister<Register>(),
2680 SP,
2681 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2682}
2683
David Brazdil74eb1b22015-12-14 11:44:01 +00002684void LocationsBuilderARM::VisitSelect(HSelect* select) {
2685 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08002686 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
2687
2688 if (is_floating_point) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002689 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002690 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002691 } else {
2692 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002693 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002694 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002695
David Brazdil74eb1b22015-12-14 11:44:01 +00002696 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002697 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2698 // The code generator handles overlap with the values, but not with the condition.
2699 locations->SetOut(Location::SameAsFirstInput());
2700 } else if (is_floating_point) {
2701 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2702 } else {
2703 if (!locations->InAt(1).IsConstant()) {
2704 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2705 }
2706
2707 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00002708 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002709}
2710
2711void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002712 HInstruction* const condition = select->GetCondition();
2713 const LocationSummary* const locations = select->GetLocations();
2714 const Primitive::Type type = select->GetType();
2715 const Location first = locations->InAt(0);
2716 const Location out = locations->Out();
2717 const Location second = locations->InAt(1);
2718 Location src;
2719
2720 if (condition->IsIntConstant()) {
2721 if (condition->AsIntConstant()->IsFalse()) {
2722 src = first;
2723 } else {
2724 src = second;
2725 }
2726
2727 codegen_->MoveLocation(out, src, type);
2728 return;
2729 }
2730
2731 if (!Primitive::IsFloatingPointType(type) &&
2732 CanGenerateTest(condition, codegen_->GetAssembler())) {
2733 bool invert = false;
2734
2735 if (out.Equals(second)) {
2736 src = first;
2737 invert = true;
2738 } else if (out.Equals(first)) {
2739 src = second;
2740 } else if (second.IsConstant()) {
2741 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2742 src = second;
2743 } else if (first.IsConstant()) {
2744 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2745 src = first;
2746 invert = true;
2747 } else {
2748 src = second;
2749 }
2750
2751 if (CanGenerateConditionalMove(out, src)) {
2752 if (!out.Equals(first) && !out.Equals(second)) {
2753 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2754 }
2755
2756 const Condition cond = GenerateTest(condition, locations->InAt(2), invert, codegen_);
2757
2758 if (out.IsRegister()) {
2759 ShifterOperand operand;
2760
2761 if (src.IsConstant()) {
2762 operand = ShifterOperand(CodeGenerator::GetInt32ValueOf(src.GetConstant()));
2763 } else {
2764 DCHECK(src.IsRegister());
2765 operand = ShifterOperand(src.AsRegister<Register>());
2766 }
2767
2768 __ it(cond);
2769 __ mov(out.AsRegister<Register>(), operand, cond);
2770 } else {
2771 DCHECK(out.IsRegisterPair());
2772
2773 ShifterOperand operand_high;
2774 ShifterOperand operand_low;
2775
2776 if (src.IsConstant()) {
2777 const int64_t value = src.GetConstant()->AsLongConstant()->GetValue();
2778
2779 operand_high = ShifterOperand(High32Bits(value));
2780 operand_low = ShifterOperand(Low32Bits(value));
2781 } else {
2782 DCHECK(src.IsRegisterPair());
2783 operand_high = ShifterOperand(src.AsRegisterPairHigh<Register>());
2784 operand_low = ShifterOperand(src.AsRegisterPairLow<Register>());
2785 }
2786
2787 __ it(cond);
2788 __ mov(out.AsRegisterPairLow<Register>(), operand_low, cond);
2789 __ it(cond);
2790 __ mov(out.AsRegisterPairHigh<Register>(), operand_high, cond);
2791 }
2792
2793 return;
2794 }
2795 }
2796
2797 Label* false_target = nullptr;
2798 Label* true_target = nullptr;
2799 Label select_end;
2800 Label* target = codegen_->GetFinalLabel(select, &select_end);
2801
2802 if (out.Equals(second)) {
2803 true_target = target;
2804 src = first;
2805 } else {
2806 false_target = target;
2807 src = second;
2808
2809 if (!out.Equals(first)) {
2810 codegen_->MoveLocation(out, first, type);
2811 }
2812 }
2813
2814 GenerateTestAndBranch(select, 2, true_target, false_target);
2815 codegen_->MoveLocation(out, src, type);
2816
2817 if (select_end.IsLinked()) {
2818 __ Bind(&select_end);
2819 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002820}
2821
David Srbecky0cf44932015-12-09 14:09:59 +00002822void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2823 new (GetGraph()->GetArena()) LocationSummary(info);
2824}
2825
David Srbeckyd28f4a02016-03-14 17:14:24 +00002826void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2827 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002828}
2829
2830void CodeGeneratorARM::GenerateNop() {
2831 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002832}
2833
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002834void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002835 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002836 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002837 // Handle the long/FP comparisons made in instruction simplification.
2838 switch (cond->InputAt(0)->GetType()) {
2839 case Primitive::kPrimLong:
2840 locations->SetInAt(0, Location::RequiresRegister());
2841 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002842 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002843 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2844 }
2845 break;
2846
2847 case Primitive::kPrimFloat:
2848 case Primitive::kPrimDouble:
2849 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002850 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002851 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002852 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2853 }
2854 break;
2855
2856 default:
2857 locations->SetInAt(0, Location::RequiresRegister());
2858 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002859 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002860 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2861 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002862 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002863}
2864
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002865void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002866 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002867 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002868 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002869
2870 LocationSummary* locations = cond->GetLocations();
2871 Location left = locations->InAt(0);
2872 Location right = locations->InAt(1);
2873 Register out = locations->Out().AsRegister<Register>();
2874 Label true_label, false_label;
2875
2876 switch (cond->InputAt(0)->GetType()) {
2877 default: {
2878 // Integer case.
2879 if (right.IsRegister()) {
2880 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>()));
2881 } else {
2882 DCHECK(right.IsConstant());
Vladimir Markoac6ac102015-12-17 12:14:00 +00002883 __ CmpConstant(left.AsRegister<Register>(),
2884 CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002885 }
Aart Bike9f37602015-10-09 11:15:55 -07002886 __ it(ARMCondition(cond->GetCondition()), kItElse);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002887 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1),
Aart Bike9f37602015-10-09 11:15:55 -07002888 ARMCondition(cond->GetCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002889 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0),
Aart Bike9f37602015-10-09 11:15:55 -07002890 ARMCondition(cond->GetOppositeCondition()));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002891 return;
2892 }
2893 case Primitive::kPrimLong:
2894 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2895 break;
2896 case Primitive::kPrimFloat:
Roland Levillain4fa13f62015-07-06 18:11:54 +01002897 case Primitive::kPrimDouble:
Donghui Bai426b49c2016-11-08 14:55:38 +08002898 GenerateVcmp(cond, codegen_);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002899 GenerateFPJumps(cond, &true_label, &false_label);
2900 break;
2901 }
2902
2903 // Convert the jumps into the result.
2904 Label done_label;
Anton Kirilov6f644202017-02-27 18:29:45 +00002905 Label* final_label = codegen_->GetFinalLabel(cond, &done_label);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002906
2907 // False case: result = 0.
2908 __ Bind(&false_label);
2909 __ LoadImmediate(out, 0);
Anton Kirilov6f644202017-02-27 18:29:45 +00002910 __ b(final_label);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002911
2912 // True case: result = 1.
2913 __ Bind(&true_label);
2914 __ LoadImmediate(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00002915
2916 if (done_label.IsLinked()) {
2917 __ Bind(&done_label);
2918 }
Dave Allison20dfc792014-06-16 20:44:29 -07002919}
2920
2921void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002922 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002923}
2924
2925void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002926 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002927}
2928
2929void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002930 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002931}
2932
2933void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002934 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002935}
2936
2937void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002938 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002939}
2940
2941void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002942 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002943}
2944
2945void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002946 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002947}
2948
2949void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002950 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002951}
2952
2953void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002954 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002955}
2956
2957void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002958 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002959}
2960
2961void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002962 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002963}
2964
2965void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002966 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002967}
2968
Aart Bike9f37602015-10-09 11:15:55 -07002969void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002970 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002971}
2972
2973void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002974 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002975}
2976
2977void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002978 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002979}
2980
2981void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002982 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002983}
2984
2985void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002986 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002987}
2988
2989void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002990 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002991}
2992
2993void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002994 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002995}
2996
2997void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002998 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07002999}
3000
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003001void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003002 LocationSummary* locations =
3003 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003004 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003005}
3006
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003007void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01003008 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003009}
3010
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003011void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
3012 LocationSummary* locations =
3013 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3014 locations->SetOut(Location::ConstantLocation(constant));
3015}
3016
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003017void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003018 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003019}
3020
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003021void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003022 LocationSummary* locations =
3023 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003024 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003025}
3026
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003027void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003028 // Will be generated at use site.
3029}
3030
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003031void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
3032 LocationSummary* locations =
3033 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3034 locations->SetOut(Location::ConstantLocation(constant));
3035}
3036
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003037void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003038 // Will be generated at use site.
3039}
3040
3041void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
3042 LocationSummary* locations =
3043 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3044 locations->SetOut(Location::ConstantLocation(constant));
3045}
3046
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003047void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003048 // Will be generated at use site.
3049}
3050
Calin Juravle27df7582015-04-17 19:12:31 +01003051void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3052 memory_barrier->SetLocations(nullptr);
3053}
3054
3055void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00003056 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01003057}
3058
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003059void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003060 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003061}
3062
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003063void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003064 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003065}
3066
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003067void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003068 LocationSummary* locations =
3069 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003070 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003071}
3072
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003073void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003074 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003075}
3076
Calin Juravle175dc732015-08-25 15:42:32 +01003077void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3078 // The trampoline uses the same calling convention as dex calling conventions,
3079 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3080 // the method_idx.
3081 HandleInvoke(invoke);
3082}
3083
3084void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3085 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3086}
3087
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003088void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003089 // Explicit clinit checks triggered by static invokes must have been pruned by
3090 // art::PrepareForRegisterAllocation.
3091 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003092
Vladimir Marko68c981f2016-08-26 13:13:33 +01003093 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003094 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00003095 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3096 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3097 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003098 return;
3099 }
3100
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003101 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00003102
3103 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3104 if (invoke->HasPcRelativeDexCache()) {
3105 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3106 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003107}
3108
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003109static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
3110 if (invoke->GetLocations()->Intrinsified()) {
3111 IntrinsicCodeGeneratorARM intrinsic(codegen);
3112 intrinsic.Dispatch(invoke);
3113 return true;
3114 }
3115 return false;
3116}
3117
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003118void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003119 // Explicit clinit checks triggered by static invokes must have been pruned by
3120 // art::PrepareForRegisterAllocation.
3121 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003122
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003123 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3124 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003125 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003126
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003127 LocationSummary* locations = invoke->GetLocations();
3128 codegen_->GenerateStaticOrDirectCall(
3129 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003130 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003131}
3132
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003133void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003134 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003135 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003136}
3137
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003138void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01003139 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003140 if (intrinsic.TryDispatch(invoke)) {
3141 return;
3142 }
3143
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003144 HandleInvoke(invoke);
3145}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003146
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003147void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003148 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3149 return;
3150 }
3151
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003152 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003153 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003154 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003155}
3156
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003157void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3158 HandleInvoke(invoke);
3159 // Add the hidden argument.
3160 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
3161}
3162
3163void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3164 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00003165 LocationSummary* locations = invoke->GetLocations();
3166 Register temp = locations->GetTemp(0).AsRegister<Register>();
3167 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003168 Location receiver = locations->InAt(0);
3169 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3170
Roland Levillain3b359c72015-11-17 19:35:12 +00003171 // Set the hidden argument. This is safe to do this here, as R12
3172 // won't be modified thereafter, before the `blx` (call) instruction.
3173 DCHECK_EQ(R12, hidden_reg);
3174 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003175
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003176 if (receiver.IsStackSlot()) {
3177 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00003178 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003179 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3180 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00003181 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00003182 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003183 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003184 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00003185 // Instead of simply (possibly) unpoisoning `temp` here, we should
3186 // emit a read barrier for the previous class reference load.
3187 // However this is not required in practice, as this is an
3188 // intermediate/temporary reference and because the current
3189 // concurrent copying collector keeps the from-space memory
3190 // intact/accessible until the end of the marking phase (the
3191 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003192 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003193 __ LoadFromOffset(kLoadWord, temp, temp,
3194 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3195 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003196 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003197 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003198 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00003199 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07003200 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003201 // LR = temp->GetEntryPoint();
3202 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
3203 // LR();
3204 __ blx(LR);
3205 DCHECK(!codegen_->IsLeafMethod());
3206 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3207}
3208
Orion Hodsonac141392017-01-13 11:53:47 +00003209void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3210 HandleInvoke(invoke);
3211}
3212
3213void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3214 codegen_->GenerateInvokePolymorphicCall(invoke);
3215}
3216
Roland Levillain88cb1752014-10-20 16:36:47 +01003217void LocationsBuilderARM::VisitNeg(HNeg* neg) {
3218 LocationSummary* locations =
3219 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3220 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003221 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01003222 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003223 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3224 break;
3225 }
3226 case Primitive::kPrimLong: {
3227 locations->SetInAt(0, Location::RequiresRegister());
3228 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003229 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003230 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003231
Roland Levillain88cb1752014-10-20 16:36:47 +01003232 case Primitive::kPrimFloat:
3233 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003234 locations->SetInAt(0, Location::RequiresFpuRegister());
3235 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003236 break;
3237
3238 default:
3239 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3240 }
3241}
3242
3243void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
3244 LocationSummary* locations = neg->GetLocations();
3245 Location out = locations->Out();
3246 Location in = locations->InAt(0);
3247 switch (neg->GetResultType()) {
3248 case Primitive::kPrimInt:
3249 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003250 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01003251 break;
3252
3253 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003254 DCHECK(in.IsRegisterPair());
3255 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3256 __ rsbs(out.AsRegisterPairLow<Register>(),
3257 in.AsRegisterPairLow<Register>(),
3258 ShifterOperand(0));
3259 // We cannot emit an RSC (Reverse Subtract with Carry)
3260 // instruction here, as it does not exist in the Thumb-2
3261 // instruction set. We use the following approach
3262 // using SBC and SUB instead.
3263 //
3264 // out.hi = -C
3265 __ sbc(out.AsRegisterPairHigh<Register>(),
3266 out.AsRegisterPairHigh<Register>(),
3267 ShifterOperand(out.AsRegisterPairHigh<Register>()));
3268 // out.hi = out.hi - in.hi
3269 __ sub(out.AsRegisterPairHigh<Register>(),
3270 out.AsRegisterPairHigh<Register>(),
3271 ShifterOperand(in.AsRegisterPairHigh<Register>()));
3272 break;
3273
Roland Levillain88cb1752014-10-20 16:36:47 +01003274 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003275 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003276 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00003277 break;
3278
Roland Levillain88cb1752014-10-20 16:36:47 +01003279 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003280 DCHECK(in.IsFpuRegisterPair());
3281 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3282 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01003283 break;
3284
3285 default:
3286 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3287 }
3288}
3289
Roland Levillaindff1f282014-11-05 14:15:05 +00003290void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003291 Primitive::Type result_type = conversion->GetResultType();
3292 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003293 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00003294
Roland Levillain5b3ee562015-04-14 16:02:41 +01003295 // The float-to-long, double-to-long and long-to-float type conversions
3296 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003297 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01003298 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3299 && result_type == Primitive::kPrimLong)
3300 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003301 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00003302 : LocationSummary::kNoCall;
3303 LocationSummary* locations =
3304 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3305
David Brazdilb2bd1c52015-03-25 11:17:37 +00003306 // The Java language does not allow treating boolean as an integral type but
3307 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00003308
Roland Levillaindff1f282014-11-05 14:15:05 +00003309 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003310 case Primitive::kPrimByte:
3311 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003312 case Primitive::kPrimLong:
3313 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003314 case Primitive::kPrimBoolean:
3315 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003316 case Primitive::kPrimShort:
3317 case Primitive::kPrimInt:
3318 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003319 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003320 locations->SetInAt(0, Location::RequiresRegister());
3321 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3322 break;
3323
3324 default:
3325 LOG(FATAL) << "Unexpected type conversion from " << input_type
3326 << " to " << result_type;
3327 }
3328 break;
3329
Roland Levillain01a8d712014-11-14 16:27:39 +00003330 case Primitive::kPrimShort:
3331 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003332 case Primitive::kPrimLong:
3333 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003334 case Primitive::kPrimBoolean:
3335 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003336 case Primitive::kPrimByte:
3337 case Primitive::kPrimInt:
3338 case Primitive::kPrimChar:
3339 // Processing a Dex `int-to-short' instruction.
3340 locations->SetInAt(0, Location::RequiresRegister());
3341 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3342 break;
3343
3344 default:
3345 LOG(FATAL) << "Unexpected type conversion from " << input_type
3346 << " to " << result_type;
3347 }
3348 break;
3349
Roland Levillain946e1432014-11-11 17:35:19 +00003350 case Primitive::kPrimInt:
3351 switch (input_type) {
3352 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003353 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003354 locations->SetInAt(0, Location::Any());
3355 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3356 break;
3357
3358 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00003359 // Processing a Dex `float-to-int' instruction.
3360 locations->SetInAt(0, Location::RequiresFpuRegister());
3361 locations->SetOut(Location::RequiresRegister());
3362 locations->AddTemp(Location::RequiresFpuRegister());
3363 break;
3364
Roland Levillain946e1432014-11-11 17:35:19 +00003365 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003366 // Processing a Dex `double-to-int' instruction.
3367 locations->SetInAt(0, Location::RequiresFpuRegister());
3368 locations->SetOut(Location::RequiresRegister());
3369 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00003370 break;
3371
3372 default:
3373 LOG(FATAL) << "Unexpected type conversion from " << input_type
3374 << " to " << result_type;
3375 }
3376 break;
3377
Roland Levillaindff1f282014-11-05 14:15:05 +00003378 case Primitive::kPrimLong:
3379 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003380 case Primitive::kPrimBoolean:
3381 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003382 case Primitive::kPrimByte:
3383 case Primitive::kPrimShort:
3384 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003385 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003386 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003387 locations->SetInAt(0, Location::RequiresRegister());
3388 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3389 break;
3390
Roland Levillain624279f2014-12-04 11:54:28 +00003391 case Primitive::kPrimFloat: {
3392 // Processing a Dex `float-to-long' instruction.
3393 InvokeRuntimeCallingConvention calling_convention;
3394 locations->SetInAt(0, Location::FpuRegisterLocation(
3395 calling_convention.GetFpuRegisterAt(0)));
3396 locations->SetOut(Location::RegisterPairLocation(R0, R1));
3397 break;
3398 }
3399
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003400 case Primitive::kPrimDouble: {
3401 // Processing a Dex `double-to-long' instruction.
3402 InvokeRuntimeCallingConvention calling_convention;
3403 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3404 calling_convention.GetFpuRegisterAt(0),
3405 calling_convention.GetFpuRegisterAt(1)));
3406 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00003407 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003408 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003409
3410 default:
3411 LOG(FATAL) << "Unexpected type conversion from " << input_type
3412 << " to " << result_type;
3413 }
3414 break;
3415
Roland Levillain981e4542014-11-14 11:47:14 +00003416 case Primitive::kPrimChar:
3417 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003418 case Primitive::kPrimLong:
3419 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003420 case Primitive::kPrimBoolean:
3421 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003422 case Primitive::kPrimByte:
3423 case Primitive::kPrimShort:
3424 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003425 // Processing a Dex `int-to-char' instruction.
3426 locations->SetInAt(0, Location::RequiresRegister());
3427 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3428 break;
3429
3430 default:
3431 LOG(FATAL) << "Unexpected type conversion from " << input_type
3432 << " to " << result_type;
3433 }
3434 break;
3435
Roland Levillaindff1f282014-11-05 14:15:05 +00003436 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003437 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003438 case Primitive::kPrimBoolean:
3439 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003440 case Primitive::kPrimByte:
3441 case Primitive::kPrimShort:
3442 case Primitive::kPrimInt:
3443 case Primitive::kPrimChar:
3444 // Processing a Dex `int-to-float' instruction.
3445 locations->SetInAt(0, Location::RequiresRegister());
3446 locations->SetOut(Location::RequiresFpuRegister());
3447 break;
3448
Roland Levillain5b3ee562015-04-14 16:02:41 +01003449 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00003450 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01003451 InvokeRuntimeCallingConvention calling_convention;
3452 locations->SetInAt(0, Location::RegisterPairLocation(
3453 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3454 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00003455 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01003456 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003457
Roland Levillaincff13742014-11-17 14:32:17 +00003458 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003459 // Processing a Dex `double-to-float' instruction.
3460 locations->SetInAt(0, Location::RequiresFpuRegister());
3461 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003462 break;
3463
3464 default:
3465 LOG(FATAL) << "Unexpected type conversion from " << input_type
3466 << " to " << result_type;
3467 };
3468 break;
3469
Roland Levillaindff1f282014-11-05 14:15:05 +00003470 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003471 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003472 case Primitive::kPrimBoolean:
3473 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003474 case Primitive::kPrimByte:
3475 case Primitive::kPrimShort:
3476 case Primitive::kPrimInt:
3477 case Primitive::kPrimChar:
3478 // Processing a Dex `int-to-double' instruction.
3479 locations->SetInAt(0, Location::RequiresRegister());
3480 locations->SetOut(Location::RequiresFpuRegister());
3481 break;
3482
3483 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003484 // Processing a Dex `long-to-double' instruction.
3485 locations->SetInAt(0, Location::RequiresRegister());
3486 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01003487 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00003488 locations->AddTemp(Location::RequiresFpuRegister());
3489 break;
3490
Roland Levillaincff13742014-11-17 14:32:17 +00003491 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003492 // Processing a Dex `float-to-double' instruction.
3493 locations->SetInAt(0, Location::RequiresFpuRegister());
3494 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003495 break;
3496
3497 default:
3498 LOG(FATAL) << "Unexpected type conversion from " << input_type
3499 << " to " << result_type;
3500 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003501 break;
3502
3503 default:
3504 LOG(FATAL) << "Unexpected type conversion from " << input_type
3505 << " to " << result_type;
3506 }
3507}
3508
3509void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
3510 LocationSummary* locations = conversion->GetLocations();
3511 Location out = locations->Out();
3512 Location in = locations->InAt(0);
3513 Primitive::Type result_type = conversion->GetResultType();
3514 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003515 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00003516 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003517 case Primitive::kPrimByte:
3518 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003519 case Primitive::kPrimLong:
3520 // Type conversion from long to byte is a result of code transformations.
3521 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
3522 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003523 case Primitive::kPrimBoolean:
3524 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003525 case Primitive::kPrimShort:
3526 case Primitive::kPrimInt:
3527 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003528 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003529 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00003530 break;
3531
3532 default:
3533 LOG(FATAL) << "Unexpected type conversion from " << input_type
3534 << " to " << result_type;
3535 }
3536 break;
3537
Roland Levillain01a8d712014-11-14 16:27:39 +00003538 case Primitive::kPrimShort:
3539 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003540 case Primitive::kPrimLong:
3541 // Type conversion from long to short is a result of code transformations.
3542 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3543 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003544 case Primitive::kPrimBoolean:
3545 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003546 case Primitive::kPrimByte:
3547 case Primitive::kPrimInt:
3548 case Primitive::kPrimChar:
3549 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003550 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00003551 break;
3552
3553 default:
3554 LOG(FATAL) << "Unexpected type conversion from " << input_type
3555 << " to " << result_type;
3556 }
3557 break;
3558
Roland Levillain946e1432014-11-11 17:35:19 +00003559 case Primitive::kPrimInt:
3560 switch (input_type) {
3561 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003562 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003563 DCHECK(out.IsRegister());
3564 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003565 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003566 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003567 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00003568 } else {
3569 DCHECK(in.IsConstant());
3570 DCHECK(in.GetConstant()->IsLongConstant());
3571 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003572 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00003573 }
3574 break;
3575
Roland Levillain3f8f9362014-12-02 17:45:01 +00003576 case Primitive::kPrimFloat: {
3577 // Processing a Dex `float-to-int' instruction.
3578 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003579 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00003580 __ vmovrs(out.AsRegister<Register>(), temp);
3581 break;
3582 }
3583
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003584 case Primitive::kPrimDouble: {
3585 // Processing a Dex `double-to-int' instruction.
3586 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003587 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003588 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00003589 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003590 }
Roland Levillain946e1432014-11-11 17:35:19 +00003591
3592 default:
3593 LOG(FATAL) << "Unexpected type conversion from " << input_type
3594 << " to " << result_type;
3595 }
3596 break;
3597
Roland Levillaindff1f282014-11-05 14:15:05 +00003598 case Primitive::kPrimLong:
3599 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003600 case Primitive::kPrimBoolean:
3601 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003602 case Primitive::kPrimByte:
3603 case Primitive::kPrimShort:
3604 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003605 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003606 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003607 DCHECK(out.IsRegisterPair());
3608 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003609 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00003610 // Sign extension.
3611 __ Asr(out.AsRegisterPairHigh<Register>(),
3612 out.AsRegisterPairLow<Register>(),
3613 31);
3614 break;
3615
3616 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00003617 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003618 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003619 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003620 break;
3621
Roland Levillaindff1f282014-11-05 14:15:05 +00003622 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003623 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003624 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003625 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003626 break;
3627
3628 default:
3629 LOG(FATAL) << "Unexpected type conversion from " << input_type
3630 << " to " << result_type;
3631 }
3632 break;
3633
Roland Levillain981e4542014-11-14 11:47:14 +00003634 case Primitive::kPrimChar:
3635 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003636 case Primitive::kPrimLong:
3637 // Type conversion from long to char is a result of code transformations.
3638 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3639 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003640 case Primitive::kPrimBoolean:
3641 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003642 case Primitive::kPrimByte:
3643 case Primitive::kPrimShort:
3644 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003645 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003646 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00003647 break;
3648
3649 default:
3650 LOG(FATAL) << "Unexpected type conversion from " << input_type
3651 << " to " << result_type;
3652 }
3653 break;
3654
Roland Levillaindff1f282014-11-05 14:15:05 +00003655 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003656 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003657 case Primitive::kPrimBoolean:
3658 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003659 case Primitive::kPrimByte:
3660 case Primitive::kPrimShort:
3661 case Primitive::kPrimInt:
3662 case Primitive::kPrimChar: {
3663 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003664 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
3665 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003666 break;
3667 }
3668
Roland Levillain5b3ee562015-04-14 16:02:41 +01003669 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003670 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003671 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003672 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00003673 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003674
Roland Levillaincff13742014-11-17 14:32:17 +00003675 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003676 // Processing a Dex `double-to-float' instruction.
3677 __ vcvtsd(out.AsFpuRegister<SRegister>(),
3678 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00003679 break;
3680
3681 default:
3682 LOG(FATAL) << "Unexpected type conversion from " << input_type
3683 << " to " << result_type;
3684 };
3685 break;
3686
Roland Levillaindff1f282014-11-05 14:15:05 +00003687 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003688 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003689 case Primitive::kPrimBoolean:
3690 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003691 case Primitive::kPrimByte:
3692 case Primitive::kPrimShort:
3693 case Primitive::kPrimInt:
3694 case Primitive::kPrimChar: {
3695 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003696 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003697 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3698 out.AsFpuRegisterPairLow<SRegister>());
3699 break;
3700 }
3701
Roland Levillain647b9ed2014-11-27 12:06:00 +00003702 case Primitive::kPrimLong: {
3703 // Processing a Dex `long-to-double' instruction.
3704 Register low = in.AsRegisterPairLow<Register>();
3705 Register high = in.AsRegisterPairHigh<Register>();
3706 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
3707 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003708 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00003709 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003710 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
3711 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003712
Roland Levillain682393c2015-04-14 15:57:52 +01003713 // temp_d = int-to-double(high)
3714 __ vmovsr(temp_s, high);
3715 __ vcvtdi(temp_d, temp_s);
3716 // constant_d = k2Pow32EncodingForDouble
3717 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
3718 // out_d = unsigned-to-double(low)
3719 __ vmovsr(out_s, low);
3720 __ vcvtdu(out_d, out_s);
3721 // out_d += temp_d * constant_d
3722 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003723 break;
3724 }
3725
Roland Levillaincff13742014-11-17 14:32:17 +00003726 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003727 // Processing a Dex `float-to-double' instruction.
3728 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3729 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003730 break;
3731
3732 default:
3733 LOG(FATAL) << "Unexpected type conversion from " << input_type
3734 << " to " << result_type;
3735 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003736 break;
3737
3738 default:
3739 LOG(FATAL) << "Unexpected type conversion from " << input_type
3740 << " to " << result_type;
3741 }
3742}
3743
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003744void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003745 LocationSummary* locations =
3746 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003747 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003748 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003749 locations->SetInAt(0, Location::RequiresRegister());
3750 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003751 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3752 break;
3753 }
3754
3755 case Primitive::kPrimLong: {
3756 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003757 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003758 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003759 break;
3760 }
3761
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003762 case Primitive::kPrimFloat:
3763 case Primitive::kPrimDouble: {
3764 locations->SetInAt(0, Location::RequiresFpuRegister());
3765 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003766 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003767 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003768 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003769
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003770 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003771 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003772 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003773}
3774
3775void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
3776 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003777 Location out = locations->Out();
3778 Location first = locations->InAt(0);
3779 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003780 switch (add->GetResultType()) {
3781 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003782 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003783 __ add(out.AsRegister<Register>(),
3784 first.AsRegister<Register>(),
3785 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003786 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003787 __ AddConstant(out.AsRegister<Register>(),
3788 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003789 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003790 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003791 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003792
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003793 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003794 if (second.IsConstant()) {
3795 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3796 GenerateAddLongConst(out, first, value);
3797 } else {
3798 DCHECK(second.IsRegisterPair());
3799 __ adds(out.AsRegisterPairLow<Register>(),
3800 first.AsRegisterPairLow<Register>(),
3801 ShifterOperand(second.AsRegisterPairLow<Register>()));
3802 __ adc(out.AsRegisterPairHigh<Register>(),
3803 first.AsRegisterPairHigh<Register>(),
3804 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3805 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003806 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003807 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003808
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003809 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00003810 __ vadds(out.AsFpuRegister<SRegister>(),
3811 first.AsFpuRegister<SRegister>(),
3812 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003813 break;
3814
3815 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003816 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3817 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3818 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003819 break;
3820
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003821 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003822 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003823 }
3824}
3825
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003826void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003827 LocationSummary* locations =
3828 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003829 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003830 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003831 locations->SetInAt(0, Location::RequiresRegister());
3832 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003833 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3834 break;
3835 }
3836
3837 case Primitive::kPrimLong: {
3838 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003839 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003840 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003841 break;
3842 }
Calin Juravle11351682014-10-23 15:38:15 +01003843 case Primitive::kPrimFloat:
3844 case Primitive::kPrimDouble: {
3845 locations->SetInAt(0, Location::RequiresFpuRegister());
3846 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003847 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003848 break;
Calin Juravle11351682014-10-23 15:38:15 +01003849 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003850 default:
Calin Juravle11351682014-10-23 15:38:15 +01003851 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003852 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003853}
3854
3855void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3856 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003857 Location out = locations->Out();
3858 Location first = locations->InAt(0);
3859 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003860 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003861 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003862 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003863 __ sub(out.AsRegister<Register>(),
3864 first.AsRegister<Register>(),
3865 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003866 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003867 __ AddConstant(out.AsRegister<Register>(),
3868 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003869 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003870 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003871 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003872 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003873
Calin Juravle11351682014-10-23 15:38:15 +01003874 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003875 if (second.IsConstant()) {
3876 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3877 GenerateAddLongConst(out, first, -value);
3878 } else {
3879 DCHECK(second.IsRegisterPair());
3880 __ subs(out.AsRegisterPairLow<Register>(),
3881 first.AsRegisterPairLow<Register>(),
3882 ShifterOperand(second.AsRegisterPairLow<Register>()));
3883 __ sbc(out.AsRegisterPairHigh<Register>(),
3884 first.AsRegisterPairHigh<Register>(),
3885 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3886 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003887 break;
Calin Juravle11351682014-10-23 15:38:15 +01003888 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003889
Calin Juravle11351682014-10-23 15:38:15 +01003890 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003891 __ vsubs(out.AsFpuRegister<SRegister>(),
3892 first.AsFpuRegister<SRegister>(),
3893 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003894 break;
Calin Juravle11351682014-10-23 15:38:15 +01003895 }
3896
3897 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003898 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3899 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3900 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003901 break;
3902 }
3903
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003904
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003905 default:
Calin Juravle11351682014-10-23 15:38:15 +01003906 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003907 }
3908}
3909
Calin Juravle34bacdf2014-10-07 20:23:36 +01003910void LocationsBuilderARM::VisitMul(HMul* mul) {
3911 LocationSummary* locations =
3912 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3913 switch (mul->GetResultType()) {
3914 case Primitive::kPrimInt:
3915 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003916 locations->SetInAt(0, Location::RequiresRegister());
3917 locations->SetInAt(1, Location::RequiresRegister());
3918 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003919 break;
3920 }
3921
Calin Juravleb5bfa962014-10-21 18:02:24 +01003922 case Primitive::kPrimFloat:
3923 case Primitive::kPrimDouble: {
3924 locations->SetInAt(0, Location::RequiresFpuRegister());
3925 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003926 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003927 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003928 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003929
3930 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003931 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003932 }
3933}
3934
3935void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3936 LocationSummary* locations = mul->GetLocations();
3937 Location out = locations->Out();
3938 Location first = locations->InAt(0);
3939 Location second = locations->InAt(1);
3940 switch (mul->GetResultType()) {
3941 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00003942 __ mul(out.AsRegister<Register>(),
3943 first.AsRegister<Register>(),
3944 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003945 break;
3946 }
3947 case Primitive::kPrimLong: {
3948 Register out_hi = out.AsRegisterPairHigh<Register>();
3949 Register out_lo = out.AsRegisterPairLow<Register>();
3950 Register in1_hi = first.AsRegisterPairHigh<Register>();
3951 Register in1_lo = first.AsRegisterPairLow<Register>();
3952 Register in2_hi = second.AsRegisterPairHigh<Register>();
3953 Register in2_lo = second.AsRegisterPairLow<Register>();
3954
3955 // Extra checks to protect caused by the existence of R1_R2.
3956 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
3957 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
3958 DCHECK_NE(out_hi, in1_lo);
3959 DCHECK_NE(out_hi, in2_lo);
3960
3961 // input: in1 - 64 bits, in2 - 64 bits
3962 // output: out
3963 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3964 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3965 // parts: out.lo = (in1.lo * in2.lo)[31:0]
3966
3967 // IP <- in1.lo * in2.hi
3968 __ mul(IP, in1_lo, in2_hi);
3969 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3970 __ mla(out_hi, in1_hi, in2_lo, IP);
3971 // out.lo <- (in1.lo * in2.lo)[31:0];
3972 __ umull(out_lo, IP, in1_lo, in2_lo);
3973 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3974 __ add(out_hi, out_hi, ShifterOperand(IP));
3975 break;
3976 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003977
3978 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003979 __ vmuls(out.AsFpuRegister<SRegister>(),
3980 first.AsFpuRegister<SRegister>(),
3981 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003982 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003983 }
3984
3985 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003986 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3987 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3988 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01003989 break;
3990 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003991
3992 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003993 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003994 }
3995}
3996
Zheng Xuc6667102015-05-15 16:08:45 +08003997void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3998 DCHECK(instruction->IsDiv() || instruction->IsRem());
3999 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4000
4001 LocationSummary* locations = instruction->GetLocations();
4002 Location second = locations->InAt(1);
4003 DCHECK(second.IsConstant());
4004
4005 Register out = locations->Out().AsRegister<Register>();
4006 Register dividend = locations->InAt(0).AsRegister<Register>();
4007 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4008 DCHECK(imm == 1 || imm == -1);
4009
4010 if (instruction->IsRem()) {
4011 __ LoadImmediate(out, 0);
4012 } else {
4013 if (imm == 1) {
4014 __ Mov(out, dividend);
4015 } else {
4016 __ rsb(out, dividend, ShifterOperand(0));
4017 }
4018 }
4019}
4020
4021void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4022 DCHECK(instruction->IsDiv() || instruction->IsRem());
4023 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4024
4025 LocationSummary* locations = instruction->GetLocations();
4026 Location second = locations->InAt(1);
4027 DCHECK(second.IsConstant());
4028
4029 Register out = locations->Out().AsRegister<Register>();
4030 Register dividend = locations->InAt(0).AsRegister<Register>();
4031 Register temp = locations->GetTemp(0).AsRegister<Register>();
4032 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004033 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08004034 int ctz_imm = CTZ(abs_imm);
4035
4036 if (ctz_imm == 1) {
4037 __ Lsr(temp, dividend, 32 - ctz_imm);
4038 } else {
4039 __ Asr(temp, dividend, 31);
4040 __ Lsr(temp, temp, 32 - ctz_imm);
4041 }
4042 __ add(out, temp, ShifterOperand(dividend));
4043
4044 if (instruction->IsDiv()) {
4045 __ Asr(out, out, ctz_imm);
4046 if (imm < 0) {
4047 __ rsb(out, out, ShifterOperand(0));
4048 }
4049 } else {
4050 __ ubfx(out, out, 0, ctz_imm);
4051 __ sub(out, out, ShifterOperand(temp));
4052 }
4053}
4054
4055void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4056 DCHECK(instruction->IsDiv() || instruction->IsRem());
4057 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4058
4059 LocationSummary* locations = instruction->GetLocations();
4060 Location second = locations->InAt(1);
4061 DCHECK(second.IsConstant());
4062
4063 Register out = locations->Out().AsRegister<Register>();
4064 Register dividend = locations->InAt(0).AsRegister<Register>();
4065 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4066 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4067 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4068
4069 int64_t magic;
4070 int shift;
4071 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4072
4073 __ LoadImmediate(temp1, magic);
4074 __ smull(temp2, temp1, dividend, temp1);
4075
4076 if (imm > 0 && magic < 0) {
4077 __ add(temp1, temp1, ShifterOperand(dividend));
4078 } else if (imm < 0 && magic > 0) {
4079 __ sub(temp1, temp1, ShifterOperand(dividend));
4080 }
4081
4082 if (shift != 0) {
4083 __ Asr(temp1, temp1, shift);
4084 }
4085
4086 if (instruction->IsDiv()) {
4087 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
4088 } else {
4089 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
4090 // TODO: Strength reduction for mls.
4091 __ LoadImmediate(temp2, imm);
4092 __ mls(out, temp1, temp2, dividend);
4093 }
4094}
4095
4096void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
4097 DCHECK(instruction->IsDiv() || instruction->IsRem());
4098 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4099
4100 LocationSummary* locations = instruction->GetLocations();
4101 Location second = locations->InAt(1);
4102 DCHECK(second.IsConstant());
4103
4104 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4105 if (imm == 0) {
4106 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4107 } else if (imm == 1 || imm == -1) {
4108 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004109 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004110 DivRemByPowerOfTwo(instruction);
4111 } else {
4112 DCHECK(imm <= -2 || imm >= 2);
4113 GenerateDivRemWithAnyConstant(instruction);
4114 }
4115}
4116
Calin Juravle7c4954d2014-10-28 16:57:40 +00004117void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004118 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4119 if (div->GetResultType() == Primitive::kPrimLong) {
4120 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004121 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004122 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4123 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004124 } else if (div->GetResultType() == Primitive::kPrimInt &&
4125 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4126 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004127 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004128 }
4129
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004130 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4131
Calin Juravle7c4954d2014-10-28 16:57:40 +00004132 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004133 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004134 if (div->InputAt(1)->IsConstant()) {
4135 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004136 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004137 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004138 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
4139 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004140 // No temp register required.
4141 } else {
4142 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004143 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004144 locations->AddTemp(Location::RequiresRegister());
4145 }
4146 }
4147 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004148 locations->SetInAt(0, Location::RequiresRegister());
4149 locations->SetInAt(1, Location::RequiresRegister());
4150 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4151 } else {
4152 InvokeRuntimeCallingConvention calling_convention;
4153 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4154 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004155 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004156 // we only need the former.
4157 locations->SetOut(Location::RegisterLocation(R0));
4158 }
Calin Juravled0d48522014-11-04 16:40:20 +00004159 break;
4160 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004161 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004162 InvokeRuntimeCallingConvention calling_convention;
4163 locations->SetInAt(0, Location::RegisterPairLocation(
4164 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4165 locations->SetInAt(1, Location::RegisterPairLocation(
4166 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004167 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004168 break;
4169 }
4170 case Primitive::kPrimFloat:
4171 case Primitive::kPrimDouble: {
4172 locations->SetInAt(0, Location::RequiresFpuRegister());
4173 locations->SetInAt(1, Location::RequiresFpuRegister());
4174 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4175 break;
4176 }
4177
4178 default:
4179 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4180 }
4181}
4182
4183void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
4184 LocationSummary* locations = div->GetLocations();
4185 Location out = locations->Out();
4186 Location first = locations->InAt(0);
4187 Location second = locations->InAt(1);
4188
4189 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004190 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004191 if (second.IsConstant()) {
4192 GenerateDivRemConstantIntegral(div);
4193 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004194 __ sdiv(out.AsRegister<Register>(),
4195 first.AsRegister<Register>(),
4196 second.AsRegister<Register>());
4197 } else {
4198 InvokeRuntimeCallingConvention calling_convention;
4199 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4200 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4201 DCHECK_EQ(R0, out.AsRegister<Register>());
4202
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004203 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004204 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004205 }
Calin Juravled0d48522014-11-04 16:40:20 +00004206 break;
4207 }
4208
Calin Juravle7c4954d2014-10-28 16:57:40 +00004209 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004210 InvokeRuntimeCallingConvention calling_convention;
4211 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4212 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4213 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4214 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4215 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004216 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004217
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004218 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004219 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00004220 break;
4221 }
4222
4223 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004224 __ vdivs(out.AsFpuRegister<SRegister>(),
4225 first.AsFpuRegister<SRegister>(),
4226 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004227 break;
4228 }
4229
4230 case Primitive::kPrimDouble: {
4231 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4232 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4233 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
4234 break;
4235 }
4236
4237 default:
4238 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4239 }
4240}
4241
Calin Juravlebacfec32014-11-14 15:54:36 +00004242void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004243 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004244
4245 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004246 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004247 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4248 // sdiv will be replaced by other instruction sequence.
4249 call_kind = LocationSummary::kNoCall;
4250 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4251 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004252 // Have hardware divide instruction for int, do it with three instructions.
4253 call_kind = LocationSummary::kNoCall;
4254 }
4255
Calin Juravlebacfec32014-11-14 15:54:36 +00004256 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4257
Calin Juravled2ec87d2014-12-08 14:24:46 +00004258 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004259 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004260 if (rem->InputAt(1)->IsConstant()) {
4261 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004262 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004263 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004264 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
4265 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004266 // No temp register required.
4267 } else {
4268 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004269 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004270 locations->AddTemp(Location::RequiresRegister());
4271 }
4272 }
4273 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004274 locations->SetInAt(0, Location::RequiresRegister());
4275 locations->SetInAt(1, Location::RequiresRegister());
4276 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4277 locations->AddTemp(Location::RequiresRegister());
4278 } else {
4279 InvokeRuntimeCallingConvention calling_convention;
4280 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4281 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004282 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004283 // we only need the latter.
4284 locations->SetOut(Location::RegisterLocation(R1));
4285 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004286 break;
4287 }
4288 case Primitive::kPrimLong: {
4289 InvokeRuntimeCallingConvention calling_convention;
4290 locations->SetInAt(0, Location::RegisterPairLocation(
4291 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4292 locations->SetInAt(1, Location::RegisterPairLocation(
4293 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4294 // The runtime helper puts the output in R2,R3.
4295 locations->SetOut(Location::RegisterPairLocation(R2, R3));
4296 break;
4297 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00004298 case Primitive::kPrimFloat: {
4299 InvokeRuntimeCallingConvention calling_convention;
4300 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4301 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4302 locations->SetOut(Location::FpuRegisterLocation(S0));
4303 break;
4304 }
4305
Calin Juravlebacfec32014-11-14 15:54:36 +00004306 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004307 InvokeRuntimeCallingConvention calling_convention;
4308 locations->SetInAt(0, Location::FpuRegisterPairLocation(
4309 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4310 locations->SetInAt(1, Location::FpuRegisterPairLocation(
4311 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4312 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00004313 break;
4314 }
4315
4316 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004317 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004318 }
4319}
4320
4321void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
4322 LocationSummary* locations = rem->GetLocations();
4323 Location out = locations->Out();
4324 Location first = locations->InAt(0);
4325 Location second = locations->InAt(1);
4326
Calin Juravled2ec87d2014-12-08 14:24:46 +00004327 Primitive::Type type = rem->GetResultType();
4328 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004329 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004330 if (second.IsConstant()) {
4331 GenerateDivRemConstantIntegral(rem);
4332 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004333 Register reg1 = first.AsRegister<Register>();
4334 Register reg2 = second.AsRegister<Register>();
4335 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004336
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004337 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004338 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004339 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004340 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004341 } else {
4342 InvokeRuntimeCallingConvention calling_convention;
4343 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4344 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4345 DCHECK_EQ(R1, out.AsRegister<Register>());
4346
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004347 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004348 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004349 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004350 break;
4351 }
4352
4353 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004354 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004355 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004356 break;
4357 }
4358
Calin Juravled2ec87d2014-12-08 14:24:46 +00004359 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004360 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004361 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00004362 break;
4363 }
4364
Calin Juravlebacfec32014-11-14 15:54:36 +00004365 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004366 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004367 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004368 break;
4369 }
4370
4371 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004372 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004373 }
4374}
4375
Calin Juravled0d48522014-11-04 16:40:20 +00004376void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004377 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004378 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00004379}
4380
4381void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004382 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004383 codegen_->AddSlowPath(slow_path);
4384
4385 LocationSummary* locations = instruction->GetLocations();
4386 Location value = locations->InAt(0);
4387
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004388 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00004389 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004390 case Primitive::kPrimByte:
4391 case Primitive::kPrimChar:
4392 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004393 case Primitive::kPrimInt: {
4394 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004395 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004396 } else {
4397 DCHECK(value.IsConstant()) << value;
4398 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4399 __ b(slow_path->GetEntryLabel());
4400 }
4401 }
4402 break;
4403 }
4404 case Primitive::kPrimLong: {
4405 if (value.IsRegisterPair()) {
4406 __ orrs(IP,
4407 value.AsRegisterPairLow<Register>(),
4408 ShifterOperand(value.AsRegisterPairHigh<Register>()));
4409 __ b(slow_path->GetEntryLabel(), EQ);
4410 } else {
4411 DCHECK(value.IsConstant()) << value;
4412 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4413 __ b(slow_path->GetEntryLabel());
4414 }
4415 }
4416 break;
4417 default:
4418 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4419 }
4420 }
Calin Juravled0d48522014-11-04 16:40:20 +00004421}
4422
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004423void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
4424 Register in = locations->InAt(0).AsRegister<Register>();
4425 Location rhs = locations->InAt(1);
4426 Register out = locations->Out().AsRegister<Register>();
4427
4428 if (rhs.IsConstant()) {
4429 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4430 // so map all rotations to a +ve. equivalent in that range.
4431 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4432 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4433 if (rot) {
4434 // Rotate, mapping left rotations to right equivalents if necessary.
4435 // (e.g. left by 2 bits == right by 30.)
4436 __ Ror(out, in, rot);
4437 } else if (out != in) {
4438 __ Mov(out, in);
4439 }
4440 } else {
4441 __ Ror(out, in, rhs.AsRegister<Register>());
4442 }
4443}
4444
4445// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4446// rotates by swapping input regs (effectively rotating by the first 32-bits of
4447// a larger rotation) or flipping direction (thus treating larger right/left
4448// rotations as sub-word sized rotations in the other direction) as appropriate.
Anton Kirilov6f644202017-02-27 18:29:45 +00004449void InstructionCodeGeneratorARM::HandleLongRotate(HRor* ror) {
4450 LocationSummary* locations = ror->GetLocations();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004451 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
4452 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
4453 Location rhs = locations->InAt(1);
4454 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
4455 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
4456
4457 if (rhs.IsConstant()) {
4458 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4459 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00004460 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004461 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4462 // logic below to a simple pair of binary orr.
4463 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4464 if (rot >= kArmBitsPerWord) {
4465 rot -= kArmBitsPerWord;
4466 std::swap(in_reg_hi, in_reg_lo);
4467 }
4468 // Rotate, or mov to out for zero or word size rotations.
4469 if (rot != 0u) {
4470 __ Lsr(out_reg_hi, in_reg_hi, rot);
4471 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
4472 __ Lsr(out_reg_lo, in_reg_lo, rot);
4473 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
4474 } else {
4475 __ Mov(out_reg_lo, in_reg_lo);
4476 __ Mov(out_reg_hi, in_reg_hi);
4477 }
4478 } else {
4479 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
4480 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
4481 Label end;
4482 Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004483 Label* final_label = codegen_->GetFinalLabel(ror, &end);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004484
4485 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
4486 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
4487 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
4488 __ b(&shift_by_32_plus_shift_right, CC);
4489
4490 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4491 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4492 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4493 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4494 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4495 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4496 __ Lsr(shift_left, in_reg_hi, shift_right);
4497 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
Anton Kirilov6f644202017-02-27 18:29:45 +00004498 __ b(final_label);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004499
4500 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4501 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4502 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4503 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4504 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4505 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4506 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4507 __ Lsl(shift_right, in_reg_hi, shift_left);
4508 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
4509
Anton Kirilov6f644202017-02-27 18:29:45 +00004510 if (end.IsLinked()) {
4511 __ Bind(&end);
4512 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004513 }
4514}
Roland Levillain22c49222016-03-18 14:04:28 +00004515
4516void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004517 LocationSummary* locations =
4518 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4519 switch (ror->GetResultType()) {
4520 case Primitive::kPrimInt: {
4521 locations->SetInAt(0, Location::RequiresRegister());
4522 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4523 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4524 break;
4525 }
4526 case Primitive::kPrimLong: {
4527 locations->SetInAt(0, Location::RequiresRegister());
4528 if (ror->InputAt(1)->IsConstant()) {
4529 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4530 } else {
4531 locations->SetInAt(1, Location::RequiresRegister());
4532 locations->AddTemp(Location::RequiresRegister());
4533 locations->AddTemp(Location::RequiresRegister());
4534 }
4535 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4536 break;
4537 }
4538 default:
4539 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4540 }
4541}
4542
Roland Levillain22c49222016-03-18 14:04:28 +00004543void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004544 LocationSummary* locations = ror->GetLocations();
4545 Primitive::Type type = ror->GetResultType();
4546 switch (type) {
4547 case Primitive::kPrimInt: {
4548 HandleIntegerRotate(locations);
4549 break;
4550 }
4551 case Primitive::kPrimLong: {
Anton Kirilov6f644202017-02-27 18:29:45 +00004552 HandleLongRotate(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004553 break;
4554 }
4555 default:
4556 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004557 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004558 }
4559}
4560
Calin Juravle9aec02f2014-11-18 23:06:35 +00004561void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
4562 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4563
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004564 LocationSummary* locations =
4565 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004566
4567 switch (op->GetResultType()) {
4568 case Primitive::kPrimInt: {
4569 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004570 if (op->InputAt(1)->IsConstant()) {
4571 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4572 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4573 } else {
4574 locations->SetInAt(1, Location::RequiresRegister());
4575 // Make the output overlap, as it will be used to hold the masked
4576 // second input.
4577 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4578 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004579 break;
4580 }
4581 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004582 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004583 if (op->InputAt(1)->IsConstant()) {
4584 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4585 // For simplicity, use kOutputOverlap even though we only require that low registers
4586 // don't clash with high registers which the register allocator currently guarantees.
4587 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4588 } else {
4589 locations->SetInAt(1, Location::RequiresRegister());
4590 locations->AddTemp(Location::RequiresRegister());
4591 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4592 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004593 break;
4594 }
4595 default:
4596 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4597 }
4598}
4599
4600void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
4601 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4602
4603 LocationSummary* locations = op->GetLocations();
4604 Location out = locations->Out();
4605 Location first = locations->InAt(0);
4606 Location second = locations->InAt(1);
4607
4608 Primitive::Type type = op->GetResultType();
4609 switch (type) {
4610 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004611 Register out_reg = out.AsRegister<Register>();
4612 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004613 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004614 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00004615 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004616 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004617 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004618 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004619 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004620 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004621 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004622 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004623 }
4624 } else {
4625 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004626 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00004627 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00004628 __ Mov(out_reg, first_reg);
4629 } else if (op->IsShl()) {
4630 __ Lsl(out_reg, first_reg, shift_value);
4631 } else if (op->IsShr()) {
4632 __ Asr(out_reg, first_reg, shift_value);
4633 } else {
4634 __ Lsr(out_reg, first_reg, shift_value);
4635 }
4636 }
4637 break;
4638 }
4639 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004640 Register o_h = out.AsRegisterPairHigh<Register>();
4641 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004642
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004643 Register high = first.AsRegisterPairHigh<Register>();
4644 Register low = first.AsRegisterPairLow<Register>();
4645
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004646 if (second.IsRegister()) {
4647 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004648
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004649 Register second_reg = second.AsRegister<Register>();
4650
4651 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004652 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004653 // Shift the high part
4654 __ Lsl(o_h, high, o_l);
4655 // Shift the low part and `or` what overflew on the high part
4656 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
4657 __ Lsr(temp, low, temp);
4658 __ orr(o_h, o_h, ShifterOperand(temp));
4659 // If the shift is > 32 bits, override the high part
4660 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
4661 __ it(PL);
4662 __ Lsl(o_h, low, temp, PL);
4663 // Shift the low part
4664 __ Lsl(o_l, low, o_l);
4665 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004666 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004667 // Shift the low part
4668 __ Lsr(o_l, low, o_h);
4669 // Shift the high part and `or` what underflew on the low part
4670 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4671 __ Lsl(temp, high, temp);
4672 __ orr(o_l, o_l, ShifterOperand(temp));
4673 // If the shift is > 32 bits, override the low part
4674 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4675 __ it(PL);
4676 __ Asr(o_l, high, temp, PL);
4677 // Shift the high part
4678 __ Asr(o_h, high, o_h);
4679 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004680 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004681 // same as Shr except we use `Lsr`s and not `Asr`s
4682 __ Lsr(o_l, low, o_h);
4683 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4684 __ Lsl(temp, high, temp);
4685 __ orr(o_l, o_l, ShifterOperand(temp));
4686 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4687 __ it(PL);
4688 __ Lsr(o_l, high, temp, PL);
4689 __ Lsr(o_h, high, o_h);
4690 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004691 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004692 // Register allocator doesn't create partial overlap.
4693 DCHECK_NE(o_l, high);
4694 DCHECK_NE(o_h, low);
4695 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004696 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004697 if (shift_value > 32) {
4698 if (op->IsShl()) {
4699 __ Lsl(o_h, low, shift_value - 32);
4700 __ LoadImmediate(o_l, 0);
4701 } else if (op->IsShr()) {
4702 __ Asr(o_l, high, shift_value - 32);
4703 __ Asr(o_h, high, 31);
4704 } else {
4705 __ Lsr(o_l, high, shift_value - 32);
4706 __ LoadImmediate(o_h, 0);
4707 }
4708 } else if (shift_value == 32) {
4709 if (op->IsShl()) {
4710 __ mov(o_h, ShifterOperand(low));
4711 __ LoadImmediate(o_l, 0);
4712 } else if (op->IsShr()) {
4713 __ mov(o_l, ShifterOperand(high));
4714 __ Asr(o_h, high, 31);
4715 } else {
4716 __ mov(o_l, ShifterOperand(high));
4717 __ LoadImmediate(o_h, 0);
4718 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00004719 } else if (shift_value == 1) {
4720 if (op->IsShl()) {
4721 __ Lsls(o_l, low, 1);
4722 __ adc(o_h, high, ShifterOperand(high));
4723 } else if (op->IsShr()) {
4724 __ Asrs(o_h, high, 1);
4725 __ Rrx(o_l, low);
4726 } else {
4727 __ Lsrs(o_h, high, 1);
4728 __ Rrx(o_l, low);
4729 }
4730 } else {
4731 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004732 if (op->IsShl()) {
4733 __ Lsl(o_h, high, shift_value);
4734 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
4735 __ Lsl(o_l, low, shift_value);
4736 } else if (op->IsShr()) {
4737 __ Lsr(o_l, low, shift_value);
4738 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4739 __ Asr(o_h, high, shift_value);
4740 } else {
4741 __ Lsr(o_l, low, shift_value);
4742 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4743 __ Lsr(o_h, high, shift_value);
4744 }
4745 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004746 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004747 break;
4748 }
4749 default:
4750 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004751 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004752 }
4753}
4754
4755void LocationsBuilderARM::VisitShl(HShl* shl) {
4756 HandleShift(shl);
4757}
4758
4759void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
4760 HandleShift(shl);
4761}
4762
4763void LocationsBuilderARM::VisitShr(HShr* shr) {
4764 HandleShift(shr);
4765}
4766
4767void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
4768 HandleShift(shr);
4769}
4770
4771void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
4772 HandleShift(ushr);
4773}
4774
4775void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
4776 HandleShift(ushr);
4777}
4778
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004779void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004780 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004781 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00004782 if (instruction->IsStringAlloc()) {
4783 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4784 } else {
4785 InvokeRuntimeCallingConvention calling_convention;
4786 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004787 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004788 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004789}
4790
4791void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004792 // Note: if heap poisoning is enabled, the entry point takes cares
4793 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004794 if (instruction->IsStringAlloc()) {
4795 // String is allocated through StringFactory. Call NewEmptyString entry point.
4796 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004797 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004798 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4799 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
4800 __ blx(LR);
4801 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4802 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004803 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004804 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004805 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004806}
4807
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004808void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
4809 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004810 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004811 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004812 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004813 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4814 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004815}
4816
4817void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004818 // Note: if heap poisoning is enabled, the entry point takes cares
4819 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004820 QuickEntrypointEnum entrypoint =
4821 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4822 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004823 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004824 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004825}
4826
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004827void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004828 LocationSummary* locations =
4829 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004830 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4831 if (location.IsStackSlot()) {
4832 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4833 } else if (location.IsDoubleStackSlot()) {
4834 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004835 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004836 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004837}
4838
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004839void InstructionCodeGeneratorARM::VisitParameterValue(
4840 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004841 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004842}
4843
4844void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4845 LocationSummary* locations =
4846 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4847 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4848}
4849
4850void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4851 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004852}
4853
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004854void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004855 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004856 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004857 locations->SetInAt(0, Location::RequiresRegister());
4858 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004859}
4860
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004861void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4862 LocationSummary* locations = not_->GetLocations();
4863 Location out = locations->Out();
4864 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004865 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004866 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004867 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004868 break;
4869
4870 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004871 __ mvn(out.AsRegisterPairLow<Register>(),
4872 ShifterOperand(in.AsRegisterPairLow<Register>()));
4873 __ mvn(out.AsRegisterPairHigh<Register>(),
4874 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004875 break;
4876
4877 default:
4878 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4879 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004880}
4881
David Brazdil66d126e2015-04-03 16:02:44 +01004882void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4883 LocationSummary* locations =
4884 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4885 locations->SetInAt(0, Location::RequiresRegister());
4886 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4887}
4888
4889void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004890 LocationSummary* locations = bool_not->GetLocations();
4891 Location out = locations->Out();
4892 Location in = locations->InAt(0);
4893 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4894}
4895
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004896void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004897 LocationSummary* locations =
4898 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004899 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004900 case Primitive::kPrimBoolean:
4901 case Primitive::kPrimByte:
4902 case Primitive::kPrimShort:
4903 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004904 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004905 case Primitive::kPrimLong: {
4906 locations->SetInAt(0, Location::RequiresRegister());
4907 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004908 // Output overlaps because it is written before doing the low comparison.
4909 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004910 break;
4911 }
4912 case Primitive::kPrimFloat:
4913 case Primitive::kPrimDouble: {
4914 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004915 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004916 locations->SetOut(Location::RequiresRegister());
4917 break;
4918 }
4919 default:
4920 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4921 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004922}
4923
4924void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004925 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004926 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004927 Location left = locations->InAt(0);
4928 Location right = locations->InAt(1);
4929
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004930 Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00004931 Label* final_label = codegen_->GetFinalLabel(compare, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00004932 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004933 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004934 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004935 case Primitive::kPrimBoolean:
4936 case Primitive::kPrimByte:
4937 case Primitive::kPrimShort:
4938 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004939 case Primitive::kPrimInt: {
4940 __ LoadImmediate(out, 0);
4941 __ cmp(left.AsRegister<Register>(),
4942 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
4943 less_cond = LT;
4944 break;
4945 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004946 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004947 __ cmp(left.AsRegisterPairHigh<Register>(),
4948 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004949 __ b(&less, LT);
4950 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01004951 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00004952 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004953 __ cmp(left.AsRegisterPairLow<Register>(),
4954 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004955 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00004956 break;
4957 }
4958 case Primitive::kPrimFloat:
4959 case Primitive::kPrimDouble: {
4960 __ LoadImmediate(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08004961 GenerateVcmp(compare, codegen_);
Calin Juravleddb7df22014-11-25 20:56:51 +00004962 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00004963 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004964 break;
4965 }
4966 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004967 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00004968 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004969 }
Aart Bika19616e2016-02-01 18:57:58 -08004970
Anton Kirilov6f644202017-02-27 18:29:45 +00004971 __ b(final_label, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00004972 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00004973
4974 __ Bind(&greater);
4975 __ LoadImmediate(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00004976 __ b(final_label);
Calin Juravleddb7df22014-11-25 20:56:51 +00004977
4978 __ Bind(&less);
4979 __ LoadImmediate(out, -1);
4980
Anton Kirilov6f644202017-02-27 18:29:45 +00004981 if (done.IsLinked()) {
4982 __ Bind(&done);
4983 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004984}
4985
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004986void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004987 LocationSummary* locations =
4988 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004989 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004990 locations->SetInAt(i, Location::Any());
4991 }
4992 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004993}
4994
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004995void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004996 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004997}
4998
Roland Levillainc9285912015-12-18 10:38:42 +00004999void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
5000 // TODO (ported from quick): revisit ARM barrier kinds.
5001 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00005002 switch (kind) {
5003 case MemBarrierKind::kAnyStore:
5004 case MemBarrierKind::kLoadAny:
5005 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005006 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00005007 break;
5008 }
5009 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005010 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00005011 break;
5012 }
5013 default:
5014 LOG(FATAL) << "Unexpected memory barrier " << kind;
5015 }
Kenny Root1d8199d2015-06-02 11:01:10 -07005016 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00005017}
5018
5019void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
5020 uint32_t offset,
5021 Register out_lo,
5022 Register out_hi) {
5023 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005024 // Ensure `out_lo` is different from `addr`, so that loading
5025 // `offset` into `out_lo` does not clutter `addr`.
5026 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00005027 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005028 __ add(IP, addr, ShifterOperand(out_lo));
5029 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005030 }
5031 __ ldrexd(out_lo, out_hi, addr);
5032}
5033
5034void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
5035 uint32_t offset,
5036 Register value_lo,
5037 Register value_hi,
5038 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00005039 Register temp2,
5040 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005041 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00005042 if (offset != 0) {
5043 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005044 __ add(IP, addr, ShifterOperand(temp1));
5045 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005046 }
5047 __ Bind(&fail);
5048 // We need a load followed by store. (The address used in a STREX instruction must
5049 // be the same as the address in the most recently executed LDREX instruction.)
5050 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00005051 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005052 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005053 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00005054}
5055
5056void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5057 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5058
Nicolas Geoffray39468442014-09-02 15:17:15 +01005059 LocationSummary* locations =
5060 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005061 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005062
Calin Juravle52c48962014-12-16 17:02:57 +00005063 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005064 if (Primitive::IsFloatingPointType(field_type)) {
5065 locations->SetInAt(1, Location::RequiresFpuRegister());
5066 } else {
5067 locations->SetInAt(1, Location::RequiresRegister());
5068 }
5069
Calin Juravle52c48962014-12-16 17:02:57 +00005070 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00005071 bool generate_volatile = field_info.IsVolatile()
5072 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005073 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01005074 bool needs_write_barrier =
5075 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005076 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00005077 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01005078 if (needs_write_barrier) {
5079 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005080 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005081 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005082 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005083 // - registers need to be consecutive
5084 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005085 // We don't test for ARM yet, and the assertion makes sure that we
5086 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005087 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5088
5089 locations->AddTemp(Location::RequiresRegister());
5090 locations->AddTemp(Location::RequiresRegister());
5091 if (field_type == Primitive::kPrimDouble) {
5092 // For doubles we need two more registers to copy the value.
5093 locations->AddTemp(Location::RegisterLocation(R2));
5094 locations->AddTemp(Location::RegisterLocation(R3));
5095 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005096 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005097}
5098
Calin Juravle52c48962014-12-16 17:02:57 +00005099void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005100 const FieldInfo& field_info,
5101 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005102 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5103
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005104 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00005105 Register base = locations->InAt(0).AsRegister<Register>();
5106 Location value = locations->InAt(1);
5107
5108 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005109 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005110 Primitive::Type field_type = field_info.GetFieldType();
5111 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005112 bool needs_write_barrier =
5113 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005114
5115 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005116 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005117 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005118
5119 switch (field_type) {
5120 case Primitive::kPrimBoolean:
5121 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00005122 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005123 break;
5124 }
5125
5126 case Primitive::kPrimShort:
5127 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00005128 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005129 break;
5130 }
5131
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005133 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01005134 if (kPoisonHeapReferences && needs_write_barrier) {
5135 // Note that in the case where `value` is a null reference,
5136 // we do not enter this block, as a null reference does not
5137 // need poisoning.
5138 DCHECK_EQ(field_type, Primitive::kPrimNot);
5139 Register temp = locations->GetTemp(0).AsRegister<Register>();
5140 __ Mov(temp, value.AsRegister<Register>());
5141 __ PoisonHeapReference(temp);
5142 __ StoreToOffset(kStoreWord, temp, base, offset);
5143 } else {
5144 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
5145 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005146 break;
5147 }
5148
5149 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00005150 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005151 GenerateWideAtomicStore(base, offset,
5152 value.AsRegisterPairLow<Register>(),
5153 value.AsRegisterPairHigh<Register>(),
5154 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005155 locations->GetTemp(1).AsRegister<Register>(),
5156 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005157 } else {
5158 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005159 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005160 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005161 break;
5162 }
5163
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005164 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00005165 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005166 break;
5167 }
5168
5169 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005170 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005171 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005172 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
5173 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
5174
5175 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
5176
5177 GenerateWideAtomicStore(base, offset,
5178 value_reg_lo,
5179 value_reg_hi,
5180 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005181 locations->GetTemp(3).AsRegister<Register>(),
5182 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005183 } else {
5184 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005185 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005186 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005187 break;
5188 }
5189
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005190 case Primitive::kPrimVoid:
5191 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005192 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005193 }
Calin Juravle52c48962014-12-16 17:02:57 +00005194
Calin Juravle77520bc2015-01-12 18:45:46 +00005195 // Longs and doubles are handled in the switch.
5196 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
5197 codegen_->MaybeRecordImplicitNullCheck(instruction);
5198 }
5199
5200 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5201 Register temp = locations->GetTemp(0).AsRegister<Register>();
5202 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005203 codegen_->MarkGCCard(
5204 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005205 }
5206
Calin Juravle52c48962014-12-16 17:02:57 +00005207 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005208 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005209 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005210}
5211
Calin Juravle52c48962014-12-16 17:02:57 +00005212void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5213 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00005214
5215 bool object_field_get_with_read_barrier =
5216 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005217 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005218 new (GetGraph()->GetArena()) LocationSummary(instruction,
5219 object_field_get_with_read_barrier ?
5220 LocationSummary::kCallOnSlowPath :
5221 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005222 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005223 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005224 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005225 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00005226
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005227 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00005228 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005229 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00005230 // The output overlaps in case of volatile long: we don't want the
5231 // code generated by GenerateWideAtomicLoad to overwrite the
5232 // object's location. Likewise, in the case of an object field get
5233 // with read barriers enabled, we do not want the load to overwrite
5234 // the object's location, as we need it to emit the read barrier.
5235 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5236 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01005237
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005238 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5239 locations->SetOut(Location::RequiresFpuRegister());
5240 } else {
5241 locations->SetOut(Location::RequiresRegister(),
5242 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5243 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005244 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00005245 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005246 // - registers need to be consecutive
5247 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005248 // We don't test for ARM yet, and the assertion makes sure that we
5249 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005250 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5251 locations->AddTemp(Location::RequiresRegister());
5252 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00005253 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5254 // We need a temporary register for the read barrier marking slow
5255 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
5256 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00005257 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005258}
5259
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005260Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5261 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
5262 << input->GetType();
5263 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5264 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5265 return Location::ConstantLocation(input->AsConstant());
5266 } else {
5267 return Location::RequiresFpuRegister();
5268 }
5269}
5270
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005271Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
5272 Opcode opcode) {
5273 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5274 if (constant->IsConstant() &&
5275 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5276 return Location::ConstantLocation(constant->AsConstant());
5277 }
5278 return Location::RequiresRegister();
5279}
5280
5281bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
5282 Opcode opcode) {
5283 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5284 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01005285 Opcode high_opcode = opcode;
5286 SetCc low_set_cc = kCcDontCare;
5287 switch (opcode) {
5288 case SUB:
5289 // Flip the operation to an ADD.
5290 value = -value;
5291 opcode = ADD;
5292 FALLTHROUGH_INTENDED;
5293 case ADD:
5294 if (Low32Bits(value) == 0u) {
5295 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5296 }
5297 high_opcode = ADC;
5298 low_set_cc = kCcSet;
5299 break;
5300 default:
5301 break;
5302 }
5303 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5304 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005305 } else {
5306 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5307 }
5308}
5309
Vladimir Marko59751a72016-08-05 14:37:27 +01005310bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
5311 Opcode opcode,
5312 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005313 ShifterOperand so;
5314 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01005315 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005316 return true;
5317 }
5318 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005319 uint32_t neg_value = 0;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005320 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005321 case AND: neg_opcode = BIC; neg_value = ~value; break;
5322 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5323 case ADD: neg_opcode = SUB; neg_value = -value; break;
5324 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5325 case SUB: neg_opcode = ADD; neg_value = -value; break;
5326 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5327 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005328 default:
5329 return false;
5330 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005331
5332 if (assembler->ShifterOperandCanHold(kNoRegister,
5333 kNoRegister,
5334 neg_opcode,
5335 neg_value,
5336 set_cc,
5337 &so)) {
5338 return true;
5339 }
5340
5341 return opcode == AND && IsPowerOfTwo(value + 1);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005342}
5343
Calin Juravle52c48962014-12-16 17:02:57 +00005344void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
5345 const FieldInfo& field_info) {
5346 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005347
Calin Juravle52c48962014-12-16 17:02:57 +00005348 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005349 Location base_loc = locations->InAt(0);
5350 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005351 Location out = locations->Out();
5352 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005353 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005354 Primitive::Type field_type = field_info.GetFieldType();
5355 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5356
5357 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00005358 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00005359 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005360 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005361
Roland Levillainc9285912015-12-18 10:38:42 +00005362 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00005363 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005364 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005365
Roland Levillainc9285912015-12-18 10:38:42 +00005366 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00005367 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005368 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005369
Roland Levillainc9285912015-12-18 10:38:42 +00005370 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00005371 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005372 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005373
5374 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00005375 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005376 break;
Roland Levillainc9285912015-12-18 10:38:42 +00005377
5378 case Primitive::kPrimNot: {
5379 // /* HeapReference<Object> */ out = *(base + offset)
5380 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5381 Location temp_loc = locations->GetTemp(0);
5382 // Note that a potential implicit null check is handled in this
5383 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
5384 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5385 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5386 if (is_volatile) {
5387 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5388 }
5389 } else {
5390 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
5391 codegen_->MaybeRecordImplicitNullCheck(instruction);
5392 if (is_volatile) {
5393 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5394 }
5395 // If read barriers are enabled, emit read barriers other than
5396 // Baker's using a slow path (and also unpoison the loaded
5397 // reference, if heap poisoning is enabled).
5398 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5399 }
5400 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005401 }
5402
Roland Levillainc9285912015-12-18 10:38:42 +00005403 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00005404 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005405 GenerateWideAtomicLoad(base, offset,
5406 out.AsRegisterPairLow<Register>(),
5407 out.AsRegisterPairHigh<Register>());
5408 } else {
5409 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
5410 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005411 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005412
Roland Levillainc9285912015-12-18 10:38:42 +00005413 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00005414 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005415 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005416
5417 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005418 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005419 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005420 Register lo = locations->GetTemp(0).AsRegister<Register>();
5421 Register hi = locations->GetTemp(1).AsRegister<Register>();
5422 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00005423 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005424 __ vmovdrr(out_reg, lo, hi);
5425 } else {
5426 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005427 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005428 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005429 break;
5430 }
5431
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005432 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005433 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005434 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005435 }
Calin Juravle52c48962014-12-16 17:02:57 +00005436
Roland Levillainc9285912015-12-18 10:38:42 +00005437 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5438 // Potential implicit null checks, in the case of reference or
5439 // double fields, are handled in the previous switch statement.
5440 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005441 codegen_->MaybeRecordImplicitNullCheck(instruction);
5442 }
5443
Calin Juravle52c48962014-12-16 17:02:57 +00005444 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005445 if (field_type == Primitive::kPrimNot) {
5446 // Memory barriers, in the case of references, are also handled
5447 // in the previous switch statement.
5448 } else {
5449 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5450 }
Roland Levillain4d027112015-07-01 15:41:14 +01005451 }
Calin Juravle52c48962014-12-16 17:02:57 +00005452}
5453
5454void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5455 HandleFieldSet(instruction, instruction->GetFieldInfo());
5456}
5457
5458void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005459 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005460}
5461
5462void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5463 HandleFieldGet(instruction, instruction->GetFieldInfo());
5464}
5465
5466void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5467 HandleFieldGet(instruction, instruction->GetFieldInfo());
5468}
5469
5470void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5471 HandleFieldGet(instruction, instruction->GetFieldInfo());
5472}
5473
5474void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5475 HandleFieldGet(instruction, instruction->GetFieldInfo());
5476}
5477
5478void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5479 HandleFieldSet(instruction, instruction->GetFieldInfo());
5480}
5481
5482void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005483 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005484}
5485
Calin Juravlee460d1d2015-09-29 04:52:17 +01005486void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
5487 HUnresolvedInstanceFieldGet* instruction) {
5488 FieldAccessCallingConventionARM calling_convention;
5489 codegen_->CreateUnresolvedFieldLocationSummary(
5490 instruction, instruction->GetFieldType(), calling_convention);
5491}
5492
5493void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
5494 HUnresolvedInstanceFieldGet* instruction) {
5495 FieldAccessCallingConventionARM calling_convention;
5496 codegen_->GenerateUnresolvedFieldAccess(instruction,
5497 instruction->GetFieldType(),
5498 instruction->GetFieldIndex(),
5499 instruction->GetDexPc(),
5500 calling_convention);
5501}
5502
5503void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
5504 HUnresolvedInstanceFieldSet* instruction) {
5505 FieldAccessCallingConventionARM calling_convention;
5506 codegen_->CreateUnresolvedFieldLocationSummary(
5507 instruction, instruction->GetFieldType(), calling_convention);
5508}
5509
5510void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
5511 HUnresolvedInstanceFieldSet* instruction) {
5512 FieldAccessCallingConventionARM calling_convention;
5513 codegen_->GenerateUnresolvedFieldAccess(instruction,
5514 instruction->GetFieldType(),
5515 instruction->GetFieldIndex(),
5516 instruction->GetDexPc(),
5517 calling_convention);
5518}
5519
5520void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
5521 HUnresolvedStaticFieldGet* instruction) {
5522 FieldAccessCallingConventionARM calling_convention;
5523 codegen_->CreateUnresolvedFieldLocationSummary(
5524 instruction, instruction->GetFieldType(), calling_convention);
5525}
5526
5527void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
5528 HUnresolvedStaticFieldGet* instruction) {
5529 FieldAccessCallingConventionARM calling_convention;
5530 codegen_->GenerateUnresolvedFieldAccess(instruction,
5531 instruction->GetFieldType(),
5532 instruction->GetFieldIndex(),
5533 instruction->GetDexPc(),
5534 calling_convention);
5535}
5536
5537void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
5538 HUnresolvedStaticFieldSet* instruction) {
5539 FieldAccessCallingConventionARM calling_convention;
5540 codegen_->CreateUnresolvedFieldLocationSummary(
5541 instruction, instruction->GetFieldType(), calling_convention);
5542}
5543
5544void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
5545 HUnresolvedStaticFieldSet* instruction) {
5546 FieldAccessCallingConventionARM calling_convention;
5547 codegen_->GenerateUnresolvedFieldAccess(instruction,
5548 instruction->GetFieldType(),
5549 instruction->GetFieldIndex(),
5550 instruction->GetDexPc(),
5551 calling_convention);
5552}
5553
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005554void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005555 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5556 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005557}
5558
Calin Juravle2ae48182016-03-16 14:05:09 +00005559void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
5560 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005561 return;
5562 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005563 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005564
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005565 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005566 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005567}
5568
Calin Juravle2ae48182016-03-16 14:05:09 +00005569void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01005570 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005571 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005572
5573 LocationSummary* locations = instruction->GetLocations();
5574 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005575
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005576 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005577}
5578
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005579void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005580 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005581}
5582
Artem Serov6c916792016-07-11 14:02:34 +01005583static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5584 switch (type) {
5585 case Primitive::kPrimNot:
5586 return kLoadWord;
5587 case Primitive::kPrimBoolean:
5588 return kLoadUnsignedByte;
5589 case Primitive::kPrimByte:
5590 return kLoadSignedByte;
5591 case Primitive::kPrimChar:
5592 return kLoadUnsignedHalfword;
5593 case Primitive::kPrimShort:
5594 return kLoadSignedHalfword;
5595 case Primitive::kPrimInt:
5596 return kLoadWord;
5597 case Primitive::kPrimLong:
5598 return kLoadWordPair;
5599 case Primitive::kPrimFloat:
5600 return kLoadSWord;
5601 case Primitive::kPrimDouble:
5602 return kLoadDWord;
5603 default:
5604 LOG(FATAL) << "Unreachable type " << type;
5605 UNREACHABLE();
5606 }
5607}
5608
5609static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5610 switch (type) {
5611 case Primitive::kPrimNot:
5612 return kStoreWord;
5613 case Primitive::kPrimBoolean:
5614 case Primitive::kPrimByte:
5615 return kStoreByte;
5616 case Primitive::kPrimChar:
5617 case Primitive::kPrimShort:
5618 return kStoreHalfword;
5619 case Primitive::kPrimInt:
5620 return kStoreWord;
5621 case Primitive::kPrimLong:
5622 return kStoreWordPair;
5623 case Primitive::kPrimFloat:
5624 return kStoreSWord;
5625 case Primitive::kPrimDouble:
5626 return kStoreDWord;
5627 default:
5628 LOG(FATAL) << "Unreachable type " << type;
5629 UNREACHABLE();
5630 }
5631}
5632
5633void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
5634 Location out_loc,
5635 Register base,
5636 Register reg_offset,
5637 Condition cond) {
5638 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5639 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5640
5641 switch (type) {
5642 case Primitive::kPrimByte:
5643 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
5644 break;
5645 case Primitive::kPrimBoolean:
5646 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
5647 break;
5648 case Primitive::kPrimShort:
5649 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
5650 break;
5651 case Primitive::kPrimChar:
5652 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
5653 break;
5654 case Primitive::kPrimNot:
5655 case Primitive::kPrimInt:
5656 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
5657 break;
5658 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5659 case Primitive::kPrimLong:
5660 case Primitive::kPrimFloat:
5661 case Primitive::kPrimDouble:
5662 default:
5663 LOG(FATAL) << "Unreachable type " << type;
5664 UNREACHABLE();
5665 }
5666}
5667
5668void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
5669 Location loc,
5670 Register base,
5671 Register reg_offset,
5672 Condition cond) {
5673 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5674 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5675
5676 switch (type) {
5677 case Primitive::kPrimByte:
5678 case Primitive::kPrimBoolean:
5679 __ strb(loc.AsRegister<Register>(), mem_address, cond);
5680 break;
5681 case Primitive::kPrimShort:
5682 case Primitive::kPrimChar:
5683 __ strh(loc.AsRegister<Register>(), mem_address, cond);
5684 break;
5685 case Primitive::kPrimNot:
5686 case Primitive::kPrimInt:
5687 __ str(loc.AsRegister<Register>(), mem_address, cond);
5688 break;
5689 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5690 case Primitive::kPrimLong:
5691 case Primitive::kPrimFloat:
5692 case Primitive::kPrimDouble:
5693 default:
5694 LOG(FATAL) << "Unreachable type " << type;
5695 UNREACHABLE();
5696 }
5697}
5698
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005699void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005700 bool object_array_get_with_read_barrier =
5701 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005702 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005703 new (GetGraph()->GetArena()) LocationSummary(instruction,
5704 object_array_get_with_read_barrier ?
5705 LocationSummary::kCallOnSlowPath :
5706 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005707 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005708 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005709 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005710 locations->SetInAt(0, Location::RequiresRegister());
5711 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005712 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5713 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5714 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005715 // The output overlaps in the case of an object array get with
5716 // read barriers enabled: we do not want the move to overwrite the
5717 // array's location, as we need it to emit the read barrier.
5718 locations->SetOut(
5719 Location::RequiresRegister(),
5720 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005721 }
Roland Levillainc9285912015-12-18 10:38:42 +00005722 // We need a temporary register for the read barrier marking slow
5723 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
jessicahandojo05765752016-09-09 19:01:32 -07005724 // Also need for String compression feature.
5725 if ((object_array_get_with_read_barrier && kUseBakerReadBarrier)
5726 || (mirror::kUseStringCompression && instruction->IsStringCharAt())) {
Roland Levillainc9285912015-12-18 10:38:42 +00005727 locations->AddTemp(Location::RequiresRegister());
5728 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005729}
5730
5731void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
5732 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005733 Location obj_loc = locations->InAt(0);
5734 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005735 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00005736 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005737 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00005738 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07005739 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5740 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01005741 HInstruction* array_instr = instruction->GetArray();
5742 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01005743
Roland Levillain4d027112015-07-01 15:41:14 +01005744 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01005745 case Primitive::kPrimBoolean:
5746 case Primitive::kPrimByte:
5747 case Primitive::kPrimShort:
5748 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00005749 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005750 Register length;
5751 if (maybe_compressed_char_at) {
5752 length = locations->GetTemp(0).AsRegister<Register>();
5753 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5754 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
5755 codegen_->MaybeRecordImplicitNullCheck(instruction);
5756 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005757 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005758 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07005759 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07005760 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005761 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005762 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5763 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5764 "Expecting 0=compressed, 1=uncompressed");
5765 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005766 __ LoadFromOffset(kLoadUnsignedByte,
5767 out_loc.AsRegister<Register>(),
5768 obj,
5769 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00005770 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07005771 __ Bind(&uncompressed_load);
5772 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5773 out_loc.AsRegister<Register>(),
5774 obj,
5775 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005776 if (done.IsLinked()) {
5777 __ Bind(&done);
5778 }
jessicahandojo05765752016-09-09 19:01:32 -07005779 } else {
5780 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01005781
jessicahandojo05765752016-09-09 19:01:32 -07005782 LoadOperandType load_type = GetLoadOperandType(type);
5783 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
5784 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005785 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005786 Register temp = IP;
5787
5788 if (has_intermediate_address) {
5789 // We do not need to compute the intermediate address from the array: the
5790 // input instruction has done it already. See the comment in
5791 // `TryExtractArrayAccessAddress()`.
5792 if (kIsDebugBuild) {
5793 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5794 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5795 }
5796 temp = obj;
5797 } else {
5798 __ add(temp, obj, ShifterOperand(data_offset));
5799 }
jessicahandojo05765752016-09-09 19:01:32 -07005800 if (maybe_compressed_char_at) {
5801 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005802 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005803 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5804 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5805 "Expecting 0=compressed, 1=uncompressed");
5806 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005807 __ ldrb(out_loc.AsRegister<Register>(),
5808 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00005809 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07005810 __ Bind(&uncompressed_load);
5811 __ ldrh(out_loc.AsRegister<Register>(),
5812 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005813 if (done.IsLinked()) {
5814 __ Bind(&done);
5815 }
jessicahandojo05765752016-09-09 19:01:32 -07005816 } else {
5817 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
5818 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005819 }
5820 break;
5821 }
5822
Roland Levillainc9285912015-12-18 10:38:42 +00005823 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00005824 // The read barrier instrumentation of object ArrayGet
5825 // instructions does not support the HIntermediateAddress
5826 // instruction.
5827 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5828
Roland Levillainc9285912015-12-18 10:38:42 +00005829 static_assert(
5830 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5831 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00005832 // /* HeapReference<Object> */ out =
5833 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5834 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5835 Location temp = locations->GetTemp(0);
5836 // Note that a potential implicit null check is handled in this
5837 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
5838 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5839 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5840 } else {
5841 Register out = out_loc.AsRegister<Register>();
5842 if (index.IsConstant()) {
5843 size_t offset =
5844 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5845 __ LoadFromOffset(kLoadWord, out, obj, offset);
5846 codegen_->MaybeRecordImplicitNullCheck(instruction);
5847 // If read barriers are enabled, emit read barriers other than
5848 // Baker's using a slow path (and also unpoison the loaded
5849 // reference, if heap poisoning is enabled).
5850 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5851 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005852 Register temp = IP;
5853
5854 if (has_intermediate_address) {
5855 // We do not need to compute the intermediate address from the array: the
5856 // input instruction has done it already. See the comment in
5857 // `TryExtractArrayAccessAddress()`.
5858 if (kIsDebugBuild) {
5859 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5860 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5861 }
5862 temp = obj;
5863 } else {
5864 __ add(temp, obj, ShifterOperand(data_offset));
5865 }
5866 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005867
Roland Levillainc9285912015-12-18 10:38:42 +00005868 codegen_->MaybeRecordImplicitNullCheck(instruction);
5869 // If read barriers are enabled, emit read barriers other than
5870 // Baker's using a slow path (and also unpoison the loaded
5871 // reference, if heap poisoning is enabled).
5872 codegen_->MaybeGenerateReadBarrierSlow(
5873 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5874 }
5875 }
5876 break;
5877 }
5878
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005879 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005880 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005881 size_t offset =
5882 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005883 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005884 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005885 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005886 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005887 }
5888 break;
5889 }
5890
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005891 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00005892 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005893 if (index.IsConstant()) {
5894 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005895 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005896 } else {
5897 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00005898 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005899 }
5900 break;
5901 }
5902
5903 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00005904 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005905 if (index.IsConstant()) {
5906 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005907 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005908 } else {
5909 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005910 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00005911 }
5912 break;
5913 }
5914
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005915 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01005916 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005917 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005918 }
Roland Levillain4d027112015-07-01 15:41:14 +01005919
5920 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00005921 // Potential implicit null checks, in the case of reference
5922 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07005923 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00005924 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01005925 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005926}
5927
5928void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005929 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005930
5931 bool needs_write_barrier =
5932 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00005933 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005934
Nicolas Geoffray39468442014-09-02 15:17:15 +01005935 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005936 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005937 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00005938 LocationSummary::kCallOnSlowPath :
5939 LocationSummary::kNoCall);
5940
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005941 locations->SetInAt(0, Location::RequiresRegister());
5942 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5943 if (Primitive::IsFloatingPointType(value_type)) {
5944 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005945 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005946 locations->SetInAt(2, Location::RequiresRegister());
5947 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005948 if (needs_write_barrier) {
5949 // Temporary registers for the write barrier.
5950 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005951 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005952 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005953}
5954
5955void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
5956 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005957 Location array_loc = locations->InAt(0);
5958 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005959 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005960 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00005961 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005962 bool needs_write_barrier =
5963 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01005964 uint32_t data_offset =
5965 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
5966 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01005967 HInstruction* array_instr = instruction->GetArray();
5968 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005969
5970 switch (value_type) {
5971 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01005972 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005973 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01005974 case Primitive::kPrimChar:
5975 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005976 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005977 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
5978 uint32_t full_offset =
5979 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
5980 StoreOperandType store_type = GetStoreOperandType(value_type);
5981 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005982 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005983 Register temp = IP;
5984
5985 if (has_intermediate_address) {
5986 // We do not need to compute the intermediate address from the array: the
5987 // input instruction has done it already. See the comment in
5988 // `TryExtractArrayAccessAddress()`.
5989 if (kIsDebugBuild) {
5990 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5991 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
5992 }
5993 temp = array;
5994 } else {
5995 __ add(temp, array, ShifterOperand(data_offset));
5996 }
Artem Serov6c916792016-07-11 14:02:34 +01005997 codegen_->StoreToShiftedRegOffset(value_type,
5998 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01005999 temp,
Artem Serov6c916792016-07-11 14:02:34 +01006000 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006001 }
6002 break;
6003 }
6004
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006005 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00006006 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01006007 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6008 // See the comment in instruction_simplifier_shared.cc.
6009 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006010
6011 if (instruction->InputAt(2)->IsNullConstant()) {
6012 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006013 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006014 size_t offset =
6015 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01006016 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006017 } else {
6018 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006019 __ add(IP, array, ShifterOperand(data_offset));
6020 codegen_->StoreToShiftedRegOffset(value_type,
6021 value_loc,
6022 IP,
6023 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006024 }
Roland Levillain1407ee72016-01-08 15:56:19 +00006025 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00006026 DCHECK(!needs_write_barrier);
6027 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006028 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006029 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006030
6031 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01006032 Location temp1_loc = locations->GetTemp(0);
6033 Register temp1 = temp1_loc.AsRegister<Register>();
6034 Location temp2_loc = locations->GetTemp(1);
6035 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006036 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6037 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6038 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6039 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006040 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006041 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006042
Roland Levillain3b359c72015-11-17 19:35:12 +00006043 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006044 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
6045 codegen_->AddSlowPath(slow_path);
6046 if (instruction->GetValueCanBeNull()) {
6047 Label non_zero;
6048 __ CompareAndBranchIfNonZero(value, &non_zero);
6049 if (index.IsConstant()) {
6050 size_t offset =
6051 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6052 __ StoreToOffset(kStoreWord, value, array, offset);
6053 } else {
6054 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006055 __ add(IP, array, ShifterOperand(data_offset));
6056 codegen_->StoreToShiftedRegOffset(value_type,
6057 value_loc,
6058 IP,
6059 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006060 }
6061 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006062 __ b(final_label);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006063 __ Bind(&non_zero);
6064 }
6065
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006066 // Note that when read barriers are enabled, the type checks
6067 // are performed without read barriers. This is fine, even in
6068 // the case where a class object is in the from-space after
6069 // the flip, as a comparison involving such a type would not
6070 // produce a false positive; it may of course produce a false
6071 // negative, in which case we would take the ArraySet slow
6072 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006073
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006074 // /* HeapReference<Class> */ temp1 = array->klass_
6075 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
6076 codegen_->MaybeRecordImplicitNullCheck(instruction);
6077 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01006078
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006079 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6080 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6081 // /* HeapReference<Class> */ temp2 = value->klass_
6082 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
6083 // If heap poisoning is enabled, no need to unpoison `temp1`
6084 // nor `temp2`, as we are comparing two poisoned references.
6085 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01006086
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006087 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6088 Label do_put;
6089 __ b(&do_put, EQ);
6090 // If heap poisoning is enabled, the `temp1` reference has
6091 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00006092 __ MaybeUnpoisonHeapReference(temp1);
6093
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006094 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6095 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6096 // If heap poisoning is enabled, no need to unpoison
6097 // `temp1`, as we are comparing against null below.
6098 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
6099 __ Bind(&do_put);
6100 } else {
6101 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006102 }
6103 }
6104
Artem Serov6c916792016-07-11 14:02:34 +01006105 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006106 if (kPoisonHeapReferences) {
6107 // Note that in the case where `value` is a null reference,
6108 // we do not enter this block, as a null reference does not
6109 // need poisoning.
6110 DCHECK_EQ(value_type, Primitive::kPrimNot);
6111 __ Mov(temp1, value);
6112 __ PoisonHeapReference(temp1);
6113 source = temp1;
6114 }
6115
6116 if (index.IsConstant()) {
6117 size_t offset =
6118 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6119 __ StoreToOffset(kStoreWord, source, array, offset);
6120 } else {
6121 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006122
6123 __ add(IP, array, ShifterOperand(data_offset));
6124 codegen_->StoreToShiftedRegOffset(value_type,
6125 Location::RegisterLocation(source),
6126 IP,
6127 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006128 }
6129
Roland Levillain3b359c72015-11-17 19:35:12 +00006130 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006131 codegen_->MaybeRecordImplicitNullCheck(instruction);
6132 }
6133
6134 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6135
6136 if (done.IsLinked()) {
6137 __ Bind(&done);
6138 }
6139
6140 if (slow_path != nullptr) {
6141 __ Bind(slow_path->GetExitLabel());
6142 }
6143
6144 break;
6145 }
6146
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006147 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006148 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006149 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006150 size_t offset =
6151 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006152 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006153 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006154 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006155 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006156 }
6157 break;
6158 }
6159
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006160 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006161 Location value = locations->InAt(2);
6162 DCHECK(value.IsFpuRegister());
6163 if (index.IsConstant()) {
6164 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006165 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006166 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006167 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006168 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
6169 }
6170 break;
6171 }
6172
6173 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006174 Location value = locations->InAt(2);
6175 DCHECK(value.IsFpuRegisterPair());
6176 if (index.IsConstant()) {
6177 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006178 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006179 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006180 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006181 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
6182 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006183
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006184 break;
6185 }
6186
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006187 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006188 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006189 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006190 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006191
Roland Levillain80e67092016-01-08 16:04:55 +00006192 // Objects are handled in the switch.
6193 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006194 codegen_->MaybeRecordImplicitNullCheck(instruction);
6195 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006196}
6197
6198void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006199 LocationSummary* locations =
6200 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006201 locations->SetInAt(0, Location::RequiresRegister());
6202 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006203}
6204
6205void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
6206 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006207 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006208 Register obj = locations->InAt(0).AsRegister<Register>();
6209 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006210 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00006211 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07006212 // Mask out compression flag from String's array length.
6213 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006214 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07006215 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006216}
6217
Artem Serov328429f2016-07-06 16:23:04 +01006218void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01006219 LocationSummary* locations =
6220 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6221
6222 locations->SetInAt(0, Location::RequiresRegister());
6223 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6224 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6225}
6226
6227void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6228 LocationSummary* locations = instruction->GetLocations();
6229 Location out = locations->Out();
6230 Location first = locations->InAt(0);
6231 Location second = locations->InAt(1);
6232
Artem Serov328429f2016-07-06 16:23:04 +01006233 if (second.IsRegister()) {
6234 __ add(out.AsRegister<Register>(),
6235 first.AsRegister<Register>(),
6236 ShifterOperand(second.AsRegister<Register>()));
6237 } else {
6238 __ AddConstant(out.AsRegister<Register>(),
6239 first.AsRegister<Register>(),
6240 second.GetConstant()->AsIntConstant()->GetValue());
6241 }
6242}
6243
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006244void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006245 RegisterSet caller_saves = RegisterSet::Empty();
6246 InvokeRuntimeCallingConvention calling_convention;
6247 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6248 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6249 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006250
6251 HInstruction* index = instruction->InputAt(0);
6252 HInstruction* length = instruction->InputAt(1);
6253 // If both index and length are constants we can statically check the bounds. But if at least one
6254 // of them is not encodable ArmEncodableConstantOrRegister will create
6255 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6256 // locations.
6257 bool both_const = index->IsConstant() && length->IsConstant();
6258 locations->SetInAt(0, both_const
6259 ? Location::ConstantLocation(index->AsConstant())
6260 : ArmEncodableConstantOrRegister(index, CMP));
6261 locations->SetInAt(1, both_const
6262 ? Location::ConstantLocation(length->AsConstant())
6263 : ArmEncodableConstantOrRegister(length, CMP));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006264}
6265
6266void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
6267 LocationSummary* locations = instruction->GetLocations();
Artem Serov2dd053d2017-03-08 14:54:06 +00006268 Location index_loc = locations->InAt(0);
6269 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006270
Artem Serov2dd053d2017-03-08 14:54:06 +00006271 if (length_loc.IsConstant()) {
6272 int32_t length = helpers::Int32ConstantFrom(length_loc);
6273 if (index_loc.IsConstant()) {
6274 // BCE will remove the bounds check if we are guaranteed to pass.
6275 int32_t index = helpers::Int32ConstantFrom(index_loc);
6276 if (index < 0 || index >= length) {
6277 SlowPathCodeARM* slow_path =
6278 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6279 codegen_->AddSlowPath(slow_path);
6280 __ b(slow_path->GetEntryLabel());
6281 } else {
6282 // Some optimization after BCE may have generated this, and we should not
6283 // generate a bounds check if it is a valid range.
6284 }
6285 return;
6286 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006287
Artem Serov2dd053d2017-03-08 14:54:06 +00006288 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6289 __ cmp(index_loc.AsRegister<Register>(), ShifterOperand(length));
6290 codegen_->AddSlowPath(slow_path);
6291 __ b(slow_path->GetEntryLabel(), HS);
6292 } else {
6293 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6294 if (index_loc.IsConstant()) {
6295 int32_t index = helpers::Int32ConstantFrom(index_loc);
6296 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index));
6297 } else {
6298 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index_loc.AsRegister<Register>()));
6299 }
6300 codegen_->AddSlowPath(slow_path);
6301 __ b(slow_path->GetEntryLabel(), LS);
6302 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006303}
6304
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006305void CodeGeneratorARM::MarkGCCard(Register temp,
6306 Register card,
6307 Register object,
6308 Register value,
6309 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006310 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006311 if (can_be_null) {
6312 __ CompareAndBranchIfZero(value, &is_null);
6313 }
Andreas Gampe542451c2016-07-26 09:02:02 -07006314 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006315 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
6316 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006317 if (can_be_null) {
6318 __ Bind(&is_null);
6319 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006320}
6321
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006322void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006323 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006324}
6325
6326void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006327 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6328}
6329
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006330void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006331 LocationSummary* locations =
6332 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006333 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006334}
6335
6336void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006337 HBasicBlock* block = instruction->GetBlock();
6338 if (block->GetLoopInformation() != nullptr) {
6339 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6340 // The back edge will generate the suspend check.
6341 return;
6342 }
6343 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6344 // The goto will generate the suspend check.
6345 return;
6346 }
6347 GenerateSuspendCheck(instruction, nullptr);
6348}
6349
6350void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
6351 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006352 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006353 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
6354 if (slow_path == nullptr) {
6355 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
6356 instruction->SetSlowPath(slow_path);
6357 codegen_->AddSlowPath(slow_path);
6358 if (successor != nullptr) {
6359 DCHECK(successor->IsLoopHeader());
6360 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6361 }
6362 } else {
6363 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6364 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006365
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00006366 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006367 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006368 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006369 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006370 __ Bind(slow_path->GetReturnLabel());
6371 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006372 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006373 __ b(slow_path->GetEntryLabel());
6374 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006375}
6376
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006377ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
6378 return codegen_->GetAssembler();
6379}
6380
6381void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006382 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006383 Location source = move->GetSource();
6384 Location destination = move->GetDestination();
6385
6386 if (source.IsRegister()) {
6387 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006388 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006389 } else if (destination.IsFpuRegister()) {
6390 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006391 } else {
6392 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006393 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006394 SP, destination.GetStackIndex());
6395 }
6396 } else if (source.IsStackSlot()) {
6397 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006398 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006399 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006400 } else if (destination.IsFpuRegister()) {
6401 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006402 } else {
6403 DCHECK(destination.IsStackSlot());
6404 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
6405 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6406 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006407 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006408 if (destination.IsRegister()) {
6409 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
6410 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006411 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006412 } else {
6413 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006414 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
6415 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006416 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006417 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006418 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
6419 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006420 } else if (destination.IsRegisterPair()) {
6421 DCHECK(ExpectedPairLayout(destination));
6422 __ LoadFromOffset(
6423 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
6424 } else {
6425 DCHECK(destination.IsFpuRegisterPair()) << destination;
6426 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6427 SP,
6428 source.GetStackIndex());
6429 }
6430 } else if (source.IsRegisterPair()) {
6431 if (destination.IsRegisterPair()) {
6432 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6433 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006434 } else if (destination.IsFpuRegisterPair()) {
6435 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6436 source.AsRegisterPairLow<Register>(),
6437 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006438 } else {
6439 DCHECK(destination.IsDoubleStackSlot()) << destination;
6440 DCHECK(ExpectedPairLayout(source));
6441 __ StoreToOffset(
6442 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
6443 }
6444 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006445 if (destination.IsRegisterPair()) {
6446 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6447 destination.AsRegisterPairHigh<Register>(),
6448 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6449 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006450 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6451 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6452 } else {
6453 DCHECK(destination.IsDoubleStackSlot()) << destination;
6454 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
6455 SP,
6456 destination.GetStackIndex());
6457 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006458 } else {
6459 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006460 HConstant* constant = source.GetConstant();
6461 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6462 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006463 if (destination.IsRegister()) {
6464 __ LoadImmediate(destination.AsRegister<Register>(), value);
6465 } else {
6466 DCHECK(destination.IsStackSlot());
6467 __ LoadImmediate(IP, value);
6468 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6469 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006470 } else if (constant->IsLongConstant()) {
6471 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006472 if (destination.IsRegisterPair()) {
6473 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
6474 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006475 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006476 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006477 __ LoadImmediate(IP, Low32Bits(value));
6478 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6479 __ LoadImmediate(IP, High32Bits(value));
6480 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6481 }
6482 } else if (constant->IsDoubleConstant()) {
6483 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006484 if (destination.IsFpuRegisterPair()) {
6485 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006486 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006487 DCHECK(destination.IsDoubleStackSlot()) << destination;
6488 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006489 __ LoadImmediate(IP, Low32Bits(int_value));
6490 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6491 __ LoadImmediate(IP, High32Bits(int_value));
6492 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6493 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006494 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006495 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006496 float value = constant->AsFloatConstant()->GetValue();
6497 if (destination.IsFpuRegister()) {
6498 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
6499 } else {
6500 DCHECK(destination.IsStackSlot());
6501 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
6502 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6503 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006504 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006505 }
6506}
6507
6508void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
6509 __ Mov(IP, reg);
6510 __ LoadFromOffset(kLoadWord, reg, SP, mem);
6511 __ StoreToOffset(kStoreWord, IP, SP, mem);
6512}
6513
6514void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
6515 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
6516 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6517 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
6518 SP, mem1 + stack_offset);
6519 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
6520 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
6521 SP, mem2 + stack_offset);
6522 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
6523}
6524
6525void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006526 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006527 Location source = move->GetSource();
6528 Location destination = move->GetDestination();
6529
6530 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006531 DCHECK_NE(source.AsRegister<Register>(), IP);
6532 DCHECK_NE(destination.AsRegister<Register>(), IP);
6533 __ Mov(IP, source.AsRegister<Register>());
6534 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
6535 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006536 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006537 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006538 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006539 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006540 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
6541 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006542 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006543 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006544 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006545 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006546 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006547 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006548 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006549 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006550 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6551 destination.AsRegisterPairHigh<Register>(),
6552 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006553 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006554 Register low_reg = source.IsRegisterPair()
6555 ? source.AsRegisterPairLow<Register>()
6556 : destination.AsRegisterPairLow<Register>();
6557 int mem = source.IsRegisterPair()
6558 ? destination.GetStackIndex()
6559 : source.GetStackIndex();
6560 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006561 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006562 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006563 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006564 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006565 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
6566 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006567 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006568 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006569 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006570 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
6571 DRegister reg = source.IsFpuRegisterPair()
6572 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
6573 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
6574 int mem = source.IsFpuRegisterPair()
6575 ? destination.GetStackIndex()
6576 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006577 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006578 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006579 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006580 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
6581 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
6582 : destination.AsFpuRegister<SRegister>();
6583 int mem = source.IsFpuRegister()
6584 ? destination.GetStackIndex()
6585 : source.GetStackIndex();
6586
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006587 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006588 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006589 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006590 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006591 Exchange(source.GetStackIndex(), destination.GetStackIndex());
6592 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006593 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006594 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006595 }
6596}
6597
6598void ParallelMoveResolverARM::SpillScratch(int reg) {
6599 __ Push(static_cast<Register>(reg));
6600}
6601
6602void ParallelMoveResolverARM::RestoreScratch(int reg) {
6603 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006604}
6605
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006606HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
6607 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006608 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006609 case HLoadClass::LoadKind::kInvalid:
6610 LOG(FATAL) << "UNREACHABLE";
6611 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006612 case HLoadClass::LoadKind::kReferrersClass:
6613 break;
6614 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6615 DCHECK(!GetCompilerOptions().GetCompilePic());
6616 break;
6617 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6618 DCHECK(GetCompilerOptions().GetCompilePic());
6619 break;
6620 case HLoadClass::LoadKind::kBootImageAddress:
6621 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006622 case HLoadClass::LoadKind::kBssEntry:
6623 DCHECK(!Runtime::Current()->UseJitCompilation());
6624 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006625 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006626 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006627 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006628 case HLoadClass::LoadKind::kDexCacheViaMethod:
6629 break;
6630 }
6631 return desired_class_load_kind;
6632}
6633
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006634void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006635 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6636 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006637 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006638 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006639 cls,
6640 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006641 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006642 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006643 return;
6644 }
Vladimir Marko41559982017-01-06 14:04:23 +00006645 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006646
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006647 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6648 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006649 ? LocationSummary::kCallOnSlowPath
6650 : LocationSummary::kNoCall;
6651 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006652 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006653 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006654 }
6655
Vladimir Marko41559982017-01-06 14:04:23 +00006656 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006657 locations->SetInAt(0, Location::RequiresRegister());
6658 }
6659 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006660 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6661 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6662 // Rely on the type resolution or initialization and marking to save everything we need.
6663 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6664 // to the custom calling convention) or by marking, so we request a different temp.
6665 locations->AddTemp(Location::RequiresRegister());
6666 RegisterSet caller_saves = RegisterSet::Empty();
6667 InvokeRuntimeCallingConvention calling_convention;
6668 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6669 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6670 // that the the kPrimNot result register is the same as the first argument register.
6671 locations->SetCustomSlowPathCallerSaves(caller_saves);
6672 } else {
6673 // For non-Baker read barrier we have a temp-clobbering call.
6674 }
6675 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006676}
6677
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006678// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6679// move.
6680void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006681 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6682 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6683 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006684 return;
6685 }
Vladimir Marko41559982017-01-06 14:04:23 +00006686 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006687
Vladimir Marko41559982017-01-06 14:04:23 +00006688 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006689 Location out_loc = locations->Out();
6690 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006691
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006692 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6693 ? kWithoutReadBarrier
6694 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006695 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006696 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006697 case HLoadClass::LoadKind::kReferrersClass: {
6698 DCHECK(!cls->CanCallRuntime());
6699 DCHECK(!cls->MustGenerateClinitCheck());
6700 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6701 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006702 GenerateGcRootFieldLoad(cls,
6703 out_loc,
6704 current_method,
6705 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006706 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006707 break;
6708 }
6709 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006710 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006711 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006712 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6713 cls->GetTypeIndex()));
6714 break;
6715 }
6716 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006717 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006718 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006719 CodeGeneratorARM::PcRelativePatchInfo* labels =
6720 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6721 __ BindTrackedLabel(&labels->movw_label);
6722 __ movw(out, /* placeholder */ 0u);
6723 __ BindTrackedLabel(&labels->movt_label);
6724 __ movt(out, /* placeholder */ 0u);
6725 __ BindTrackedLabel(&labels->add_pc_label);
6726 __ add(out, out, ShifterOperand(PC));
6727 break;
6728 }
6729 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006730 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006731 uint32_t address = dchecked_integral_cast<uint32_t>(
6732 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6733 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006734 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6735 break;
6736 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006737 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006738 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6739 ? locations->GetTemp(0).AsRegister<Register>()
6740 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006741 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006742 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006743 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006744 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006745 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006746 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006747 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006748 __ add(temp, temp, ShifterOperand(PC));
6749 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006750 generate_null_check = true;
6751 break;
6752 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006753 case HLoadClass::LoadKind::kJitTableAddress: {
6754 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6755 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006756 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006757 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006758 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006759 break;
6760 }
Vladimir Marko41559982017-01-06 14:04:23 +00006761 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006762 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006763 LOG(FATAL) << "UNREACHABLE";
6764 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006765 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006766
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006767 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6768 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01006769 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006770 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6771 codegen_->AddSlowPath(slow_path);
6772 if (generate_null_check) {
6773 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6774 }
6775 if (cls->MustGenerateClinitCheck()) {
6776 GenerateClassInitializationCheck(slow_path, out);
6777 } else {
6778 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006779 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006780 }
6781}
6782
6783void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
6784 LocationSummary* locations =
6785 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6786 locations->SetInAt(0, Location::RequiresRegister());
6787 if (check->HasUses()) {
6788 locations->SetOut(Location::SameAsFirstInput());
6789 }
6790}
6791
6792void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006793 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006794 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006795 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006796 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006797 GenerateClassInitializationCheck(slow_path,
6798 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006799}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006800
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006801void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01006802 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006803 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
6804 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
6805 __ b(slow_path->GetEntryLabel(), LT);
6806 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6807 // properly. Therefore, we do a memory fence.
6808 __ dmb(ISH);
6809 __ Bind(slow_path->GetExitLabel());
6810}
6811
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006812HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
6813 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006814 switch (desired_string_load_kind) {
6815 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6816 DCHECK(!GetCompilerOptions().GetCompilePic());
6817 break;
6818 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6819 DCHECK(GetCompilerOptions().GetCompilePic());
6820 break;
6821 case HLoadString::LoadKind::kBootImageAddress:
6822 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006823 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006824 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006825 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006826 case HLoadString::LoadKind::kJitTableAddress:
6827 DCHECK(Runtime::Current()->UseJitCompilation());
6828 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006829 case HLoadString::LoadKind::kDexCacheViaMethod:
6830 break;
6831 }
6832 return desired_string_load_kind;
6833}
6834
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006835void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006836 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006837 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006838 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006839 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006840 locations->SetOut(Location::RegisterLocation(R0));
6841 } else {
6842 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006843 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6844 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006845 // Rely on the pResolveString and marking to save everything we need, including temps.
6846 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6847 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006848 locations->AddTemp(Location::RequiresRegister());
6849 RegisterSet caller_saves = RegisterSet::Empty();
6850 InvokeRuntimeCallingConvention calling_convention;
6851 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6852 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6853 // that the the kPrimNot result register is the same as the first argument register.
6854 locations->SetCustomSlowPathCallerSaves(caller_saves);
6855 } else {
6856 // For non-Baker read barrier we have a temp-clobbering call.
6857 }
6858 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006859 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006860}
6861
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006862// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6863// move.
6864void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006865 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006866 Location out_loc = locations->Out();
6867 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006868 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00006869
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006870 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006871 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006872 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006873 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6874 load->GetStringIndex()));
6875 return; // No dex cache slow path.
6876 }
6877 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006878 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006879 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006880 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006881 __ BindTrackedLabel(&labels->movw_label);
6882 __ movw(out, /* placeholder */ 0u);
6883 __ BindTrackedLabel(&labels->movt_label);
6884 __ movt(out, /* placeholder */ 0u);
6885 __ BindTrackedLabel(&labels->add_pc_label);
6886 __ add(out, out, ShifterOperand(PC));
6887 return; // No dex cache slow path.
6888 }
6889 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006890 uint32_t address = dchecked_integral_cast<uint32_t>(
6891 reinterpret_cast<uintptr_t>(load->GetString().Get()));
6892 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006893 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6894 return; // No dex cache slow path.
6895 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006896 case HLoadString::LoadKind::kBssEntry: {
6897 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006898 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6899 ? locations->GetTemp(0).AsRegister<Register>()
6900 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006901 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006902 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00006903 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006904 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006905 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006906 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006907 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006908 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006909 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006910 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
6911 codegen_->AddSlowPath(slow_path);
6912 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6913 __ Bind(slow_path->GetExitLabel());
6914 return;
6915 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006916 case HLoadString::LoadKind::kJitTableAddress: {
6917 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006918 load->GetStringIndex(),
6919 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006920 // /* GcRoot<mirror::String> */ out = *out
6921 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
6922 return;
6923 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006924 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006925 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006926 }
Roland Levillain3b359c72015-11-17 19:35:12 +00006927
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006928 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
6929 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
6930 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006931 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006932 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006933 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6934 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006935}
6936
David Brazdilcb1c0552015-08-04 16:22:25 +01006937static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006938 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01006939}
6940
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006941void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
6942 LocationSummary* locations =
6943 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6944 locations->SetOut(Location::RequiresRegister());
6945}
6946
6947void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006948 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01006949 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
6950}
6951
6952void LocationsBuilderARM::VisitClearException(HClearException* clear) {
6953 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6954}
6955
6956void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006957 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01006958 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006959}
6960
6961void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
6962 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006963 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006964 InvokeRuntimeCallingConvention calling_convention;
6965 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6966}
6967
6968void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01006969 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006970 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006971}
6972
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006973// Temp is used for read barrier.
6974static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6975 if (kEmitCompilerReadBarrier &&
6976 (kUseBakerReadBarrier ||
6977 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6978 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6979 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6980 return 1;
6981 }
6982 return 0;
6983}
6984
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006985// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006986// interface pointer, one for loading the current interface.
6987// The other checks have one temp for loading the object's class.
6988static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6989 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6990 return 3;
6991 }
6992 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00006993}
6994
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006995void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006996 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00006997 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006998 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00006999 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007000 case TypeCheckKind::kExactCheck:
7001 case TypeCheckKind::kAbstractClassCheck:
7002 case TypeCheckKind::kClassHierarchyCheck:
7003 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007004 call_kind =
7005 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01007006 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007007 break;
7008 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007009 case TypeCheckKind::kUnresolvedCheck:
7010 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007011 call_kind = LocationSummary::kCallOnSlowPath;
7012 break;
7013 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007014
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007015 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007016 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007017 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007018 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007019 locations->SetInAt(0, Location::RequiresRegister());
7020 locations->SetInAt(1, Location::RequiresRegister());
7021 // The "out" register is used as a temporary, so it overlaps with the inputs.
7022 // Note that TypeCheckSlowPathARM uses this register too.
7023 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007024 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007025}
7026
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007027void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007028 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007029 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007030 Location obj_loc = locations->InAt(0);
7031 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007032 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007033 Location out_loc = locations->Out();
7034 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007035 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7036 DCHECK_LE(num_temps, 1u);
7037 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007038 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007039 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7040 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7041 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Vladimir Markocf93a5c2015-06-16 11:33:24 +00007042 Label done, zero;
Anton Kirilov6f644202017-02-27 18:29:45 +00007043 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01007044 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007045
7046 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007047 // avoid null check if we know obj is not null.
7048 if (instruction->MustDoNullCheck()) {
Nicolas Geoffrayd56376c2015-05-21 12:32:34 +00007049 __ CompareAndBranchIfZero(obj, &zero);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007050 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007051
Roland Levillainc9285912015-12-18 10:38:42 +00007052 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007053 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007054 // /* HeapReference<Class> */ out = obj->klass_
7055 GenerateReferenceLoadTwoRegisters(instruction,
7056 out_loc,
7057 obj_loc,
7058 class_offset,
7059 maybe_temp_loc,
7060 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007061 __ cmp(out, ShifterOperand(cls));
7062 // Classes must be equal for the instanceof to succeed.
7063 __ b(&zero, NE);
7064 __ LoadImmediate(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00007065 __ b(final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007066 break;
7067 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007068
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007069 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007070 // /* HeapReference<Class> */ out = obj->klass_
7071 GenerateReferenceLoadTwoRegisters(instruction,
7072 out_loc,
7073 obj_loc,
7074 class_offset,
7075 maybe_temp_loc,
7076 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007077 // If the class is abstract, we eagerly fetch the super class of the
7078 // object to avoid doing a comparison we know will fail.
7079 Label loop;
7080 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007081 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007082 GenerateReferenceLoadOneRegister(instruction,
7083 out_loc,
7084 super_offset,
7085 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007086 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007087 // If `out` is null, we use it for the result, and jump to `done`.
Anton Kirilov6f644202017-02-27 18:29:45 +00007088 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007089 __ cmp(out, ShifterOperand(cls));
7090 __ b(&loop, NE);
7091 __ LoadImmediate(out, 1);
7092 if (zero.IsLinked()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007093 __ b(final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007094 }
7095 break;
7096 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007097
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007098 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007099 // /* HeapReference<Class> */ out = obj->klass_
7100 GenerateReferenceLoadTwoRegisters(instruction,
7101 out_loc,
7102 obj_loc,
7103 class_offset,
7104 maybe_temp_loc,
7105 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007106 // Walk over the class hierarchy to find a match.
7107 Label loop, success;
7108 __ Bind(&loop);
7109 __ cmp(out, ShifterOperand(cls));
7110 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007111 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007112 GenerateReferenceLoadOneRegister(instruction,
7113 out_loc,
7114 super_offset,
7115 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007116 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007117 __ CompareAndBranchIfNonZero(out, &loop);
7118 // If `out` is null, we use it for the result, and jump to `done`.
Anton Kirilov6f644202017-02-27 18:29:45 +00007119 __ b(final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007120 __ Bind(&success);
7121 __ LoadImmediate(out, 1);
7122 if (zero.IsLinked()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007123 __ b(final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007124 }
7125 break;
7126 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007127
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007128 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007129 // /* HeapReference<Class> */ out = obj->klass_
7130 GenerateReferenceLoadTwoRegisters(instruction,
7131 out_loc,
7132 obj_loc,
7133 class_offset,
7134 maybe_temp_loc,
7135 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007136 // Do an exact check.
7137 Label exact_check;
7138 __ cmp(out, ShifterOperand(cls));
7139 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007140 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007141 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007142 GenerateReferenceLoadOneRegister(instruction,
7143 out_loc,
7144 component_offset,
7145 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007146 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007147 // If `out` is null, we use it for the result, and jump to `done`.
Anton Kirilov6f644202017-02-27 18:29:45 +00007148 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007149 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7150 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7151 __ CompareAndBranchIfNonZero(out, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007152 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007153 __ LoadImmediate(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00007154 __ b(final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007155 break;
7156 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007157
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007158 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007159 // No read barrier since the slow path will retry upon failure.
7160 // /* HeapReference<Class> */ out = obj->klass_
7161 GenerateReferenceLoadTwoRegisters(instruction,
7162 out_loc,
7163 obj_loc,
7164 class_offset,
7165 maybe_temp_loc,
7166 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007167 __ cmp(out, ShifterOperand(cls));
7168 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00007169 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7170 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007171 codegen_->AddSlowPath(slow_path);
7172 __ b(slow_path->GetEntryLabel(), NE);
7173 __ LoadImmediate(out, 1);
7174 if (zero.IsLinked()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007175 __ b(final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007176 }
7177 break;
7178 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007179
Calin Juravle98893e12015-10-02 21:05:03 +01007180 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007181 case TypeCheckKind::kInterfaceCheck: {
7182 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007183 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00007184 // cases.
7185 //
7186 // We cannot directly call the InstanceofNonTrivial runtime
7187 // entry point without resorting to a type checking slow path
7188 // here (i.e. by calling InvokeRuntime directly), as it would
7189 // require to assign fixed registers for the inputs of this
7190 // HInstanceOf instruction (following the runtime calling
7191 // convention), which might be cluttered by the potential first
7192 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00007193 //
7194 // TODO: Introduce a new runtime entry point taking the object
7195 // to test (instead of its class) as argument, and let it deal
7196 // with the read barrier issues. This will let us refactor this
7197 // case of the `switch` code as it was previously (with a direct
7198 // call to the runtime not using a type checking slow path).
7199 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00007200 DCHECK(locations->OnlyCallsOnSlowPath());
7201 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7202 /* is_fatal */ false);
7203 codegen_->AddSlowPath(slow_path);
7204 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007205 if (zero.IsLinked()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007206 __ b(final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007207 }
7208 break;
7209 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007210 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007211
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007212 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007213 __ Bind(&zero);
7214 __ LoadImmediate(out, 0);
7215 }
7216
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007217 if (done.IsLinked()) {
7218 __ Bind(&done);
7219 }
7220
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007221 if (slow_path != nullptr) {
7222 __ Bind(slow_path->GetExitLabel());
7223 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007224}
7225
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007226void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007227 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7228 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7229
Roland Levillain3b359c72015-11-17 19:35:12 +00007230 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7231 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007232 case TypeCheckKind::kExactCheck:
7233 case TypeCheckKind::kAbstractClassCheck:
7234 case TypeCheckKind::kClassHierarchyCheck:
7235 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007236 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7237 LocationSummary::kCallOnSlowPath :
7238 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007239 break;
7240 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007241 case TypeCheckKind::kUnresolvedCheck:
7242 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007243 call_kind = LocationSummary::kCallOnSlowPath;
7244 break;
7245 }
7246
Roland Levillain3b359c72015-11-17 19:35:12 +00007247 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7248 locations->SetInAt(0, Location::RequiresRegister());
7249 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007250 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007251}
7252
7253void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007254 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007255 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007256 Location obj_loc = locations->InAt(0);
7257 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007258 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007259 Location temp_loc = locations->GetTemp(0);
7260 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007261 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7262 DCHECK_LE(num_temps, 3u);
7263 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7264 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7265 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7266 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7267 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7268 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7269 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7270 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7271 const uint32_t object_array_data_offset =
7272 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007273
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007274 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7275 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7276 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007277 bool is_type_check_slow_path_fatal = false;
7278 if (!kEmitCompilerReadBarrier) {
7279 is_type_check_slow_path_fatal =
7280 (type_check_kind == TypeCheckKind::kExactCheck ||
7281 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7282 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7283 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7284 !instruction->CanThrowIntoCatchBlock();
7285 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01007286 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007287 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7288 is_type_check_slow_path_fatal);
7289 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007290
7291 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007292 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007293 // Avoid null check if we know obj is not null.
7294 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007295 __ CompareAndBranchIfZero(obj, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007296 }
7297
Roland Levillain3b359c72015-11-17 19:35:12 +00007298 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007299 case TypeCheckKind::kExactCheck:
7300 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007301 // /* HeapReference<Class> */ temp = obj->klass_
7302 GenerateReferenceLoadTwoRegisters(instruction,
7303 temp_loc,
7304 obj_loc,
7305 class_offset,
7306 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007307 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007308
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007309 __ cmp(temp, ShifterOperand(cls));
7310 // Jump to slow path for throwing the exception or doing a
7311 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00007312 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007313 break;
7314 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007315
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007316 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007317 // /* HeapReference<Class> */ temp = obj->klass_
7318 GenerateReferenceLoadTwoRegisters(instruction,
7319 temp_loc,
7320 obj_loc,
7321 class_offset,
7322 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007323 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007324
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007325 // If the class is abstract, we eagerly fetch the super class of the
7326 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007327 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007328 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007329 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007330 GenerateReferenceLoadOneRegister(instruction,
7331 temp_loc,
7332 super_offset,
7333 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007334 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007335
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007336 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7337 // exception.
7338 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00007339
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007340 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007341 __ cmp(temp, ShifterOperand(cls));
7342 __ b(&loop, NE);
7343 break;
7344 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007346 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007347 // /* HeapReference<Class> */ temp = obj->klass_
7348 GenerateReferenceLoadTwoRegisters(instruction,
7349 temp_loc,
7350 obj_loc,
7351 class_offset,
7352 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007353 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007354
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007355 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007356 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007357 __ Bind(&loop);
7358 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007359 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007360
Roland Levillain3b359c72015-11-17 19:35:12 +00007361 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007362 GenerateReferenceLoadOneRegister(instruction,
7363 temp_loc,
7364 super_offset,
7365 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007366 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007367
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007368 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7369 // exception.
7370 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7371 // Otherwise, jump to the beginning of the loop.
7372 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007373 break;
7374 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007375
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007376 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007377 // /* HeapReference<Class> */ temp = obj->klass_
7378 GenerateReferenceLoadTwoRegisters(instruction,
7379 temp_loc,
7380 obj_loc,
7381 class_offset,
7382 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007383 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007384
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007385 // Do an exact check.
7386 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007387 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007388
7389 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007390 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007391 GenerateReferenceLoadOneRegister(instruction,
7392 temp_loc,
7393 component_offset,
7394 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007395 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007396 // If the component type is null, jump to the slow path to throw the exception.
7397 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7398 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7399 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007400 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00007401 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007402 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007403 break;
7404 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007405
Calin Juravle98893e12015-10-02 21:05:03 +01007406 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007407 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00007408 // We cannot directly call the CheckCast runtime entry point
7409 // without resorting to a type checking slow path here (i.e. by
7410 // calling InvokeRuntime directly), as it would require to
7411 // assign fixed registers for the inputs of this HInstanceOf
7412 // instruction (following the runtime calling convention), which
7413 // might be cluttered by the potential first read barrier
7414 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007415
Roland Levillain3b359c72015-11-17 19:35:12 +00007416 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007417 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007418
7419 case TypeCheckKind::kInterfaceCheck: {
7420 // Avoid read barriers to improve performance of the fast path. We can not get false
7421 // positives by doing this.
7422 // /* HeapReference<Class> */ temp = obj->klass_
7423 GenerateReferenceLoadTwoRegisters(instruction,
7424 temp_loc,
7425 obj_loc,
7426 class_offset,
7427 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007428 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007429
7430 // /* HeapReference<Class> */ temp = temp->iftable_
7431 GenerateReferenceLoadTwoRegisters(instruction,
7432 temp_loc,
7433 temp_loc,
7434 iftable_offset,
7435 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007436 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007437 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007438 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08007439 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007440 Label start_loop;
7441 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007442 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
7443 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007444 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
7445 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007446 // Go to next interface.
7447 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
7448 __ sub(maybe_temp2_loc.AsRegister<Register>(),
7449 maybe_temp2_loc.AsRegister<Register>(),
7450 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007451 // Compare the classes and continue the loop if they do not match.
7452 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
7453 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007454 break;
7455 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007456 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007457
7458 if (done.IsLinked()) {
7459 __ Bind(&done);
7460 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007461
Roland Levillain3b359c72015-11-17 19:35:12 +00007462 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007463}
7464
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007465void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
7466 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007467 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007468 InvokeRuntimeCallingConvention calling_convention;
7469 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7470}
7471
7472void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007473 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7474 instruction,
7475 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007476 if (instruction->IsEnter()) {
7477 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7478 } else {
7479 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7480 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007481}
7482
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007483void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
7484void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
7485void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007486
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007487void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007488 LocationSummary* locations =
7489 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7490 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7491 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007492 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007493 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007494 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00007495 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007496}
7497
7498void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
7499 HandleBitwiseOperation(instruction);
7500}
7501
7502void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
7503 HandleBitwiseOperation(instruction);
7504}
7505
7506void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
7507 HandleBitwiseOperation(instruction);
7508}
7509
Artem Serov7fc63502016-02-09 17:15:29 +00007510
7511void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7512 LocationSummary* locations =
7513 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7514 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7515 || instruction->GetResultType() == Primitive::kPrimLong);
7516
7517 locations->SetInAt(0, Location::RequiresRegister());
7518 locations->SetInAt(1, Location::RequiresRegister());
7519 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7520}
7521
7522void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7523 LocationSummary* locations = instruction->GetLocations();
7524 Location first = locations->InAt(0);
7525 Location second = locations->InAt(1);
7526 Location out = locations->Out();
7527
7528 if (instruction->GetResultType() == Primitive::kPrimInt) {
7529 Register first_reg = first.AsRegister<Register>();
7530 ShifterOperand second_reg(second.AsRegister<Register>());
7531 Register out_reg = out.AsRegister<Register>();
7532
7533 switch (instruction->GetOpKind()) {
7534 case HInstruction::kAnd:
7535 __ bic(out_reg, first_reg, second_reg);
7536 break;
7537 case HInstruction::kOr:
7538 __ orn(out_reg, first_reg, second_reg);
7539 break;
7540 // There is no EON on arm.
7541 case HInstruction::kXor:
7542 default:
7543 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7544 UNREACHABLE();
7545 }
7546 return;
7547
7548 } else {
7549 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7550 Register first_low = first.AsRegisterPairLow<Register>();
7551 Register first_high = first.AsRegisterPairHigh<Register>();
7552 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7553 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7554 Register out_low = out.AsRegisterPairLow<Register>();
7555 Register out_high = out.AsRegisterPairHigh<Register>();
7556
7557 switch (instruction->GetOpKind()) {
7558 case HInstruction::kAnd:
7559 __ bic(out_low, first_low, second_low);
7560 __ bic(out_high, first_high, second_high);
7561 break;
7562 case HInstruction::kOr:
7563 __ orn(out_low, first_low, second_low);
7564 __ orn(out_high, first_high, second_high);
7565 break;
7566 // There is no EON on arm.
7567 case HInstruction::kXor:
7568 default:
7569 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7570 UNREACHABLE();
7571 }
7572 }
7573}
7574
Anton Kirilov74234da2017-01-13 14:42:47 +00007575void LocationsBuilderARM::VisitDataProcWithShifterOp(
7576 HDataProcWithShifterOp* instruction) {
7577 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
7578 instruction->GetType() == Primitive::kPrimLong);
7579 LocationSummary* locations =
7580 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7581 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
7582 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7583
7584 locations->SetInAt(0, Location::RequiresRegister());
7585 locations->SetInAt(1, Location::RequiresRegister());
7586 locations->SetOut(Location::RequiresRegister(),
7587 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7588}
7589
7590void InstructionCodeGeneratorARM::VisitDataProcWithShifterOp(
7591 HDataProcWithShifterOp* instruction) {
7592 const LocationSummary* const locations = instruction->GetLocations();
7593 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
7594 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
7595 const Location left = locations->InAt(0);
7596 const Location right = locations->InAt(1);
7597 const Location out = locations->Out();
7598
7599 if (instruction->GetType() == Primitive::kPrimInt) {
7600 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
7601
7602 const Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
7603 ? right.AsRegisterPairLow<Register>()
7604 : right.AsRegister<Register>();
7605
7606 GenerateDataProcInstruction(kind,
7607 out.AsRegister<Register>(),
7608 left.AsRegister<Register>(),
7609 ShifterOperand(second,
7610 ShiftFromOpKind(op_kind),
7611 instruction->GetShiftAmount()),
7612 codegen_);
7613 } else {
7614 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7615
7616 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7617 const Register second = right.AsRegister<Register>();
7618
7619 DCHECK_NE(out.AsRegisterPairLow<Register>(), second);
7620 GenerateDataProc(kind,
7621 out,
7622 left,
7623 ShifterOperand(second),
7624 ShifterOperand(second, ASR, 31),
7625 codegen_);
7626 } else {
7627 GenerateLongDataProc(instruction, codegen_);
7628 }
7629 }
7630}
7631
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007632void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
7633 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7634 if (value == 0xffffffffu) {
7635 if (out != first) {
7636 __ mov(out, ShifterOperand(first));
7637 }
7638 return;
7639 }
7640 if (value == 0u) {
7641 __ mov(out, ShifterOperand(0));
7642 return;
7643 }
7644 ShifterOperand so;
7645 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
7646 __ and_(out, first, so);
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007647 } else if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007648 __ bic(out, first, ShifterOperand(~value));
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007649 } else {
7650 DCHECK(IsPowerOfTwo(value + 1));
7651 __ ubfx(out, first, 0, WhichPowerOf2(value + 1));
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007652 }
7653}
7654
7655void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
7656 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7657 if (value == 0u) {
7658 if (out != first) {
7659 __ mov(out, ShifterOperand(first));
7660 }
7661 return;
7662 }
7663 if (value == 0xffffffffu) {
7664 __ mvn(out, ShifterOperand(0));
7665 return;
7666 }
7667 ShifterOperand so;
7668 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
7669 __ orr(out, first, so);
7670 } else {
7671 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
7672 __ orn(out, first, ShifterOperand(~value));
7673 }
7674}
7675
7676void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
7677 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7678 if (value == 0u) {
7679 if (out != first) {
7680 __ mov(out, ShifterOperand(first));
7681 }
7682 return;
7683 }
7684 __ eor(out, first, ShifterOperand(value));
7685}
7686
Vladimir Marko59751a72016-08-05 14:37:27 +01007687void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
7688 Location first,
7689 uint64_t value) {
7690 Register out_low = out.AsRegisterPairLow<Register>();
7691 Register out_high = out.AsRegisterPairHigh<Register>();
7692 Register first_low = first.AsRegisterPairLow<Register>();
7693 Register first_high = first.AsRegisterPairHigh<Register>();
7694 uint32_t value_low = Low32Bits(value);
7695 uint32_t value_high = High32Bits(value);
7696 if (value_low == 0u) {
7697 if (out_low != first_low) {
7698 __ mov(out_low, ShifterOperand(first_low));
7699 }
7700 __ AddConstant(out_high, first_high, value_high);
7701 return;
7702 }
7703 __ AddConstantSetFlags(out_low, first_low, value_low);
7704 ShifterOperand so;
7705 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
7706 __ adc(out_high, first_high, so);
7707 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
7708 __ sbc(out_high, first_high, so);
7709 } else {
7710 LOG(FATAL) << "Unexpected constant " << value_high;
7711 UNREACHABLE();
7712 }
7713}
7714
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007715void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
7716 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007717 Location first = locations->InAt(0);
7718 Location second = locations->InAt(1);
7719 Location out = locations->Out();
7720
7721 if (second.IsConstant()) {
7722 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7723 uint32_t value_low = Low32Bits(value);
7724 if (instruction->GetResultType() == Primitive::kPrimInt) {
7725 Register first_reg = first.AsRegister<Register>();
7726 Register out_reg = out.AsRegister<Register>();
7727 if (instruction->IsAnd()) {
7728 GenerateAndConst(out_reg, first_reg, value_low);
7729 } else if (instruction->IsOr()) {
7730 GenerateOrrConst(out_reg, first_reg, value_low);
7731 } else {
7732 DCHECK(instruction->IsXor());
7733 GenerateEorConst(out_reg, first_reg, value_low);
7734 }
7735 } else {
7736 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7737 uint32_t value_high = High32Bits(value);
7738 Register first_low = first.AsRegisterPairLow<Register>();
7739 Register first_high = first.AsRegisterPairHigh<Register>();
7740 Register out_low = out.AsRegisterPairLow<Register>();
7741 Register out_high = out.AsRegisterPairHigh<Register>();
7742 if (instruction->IsAnd()) {
7743 GenerateAndConst(out_low, first_low, value_low);
7744 GenerateAndConst(out_high, first_high, value_high);
7745 } else if (instruction->IsOr()) {
7746 GenerateOrrConst(out_low, first_low, value_low);
7747 GenerateOrrConst(out_high, first_high, value_high);
7748 } else {
7749 DCHECK(instruction->IsXor());
7750 GenerateEorConst(out_low, first_low, value_low);
7751 GenerateEorConst(out_high, first_high, value_high);
7752 }
7753 }
7754 return;
7755 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007756
7757 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007758 Register first_reg = first.AsRegister<Register>();
7759 ShifterOperand second_reg(second.AsRegister<Register>());
7760 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007761 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007762 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007763 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007764 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007765 } else {
7766 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007767 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007768 }
7769 } else {
7770 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007771 Register first_low = first.AsRegisterPairLow<Register>();
7772 Register first_high = first.AsRegisterPairHigh<Register>();
7773 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7774 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7775 Register out_low = out.AsRegisterPairLow<Register>();
7776 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007777 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007778 __ and_(out_low, first_low, second_low);
7779 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007780 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007781 __ orr(out_low, first_low, second_low);
7782 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007783 } else {
7784 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007785 __ eor(out_low, first_low, second_low);
7786 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007787 }
7788 }
7789}
7790
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007791void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
7792 HInstruction* instruction,
7793 Location out,
7794 uint32_t offset,
7795 Location maybe_temp,
7796 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007797 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007798 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007799 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007800 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007801 if (kUseBakerReadBarrier) {
7802 // Load with fast path based Baker's read barrier.
7803 // /* HeapReference<Object> */ out = *(out + offset)
7804 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007805 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007806 } else {
7807 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007808 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00007809 // in the following move operation, as we will need it for the
7810 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007811 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00007812 // /* HeapReference<Object> */ out = *(out + offset)
7813 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007814 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00007815 }
7816 } else {
7817 // Plain load with no read barrier.
7818 // /* HeapReference<Object> */ out = *(out + offset)
7819 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7820 __ MaybeUnpoisonHeapReference(out_reg);
7821 }
7822}
7823
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007824void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
7825 HInstruction* instruction,
7826 Location out,
7827 Location obj,
7828 uint32_t offset,
7829 Location maybe_temp,
7830 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007831 Register out_reg = out.AsRegister<Register>();
7832 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007833 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007834 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007835 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007836 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007837 // Load with fast path based Baker's read barrier.
7838 // /* HeapReference<Object> */ out = *(obj + offset)
7839 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007840 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007841 } else {
7842 // Load with slow path based read barrier.
7843 // /* HeapReference<Object> */ out = *(obj + offset)
7844 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7845 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7846 }
7847 } else {
7848 // Plain load with no read barrier.
7849 // /* HeapReference<Object> */ out = *(obj + offset)
7850 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7851 __ MaybeUnpoisonHeapReference(out_reg);
7852 }
7853}
7854
7855void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
7856 Location root,
7857 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007858 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007859 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007860 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007861 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007862 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007863 if (kUseBakerReadBarrier) {
7864 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00007865 // Baker's read barrier are used.
Roland Levillainc9285912015-12-18 10:38:42 +00007866 //
Roland Levillainba650a42017-03-06 13:52:32 +00007867 // Note that we do not actually check the value of
7868 // `GetIsGcMarking()` to decide whether to mark the loaded GC
7869 // root or not. Instead, we load into `temp` the read barrier
7870 // mark entry point corresponding to register `root`. If `temp`
7871 // is null, it means that `GetIsGcMarking()` is false, and vice
7872 // versa.
7873 //
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007874 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00007875 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7876 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
7877 // // Slow path.
7878 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
Roland Levillainc9285912015-12-18 10:38:42 +00007879 // }
7880
Roland Levillainba650a42017-03-06 13:52:32 +00007881 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
7882 Location temp = Location::RegisterLocation(LR);
7883 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
7884 instruction, root, /* entrypoint */ temp);
7885 codegen_->AddSlowPath(slow_path);
7886
7887 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7888 const int32_t entry_point_offset =
7889 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
7890 // Loading the entrypoint does not require a load acquire since it is only changed when
7891 // threads are suspended or running a checkpoint.
7892 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
7893
Roland Levillainc9285912015-12-18 10:38:42 +00007894 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7895 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7896 static_assert(
7897 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7898 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7899 "have different sizes.");
7900 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7901 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7902 "have different sizes.");
7903
Mathieu Chartierfe814e82016-11-09 14:32:49 -08007904 // The entrypoint is null when the GC is not marking, this prevents one load compared to
7905 // checking GetIsGcMarking.
7906 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainc9285912015-12-18 10:38:42 +00007907 __ Bind(slow_path->GetExitLabel());
7908 } else {
7909 // GC root loaded through a slow path for read barriers other
7910 // than Baker's.
7911 // /* GcRoot<mirror::Object>* */ root = obj + offset
7912 __ AddConstant(root_reg, obj, offset);
7913 // /* mirror::Object* */ root = root->Read()
7914 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7915 }
7916 } else {
7917 // Plain GC root load with no read barrier.
7918 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7919 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7920 // Note that GC roots are not affected by heap poisoning, thus we
7921 // do not have to unpoison `root_reg` here.
7922 }
7923}
7924
7925void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7926 Location ref,
7927 Register obj,
7928 uint32_t offset,
7929 Location temp,
7930 bool needs_null_check) {
7931 DCHECK(kEmitCompilerReadBarrier);
7932 DCHECK(kUseBakerReadBarrier);
7933
7934 // /* HeapReference<Object> */ ref = *(obj + offset)
7935 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01007936 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00007937 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007938 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007939}
7940
7941void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7942 Location ref,
7943 Register obj,
7944 uint32_t data_offset,
7945 Location index,
7946 Location temp,
7947 bool needs_null_check) {
7948 DCHECK(kEmitCompilerReadBarrier);
7949 DCHECK(kUseBakerReadBarrier);
7950
Roland Levillainbfea3352016-06-23 13:48:47 +01007951 static_assert(
7952 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7953 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00007954 // /* HeapReference<Object> */ ref =
7955 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Roland Levillainbfea3352016-06-23 13:48:47 +01007956 ScaleFactor scale_factor = TIMES_4;
Roland Levillainc9285912015-12-18 10:38:42 +00007957 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01007958 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00007959}
7960
7961void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7962 Location ref,
7963 Register obj,
7964 uint32_t offset,
7965 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01007966 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00007967 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007968 bool needs_null_check,
7969 bool always_update_field,
7970 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00007971 DCHECK(kEmitCompilerReadBarrier);
7972 DCHECK(kUseBakerReadBarrier);
7973
Roland Levillain54f869e2017-03-06 13:54:11 +00007974 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
7975 // whether we need to enter the slow path to mark the reference.
7976 // Then, in the slow path, check the gray bit in the lock word of
7977 // the reference's holder (`obj`) to decide whether to mark `ref` or
7978 // not.
Roland Levillainc9285912015-12-18 10:38:42 +00007979 //
Roland Levillainba650a42017-03-06 13:52:32 +00007980 // Note that we do not actually check the value of `GetIsGcMarking()`;
7981 // instead, we load into `temp3` the read barrier mark entry point
7982 // corresponding to register `ref`. If `temp3` is null, it means
7983 // that `GetIsGcMarking()` is false, and vice versa.
7984 //
7985 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00007986 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
7987 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00007988 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7989 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7990 // HeapReference<mirror::Object> ref = *src; // Original reference load.
7991 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7992 // if (is_gray) {
7993 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
7994 // }
7995 // } else {
7996 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillainc9285912015-12-18 10:38:42 +00007997 // }
Roland Levillainc9285912015-12-18 10:38:42 +00007998
Roland Levillain35345a52017-02-27 14:32:08 +00007999 Register temp_reg = temp.AsRegister<Register>();
Roland Levillain1372c9f2017-01-13 11:47:39 +00008000
Roland Levillainba650a42017-03-06 13:52:32 +00008001 // Slow path marking the object `ref` when the GC is marking. The
8002 // entrypoint will already be loaded in `temp3`.
8003 Location temp3 = Location::RegisterLocation(LR);
8004 SlowPathCodeARM* slow_path;
8005 if (always_update_field) {
8006 DCHECK(temp2 != nullptr);
Roland Levillain54f869e2017-03-06 13:54:11 +00008007 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM only
8008 // supports address of the form `obj + field_offset`, where `obj`
8009 // is a register and `field_offset` is a register pair (of which
8010 // only the lower half is used). Thus `offset` and `scale_factor`
8011 // above are expected to be null in this code path.
Roland Levillainba650a42017-03-06 13:52:32 +00008012 DCHECK_EQ(offset, 0u);
8013 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Roland Levillain54f869e2017-03-06 13:54:11 +00008014 Location field_offset = index;
8015 slow_path =
8016 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(
8017 instruction,
8018 ref,
8019 obj,
8020 offset,
8021 /* index */ field_offset,
8022 scale_factor,
8023 needs_null_check,
8024 temp_reg,
8025 *temp2,
8026 /* entrypoint */ temp3);
Roland Levillainba650a42017-03-06 13:52:32 +00008027 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00008028 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM(
8029 instruction,
8030 ref,
8031 obj,
8032 offset,
8033 index,
8034 scale_factor,
8035 needs_null_check,
8036 temp_reg,
8037 /* entrypoint */ temp3);
Roland Levillain35345a52017-02-27 14:32:08 +00008038 }
Roland Levillainba650a42017-03-06 13:52:32 +00008039 AddSlowPath(slow_path);
Roland Levillain35345a52017-02-27 14:32:08 +00008040
Roland Levillainba650a42017-03-06 13:52:32 +00008041 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8042 const int32_t entry_point_offset =
8043 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8044 // Loading the entrypoint does not require a load acquire since it is only changed when
8045 // threads are suspended or running a checkpoint.
8046 __ LoadFromOffset(kLoadWord, temp3.AsRegister<Register>(), TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008047 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8048 // checking GetIsGcMarking.
8049 __ CompareAndBranchIfNonZero(temp3.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00008050 // Fast path: just load the reference.
8051 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
Roland Levillainba650a42017-03-06 13:52:32 +00008052 __ Bind(slow_path->GetExitLabel());
8053}
Roland Levillain35345a52017-02-27 14:32:08 +00008054
Roland Levillainba650a42017-03-06 13:52:32 +00008055void CodeGeneratorARM::GenerateRawReferenceLoad(HInstruction* instruction,
8056 Location ref,
8057 Register obj,
8058 uint32_t offset,
8059 Location index,
8060 ScaleFactor scale_factor,
8061 bool needs_null_check) {
8062 Register ref_reg = ref.AsRegister<Register>();
8063
Roland Levillainc9285912015-12-18 10:38:42 +00008064 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008065 // Load types involving an "index": ArrayGet,
8066 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8067 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008068 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00008069 if (index.IsConstant()) {
8070 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01008071 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00008072 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
8073 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01008074 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008075 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8076 // intrinsics, which use a register pair as index ("long
8077 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01008078 Register index_reg = index.IsRegisterPair()
8079 ? index.AsRegisterPairLow<Register>()
8080 : index.AsRegister<Register>();
8081 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00008082 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
8083 }
8084 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008085 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillainc9285912015-12-18 10:38:42 +00008086 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
8087 }
8088
Roland Levillainba650a42017-03-06 13:52:32 +00008089 if (needs_null_check) {
8090 MaybeRecordImplicitNullCheck(instruction);
8091 }
8092
Roland Levillainc9285912015-12-18 10:38:42 +00008093 // Object* ref = ref_addr->AsMirrorPtr()
8094 __ MaybeUnpoisonHeapReference(ref_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00008095}
8096
8097void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
8098 Location out,
8099 Location ref,
8100 Location obj,
8101 uint32_t offset,
8102 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008103 DCHECK(kEmitCompilerReadBarrier);
8104
Roland Levillainc9285912015-12-18 10:38:42 +00008105 // Insert a slow path based read barrier *after* the reference load.
8106 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008107 // If heap poisoning is enabled, the unpoisoning of the loaded
8108 // reference will be carried out by the runtime within the slow
8109 // path.
8110 //
8111 // Note that `ref` currently does not get unpoisoned (when heap
8112 // poisoning is enabled), which is alright as the `ref` argument is
8113 // not used by the artReadBarrierSlow entry point.
8114 //
8115 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008116 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00008117 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
8118 AddSlowPath(slow_path);
8119
Roland Levillain3b359c72015-11-17 19:35:12 +00008120 __ b(slow_path->GetEntryLabel());
8121 __ Bind(slow_path->GetExitLabel());
8122}
8123
Roland Levillainc9285912015-12-18 10:38:42 +00008124void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8125 Location out,
8126 Location ref,
8127 Location obj,
8128 uint32_t offset,
8129 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008130 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00008131 // Baker's read barriers shall be handled by the fast path
8132 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
8133 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00008134 // If heap poisoning is enabled, unpoisoning will be taken care of
8135 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00008136 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00008137 } else if (kPoisonHeapReferences) {
8138 __ UnpoisonHeapReference(out.AsRegister<Register>());
8139 }
8140}
8141
Roland Levillainc9285912015-12-18 10:38:42 +00008142void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8143 Location out,
8144 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008145 DCHECK(kEmitCompilerReadBarrier);
8146
Roland Levillainc9285912015-12-18 10:38:42 +00008147 // Insert a slow path based read barrier *after* the GC root load.
8148 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008149 // Note that GC roots are not affected by heap poisoning, so we do
8150 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008151 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00008152 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
8153 AddSlowPath(slow_path);
8154
Roland Levillain3b359c72015-11-17 19:35:12 +00008155 __ b(slow_path->GetEntryLabel());
8156 __ Bind(slow_path->GetExitLabel());
8157}
8158
Vladimir Markodc151b22015-10-15 18:02:30 +01008159HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
8160 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008161 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008162 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01008163}
8164
Vladimir Markob4536b72015-11-24 13:45:23 +00008165Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
8166 Register temp) {
8167 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8168 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8169 if (!invoke->GetLocations()->Intrinsified()) {
8170 return location.AsRegister<Register>();
8171 }
8172 // For intrinsics we allow any location, so it may be on the stack.
8173 if (!location.IsRegister()) {
8174 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
8175 return temp;
8176 }
8177 // For register locations, check if the register was saved. If so, get it from the stack.
8178 // Note: There is a chance that the register was saved but not overwritten, so we could
8179 // save one load. However, since this is just an intrinsic slow path we prefer this
8180 // simple and more robust approach rather that trying to determine if that's the case.
8181 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08008182 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00008183 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
8184 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
8185 return temp;
8186 }
8187 return location.AsRegister<Register>();
8188}
8189
TatWai Chongd8c052a2016-11-02 16:12:48 +08008190Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
8191 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00008192 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
8193 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008194 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8195 uint32_t offset =
8196 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00008197 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008198 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00008199 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008200 }
Vladimir Marko58155012015-08-19 12:49:41 +00008201 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00008202 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008203 break;
8204 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8205 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
8206 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00008207 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
8208 HArmDexCacheArraysBase* base =
8209 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
8210 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
8211 temp.AsRegister<Register>());
8212 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
8213 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
8214 break;
8215 }
Vladimir Marko58155012015-08-19 12:49:41 +00008216 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00008217 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008218 Register method_reg;
8219 Register reg = temp.AsRegister<Register>();
8220 if (current_method.IsRegister()) {
8221 method_reg = current_method.AsRegister<Register>();
8222 } else {
8223 DCHECK(invoke->GetLocations()->Intrinsified());
8224 DCHECK(!current_method.IsValid());
8225 method_reg = reg;
8226 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
8227 }
Roland Levillain3b359c72015-11-17 19:35:12 +00008228 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
8229 __ LoadFromOffset(kLoadWord,
8230 reg,
8231 method_reg,
8232 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01008233 // temp = temp[index_in_cache];
8234 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
8235 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00008236 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
8237 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01008238 }
Vladimir Marko58155012015-08-19 12:49:41 +00008239 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08008240 return callee_method;
8241}
8242
8243void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
8244 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00008245
8246 switch (invoke->GetCodePtrLocation()) {
8247 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
8248 __ bl(GetFrameEntryLabel());
8249 break;
Vladimir Marko58155012015-08-19 12:49:41 +00008250 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8251 // LR = callee_method->entry_point_from_quick_compiled_code_
8252 __ LoadFromOffset(
8253 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07008254 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00008255 // LR()
8256 __ blx(LR);
8257 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08008258 }
8259
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08008260 DCHECK(!IsLeafMethod());
8261}
8262
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008263void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
8264 Register temp = temp_location.AsRegister<Register>();
8265 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8266 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00008267
8268 // Use the calling convention instead of the location of the receiver, as
8269 // intrinsics may have put the receiver in a different register. In the intrinsics
8270 // slow path, the arguments have been moved to the right place, so here we are
8271 // guaranteed that the receiver is the first register of the calling convention.
8272 InvokeDexCallingConvention calling_convention;
8273 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008274 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00008275 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00008276 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008277 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00008278 // Instead of simply (possibly) unpoisoning `temp` here, we should
8279 // emit a read barrier for the previous class reference load.
8280 // However this is not required in practice, as this is an
8281 // intermediate/temporary reference and because the current
8282 // concurrent copying collector keeps the from-space memory
8283 // intact/accessible until the end of the marking phase (the
8284 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008285 __ MaybeUnpoisonHeapReference(temp);
8286 // temp = temp->GetMethodAt(method_offset);
8287 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07008288 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008289 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
8290 // LR = temp->GetEntryPoint();
8291 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
8292 // LR();
8293 __ blx(LR);
8294}
8295
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008296CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008297 const DexFile& dex_file, dex::StringIndex string_index) {
8298 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008299}
8300
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008301CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08008302 const DexFile& dex_file, dex::TypeIndex type_index) {
8303 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008304}
8305
Vladimir Marko1998cd02017-01-13 13:02:58 +00008306CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
8307 const DexFile& dex_file, dex::TypeIndex type_index) {
8308 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8309}
8310
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008311CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
8312 const DexFile& dex_file, uint32_t element_offset) {
8313 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
8314}
8315
8316CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
8317 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8318 patches->emplace_back(dex_file, offset_or_index);
8319 return &patches->back();
8320}
8321
8322Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008323 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008324 return boot_image_string_patches_.GetOrCreate(
8325 StringReference(&dex_file, string_index),
8326 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8327}
8328
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008329Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08008330 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008331 return boot_image_type_patches_.GetOrCreate(
8332 TypeReference(&dex_file, type_index),
8333 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8334}
8335
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008336Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00008337 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008338}
8339
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008340Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008341 dex::StringIndex string_index,
8342 Handle<mirror::String> handle) {
8343 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
8344 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008345 return jit_string_patches_.GetOrCreate(
8346 StringReference(&dex_file, string_index),
8347 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8348}
8349
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008350Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
8351 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008352 Handle<mirror::Class> handle) {
8353 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
8354 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008355 return jit_class_patches_.GetOrCreate(
8356 TypeReference(&dex_file, type_index),
8357 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8358}
8359
Vladimir Markoaad75c62016-10-03 08:46:48 +00008360template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
8361inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
8362 const ArenaDeque<PcRelativePatchInfo>& infos,
8363 ArenaVector<LinkerPatch>* linker_patches) {
8364 for (const PcRelativePatchInfo& info : infos) {
8365 const DexFile& dex_file = info.target_dex_file;
8366 size_t offset_or_index = info.offset_or_index;
8367 DCHECK(info.add_pc_label.IsBound());
8368 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
8369 // Add MOVW patch.
8370 DCHECK(info.movw_label.IsBound());
8371 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
8372 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
8373 // Add MOVT patch.
8374 DCHECK(info.movt_label.IsBound());
8375 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
8376 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
8377 }
8378}
8379
Vladimir Marko58155012015-08-19 12:49:41 +00008380void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
8381 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00008382 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00008383 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008384 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008385 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008386 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008387 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Richard Uhlerc52f3032017-03-02 13:45:45 +00008388 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00008389 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008390 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
8391 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008392 for (const auto& entry : boot_image_string_patches_) {
8393 const StringReference& target_string = entry.first;
8394 Literal* literal = entry.second;
8395 DCHECK(literal->GetLabel()->IsBound());
8396 uint32_t literal_offset = literal->GetLabel()->Position();
8397 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
8398 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008399 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008400 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00008401 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00008402 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00008403 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
8404 linker_patches);
8405 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008406 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
8407 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008408 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
8409 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008410 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008411 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
8412 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008413 for (const auto& entry : boot_image_type_patches_) {
8414 const TypeReference& target_type = entry.first;
8415 Literal* literal = entry.second;
8416 DCHECK(literal->GetLabel()->IsBound());
8417 uint32_t literal_offset = literal->GetLabel()->Position();
8418 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
8419 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08008420 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008421 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008422 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008423}
8424
8425Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
8426 return map->GetOrCreate(
8427 value,
8428 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00008429}
8430
8431Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
8432 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008433 return map->GetOrCreate(
8434 target_method,
8435 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00008436}
8437
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03008438void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8439 LocationSummary* locations =
8440 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
8441 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
8442 Location::RequiresRegister());
8443 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
8444 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
8445 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8446}
8447
8448void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8449 LocationSummary* locations = instr->GetLocations();
8450 Register res = locations->Out().AsRegister<Register>();
8451 Register accumulator =
8452 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
8453 Register mul_left =
8454 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
8455 Register mul_right =
8456 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
8457
8458 if (instr->GetOpKind() == HInstruction::kAdd) {
8459 __ mla(res, mul_left, mul_right, accumulator);
8460 } else {
8461 __ mls(res, mul_left, mul_right, accumulator);
8462 }
8463}
8464
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008465void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008466 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008467 LOG(FATAL) << "Unreachable";
8468}
8469
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008470void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008471 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008472 LOG(FATAL) << "Unreachable";
8473}
8474
Mark Mendellfe57faa2015-09-18 09:26:15 -04008475// Simple implementation of packed switch - generate cascaded compare/jumps.
8476void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8477 LocationSummary* locations =
8478 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8479 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008480 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008481 codegen_->GetAssembler()->IsThumb()) {
8482 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
8483 if (switch_instr->GetStartValue() != 0) {
8484 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
8485 }
8486 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008487}
8488
8489void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8490 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008491 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04008492 LocationSummary* locations = switch_instr->GetLocations();
8493 Register value_reg = locations->InAt(0).AsRegister<Register>();
8494 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8495
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008496 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008497 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008498 Register temp_reg = IP;
8499 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
8500 // the immediate, because IP is used as the destination register. For the other
8501 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
8502 // and they can be encoded in the instruction without making use of IP register.
8503 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
8504
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008505 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008506 // Jump to successors[0] if value == lower_bound.
8507 __ b(codegen_->GetLabelOf(successors[0]), EQ);
8508 int32_t last_index = 0;
8509 for (; num_entries - last_index > 2; last_index += 2) {
8510 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
8511 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8512 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
8513 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8514 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
8515 }
8516 if (num_entries - last_index == 2) {
8517 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00008518 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008519 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008520 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008521
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008522 // And the default for any other value.
8523 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
8524 __ b(codegen_->GetLabelOf(default_block));
8525 }
8526 } else {
8527 // Create a table lookup.
8528 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8529
8530 // Materialize a pointer to the switch table
8531 std::vector<Label*> labels(num_entries);
8532 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
8533 for (uint32_t i = 0; i < num_entries; i++) {
8534 labels[i] = codegen_->GetLabelOf(successors[i]);
8535 }
8536 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
8537
8538 // Remove the bias.
8539 Register key_reg;
8540 if (lower_bound != 0) {
8541 key_reg = locations->GetTemp(1).AsRegister<Register>();
8542 __ AddConstant(key_reg, value_reg, -lower_bound);
8543 } else {
8544 key_reg = value_reg;
8545 }
8546
8547 // Check whether the value is in the table, jump to default block if not.
8548 __ CmpConstant(key_reg, num_entries - 1);
8549 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
8550
8551 // Load the displacement from the table.
8552 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
8553
8554 // Dispatch is a direct add to the PC (for Thumb2).
8555 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008556 }
8557}
8558
Vladimir Markob4536b72015-11-24 13:45:23 +00008559void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8560 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8561 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00008562}
8563
8564void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8565 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008566 CodeGeneratorARM::PcRelativePatchInfo* labels =
8567 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00008568 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008569 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008570 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008571 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008572 __ BindTrackedLabel(&labels->add_pc_label);
8573 __ add(base_reg, base_reg, ShifterOperand(PC));
8574}
8575
Andreas Gampe85b62f22015-09-09 13:15:38 -07008576void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
8577 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00008578 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008579 return;
8580 }
8581
8582 DCHECK_NE(type, Primitive::kPrimVoid);
8583
8584 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
8585 if (return_loc.Equals(trg)) {
8586 return;
8587 }
8588
8589 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8590 // with the last branch.
8591 if (type == Primitive::kPrimLong) {
8592 HParallelMove parallel_move(GetGraph()->GetArena());
8593 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
8594 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
8595 GetMoveResolver()->EmitNativeCode(&parallel_move);
8596 } else if (type == Primitive::kPrimDouble) {
8597 HParallelMove parallel_move(GetGraph()->GetArena());
8598 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
8599 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
8600 GetMoveResolver()->EmitNativeCode(&parallel_move);
8601 } else {
8602 // Let the parallel move resolver take care of all of this.
8603 HParallelMove parallel_move(GetGraph()->GetArena());
8604 parallel_move.AddMove(return_loc, trg, type, nullptr);
8605 GetMoveResolver()->EmitNativeCode(&parallel_move);
8606 }
8607}
8608
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008609void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
8610 LocationSummary* locations =
8611 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8612 locations->SetInAt(0, Location::RequiresRegister());
8613 locations->SetOut(Location::RequiresRegister());
8614}
8615
8616void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
8617 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008618 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008619 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008620 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008621 __ LoadFromOffset(kLoadWord,
8622 locations->Out().AsRegister<Register>(),
8623 locations->InAt(0).AsRegister<Register>(),
8624 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008625 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008626 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008627 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008628 __ LoadFromOffset(kLoadWord,
8629 locations->Out().AsRegister<Register>(),
8630 locations->InAt(0).AsRegister<Register>(),
8631 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
8632 __ LoadFromOffset(kLoadWord,
8633 locations->Out().AsRegister<Register>(),
8634 locations->Out().AsRegister<Register>(),
8635 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008636 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008637}
8638
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008639static void PatchJitRootUse(uint8_t* code,
8640 const uint8_t* roots_data,
8641 Literal* literal,
8642 uint64_t index_in_table) {
8643 DCHECK(literal->GetLabel()->IsBound());
8644 uint32_t literal_offset = literal->GetLabel()->Position();
8645 uintptr_t address =
8646 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8647 uint8_t* data = code + literal_offset;
8648 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
8649}
8650
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008651void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8652 for (const auto& entry : jit_string_patches_) {
8653 const auto& it = jit_string_roots_.find(entry.first);
8654 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008655 PatchJitRootUse(code, roots_data, entry.second, it->second);
8656 }
8657 for (const auto& entry : jit_class_patches_) {
8658 const auto& it = jit_class_roots_.find(entry.first);
8659 DCHECK(it != jit_class_roots_.end());
8660 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008661 }
8662}
8663
Roland Levillain4d027112015-07-01 15:41:14 +01008664#undef __
8665#undef QUICK_ENTRY_POINT
8666
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00008667} // namespace arm
8668} // namespace art