blob: cf2a391e8f2794fbfd09e6dd471f3f48fd6ad908 [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
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010019#include "arch/arm/asm_support_arm.h"
Calin Juravle34166012014-12-19 17:22:29 +000020#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080022#include "code_generator_utils.h"
Anton Kirilov74234da2017-01-13 14:42:47 +000023#include "common_arm.h"
Vladimir Marko58155012015-08-19 12:49:41 +000024#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070025#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010026#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080027#include "intrinsics.h"
28#include "intrinsics_arm.h"
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010029#include "linker/arm/relative_patcher_thumb2.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070030#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070032#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033#include "utils/arm/assembler_arm.h"
34#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000035#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000037
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010039
Roland Levillain3b359c72015-11-17 19:35:12 +000040template<class MirrorType>
41class GcRoot;
42
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000043namespace arm {
44
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000045static bool ExpectedPairLayout(Location location) {
46 // We expected this for both core and fpu register pairs.
47 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
48}
49
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010051static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010052
David Brazdil58282f42016-01-14 12:45:10 +000053static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000054static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070055 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000056static constexpr SRegister kFpuCalleeSaves[] =
57 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010058
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000059// D31 cannot be split into two S registers, and the register allocator only works on
60// S registers. Therefore there is no need to block it.
61static constexpr DRegister DTMP = D31;
62
Vladimir Markof3e0ee22015-12-17 15:23:13 +000063static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070064
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010065// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
66// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
67// For the Baker read barrier implementation using link-generated thunks we need to split
68// the offset explicitly.
69constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
70
71// Flags controlling the use of link-time generated thunks for Baker read barriers.
72constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
73constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
74constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
75
76// The reserved entrypoint register for link-time generated thunks.
77const Register kBakerCcEntrypointRegister = R4;
78
Roland Levillain7cbd27f2016-08-11 23:53:33 +010079// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
80#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070081#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010082
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010083static inline void CheckLastTempIsBakerCcEntrypointRegister(HInstruction* instruction) {
84 DCHECK_EQ(static_cast<uint32_t>(kBakerCcEntrypointRegister),
85 linker::Thumb2RelativePatcher::kBakerCcEntrypointRegister);
86 DCHECK_NE(instruction->GetLocations()->GetTempCount(), 0u);
87 DCHECK_EQ(kBakerCcEntrypointRegister,
88 instruction->GetLocations()->GetTemp(
89 instruction->GetLocations()->GetTempCount() - 1u).AsRegister<Register>());
90}
91
92static inline void EmitPlaceholderBne(CodeGeneratorARM* codegen, Label* bne_label) {
Vladimir Marko88abba22017-05-03 17:09:25 +010093 ScopedForce32Bit force_32bit(down_cast<Thumb2Assembler*>(codegen->GetAssembler()));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010094 __ BindTrackedLabel(bne_label);
95 Label placeholder_label;
96 __ b(&placeholder_label, NE); // Placeholder, patched at link-time.
97 __ Bind(&placeholder_label);
98}
99
Vladimir Marko88abba22017-05-03 17:09:25 +0100100static inline bool CanEmitNarrowLdr(Register rt, Register rn, uint32_t offset) {
101 return ArmAssembler::IsLowRegister(rt) && ArmAssembler::IsLowRegister(rn) && offset < 32u;
102}
103
Artem Serovf4d6aee2016-07-11 10:41:45 +0100104static constexpr int kRegListThreshold = 4;
105
Artem Serovd300d8f2016-07-15 14:00:56 +0100106// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
107// for each live D registers they treat two corresponding S registers as live ones.
108//
109// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
110// from a list of contiguous S registers a list of contiguous D registers (processing first/last
111// S registers corner cases) and save/restore this new list treating them as D registers.
112// - decreasing code size
113// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
114// restored and then used in regular non SlowPath code as D register.
115//
116// For the following example (v means the S register is live):
117// D names: | D0 | D1 | D2 | D4 | ...
118// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
119// Live? | | v | v | v | v | v | v | | ...
120//
121// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
122// as D registers.
123static size_t SaveContiguousSRegisterList(size_t first,
124 size_t last,
125 CodeGenerator* codegen,
126 size_t stack_offset) {
127 DCHECK_LE(first, last);
128 if ((first == last) && (first == 0)) {
129 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first);
130 return stack_offset;
131 }
132 if (first % 2 == 1) {
133 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first++);
134 }
135
136 bool save_last = false;
137 if (last % 2 == 0) {
138 save_last = true;
139 --last;
140 }
141
142 if (first < last) {
143 DRegister d_reg = static_cast<DRegister>(first / 2);
144 DCHECK_EQ((last - first + 1) % 2, 0u);
145 size_t number_of_d_regs = (last - first + 1) / 2;
146
147 if (number_of_d_regs == 1) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100148 __ StoreDToOffset(d_reg, SP, stack_offset);
Artem Serovd300d8f2016-07-15 14:00:56 +0100149 } else if (number_of_d_regs > 1) {
150 __ add(IP, SP, ShifterOperand(stack_offset));
151 __ vstmiad(IP, d_reg, number_of_d_regs);
152 }
153 stack_offset += number_of_d_regs * kArmWordSize * 2;
154 }
155
156 if (save_last) {
157 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, last + 1);
158 }
159
160 return stack_offset;
161}
162
163static size_t RestoreContiguousSRegisterList(size_t first,
164 size_t last,
165 CodeGenerator* codegen,
166 size_t stack_offset) {
167 DCHECK_LE(first, last);
168 if ((first == last) && (first == 0)) {
169 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first);
170 return stack_offset;
171 }
172 if (first % 2 == 1) {
173 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first++);
174 }
175
176 bool restore_last = false;
177 if (last % 2 == 0) {
178 restore_last = true;
179 --last;
180 }
181
182 if (first < last) {
183 DRegister d_reg = static_cast<DRegister>(first / 2);
184 DCHECK_EQ((last - first + 1) % 2, 0u);
185 size_t number_of_d_regs = (last - first + 1) / 2;
186 if (number_of_d_regs == 1) {
187 __ LoadDFromOffset(d_reg, SP, stack_offset);
188 } else if (number_of_d_regs > 1) {
189 __ add(IP, SP, ShifterOperand(stack_offset));
190 __ vldmiad(IP, d_reg, number_of_d_regs);
191 }
192 stack_offset += number_of_d_regs * kArmWordSize * 2;
193 }
194
195 if (restore_last) {
196 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, last + 1);
197 }
198
199 return stack_offset;
200}
201
Artem Serovf4d6aee2016-07-11 10:41:45 +0100202void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
203 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
204 size_t orig_offset = stack_offset;
205
206 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
207 for (uint32_t i : LowToHighBits(core_spills)) {
208 // If the register holds an object, update the stack mask.
209 if (locations->RegisterContainsObject(i)) {
210 locations->SetStackBit(stack_offset / kVRegSize);
211 }
212 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
213 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
214 saved_core_stack_offsets_[i] = stack_offset;
215 stack_offset += kArmWordSize;
216 }
217
218 int reg_num = POPCOUNT(core_spills);
219 if (reg_num != 0) {
220 if (reg_num > kRegListThreshold) {
221 __ StoreList(RegList(core_spills), orig_offset);
222 } else {
223 stack_offset = orig_offset;
224 for (uint32_t i : LowToHighBits(core_spills)) {
225 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
226 }
227 }
228 }
229
Artem Serovd300d8f2016-07-15 14:00:56 +0100230 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
231 orig_offset = stack_offset;
Vladimir Marko804b03f2016-09-14 16:26:36 +0100232 for (uint32_t i : LowToHighBits(fp_spills)) {
Artem Serovf4d6aee2016-07-11 10:41:45 +0100233 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
234 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serovd300d8f2016-07-15 14:00:56 +0100235 stack_offset += kArmWordSize;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100236 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100237
238 stack_offset = orig_offset;
239 while (fp_spills != 0u) {
240 uint32_t begin = CTZ(fp_spills);
241 uint32_t tmp = fp_spills + (1u << begin);
242 fp_spills &= tmp; // Clear the contiguous range of 1s.
243 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
244 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
245 }
246 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100247}
248
249void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
250 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
251 size_t orig_offset = stack_offset;
252
253 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
254 for (uint32_t i : LowToHighBits(core_spills)) {
255 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
256 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
257 stack_offset += kArmWordSize;
258 }
259
260 int reg_num = POPCOUNT(core_spills);
261 if (reg_num != 0) {
262 if (reg_num > kRegListThreshold) {
263 __ LoadList(RegList(core_spills), orig_offset);
264 } else {
265 stack_offset = orig_offset;
266 for (uint32_t i : LowToHighBits(core_spills)) {
267 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
268 }
269 }
270 }
271
Artem Serovd300d8f2016-07-15 14:00:56 +0100272 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
273 while (fp_spills != 0u) {
274 uint32_t begin = CTZ(fp_spills);
275 uint32_t tmp = fp_spills + (1u << begin);
276 fp_spills &= tmp; // Clear the contiguous range of 1s.
277 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
278 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
Artem Serovf4d6aee2016-07-11 10:41:45 +0100279 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100280 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100281}
282
283class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100284 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100285 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100286
Alexandre Rames67555f72014-11-18 10:55:16 +0000287 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100288 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100289 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000290 if (instruction_->CanThrowIntoCatchBlock()) {
291 // Live registers will be restored in the catch block if caught.
292 SaveLiveRegisters(codegen, instruction_->GetLocations());
293 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100294 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
295 instruction_,
296 instruction_->GetDexPc(),
297 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000298 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100299 }
300
Alexandre Rames8158f282015-08-07 10:26:17 +0100301 bool IsFatal() const OVERRIDE { return true; }
302
Alexandre Rames9931f312015-06-19 14:47:01 +0100303 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
304
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100305 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100306 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
307};
308
Artem Serovf4d6aee2016-07-11 10:41:45 +0100309class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000310 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100311 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000312
Alexandre Rames67555f72014-11-18 10:55:16 +0000313 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000314 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
315 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100316 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000317 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000318 }
319
Alexandre Rames8158f282015-08-07 10:26:17 +0100320 bool IsFatal() const OVERRIDE { return true; }
321
Alexandre Rames9931f312015-06-19 14:47:01 +0100322 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
323
Calin Juravled0d48522014-11-04 16:40:20 +0000324 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000325 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
326};
327
Artem Serovf4d6aee2016-07-11 10:41:45 +0100328class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000329 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000330 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100331 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000332
Alexandre Rames67555f72014-11-18 10:55:16 +0000333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100334 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000335 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100336 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000337 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100338 if (successor_ == nullptr) {
339 __ b(GetReturnLabel());
340 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100341 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100342 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000343 }
344
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100345 Label* GetReturnLabel() {
346 DCHECK(successor_ == nullptr);
347 return &return_label_;
348 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000349
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100350 HBasicBlock* GetSuccessor() const {
351 return successor_;
352 }
353
Alexandre Rames9931f312015-06-19 14:47:01 +0100354 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
355
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000356 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100357 // If not null, the block to branch to after the suspend check.
358 HBasicBlock* const successor_;
359
360 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000361 Label return_label_;
362
363 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
364};
365
Artem Serovf4d6aee2016-07-11 10:41:45 +0100366class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100367 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100368 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100369 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100370
Alexandre Rames67555f72014-11-18 10:55:16 +0000371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100372 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100373 LocationSummary* locations = instruction_->GetLocations();
374
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100375 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000376 if (instruction_->CanThrowIntoCatchBlock()) {
377 // Live registers will be restored in the catch block if caught.
378 SaveLiveRegisters(codegen, instruction_->GetLocations());
379 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000380 // We're moving two locations to locations that could overlap, so we need a parallel
381 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100382 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000383 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100384 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000385 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100386 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100387 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100388 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
389 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100390 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
391 ? kQuickThrowStringBounds
392 : kQuickThrowArrayBounds;
393 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100394 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000395 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100396 }
397
Alexandre Rames8158f282015-08-07 10:26:17 +0100398 bool IsFatal() const OVERRIDE { return true; }
399
Alexandre Rames9931f312015-06-19 14:47:01 +0100400 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
401
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100402 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100403 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
404};
405
Artem Serovf4d6aee2016-07-11 10:41:45 +0100406class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100407 public:
Vladimir Markoea4c1262017-02-06 19:59:33 +0000408 LoadClassSlowPathARM(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000409 : SlowPathCodeARM(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000410 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
411 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100412
Alexandre Rames67555f72014-11-18 10:55:16 +0000413 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000414 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000415 Location out = locations->Out();
416 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000417
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100418 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
419 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000420 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100421
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100422 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000423 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
424 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
425 bool is_load_class_bss_entry =
426 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
427 Register entry_address = kNoRegister;
428 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
429 Register temp = locations->GetTemp(0).AsRegister<Register>();
430 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
431 // the kSaveEverything call.
432 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
433 entry_address = temp_is_r0 ? out.AsRegister<Register>() : temp;
434 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
435 if (temp_is_r0) {
436 __ mov(entry_address, ShifterOperand(temp));
437 }
438 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000439 dex::TypeIndex type_index = cls_->GetTypeIndex();
440 __ LoadImmediate(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100441 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
442 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000443 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000444 if (do_clinit_) {
445 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
446 } else {
447 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
448 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000449
Vladimir Markoea4c1262017-02-06 19:59:33 +0000450 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
451 if (is_load_class_bss_entry) {
452 if (call_saves_everything_except_r0) {
453 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
454 __ str(R0, Address(entry_address));
455 } else {
456 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
457 Register temp = IP;
458 CodeGeneratorARM::PcRelativePatchInfo* labels =
459 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
460 __ BindTrackedLabel(&labels->movw_label);
461 __ movw(temp, /* placeholder */ 0u);
462 __ BindTrackedLabel(&labels->movt_label);
463 __ movt(temp, /* placeholder */ 0u);
464 __ BindTrackedLabel(&labels->add_pc_label);
465 __ add(temp, temp, ShifterOperand(PC));
466 __ str(R0, Address(temp));
467 }
468 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000469 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000470 if (out.IsValid()) {
471 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000472 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
473 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000474 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100475 __ b(GetExitLabel());
476 }
477
Alexandre Rames9931f312015-06-19 14:47:01 +0100478 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
479
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100480 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000481 // The class this slow path will load.
482 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100483
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000484 // The dex PC of `at_`.
485 const uint32_t dex_pc_;
486
487 // Whether to initialize the class.
488 const bool do_clinit_;
489
490 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100491};
492
Vladimir Markoaad75c62016-10-03 08:46:48 +0000493class LoadStringSlowPathARM : public SlowPathCodeARM {
494 public:
495 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
496
497 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000498 DCHECK(instruction_->IsLoadString());
499 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000500 LocationSummary* locations = instruction_->GetLocations();
501 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100502 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000503 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100504 Register out = locations->Out().AsRegister<Register>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100505 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000506
507 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
508 __ Bind(GetEntryLabel());
509 SaveLiveRegisters(codegen, locations);
510
511 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100512 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000513 // the kSaveEverything call.
514 Register entry_address = kNoRegister;
515 if (call_saves_everything_except_r0) {
516 Register temp = locations->GetTemp(0).AsRegister<Register>();
517 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
518 entry_address = temp_is_r0 ? out : temp;
519 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
520 if (temp_is_r0) {
521 __ mov(entry_address, ShifterOperand(temp));
522 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100523 }
524
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000525 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000526 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
527 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100528
529 // Store the resolved String to the .bss entry.
530 if (call_saves_everything_except_r0) {
531 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
532 __ str(R0, Address(entry_address));
533 } else {
534 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000535 Register temp = IP;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100536 CodeGeneratorARM::PcRelativePatchInfo* labels =
537 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
538 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000539 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100540 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000541 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100542 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000543 __ add(temp, temp, ShifterOperand(PC));
544 __ str(R0, Address(temp));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100545 }
546
Vladimir Markoaad75c62016-10-03 08:46:48 +0000547 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000548 RestoreLiveRegisters(codegen, locations);
549
Vladimir Markoaad75c62016-10-03 08:46:48 +0000550 __ b(GetExitLabel());
551 }
552
553 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
554
555 private:
556 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
557};
558
Artem Serovf4d6aee2016-07-11 10:41:45 +0100559class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000560 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000561 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100562 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000563
Alexandre Rames67555f72014-11-18 10:55:16 +0000564 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000565 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000566 DCHECK(instruction_->IsCheckCast()
567 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000568
569 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
570 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000571
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000572 if (!is_fatal_) {
573 SaveLiveRegisters(codegen, locations);
574 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000575
576 // We're moving two locations to locations that could overlap, so we need a parallel
577 // move resolver.
578 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800579 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800580 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
581 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800582 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800583 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
584 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000585 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100586 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100587 instruction_,
588 instruction_->GetDexPc(),
589 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800590 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000591 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
592 } else {
593 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800594 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
595 instruction_,
596 instruction_->GetDexPc(),
597 this);
598 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000599 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000600
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000601 if (!is_fatal_) {
602 RestoreLiveRegisters(codegen, locations);
603 __ b(GetExitLabel());
604 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000605 }
606
Alexandre Rames9931f312015-06-19 14:47:01 +0100607 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
608
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000609 bool IsFatal() const OVERRIDE { return is_fatal_; }
610
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000611 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000612 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000613
614 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
615};
616
Artem Serovf4d6aee2016-07-11 10:41:45 +0100617class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700618 public:
Aart Bik42249c32016-01-07 15:33:50 -0800619 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100620 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700621
622 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800623 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700624 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100625 LocationSummary* locations = instruction_->GetLocations();
626 SaveLiveRegisters(codegen, locations);
627 InvokeRuntimeCallingConvention calling_convention;
628 __ LoadImmediate(calling_convention.GetRegisterAt(0),
629 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100630 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100631 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700632 }
633
Alexandre Rames9931f312015-06-19 14:47:01 +0100634 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
635
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700636 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700637 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
638};
639
Artem Serovf4d6aee2016-07-11 10:41:45 +0100640class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100641 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100642 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100643
644 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
645 LocationSummary* locations = instruction_->GetLocations();
646 __ Bind(GetEntryLabel());
647 SaveLiveRegisters(codegen, locations);
648
649 InvokeRuntimeCallingConvention calling_convention;
650 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
651 parallel_move.AddMove(
652 locations->InAt(0),
653 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
654 Primitive::kPrimNot,
655 nullptr);
656 parallel_move.AddMove(
657 locations->InAt(1),
658 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
659 Primitive::kPrimInt,
660 nullptr);
661 parallel_move.AddMove(
662 locations->InAt(2),
663 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
664 Primitive::kPrimNot,
665 nullptr);
666 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
667
668 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100669 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000670 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100671 RestoreLiveRegisters(codegen, locations);
672 __ b(GetExitLabel());
673 }
674
675 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
676
677 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100678 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
679};
680
Roland Levillain54f869e2017-03-06 13:54:11 +0000681// Abstract base class for read barrier slow paths marking a reference
682// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000683//
Roland Levillain54f869e2017-03-06 13:54:11 +0000684// Argument `entrypoint` must be a register location holding the read
685// barrier marking runtime entry point to be invoked.
686class ReadBarrierMarkSlowPathBaseARM : public SlowPathCodeARM {
687 protected:
688 ReadBarrierMarkSlowPathBaseARM(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000689 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
690 DCHECK(kEmitCompilerReadBarrier);
691 }
692
Roland Levillain54f869e2017-03-06 13:54:11 +0000693 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000694
Roland Levillain54f869e2017-03-06 13:54:11 +0000695 // Generate assembly code calling the read barrier marking runtime
696 // entry point (ReadBarrierMarkRegX).
697 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000698 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain47b3ab22017-02-27 14:31:35 +0000699
Roland Levillain47b3ab22017-02-27 14:31:35 +0000700 // No need to save live registers; it's taken care of by the
701 // entrypoint. Also, there is no need to update the stack mask,
702 // as this runtime call will not trigger a garbage collection.
703 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
704 DCHECK_NE(ref_reg, SP);
705 DCHECK_NE(ref_reg, LR);
706 DCHECK_NE(ref_reg, PC);
707 // IP is used internally by the ReadBarrierMarkRegX entry point
708 // as a temporary, it cannot be the entry point's input/output.
709 DCHECK_NE(ref_reg, IP);
710 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
711 // "Compact" slow path, saving two moves.
712 //
713 // Instead of using the standard runtime calling convention (input
714 // and output in R0):
715 //
716 // R0 <- ref
717 // R0 <- ReadBarrierMark(R0)
718 // ref <- R0
719 //
720 // we just use rX (the register containing `ref`) as input and output
721 // of a dedicated entrypoint:
722 //
723 // rX <- ReadBarrierMarkRegX(rX)
724 //
725 if (entrypoint_.IsValid()) {
726 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
727 __ blx(entrypoint_.AsRegister<Register>());
728 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000729 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000730 int32_t entry_point_offset =
731 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
732 // This runtime call does not require a stack map.
733 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
734 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000735 }
736
737 // The location (register) of the marked object reference.
738 const Location ref_;
739
740 // The location of the entrypoint if it is already loaded.
741 const Location entrypoint_;
742
743 private:
744 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM);
745};
746
Dave Allison20dfc792014-06-16 20:44:29 -0700747// Slow path marking an object reference `ref` during a read
748// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000749// reference does not get updated by this slow path after marking.
Dave Allison20dfc792014-06-16 20:44:29 -0700750//
751// This means that after the execution of this slow path, `ref` will
752// always be up-to-date, but `obj.field` may not; i.e., after the
753// flip, `ref` will be a to-space reference, but `obj.field` will
754// probably still be a from-space reference (unless it gets updated by
755// another thread, or if another thread installed another object
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000756// reference (different from `ref`) in `obj.field`).
757//
758// If `entrypoint` is a valid location it is assumed to already be
759// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000760// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000761class ReadBarrierMarkSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000762 public:
763 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
764 Location ref,
765 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000766 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000767 DCHECK(kEmitCompilerReadBarrier);
768 }
769
770 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
771
772 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
773 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000774 DCHECK(locations->CanCall());
775 if (kIsDebugBuild) {
776 Register ref_reg = ref_.AsRegister<Register>();
777 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
778 }
779 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
780 << "Unexpected instruction in read barrier marking slow path: "
781 << instruction_->DebugName();
782
783 __ Bind(GetEntryLabel());
784 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000785 __ b(GetExitLabel());
786 }
787
788 private:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000789 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
790};
791
Roland Levillain54f869e2017-03-06 13:54:11 +0000792// Slow path loading `obj`'s lock word, loading a reference from
793// object `*(obj + offset + (index << scale_factor))` into `ref`, and
794// marking `ref` if `obj` is gray according to the lock word (Baker
795// read barrier). The field `obj.field` in the object `obj` holding
796// this reference does not get updated by this slow path after marking
797// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
798// below for that).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000799//
Roland Levillain54f869e2017-03-06 13:54:11 +0000800// This means that after the execution of this slow path, `ref` will
801// always be up-to-date, but `obj.field` may not; i.e., after the
802// flip, `ref` will be a to-space reference, but `obj.field` will
803// probably still be a from-space reference (unless it gets updated by
804// another thread, or if another thread installed another object
805// reference (different from `ref`) in `obj.field`).
806//
807// Argument `entrypoint` must be a register location holding the read
808// barrier marking runtime entry point to be invoked.
809class LoadReferenceWithBakerReadBarrierSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000810 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000811 LoadReferenceWithBakerReadBarrierSlowPathARM(HInstruction* instruction,
812 Location ref,
813 Register obj,
814 uint32_t offset,
815 Location index,
816 ScaleFactor scale_factor,
817 bool needs_null_check,
818 Register temp,
819 Location entrypoint)
820 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000821 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000822 offset_(offset),
823 index_(index),
824 scale_factor_(scale_factor),
825 needs_null_check_(needs_null_check),
826 temp_(temp) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000827 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000828 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000829 }
830
Roland Levillain54f869e2017-03-06 13:54:11 +0000831 const char* GetDescription() const OVERRIDE {
832 return "LoadReferenceWithBakerReadBarrierSlowPathARM";
833 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000834
835 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
836 LocationSummary* locations = instruction_->GetLocations();
837 Register ref_reg = ref_.AsRegister<Register>();
838 DCHECK(locations->CanCall());
839 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000840 DCHECK_NE(ref_reg, temp_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000841 DCHECK(instruction_->IsInstanceFieldGet() ||
842 instruction_->IsStaticFieldGet() ||
843 instruction_->IsArrayGet() ||
844 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000845 instruction_->IsInstanceOf() ||
846 instruction_->IsCheckCast() ||
847 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
848 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
849 << "Unexpected instruction in read barrier marking slow path: "
850 << instruction_->DebugName();
851 // The read barrier instrumentation of object ArrayGet
852 // instructions does not support the HIntermediateAddress
853 // instruction.
854 DCHECK(!(instruction_->IsArrayGet() &&
855 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
856
857 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000858
859 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
860 // inserted after the original load. However, in fast path based
861 // Baker's read barriers, we need to perform the load of
862 // mirror::Object::monitor_ *before* the original reference load.
863 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000864 // The slow path (for Baker's algorithm) should look like:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000865 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000866 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
867 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
868 // HeapReference<mirror::Object> ref = *src; // Original reference load.
869 // bool is_gray = (rb_state == ReadBarrier::GrayState());
870 // if (is_gray) {
871 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
872 // }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000873 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000874 // Note: the original implementation in ReadBarrier::Barrier is
875 // slightly more complex as it performs additional checks that we do
876 // not do here for performance reasons.
877
878 // /* int32_t */ monitor = obj->monitor_
879 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
880 __ LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
881 if (needs_null_check_) {
882 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000883 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000884 // /* LockWord */ lock_word = LockWord(monitor)
885 static_assert(sizeof(LockWord) == sizeof(int32_t),
886 "art::LockWord and int32_t have different sizes.");
887
888 // Introduce a dependency on the lock_word including the rb_state,
889 // which shall prevent load-load reordering without using
890 // a memory barrier (which would be more expensive).
891 // `obj` is unchanged by this operation, but its value now depends
892 // on `temp`.
893 __ add(obj_, obj_, ShifterOperand(temp_, LSR, 32));
894
895 // The actual reference load.
896 // A possible implicit null check has already been handled above.
897 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
898 arm_codegen->GenerateRawReferenceLoad(
899 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
900
901 // Mark the object `ref` when `obj` is gray.
902 //
903 // if (rb_state == ReadBarrier::GrayState())
904 // ref = ReadBarrier::Mark(ref);
905 //
906 // Given the numeric representation, it's enough to check the low bit of the
907 // rb_state. We do that by shifting the bit out of the lock word with LSRS
908 // which can be a 16-bit instruction unlike the TST immediate.
909 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
910 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
911 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
912 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
913 GenerateReadBarrierMarkRuntimeCall(codegen);
914
Roland Levillain47b3ab22017-02-27 14:31:35 +0000915 __ b(GetExitLabel());
916 }
917
918 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000919 // The register containing the object holding the marked object reference field.
920 Register obj_;
921 // The offset, index and scale factor to access the reference in `obj_`.
922 uint32_t offset_;
923 Location index_;
924 ScaleFactor scale_factor_;
925 // Is a null check required?
926 bool needs_null_check_;
927 // A temporary register used to hold the lock word of `obj_`.
928 Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000929
Roland Levillain54f869e2017-03-06 13:54:11 +0000930 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000931};
932
Roland Levillain54f869e2017-03-06 13:54:11 +0000933// Slow path loading `obj`'s lock word, loading a reference from
934// object `*(obj + offset + (index << scale_factor))` into `ref`, and
935// marking `ref` if `obj` is gray according to the lock word (Baker
936// read barrier). If needed, this slow path also atomically updates
937// the field `obj.field` in the object `obj` holding this reference
938// after marking (contrary to
939// LoadReferenceWithBakerReadBarrierSlowPathARM above, which never
940// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000941//
942// This means that after the execution of this slow path, both `ref`
943// and `obj.field` will be up-to-date; i.e., after the flip, both will
944// hold the same to-space reference (unless another thread installed
945// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000946//
Roland Levillain54f869e2017-03-06 13:54:11 +0000947// Argument `entrypoint` must be a register location holding the read
948// barrier marking runtime entry point to be invoked.
949class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
950 : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000951 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000952 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(HInstruction* instruction,
953 Location ref,
954 Register obj,
955 uint32_t offset,
956 Location index,
957 ScaleFactor scale_factor,
958 bool needs_null_check,
959 Register temp1,
960 Register temp2,
961 Location entrypoint)
962 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000963 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000964 offset_(offset),
965 index_(index),
966 scale_factor_(scale_factor),
967 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000968 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +0000969 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000970 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000971 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000972 }
973
Roland Levillain54f869e2017-03-06 13:54:11 +0000974 const char* GetDescription() const OVERRIDE {
975 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM";
976 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000977
978 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
979 LocationSummary* locations = instruction_->GetLocations();
980 Register ref_reg = ref_.AsRegister<Register>();
981 DCHECK(locations->CanCall());
982 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000983 DCHECK_NE(ref_reg, temp1_);
984
985 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000986 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
987 << "Unexpected instruction in read barrier marking and field updating slow path: "
988 << instruction_->DebugName();
989 DCHECK(instruction_->GetLocations()->Intrinsified());
990 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000991 DCHECK_EQ(offset_, 0u);
992 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
993 // The location of the offset of the marked reference field within `obj_`.
994 Location field_offset = index_;
995 DCHECK(field_offset.IsRegisterPair()) << field_offset;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000996
997 __ Bind(GetEntryLabel());
998
Roland Levillainff487002017-03-07 16:50:01 +0000999 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM's:
1000 //
1001 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1002 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1003 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1004 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1005 // if (is_gray) {
1006 // old_ref = ref;
1007 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1008 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1009 // }
1010
Roland Levillain54f869e2017-03-06 13:54:11 +00001011 // /* int32_t */ monitor = obj->monitor_
1012 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1013 __ LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1014 if (needs_null_check_) {
1015 codegen->MaybeRecordImplicitNullCheck(instruction_);
1016 }
1017 // /* LockWord */ lock_word = LockWord(monitor)
1018 static_assert(sizeof(LockWord) == sizeof(int32_t),
1019 "art::LockWord and int32_t have different sizes.");
1020
1021 // Introduce a dependency on the lock_word including the rb_state,
1022 // which shall prevent load-load reordering without using
1023 // a memory barrier (which would be more expensive).
1024 // `obj` is unchanged by this operation, but its value now depends
1025 // on `temp1`.
1026 __ add(obj_, obj_, ShifterOperand(temp1_, LSR, 32));
1027
1028 // The actual reference load.
1029 // A possible implicit null check has already been handled above.
1030 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1031 arm_codegen->GenerateRawReferenceLoad(
1032 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1033
1034 // Mark the object `ref` when `obj` is gray.
1035 //
1036 // if (rb_state == ReadBarrier::GrayState())
1037 // ref = ReadBarrier::Mark(ref);
1038 //
1039 // Given the numeric representation, it's enough to check the low bit of the
1040 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1041 // which can be a 16-bit instruction unlike the TST immediate.
1042 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1043 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1044 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1045 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
1046
1047 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001048 // Note that we cannot use IP to save the old reference, as IP is
1049 // used internally by the ReadBarrierMarkRegX entry point, and we
1050 // need the old reference after the call to that entry point.
1051 DCHECK_NE(temp1_, IP);
1052 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001053
Roland Levillain54f869e2017-03-06 13:54:11 +00001054 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001055
1056 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001057 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001058 //
1059 // Note that this field could also hold a different object, if
1060 // another thread had concurrently changed it. In that case, the
1061 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1062 // (CAS) operation below would abort the CAS, leaving the field
1063 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001064 __ cmp(temp1_, ShifterOperand(ref_reg));
Roland Levillain54f869e2017-03-06 13:54:11 +00001065 __ b(GetExitLabel(), EQ);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001066
1067 // Update the the holder's field atomically. This may fail if
1068 // mutator updates before us, but it's OK. This is achieved
1069 // using a strong compare-and-set (CAS) operation with relaxed
1070 // memory synchronization ordering, where the expected value is
1071 // the old reference and the desired value is the new reference.
1072
1073 // Convenience aliases.
1074 Register base = obj_;
1075 // The UnsafeCASObject intrinsic uses a register pair as field
1076 // offset ("long offset"), of which only the low part contains
1077 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001078 Register offset = field_offset.AsRegisterPairLow<Register>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001079 Register expected = temp1_;
1080 Register value = ref_reg;
1081 Register tmp_ptr = IP; // Pointer to actual memory.
1082 Register tmp = temp2_; // Value in memory.
1083
1084 __ add(tmp_ptr, base, ShifterOperand(offset));
1085
1086 if (kPoisonHeapReferences) {
1087 __ PoisonHeapReference(expected);
1088 if (value == expected) {
1089 // Do not poison `value`, as it is the same register as
1090 // `expected`, which has just been poisoned.
1091 } else {
1092 __ PoisonHeapReference(value);
1093 }
1094 }
1095
1096 // do {
1097 // tmp = [r_ptr] - expected;
1098 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1099
Roland Levillain24a4d112016-10-26 13:10:46 +01001100 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001101 __ Bind(&loop_head);
1102
1103 __ ldrex(tmp, tmp_ptr);
1104
1105 __ subs(tmp, tmp, ShifterOperand(expected));
1106
Roland Levillain24a4d112016-10-26 13:10:46 +01001107 __ it(NE);
1108 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001109
Roland Levillain24a4d112016-10-26 13:10:46 +01001110 __ b(&exit_loop, NE);
1111
1112 __ strex(tmp, value, tmp_ptr);
1113 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001114 __ b(&loop_head, EQ);
1115
Roland Levillain24a4d112016-10-26 13:10:46 +01001116 __ Bind(&exit_loop);
1117
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001118 if (kPoisonHeapReferences) {
1119 __ UnpoisonHeapReference(expected);
1120 if (value == expected) {
1121 // Do not unpoison `value`, as it is the same register as
1122 // `expected`, which has just been unpoisoned.
1123 } else {
1124 __ UnpoisonHeapReference(value);
1125 }
1126 }
1127
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001128 __ b(GetExitLabel());
1129 }
1130
1131 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001132 // The register containing the object holding the marked object reference field.
1133 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001134 // The offset, index and scale factor to access the reference in `obj_`.
1135 uint32_t offset_;
1136 Location index_;
1137 ScaleFactor scale_factor_;
1138 // Is a null check required?
1139 bool needs_null_check_;
1140 // A temporary register used to hold the lock word of `obj_`; and
1141 // also to hold the original reference value, when the reference is
1142 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001143 const Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001144 // A temporary register used in the implementation of the CAS, to
1145 // update the object's reference field.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001146 const Register temp2_;
1147
Roland Levillain54f869e2017-03-06 13:54:11 +00001148 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001149};
1150
Roland Levillain3b359c72015-11-17 19:35:12 +00001151// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001152class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001153 public:
1154 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
1155 Location out,
1156 Location ref,
1157 Location obj,
1158 uint32_t offset,
1159 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001160 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +00001161 out_(out),
1162 ref_(ref),
1163 obj_(obj),
1164 offset_(offset),
1165 index_(index) {
1166 DCHECK(kEmitCompilerReadBarrier);
1167 // If `obj` is equal to `out` or `ref`, it means the initial object
1168 // has been overwritten by (or after) the heap object reference load
1169 // to be instrumented, e.g.:
1170 //
1171 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00001172 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001173 //
1174 // In that case, we have lost the information about the original
1175 // object, and the emitted read barrier cannot work properly.
1176 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1177 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1178 }
1179
1180 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1181 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1182 LocationSummary* locations = instruction_->GetLocations();
1183 Register reg_out = out_.AsRegister<Register>();
1184 DCHECK(locations->CanCall());
1185 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +01001186 DCHECK(instruction_->IsInstanceFieldGet() ||
1187 instruction_->IsStaticFieldGet() ||
1188 instruction_->IsArrayGet() ||
1189 instruction_->IsInstanceOf() ||
1190 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001191 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +00001192 << "Unexpected instruction in read barrier for heap reference slow path: "
1193 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001194 // The read barrier instrumentation of object ArrayGet
1195 // instructions does not support the HIntermediateAddress
1196 // instruction.
1197 DCHECK(!(instruction_->IsArrayGet() &&
1198 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +00001199
1200 __ Bind(GetEntryLabel());
1201 SaveLiveRegisters(codegen, locations);
1202
1203 // We may have to change the index's value, but as `index_` is a
1204 // constant member (like other "inputs" of this slow path),
1205 // introduce a copy of it, `index`.
1206 Location index = index_;
1207 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001208 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +00001209 if (instruction_->IsArrayGet()) {
1210 // Compute the actual memory offset and store it in `index`.
1211 Register index_reg = index_.AsRegister<Register>();
1212 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
1213 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
1214 // We are about to change the value of `index_reg` (see the
1215 // calls to art::arm::Thumb2Assembler::Lsl and
1216 // art::arm::Thumb2Assembler::AddConstant below), but it has
1217 // not been saved by the previous call to
1218 // art::SlowPathCode::SaveLiveRegisters, as it is a
1219 // callee-save register --
1220 // art::SlowPathCode::SaveLiveRegisters does not consider
1221 // callee-save registers, as it has been designed with the
1222 // assumption that callee-save registers are supposed to be
1223 // handled by the called function. So, as a callee-save
1224 // register, `index_reg` _would_ eventually be saved onto
1225 // the stack, but it would be too late: we would have
1226 // changed its value earlier. Therefore, we manually save
1227 // it here into another freely available register,
1228 // `free_reg`, chosen of course among the caller-save
1229 // registers (as a callee-save `free_reg` register would
1230 // exhibit the same problem).
1231 //
1232 // Note we could have requested a temporary register from
1233 // the register allocator instead; but we prefer not to, as
1234 // this is a slow path, and we know we can find a
1235 // caller-save register that is available.
1236 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1237 __ Mov(free_reg, index_reg);
1238 index_reg = free_reg;
1239 index = Location::RegisterLocation(index_reg);
1240 } else {
1241 // The initial register stored in `index_` has already been
1242 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1243 // (as it is not a callee-save register), so we can freely
1244 // use it.
1245 }
1246 // Shifting the index value contained in `index_reg` by the scale
1247 // factor (2) cannot overflow in practice, as the runtime is
1248 // unable to allocate object arrays with a size larger than
1249 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1250 __ Lsl(index_reg, index_reg, TIMES_4);
1251 static_assert(
1252 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1253 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1254 __ AddConstant(index_reg, index_reg, offset_);
1255 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001256 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1257 // intrinsics, `index_` is not shifted by a scale factor of 2
1258 // (as in the case of ArrayGet), as it is actually an offset
1259 // to an object field within an object.
1260 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001261 DCHECK(instruction_->GetLocations()->Intrinsified());
1262 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1263 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1264 << instruction_->AsInvoke()->GetIntrinsic();
1265 DCHECK_EQ(offset_, 0U);
1266 DCHECK(index_.IsRegisterPair());
1267 // UnsafeGet's offset location is a register pair, the low
1268 // part contains the correct offset.
1269 index = index_.ToLow();
1270 }
1271 }
1272
1273 // We're moving two or three locations to locations that could
1274 // overlap, so we need a parallel move resolver.
1275 InvokeRuntimeCallingConvention calling_convention;
1276 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1277 parallel_move.AddMove(ref_,
1278 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1279 Primitive::kPrimNot,
1280 nullptr);
1281 parallel_move.AddMove(obj_,
1282 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1283 Primitive::kPrimNot,
1284 nullptr);
1285 if (index.IsValid()) {
1286 parallel_move.AddMove(index,
1287 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1288 Primitive::kPrimInt,
1289 nullptr);
1290 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1291 } else {
1292 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1293 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1294 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001295 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001296 CheckEntrypointTypes<
1297 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1298 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1299
1300 RestoreLiveRegisters(codegen, locations);
1301 __ b(GetExitLabel());
1302 }
1303
1304 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1305
1306 private:
1307 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1308 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1309 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1310 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1311 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1312 return static_cast<Register>(i);
1313 }
1314 }
1315 // We shall never fail to find a free caller-save register, as
1316 // there are more than two core caller-save registers on ARM
1317 // (meaning it is possible to find one which is different from
1318 // `ref` and `obj`).
1319 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1320 LOG(FATAL) << "Could not find a free caller-save register";
1321 UNREACHABLE();
1322 }
1323
Roland Levillain3b359c72015-11-17 19:35:12 +00001324 const Location out_;
1325 const Location ref_;
1326 const Location obj_;
1327 const uint32_t offset_;
1328 // An additional location containing an index to an array.
1329 // Only used for HArrayGet and the UnsafeGetObject &
1330 // UnsafeGetObjectVolatile intrinsics.
1331 const Location index_;
1332
1333 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1334};
1335
1336// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001337class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001338 public:
1339 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001340 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001341 DCHECK(kEmitCompilerReadBarrier);
1342 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001343
1344 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1345 LocationSummary* locations = instruction_->GetLocations();
1346 Register reg_out = out_.AsRegister<Register>();
1347 DCHECK(locations->CanCall());
1348 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001349 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1350 << "Unexpected instruction in read barrier for GC root slow path: "
1351 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001352
1353 __ Bind(GetEntryLabel());
1354 SaveLiveRegisters(codegen, locations);
1355
1356 InvokeRuntimeCallingConvention calling_convention;
1357 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1358 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001359 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001360 instruction_,
1361 instruction_->GetDexPc(),
1362 this);
1363 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1364 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1365
1366 RestoreLiveRegisters(codegen, locations);
1367 __ b(GetExitLabel());
1368 }
1369
1370 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1371
1372 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001373 const Location out_;
1374 const Location root_;
1375
1376 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1377};
1378
Aart Bike9f37602015-10-09 11:15:55 -07001379inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001380 switch (cond) {
1381 case kCondEQ: return EQ;
1382 case kCondNE: return NE;
1383 case kCondLT: return LT;
1384 case kCondLE: return LE;
1385 case kCondGT: return GT;
1386 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001387 case kCondB: return LO;
1388 case kCondBE: return LS;
1389 case kCondA: return HI;
1390 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001391 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001392 LOG(FATAL) << "Unreachable";
1393 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001394}
1395
Aart Bike9f37602015-10-09 11:15:55 -07001396// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001397inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001398 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001399 case kCondEQ: return EQ;
1400 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001401 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001402 case kCondLT: return LO;
1403 case kCondLE: return LS;
1404 case kCondGT: return HI;
1405 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001406 // Unsigned remain unchanged.
1407 case kCondB: return LO;
1408 case kCondBE: return LS;
1409 case kCondA: return HI;
1410 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001411 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001412 LOG(FATAL) << "Unreachable";
1413 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001414}
1415
Vladimir Markod6e069b2016-01-18 11:11:01 +00001416inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1417 // The ARM condition codes can express all the necessary branches, see the
1418 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1419 // There is no dex instruction or HIR that would need the missing conditions
1420 // "equal or unordered" or "not equal".
1421 switch (cond) {
1422 case kCondEQ: return EQ;
1423 case kCondNE: return NE /* unordered */;
1424 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1425 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1426 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1427 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1428 default:
1429 LOG(FATAL) << "UNREACHABLE";
1430 UNREACHABLE();
1431 }
1432}
1433
Anton Kirilov74234da2017-01-13 14:42:47 +00001434inline Shift ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1435 switch (op_kind) {
1436 case HDataProcWithShifterOp::kASR: return ASR;
1437 case HDataProcWithShifterOp::kLSL: return LSL;
1438 case HDataProcWithShifterOp::kLSR: return LSR;
1439 default:
1440 LOG(FATAL) << "Unexpected op kind " << op_kind;
1441 UNREACHABLE();
1442 }
1443}
1444
1445static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1446 Register out,
1447 Register first,
1448 const ShifterOperand& second,
1449 CodeGeneratorARM* codegen) {
1450 if (second.IsImmediate() && second.GetImmediate() == 0) {
1451 const ShifterOperand in = kind == HInstruction::kAnd
1452 ? ShifterOperand(0)
1453 : ShifterOperand(first);
1454
1455 __ mov(out, in);
1456 } else {
1457 switch (kind) {
1458 case HInstruction::kAdd:
1459 __ add(out, first, second);
1460 break;
1461 case HInstruction::kAnd:
1462 __ and_(out, first, second);
1463 break;
1464 case HInstruction::kOr:
1465 __ orr(out, first, second);
1466 break;
1467 case HInstruction::kSub:
1468 __ sub(out, first, second);
1469 break;
1470 case HInstruction::kXor:
1471 __ eor(out, first, second);
1472 break;
1473 default:
1474 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1475 UNREACHABLE();
1476 }
1477 }
1478}
1479
1480static void GenerateDataProc(HInstruction::InstructionKind kind,
1481 const Location& out,
1482 const Location& first,
1483 const ShifterOperand& second_lo,
1484 const ShifterOperand& second_hi,
1485 CodeGeneratorARM* codegen) {
1486 const Register first_hi = first.AsRegisterPairHigh<Register>();
1487 const Register first_lo = first.AsRegisterPairLow<Register>();
1488 const Register out_hi = out.AsRegisterPairHigh<Register>();
1489 const Register out_lo = out.AsRegisterPairLow<Register>();
1490
1491 if (kind == HInstruction::kAdd) {
1492 __ adds(out_lo, first_lo, second_lo);
1493 __ adc(out_hi, first_hi, second_hi);
1494 } else if (kind == HInstruction::kSub) {
1495 __ subs(out_lo, first_lo, second_lo);
1496 __ sbc(out_hi, first_hi, second_hi);
1497 } else {
1498 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1499 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1500 }
1501}
1502
1503static ShifterOperand GetShifterOperand(Register rm, Shift shift, uint32_t shift_imm) {
1504 return shift_imm == 0 ? ShifterOperand(rm) : ShifterOperand(rm, shift, shift_imm);
1505}
1506
1507static void GenerateLongDataProc(HDataProcWithShifterOp* instruction, CodeGeneratorARM* codegen) {
1508 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1509 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1510
1511 const LocationSummary* const locations = instruction->GetLocations();
1512 const uint32_t shift_value = instruction->GetShiftAmount();
1513 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1514 const Location first = locations->InAt(0);
1515 const Location second = locations->InAt(1);
1516 const Location out = locations->Out();
1517 const Register first_hi = first.AsRegisterPairHigh<Register>();
1518 const Register first_lo = first.AsRegisterPairLow<Register>();
1519 const Register out_hi = out.AsRegisterPairHigh<Register>();
1520 const Register out_lo = out.AsRegisterPairLow<Register>();
1521 const Register second_hi = second.AsRegisterPairHigh<Register>();
1522 const Register second_lo = second.AsRegisterPairLow<Register>();
1523 const Shift shift = ShiftFromOpKind(instruction->GetOpKind());
1524
1525 if (shift_value >= 32) {
1526 if (shift == LSL) {
1527 GenerateDataProcInstruction(kind,
1528 out_hi,
1529 first_hi,
1530 ShifterOperand(second_lo, LSL, shift_value - 32),
1531 codegen);
1532 GenerateDataProcInstruction(kind,
1533 out_lo,
1534 first_lo,
1535 ShifterOperand(0),
1536 codegen);
1537 } else if (shift == ASR) {
1538 GenerateDataProc(kind,
1539 out,
1540 first,
1541 GetShifterOperand(second_hi, ASR, shift_value - 32),
1542 ShifterOperand(second_hi, ASR, 31),
1543 codegen);
1544 } else {
1545 DCHECK_EQ(shift, LSR);
1546 GenerateDataProc(kind,
1547 out,
1548 first,
1549 GetShifterOperand(second_hi, LSR, shift_value - 32),
1550 ShifterOperand(0),
1551 codegen);
1552 }
1553 } else {
1554 DCHECK_GT(shift_value, 1U);
1555 DCHECK_LT(shift_value, 32U);
1556
1557 if (shift == LSL) {
1558 // We are not doing this for HInstruction::kAdd because the output will require
1559 // Location::kOutputOverlap; not applicable to other cases.
1560 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1561 GenerateDataProcInstruction(kind,
1562 out_hi,
1563 first_hi,
1564 ShifterOperand(second_hi, LSL, shift_value),
1565 codegen);
1566 GenerateDataProcInstruction(kind,
1567 out_hi,
1568 out_hi,
1569 ShifterOperand(second_lo, LSR, 32 - shift_value),
1570 codegen);
1571 GenerateDataProcInstruction(kind,
1572 out_lo,
1573 first_lo,
1574 ShifterOperand(second_lo, LSL, shift_value),
1575 codegen);
1576 } else {
1577 __ Lsl(IP, second_hi, shift_value);
1578 __ orr(IP, IP, ShifterOperand(second_lo, LSR, 32 - shift_value));
1579 GenerateDataProc(kind,
1580 out,
1581 first,
1582 ShifterOperand(second_lo, LSL, shift_value),
1583 ShifterOperand(IP),
1584 codegen);
1585 }
1586 } else {
1587 DCHECK(shift == ASR || shift == LSR);
1588
1589 // We are not doing this for HInstruction::kAdd because the output will require
1590 // Location::kOutputOverlap; not applicable to other cases.
1591 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1592 GenerateDataProcInstruction(kind,
1593 out_lo,
1594 first_lo,
1595 ShifterOperand(second_lo, LSR, shift_value),
1596 codegen);
1597 GenerateDataProcInstruction(kind,
1598 out_lo,
1599 out_lo,
1600 ShifterOperand(second_hi, LSL, 32 - shift_value),
1601 codegen);
1602 GenerateDataProcInstruction(kind,
1603 out_hi,
1604 first_hi,
1605 ShifterOperand(second_hi, shift, shift_value),
1606 codegen);
1607 } else {
1608 __ Lsr(IP, second_lo, shift_value);
1609 __ orr(IP, IP, ShifterOperand(second_hi, LSL, 32 - shift_value));
1610 GenerateDataProc(kind,
1611 out,
1612 first,
1613 ShifterOperand(IP),
1614 ShifterOperand(second_hi, shift, shift_value),
1615 codegen);
1616 }
1617 }
1618 }
1619}
1620
Donghui Bai426b49c2016-11-08 14:55:38 +08001621static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARM* codegen) {
1622 Primitive::Type type = instruction->InputAt(0)->GetType();
1623 Location lhs_loc = instruction->GetLocations()->InAt(0);
1624 Location rhs_loc = instruction->GetLocations()->InAt(1);
1625 if (rhs_loc.IsConstant()) {
1626 // 0.0 is the only immediate that can be encoded directly in
1627 // a VCMP instruction.
1628 //
1629 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1630 // specify that in a floating-point comparison, positive zero
1631 // and negative zero are considered equal, so we can use the
1632 // literal 0.0 for both cases here.
1633 //
1634 // Note however that some methods (Float.equal, Float.compare,
1635 // Float.compareTo, Double.equal, Double.compare,
1636 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1637 // StrictMath.min) consider 0.0 to be (strictly) greater than
1638 // -0.0. So if we ever translate calls to these methods into a
1639 // HCompare instruction, we must handle the -0.0 case with
1640 // care here.
1641 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1642 if (type == Primitive::kPrimFloat) {
1643 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1644 } else {
1645 DCHECK_EQ(type, Primitive::kPrimDouble);
1646 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1647 }
1648 } else {
1649 if (type == Primitive::kPrimFloat) {
1650 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1651 } else {
1652 DCHECK_EQ(type, Primitive::kPrimDouble);
1653 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1654 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1655 }
1656 }
1657}
1658
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001659static std::pair<Condition, Condition> GenerateLongTestConstant(HCondition* condition,
1660 bool invert,
1661 CodeGeneratorARM* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001662 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1663
1664 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001665 IfCondition cond = condition->GetCondition();
1666 IfCondition opposite = condition->GetOppositeCondition();
1667
1668 if (invert) {
1669 std::swap(cond, opposite);
1670 }
1671
Nicolas Geoffray30826612017-05-10 11:59:26 +00001672 std::pair<Condition, Condition> ret;
Donghui Bai426b49c2016-11-08 14:55:38 +08001673 const Location left = locations->InAt(0);
1674 const Location right = locations->InAt(1);
1675
1676 DCHECK(right.IsConstant());
1677
1678 const Register left_high = left.AsRegisterPairHigh<Register>();
1679 const Register left_low = left.AsRegisterPairLow<Register>();
Nicolas Geoffray30826612017-05-10 11:59:26 +00001680 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Donghui Bai426b49c2016-11-08 14:55:38 +08001681
1682 switch (cond) {
1683 case kCondEQ:
1684 case kCondNE:
1685 case kCondB:
1686 case kCondBE:
1687 case kCondA:
1688 case kCondAE:
1689 __ CmpConstant(left_high, High32Bits(value));
1690 __ it(EQ);
1691 __ cmp(left_low, ShifterOperand(Low32Bits(value)), EQ);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001692 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001693 break;
1694 case kCondLE:
1695 case kCondGT:
1696 // Trivially true or false.
1697 if (value == std::numeric_limits<int64_t>::max()) {
1698 __ cmp(left_low, ShifterOperand(left_low));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001699 ret = cond == kCondLE ? std::make_pair(EQ, NE) : std::make_pair(NE, EQ);
Donghui Bai426b49c2016-11-08 14:55:38 +08001700 break;
1701 }
1702
1703 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001704 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001705 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001706 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001707 } else {
1708 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001709 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001710 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001711 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001712 }
1713
1714 value++;
1715 FALLTHROUGH_INTENDED;
1716 case kCondGE:
1717 case kCondLT:
1718 __ CmpConstant(left_low, Low32Bits(value));
1719 __ sbcs(IP, left_high, ShifterOperand(High32Bits(value)));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001720 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001721 break;
1722 default:
1723 LOG(FATAL) << "Unreachable";
1724 UNREACHABLE();
1725 }
1726
1727 return ret;
1728}
1729
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001730static std::pair<Condition, Condition> GenerateLongTest(HCondition* condition,
1731 bool invert,
1732 CodeGeneratorARM* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001733 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1734
1735 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001736 IfCondition cond = condition->GetCondition();
1737 IfCondition opposite = condition->GetOppositeCondition();
1738
1739 if (invert) {
1740 std::swap(cond, opposite);
1741 }
1742
1743 std::pair<Condition, Condition> ret;
Donghui Bai426b49c2016-11-08 14:55:38 +08001744 Location left = locations->InAt(0);
1745 Location right = locations->InAt(1);
1746
1747 DCHECK(right.IsRegisterPair());
1748
1749 switch (cond) {
1750 case kCondEQ:
1751 case kCondNE:
1752 case kCondB:
1753 case kCondBE:
1754 case kCondA:
1755 case kCondAE:
1756 __ cmp(left.AsRegisterPairHigh<Register>(),
1757 ShifterOperand(right.AsRegisterPairHigh<Register>()));
1758 __ it(EQ);
1759 __ cmp(left.AsRegisterPairLow<Register>(),
1760 ShifterOperand(right.AsRegisterPairLow<Register>()),
1761 EQ);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001762 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001763 break;
1764 case kCondLE:
1765 case kCondGT:
1766 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001767 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001768 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001769 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001770 } else {
1771 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001772 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001773 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001774 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001775 }
1776
1777 std::swap(left, right);
1778 FALLTHROUGH_INTENDED;
1779 case kCondGE:
1780 case kCondLT:
1781 __ cmp(left.AsRegisterPairLow<Register>(),
1782 ShifterOperand(right.AsRegisterPairLow<Register>()));
1783 __ sbcs(IP,
1784 left.AsRegisterPairHigh<Register>(),
1785 ShifterOperand(right.AsRegisterPairHigh<Register>()));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001786 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001787 break;
1788 default:
1789 LOG(FATAL) << "Unreachable";
1790 UNREACHABLE();
1791 }
1792
1793 return ret;
1794}
1795
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001796static std::pair<Condition, Condition> GenerateTest(HCondition* condition,
1797 bool invert,
1798 CodeGeneratorARM* codegen) {
1799 const LocationSummary* const locations = condition->GetLocations();
1800 const Primitive::Type type = condition->GetLeft()->GetType();
1801 IfCondition cond = condition->GetCondition();
1802 IfCondition opposite = condition->GetOppositeCondition();
1803 std::pair<Condition, Condition> ret;
1804 const Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08001805
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001806 if (invert) {
1807 std::swap(cond, opposite);
1808 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001809
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001810 if (type == Primitive::kPrimLong) {
1811 ret = locations->InAt(1).IsConstant()
1812 ? GenerateLongTestConstant(condition, invert, codegen)
1813 : GenerateLongTest(condition, invert, codegen);
1814 } else if (Primitive::IsFloatingPointType(type)) {
1815 GenerateVcmp(condition, codegen);
1816 __ vmstat();
1817 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1818 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001819 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001820 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08001821
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001822 const Register left = locations->InAt(0).AsRegister<Register>();
1823
1824 if (right.IsRegister()) {
1825 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001826 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001827 DCHECK(right.IsConstant());
1828 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001829 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001830
1831 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001832 }
1833
1834 return ret;
1835}
1836
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001837static bool CanGenerateTest(HCondition* condition, ArmAssembler* assembler) {
1838 if (condition->GetLeft()->GetType() == Primitive::kPrimLong) {
1839 const LocationSummary* const locations = condition->GetLocations();
Nicolas Geoffray30826612017-05-10 11:59:26 +00001840 const IfCondition c = condition->GetCondition();
Donghui Bai426b49c2016-11-08 14:55:38 +08001841
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001842 if (locations->InAt(1).IsConstant()) {
Nicolas Geoffray30826612017-05-10 11:59:26 +00001843 const int64_t value = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001844 ShifterOperand so;
Donghui Bai426b49c2016-11-08 14:55:38 +08001845
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001846 if (c < kCondLT || c > kCondGE) {
1847 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1848 // we check that the least significant half of the first input to be compared
1849 // is in a low register (the other half is read outside an IT block), and
1850 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
Nicolas Geoffray30826612017-05-10 11:59:26 +00001851 // encoding can be used.
1852 if (!ArmAssembler::IsLowRegister(locations->InAt(0).AsRegisterPairLow<Register>()) ||
1853 !IsUint<8>(Low32Bits(value))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001854 return false;
1855 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001856 } else if (c == kCondLE || c == kCondGT) {
1857 if (value < std::numeric_limits<int64_t>::max() &&
1858 !assembler->ShifterOperandCanHold(kNoRegister,
1859 kNoRegister,
1860 SBC,
1861 High32Bits(value + 1),
1862 kCcSet,
1863 &so)) {
1864 return false;
1865 }
1866 } else if (!assembler->ShifterOperandCanHold(kNoRegister,
1867 kNoRegister,
1868 SBC,
1869 High32Bits(value),
1870 kCcSet,
1871 &so)) {
1872 return false;
Donghui Bai426b49c2016-11-08 14:55:38 +08001873 }
1874 }
1875 }
1876
1877 return true;
1878}
1879
1880static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
1881 const Primitive::Type type = constant->GetType();
1882 bool ret = false;
1883
1884 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1885
1886 if (type == Primitive::kPrimLong) {
1887 const uint64_t value = constant->AsLongConstant()->GetValueAsUint64();
1888
1889 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1890 } else {
1891 ret = IsUint<8>(CodeGenerator::GetInt32ValueOf(constant));
1892 }
1893
1894 return ret;
1895}
1896
1897static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
1898 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
1899
1900 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1901 return Location::ConstantLocation(constant->AsConstant());
1902 }
1903
1904 return Location::RequiresRegister();
1905}
1906
1907static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1908 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1909 // we check that we are not dealing with floating-point output (there is no
1910 // 16-bit VMOV encoding).
1911 if (!out.IsRegister() && !out.IsRegisterPair()) {
1912 return false;
1913 }
1914
1915 // For constants, we also check that the output is in one or two low registers,
1916 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1917 // MOV encoding can be used.
1918 if (src.IsConstant()) {
1919 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1920 return false;
1921 }
1922
1923 if (out.IsRegister()) {
1924 if (!ArmAssembler::IsLowRegister(out.AsRegister<Register>())) {
1925 return false;
1926 }
1927 } else {
1928 DCHECK(out.IsRegisterPair());
1929
1930 if (!ArmAssembler::IsLowRegister(out.AsRegisterPairHigh<Register>())) {
1931 return false;
1932 }
1933 }
1934 }
1935
1936 return true;
1937}
1938
Anton Kirilov74234da2017-01-13 14:42:47 +00001939#undef __
1940// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1941#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
1942
Donghui Bai426b49c2016-11-08 14:55:38 +08001943Label* CodeGeneratorARM::GetFinalLabel(HInstruction* instruction, Label* final_label) {
1944 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001945 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001946
1947 const HBasicBlock* const block = instruction->GetBlock();
1948 const HLoopInformation* const info = block->GetLoopInformation();
1949 HInstruction* const next = instruction->GetNext();
1950
1951 // Avoid a branch to a branch.
1952 if (next->IsGoto() && (info == nullptr ||
1953 !info->IsBackEdge(*block) ||
1954 !info->HasSuspendCheck())) {
1955 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1956 }
1957
1958 return final_label;
1959}
1960
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001961void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001962 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001963}
1964
1965void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001966 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001967}
1968
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001969size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1970 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1971 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001972}
1973
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001974size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1975 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1976 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001977}
1978
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001979size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1980 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1981 return kArmWordSize;
1982}
1983
1984size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1985 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1986 return kArmWordSize;
1987}
1988
Calin Juravle34166012014-12-19 17:22:29 +00001989CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001990 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001991 const CompilerOptions& compiler_options,
1992 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001993 : CodeGenerator(graph,
1994 kNumberOfCoreRegisters,
1995 kNumberOfSRegisters,
1996 kNumberOfRegisterPairs,
1997 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1998 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001999 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
2000 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01002001 compiler_options,
2002 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01002003 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002004 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002005 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01002006 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01002007 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00002008 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002009 uint32_literals_(std::less<uint32_t>(),
2010 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002011 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2012 boot_image_string_patches_(StringReferenceValueComparator(),
2013 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2014 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002015 boot_image_type_patches_(TypeReferenceValueComparator(),
2016 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2017 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00002018 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01002019 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00002020 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00002021 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2022 jit_class_patches_(TypeReferenceValueComparator(),
2023 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07002024 // Always save the LR register to mimic Quick.
2025 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01002026}
2027
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002028void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
2029 // Ensure that we fix up branches and literal loads and emit the literal pool.
2030 __ FinalizeCode();
2031
2032 // Adjust native pc offsets in stack maps.
2033 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08002034 uint32_t old_position =
2035 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002036 uint32_t new_position = __ GetAdjustedPosition(old_position);
2037 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
2038 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01002039 // Adjust pc offsets for the disassembly information.
2040 if (disasm_info_ != nullptr) {
2041 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
2042 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
2043 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
2044 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
2045 it.second.start = __ GetAdjustedPosition(it.second.start);
2046 it.second.end = __ GetAdjustedPosition(it.second.end);
2047 }
2048 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
2049 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
2050 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
2051 }
2052 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002053
2054 CodeGenerator::Finalize(allocator);
2055}
2056
David Brazdil58282f42016-01-14 12:45:10 +00002057void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002058 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002059 blocked_core_registers_[SP] = true;
2060 blocked_core_registers_[LR] = true;
2061 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002062
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002063 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002064 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002065
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002066 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002067 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002068
David Brazdil58282f42016-01-14 12:45:10 +00002069 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01002070 // Stubs do not save callee-save floating point registers. If the graph
2071 // is debuggable, we need to deal with these registers differently. For
2072 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002073 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
2074 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
2075 }
2076 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002077}
2078
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002079InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002080 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002081 assembler_(codegen->GetAssembler()),
2082 codegen_(codegen) {}
2083
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002084void CodeGeneratorARM::ComputeSpillMask() {
2085 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2086 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00002087 // There is no easy instruction to restore just the PC on thumb2. We spill and
2088 // restore another arbitrary register.
2089 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002090 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2091 // We use vpush and vpop for saving and restoring floating point registers, which take
2092 // a SRegister and the number of registers to save/restore after that SRegister. We
2093 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2094 // but in the range.
2095 if (fpu_spill_mask_ != 0) {
2096 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2097 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2098 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2099 fpu_spill_mask_ |= (1 << i);
2100 }
2101 }
2102}
2103
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002104static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002105 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002106}
2107
2108static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002109 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002110}
2111
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002112void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00002113 bool skip_overflow_check =
2114 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002115 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002116 __ Bind(&frame_entry_label_);
2117
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002118 if (HasEmptyFrame()) {
2119 return;
2120 }
2121
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002122 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002123 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
2124 __ LoadFromOffset(kLoadWord, IP, IP, 0);
2125 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002126 }
2127
Andreas Gampe501fd632015-09-10 16:11:06 -07002128 __ PushList(core_spill_mask_);
2129 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2130 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002131 if (fpu_spill_mask_ != 0) {
2132 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2133 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002134 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01002135 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002136 }
Mingyao Yang063fc772016-08-02 11:02:54 -07002137
2138 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2139 // Initialize should_deoptimize flag to 0.
2140 __ mov(IP, ShifterOperand(0));
2141 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
2142 }
2143
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002144 int adjust = GetFrameSize() - FrameEntrySpillSize();
2145 __ AddConstant(SP, -adjust);
2146 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002147
2148 // Save the current method if we need it. Note that we do not
2149 // do this in HCurrentMethod, as the instruction might have been removed
2150 // in the SSA graph.
2151 if (RequiresCurrentMethod()) {
2152 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
2153 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002154}
2155
2156void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002157 if (HasEmptyFrame()) {
2158 __ bx(LR);
2159 return;
2160 }
David Srbeckyc34dc932015-04-12 09:27:43 +01002161 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002162 int adjust = GetFrameSize() - FrameEntrySpillSize();
2163 __ AddConstant(SP, adjust);
2164 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002165 if (fpu_spill_mask_ != 0) {
2166 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2167 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07002168 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002169 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002170 }
Andreas Gampe501fd632015-09-10 16:11:06 -07002171 // Pop LR into PC to return.
2172 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
2173 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
2174 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01002175 __ cfi().RestoreState();
2176 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002177}
2178
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002179void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07002180 Label* label = GetLabelOf(block);
2181 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002182}
2183
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002184Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002185 switch (type) {
2186 case Primitive::kPrimBoolean:
2187 case Primitive::kPrimByte:
2188 case Primitive::kPrimChar:
2189 case Primitive::kPrimShort:
2190 case Primitive::kPrimInt:
2191 case Primitive::kPrimNot: {
2192 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002193 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002194 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002195 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002196 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002197 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002198 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002199 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002200
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002201 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002202 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002203 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002204 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002205 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002206 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002207 if (calling_convention.GetRegisterAt(index) == R1) {
2208 // Skip R1, and use R2_R3 instead.
2209 gp_index_++;
2210 index++;
2211 }
2212 }
2213 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2214 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002215 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01002216
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002217 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002218 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002219 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002220 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2221 }
2222 }
2223
2224 case Primitive::kPrimFloat: {
2225 uint32_t stack_index = stack_index_++;
2226 if (float_index_ % 2 == 0) {
2227 float_index_ = std::max(double_index_, float_index_);
2228 }
2229 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2230 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
2231 } else {
2232 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2233 }
2234 }
2235
2236 case Primitive::kPrimDouble: {
2237 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2238 uint32_t stack_index = stack_index_;
2239 stack_index_ += 2;
2240 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2241 uint32_t index = double_index_;
2242 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002243 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002244 calling_convention.GetFpuRegisterAt(index),
2245 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002246 DCHECK(ExpectedPairLayout(result));
2247 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002248 } else {
2249 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002250 }
2251 }
2252
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002253 case Primitive::kPrimVoid:
2254 LOG(FATAL) << "Unexpected parameter type " << type;
2255 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002256 }
Roland Levillain3b359c72015-11-17 19:35:12 +00002257 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002258}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002259
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002260Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002261 switch (type) {
2262 case Primitive::kPrimBoolean:
2263 case Primitive::kPrimByte:
2264 case Primitive::kPrimChar:
2265 case Primitive::kPrimShort:
2266 case Primitive::kPrimInt:
2267 case Primitive::kPrimNot: {
2268 return Location::RegisterLocation(R0);
2269 }
2270
2271 case Primitive::kPrimFloat: {
2272 return Location::FpuRegisterLocation(S0);
2273 }
2274
2275 case Primitive::kPrimLong: {
2276 return Location::RegisterPairLocation(R0, R1);
2277 }
2278
2279 case Primitive::kPrimDouble: {
2280 return Location::FpuRegisterPairLocation(S0, S1);
2281 }
2282
2283 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00002284 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002285 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002286
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002287 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002288}
2289
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002290Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
2291 return Location::RegisterLocation(kMethodRegisterArgument);
2292}
2293
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002294void CodeGeneratorARM::Move32(Location destination, Location source) {
2295 if (source.Equals(destination)) {
2296 return;
2297 }
2298 if (destination.IsRegister()) {
2299 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002300 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002301 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002302 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002303 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002304 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002305 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002306 } else if (destination.IsFpuRegister()) {
2307 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002308 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002309 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002310 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002311 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002312 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002313 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002314 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002315 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002316 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002317 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002318 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002319 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002320 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002321 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002322 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
2323 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002324 }
2325 }
2326}
2327
2328void CodeGeneratorARM::Move64(Location destination, Location source) {
2329 if (source.Equals(destination)) {
2330 return;
2331 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002332 if (destination.IsRegisterPair()) {
2333 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002334 EmitParallelMoves(
2335 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
2336 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002337 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002338 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002339 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
2340 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002341 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002342 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002343 } else if (source.IsFpuRegisterPair()) {
2344 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
2345 destination.AsRegisterPairHigh<Register>(),
2346 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002347 } else {
2348 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002349 DCHECK(ExpectedPairLayout(destination));
2350 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
2351 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002352 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002353 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002354 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002355 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2356 SP,
2357 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01002358 } else if (source.IsRegisterPair()) {
2359 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2360 source.AsRegisterPairLow<Register>(),
2361 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002362 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002363 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002364 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002365 } else {
2366 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002367 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002368 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002369 if (source.AsRegisterPairLow<Register>() == R1) {
2370 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002371 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
2372 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002373 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002374 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002375 SP, destination.GetStackIndex());
2376 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002377 } else if (source.IsFpuRegisterPair()) {
2378 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
2379 SP,
2380 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002381 } else {
2382 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002383 EmitParallelMoves(
2384 Location::StackSlot(source.GetStackIndex()),
2385 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002386 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002387 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002388 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
2389 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002390 }
2391 }
2392}
2393
Calin Juravle175dc732015-08-25 15:42:32 +01002394void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
2395 DCHECK(location.IsRegister());
2396 __ LoadImmediate(location.AsRegister<Register>(), value);
2397}
2398
Calin Juravlee460d1d2015-09-29 04:52:17 +01002399void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002400 HParallelMove move(GetGraph()->GetArena());
2401 move.AddMove(src, dst, dst_type, nullptr);
2402 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002403}
2404
2405void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
2406 if (location.IsRegister()) {
2407 locations->AddTemp(location);
2408 } else if (location.IsRegisterPair()) {
2409 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
2410 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
2411 } else {
2412 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2413 }
2414}
2415
Calin Juravle175dc732015-08-25 15:42:32 +01002416void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
2417 HInstruction* instruction,
2418 uint32_t dex_pc,
2419 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002420 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002421 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002422 if (EntrypointRequiresStackMap(entrypoint)) {
2423 RecordPcInfo(instruction, dex_pc, slow_path);
2424 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002425}
2426
Roland Levillaindec8f632016-07-22 17:10:06 +01002427void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2428 HInstruction* instruction,
2429 SlowPathCode* slow_path) {
2430 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002431 GenerateInvokeRuntime(entry_point_offset);
2432}
2433
2434void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01002435 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2436 __ blx(LR);
2437}
2438
David Brazdilfc6a86a2015-06-26 10:33:45 +00002439void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002440 DCHECK(!successor->IsExitBlock());
2441
2442 HBasicBlock* block = got->GetBlock();
2443 HInstruction* previous = got->GetPrevious();
2444
2445 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00002446 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002447 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2448 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2449 return;
2450 }
2451
2452 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2453 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2454 }
2455 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002456 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002457 }
2458}
2459
David Brazdilfc6a86a2015-06-26 10:33:45 +00002460void LocationsBuilderARM::VisitGoto(HGoto* got) {
2461 got->SetLocations(nullptr);
2462}
2463
2464void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
2465 HandleGoto(got, got->GetSuccessor());
2466}
2467
2468void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2469 try_boundary->SetLocations(nullptr);
2470}
2471
2472void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2473 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2474 if (!successor->IsExitBlock()) {
2475 HandleGoto(try_boundary, successor);
2476 }
2477}
2478
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002479void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002480 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002481}
2482
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002483void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002484}
2485
Nicolas Geoffray30826612017-05-10 11:59:26 +00002486void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
2487 Label* true_label,
2488 Label* false_label) {
2489 LocationSummary* locations = cond->GetLocations();
2490 Location left = locations->InAt(0);
2491 Location right = locations->InAt(1);
2492 IfCondition if_cond = cond->GetCondition();
2493
2494 Register left_high = left.AsRegisterPairHigh<Register>();
2495 Register left_low = left.AsRegisterPairLow<Register>();
2496 IfCondition true_high_cond = if_cond;
2497 IfCondition false_high_cond = cond->GetOppositeCondition();
2498 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
2499
2500 // Set the conditions for the test, remembering that == needs to be
2501 // decided using the low words.
2502 switch (if_cond) {
2503 case kCondEQ:
2504 case kCondNE:
2505 // Nothing to do.
2506 break;
2507 case kCondLT:
2508 false_high_cond = kCondGT;
2509 break;
2510 case kCondLE:
2511 true_high_cond = kCondLT;
2512 break;
2513 case kCondGT:
2514 false_high_cond = kCondLT;
2515 break;
2516 case kCondGE:
2517 true_high_cond = kCondGT;
2518 break;
2519 case kCondB:
2520 false_high_cond = kCondA;
2521 break;
2522 case kCondBE:
2523 true_high_cond = kCondB;
2524 break;
2525 case kCondA:
2526 false_high_cond = kCondB;
2527 break;
2528 case kCondAE:
2529 true_high_cond = kCondA;
2530 break;
2531 }
2532 if (right.IsConstant()) {
2533 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
2534 int32_t val_low = Low32Bits(value);
2535 int32_t val_high = High32Bits(value);
2536
2537 __ CmpConstant(left_high, val_high);
2538 if (if_cond == kCondNE) {
2539 __ b(true_label, ARMCondition(true_high_cond));
2540 } else if (if_cond == kCondEQ) {
2541 __ b(false_label, ARMCondition(false_high_cond));
2542 } else {
2543 __ b(true_label, ARMCondition(true_high_cond));
2544 __ b(false_label, ARMCondition(false_high_cond));
2545 }
2546 // Must be equal high, so compare the lows.
2547 __ CmpConstant(left_low, val_low);
2548 } else {
2549 Register right_high = right.AsRegisterPairHigh<Register>();
2550 Register right_low = right.AsRegisterPairLow<Register>();
2551
2552 __ cmp(left_high, ShifterOperand(right_high));
2553 if (if_cond == kCondNE) {
2554 __ b(true_label, ARMCondition(true_high_cond));
2555 } else if (if_cond == kCondEQ) {
2556 __ b(false_label, ARMCondition(false_high_cond));
2557 } else {
2558 __ b(true_label, ARMCondition(true_high_cond));
2559 __ b(false_label, ARMCondition(false_high_cond));
2560 }
2561 // Must be equal high, so compare the lows.
2562 __ cmp(left_low, ShifterOperand(right_low));
2563 }
2564 // The last comparison might be unsigned.
2565 // TODO: optimize cases where this is always true/false
2566 __ b(true_label, final_condition);
2567}
2568
David Brazdil0debae72015-11-12 18:37:00 +00002569void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
2570 Label* true_target_in,
2571 Label* false_target_in) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002572 if (CanGenerateTest(condition, codegen_->GetAssembler())) {
2573 Label* non_fallthrough_target;
2574 bool invert;
2575
2576 if (true_target_in == nullptr) {
2577 DCHECK(false_target_in != nullptr);
2578 non_fallthrough_target = false_target_in;
2579 invert = true;
2580 } else {
2581 non_fallthrough_target = true_target_in;
2582 invert = false;
2583 }
2584
2585 const auto cond = GenerateTest(condition, invert, codegen_);
2586
2587 __ b(non_fallthrough_target, cond.first);
2588
2589 if (false_target_in != nullptr && false_target_in != non_fallthrough_target) {
2590 __ b(false_target_in);
2591 }
2592
2593 return;
2594 }
2595
David Brazdil0debae72015-11-12 18:37:00 +00002596 // Generated branching requires both targets to be explicit. If either of the
2597 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2598 Label fallthrough_target;
2599 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
2600 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
2601
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002602 DCHECK_EQ(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
Nicolas Geoffray30826612017-05-10 11:59:26 +00002603 GenerateLongComparesAndJumps(condition, true_target, false_target);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002604
David Brazdil0debae72015-11-12 18:37:00 +00002605 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002606 __ b(false_target);
2607 }
David Brazdil0debae72015-11-12 18:37:00 +00002608
2609 if (fallthrough_target.IsLinked()) {
2610 __ Bind(&fallthrough_target);
2611 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002612}
2613
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002614void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002615 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002616 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002617 Label* false_target) {
2618 HInstruction* cond = instruction->InputAt(condition_input_index);
2619
2620 if (true_target == nullptr && false_target == nullptr) {
2621 // Nothing to do. The code always falls through.
2622 return;
2623 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002624 // Constant condition, statically compared against "true" (integer value 1).
2625 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002626 if (true_target != nullptr) {
2627 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002628 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002629 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002630 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002631 if (false_target != nullptr) {
2632 __ b(false_target);
2633 }
2634 }
2635 return;
2636 }
2637
2638 // The following code generates these patterns:
2639 // (1) true_target == nullptr && false_target != nullptr
2640 // - opposite condition true => branch to false_target
2641 // (2) true_target != nullptr && false_target == nullptr
2642 // - condition true => branch to true_target
2643 // (3) true_target != nullptr && false_target != nullptr
2644 // - condition true => branch to true_target
2645 // - branch to false_target
2646 if (IsBooleanValueOrMaterializedCondition(cond)) {
2647 // Condition has been materialized, compare the output to 0.
2648 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2649 DCHECK(cond_val.IsRegister());
2650 if (true_target == nullptr) {
2651 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
2652 } else {
2653 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002654 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002655 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002656 // Condition has not been materialized. Use its inputs as the comparison and
2657 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002658 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002659
2660 // If this is a long or FP comparison that has been folded into
2661 // the HCondition, generate the comparison directly.
2662 Primitive::Type type = condition->InputAt(0)->GetType();
2663 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2664 GenerateCompareTestAndBranch(condition, true_target, false_target);
2665 return;
2666 }
2667
Donghui Bai426b49c2016-11-08 14:55:38 +08002668 Label* non_fallthrough_target;
2669 Condition arm_cond;
David Brazdil0debae72015-11-12 18:37:00 +00002670 LocationSummary* locations = cond->GetLocations();
2671 DCHECK(locations->InAt(0).IsRegister());
2672 Register left = locations->InAt(0).AsRegister<Register>();
2673 Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08002674
David Brazdil0debae72015-11-12 18:37:00 +00002675 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002676 arm_cond = ARMCondition(condition->GetOppositeCondition());
2677 non_fallthrough_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002678 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002679 arm_cond = ARMCondition(condition->GetCondition());
2680 non_fallthrough_target = true_target;
2681 }
2682
2683 if (right.IsConstant() && (arm_cond == NE || arm_cond == EQ) &&
2684 CodeGenerator::GetInt32ValueOf(right.GetConstant()) == 0) {
2685 if (arm_cond == EQ) {
2686 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2687 } else {
2688 DCHECK_EQ(arm_cond, NE);
2689 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2690 }
2691 } else {
2692 if (right.IsRegister()) {
2693 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
2694 } else {
2695 DCHECK(right.IsConstant());
2696 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2697 }
2698
2699 __ b(non_fallthrough_target, arm_cond);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002700 }
Dave Allison20dfc792014-06-16 20:44:29 -07002701 }
David Brazdil0debae72015-11-12 18:37:00 +00002702
2703 // If neither branch falls through (case 3), the conditional branch to `true_target`
2704 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2705 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002706 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002707 }
2708}
2709
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002710void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002711 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2712 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002713 locations->SetInAt(0, Location::RequiresRegister());
2714 }
2715}
2716
2717void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002718 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2719 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2720 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2721 nullptr : codegen_->GetLabelOf(true_successor);
2722 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2723 nullptr : codegen_->GetLabelOf(false_successor);
2724 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002725}
2726
2727void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
2728 LocationSummary* locations = new (GetGraph()->GetArena())
2729 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002730 InvokeRuntimeCallingConvention calling_convention;
2731 RegisterSet caller_saves = RegisterSet::Empty();
2732 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2733 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00002734 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002735 locations->SetInAt(0, Location::RequiresRegister());
2736 }
2737}
2738
2739void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01002740 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002741 GenerateTestAndBranch(deoptimize,
2742 /* condition_input_index */ 0,
2743 slow_path->GetEntryLabel(),
2744 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002745}
Dave Allison20dfc792014-06-16 20:44:29 -07002746
Mingyao Yang063fc772016-08-02 11:02:54 -07002747void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2748 LocationSummary* locations = new (GetGraph()->GetArena())
2749 LocationSummary(flag, LocationSummary::kNoCall);
2750 locations->SetOut(Location::RequiresRegister());
2751}
2752
2753void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2754 __ LoadFromOffset(kLoadWord,
2755 flag->GetLocations()->Out().AsRegister<Register>(),
2756 SP,
2757 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2758}
2759
David Brazdil74eb1b22015-12-14 11:44:01 +00002760void LocationsBuilderARM::VisitSelect(HSelect* select) {
2761 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08002762 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
2763
2764 if (is_floating_point) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002765 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002766 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002767 } else {
2768 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002769 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002770 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002771
David Brazdil74eb1b22015-12-14 11:44:01 +00002772 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002773 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2774 // The code generator handles overlap with the values, but not with the condition.
2775 locations->SetOut(Location::SameAsFirstInput());
2776 } else if (is_floating_point) {
2777 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2778 } else {
2779 if (!locations->InAt(1).IsConstant()) {
2780 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2781 }
2782
2783 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00002784 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002785}
2786
2787void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002788 HInstruction* const condition = select->GetCondition();
2789 const LocationSummary* const locations = select->GetLocations();
2790 const Primitive::Type type = select->GetType();
2791 const Location first = locations->InAt(0);
2792 const Location out = locations->Out();
2793 const Location second = locations->InAt(1);
2794 Location src;
2795
2796 if (condition->IsIntConstant()) {
2797 if (condition->AsIntConstant()->IsFalse()) {
2798 src = first;
2799 } else {
2800 src = second;
2801 }
2802
2803 codegen_->MoveLocation(out, src, type);
2804 return;
2805 }
2806
2807 if (!Primitive::IsFloatingPointType(type) &&
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002808 (IsBooleanValueOrMaterializedCondition(condition) ||
2809 CanGenerateTest(condition->AsCondition(), codegen_->GetAssembler()))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002810 bool invert = false;
2811
2812 if (out.Equals(second)) {
2813 src = first;
2814 invert = true;
2815 } else if (out.Equals(first)) {
2816 src = second;
2817 } else if (second.IsConstant()) {
2818 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2819 src = second;
2820 } else if (first.IsConstant()) {
2821 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2822 src = first;
2823 invert = true;
2824 } else {
2825 src = second;
2826 }
2827
2828 if (CanGenerateConditionalMove(out, src)) {
2829 if (!out.Equals(first) && !out.Equals(second)) {
2830 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2831 }
2832
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002833 std::pair<Condition, Condition> cond;
2834
2835 if (IsBooleanValueOrMaterializedCondition(condition)) {
2836 __ CmpConstant(locations->InAt(2).AsRegister<Register>(), 0);
2837 cond = invert ? std::make_pair(EQ, NE) : std::make_pair(NE, EQ);
2838 } else {
2839 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2840 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002841
2842 if (out.IsRegister()) {
2843 ShifterOperand operand;
2844
2845 if (src.IsConstant()) {
2846 operand = ShifterOperand(CodeGenerator::GetInt32ValueOf(src.GetConstant()));
2847 } else {
2848 DCHECK(src.IsRegister());
2849 operand = ShifterOperand(src.AsRegister<Register>());
2850 }
2851
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002852 __ it(cond.first);
2853 __ mov(out.AsRegister<Register>(), operand, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08002854 } else {
2855 DCHECK(out.IsRegisterPair());
2856
2857 ShifterOperand operand_high;
2858 ShifterOperand operand_low;
2859
2860 if (src.IsConstant()) {
2861 const int64_t value = src.GetConstant()->AsLongConstant()->GetValue();
2862
2863 operand_high = ShifterOperand(High32Bits(value));
2864 operand_low = ShifterOperand(Low32Bits(value));
2865 } else {
2866 DCHECK(src.IsRegisterPair());
2867 operand_high = ShifterOperand(src.AsRegisterPairHigh<Register>());
2868 operand_low = ShifterOperand(src.AsRegisterPairLow<Register>());
2869 }
2870
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002871 __ it(cond.first);
2872 __ mov(out.AsRegisterPairLow<Register>(), operand_low, cond.first);
2873 __ it(cond.first);
2874 __ mov(out.AsRegisterPairHigh<Register>(), operand_high, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08002875 }
2876
2877 return;
2878 }
2879 }
2880
2881 Label* false_target = nullptr;
2882 Label* true_target = nullptr;
2883 Label select_end;
2884 Label* target = codegen_->GetFinalLabel(select, &select_end);
2885
2886 if (out.Equals(second)) {
2887 true_target = target;
2888 src = first;
2889 } else {
2890 false_target = target;
2891 src = second;
2892
2893 if (!out.Equals(first)) {
2894 codegen_->MoveLocation(out, first, type);
2895 }
2896 }
2897
2898 GenerateTestAndBranch(select, 2, true_target, false_target);
2899 codegen_->MoveLocation(out, src, type);
2900
2901 if (select_end.IsLinked()) {
2902 __ Bind(&select_end);
2903 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002904}
2905
David Srbecky0cf44932015-12-09 14:09:59 +00002906void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2907 new (GetGraph()->GetArena()) LocationSummary(info);
2908}
2909
David Srbeckyd28f4a02016-03-14 17:14:24 +00002910void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2911 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002912}
2913
2914void CodeGeneratorARM::GenerateNop() {
2915 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002916}
2917
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002918void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002919 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002920 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002921 // Handle the long/FP comparisons made in instruction simplification.
2922 switch (cond->InputAt(0)->GetType()) {
2923 case Primitive::kPrimLong:
2924 locations->SetInAt(0, Location::RequiresRegister());
2925 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002926 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002927 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002928 }
2929 break;
2930
2931 case Primitive::kPrimFloat:
2932 case Primitive::kPrimDouble:
2933 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002934 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002935 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002936 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2937 }
2938 break;
2939
2940 default:
2941 locations->SetInAt(0, Location::RequiresRegister());
2942 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002943 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002944 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2945 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002946 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002947}
2948
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002949void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002950 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002951 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002952 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002953
Nicolas Geoffray30826612017-05-10 11:59:26 +00002954 const Register out = cond->GetLocations()->Out().AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002955
Nicolas Geoffray30826612017-05-10 11:59:26 +00002956 if (ArmAssembler::IsLowRegister(out) && CanGenerateTest(cond, codegen_->GetAssembler())) {
2957 const auto condition = GenerateTest(cond, false, codegen_);
2958
2959 __ it(condition.first);
2960 __ mov(out, ShifterOperand(1), condition.first);
2961 __ it(condition.second);
2962 __ mov(out, ShifterOperand(0), condition.second);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002963 return;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002964 }
2965
Nicolas Geoffray30826612017-05-10 11:59:26 +00002966 // Convert the jumps into the result.
2967 Label done_label;
2968 Label* const final_label = codegen_->GetFinalLabel(cond, &done_label);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002969
Nicolas Geoffray30826612017-05-10 11:59:26 +00002970 if (cond->InputAt(0)->GetType() == Primitive::kPrimLong) {
2971 Label true_label, false_label;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002972
Nicolas Geoffray30826612017-05-10 11:59:26 +00002973 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002974
Nicolas Geoffray30826612017-05-10 11:59:26 +00002975 // False case: result = 0.
2976 __ Bind(&false_label);
2977 __ LoadImmediate(out, 0);
2978 __ b(final_label);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002979
Nicolas Geoffray30826612017-05-10 11:59:26 +00002980 // True case: result = 1.
2981 __ Bind(&true_label);
2982 __ LoadImmediate(out, 1);
2983 } else {
2984 DCHECK(CanGenerateTest(cond, codegen_->GetAssembler()));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002985
Nicolas Geoffray30826612017-05-10 11:59:26 +00002986 const auto condition = GenerateTest(cond, false, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002987
Nicolas Geoffray30826612017-05-10 11:59:26 +00002988 __ mov(out, ShifterOperand(0), AL, kCcKeep);
2989 __ b(final_label, condition.second);
2990 __ LoadImmediate(out, 1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002991 }
Anton Kirilov6f644202017-02-27 18:29:45 +00002992
Nicolas Geoffray30826612017-05-10 11:59:26 +00002993 if (done_label.IsLinked()) {
2994 __ Bind(&done_label);
2995 }
Dave Allison20dfc792014-06-16 20:44:29 -07002996}
2997
2998void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002999 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003000}
3001
3002void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003003 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003004}
3005
3006void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003007 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003008}
3009
3010void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003011 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003012}
3013
3014void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003015 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003016}
3017
3018void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003019 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003020}
3021
3022void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003023 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003024}
3025
3026void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003027 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003028}
3029
3030void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003031 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003032}
3033
3034void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003035 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003036}
3037
3038void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003039 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003040}
3041
3042void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003043 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003044}
3045
Aart Bike9f37602015-10-09 11:15:55 -07003046void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003047 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003048}
3049
3050void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003051 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003052}
3053
3054void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003055 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003056}
3057
3058void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003059 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003060}
3061
3062void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003063 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003064}
3065
3066void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003067 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003068}
3069
3070void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003071 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003072}
3073
3074void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003075 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003076}
3077
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003078void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003079 LocationSummary* locations =
3080 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003081 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003082}
3083
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003084void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01003085 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003086}
3087
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003088void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
3089 LocationSummary* locations =
3090 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3091 locations->SetOut(Location::ConstantLocation(constant));
3092}
3093
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003094void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003095 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003096}
3097
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003098void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003099 LocationSummary* locations =
3100 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003101 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003102}
3103
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003104void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003105 // Will be generated at use site.
3106}
3107
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003108void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
3109 LocationSummary* locations =
3110 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3111 locations->SetOut(Location::ConstantLocation(constant));
3112}
3113
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003114void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003115 // Will be generated at use site.
3116}
3117
3118void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
3119 LocationSummary* locations =
3120 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3121 locations->SetOut(Location::ConstantLocation(constant));
3122}
3123
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003124void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003125 // Will be generated at use site.
3126}
3127
Igor Murashkind01745e2017-04-05 16:40:31 -07003128void LocationsBuilderARM::VisitConstructorFence(HConstructorFence* constructor_fence) {
3129 constructor_fence->SetLocations(nullptr);
3130}
3131
3132void InstructionCodeGeneratorARM::VisitConstructorFence(
3133 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3134 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3135}
3136
Calin Juravle27df7582015-04-17 19:12:31 +01003137void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3138 memory_barrier->SetLocations(nullptr);
3139}
3140
3141void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00003142 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01003143}
3144
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003145void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003146 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003147}
3148
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003149void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003150 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003151}
3152
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003153void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003154 LocationSummary* locations =
3155 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003156 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003157}
3158
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003159void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003160 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003161}
3162
Calin Juravle175dc732015-08-25 15:42:32 +01003163void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3164 // The trampoline uses the same calling convention as dex calling conventions,
3165 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3166 // the method_idx.
3167 HandleInvoke(invoke);
3168}
3169
3170void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3171 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3172}
3173
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003174void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003175 // Explicit clinit checks triggered by static invokes must have been pruned by
3176 // art::PrepareForRegisterAllocation.
3177 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003178
Vladimir Marko68c981f2016-08-26 13:13:33 +01003179 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003180 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00003181 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3182 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3183 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003184 return;
3185 }
3186
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003187 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00003188
3189 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3190 if (invoke->HasPcRelativeDexCache()) {
3191 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3192 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003193}
3194
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003195static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
3196 if (invoke->GetLocations()->Intrinsified()) {
3197 IntrinsicCodeGeneratorARM intrinsic(codegen);
3198 intrinsic.Dispatch(invoke);
3199 return true;
3200 }
3201 return false;
3202}
3203
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003204void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003205 // Explicit clinit checks triggered by static invokes must have been pruned by
3206 // art::PrepareForRegisterAllocation.
3207 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003208
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003209 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3210 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003211 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003212
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003213 LocationSummary* locations = invoke->GetLocations();
3214 codegen_->GenerateStaticOrDirectCall(
3215 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003216 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003217}
3218
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003219void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003220 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003221 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003222}
3223
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003224void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01003225 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003226 if (intrinsic.TryDispatch(invoke)) {
3227 return;
3228 }
3229
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003230 HandleInvoke(invoke);
3231}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003232
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003233void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003234 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3235 return;
3236 }
3237
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003238 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003239 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003240 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003241}
3242
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003243void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3244 HandleInvoke(invoke);
3245 // Add the hidden argument.
3246 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
3247}
3248
3249void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3250 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00003251 LocationSummary* locations = invoke->GetLocations();
3252 Register temp = locations->GetTemp(0).AsRegister<Register>();
3253 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003254 Location receiver = locations->InAt(0);
3255 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3256
Roland Levillain3b359c72015-11-17 19:35:12 +00003257 // Set the hidden argument. This is safe to do this here, as R12
3258 // won't be modified thereafter, before the `blx` (call) instruction.
3259 DCHECK_EQ(R12, hidden_reg);
3260 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003261
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003262 if (receiver.IsStackSlot()) {
3263 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00003264 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003265 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3266 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00003267 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00003268 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003269 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003270 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00003271 // Instead of simply (possibly) unpoisoning `temp` here, we should
3272 // emit a read barrier for the previous class reference load.
3273 // However this is not required in practice, as this is an
3274 // intermediate/temporary reference and because the current
3275 // concurrent copying collector keeps the from-space memory
3276 // intact/accessible until the end of the marking phase (the
3277 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003278 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003279 __ LoadFromOffset(kLoadWord, temp, temp,
3280 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3281 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003282 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003283 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003284 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00003285 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07003286 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003287 // LR = temp->GetEntryPoint();
3288 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
3289 // LR();
3290 __ blx(LR);
3291 DCHECK(!codegen_->IsLeafMethod());
3292 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3293}
3294
Orion Hodsonac141392017-01-13 11:53:47 +00003295void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3296 HandleInvoke(invoke);
3297}
3298
3299void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3300 codegen_->GenerateInvokePolymorphicCall(invoke);
3301}
3302
Roland Levillain88cb1752014-10-20 16:36:47 +01003303void LocationsBuilderARM::VisitNeg(HNeg* neg) {
3304 LocationSummary* locations =
3305 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3306 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003307 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01003308 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003309 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3310 break;
3311 }
3312 case Primitive::kPrimLong: {
3313 locations->SetInAt(0, Location::RequiresRegister());
3314 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003315 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003316 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003317
Roland Levillain88cb1752014-10-20 16:36:47 +01003318 case Primitive::kPrimFloat:
3319 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003320 locations->SetInAt(0, Location::RequiresFpuRegister());
3321 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003322 break;
3323
3324 default:
3325 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3326 }
3327}
3328
3329void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
3330 LocationSummary* locations = neg->GetLocations();
3331 Location out = locations->Out();
3332 Location in = locations->InAt(0);
3333 switch (neg->GetResultType()) {
3334 case Primitive::kPrimInt:
3335 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003336 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01003337 break;
3338
3339 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003340 DCHECK(in.IsRegisterPair());
3341 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3342 __ rsbs(out.AsRegisterPairLow<Register>(),
3343 in.AsRegisterPairLow<Register>(),
3344 ShifterOperand(0));
3345 // We cannot emit an RSC (Reverse Subtract with Carry)
3346 // instruction here, as it does not exist in the Thumb-2
3347 // instruction set. We use the following approach
3348 // using SBC and SUB instead.
3349 //
3350 // out.hi = -C
3351 __ sbc(out.AsRegisterPairHigh<Register>(),
3352 out.AsRegisterPairHigh<Register>(),
3353 ShifterOperand(out.AsRegisterPairHigh<Register>()));
3354 // out.hi = out.hi - in.hi
3355 __ sub(out.AsRegisterPairHigh<Register>(),
3356 out.AsRegisterPairHigh<Register>(),
3357 ShifterOperand(in.AsRegisterPairHigh<Register>()));
3358 break;
3359
Roland Levillain88cb1752014-10-20 16:36:47 +01003360 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003361 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003362 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00003363 break;
3364
Roland Levillain88cb1752014-10-20 16:36:47 +01003365 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003366 DCHECK(in.IsFpuRegisterPair());
3367 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3368 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01003369 break;
3370
3371 default:
3372 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3373 }
3374}
3375
Roland Levillaindff1f282014-11-05 14:15:05 +00003376void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003377 Primitive::Type result_type = conversion->GetResultType();
3378 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003379 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00003380
Roland Levillain5b3ee562015-04-14 16:02:41 +01003381 // The float-to-long, double-to-long and long-to-float type conversions
3382 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003383 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01003384 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3385 && result_type == Primitive::kPrimLong)
3386 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003387 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00003388 : LocationSummary::kNoCall;
3389 LocationSummary* locations =
3390 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3391
David Brazdilb2bd1c52015-03-25 11:17:37 +00003392 // The Java language does not allow treating boolean as an integral type but
3393 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00003394
Roland Levillaindff1f282014-11-05 14:15:05 +00003395 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003396 case Primitive::kPrimByte:
3397 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003398 case Primitive::kPrimLong:
3399 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003400 case Primitive::kPrimBoolean:
3401 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003402 case Primitive::kPrimShort:
3403 case Primitive::kPrimInt:
3404 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003405 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003406 locations->SetInAt(0, Location::RequiresRegister());
3407 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3408 break;
3409
3410 default:
3411 LOG(FATAL) << "Unexpected type conversion from " << input_type
3412 << " to " << result_type;
3413 }
3414 break;
3415
Roland Levillain01a8d712014-11-14 16:27:39 +00003416 case Primitive::kPrimShort:
3417 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003418 case Primitive::kPrimLong:
3419 // Type conversion from long to short 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 Levillain01a8d712014-11-14 16:27:39 +00003422 case Primitive::kPrimByte:
3423 case Primitive::kPrimInt:
3424 case Primitive::kPrimChar:
3425 // Processing a Dex `int-to-short' 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 Levillain946e1432014-11-11 17:35:19 +00003436 case Primitive::kPrimInt:
3437 switch (input_type) {
3438 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003439 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003440 locations->SetInAt(0, Location::Any());
3441 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3442 break;
3443
3444 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00003445 // Processing a Dex `float-to-int' instruction.
3446 locations->SetInAt(0, Location::RequiresFpuRegister());
3447 locations->SetOut(Location::RequiresRegister());
3448 locations->AddTemp(Location::RequiresFpuRegister());
3449 break;
3450
Roland Levillain946e1432014-11-11 17:35:19 +00003451 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003452 // Processing a Dex `double-to-int' instruction.
3453 locations->SetInAt(0, Location::RequiresFpuRegister());
3454 locations->SetOut(Location::RequiresRegister());
3455 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00003456 break;
3457
3458 default:
3459 LOG(FATAL) << "Unexpected type conversion from " << input_type
3460 << " to " << result_type;
3461 }
3462 break;
3463
Roland Levillaindff1f282014-11-05 14:15:05 +00003464 case Primitive::kPrimLong:
3465 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003466 case Primitive::kPrimBoolean:
3467 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003468 case Primitive::kPrimByte:
3469 case Primitive::kPrimShort:
3470 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003471 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003472 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003473 locations->SetInAt(0, Location::RequiresRegister());
3474 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3475 break;
3476
Roland Levillain624279f2014-12-04 11:54:28 +00003477 case Primitive::kPrimFloat: {
3478 // Processing a Dex `float-to-long' instruction.
3479 InvokeRuntimeCallingConvention calling_convention;
3480 locations->SetInAt(0, Location::FpuRegisterLocation(
3481 calling_convention.GetFpuRegisterAt(0)));
3482 locations->SetOut(Location::RegisterPairLocation(R0, R1));
3483 break;
3484 }
3485
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003486 case Primitive::kPrimDouble: {
3487 // Processing a Dex `double-to-long' instruction.
3488 InvokeRuntimeCallingConvention calling_convention;
3489 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3490 calling_convention.GetFpuRegisterAt(0),
3491 calling_convention.GetFpuRegisterAt(1)));
3492 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00003493 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003494 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003495
3496 default:
3497 LOG(FATAL) << "Unexpected type conversion from " << input_type
3498 << " to " << result_type;
3499 }
3500 break;
3501
Roland Levillain981e4542014-11-14 11:47:14 +00003502 case Primitive::kPrimChar:
3503 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003504 case Primitive::kPrimLong:
3505 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003506 case Primitive::kPrimBoolean:
3507 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003508 case Primitive::kPrimByte:
3509 case Primitive::kPrimShort:
3510 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003511 // Processing a Dex `int-to-char' instruction.
3512 locations->SetInAt(0, Location::RequiresRegister());
3513 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3514 break;
3515
3516 default:
3517 LOG(FATAL) << "Unexpected type conversion from " << input_type
3518 << " to " << result_type;
3519 }
3520 break;
3521
Roland Levillaindff1f282014-11-05 14:15:05 +00003522 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003523 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003524 case Primitive::kPrimBoolean:
3525 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003526 case Primitive::kPrimByte:
3527 case Primitive::kPrimShort:
3528 case Primitive::kPrimInt:
3529 case Primitive::kPrimChar:
3530 // Processing a Dex `int-to-float' instruction.
3531 locations->SetInAt(0, Location::RequiresRegister());
3532 locations->SetOut(Location::RequiresFpuRegister());
3533 break;
3534
Roland Levillain5b3ee562015-04-14 16:02:41 +01003535 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00003536 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01003537 InvokeRuntimeCallingConvention calling_convention;
3538 locations->SetInAt(0, Location::RegisterPairLocation(
3539 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3540 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00003541 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01003542 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003543
Roland Levillaincff13742014-11-17 14:32:17 +00003544 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003545 // Processing a Dex `double-to-float' instruction.
3546 locations->SetInAt(0, Location::RequiresFpuRegister());
3547 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003548 break;
3549
3550 default:
3551 LOG(FATAL) << "Unexpected type conversion from " << input_type
3552 << " to " << result_type;
3553 };
3554 break;
3555
Roland Levillaindff1f282014-11-05 14:15:05 +00003556 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003557 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003558 case Primitive::kPrimBoolean:
3559 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003560 case Primitive::kPrimByte:
3561 case Primitive::kPrimShort:
3562 case Primitive::kPrimInt:
3563 case Primitive::kPrimChar:
3564 // Processing a Dex `int-to-double' instruction.
3565 locations->SetInAt(0, Location::RequiresRegister());
3566 locations->SetOut(Location::RequiresFpuRegister());
3567 break;
3568
3569 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003570 // Processing a Dex `long-to-double' instruction.
3571 locations->SetInAt(0, Location::RequiresRegister());
3572 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01003573 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00003574 locations->AddTemp(Location::RequiresFpuRegister());
3575 break;
3576
Roland Levillaincff13742014-11-17 14:32:17 +00003577 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003578 // Processing a Dex `float-to-double' instruction.
3579 locations->SetInAt(0, Location::RequiresFpuRegister());
3580 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003581 break;
3582
3583 default:
3584 LOG(FATAL) << "Unexpected type conversion from " << input_type
3585 << " to " << result_type;
3586 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003587 break;
3588
3589 default:
3590 LOG(FATAL) << "Unexpected type conversion from " << input_type
3591 << " to " << result_type;
3592 }
3593}
3594
3595void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
3596 LocationSummary* locations = conversion->GetLocations();
3597 Location out = locations->Out();
3598 Location in = locations->InAt(0);
3599 Primitive::Type result_type = conversion->GetResultType();
3600 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003601 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00003602 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003603 case Primitive::kPrimByte:
3604 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003605 case Primitive::kPrimLong:
3606 // Type conversion from long to byte is a result of code transformations.
3607 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
3608 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003609 case Primitive::kPrimBoolean:
3610 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003611 case Primitive::kPrimShort:
3612 case Primitive::kPrimInt:
3613 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003614 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003615 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00003616 break;
3617
3618 default:
3619 LOG(FATAL) << "Unexpected type conversion from " << input_type
3620 << " to " << result_type;
3621 }
3622 break;
3623
Roland Levillain01a8d712014-11-14 16:27:39 +00003624 case Primitive::kPrimShort:
3625 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003626 case Primitive::kPrimLong:
3627 // Type conversion from long to short is a result of code transformations.
3628 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3629 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003630 case Primitive::kPrimBoolean:
3631 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003632 case Primitive::kPrimByte:
3633 case Primitive::kPrimInt:
3634 case Primitive::kPrimChar:
3635 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003636 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00003637 break;
3638
3639 default:
3640 LOG(FATAL) << "Unexpected type conversion from " << input_type
3641 << " to " << result_type;
3642 }
3643 break;
3644
Roland Levillain946e1432014-11-11 17:35:19 +00003645 case Primitive::kPrimInt:
3646 switch (input_type) {
3647 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003648 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003649 DCHECK(out.IsRegister());
3650 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003651 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003652 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003653 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00003654 } else {
3655 DCHECK(in.IsConstant());
3656 DCHECK(in.GetConstant()->IsLongConstant());
3657 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003658 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00003659 }
3660 break;
3661
Roland Levillain3f8f9362014-12-02 17:45:01 +00003662 case Primitive::kPrimFloat: {
3663 // Processing a Dex `float-to-int' instruction.
3664 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003665 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00003666 __ vmovrs(out.AsRegister<Register>(), temp);
3667 break;
3668 }
3669
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003670 case Primitive::kPrimDouble: {
3671 // Processing a Dex `double-to-int' instruction.
3672 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003673 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003674 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00003675 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003676 }
Roland Levillain946e1432014-11-11 17:35:19 +00003677
3678 default:
3679 LOG(FATAL) << "Unexpected type conversion from " << input_type
3680 << " to " << result_type;
3681 }
3682 break;
3683
Roland Levillaindff1f282014-11-05 14:15:05 +00003684 case Primitive::kPrimLong:
3685 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003686 case Primitive::kPrimBoolean:
3687 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003688 case Primitive::kPrimByte:
3689 case Primitive::kPrimShort:
3690 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003691 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003692 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003693 DCHECK(out.IsRegisterPair());
3694 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003695 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00003696 // Sign extension.
3697 __ Asr(out.AsRegisterPairHigh<Register>(),
3698 out.AsRegisterPairLow<Register>(),
3699 31);
3700 break;
3701
3702 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00003703 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003704 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003705 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003706 break;
3707
Roland Levillaindff1f282014-11-05 14:15:05 +00003708 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003709 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003710 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003711 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003712 break;
3713
3714 default:
3715 LOG(FATAL) << "Unexpected type conversion from " << input_type
3716 << " to " << result_type;
3717 }
3718 break;
3719
Roland Levillain981e4542014-11-14 11:47:14 +00003720 case Primitive::kPrimChar:
3721 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003722 case Primitive::kPrimLong:
3723 // Type conversion from long to char is a result of code transformations.
3724 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3725 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003726 case Primitive::kPrimBoolean:
3727 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003728 case Primitive::kPrimByte:
3729 case Primitive::kPrimShort:
3730 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003731 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003732 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00003733 break;
3734
3735 default:
3736 LOG(FATAL) << "Unexpected type conversion from " << input_type
3737 << " to " << result_type;
3738 }
3739 break;
3740
Roland Levillaindff1f282014-11-05 14:15:05 +00003741 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003742 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003743 case Primitive::kPrimBoolean:
3744 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003745 case Primitive::kPrimByte:
3746 case Primitive::kPrimShort:
3747 case Primitive::kPrimInt:
3748 case Primitive::kPrimChar: {
3749 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003750 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
3751 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003752 break;
3753 }
3754
Roland Levillain5b3ee562015-04-14 16:02:41 +01003755 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003756 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003757 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003758 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00003759 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003760
Roland Levillaincff13742014-11-17 14:32:17 +00003761 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003762 // Processing a Dex `double-to-float' instruction.
3763 __ vcvtsd(out.AsFpuRegister<SRegister>(),
3764 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00003765 break;
3766
3767 default:
3768 LOG(FATAL) << "Unexpected type conversion from " << input_type
3769 << " to " << result_type;
3770 };
3771 break;
3772
Roland Levillaindff1f282014-11-05 14:15:05 +00003773 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003774 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003775 case Primitive::kPrimBoolean:
3776 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003777 case Primitive::kPrimByte:
3778 case Primitive::kPrimShort:
3779 case Primitive::kPrimInt:
3780 case Primitive::kPrimChar: {
3781 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003782 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003783 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3784 out.AsFpuRegisterPairLow<SRegister>());
3785 break;
3786 }
3787
Roland Levillain647b9ed2014-11-27 12:06:00 +00003788 case Primitive::kPrimLong: {
3789 // Processing a Dex `long-to-double' instruction.
3790 Register low = in.AsRegisterPairLow<Register>();
3791 Register high = in.AsRegisterPairHigh<Register>();
3792 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
3793 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003794 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00003795 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003796 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
3797 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003798
Roland Levillain682393c2015-04-14 15:57:52 +01003799 // temp_d = int-to-double(high)
3800 __ vmovsr(temp_s, high);
3801 __ vcvtdi(temp_d, temp_s);
3802 // constant_d = k2Pow32EncodingForDouble
3803 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
3804 // out_d = unsigned-to-double(low)
3805 __ vmovsr(out_s, low);
3806 __ vcvtdu(out_d, out_s);
3807 // out_d += temp_d * constant_d
3808 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003809 break;
3810 }
3811
Roland Levillaincff13742014-11-17 14:32:17 +00003812 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003813 // Processing a Dex `float-to-double' instruction.
3814 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3815 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003816 break;
3817
3818 default:
3819 LOG(FATAL) << "Unexpected type conversion from " << input_type
3820 << " to " << result_type;
3821 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003822 break;
3823
3824 default:
3825 LOG(FATAL) << "Unexpected type conversion from " << input_type
3826 << " to " << result_type;
3827 }
3828}
3829
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003830void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003831 LocationSummary* locations =
3832 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003833 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003834 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003835 locations->SetInAt(0, Location::RequiresRegister());
3836 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003837 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3838 break;
3839 }
3840
3841 case Primitive::kPrimLong: {
3842 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003843 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003844 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003845 break;
3846 }
3847
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003848 case Primitive::kPrimFloat:
3849 case Primitive::kPrimDouble: {
3850 locations->SetInAt(0, Location::RequiresFpuRegister());
3851 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003852 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003853 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003854 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003855
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003856 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003857 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003858 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003859}
3860
3861void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
3862 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003863 Location out = locations->Out();
3864 Location first = locations->InAt(0);
3865 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003866 switch (add->GetResultType()) {
3867 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003868 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003869 __ add(out.AsRegister<Register>(),
3870 first.AsRegister<Register>(),
3871 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003872 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003873 __ AddConstant(out.AsRegister<Register>(),
3874 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003875 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003876 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003877 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003878
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003879 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003880 if (second.IsConstant()) {
3881 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3882 GenerateAddLongConst(out, first, value);
3883 } else {
3884 DCHECK(second.IsRegisterPair());
3885 __ adds(out.AsRegisterPairLow<Register>(),
3886 first.AsRegisterPairLow<Register>(),
3887 ShifterOperand(second.AsRegisterPairLow<Register>()));
3888 __ adc(out.AsRegisterPairHigh<Register>(),
3889 first.AsRegisterPairHigh<Register>(),
3890 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3891 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003892 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003893 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003894
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003895 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00003896 __ vadds(out.AsFpuRegister<SRegister>(),
3897 first.AsFpuRegister<SRegister>(),
3898 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003899 break;
3900
3901 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003902 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3903 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3904 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003905 break;
3906
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003907 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003908 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003909 }
3910}
3911
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003912void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003913 LocationSummary* locations =
3914 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003915 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003916 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003917 locations->SetInAt(0, Location::RequiresRegister());
3918 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003919 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3920 break;
3921 }
3922
3923 case Primitive::kPrimLong: {
3924 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003925 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003926 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003927 break;
3928 }
Calin Juravle11351682014-10-23 15:38:15 +01003929 case Primitive::kPrimFloat:
3930 case Primitive::kPrimDouble: {
3931 locations->SetInAt(0, Location::RequiresFpuRegister());
3932 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003933 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003934 break;
Calin Juravle11351682014-10-23 15:38:15 +01003935 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003936 default:
Calin Juravle11351682014-10-23 15:38:15 +01003937 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003938 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003939}
3940
3941void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3942 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003943 Location out = locations->Out();
3944 Location first = locations->InAt(0);
3945 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003946 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003947 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003948 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003949 __ sub(out.AsRegister<Register>(),
3950 first.AsRegister<Register>(),
3951 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003952 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003953 __ AddConstant(out.AsRegister<Register>(),
3954 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003955 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003956 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003957 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003958 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003959
Calin Juravle11351682014-10-23 15:38:15 +01003960 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003961 if (second.IsConstant()) {
3962 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3963 GenerateAddLongConst(out, first, -value);
3964 } else {
3965 DCHECK(second.IsRegisterPair());
3966 __ subs(out.AsRegisterPairLow<Register>(),
3967 first.AsRegisterPairLow<Register>(),
3968 ShifterOperand(second.AsRegisterPairLow<Register>()));
3969 __ sbc(out.AsRegisterPairHigh<Register>(),
3970 first.AsRegisterPairHigh<Register>(),
3971 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3972 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003973 break;
Calin Juravle11351682014-10-23 15:38:15 +01003974 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003975
Calin Juravle11351682014-10-23 15:38:15 +01003976 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003977 __ vsubs(out.AsFpuRegister<SRegister>(),
3978 first.AsFpuRegister<SRegister>(),
3979 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003980 break;
Calin Juravle11351682014-10-23 15:38:15 +01003981 }
3982
3983 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003984 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3985 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3986 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003987 break;
3988 }
3989
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003990
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003991 default:
Calin Juravle11351682014-10-23 15:38:15 +01003992 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003993 }
3994}
3995
Calin Juravle34bacdf2014-10-07 20:23:36 +01003996void LocationsBuilderARM::VisitMul(HMul* mul) {
3997 LocationSummary* locations =
3998 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3999 switch (mul->GetResultType()) {
4000 case Primitive::kPrimInt:
4001 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004002 locations->SetInAt(0, Location::RequiresRegister());
4003 locations->SetInAt(1, Location::RequiresRegister());
4004 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004005 break;
4006 }
4007
Calin Juravleb5bfa962014-10-21 18:02:24 +01004008 case Primitive::kPrimFloat:
4009 case Primitive::kPrimDouble: {
4010 locations->SetInAt(0, Location::RequiresFpuRegister());
4011 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004012 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004013 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004014 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004015
4016 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004017 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004018 }
4019}
4020
4021void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
4022 LocationSummary* locations = mul->GetLocations();
4023 Location out = locations->Out();
4024 Location first = locations->InAt(0);
4025 Location second = locations->InAt(1);
4026 switch (mul->GetResultType()) {
4027 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00004028 __ mul(out.AsRegister<Register>(),
4029 first.AsRegister<Register>(),
4030 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004031 break;
4032 }
4033 case Primitive::kPrimLong: {
4034 Register out_hi = out.AsRegisterPairHigh<Register>();
4035 Register out_lo = out.AsRegisterPairLow<Register>();
4036 Register in1_hi = first.AsRegisterPairHigh<Register>();
4037 Register in1_lo = first.AsRegisterPairLow<Register>();
4038 Register in2_hi = second.AsRegisterPairHigh<Register>();
4039 Register in2_lo = second.AsRegisterPairLow<Register>();
4040
4041 // Extra checks to protect caused by the existence of R1_R2.
4042 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4043 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
4044 DCHECK_NE(out_hi, in1_lo);
4045 DCHECK_NE(out_hi, in2_lo);
4046
4047 // input: in1 - 64 bits, in2 - 64 bits
4048 // output: out
4049 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4050 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4051 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4052
4053 // IP <- in1.lo * in2.hi
4054 __ mul(IP, in1_lo, in2_hi);
4055 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4056 __ mla(out_hi, in1_hi, in2_lo, IP);
4057 // out.lo <- (in1.lo * in2.lo)[31:0];
4058 __ umull(out_lo, IP, in1_lo, in2_lo);
4059 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
4060 __ add(out_hi, out_hi, ShifterOperand(IP));
4061 break;
4062 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01004063
4064 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004065 __ vmuls(out.AsFpuRegister<SRegister>(),
4066 first.AsFpuRegister<SRegister>(),
4067 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004068 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004069 }
4070
4071 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004072 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4073 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4074 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01004075 break;
4076 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004077
4078 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004079 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004080 }
4081}
4082
Zheng Xuc6667102015-05-15 16:08:45 +08004083void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4084 DCHECK(instruction->IsDiv() || instruction->IsRem());
4085 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4086
4087 LocationSummary* locations = instruction->GetLocations();
4088 Location second = locations->InAt(1);
4089 DCHECK(second.IsConstant());
4090
4091 Register out = locations->Out().AsRegister<Register>();
4092 Register dividend = locations->InAt(0).AsRegister<Register>();
4093 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4094 DCHECK(imm == 1 || imm == -1);
4095
4096 if (instruction->IsRem()) {
4097 __ LoadImmediate(out, 0);
4098 } else {
4099 if (imm == 1) {
4100 __ Mov(out, dividend);
4101 } else {
4102 __ rsb(out, dividend, ShifterOperand(0));
4103 }
4104 }
4105}
4106
4107void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4108 DCHECK(instruction->IsDiv() || instruction->IsRem());
4109 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4110
4111 LocationSummary* locations = instruction->GetLocations();
4112 Location second = locations->InAt(1);
4113 DCHECK(second.IsConstant());
4114
4115 Register out = locations->Out().AsRegister<Register>();
4116 Register dividend = locations->InAt(0).AsRegister<Register>();
4117 Register temp = locations->GetTemp(0).AsRegister<Register>();
4118 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004119 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08004120 int ctz_imm = CTZ(abs_imm);
4121
4122 if (ctz_imm == 1) {
4123 __ Lsr(temp, dividend, 32 - ctz_imm);
4124 } else {
4125 __ Asr(temp, dividend, 31);
4126 __ Lsr(temp, temp, 32 - ctz_imm);
4127 }
4128 __ add(out, temp, ShifterOperand(dividend));
4129
4130 if (instruction->IsDiv()) {
4131 __ Asr(out, out, ctz_imm);
4132 if (imm < 0) {
4133 __ rsb(out, out, ShifterOperand(0));
4134 }
4135 } else {
4136 __ ubfx(out, out, 0, ctz_imm);
4137 __ sub(out, out, ShifterOperand(temp));
4138 }
4139}
4140
4141void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4142 DCHECK(instruction->IsDiv() || instruction->IsRem());
4143 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4144
4145 LocationSummary* locations = instruction->GetLocations();
4146 Location second = locations->InAt(1);
4147 DCHECK(second.IsConstant());
4148
4149 Register out = locations->Out().AsRegister<Register>();
4150 Register dividend = locations->InAt(0).AsRegister<Register>();
4151 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4152 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4153 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4154
4155 int64_t magic;
4156 int shift;
4157 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4158
4159 __ LoadImmediate(temp1, magic);
4160 __ smull(temp2, temp1, dividend, temp1);
4161
4162 if (imm > 0 && magic < 0) {
4163 __ add(temp1, temp1, ShifterOperand(dividend));
4164 } else if (imm < 0 && magic > 0) {
4165 __ sub(temp1, temp1, ShifterOperand(dividend));
4166 }
4167
4168 if (shift != 0) {
4169 __ Asr(temp1, temp1, shift);
4170 }
4171
4172 if (instruction->IsDiv()) {
4173 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
4174 } else {
4175 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
4176 // TODO: Strength reduction for mls.
4177 __ LoadImmediate(temp2, imm);
4178 __ mls(out, temp1, temp2, dividend);
4179 }
4180}
4181
4182void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
4183 DCHECK(instruction->IsDiv() || instruction->IsRem());
4184 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4185
4186 LocationSummary* locations = instruction->GetLocations();
4187 Location second = locations->InAt(1);
4188 DCHECK(second.IsConstant());
4189
4190 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4191 if (imm == 0) {
4192 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4193 } else if (imm == 1 || imm == -1) {
4194 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004195 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004196 DivRemByPowerOfTwo(instruction);
4197 } else {
4198 DCHECK(imm <= -2 || imm >= 2);
4199 GenerateDivRemWithAnyConstant(instruction);
4200 }
4201}
4202
Calin Juravle7c4954d2014-10-28 16:57:40 +00004203void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004204 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4205 if (div->GetResultType() == Primitive::kPrimLong) {
4206 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004207 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004208 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4209 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004210 } else if (div->GetResultType() == Primitive::kPrimInt &&
4211 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4212 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004213 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004214 }
4215
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004216 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4217
Calin Juravle7c4954d2014-10-28 16:57:40 +00004218 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004219 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004220 if (div->InputAt(1)->IsConstant()) {
4221 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004222 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004223 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004224 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
4225 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004226 // No temp register required.
4227 } else {
4228 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004229 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004230 locations->AddTemp(Location::RequiresRegister());
4231 }
4232 }
4233 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004234 locations->SetInAt(0, Location::RequiresRegister());
4235 locations->SetInAt(1, Location::RequiresRegister());
4236 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4237 } else {
4238 InvokeRuntimeCallingConvention calling_convention;
4239 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4240 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004241 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004242 // we only need the former.
4243 locations->SetOut(Location::RegisterLocation(R0));
4244 }
Calin Juravled0d48522014-11-04 16:40:20 +00004245 break;
4246 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004247 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004248 InvokeRuntimeCallingConvention calling_convention;
4249 locations->SetInAt(0, Location::RegisterPairLocation(
4250 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4251 locations->SetInAt(1, Location::RegisterPairLocation(
4252 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004253 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004254 break;
4255 }
4256 case Primitive::kPrimFloat:
4257 case Primitive::kPrimDouble: {
4258 locations->SetInAt(0, Location::RequiresFpuRegister());
4259 locations->SetInAt(1, Location::RequiresFpuRegister());
4260 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4261 break;
4262 }
4263
4264 default:
4265 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4266 }
4267}
4268
4269void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
4270 LocationSummary* locations = div->GetLocations();
4271 Location out = locations->Out();
4272 Location first = locations->InAt(0);
4273 Location second = locations->InAt(1);
4274
4275 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004276 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004277 if (second.IsConstant()) {
4278 GenerateDivRemConstantIntegral(div);
4279 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004280 __ sdiv(out.AsRegister<Register>(),
4281 first.AsRegister<Register>(),
4282 second.AsRegister<Register>());
4283 } else {
4284 InvokeRuntimeCallingConvention calling_convention;
4285 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4286 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4287 DCHECK_EQ(R0, out.AsRegister<Register>());
4288
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004289 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004290 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004291 }
Calin Juravled0d48522014-11-04 16:40:20 +00004292 break;
4293 }
4294
Calin Juravle7c4954d2014-10-28 16:57:40 +00004295 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004296 InvokeRuntimeCallingConvention calling_convention;
4297 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4298 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4299 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4300 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4301 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004302 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004303
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004304 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004305 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00004306 break;
4307 }
4308
4309 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004310 __ vdivs(out.AsFpuRegister<SRegister>(),
4311 first.AsFpuRegister<SRegister>(),
4312 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004313 break;
4314 }
4315
4316 case Primitive::kPrimDouble: {
4317 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4318 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4319 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
4320 break;
4321 }
4322
4323 default:
4324 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4325 }
4326}
4327
Calin Juravlebacfec32014-11-14 15:54:36 +00004328void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004329 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004330
4331 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004332 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004333 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4334 // sdiv will be replaced by other instruction sequence.
4335 call_kind = LocationSummary::kNoCall;
4336 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4337 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004338 // Have hardware divide instruction for int, do it with three instructions.
4339 call_kind = LocationSummary::kNoCall;
4340 }
4341
Calin Juravlebacfec32014-11-14 15:54:36 +00004342 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4343
Calin Juravled2ec87d2014-12-08 14:24:46 +00004344 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004345 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004346 if (rem->InputAt(1)->IsConstant()) {
4347 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004348 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004350 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
4351 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004352 // No temp register required.
4353 } else {
4354 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004355 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004356 locations->AddTemp(Location::RequiresRegister());
4357 }
4358 }
4359 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004360 locations->SetInAt(0, Location::RequiresRegister());
4361 locations->SetInAt(1, Location::RequiresRegister());
4362 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4363 locations->AddTemp(Location::RequiresRegister());
4364 } else {
4365 InvokeRuntimeCallingConvention calling_convention;
4366 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4367 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004368 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004369 // we only need the latter.
4370 locations->SetOut(Location::RegisterLocation(R1));
4371 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004372 break;
4373 }
4374 case Primitive::kPrimLong: {
4375 InvokeRuntimeCallingConvention calling_convention;
4376 locations->SetInAt(0, Location::RegisterPairLocation(
4377 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4378 locations->SetInAt(1, Location::RegisterPairLocation(
4379 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4380 // The runtime helper puts the output in R2,R3.
4381 locations->SetOut(Location::RegisterPairLocation(R2, R3));
4382 break;
4383 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00004384 case Primitive::kPrimFloat: {
4385 InvokeRuntimeCallingConvention calling_convention;
4386 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4387 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4388 locations->SetOut(Location::FpuRegisterLocation(S0));
4389 break;
4390 }
4391
Calin Juravlebacfec32014-11-14 15:54:36 +00004392 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004393 InvokeRuntimeCallingConvention calling_convention;
4394 locations->SetInAt(0, Location::FpuRegisterPairLocation(
4395 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4396 locations->SetInAt(1, Location::FpuRegisterPairLocation(
4397 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4398 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00004399 break;
4400 }
4401
4402 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004403 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004404 }
4405}
4406
4407void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
4408 LocationSummary* locations = rem->GetLocations();
4409 Location out = locations->Out();
4410 Location first = locations->InAt(0);
4411 Location second = locations->InAt(1);
4412
Calin Juravled2ec87d2014-12-08 14:24:46 +00004413 Primitive::Type type = rem->GetResultType();
4414 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004415 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004416 if (second.IsConstant()) {
4417 GenerateDivRemConstantIntegral(rem);
4418 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004419 Register reg1 = first.AsRegister<Register>();
4420 Register reg2 = second.AsRegister<Register>();
4421 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004422
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004423 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004424 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004425 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004426 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004427 } else {
4428 InvokeRuntimeCallingConvention calling_convention;
4429 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4430 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4431 DCHECK_EQ(R1, out.AsRegister<Register>());
4432
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004433 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004434 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004435 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004436 break;
4437 }
4438
4439 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004440 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004441 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004442 break;
4443 }
4444
Calin Juravled2ec87d2014-12-08 14:24:46 +00004445 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004446 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004447 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00004448 break;
4449 }
4450
Calin Juravlebacfec32014-11-14 15:54:36 +00004451 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004452 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004453 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004454 break;
4455 }
4456
4457 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004458 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004459 }
4460}
4461
Calin Juravled0d48522014-11-04 16:40:20 +00004462void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004463 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004464 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00004465}
4466
4467void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004468 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004469 codegen_->AddSlowPath(slow_path);
4470
4471 LocationSummary* locations = instruction->GetLocations();
4472 Location value = locations->InAt(0);
4473
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004474 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00004475 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004476 case Primitive::kPrimByte:
4477 case Primitive::kPrimChar:
4478 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004479 case Primitive::kPrimInt: {
4480 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004481 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004482 } else {
4483 DCHECK(value.IsConstant()) << value;
4484 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4485 __ b(slow_path->GetEntryLabel());
4486 }
4487 }
4488 break;
4489 }
4490 case Primitive::kPrimLong: {
4491 if (value.IsRegisterPair()) {
4492 __ orrs(IP,
4493 value.AsRegisterPairLow<Register>(),
4494 ShifterOperand(value.AsRegisterPairHigh<Register>()));
4495 __ b(slow_path->GetEntryLabel(), EQ);
4496 } else {
4497 DCHECK(value.IsConstant()) << value;
4498 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4499 __ b(slow_path->GetEntryLabel());
4500 }
4501 }
4502 break;
4503 default:
4504 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4505 }
4506 }
Calin Juravled0d48522014-11-04 16:40:20 +00004507}
4508
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004509void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
4510 Register in = locations->InAt(0).AsRegister<Register>();
4511 Location rhs = locations->InAt(1);
4512 Register out = locations->Out().AsRegister<Register>();
4513
4514 if (rhs.IsConstant()) {
4515 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4516 // so map all rotations to a +ve. equivalent in that range.
4517 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4518 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4519 if (rot) {
4520 // Rotate, mapping left rotations to right equivalents if necessary.
4521 // (e.g. left by 2 bits == right by 30.)
4522 __ Ror(out, in, rot);
4523 } else if (out != in) {
4524 __ Mov(out, in);
4525 }
4526 } else {
4527 __ Ror(out, in, rhs.AsRegister<Register>());
4528 }
4529}
4530
4531// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4532// rotates by swapping input regs (effectively rotating by the first 32-bits of
4533// a larger rotation) or flipping direction (thus treating larger right/left
4534// rotations as sub-word sized rotations in the other direction) as appropriate.
Anton Kirilov6f644202017-02-27 18:29:45 +00004535void InstructionCodeGeneratorARM::HandleLongRotate(HRor* ror) {
4536 LocationSummary* locations = ror->GetLocations();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004537 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
4538 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
4539 Location rhs = locations->InAt(1);
4540 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
4541 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
4542
4543 if (rhs.IsConstant()) {
4544 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4545 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00004546 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004547 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4548 // logic below to a simple pair of binary orr.
4549 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4550 if (rot >= kArmBitsPerWord) {
4551 rot -= kArmBitsPerWord;
4552 std::swap(in_reg_hi, in_reg_lo);
4553 }
4554 // Rotate, or mov to out for zero or word size rotations.
4555 if (rot != 0u) {
4556 __ Lsr(out_reg_hi, in_reg_hi, rot);
4557 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
4558 __ Lsr(out_reg_lo, in_reg_lo, rot);
4559 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
4560 } else {
4561 __ Mov(out_reg_lo, in_reg_lo);
4562 __ Mov(out_reg_hi, in_reg_hi);
4563 }
4564 } else {
4565 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
4566 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
4567 Label end;
4568 Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004569 Label* final_label = codegen_->GetFinalLabel(ror, &end);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004570
4571 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
4572 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
4573 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
4574 __ b(&shift_by_32_plus_shift_right, CC);
4575
4576 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4577 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4578 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4579 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4580 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4581 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4582 __ Lsr(shift_left, in_reg_hi, shift_right);
4583 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
Anton Kirilov6f644202017-02-27 18:29:45 +00004584 __ b(final_label);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004585
4586 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4587 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4588 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4589 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4590 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4591 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4592 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4593 __ Lsl(shift_right, in_reg_hi, shift_left);
4594 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
4595
Anton Kirilov6f644202017-02-27 18:29:45 +00004596 if (end.IsLinked()) {
4597 __ Bind(&end);
4598 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004599 }
4600}
Roland Levillain22c49222016-03-18 14:04:28 +00004601
4602void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004603 LocationSummary* locations =
4604 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4605 switch (ror->GetResultType()) {
4606 case Primitive::kPrimInt: {
4607 locations->SetInAt(0, Location::RequiresRegister());
4608 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4609 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4610 break;
4611 }
4612 case Primitive::kPrimLong: {
4613 locations->SetInAt(0, Location::RequiresRegister());
4614 if (ror->InputAt(1)->IsConstant()) {
4615 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4616 } else {
4617 locations->SetInAt(1, Location::RequiresRegister());
4618 locations->AddTemp(Location::RequiresRegister());
4619 locations->AddTemp(Location::RequiresRegister());
4620 }
4621 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4622 break;
4623 }
4624 default:
4625 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4626 }
4627}
4628
Roland Levillain22c49222016-03-18 14:04:28 +00004629void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004630 LocationSummary* locations = ror->GetLocations();
4631 Primitive::Type type = ror->GetResultType();
4632 switch (type) {
4633 case Primitive::kPrimInt: {
4634 HandleIntegerRotate(locations);
4635 break;
4636 }
4637 case Primitive::kPrimLong: {
Anton Kirilov6f644202017-02-27 18:29:45 +00004638 HandleLongRotate(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004639 break;
4640 }
4641 default:
4642 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004643 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004644 }
4645}
4646
Calin Juravle9aec02f2014-11-18 23:06:35 +00004647void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
4648 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4649
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004650 LocationSummary* locations =
4651 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004652
4653 switch (op->GetResultType()) {
4654 case Primitive::kPrimInt: {
4655 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004656 if (op->InputAt(1)->IsConstant()) {
4657 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4658 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4659 } else {
4660 locations->SetInAt(1, Location::RequiresRegister());
4661 // Make the output overlap, as it will be used to hold the masked
4662 // second input.
4663 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4664 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004665 break;
4666 }
4667 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004668 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004669 if (op->InputAt(1)->IsConstant()) {
4670 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4671 // For simplicity, use kOutputOverlap even though we only require that low registers
4672 // don't clash with high registers which the register allocator currently guarantees.
4673 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4674 } else {
4675 locations->SetInAt(1, Location::RequiresRegister());
4676 locations->AddTemp(Location::RequiresRegister());
4677 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4678 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004679 break;
4680 }
4681 default:
4682 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4683 }
4684}
4685
4686void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
4687 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4688
4689 LocationSummary* locations = op->GetLocations();
4690 Location out = locations->Out();
4691 Location first = locations->InAt(0);
4692 Location second = locations->InAt(1);
4693
4694 Primitive::Type type = op->GetResultType();
4695 switch (type) {
4696 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004697 Register out_reg = out.AsRegister<Register>();
4698 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004699 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004700 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00004701 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004702 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004703 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004704 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004705 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004706 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004707 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004708 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004709 }
4710 } else {
4711 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004712 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00004713 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00004714 __ Mov(out_reg, first_reg);
4715 } else if (op->IsShl()) {
4716 __ Lsl(out_reg, first_reg, shift_value);
4717 } else if (op->IsShr()) {
4718 __ Asr(out_reg, first_reg, shift_value);
4719 } else {
4720 __ Lsr(out_reg, first_reg, shift_value);
4721 }
4722 }
4723 break;
4724 }
4725 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004726 Register o_h = out.AsRegisterPairHigh<Register>();
4727 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004728
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004729 Register high = first.AsRegisterPairHigh<Register>();
4730 Register low = first.AsRegisterPairLow<Register>();
4731
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004732 if (second.IsRegister()) {
4733 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004734
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004735 Register second_reg = second.AsRegister<Register>();
4736
4737 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004738 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004739 // Shift the high part
4740 __ Lsl(o_h, high, o_l);
4741 // Shift the low part and `or` what overflew on the high part
4742 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
4743 __ Lsr(temp, low, temp);
4744 __ orr(o_h, o_h, ShifterOperand(temp));
4745 // If the shift is > 32 bits, override the high part
4746 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
4747 __ it(PL);
4748 __ Lsl(o_h, low, temp, PL);
4749 // Shift the low part
4750 __ Lsl(o_l, low, o_l);
4751 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004752 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004753 // Shift the low part
4754 __ Lsr(o_l, low, o_h);
4755 // Shift the high part and `or` what underflew on the low part
4756 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4757 __ Lsl(temp, high, temp);
4758 __ orr(o_l, o_l, ShifterOperand(temp));
4759 // If the shift is > 32 bits, override the low part
4760 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4761 __ it(PL);
4762 __ Asr(o_l, high, temp, PL);
4763 // Shift the high part
4764 __ Asr(o_h, high, o_h);
4765 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004766 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004767 // same as Shr except we use `Lsr`s and not `Asr`s
4768 __ Lsr(o_l, low, o_h);
4769 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4770 __ Lsl(temp, high, temp);
4771 __ orr(o_l, o_l, ShifterOperand(temp));
4772 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4773 __ it(PL);
4774 __ Lsr(o_l, high, temp, PL);
4775 __ Lsr(o_h, high, o_h);
4776 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004777 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004778 // Register allocator doesn't create partial overlap.
4779 DCHECK_NE(o_l, high);
4780 DCHECK_NE(o_h, low);
4781 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004782 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004783 if (shift_value > 32) {
4784 if (op->IsShl()) {
4785 __ Lsl(o_h, low, shift_value - 32);
4786 __ LoadImmediate(o_l, 0);
4787 } else if (op->IsShr()) {
4788 __ Asr(o_l, high, shift_value - 32);
4789 __ Asr(o_h, high, 31);
4790 } else {
4791 __ Lsr(o_l, high, shift_value - 32);
4792 __ LoadImmediate(o_h, 0);
4793 }
4794 } else if (shift_value == 32) {
4795 if (op->IsShl()) {
4796 __ mov(o_h, ShifterOperand(low));
4797 __ LoadImmediate(o_l, 0);
4798 } else if (op->IsShr()) {
4799 __ mov(o_l, ShifterOperand(high));
4800 __ Asr(o_h, high, 31);
4801 } else {
4802 __ mov(o_l, ShifterOperand(high));
4803 __ LoadImmediate(o_h, 0);
4804 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00004805 } else if (shift_value == 1) {
4806 if (op->IsShl()) {
4807 __ Lsls(o_l, low, 1);
4808 __ adc(o_h, high, ShifterOperand(high));
4809 } else if (op->IsShr()) {
4810 __ Asrs(o_h, high, 1);
4811 __ Rrx(o_l, low);
4812 } else {
4813 __ Lsrs(o_h, high, 1);
4814 __ Rrx(o_l, low);
4815 }
4816 } else {
4817 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004818 if (op->IsShl()) {
4819 __ Lsl(o_h, high, shift_value);
4820 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
4821 __ Lsl(o_l, low, shift_value);
4822 } else if (op->IsShr()) {
4823 __ Lsr(o_l, low, shift_value);
4824 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4825 __ Asr(o_h, high, shift_value);
4826 } else {
4827 __ Lsr(o_l, low, shift_value);
4828 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4829 __ Lsr(o_h, high, shift_value);
4830 }
4831 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004832 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004833 break;
4834 }
4835 default:
4836 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004837 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004838 }
4839}
4840
4841void LocationsBuilderARM::VisitShl(HShl* shl) {
4842 HandleShift(shl);
4843}
4844
4845void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
4846 HandleShift(shl);
4847}
4848
4849void LocationsBuilderARM::VisitShr(HShr* shr) {
4850 HandleShift(shr);
4851}
4852
4853void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
4854 HandleShift(shr);
4855}
4856
4857void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
4858 HandleShift(ushr);
4859}
4860
4861void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
4862 HandleShift(ushr);
4863}
4864
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004865void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004866 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004867 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00004868 if (instruction->IsStringAlloc()) {
4869 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4870 } else {
4871 InvokeRuntimeCallingConvention calling_convention;
4872 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004873 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004874 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004875}
4876
4877void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004878 // Note: if heap poisoning is enabled, the entry point takes cares
4879 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004880 if (instruction->IsStringAlloc()) {
4881 // String is allocated through StringFactory. Call NewEmptyString entry point.
4882 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004883 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004884 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4885 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
4886 __ blx(LR);
4887 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4888 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004889 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004890 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004891 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004892}
4893
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004894void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
4895 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004896 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004897 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004898 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004899 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4900 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004901}
4902
4903void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004904 // Note: if heap poisoning is enabled, the entry point takes cares
4905 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004906 QuickEntrypointEnum entrypoint =
4907 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4908 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004909 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004910 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004911}
4912
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004913void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004914 LocationSummary* locations =
4915 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004916 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4917 if (location.IsStackSlot()) {
4918 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4919 } else if (location.IsDoubleStackSlot()) {
4920 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004921 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004922 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004923}
4924
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004925void InstructionCodeGeneratorARM::VisitParameterValue(
4926 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004927 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004928}
4929
4930void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4931 LocationSummary* locations =
4932 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4933 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4934}
4935
4936void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4937 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004938}
4939
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004940void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004941 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004942 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004943 locations->SetInAt(0, Location::RequiresRegister());
4944 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004945}
4946
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004947void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4948 LocationSummary* locations = not_->GetLocations();
4949 Location out = locations->Out();
4950 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004951 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004952 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004953 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004954 break;
4955
4956 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004957 __ mvn(out.AsRegisterPairLow<Register>(),
4958 ShifterOperand(in.AsRegisterPairLow<Register>()));
4959 __ mvn(out.AsRegisterPairHigh<Register>(),
4960 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004961 break;
4962
4963 default:
4964 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4965 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004966}
4967
David Brazdil66d126e2015-04-03 16:02:44 +01004968void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4969 LocationSummary* locations =
4970 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4971 locations->SetInAt(0, Location::RequiresRegister());
4972 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4973}
4974
4975void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004976 LocationSummary* locations = bool_not->GetLocations();
4977 Location out = locations->Out();
4978 Location in = locations->InAt(0);
4979 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4980}
4981
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004982void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004983 LocationSummary* locations =
4984 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004985 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004986 case Primitive::kPrimBoolean:
4987 case Primitive::kPrimByte:
4988 case Primitive::kPrimShort:
4989 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004990 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004991 case Primitive::kPrimLong: {
4992 locations->SetInAt(0, Location::RequiresRegister());
4993 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004994 // Output overlaps because it is written before doing the low comparison.
4995 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004996 break;
4997 }
4998 case Primitive::kPrimFloat:
4999 case Primitive::kPrimDouble: {
5000 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005001 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00005002 locations->SetOut(Location::RequiresRegister());
5003 break;
5004 }
5005 default:
5006 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5007 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005008}
5009
5010void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005011 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005012 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005013 Location left = locations->InAt(0);
5014 Location right = locations->InAt(1);
5015
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005016 Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005017 Label* final_label = codegen_->GetFinalLabel(compare, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00005018 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00005019 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00005020 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00005021 case Primitive::kPrimBoolean:
5022 case Primitive::kPrimByte:
5023 case Primitive::kPrimShort:
5024 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08005025 case Primitive::kPrimInt: {
5026 __ LoadImmediate(out, 0);
5027 __ cmp(left.AsRegister<Register>(),
5028 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
5029 less_cond = LT;
5030 break;
5031 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005032 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005033 __ cmp(left.AsRegisterPairHigh<Register>(),
5034 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005035 __ b(&less, LT);
5036 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01005037 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00005038 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005039 __ cmp(left.AsRegisterPairLow<Register>(),
5040 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005041 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00005042 break;
5043 }
5044 case Primitive::kPrimFloat:
5045 case Primitive::kPrimDouble: {
5046 __ LoadImmediate(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005047 GenerateVcmp(compare, codegen_);
Calin Juravleddb7df22014-11-25 20:56:51 +00005048 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005049 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005050 break;
5051 }
5052 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005053 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00005054 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005055 }
Aart Bika19616e2016-02-01 18:57:58 -08005056
Anton Kirilov6f644202017-02-27 18:29:45 +00005057 __ b(final_label, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00005058 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00005059
5060 __ Bind(&greater);
5061 __ LoadImmediate(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005062 __ b(final_label);
Calin Juravleddb7df22014-11-25 20:56:51 +00005063
5064 __ Bind(&less);
5065 __ LoadImmediate(out, -1);
5066
Anton Kirilov6f644202017-02-27 18:29:45 +00005067 if (done.IsLinked()) {
5068 __ Bind(&done);
5069 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005070}
5071
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005072void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005073 LocationSummary* locations =
5074 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005075 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005076 locations->SetInAt(i, Location::Any());
5077 }
5078 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005079}
5080
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005081void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005082 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005083}
5084
Roland Levillainc9285912015-12-18 10:38:42 +00005085void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
5086 // TODO (ported from quick): revisit ARM barrier kinds.
5087 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00005088 switch (kind) {
5089 case MemBarrierKind::kAnyStore:
5090 case MemBarrierKind::kLoadAny:
5091 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005092 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00005093 break;
5094 }
5095 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005096 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00005097 break;
5098 }
5099 default:
5100 LOG(FATAL) << "Unexpected memory barrier " << kind;
5101 }
Kenny Root1d8199d2015-06-02 11:01:10 -07005102 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00005103}
5104
5105void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
5106 uint32_t offset,
5107 Register out_lo,
5108 Register out_hi) {
5109 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005110 // Ensure `out_lo` is different from `addr`, so that loading
5111 // `offset` into `out_lo` does not clutter `addr`.
5112 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00005113 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005114 __ add(IP, addr, ShifterOperand(out_lo));
5115 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005116 }
5117 __ ldrexd(out_lo, out_hi, addr);
5118}
5119
5120void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
5121 uint32_t offset,
5122 Register value_lo,
5123 Register value_hi,
5124 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00005125 Register temp2,
5126 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005127 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00005128 if (offset != 0) {
5129 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005130 __ add(IP, addr, ShifterOperand(temp1));
5131 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005132 }
5133 __ Bind(&fail);
5134 // We need a load followed by store. (The address used in a STREX instruction must
5135 // be the same as the address in the most recently executed LDREX instruction.)
5136 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00005137 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005138 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005139 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00005140}
5141
5142void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5143 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5144
Nicolas Geoffray39468442014-09-02 15:17:15 +01005145 LocationSummary* locations =
5146 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005147 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005148
Calin Juravle52c48962014-12-16 17:02:57 +00005149 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005150 if (Primitive::IsFloatingPointType(field_type)) {
5151 locations->SetInAt(1, Location::RequiresFpuRegister());
5152 } else {
5153 locations->SetInAt(1, Location::RequiresRegister());
5154 }
5155
Calin Juravle52c48962014-12-16 17:02:57 +00005156 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00005157 bool generate_volatile = field_info.IsVolatile()
5158 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005159 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01005160 bool needs_write_barrier =
5161 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005162 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00005163 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01005164 if (needs_write_barrier) {
5165 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005166 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005167 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005168 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005169 // - registers need to be consecutive
5170 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005171 // We don't test for ARM yet, and the assertion makes sure that we
5172 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005173 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5174
5175 locations->AddTemp(Location::RequiresRegister());
5176 locations->AddTemp(Location::RequiresRegister());
5177 if (field_type == Primitive::kPrimDouble) {
5178 // For doubles we need two more registers to copy the value.
5179 locations->AddTemp(Location::RegisterLocation(R2));
5180 locations->AddTemp(Location::RegisterLocation(R3));
5181 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005182 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005183}
5184
Calin Juravle52c48962014-12-16 17:02:57 +00005185void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005186 const FieldInfo& field_info,
5187 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005188 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5189
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005190 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00005191 Register base = locations->InAt(0).AsRegister<Register>();
5192 Location value = locations->InAt(1);
5193
5194 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005195 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005196 Primitive::Type field_type = field_info.GetFieldType();
5197 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005198 bool needs_write_barrier =
5199 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005200
5201 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005202 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005203 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005204
5205 switch (field_type) {
5206 case Primitive::kPrimBoolean:
5207 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00005208 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005209 break;
5210 }
5211
5212 case Primitive::kPrimShort:
5213 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00005214 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005215 break;
5216 }
5217
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005218 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005219 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01005220 if (kPoisonHeapReferences && needs_write_barrier) {
5221 // Note that in the case where `value` is a null reference,
5222 // we do not enter this block, as a null reference does not
5223 // need poisoning.
5224 DCHECK_EQ(field_type, Primitive::kPrimNot);
5225 Register temp = locations->GetTemp(0).AsRegister<Register>();
5226 __ Mov(temp, value.AsRegister<Register>());
5227 __ PoisonHeapReference(temp);
5228 __ StoreToOffset(kStoreWord, temp, base, offset);
5229 } else {
5230 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
5231 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005232 break;
5233 }
5234
5235 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00005236 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005237 GenerateWideAtomicStore(base, offset,
5238 value.AsRegisterPairLow<Register>(),
5239 value.AsRegisterPairHigh<Register>(),
5240 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005241 locations->GetTemp(1).AsRegister<Register>(),
5242 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005243 } else {
5244 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005245 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005246 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005247 break;
5248 }
5249
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005250 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00005251 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005252 break;
5253 }
5254
5255 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005256 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005257 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005258 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
5259 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
5260
5261 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
5262
5263 GenerateWideAtomicStore(base, offset,
5264 value_reg_lo,
5265 value_reg_hi,
5266 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005267 locations->GetTemp(3).AsRegister<Register>(),
5268 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005269 } else {
5270 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005271 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005272 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005273 break;
5274 }
5275
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005276 case Primitive::kPrimVoid:
5277 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005278 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005279 }
Calin Juravle52c48962014-12-16 17:02:57 +00005280
Calin Juravle77520bc2015-01-12 18:45:46 +00005281 // Longs and doubles are handled in the switch.
5282 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
5283 codegen_->MaybeRecordImplicitNullCheck(instruction);
5284 }
5285
5286 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5287 Register temp = locations->GetTemp(0).AsRegister<Register>();
5288 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005289 codegen_->MarkGCCard(
5290 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005291 }
5292
Calin Juravle52c48962014-12-16 17:02:57 +00005293 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005294 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005295 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005296}
5297
Calin Juravle52c48962014-12-16 17:02:57 +00005298void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5299 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00005300
5301 bool object_field_get_with_read_barrier =
5302 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005303 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005304 new (GetGraph()->GetArena()) LocationSummary(instruction,
5305 object_field_get_with_read_barrier ?
5306 LocationSummary::kCallOnSlowPath :
5307 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005308 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005309 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005310 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005311 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00005312
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005313 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00005314 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005315 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00005316 // The output overlaps in case of volatile long: we don't want the
5317 // code generated by GenerateWideAtomicLoad to overwrite the
5318 // object's location. Likewise, in the case of an object field get
5319 // with read barriers enabled, we do not want the load to overwrite
5320 // the object's location, as we need it to emit the read barrier.
5321 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5322 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01005323
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005324 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5325 locations->SetOut(Location::RequiresFpuRegister());
5326 } else {
5327 locations->SetOut(Location::RequiresRegister(),
5328 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5329 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005330 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00005331 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005332 // - registers need to be consecutive
5333 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005334 // We don't test for ARM yet, and the assertion makes sure that we
5335 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005336 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5337 locations->AddTemp(Location::RequiresRegister());
5338 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00005339 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5340 // We need a temporary register for the read barrier marking slow
5341 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005342 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5343 !Runtime::Current()->UseJitCompilation()) {
5344 // If link-time thunks for the Baker read barrier are enabled, for AOT
5345 // loads we need a temporary only if the offset is too big.
5346 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5347 locations->AddTemp(Location::RequiresRegister());
5348 }
5349 // And we always need the reserved entrypoint register.
5350 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5351 } else {
5352 locations->AddTemp(Location::RequiresRegister());
5353 }
Calin Juravle52c48962014-12-16 17:02:57 +00005354 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005355}
5356
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005357Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5358 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
5359 << input->GetType();
5360 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5361 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5362 return Location::ConstantLocation(input->AsConstant());
5363 } else {
5364 return Location::RequiresFpuRegister();
5365 }
5366}
5367
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005368Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
5369 Opcode opcode) {
5370 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5371 if (constant->IsConstant() &&
5372 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5373 return Location::ConstantLocation(constant->AsConstant());
5374 }
5375 return Location::RequiresRegister();
5376}
5377
5378bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
5379 Opcode opcode) {
5380 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5381 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01005382 Opcode high_opcode = opcode;
5383 SetCc low_set_cc = kCcDontCare;
5384 switch (opcode) {
5385 case SUB:
5386 // Flip the operation to an ADD.
5387 value = -value;
5388 opcode = ADD;
5389 FALLTHROUGH_INTENDED;
5390 case ADD:
5391 if (Low32Bits(value) == 0u) {
5392 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5393 }
5394 high_opcode = ADC;
5395 low_set_cc = kCcSet;
5396 break;
5397 default:
5398 break;
5399 }
5400 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5401 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005402 } else {
5403 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5404 }
5405}
5406
Vladimir Marko59751a72016-08-05 14:37:27 +01005407bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
5408 Opcode opcode,
5409 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005410 ShifterOperand so;
5411 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01005412 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005413 return true;
5414 }
5415 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005416 uint32_t neg_value = 0;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005417 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005418 case AND: neg_opcode = BIC; neg_value = ~value; break;
5419 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5420 case ADD: neg_opcode = SUB; neg_value = -value; break;
5421 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5422 case SUB: neg_opcode = ADD; neg_value = -value; break;
5423 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5424 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005425 default:
5426 return false;
5427 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005428
5429 if (assembler->ShifterOperandCanHold(kNoRegister,
5430 kNoRegister,
5431 neg_opcode,
5432 neg_value,
5433 set_cc,
5434 &so)) {
5435 return true;
5436 }
5437
5438 return opcode == AND && IsPowerOfTwo(value + 1);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005439}
5440
Calin Juravle52c48962014-12-16 17:02:57 +00005441void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
5442 const FieldInfo& field_info) {
5443 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005444
Calin Juravle52c48962014-12-16 17:02:57 +00005445 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005446 Location base_loc = locations->InAt(0);
5447 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005448 Location out = locations->Out();
5449 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005450 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005451 Primitive::Type field_type = field_info.GetFieldType();
5452 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5453
5454 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00005455 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00005456 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005457 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005458
Roland Levillainc9285912015-12-18 10:38:42 +00005459 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00005460 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005461 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005462
Roland Levillainc9285912015-12-18 10:38:42 +00005463 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00005464 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005465 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005466
Roland Levillainc9285912015-12-18 10:38:42 +00005467 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00005468 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005469 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005470
5471 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00005472 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005473 break;
Roland Levillainc9285912015-12-18 10:38:42 +00005474
5475 case Primitive::kPrimNot: {
5476 // /* HeapReference<Object> */ out = *(base + offset)
5477 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5478 Location temp_loc = locations->GetTemp(0);
5479 // Note that a potential implicit null check is handled in this
5480 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
5481 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5482 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5483 if (is_volatile) {
5484 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5485 }
5486 } else {
5487 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
5488 codegen_->MaybeRecordImplicitNullCheck(instruction);
5489 if (is_volatile) {
5490 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5491 }
5492 // If read barriers are enabled, emit read barriers other than
5493 // Baker's using a slow path (and also unpoison the loaded
5494 // reference, if heap poisoning is enabled).
5495 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5496 }
5497 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005498 }
5499
Roland Levillainc9285912015-12-18 10:38:42 +00005500 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00005501 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005502 GenerateWideAtomicLoad(base, offset,
5503 out.AsRegisterPairLow<Register>(),
5504 out.AsRegisterPairHigh<Register>());
5505 } else {
5506 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
5507 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005508 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005509
Roland Levillainc9285912015-12-18 10:38:42 +00005510 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00005511 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005512 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005513
5514 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005515 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005516 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005517 Register lo = locations->GetTemp(0).AsRegister<Register>();
5518 Register hi = locations->GetTemp(1).AsRegister<Register>();
5519 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00005520 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005521 __ vmovdrr(out_reg, lo, hi);
5522 } else {
5523 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005524 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005525 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005526 break;
5527 }
5528
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005529 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005530 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005531 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005532 }
Calin Juravle52c48962014-12-16 17:02:57 +00005533
Roland Levillainc9285912015-12-18 10:38:42 +00005534 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5535 // Potential implicit null checks, in the case of reference or
5536 // double fields, are handled in the previous switch statement.
5537 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005538 codegen_->MaybeRecordImplicitNullCheck(instruction);
5539 }
5540
Calin Juravle52c48962014-12-16 17:02:57 +00005541 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005542 if (field_type == Primitive::kPrimNot) {
5543 // Memory barriers, in the case of references, are also handled
5544 // in the previous switch statement.
5545 } else {
5546 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5547 }
Roland Levillain4d027112015-07-01 15:41:14 +01005548 }
Calin Juravle52c48962014-12-16 17:02:57 +00005549}
5550
5551void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5552 HandleFieldSet(instruction, instruction->GetFieldInfo());
5553}
5554
5555void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005556 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005557}
5558
5559void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5560 HandleFieldGet(instruction, instruction->GetFieldInfo());
5561}
5562
5563void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5564 HandleFieldGet(instruction, instruction->GetFieldInfo());
5565}
5566
5567void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5568 HandleFieldGet(instruction, instruction->GetFieldInfo());
5569}
5570
5571void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5572 HandleFieldGet(instruction, instruction->GetFieldInfo());
5573}
5574
5575void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5576 HandleFieldSet(instruction, instruction->GetFieldInfo());
5577}
5578
5579void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005580 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005581}
5582
Calin Juravlee460d1d2015-09-29 04:52:17 +01005583void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
5584 HUnresolvedInstanceFieldGet* instruction) {
5585 FieldAccessCallingConventionARM calling_convention;
5586 codegen_->CreateUnresolvedFieldLocationSummary(
5587 instruction, instruction->GetFieldType(), calling_convention);
5588}
5589
5590void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
5591 HUnresolvedInstanceFieldGet* instruction) {
5592 FieldAccessCallingConventionARM calling_convention;
5593 codegen_->GenerateUnresolvedFieldAccess(instruction,
5594 instruction->GetFieldType(),
5595 instruction->GetFieldIndex(),
5596 instruction->GetDexPc(),
5597 calling_convention);
5598}
5599
5600void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
5601 HUnresolvedInstanceFieldSet* instruction) {
5602 FieldAccessCallingConventionARM calling_convention;
5603 codegen_->CreateUnresolvedFieldLocationSummary(
5604 instruction, instruction->GetFieldType(), calling_convention);
5605}
5606
5607void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
5608 HUnresolvedInstanceFieldSet* instruction) {
5609 FieldAccessCallingConventionARM calling_convention;
5610 codegen_->GenerateUnresolvedFieldAccess(instruction,
5611 instruction->GetFieldType(),
5612 instruction->GetFieldIndex(),
5613 instruction->GetDexPc(),
5614 calling_convention);
5615}
5616
5617void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
5618 HUnresolvedStaticFieldGet* instruction) {
5619 FieldAccessCallingConventionARM calling_convention;
5620 codegen_->CreateUnresolvedFieldLocationSummary(
5621 instruction, instruction->GetFieldType(), calling_convention);
5622}
5623
5624void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
5625 HUnresolvedStaticFieldGet* instruction) {
5626 FieldAccessCallingConventionARM calling_convention;
5627 codegen_->GenerateUnresolvedFieldAccess(instruction,
5628 instruction->GetFieldType(),
5629 instruction->GetFieldIndex(),
5630 instruction->GetDexPc(),
5631 calling_convention);
5632}
5633
5634void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
5635 HUnresolvedStaticFieldSet* instruction) {
5636 FieldAccessCallingConventionARM calling_convention;
5637 codegen_->CreateUnresolvedFieldLocationSummary(
5638 instruction, instruction->GetFieldType(), calling_convention);
5639}
5640
5641void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
5642 HUnresolvedStaticFieldSet* instruction) {
5643 FieldAccessCallingConventionARM calling_convention;
5644 codegen_->GenerateUnresolvedFieldAccess(instruction,
5645 instruction->GetFieldType(),
5646 instruction->GetFieldIndex(),
5647 instruction->GetDexPc(),
5648 calling_convention);
5649}
5650
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005651void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005652 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5653 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005654}
5655
Calin Juravle2ae48182016-03-16 14:05:09 +00005656void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
5657 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005658 return;
5659 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005660 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005661
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005662 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005663 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005664}
5665
Calin Juravle2ae48182016-03-16 14:05:09 +00005666void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01005667 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005668 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005669
5670 LocationSummary* locations = instruction->GetLocations();
5671 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005672
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005673 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005674}
5675
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005676void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005677 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005678}
5679
Artem Serov6c916792016-07-11 14:02:34 +01005680static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5681 switch (type) {
5682 case Primitive::kPrimNot:
5683 return kLoadWord;
5684 case Primitive::kPrimBoolean:
5685 return kLoadUnsignedByte;
5686 case Primitive::kPrimByte:
5687 return kLoadSignedByte;
5688 case Primitive::kPrimChar:
5689 return kLoadUnsignedHalfword;
5690 case Primitive::kPrimShort:
5691 return kLoadSignedHalfword;
5692 case Primitive::kPrimInt:
5693 return kLoadWord;
5694 case Primitive::kPrimLong:
5695 return kLoadWordPair;
5696 case Primitive::kPrimFloat:
5697 return kLoadSWord;
5698 case Primitive::kPrimDouble:
5699 return kLoadDWord;
5700 default:
5701 LOG(FATAL) << "Unreachable type " << type;
5702 UNREACHABLE();
5703 }
5704}
5705
5706static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5707 switch (type) {
5708 case Primitive::kPrimNot:
5709 return kStoreWord;
5710 case Primitive::kPrimBoolean:
5711 case Primitive::kPrimByte:
5712 return kStoreByte;
5713 case Primitive::kPrimChar:
5714 case Primitive::kPrimShort:
5715 return kStoreHalfword;
5716 case Primitive::kPrimInt:
5717 return kStoreWord;
5718 case Primitive::kPrimLong:
5719 return kStoreWordPair;
5720 case Primitive::kPrimFloat:
5721 return kStoreSWord;
5722 case Primitive::kPrimDouble:
5723 return kStoreDWord;
5724 default:
5725 LOG(FATAL) << "Unreachable type " << type;
5726 UNREACHABLE();
5727 }
5728}
5729
5730void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
5731 Location out_loc,
5732 Register base,
5733 Register reg_offset,
5734 Condition cond) {
5735 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5736 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5737
5738 switch (type) {
5739 case Primitive::kPrimByte:
5740 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
5741 break;
5742 case Primitive::kPrimBoolean:
5743 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
5744 break;
5745 case Primitive::kPrimShort:
5746 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
5747 break;
5748 case Primitive::kPrimChar:
5749 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
5750 break;
5751 case Primitive::kPrimNot:
5752 case Primitive::kPrimInt:
5753 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
5754 break;
5755 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5756 case Primitive::kPrimLong:
5757 case Primitive::kPrimFloat:
5758 case Primitive::kPrimDouble:
5759 default:
5760 LOG(FATAL) << "Unreachable type " << type;
5761 UNREACHABLE();
5762 }
5763}
5764
5765void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
5766 Location loc,
5767 Register base,
5768 Register reg_offset,
5769 Condition cond) {
5770 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5771 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5772
5773 switch (type) {
5774 case Primitive::kPrimByte:
5775 case Primitive::kPrimBoolean:
5776 __ strb(loc.AsRegister<Register>(), mem_address, cond);
5777 break;
5778 case Primitive::kPrimShort:
5779 case Primitive::kPrimChar:
5780 __ strh(loc.AsRegister<Register>(), mem_address, cond);
5781 break;
5782 case Primitive::kPrimNot:
5783 case Primitive::kPrimInt:
5784 __ str(loc.AsRegister<Register>(), mem_address, cond);
5785 break;
5786 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5787 case Primitive::kPrimLong:
5788 case Primitive::kPrimFloat:
5789 case Primitive::kPrimDouble:
5790 default:
5791 LOG(FATAL) << "Unreachable type " << type;
5792 UNREACHABLE();
5793 }
5794}
5795
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005796void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005797 bool object_array_get_with_read_barrier =
5798 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005799 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005800 new (GetGraph()->GetArena()) LocationSummary(instruction,
5801 object_array_get_with_read_barrier ?
5802 LocationSummary::kCallOnSlowPath :
5803 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005804 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005805 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005806 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005807 locations->SetInAt(0, Location::RequiresRegister());
5808 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005809 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5810 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5811 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005812 // The output overlaps in the case of an object array get with
5813 // read barriers enabled: we do not want the move to overwrite the
5814 // array's location, as we need it to emit the read barrier.
5815 locations->SetOut(
5816 Location::RequiresRegister(),
5817 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005818 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005819 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5820 // We need a temporary register for the read barrier marking slow
5821 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
5822 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5823 !Runtime::Current()->UseJitCompilation() &&
5824 instruction->GetIndex()->IsConstant()) {
5825 // Array loads with constant index are treated as field loads.
5826 // If link-time thunks for the Baker read barrier are enabled, for AOT
5827 // constant index loads we need a temporary only if the offset is too big.
5828 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
5829 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
5830 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
5831 if (offset >= kReferenceLoadMinFarOffset) {
5832 locations->AddTemp(Location::RequiresRegister());
5833 }
5834 // And we always need the reserved entrypoint register.
5835 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5836 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
5837 !Runtime::Current()->UseJitCompilation() &&
5838 !instruction->GetIndex()->IsConstant()) {
5839 // We need a non-scratch temporary for the array data pointer.
5840 locations->AddTemp(Location::RequiresRegister());
5841 // And we always need the reserved entrypoint register.
5842 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5843 } else {
5844 locations->AddTemp(Location::RequiresRegister());
5845 }
5846 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5847 // Also need a temporary for String compression feature.
Roland Levillainc9285912015-12-18 10:38:42 +00005848 locations->AddTemp(Location::RequiresRegister());
5849 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005850}
5851
5852void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
5853 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005854 Location obj_loc = locations->InAt(0);
5855 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005856 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00005857 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005858 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00005859 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07005860 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5861 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01005862 HInstruction* array_instr = instruction->GetArray();
5863 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01005864
Roland Levillain4d027112015-07-01 15:41:14 +01005865 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01005866 case Primitive::kPrimBoolean:
5867 case Primitive::kPrimByte:
5868 case Primitive::kPrimShort:
5869 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00005870 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005871 Register length;
5872 if (maybe_compressed_char_at) {
5873 length = locations->GetTemp(0).AsRegister<Register>();
5874 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5875 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
5876 codegen_->MaybeRecordImplicitNullCheck(instruction);
5877 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005878 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005879 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07005880 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07005881 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005882 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005883 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5884 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5885 "Expecting 0=compressed, 1=uncompressed");
5886 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005887 __ LoadFromOffset(kLoadUnsignedByte,
5888 out_loc.AsRegister<Register>(),
5889 obj,
5890 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00005891 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07005892 __ Bind(&uncompressed_load);
5893 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5894 out_loc.AsRegister<Register>(),
5895 obj,
5896 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005897 if (done.IsLinked()) {
5898 __ Bind(&done);
5899 }
jessicahandojo05765752016-09-09 19:01:32 -07005900 } else {
5901 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01005902
jessicahandojo05765752016-09-09 19:01:32 -07005903 LoadOperandType load_type = GetLoadOperandType(type);
5904 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
5905 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005906 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005907 Register temp = IP;
5908
5909 if (has_intermediate_address) {
5910 // We do not need to compute the intermediate address from the array: the
5911 // input instruction has done it already. See the comment in
5912 // `TryExtractArrayAccessAddress()`.
5913 if (kIsDebugBuild) {
5914 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5915 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5916 }
5917 temp = obj;
5918 } else {
5919 __ add(temp, obj, ShifterOperand(data_offset));
5920 }
jessicahandojo05765752016-09-09 19:01:32 -07005921 if (maybe_compressed_char_at) {
5922 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005923 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005924 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5925 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5926 "Expecting 0=compressed, 1=uncompressed");
5927 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005928 __ ldrb(out_loc.AsRegister<Register>(),
5929 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00005930 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07005931 __ Bind(&uncompressed_load);
5932 __ ldrh(out_loc.AsRegister<Register>(),
5933 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005934 if (done.IsLinked()) {
5935 __ Bind(&done);
5936 }
jessicahandojo05765752016-09-09 19:01:32 -07005937 } else {
5938 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
5939 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005940 }
5941 break;
5942 }
5943
Roland Levillainc9285912015-12-18 10:38:42 +00005944 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00005945 // The read barrier instrumentation of object ArrayGet
5946 // instructions does not support the HIntermediateAddress
5947 // instruction.
5948 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5949
Roland Levillainc9285912015-12-18 10:38:42 +00005950 static_assert(
5951 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5952 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00005953 // /* HeapReference<Object> */ out =
5954 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5955 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5956 Location temp = locations->GetTemp(0);
5957 // Note that a potential implicit null check is handled in this
5958 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005959 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
5960 if (index.IsConstant()) {
5961 // Array load with a constant index can be treated as a field load.
5962 data_offset += helpers::Int32ConstantFrom(index) << Primitive::ComponentSizeShift(type);
5963 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5964 out_loc,
5965 obj,
5966 data_offset,
5967 locations->GetTemp(0),
5968 /* needs_null_check */ false);
5969 } else {
5970 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5971 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
5972 }
Roland Levillainc9285912015-12-18 10:38:42 +00005973 } else {
5974 Register out = out_loc.AsRegister<Register>();
5975 if (index.IsConstant()) {
5976 size_t offset =
5977 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5978 __ LoadFromOffset(kLoadWord, out, obj, offset);
5979 codegen_->MaybeRecordImplicitNullCheck(instruction);
5980 // If read barriers are enabled, emit read barriers other than
5981 // Baker's using a slow path (and also unpoison the loaded
5982 // reference, if heap poisoning is enabled).
5983 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5984 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005985 Register temp = IP;
5986
5987 if (has_intermediate_address) {
5988 // We do not need to compute the intermediate address from the array: the
5989 // input instruction has done it already. See the comment in
5990 // `TryExtractArrayAccessAddress()`.
5991 if (kIsDebugBuild) {
5992 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5993 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5994 }
5995 temp = obj;
5996 } else {
5997 __ add(temp, obj, ShifterOperand(data_offset));
5998 }
5999 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01006000
Roland Levillainc9285912015-12-18 10:38:42 +00006001 codegen_->MaybeRecordImplicitNullCheck(instruction);
6002 // If read barriers are enabled, emit read barriers other than
6003 // Baker's using a slow path (and also unpoison the loaded
6004 // reference, if heap poisoning is enabled).
6005 codegen_->MaybeGenerateReadBarrierSlow(
6006 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6007 }
6008 }
6009 break;
6010 }
6011
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006012 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006013 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006014 size_t offset =
6015 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006016 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006017 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006018 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00006019 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006020 }
6021 break;
6022 }
6023
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006024 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00006025 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006026 if (index.IsConstant()) {
6027 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006028 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006029 } else {
6030 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00006031 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006032 }
6033 break;
6034 }
6035
6036 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00006037 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006038 if (index.IsConstant()) {
6039 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006040 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006041 } else {
6042 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00006043 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006044 }
6045 break;
6046 }
6047
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006048 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01006049 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006050 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006051 }
Roland Levillain4d027112015-07-01 15:41:14 +01006052
6053 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00006054 // Potential implicit null checks, in the case of reference
6055 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07006056 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00006057 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01006058 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006059}
6060
6061void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006062 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006063
6064 bool needs_write_barrier =
6065 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00006066 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006067
Nicolas Geoffray39468442014-09-02 15:17:15 +01006068 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006069 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01006070 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00006071 LocationSummary::kCallOnSlowPath :
6072 LocationSummary::kNoCall);
6073
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006074 locations->SetInAt(0, Location::RequiresRegister());
6075 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6076 if (Primitive::IsFloatingPointType(value_type)) {
6077 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006078 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006079 locations->SetInAt(2, Location::RequiresRegister());
6080 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006081 if (needs_write_barrier) {
6082 // Temporary registers for the write barrier.
6083 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006084 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006085 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006086}
6087
6088void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
6089 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006090 Location array_loc = locations->InAt(0);
6091 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006092 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006093 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00006094 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006095 bool needs_write_barrier =
6096 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01006097 uint32_t data_offset =
6098 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6099 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01006100 HInstruction* array_instr = instruction->GetArray();
6101 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006102
6103 switch (value_type) {
6104 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01006105 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006106 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01006107 case Primitive::kPrimChar:
6108 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006109 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01006110 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
6111 uint32_t full_offset =
6112 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6113 StoreOperandType store_type = GetStoreOperandType(value_type);
6114 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006115 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006116 Register temp = IP;
6117
6118 if (has_intermediate_address) {
6119 // We do not need to compute the intermediate address from the array: the
6120 // input instruction has done it already. See the comment in
6121 // `TryExtractArrayAccessAddress()`.
6122 if (kIsDebugBuild) {
6123 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6124 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
6125 }
6126 temp = array;
6127 } else {
6128 __ add(temp, array, ShifterOperand(data_offset));
6129 }
Artem Serov6c916792016-07-11 14:02:34 +01006130 codegen_->StoreToShiftedRegOffset(value_type,
6131 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01006132 temp,
Artem Serov6c916792016-07-11 14:02:34 +01006133 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006134 }
6135 break;
6136 }
6137
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006138 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00006139 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01006140 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6141 // See the comment in instruction_simplifier_shared.cc.
6142 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006143
6144 if (instruction->InputAt(2)->IsNullConstant()) {
6145 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006146 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006147 size_t offset =
6148 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01006149 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006150 } else {
6151 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006152 __ add(IP, array, ShifterOperand(data_offset));
6153 codegen_->StoreToShiftedRegOffset(value_type,
6154 value_loc,
6155 IP,
6156 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006157 }
Roland Levillain1407ee72016-01-08 15:56:19 +00006158 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00006159 DCHECK(!needs_write_barrier);
6160 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006161 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006162 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006163
6164 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01006165 Location temp1_loc = locations->GetTemp(0);
6166 Register temp1 = temp1_loc.AsRegister<Register>();
6167 Location temp2_loc = locations->GetTemp(1);
6168 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006169 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6170 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6171 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6172 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006173 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006174 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006175
Roland Levillain3b359c72015-11-17 19:35:12 +00006176 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006177 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
6178 codegen_->AddSlowPath(slow_path);
6179 if (instruction->GetValueCanBeNull()) {
6180 Label non_zero;
6181 __ CompareAndBranchIfNonZero(value, &non_zero);
6182 if (index.IsConstant()) {
6183 size_t offset =
6184 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6185 __ StoreToOffset(kStoreWord, value, array, offset);
6186 } else {
6187 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006188 __ add(IP, array, ShifterOperand(data_offset));
6189 codegen_->StoreToShiftedRegOffset(value_type,
6190 value_loc,
6191 IP,
6192 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006193 }
6194 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006195 __ b(final_label);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006196 __ Bind(&non_zero);
6197 }
6198
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006199 // Note that when read barriers are enabled, the type checks
6200 // are performed without read barriers. This is fine, even in
6201 // the case where a class object is in the from-space after
6202 // the flip, as a comparison involving such a type would not
6203 // produce a false positive; it may of course produce a false
6204 // negative, in which case we would take the ArraySet slow
6205 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006206
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006207 // /* HeapReference<Class> */ temp1 = array->klass_
6208 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
6209 codegen_->MaybeRecordImplicitNullCheck(instruction);
6210 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01006211
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006212 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6213 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6214 // /* HeapReference<Class> */ temp2 = value->klass_
6215 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
6216 // If heap poisoning is enabled, no need to unpoison `temp1`
6217 // nor `temp2`, as we are comparing two poisoned references.
6218 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01006219
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006220 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6221 Label do_put;
6222 __ b(&do_put, EQ);
6223 // If heap poisoning is enabled, the `temp1` reference has
6224 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00006225 __ MaybeUnpoisonHeapReference(temp1);
6226
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006227 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6228 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6229 // If heap poisoning is enabled, no need to unpoison
6230 // `temp1`, as we are comparing against null below.
6231 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
6232 __ Bind(&do_put);
6233 } else {
6234 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006235 }
6236 }
6237
Artem Serov6c916792016-07-11 14:02:34 +01006238 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006239 if (kPoisonHeapReferences) {
6240 // Note that in the case where `value` is a null reference,
6241 // we do not enter this block, as a null reference does not
6242 // need poisoning.
6243 DCHECK_EQ(value_type, Primitive::kPrimNot);
6244 __ Mov(temp1, value);
6245 __ PoisonHeapReference(temp1);
6246 source = temp1;
6247 }
6248
6249 if (index.IsConstant()) {
6250 size_t offset =
6251 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6252 __ StoreToOffset(kStoreWord, source, array, offset);
6253 } else {
6254 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006255
6256 __ add(IP, array, ShifterOperand(data_offset));
6257 codegen_->StoreToShiftedRegOffset(value_type,
6258 Location::RegisterLocation(source),
6259 IP,
6260 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006261 }
6262
Roland Levillain3b359c72015-11-17 19:35:12 +00006263 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006264 codegen_->MaybeRecordImplicitNullCheck(instruction);
6265 }
6266
6267 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6268
6269 if (done.IsLinked()) {
6270 __ Bind(&done);
6271 }
6272
6273 if (slow_path != nullptr) {
6274 __ Bind(slow_path->GetExitLabel());
6275 }
6276
6277 break;
6278 }
6279
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006280 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006281 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006282 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006283 size_t offset =
6284 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006285 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006286 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006287 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006288 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006289 }
6290 break;
6291 }
6292
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006293 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006294 Location value = locations->InAt(2);
6295 DCHECK(value.IsFpuRegister());
6296 if (index.IsConstant()) {
6297 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006298 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006299 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006300 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006301 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
6302 }
6303 break;
6304 }
6305
6306 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006307 Location value = locations->InAt(2);
6308 DCHECK(value.IsFpuRegisterPair());
6309 if (index.IsConstant()) {
6310 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006311 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006312 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006313 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006314 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
6315 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006316
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006317 break;
6318 }
6319
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006320 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006321 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006322 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006323 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006324
Roland Levillain80e67092016-01-08 16:04:55 +00006325 // Objects are handled in the switch.
6326 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006327 codegen_->MaybeRecordImplicitNullCheck(instruction);
6328 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006329}
6330
6331void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006332 LocationSummary* locations =
6333 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006334 locations->SetInAt(0, Location::RequiresRegister());
6335 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006336}
6337
6338void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
6339 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006340 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006341 Register obj = locations->InAt(0).AsRegister<Register>();
6342 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006343 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00006344 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07006345 // Mask out compression flag from String's array length.
6346 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006347 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07006348 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006349}
6350
Artem Serov328429f2016-07-06 16:23:04 +01006351void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01006352 LocationSummary* locations =
6353 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6354
6355 locations->SetInAt(0, Location::RequiresRegister());
6356 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6357 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6358}
6359
6360void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6361 LocationSummary* locations = instruction->GetLocations();
6362 Location out = locations->Out();
6363 Location first = locations->InAt(0);
6364 Location second = locations->InAt(1);
6365
Artem Serov328429f2016-07-06 16:23:04 +01006366 if (second.IsRegister()) {
6367 __ add(out.AsRegister<Register>(),
6368 first.AsRegister<Register>(),
6369 ShifterOperand(second.AsRegister<Register>()));
6370 } else {
6371 __ AddConstant(out.AsRegister<Register>(),
6372 first.AsRegister<Register>(),
6373 second.GetConstant()->AsIntConstant()->GetValue());
6374 }
6375}
6376
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006377void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006378 RegisterSet caller_saves = RegisterSet::Empty();
6379 InvokeRuntimeCallingConvention calling_convention;
6380 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6381 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6382 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006383
6384 HInstruction* index = instruction->InputAt(0);
6385 HInstruction* length = instruction->InputAt(1);
6386 // If both index and length are constants we can statically check the bounds. But if at least one
6387 // of them is not encodable ArmEncodableConstantOrRegister will create
6388 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6389 // locations.
6390 bool both_const = index->IsConstant() && length->IsConstant();
6391 locations->SetInAt(0, both_const
6392 ? Location::ConstantLocation(index->AsConstant())
6393 : ArmEncodableConstantOrRegister(index, CMP));
6394 locations->SetInAt(1, both_const
6395 ? Location::ConstantLocation(length->AsConstant())
6396 : ArmEncodableConstantOrRegister(length, CMP));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006397}
6398
6399void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
6400 LocationSummary* locations = instruction->GetLocations();
Artem Serov2dd053d2017-03-08 14:54:06 +00006401 Location index_loc = locations->InAt(0);
6402 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006403
Artem Serov2dd053d2017-03-08 14:54:06 +00006404 if (length_loc.IsConstant()) {
6405 int32_t length = helpers::Int32ConstantFrom(length_loc);
6406 if (index_loc.IsConstant()) {
6407 // BCE will remove the bounds check if we are guaranteed to pass.
6408 int32_t index = helpers::Int32ConstantFrom(index_loc);
6409 if (index < 0 || index >= length) {
6410 SlowPathCodeARM* slow_path =
6411 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6412 codegen_->AddSlowPath(slow_path);
6413 __ b(slow_path->GetEntryLabel());
6414 } else {
6415 // Some optimization after BCE may have generated this, and we should not
6416 // generate a bounds check if it is a valid range.
6417 }
6418 return;
6419 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006420
Artem Serov2dd053d2017-03-08 14:54:06 +00006421 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6422 __ cmp(index_loc.AsRegister<Register>(), ShifterOperand(length));
6423 codegen_->AddSlowPath(slow_path);
6424 __ b(slow_path->GetEntryLabel(), HS);
6425 } else {
6426 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6427 if (index_loc.IsConstant()) {
6428 int32_t index = helpers::Int32ConstantFrom(index_loc);
6429 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index));
6430 } else {
6431 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index_loc.AsRegister<Register>()));
6432 }
6433 codegen_->AddSlowPath(slow_path);
6434 __ b(slow_path->GetEntryLabel(), LS);
6435 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006436}
6437
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006438void CodeGeneratorARM::MarkGCCard(Register temp,
6439 Register card,
6440 Register object,
6441 Register value,
6442 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006443 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006444 if (can_be_null) {
6445 __ CompareAndBranchIfZero(value, &is_null);
6446 }
Andreas Gampe542451c2016-07-26 09:02:02 -07006447 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006448 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
6449 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006450 if (can_be_null) {
6451 __ Bind(&is_null);
6452 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006453}
6454
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006455void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006456 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006457}
6458
6459void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006460 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6461}
6462
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006463void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006464 LocationSummary* locations =
6465 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006466 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006467}
6468
6469void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006470 HBasicBlock* block = instruction->GetBlock();
6471 if (block->GetLoopInformation() != nullptr) {
6472 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6473 // The back edge will generate the suspend check.
6474 return;
6475 }
6476 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6477 // The goto will generate the suspend check.
6478 return;
6479 }
6480 GenerateSuspendCheck(instruction, nullptr);
6481}
6482
6483void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
6484 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006485 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006486 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
6487 if (slow_path == nullptr) {
6488 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
6489 instruction->SetSlowPath(slow_path);
6490 codegen_->AddSlowPath(slow_path);
6491 if (successor != nullptr) {
6492 DCHECK(successor->IsLoopHeader());
6493 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6494 }
6495 } else {
6496 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6497 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006498
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00006499 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006500 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006501 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006502 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006503 __ Bind(slow_path->GetReturnLabel());
6504 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006505 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006506 __ b(slow_path->GetEntryLabel());
6507 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006508}
6509
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006510ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
6511 return codegen_->GetAssembler();
6512}
6513
6514void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006515 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006516 Location source = move->GetSource();
6517 Location destination = move->GetDestination();
6518
6519 if (source.IsRegister()) {
6520 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006521 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006522 } else if (destination.IsFpuRegister()) {
6523 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006524 } else {
6525 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006526 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006527 SP, destination.GetStackIndex());
6528 }
6529 } else if (source.IsStackSlot()) {
6530 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006531 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006532 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006533 } else if (destination.IsFpuRegister()) {
6534 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006535 } else {
6536 DCHECK(destination.IsStackSlot());
6537 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
6538 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6539 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006540 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006541 if (destination.IsRegister()) {
6542 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
6543 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006544 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006545 } else {
6546 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006547 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
6548 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006549 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006550 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006551 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
6552 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006553 } else if (destination.IsRegisterPair()) {
6554 DCHECK(ExpectedPairLayout(destination));
6555 __ LoadFromOffset(
6556 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
6557 } else {
6558 DCHECK(destination.IsFpuRegisterPair()) << destination;
6559 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6560 SP,
6561 source.GetStackIndex());
6562 }
6563 } else if (source.IsRegisterPair()) {
6564 if (destination.IsRegisterPair()) {
6565 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6566 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006567 } else if (destination.IsFpuRegisterPair()) {
6568 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6569 source.AsRegisterPairLow<Register>(),
6570 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006571 } else {
6572 DCHECK(destination.IsDoubleStackSlot()) << destination;
6573 DCHECK(ExpectedPairLayout(source));
6574 __ StoreToOffset(
6575 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
6576 }
6577 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006578 if (destination.IsRegisterPair()) {
6579 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6580 destination.AsRegisterPairHigh<Register>(),
6581 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6582 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006583 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6584 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6585 } else {
6586 DCHECK(destination.IsDoubleStackSlot()) << destination;
6587 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
6588 SP,
6589 destination.GetStackIndex());
6590 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006591 } else {
6592 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006593 HConstant* constant = source.GetConstant();
6594 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6595 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006596 if (destination.IsRegister()) {
6597 __ LoadImmediate(destination.AsRegister<Register>(), value);
6598 } else {
6599 DCHECK(destination.IsStackSlot());
6600 __ LoadImmediate(IP, value);
6601 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6602 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006603 } else if (constant->IsLongConstant()) {
6604 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006605 if (destination.IsRegisterPair()) {
6606 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
6607 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006608 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006609 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006610 __ LoadImmediate(IP, Low32Bits(value));
6611 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6612 __ LoadImmediate(IP, High32Bits(value));
6613 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6614 }
6615 } else if (constant->IsDoubleConstant()) {
6616 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006617 if (destination.IsFpuRegisterPair()) {
6618 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006619 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006620 DCHECK(destination.IsDoubleStackSlot()) << destination;
6621 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006622 __ LoadImmediate(IP, Low32Bits(int_value));
6623 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6624 __ LoadImmediate(IP, High32Bits(int_value));
6625 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6626 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006627 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006628 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006629 float value = constant->AsFloatConstant()->GetValue();
6630 if (destination.IsFpuRegister()) {
6631 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
6632 } else {
6633 DCHECK(destination.IsStackSlot());
6634 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
6635 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6636 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006637 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006638 }
6639}
6640
6641void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
6642 __ Mov(IP, reg);
6643 __ LoadFromOffset(kLoadWord, reg, SP, mem);
6644 __ StoreToOffset(kStoreWord, IP, SP, mem);
6645}
6646
6647void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
6648 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
6649 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6650 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
6651 SP, mem1 + stack_offset);
6652 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
6653 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
6654 SP, mem2 + stack_offset);
6655 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
6656}
6657
6658void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006659 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006660 Location source = move->GetSource();
6661 Location destination = move->GetDestination();
6662
6663 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006664 DCHECK_NE(source.AsRegister<Register>(), IP);
6665 DCHECK_NE(destination.AsRegister<Register>(), IP);
6666 __ Mov(IP, source.AsRegister<Register>());
6667 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
6668 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006669 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006670 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006671 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006672 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006673 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
6674 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006675 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006676 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006677 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006678 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006679 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006680 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006681 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006682 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006683 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6684 destination.AsRegisterPairHigh<Register>(),
6685 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006686 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006687 Register low_reg = source.IsRegisterPair()
6688 ? source.AsRegisterPairLow<Register>()
6689 : destination.AsRegisterPairLow<Register>();
6690 int mem = source.IsRegisterPair()
6691 ? destination.GetStackIndex()
6692 : source.GetStackIndex();
6693 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006694 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006695 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006696 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006697 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006698 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
6699 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006700 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006701 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006702 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006703 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
6704 DRegister reg = source.IsFpuRegisterPair()
6705 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
6706 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
6707 int mem = source.IsFpuRegisterPair()
6708 ? destination.GetStackIndex()
6709 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006710 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006711 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006712 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006713 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
6714 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
6715 : destination.AsFpuRegister<SRegister>();
6716 int mem = source.IsFpuRegister()
6717 ? destination.GetStackIndex()
6718 : source.GetStackIndex();
6719
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006720 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006721 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006722 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006723 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006724 Exchange(source.GetStackIndex(), destination.GetStackIndex());
6725 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006726 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006727 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006728 }
6729}
6730
6731void ParallelMoveResolverARM::SpillScratch(int reg) {
6732 __ Push(static_cast<Register>(reg));
6733}
6734
6735void ParallelMoveResolverARM::RestoreScratch(int reg) {
6736 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006737}
6738
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006739HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
6740 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006741 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006742 case HLoadClass::LoadKind::kInvalid:
6743 LOG(FATAL) << "UNREACHABLE";
6744 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006745 case HLoadClass::LoadKind::kReferrersClass:
6746 break;
6747 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6748 DCHECK(!GetCompilerOptions().GetCompilePic());
6749 break;
6750 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6751 DCHECK(GetCompilerOptions().GetCompilePic());
6752 break;
6753 case HLoadClass::LoadKind::kBootImageAddress:
6754 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006755 case HLoadClass::LoadKind::kBssEntry:
6756 DCHECK(!Runtime::Current()->UseJitCompilation());
6757 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006758 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006759 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006760 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006761 case HLoadClass::LoadKind::kDexCacheViaMethod:
6762 break;
6763 }
6764 return desired_class_load_kind;
6765}
6766
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006767void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006768 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6769 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006770 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006771 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006772 cls,
6773 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006774 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006775 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006776 return;
6777 }
Vladimir Marko41559982017-01-06 14:04:23 +00006778 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006779
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006780 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6781 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006782 ? LocationSummary::kCallOnSlowPath
6783 : LocationSummary::kNoCall;
6784 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006785 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006786 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006787 }
6788
Vladimir Marko41559982017-01-06 14:04:23 +00006789 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006790 locations->SetInAt(0, Location::RequiresRegister());
6791 }
6792 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006793 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6794 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6795 // Rely on the type resolution or initialization and marking to save everything we need.
6796 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6797 // to the custom calling convention) or by marking, so we request a different temp.
6798 locations->AddTemp(Location::RequiresRegister());
6799 RegisterSet caller_saves = RegisterSet::Empty();
6800 InvokeRuntimeCallingConvention calling_convention;
6801 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6802 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6803 // that the the kPrimNot result register is the same as the first argument register.
6804 locations->SetCustomSlowPathCallerSaves(caller_saves);
6805 } else {
6806 // For non-Baker read barrier we have a temp-clobbering call.
6807 }
6808 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006809 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
6810 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
6811 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
6812 !Runtime::Current()->UseJitCompilation())) {
6813 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6814 }
6815 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006816}
6817
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006818// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6819// move.
6820void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006821 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6822 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6823 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006824 return;
6825 }
Vladimir Marko41559982017-01-06 14:04:23 +00006826 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006827
Vladimir Marko41559982017-01-06 14:04:23 +00006828 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006829 Location out_loc = locations->Out();
6830 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006831
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006832 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6833 ? kWithoutReadBarrier
6834 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006835 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006836 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006837 case HLoadClass::LoadKind::kReferrersClass: {
6838 DCHECK(!cls->CanCallRuntime());
6839 DCHECK(!cls->MustGenerateClinitCheck());
6840 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6841 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006842 GenerateGcRootFieldLoad(cls,
6843 out_loc,
6844 current_method,
6845 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006846 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006847 break;
6848 }
6849 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006850 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006851 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006852 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6853 cls->GetTypeIndex()));
6854 break;
6855 }
6856 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006857 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006858 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006859 CodeGeneratorARM::PcRelativePatchInfo* labels =
6860 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6861 __ BindTrackedLabel(&labels->movw_label);
6862 __ movw(out, /* placeholder */ 0u);
6863 __ BindTrackedLabel(&labels->movt_label);
6864 __ movt(out, /* placeholder */ 0u);
6865 __ BindTrackedLabel(&labels->add_pc_label);
6866 __ add(out, out, ShifterOperand(PC));
6867 break;
6868 }
6869 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006870 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006871 uint32_t address = dchecked_integral_cast<uint32_t>(
6872 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6873 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006874 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6875 break;
6876 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006877 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006878 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6879 ? locations->GetTemp(0).AsRegister<Register>()
6880 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006881 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006882 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006883 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006884 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006885 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006886 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006887 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006888 __ add(temp, temp, ShifterOperand(PC));
6889 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006890 generate_null_check = true;
6891 break;
6892 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006893 case HLoadClass::LoadKind::kJitTableAddress: {
6894 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6895 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006896 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006897 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006898 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006899 break;
6900 }
Vladimir Marko41559982017-01-06 14:04:23 +00006901 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006902 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006903 LOG(FATAL) << "UNREACHABLE";
6904 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006905 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006906
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006907 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6908 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01006909 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006910 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6911 codegen_->AddSlowPath(slow_path);
6912 if (generate_null_check) {
6913 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6914 }
6915 if (cls->MustGenerateClinitCheck()) {
6916 GenerateClassInitializationCheck(slow_path, out);
6917 } else {
6918 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006919 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006920 }
6921}
6922
6923void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
6924 LocationSummary* locations =
6925 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6926 locations->SetInAt(0, Location::RequiresRegister());
6927 if (check->HasUses()) {
6928 locations->SetOut(Location::SameAsFirstInput());
6929 }
6930}
6931
6932void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006933 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006934 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006935 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006936 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006937 GenerateClassInitializationCheck(slow_path,
6938 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006939}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006940
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006941void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01006942 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006943 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
6944 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
6945 __ b(slow_path->GetEntryLabel(), LT);
6946 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6947 // properly. Therefore, we do a memory fence.
6948 __ dmb(ISH);
6949 __ Bind(slow_path->GetExitLabel());
6950}
6951
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006952HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
6953 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006954 switch (desired_string_load_kind) {
6955 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6956 DCHECK(!GetCompilerOptions().GetCompilePic());
6957 break;
6958 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6959 DCHECK(GetCompilerOptions().GetCompilePic());
6960 break;
6961 case HLoadString::LoadKind::kBootImageAddress:
6962 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006963 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006964 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006965 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006966 case HLoadString::LoadKind::kJitTableAddress:
6967 DCHECK(Runtime::Current()->UseJitCompilation());
6968 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006969 case HLoadString::LoadKind::kDexCacheViaMethod:
6970 break;
6971 }
6972 return desired_string_load_kind;
6973}
6974
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006975void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006976 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006977 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006978 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006979 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006980 locations->SetOut(Location::RegisterLocation(R0));
6981 } else {
6982 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006983 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6984 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006985 // Rely on the pResolveString and marking to save everything we need, including temps.
6986 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6987 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006988 locations->AddTemp(Location::RequiresRegister());
6989 RegisterSet caller_saves = RegisterSet::Empty();
6990 InvokeRuntimeCallingConvention calling_convention;
6991 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6992 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6993 // that the the kPrimNot result register is the same as the first argument register.
6994 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006995 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
6996 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6997 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006998 } else {
6999 // For non-Baker read barrier we have a temp-clobbering call.
7000 }
7001 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007002 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007003}
7004
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007005// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7006// move.
7007void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007008 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007009 Location out_loc = locations->Out();
7010 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007011 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00007012
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007013 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007014 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007015 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007016 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
7017 load->GetStringIndex()));
7018 return; // No dex cache slow path.
7019 }
7020 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007021 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007022 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007023 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007024 __ BindTrackedLabel(&labels->movw_label);
7025 __ movw(out, /* placeholder */ 0u);
7026 __ BindTrackedLabel(&labels->movt_label);
7027 __ movt(out, /* placeholder */ 0u);
7028 __ BindTrackedLabel(&labels->add_pc_label);
7029 __ add(out, out, ShifterOperand(PC));
7030 return; // No dex cache slow path.
7031 }
7032 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007033 uint32_t address = dchecked_integral_cast<uint32_t>(
7034 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7035 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007036 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7037 return; // No dex cache slow path.
7038 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007039 case HLoadString::LoadKind::kBssEntry: {
7040 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007041 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7042 ? locations->GetTemp(0).AsRegister<Register>()
7043 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00007044 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007045 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007046 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007047 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007048 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007049 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007050 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007051 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007052 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007053 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
7054 codegen_->AddSlowPath(slow_path);
7055 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7056 __ Bind(slow_path->GetExitLabel());
7057 return;
7058 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007059 case HLoadString::LoadKind::kJitTableAddress: {
7060 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007061 load->GetStringIndex(),
7062 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007063 // /* GcRoot<mirror::String> */ out = *out
7064 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7065 return;
7066 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007067 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007068 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007069 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007070
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007071 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
7072 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
7073 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007074 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007075 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007076 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7077 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007078}
7079
David Brazdilcb1c0552015-08-04 16:22:25 +01007080static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007081 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01007082}
7083
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007084void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
7085 LocationSummary* locations =
7086 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7087 locations->SetOut(Location::RequiresRegister());
7088}
7089
7090void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007091 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01007092 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7093}
7094
7095void LocationsBuilderARM::VisitClearException(HClearException* clear) {
7096 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7097}
7098
7099void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007100 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01007101 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007102}
7103
7104void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
7105 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007106 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007107 InvokeRuntimeCallingConvention calling_convention;
7108 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7109}
7110
7111void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007112 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007113 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007114}
7115
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007116// Temp is used for read barrier.
7117static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7118 if (kEmitCompilerReadBarrier &&
7119 (kUseBakerReadBarrier ||
7120 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7121 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7122 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7123 return 1;
7124 }
7125 return 0;
7126}
7127
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007128// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007129// interface pointer, one for loading the current interface.
7130// The other checks have one temp for loading the object's class.
7131static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7132 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7133 return 3;
7134 }
7135 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00007136}
7137
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007138void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007139 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00007140 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007141 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00007142 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007143 case TypeCheckKind::kExactCheck:
7144 case TypeCheckKind::kAbstractClassCheck:
7145 case TypeCheckKind::kClassHierarchyCheck:
7146 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007147 call_kind =
7148 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01007149 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007150 break;
7151 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007152 case TypeCheckKind::kUnresolvedCheck:
7153 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007154 call_kind = LocationSummary::kCallOnSlowPath;
7155 break;
7156 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007157
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007158 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007159 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007160 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007161 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007162 locations->SetInAt(0, Location::RequiresRegister());
7163 locations->SetInAt(1, Location::RequiresRegister());
7164 // The "out" register is used as a temporary, so it overlaps with the inputs.
7165 // Note that TypeCheckSlowPathARM uses this register too.
7166 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007167 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007168 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7169 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7170 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007171}
7172
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007173void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007174 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007175 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007176 Location obj_loc = locations->InAt(0);
7177 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007178 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007179 Location out_loc = locations->Out();
7180 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007181 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7182 DCHECK_LE(num_temps, 1u);
7183 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007184 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007185 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7186 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7187 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007188 Label done;
7189 Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01007190 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007191
7192 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007193 // avoid null check if we know obj is not null.
7194 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007195 DCHECK_NE(out, obj);
7196 __ LoadImmediate(out, 0);
7197 __ CompareAndBranchIfZero(obj, final_label);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007198 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007199
Roland Levillainc9285912015-12-18 10:38:42 +00007200 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007201 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007202 // /* HeapReference<Class> */ out = obj->klass_
7203 GenerateReferenceLoadTwoRegisters(instruction,
7204 out_loc,
7205 obj_loc,
7206 class_offset,
7207 maybe_temp_loc,
7208 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007209 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007210 __ cmp(out, ShifterOperand(cls));
7211 // We speculatively set the result to false without changing the condition
7212 // flags, which allows us to avoid some branching later.
7213 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7214
7215 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7216 // we check that the output is in a low register, so that a 16-bit MOV
7217 // encoding can be used.
7218 if (ArmAssembler::IsLowRegister(out)) {
7219 __ it(EQ);
7220 __ mov(out, ShifterOperand(1), EQ);
7221 } else {
7222 __ b(final_label, NE);
7223 __ LoadImmediate(out, 1);
7224 }
7225
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007226 break;
7227 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007228
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007229 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007230 // /* HeapReference<Class> */ out = obj->klass_
7231 GenerateReferenceLoadTwoRegisters(instruction,
7232 out_loc,
7233 obj_loc,
7234 class_offset,
7235 maybe_temp_loc,
7236 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007237 // If the class is abstract, we eagerly fetch the super class of the
7238 // object to avoid doing a comparison we know will fail.
7239 Label loop;
7240 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007241 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007242 GenerateReferenceLoadOneRegister(instruction,
7243 out_loc,
7244 super_offset,
7245 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007246 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007247 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007248 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007249 __ cmp(out, ShifterOperand(cls));
7250 __ b(&loop, NE);
7251 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007252 break;
7253 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007254
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007255 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007256 // /* HeapReference<Class> */ out = obj->klass_
7257 GenerateReferenceLoadTwoRegisters(instruction,
7258 out_loc,
7259 obj_loc,
7260 class_offset,
7261 maybe_temp_loc,
7262 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007263 // Walk over the class hierarchy to find a match.
7264 Label loop, success;
7265 __ Bind(&loop);
7266 __ cmp(out, ShifterOperand(cls));
7267 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007268 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007269 GenerateReferenceLoadOneRegister(instruction,
7270 out_loc,
7271 super_offset,
7272 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007273 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007274 // This is essentially a null check, but it sets the condition flags to the
7275 // proper value for the code that follows the loop, i.e. not `EQ`.
7276 __ cmp(out, ShifterOperand(1));
7277 __ b(&loop, HS);
7278
7279 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7280 // we check that the output is in a low register, so that a 16-bit MOV
7281 // encoding can be used.
7282 if (ArmAssembler::IsLowRegister(out)) {
7283 // If `out` is null, we use it for the result, and the condition flags
7284 // have already been set to `NE`, so the IT block that comes afterwards
7285 // (and which handles the successful case) turns into a NOP (instead of
7286 // overwriting `out`).
7287 __ Bind(&success);
7288 // There is only one branch to the `success` label (which is bound to this
7289 // IT block), and it has the same condition, `EQ`, so in that case the MOV
7290 // is executed.
7291 __ it(EQ);
7292 __ mov(out, ShifterOperand(1), EQ);
7293 } else {
7294 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007295 __ b(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007296 __ Bind(&success);
7297 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007298 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007299
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007300 break;
7301 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007302
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007303 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007304 // /* HeapReference<Class> */ out = obj->klass_
7305 GenerateReferenceLoadTwoRegisters(instruction,
7306 out_loc,
7307 obj_loc,
7308 class_offset,
7309 maybe_temp_loc,
7310 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007311 // Do an exact check.
7312 Label exact_check;
7313 __ cmp(out, ShifterOperand(cls));
7314 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007315 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007316 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007317 GenerateReferenceLoadOneRegister(instruction,
7318 out_loc,
7319 component_offset,
7320 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007321 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007322 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007323 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007324 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7325 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007326 __ cmp(out, ShifterOperand(0));
7327 // We speculatively set the result to false without changing the condition
7328 // flags, which allows us to avoid some branching later.
7329 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7330
7331 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7332 // we check that the output is in a low register, so that a 16-bit MOV
7333 // encoding can be used.
7334 if (ArmAssembler::IsLowRegister(out)) {
7335 __ Bind(&exact_check);
7336 __ it(EQ);
7337 __ mov(out, ShifterOperand(1), EQ);
7338 } else {
7339 __ b(final_label, NE);
7340 __ Bind(&exact_check);
7341 __ LoadImmediate(out, 1);
7342 }
7343
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007344 break;
7345 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007346
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007347 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007348 // No read barrier since the slow path will retry upon failure.
7349 // /* HeapReference<Class> */ out = obj->klass_
7350 GenerateReferenceLoadTwoRegisters(instruction,
7351 out_loc,
7352 obj_loc,
7353 class_offset,
7354 maybe_temp_loc,
7355 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007356 __ cmp(out, ShifterOperand(cls));
7357 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00007358 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7359 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007360 codegen_->AddSlowPath(slow_path);
7361 __ b(slow_path->GetEntryLabel(), NE);
7362 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007363 break;
7364 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007365
Calin Juravle98893e12015-10-02 21:05:03 +01007366 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007367 case TypeCheckKind::kInterfaceCheck: {
7368 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007369 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00007370 // cases.
7371 //
7372 // We cannot directly call the InstanceofNonTrivial runtime
7373 // entry point without resorting to a type checking slow path
7374 // here (i.e. by calling InvokeRuntime directly), as it would
7375 // require to assign fixed registers for the inputs of this
7376 // HInstanceOf instruction (following the runtime calling
7377 // convention), which might be cluttered by the potential first
7378 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00007379 //
7380 // TODO: Introduce a new runtime entry point taking the object
7381 // to test (instead of its class) as argument, and let it deal
7382 // with the read barrier issues. This will let us refactor this
7383 // case of the `switch` code as it was previously (with a direct
7384 // call to the runtime not using a type checking slow path).
7385 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00007386 DCHECK(locations->OnlyCallsOnSlowPath());
7387 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7388 /* is_fatal */ false);
7389 codegen_->AddSlowPath(slow_path);
7390 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007391 break;
7392 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007393 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007394
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007395 if (done.IsLinked()) {
7396 __ Bind(&done);
7397 }
7398
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007399 if (slow_path != nullptr) {
7400 __ Bind(slow_path->GetExitLabel());
7401 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007402}
7403
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007404void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007405 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7406 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7407
Roland Levillain3b359c72015-11-17 19:35:12 +00007408 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7409 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007410 case TypeCheckKind::kExactCheck:
7411 case TypeCheckKind::kAbstractClassCheck:
7412 case TypeCheckKind::kClassHierarchyCheck:
7413 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007414 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7415 LocationSummary::kCallOnSlowPath :
7416 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007417 break;
7418 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007419 case TypeCheckKind::kUnresolvedCheck:
7420 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007421 call_kind = LocationSummary::kCallOnSlowPath;
7422 break;
7423 }
7424
Roland Levillain3b359c72015-11-17 19:35:12 +00007425 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7426 locations->SetInAt(0, Location::RequiresRegister());
7427 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007428 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007429}
7430
7431void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007432 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007433 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007434 Location obj_loc = locations->InAt(0);
7435 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007436 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007437 Location temp_loc = locations->GetTemp(0);
7438 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007439 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7440 DCHECK_LE(num_temps, 3u);
7441 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7442 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7443 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7444 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7445 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7446 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7447 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7448 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7449 const uint32_t object_array_data_offset =
7450 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007451
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007452 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7453 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7454 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007455 bool is_type_check_slow_path_fatal = false;
7456 if (!kEmitCompilerReadBarrier) {
7457 is_type_check_slow_path_fatal =
7458 (type_check_kind == TypeCheckKind::kExactCheck ||
7459 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7460 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7461 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7462 !instruction->CanThrowIntoCatchBlock();
7463 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01007464 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007465 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7466 is_type_check_slow_path_fatal);
7467 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007468
7469 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007470 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007471 // Avoid null check if we know obj is not null.
7472 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007473 __ CompareAndBranchIfZero(obj, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007474 }
7475
Roland Levillain3b359c72015-11-17 19:35:12 +00007476 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007477 case TypeCheckKind::kExactCheck:
7478 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007479 // /* HeapReference<Class> */ temp = obj->klass_
7480 GenerateReferenceLoadTwoRegisters(instruction,
7481 temp_loc,
7482 obj_loc,
7483 class_offset,
7484 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007485 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007486
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007487 __ cmp(temp, ShifterOperand(cls));
7488 // Jump to slow path for throwing the exception or doing a
7489 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00007490 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007491 break;
7492 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007493
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007494 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007495 // /* HeapReference<Class> */ temp = obj->klass_
7496 GenerateReferenceLoadTwoRegisters(instruction,
7497 temp_loc,
7498 obj_loc,
7499 class_offset,
7500 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007501 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007502
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007503 // If the class is abstract, we eagerly fetch the super class of the
7504 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007505 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007506 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007507 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007508 GenerateReferenceLoadOneRegister(instruction,
7509 temp_loc,
7510 super_offset,
7511 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007512 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007513
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007514 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7515 // exception.
7516 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00007517
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007518 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007519 __ cmp(temp, ShifterOperand(cls));
7520 __ b(&loop, NE);
7521 break;
7522 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007523
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007524 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007525 // /* HeapReference<Class> */ temp = obj->klass_
7526 GenerateReferenceLoadTwoRegisters(instruction,
7527 temp_loc,
7528 obj_loc,
7529 class_offset,
7530 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007531 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007532
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007533 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007534 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007535 __ Bind(&loop);
7536 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007537 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007538
Roland Levillain3b359c72015-11-17 19:35:12 +00007539 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007540 GenerateReferenceLoadOneRegister(instruction,
7541 temp_loc,
7542 super_offset,
7543 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007544 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007545
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007546 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7547 // exception.
7548 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7549 // Otherwise, jump to the beginning of the loop.
7550 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007551 break;
7552 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007553
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007554 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007555 // /* HeapReference<Class> */ temp = obj->klass_
7556 GenerateReferenceLoadTwoRegisters(instruction,
7557 temp_loc,
7558 obj_loc,
7559 class_offset,
7560 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007561 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007562
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007563 // Do an exact check.
7564 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007565 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007566
7567 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007568 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007569 GenerateReferenceLoadOneRegister(instruction,
7570 temp_loc,
7571 component_offset,
7572 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007573 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007574 // If the component type is null, jump to the slow path to throw the exception.
7575 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7576 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7577 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007578 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00007579 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007580 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007581 break;
7582 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007583
Calin Juravle98893e12015-10-02 21:05:03 +01007584 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007585 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00007586 // We cannot directly call the CheckCast runtime entry point
7587 // without resorting to a type checking slow path here (i.e. by
7588 // calling InvokeRuntime directly), as it would require to
7589 // assign fixed registers for the inputs of this HInstanceOf
7590 // instruction (following the runtime calling convention), which
7591 // might be cluttered by the potential first read barrier
7592 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007593
Roland Levillain3b359c72015-11-17 19:35:12 +00007594 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007595 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007596
7597 case TypeCheckKind::kInterfaceCheck: {
7598 // Avoid read barriers to improve performance of the fast path. We can not get false
7599 // positives by doing this.
7600 // /* HeapReference<Class> */ temp = obj->klass_
7601 GenerateReferenceLoadTwoRegisters(instruction,
7602 temp_loc,
7603 obj_loc,
7604 class_offset,
7605 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007606 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007607
7608 // /* HeapReference<Class> */ temp = temp->iftable_
7609 GenerateReferenceLoadTwoRegisters(instruction,
7610 temp_loc,
7611 temp_loc,
7612 iftable_offset,
7613 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007614 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007615 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007616 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08007617 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007618 Label start_loop;
7619 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007620 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
7621 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007622 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
7623 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007624 // Go to next interface.
7625 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
7626 __ sub(maybe_temp2_loc.AsRegister<Register>(),
7627 maybe_temp2_loc.AsRegister<Register>(),
7628 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007629 // Compare the classes and continue the loop if they do not match.
7630 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
7631 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007632 break;
7633 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007634 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007635
7636 if (done.IsLinked()) {
7637 __ Bind(&done);
7638 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007639
Roland Levillain3b359c72015-11-17 19:35:12 +00007640 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007641}
7642
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007643void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
7644 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007645 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007646 InvokeRuntimeCallingConvention calling_convention;
7647 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7648}
7649
7650void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007651 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7652 instruction,
7653 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007654 if (instruction->IsEnter()) {
7655 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7656 } else {
7657 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7658 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007659}
7660
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007661void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
7662void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
7663void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007664
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007665void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007666 LocationSummary* locations =
7667 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7668 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7669 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007670 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007671 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007672 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00007673 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007674}
7675
7676void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
7677 HandleBitwiseOperation(instruction);
7678}
7679
7680void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
7681 HandleBitwiseOperation(instruction);
7682}
7683
7684void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
7685 HandleBitwiseOperation(instruction);
7686}
7687
Artem Serov7fc63502016-02-09 17:15:29 +00007688
7689void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7690 LocationSummary* locations =
7691 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7692 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7693 || instruction->GetResultType() == Primitive::kPrimLong);
7694
7695 locations->SetInAt(0, Location::RequiresRegister());
7696 locations->SetInAt(1, Location::RequiresRegister());
7697 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7698}
7699
7700void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7701 LocationSummary* locations = instruction->GetLocations();
7702 Location first = locations->InAt(0);
7703 Location second = locations->InAt(1);
7704 Location out = locations->Out();
7705
7706 if (instruction->GetResultType() == Primitive::kPrimInt) {
7707 Register first_reg = first.AsRegister<Register>();
7708 ShifterOperand second_reg(second.AsRegister<Register>());
7709 Register out_reg = out.AsRegister<Register>();
7710
7711 switch (instruction->GetOpKind()) {
7712 case HInstruction::kAnd:
7713 __ bic(out_reg, first_reg, second_reg);
7714 break;
7715 case HInstruction::kOr:
7716 __ orn(out_reg, first_reg, second_reg);
7717 break;
7718 // There is no EON on arm.
7719 case HInstruction::kXor:
7720 default:
7721 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7722 UNREACHABLE();
7723 }
7724 return;
7725
7726 } else {
7727 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7728 Register first_low = first.AsRegisterPairLow<Register>();
7729 Register first_high = first.AsRegisterPairHigh<Register>();
7730 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7731 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7732 Register out_low = out.AsRegisterPairLow<Register>();
7733 Register out_high = out.AsRegisterPairHigh<Register>();
7734
7735 switch (instruction->GetOpKind()) {
7736 case HInstruction::kAnd:
7737 __ bic(out_low, first_low, second_low);
7738 __ bic(out_high, first_high, second_high);
7739 break;
7740 case HInstruction::kOr:
7741 __ orn(out_low, first_low, second_low);
7742 __ orn(out_high, first_high, second_high);
7743 break;
7744 // There is no EON on arm.
7745 case HInstruction::kXor:
7746 default:
7747 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7748 UNREACHABLE();
7749 }
7750 }
7751}
7752
Anton Kirilov74234da2017-01-13 14:42:47 +00007753void LocationsBuilderARM::VisitDataProcWithShifterOp(
7754 HDataProcWithShifterOp* instruction) {
7755 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
7756 instruction->GetType() == Primitive::kPrimLong);
7757 LocationSummary* locations =
7758 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7759 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
7760 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7761
7762 locations->SetInAt(0, Location::RequiresRegister());
7763 locations->SetInAt(1, Location::RequiresRegister());
7764 locations->SetOut(Location::RequiresRegister(),
7765 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7766}
7767
7768void InstructionCodeGeneratorARM::VisitDataProcWithShifterOp(
7769 HDataProcWithShifterOp* instruction) {
7770 const LocationSummary* const locations = instruction->GetLocations();
7771 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
7772 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
7773 const Location left = locations->InAt(0);
7774 const Location right = locations->InAt(1);
7775 const Location out = locations->Out();
7776
7777 if (instruction->GetType() == Primitive::kPrimInt) {
7778 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
7779
7780 const Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
7781 ? right.AsRegisterPairLow<Register>()
7782 : right.AsRegister<Register>();
7783
7784 GenerateDataProcInstruction(kind,
7785 out.AsRegister<Register>(),
7786 left.AsRegister<Register>(),
7787 ShifterOperand(second,
7788 ShiftFromOpKind(op_kind),
7789 instruction->GetShiftAmount()),
7790 codegen_);
7791 } else {
7792 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7793
7794 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7795 const Register second = right.AsRegister<Register>();
7796
7797 DCHECK_NE(out.AsRegisterPairLow<Register>(), second);
7798 GenerateDataProc(kind,
7799 out,
7800 left,
7801 ShifterOperand(second),
7802 ShifterOperand(second, ASR, 31),
7803 codegen_);
7804 } else {
7805 GenerateLongDataProc(instruction, codegen_);
7806 }
7807 }
7808}
7809
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007810void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
7811 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7812 if (value == 0xffffffffu) {
7813 if (out != first) {
7814 __ mov(out, ShifterOperand(first));
7815 }
7816 return;
7817 }
7818 if (value == 0u) {
7819 __ mov(out, ShifterOperand(0));
7820 return;
7821 }
7822 ShifterOperand so;
7823 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
7824 __ and_(out, first, so);
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007825 } else if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007826 __ bic(out, first, ShifterOperand(~value));
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007827 } else {
7828 DCHECK(IsPowerOfTwo(value + 1));
7829 __ ubfx(out, first, 0, WhichPowerOf2(value + 1));
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007830 }
7831}
7832
7833void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
7834 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7835 if (value == 0u) {
7836 if (out != first) {
7837 __ mov(out, ShifterOperand(first));
7838 }
7839 return;
7840 }
7841 if (value == 0xffffffffu) {
7842 __ mvn(out, ShifterOperand(0));
7843 return;
7844 }
7845 ShifterOperand so;
7846 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
7847 __ orr(out, first, so);
7848 } else {
7849 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
7850 __ orn(out, first, ShifterOperand(~value));
7851 }
7852}
7853
7854void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
7855 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7856 if (value == 0u) {
7857 if (out != first) {
7858 __ mov(out, ShifterOperand(first));
7859 }
7860 return;
7861 }
7862 __ eor(out, first, ShifterOperand(value));
7863}
7864
Vladimir Marko59751a72016-08-05 14:37:27 +01007865void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
7866 Location first,
7867 uint64_t value) {
7868 Register out_low = out.AsRegisterPairLow<Register>();
7869 Register out_high = out.AsRegisterPairHigh<Register>();
7870 Register first_low = first.AsRegisterPairLow<Register>();
7871 Register first_high = first.AsRegisterPairHigh<Register>();
7872 uint32_t value_low = Low32Bits(value);
7873 uint32_t value_high = High32Bits(value);
7874 if (value_low == 0u) {
7875 if (out_low != first_low) {
7876 __ mov(out_low, ShifterOperand(first_low));
7877 }
7878 __ AddConstant(out_high, first_high, value_high);
7879 return;
7880 }
7881 __ AddConstantSetFlags(out_low, first_low, value_low);
7882 ShifterOperand so;
7883 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
7884 __ adc(out_high, first_high, so);
7885 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
7886 __ sbc(out_high, first_high, so);
7887 } else {
7888 LOG(FATAL) << "Unexpected constant " << value_high;
7889 UNREACHABLE();
7890 }
7891}
7892
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007893void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
7894 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007895 Location first = locations->InAt(0);
7896 Location second = locations->InAt(1);
7897 Location out = locations->Out();
7898
7899 if (second.IsConstant()) {
7900 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7901 uint32_t value_low = Low32Bits(value);
7902 if (instruction->GetResultType() == Primitive::kPrimInt) {
7903 Register first_reg = first.AsRegister<Register>();
7904 Register out_reg = out.AsRegister<Register>();
7905 if (instruction->IsAnd()) {
7906 GenerateAndConst(out_reg, first_reg, value_low);
7907 } else if (instruction->IsOr()) {
7908 GenerateOrrConst(out_reg, first_reg, value_low);
7909 } else {
7910 DCHECK(instruction->IsXor());
7911 GenerateEorConst(out_reg, first_reg, value_low);
7912 }
7913 } else {
7914 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7915 uint32_t value_high = High32Bits(value);
7916 Register first_low = first.AsRegisterPairLow<Register>();
7917 Register first_high = first.AsRegisterPairHigh<Register>();
7918 Register out_low = out.AsRegisterPairLow<Register>();
7919 Register out_high = out.AsRegisterPairHigh<Register>();
7920 if (instruction->IsAnd()) {
7921 GenerateAndConst(out_low, first_low, value_low);
7922 GenerateAndConst(out_high, first_high, value_high);
7923 } else if (instruction->IsOr()) {
7924 GenerateOrrConst(out_low, first_low, value_low);
7925 GenerateOrrConst(out_high, first_high, value_high);
7926 } else {
7927 DCHECK(instruction->IsXor());
7928 GenerateEorConst(out_low, first_low, value_low);
7929 GenerateEorConst(out_high, first_high, value_high);
7930 }
7931 }
7932 return;
7933 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007934
7935 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007936 Register first_reg = first.AsRegister<Register>();
7937 ShifterOperand second_reg(second.AsRegister<Register>());
7938 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007939 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007940 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007941 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007942 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007943 } else {
7944 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007945 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007946 }
7947 } else {
7948 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007949 Register first_low = first.AsRegisterPairLow<Register>();
7950 Register first_high = first.AsRegisterPairHigh<Register>();
7951 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7952 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7953 Register out_low = out.AsRegisterPairLow<Register>();
7954 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007955 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007956 __ and_(out_low, first_low, second_low);
7957 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007958 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007959 __ orr(out_low, first_low, second_low);
7960 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007961 } else {
7962 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007963 __ eor(out_low, first_low, second_low);
7964 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007965 }
7966 }
7967}
7968
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007969void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
7970 HInstruction* instruction,
7971 Location out,
7972 uint32_t offset,
7973 Location maybe_temp,
7974 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007975 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007976 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007977 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007978 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007979 if (kUseBakerReadBarrier) {
7980 // Load with fast path based Baker's read barrier.
7981 // /* HeapReference<Object> */ out = *(out + offset)
7982 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007983 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007984 } else {
7985 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007986 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00007987 // in the following move operation, as we will need it for the
7988 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007989 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00007990 // /* HeapReference<Object> */ out = *(out + offset)
7991 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007992 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00007993 }
7994 } else {
7995 // Plain load with no read barrier.
7996 // /* HeapReference<Object> */ out = *(out + offset)
7997 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7998 __ MaybeUnpoisonHeapReference(out_reg);
7999 }
8000}
8001
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008002void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
8003 HInstruction* instruction,
8004 Location out,
8005 Location obj,
8006 uint32_t offset,
8007 Location maybe_temp,
8008 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008009 Register out_reg = out.AsRegister<Register>();
8010 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008011 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08008012 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00008013 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008014 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00008015 // Load with fast path based Baker's read barrier.
8016 // /* HeapReference<Object> */ out = *(obj + offset)
8017 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008018 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00008019 } else {
8020 // Load with slow path based read barrier.
8021 // /* HeapReference<Object> */ out = *(obj + offset)
8022 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8023 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8024 }
8025 } else {
8026 // Plain load with no read barrier.
8027 // /* HeapReference<Object> */ out = *(obj + offset)
8028 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8029 __ MaybeUnpoisonHeapReference(out_reg);
8030 }
8031}
8032
8033void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
8034 Location root,
8035 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008036 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008037 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008038 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008039 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008040 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00008041 if (kUseBakerReadBarrier) {
8042 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008043 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008044 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8045 !Runtime::Current()->UseJitCompilation()) {
8046 // Note that we do not actually check the value of `GetIsGcMarking()`
8047 // to decide whether to mark the loaded GC root or not. Instead, we
8048 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8049 // barrier mark introspection entrypoint. If `temp` is null, it means
8050 // that `GetIsGcMarking()` is false, and vice versa.
8051 //
8052 // We use link-time generated thunks for the slow path. That thunk
8053 // checks the reference and jumps to the entrypoint if needed.
8054 //
8055 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8056 // lr = &return_address;
8057 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8058 // if (temp != nullptr) {
8059 // goto gc_root_thunk<root_reg>(lr)
8060 // }
8061 // return_address:
Roland Levillainc9285912015-12-18 10:38:42 +00008062
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008063 CheckLastTempIsBakerCcEntrypointRegister(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008064 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008065 uint32_t custom_data =
Vladimir Marko88abba22017-05-03 17:09:25 +01008066 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg, narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008067 Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008068
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008069 // entrypoint_reg =
8070 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8071 DCHECK_EQ(IP, 12);
8072 const int32_t entry_point_offset =
8073 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8074 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008075
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008076 Label return_address;
8077 __ AdrCode(LR, &return_address);
8078 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008079 // Currently the offset is always within range. If that changes,
8080 // we shall have to split the load the same way as for fields.
8081 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008082 DCHECK(!down_cast<Thumb2Assembler*>(GetAssembler())->IsForced32Bit());
8083 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()), !narrow);
8084 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008085 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8086 EmitPlaceholderBne(codegen_, bne_label);
8087 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008088 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8089 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8090 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008091 } else {
8092 // Note that we do not actually check the value of
8093 // `GetIsGcMarking()` to decide whether to mark the loaded GC
8094 // root or not. Instead, we load into `temp` the read barrier
8095 // mark entry point corresponding to register `root`. If `temp`
8096 // is null, it means that `GetIsGcMarking()` is false, and vice
8097 // versa.
8098 //
8099 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8100 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8101 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8102 // // Slow path.
8103 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
8104 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008105
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008106 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
8107 Location temp = Location::RegisterLocation(LR);
8108 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
8109 instruction, root, /* entrypoint */ temp);
8110 codegen_->AddSlowPath(slow_path);
8111
8112 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8113 const int32_t entry_point_offset =
8114 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
8115 // Loading the entrypoint does not require a load acquire since it is only changed when
8116 // threads are suspended or running a checkpoint.
8117 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
8118
8119 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8120 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8121 static_assert(
8122 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8123 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8124 "have different sizes.");
8125 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8126 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8127 "have different sizes.");
8128
8129 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8130 // checking GetIsGcMarking.
8131 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
8132 __ Bind(slow_path->GetExitLabel());
8133 }
Roland Levillainc9285912015-12-18 10:38:42 +00008134 } else {
8135 // GC root loaded through a slow path for read barriers other
8136 // than Baker's.
8137 // /* GcRoot<mirror::Object>* */ root = obj + offset
8138 __ AddConstant(root_reg, obj, offset);
8139 // /* mirror::Object* */ root = root->Read()
8140 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8141 }
8142 } else {
8143 // Plain GC root load with no read barrier.
8144 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8145 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8146 // Note that GC roots are not affected by heap poisoning, thus we
8147 // do not have to unpoison `root_reg` here.
8148 }
8149}
8150
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008151void CodeGeneratorARM::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8152 DCHECK(kEmitCompilerReadBarrier);
8153 DCHECK(kUseBakerReadBarrier);
8154 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8155 if (!Runtime::Current()->UseJitCompilation()) {
8156 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
8157 }
8158 }
8159}
8160
Roland Levillainc9285912015-12-18 10:38:42 +00008161void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8162 Location ref,
8163 Register obj,
8164 uint32_t offset,
8165 Location temp,
8166 bool needs_null_check) {
8167 DCHECK(kEmitCompilerReadBarrier);
8168 DCHECK(kUseBakerReadBarrier);
8169
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008170 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8171 !Runtime::Current()->UseJitCompilation()) {
8172 // Note that we do not actually check the value of `GetIsGcMarking()`
8173 // to decide whether to mark the loaded reference or not. Instead, we
8174 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8175 // barrier mark introspection entrypoint. If `temp` is null, it means
8176 // that `GetIsGcMarking()` is false, and vice versa.
8177 //
8178 // We use link-time generated thunks for the slow path. That thunk checks
8179 // the holder and jumps to the entrypoint if needed. If the holder is not
8180 // gray, it creates a fake dependency and returns to the LDR instruction.
8181 //
8182 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8183 // lr = &gray_return_address;
8184 // if (temp != nullptr) {
8185 // goto field_thunk<holder_reg, base_reg>(lr)
8186 // }
8187 // not_gray_return_address:
8188 // // Original reference load. If the offset is too large to fit
8189 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008190 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008191 // gray_return_address:
8192
8193 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko88abba22017-05-03 17:09:25 +01008194 Register ref_reg = ref.AsRegister<Register>();
8195 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008196 Register base = obj;
8197 if (offset >= kReferenceLoadMinFarOffset) {
8198 base = temp.AsRegister<Register>();
8199 DCHECK_NE(base, kBakerCcEntrypointRegister);
8200 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8201 __ AddConstant(base, obj, offset & ~(kReferenceLoadMinFarOffset - 1u));
8202 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008203 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8204 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8205 // increase the overall code size when taking the generated thunks into account.
8206 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008207 }
8208 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8209 uint32_t custom_data =
Vladimir Marko88abba22017-05-03 17:09:25 +01008210 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(base, obj, narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008211 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8212
8213 // entrypoint_reg =
8214 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8215 DCHECK_EQ(IP, 12);
8216 const int32_t entry_point_offset =
8217 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8218 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8219
8220 Label return_address;
8221 __ AdrCode(LR, &return_address);
8222 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008223 EmitPlaceholderBne(this, bne_label);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008224 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008225 DCHECK(!down_cast<Thumb2Assembler*>(GetAssembler())->IsForced32Bit());
8226 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()), !narrow);
8227 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008228 __ LoadFromOffset(kLoadWord, ref_reg, base, offset);
8229 if (needs_null_check) {
8230 MaybeRecordImplicitNullCheck(instruction);
8231 }
8232 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8233 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008234 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8235 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8236 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008237 return;
8238 }
8239
Roland Levillainc9285912015-12-18 10:38:42 +00008240 // /* HeapReference<Object> */ ref = *(obj + offset)
8241 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01008242 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00008243 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008244 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008245}
8246
8247void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8248 Location ref,
8249 Register obj,
8250 uint32_t data_offset,
8251 Location index,
8252 Location temp,
8253 bool needs_null_check) {
8254 DCHECK(kEmitCompilerReadBarrier);
8255 DCHECK(kUseBakerReadBarrier);
8256
Roland Levillainbfea3352016-06-23 13:48:47 +01008257 static_assert(
8258 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8259 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008260 ScaleFactor scale_factor = TIMES_4;
8261
8262 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8263 !Runtime::Current()->UseJitCompilation()) {
8264 // Note that we do not actually check the value of `GetIsGcMarking()`
8265 // to decide whether to mark the loaded reference or not. Instead, we
8266 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8267 // barrier mark introspection entrypoint. If `temp` is null, it means
8268 // that `GetIsGcMarking()` is false, and vice versa.
8269 //
8270 // We use link-time generated thunks for the slow path. That thunk checks
8271 // the holder and jumps to the entrypoint if needed. If the holder is not
8272 // gray, it creates a fake dependency and returns to the LDR instruction.
8273 //
8274 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8275 // lr = &gray_return_address;
8276 // if (temp != nullptr) {
8277 // goto field_thunk<holder_reg, base_reg>(lr)
8278 // }
8279 // not_gray_return_address:
8280 // // Original reference load. If the offset is too large to fit
8281 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008282 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008283 // gray_return_address:
8284
8285 DCHECK(index.IsValid());
8286 Register index_reg = index.AsRegister<Register>();
8287 Register ref_reg = ref.AsRegister<Register>();
8288 Register data_reg = temp.AsRegister<Register>();
8289 DCHECK_NE(data_reg, kBakerCcEntrypointRegister);
8290
8291 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8292 uint32_t custom_data =
8293 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg);
8294 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8295
8296 // entrypoint_reg =
8297 // Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
8298 DCHECK_EQ(IP, 12);
8299 const int32_t entry_point_offset =
8300 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8301 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8302 __ AddConstant(data_reg, obj, data_offset);
8303
8304 Label return_address;
8305 __ AdrCode(LR, &return_address);
8306 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008307 EmitPlaceholderBne(this, bne_label);
Vladimir Marko88abba22017-05-03 17:09:25 +01008308 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()));
8309 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008310 __ ldr(ref_reg, Address(data_reg, index_reg, LSL, scale_factor));
8311 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8312 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8313 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008314 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8315 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008316 return;
8317 }
8318
Roland Levillainc9285912015-12-18 10:38:42 +00008319 // /* HeapReference<Object> */ ref =
8320 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
8321 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008322 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008323}
8324
8325void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8326 Location ref,
8327 Register obj,
8328 uint32_t offset,
8329 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01008330 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00008331 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008332 bool needs_null_check) {
Roland Levillainc9285912015-12-18 10:38:42 +00008333 DCHECK(kEmitCompilerReadBarrier);
8334 DCHECK(kUseBakerReadBarrier);
8335
Roland Levillain54f869e2017-03-06 13:54:11 +00008336 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8337 // whether we need to enter the slow path to mark the reference.
8338 // Then, in the slow path, check the gray bit in the lock word of
8339 // the reference's holder (`obj`) to decide whether to mark `ref` or
8340 // not.
Roland Levillainc9285912015-12-18 10:38:42 +00008341 //
Roland Levillainba650a42017-03-06 13:52:32 +00008342 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainff487002017-03-07 16:50:01 +00008343 // instead, we load into `temp2` the read barrier mark entry point
8344 // corresponding to register `ref`. If `temp2` is null, it means
8345 // that `GetIsGcMarking()` is false, and vice versa.
8346 //
8347 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8348 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8349 // // Slow path.
8350 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8351 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8352 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8353 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8354 // if (is_gray) {
8355 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8356 // }
8357 // } else {
8358 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8359 // }
8360
8361 Register temp_reg = temp.AsRegister<Register>();
8362
8363 // Slow path marking the object `ref` when the GC is marking. The
8364 // entrypoint will already be loaded in `temp2`.
8365 Location temp2 = Location::RegisterLocation(LR);
8366 SlowPathCodeARM* slow_path =
8367 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM(
8368 instruction,
8369 ref,
8370 obj,
8371 offset,
8372 index,
8373 scale_factor,
8374 needs_null_check,
8375 temp_reg,
8376 /* entrypoint */ temp2);
8377 AddSlowPath(slow_path);
8378
8379 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8380 const int32_t entry_point_offset =
8381 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8382 // Loading the entrypoint does not require a load acquire since it is only changed when
8383 // threads are suspended or running a checkpoint.
8384 __ LoadFromOffset(kLoadWord, temp2.AsRegister<Register>(), TR, entry_point_offset);
8385 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8386 // checking GetIsGcMarking.
8387 __ CompareAndBranchIfNonZero(temp2.AsRegister<Register>(), slow_path->GetEntryLabel());
8388 // Fast path: the GC is not marking: just load the reference.
8389 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8390 __ Bind(slow_path->GetExitLabel());
8391}
8392
8393void CodeGeneratorARM::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8394 Location ref,
8395 Register obj,
8396 Location field_offset,
8397 Location temp,
8398 bool needs_null_check,
8399 Register temp2) {
8400 DCHECK(kEmitCompilerReadBarrier);
8401 DCHECK(kUseBakerReadBarrier);
8402
8403 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8404 // whether we need to enter the slow path to update the reference
8405 // field within `obj`. Then, in the slow path, check the gray bit
8406 // in the lock word of the reference's holder (`obj`) to decide
8407 // whether to mark `ref` and update the field or not.
8408 //
8409 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainba650a42017-03-06 13:52:32 +00008410 // instead, we load into `temp3` the read barrier mark entry point
8411 // corresponding to register `ref`. If `temp3` is null, it means
8412 // that `GetIsGcMarking()` is false, and vice versa.
8413 //
8414 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008415 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8416 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008417 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8418 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8419 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8420 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8421 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008422 // old_ref = ref;
Roland Levillain54f869e2017-03-06 13:54:11 +00008423 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008424 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008425 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008426 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008427
Roland Levillain35345a52017-02-27 14:32:08 +00008428 Register temp_reg = temp.AsRegister<Register>();
Roland Levillain1372c9f2017-01-13 11:47:39 +00008429
Roland Levillainff487002017-03-07 16:50:01 +00008430 // Slow path updating the object reference at address `obj +
8431 // field_offset` when the GC is marking. The entrypoint will already
8432 // be loaded in `temp3`.
Roland Levillainba650a42017-03-06 13:52:32 +00008433 Location temp3 = Location::RegisterLocation(LR);
Roland Levillainff487002017-03-07 16:50:01 +00008434 SlowPathCodeARM* slow_path =
8435 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(
8436 instruction,
8437 ref,
8438 obj,
8439 /* offset */ 0u,
8440 /* index */ field_offset,
8441 /* scale_factor */ ScaleFactor::TIMES_1,
8442 needs_null_check,
8443 temp_reg,
8444 temp2,
8445 /* entrypoint */ temp3);
Roland Levillainba650a42017-03-06 13:52:32 +00008446 AddSlowPath(slow_path);
Roland Levillain35345a52017-02-27 14:32:08 +00008447
Roland Levillainba650a42017-03-06 13:52:32 +00008448 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8449 const int32_t entry_point_offset =
8450 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8451 // Loading the entrypoint does not require a load acquire since it is only changed when
8452 // threads are suspended or running a checkpoint.
8453 __ LoadFromOffset(kLoadWord, temp3.AsRegister<Register>(), TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008454 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8455 // checking GetIsGcMarking.
8456 __ CompareAndBranchIfNonZero(temp3.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008457 // Fast path: the GC is not marking: nothing to do (the field is
8458 // up-to-date, and we don't need to load the reference).
Roland Levillainba650a42017-03-06 13:52:32 +00008459 __ Bind(slow_path->GetExitLabel());
8460}
Roland Levillain35345a52017-02-27 14:32:08 +00008461
Roland Levillainba650a42017-03-06 13:52:32 +00008462void CodeGeneratorARM::GenerateRawReferenceLoad(HInstruction* instruction,
8463 Location ref,
8464 Register obj,
8465 uint32_t offset,
8466 Location index,
8467 ScaleFactor scale_factor,
8468 bool needs_null_check) {
8469 Register ref_reg = ref.AsRegister<Register>();
8470
Roland Levillainc9285912015-12-18 10:38:42 +00008471 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008472 // Load types involving an "index": ArrayGet,
8473 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8474 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008475 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00008476 if (index.IsConstant()) {
8477 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01008478 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00008479 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
8480 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01008481 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008482 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8483 // intrinsics, which use a register pair as index ("long
8484 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01008485 Register index_reg = index.IsRegisterPair()
8486 ? index.AsRegisterPairLow<Register>()
8487 : index.AsRegister<Register>();
8488 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00008489 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
8490 }
8491 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008492 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillainc9285912015-12-18 10:38:42 +00008493 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
8494 }
8495
Roland Levillainba650a42017-03-06 13:52:32 +00008496 if (needs_null_check) {
8497 MaybeRecordImplicitNullCheck(instruction);
8498 }
8499
Roland Levillainc9285912015-12-18 10:38:42 +00008500 // Object* ref = ref_addr->AsMirrorPtr()
8501 __ MaybeUnpoisonHeapReference(ref_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00008502}
8503
8504void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
8505 Location out,
8506 Location ref,
8507 Location obj,
8508 uint32_t offset,
8509 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008510 DCHECK(kEmitCompilerReadBarrier);
8511
Roland Levillainc9285912015-12-18 10:38:42 +00008512 // Insert a slow path based read barrier *after* the reference load.
8513 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008514 // If heap poisoning is enabled, the unpoisoning of the loaded
8515 // reference will be carried out by the runtime within the slow
8516 // path.
8517 //
8518 // Note that `ref` currently does not get unpoisoned (when heap
8519 // poisoning is enabled), which is alright as the `ref` argument is
8520 // not used by the artReadBarrierSlow entry point.
8521 //
8522 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008523 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00008524 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
8525 AddSlowPath(slow_path);
8526
Roland Levillain3b359c72015-11-17 19:35:12 +00008527 __ b(slow_path->GetEntryLabel());
8528 __ Bind(slow_path->GetExitLabel());
8529}
8530
Roland Levillainc9285912015-12-18 10:38:42 +00008531void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8532 Location out,
8533 Location ref,
8534 Location obj,
8535 uint32_t offset,
8536 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008537 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00008538 // Baker's read barriers shall be handled by the fast path
8539 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
8540 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00008541 // If heap poisoning is enabled, unpoisoning will be taken care of
8542 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00008543 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00008544 } else if (kPoisonHeapReferences) {
8545 __ UnpoisonHeapReference(out.AsRegister<Register>());
8546 }
8547}
8548
Roland Levillainc9285912015-12-18 10:38:42 +00008549void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8550 Location out,
8551 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008552 DCHECK(kEmitCompilerReadBarrier);
8553
Roland Levillainc9285912015-12-18 10:38:42 +00008554 // Insert a slow path based read barrier *after* the GC root load.
8555 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008556 // Note that GC roots are not affected by heap poisoning, so we do
8557 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008558 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00008559 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
8560 AddSlowPath(slow_path);
8561
Roland Levillain3b359c72015-11-17 19:35:12 +00008562 __ b(slow_path->GetEntryLabel());
8563 __ Bind(slow_path->GetExitLabel());
8564}
8565
Vladimir Markodc151b22015-10-15 18:02:30 +01008566HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
8567 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008568 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008569 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01008570}
8571
Vladimir Markob4536b72015-11-24 13:45:23 +00008572Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
8573 Register temp) {
8574 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8575 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8576 if (!invoke->GetLocations()->Intrinsified()) {
8577 return location.AsRegister<Register>();
8578 }
8579 // For intrinsics we allow any location, so it may be on the stack.
8580 if (!location.IsRegister()) {
8581 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
8582 return temp;
8583 }
8584 // For register locations, check if the register was saved. If so, get it from the stack.
8585 // Note: There is a chance that the register was saved but not overwritten, so we could
8586 // save one load. However, since this is just an intrinsic slow path we prefer this
8587 // simple and more robust approach rather that trying to determine if that's the case.
8588 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08008589 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00008590 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
8591 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
8592 return temp;
8593 }
8594 return location.AsRegister<Register>();
8595}
8596
TatWai Chongd8c052a2016-11-02 16:12:48 +08008597Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
8598 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00008599 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
8600 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008601 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8602 uint32_t offset =
8603 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00008604 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008605 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00008606 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008607 }
Vladimir Marko58155012015-08-19 12:49:41 +00008608 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00008609 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008610 break;
8611 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8612 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
8613 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00008614 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
8615 HArmDexCacheArraysBase* base =
8616 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
8617 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
8618 temp.AsRegister<Register>());
8619 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
8620 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
8621 break;
8622 }
Vladimir Marko58155012015-08-19 12:49:41 +00008623 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00008624 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008625 Register method_reg;
8626 Register reg = temp.AsRegister<Register>();
8627 if (current_method.IsRegister()) {
8628 method_reg = current_method.AsRegister<Register>();
8629 } else {
8630 DCHECK(invoke->GetLocations()->Intrinsified());
8631 DCHECK(!current_method.IsValid());
8632 method_reg = reg;
8633 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
8634 }
Roland Levillain3b359c72015-11-17 19:35:12 +00008635 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
8636 __ LoadFromOffset(kLoadWord,
8637 reg,
8638 method_reg,
8639 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01008640 // temp = temp[index_in_cache];
8641 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
8642 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00008643 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
8644 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01008645 }
Vladimir Marko58155012015-08-19 12:49:41 +00008646 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08008647 return callee_method;
8648}
8649
8650void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
8651 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00008652
8653 switch (invoke->GetCodePtrLocation()) {
8654 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
8655 __ bl(GetFrameEntryLabel());
8656 break;
Vladimir Marko58155012015-08-19 12:49:41 +00008657 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8658 // LR = callee_method->entry_point_from_quick_compiled_code_
8659 __ LoadFromOffset(
8660 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07008661 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00008662 // LR()
8663 __ blx(LR);
8664 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08008665 }
8666
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08008667 DCHECK(!IsLeafMethod());
8668}
8669
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008670void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
8671 Register temp = temp_location.AsRegister<Register>();
8672 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8673 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00008674
8675 // Use the calling convention instead of the location of the receiver, as
8676 // intrinsics may have put the receiver in a different register. In the intrinsics
8677 // slow path, the arguments have been moved to the right place, so here we are
8678 // guaranteed that the receiver is the first register of the calling convention.
8679 InvokeDexCallingConvention calling_convention;
8680 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008681 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00008682 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00008683 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008684 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00008685 // Instead of simply (possibly) unpoisoning `temp` here, we should
8686 // emit a read barrier for the previous class reference load.
8687 // However this is not required in practice, as this is an
8688 // intermediate/temporary reference and because the current
8689 // concurrent copying collector keeps the from-space memory
8690 // intact/accessible until the end of the marking phase (the
8691 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008692 __ MaybeUnpoisonHeapReference(temp);
8693 // temp = temp->GetMethodAt(method_offset);
8694 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07008695 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008696 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
8697 // LR = temp->GetEntryPoint();
8698 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
8699 // LR();
8700 __ blx(LR);
8701}
8702
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008703CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008704 const DexFile& dex_file, dex::StringIndex string_index) {
8705 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008706}
8707
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008708CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08008709 const DexFile& dex_file, dex::TypeIndex type_index) {
8710 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008711}
8712
Vladimir Marko1998cd02017-01-13 13:02:58 +00008713CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
8714 const DexFile& dex_file, dex::TypeIndex type_index) {
8715 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8716}
8717
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008718CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
8719 const DexFile& dex_file, uint32_t element_offset) {
8720 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
8721}
8722
8723CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
8724 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8725 patches->emplace_back(dex_file, offset_or_index);
8726 return &patches->back();
8727}
8728
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008729Label* CodeGeneratorARM::NewBakerReadBarrierPatch(uint32_t custom_data) {
8730 baker_read_barrier_patches_.emplace_back(custom_data);
8731 return &baker_read_barrier_patches_.back().label;
8732}
8733
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008734Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008735 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008736 return boot_image_string_patches_.GetOrCreate(
8737 StringReference(&dex_file, string_index),
8738 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8739}
8740
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008741Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08008742 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008743 return boot_image_type_patches_.GetOrCreate(
8744 TypeReference(&dex_file, type_index),
8745 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8746}
8747
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008748Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00008749 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008750}
8751
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008752Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008753 dex::StringIndex string_index,
8754 Handle<mirror::String> handle) {
8755 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
8756 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008757 return jit_string_patches_.GetOrCreate(
8758 StringReference(&dex_file, string_index),
8759 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8760}
8761
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008762Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
8763 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008764 Handle<mirror::Class> handle) {
8765 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
8766 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008767 return jit_class_patches_.GetOrCreate(
8768 TypeReference(&dex_file, type_index),
8769 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8770}
8771
Vladimir Markoaad75c62016-10-03 08:46:48 +00008772template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
8773inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
8774 const ArenaDeque<PcRelativePatchInfo>& infos,
8775 ArenaVector<LinkerPatch>* linker_patches) {
8776 for (const PcRelativePatchInfo& info : infos) {
8777 const DexFile& dex_file = info.target_dex_file;
8778 size_t offset_or_index = info.offset_or_index;
8779 DCHECK(info.add_pc_label.IsBound());
8780 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
8781 // Add MOVW patch.
8782 DCHECK(info.movw_label.IsBound());
8783 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
8784 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
8785 // Add MOVT patch.
8786 DCHECK(info.movt_label.IsBound());
8787 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
8788 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
8789 }
8790}
8791
Vladimir Marko58155012015-08-19 12:49:41 +00008792void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
8793 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00008794 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00008795 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008796 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008797 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008798 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008799 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008800 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
8801 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00008802 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008803 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
8804 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008805 for (const auto& entry : boot_image_string_patches_) {
8806 const StringReference& target_string = entry.first;
8807 Literal* literal = entry.second;
8808 DCHECK(literal->GetLabel()->IsBound());
8809 uint32_t literal_offset = literal->GetLabel()->Position();
8810 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
8811 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008812 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008813 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00008814 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00008815 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00008816 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
8817 linker_patches);
8818 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008819 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
8820 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008821 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
8822 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008823 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008824 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
8825 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008826 for (const auto& entry : boot_image_type_patches_) {
8827 const TypeReference& target_type = entry.first;
8828 Literal* literal = entry.second;
8829 DCHECK(literal->GetLabel()->IsBound());
8830 uint32_t literal_offset = literal->GetLabel()->Position();
8831 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
8832 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08008833 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008834 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008835 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
8836 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.Position(),
8837 info.custom_data));
8838 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008839 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008840}
8841
8842Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
8843 return map->GetOrCreate(
8844 value,
8845 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00008846}
8847
8848Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
8849 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008850 return map->GetOrCreate(
8851 target_method,
8852 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00008853}
8854
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03008855void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8856 LocationSummary* locations =
8857 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
8858 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
8859 Location::RequiresRegister());
8860 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
8861 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
8862 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8863}
8864
8865void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8866 LocationSummary* locations = instr->GetLocations();
8867 Register res = locations->Out().AsRegister<Register>();
8868 Register accumulator =
8869 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
8870 Register mul_left =
8871 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
8872 Register mul_right =
8873 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
8874
8875 if (instr->GetOpKind() == HInstruction::kAdd) {
8876 __ mla(res, mul_left, mul_right, accumulator);
8877 } else {
8878 __ mls(res, mul_left, mul_right, accumulator);
8879 }
8880}
8881
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008882void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008883 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008884 LOG(FATAL) << "Unreachable";
8885}
8886
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008887void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008888 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008889 LOG(FATAL) << "Unreachable";
8890}
8891
Mark Mendellfe57faa2015-09-18 09:26:15 -04008892// Simple implementation of packed switch - generate cascaded compare/jumps.
8893void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8894 LocationSummary* locations =
8895 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8896 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008897 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008898 codegen_->GetAssembler()->IsThumb()) {
8899 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
8900 if (switch_instr->GetStartValue() != 0) {
8901 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
8902 }
8903 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008904}
8905
8906void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8907 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008908 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04008909 LocationSummary* locations = switch_instr->GetLocations();
8910 Register value_reg = locations->InAt(0).AsRegister<Register>();
8911 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8912
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008913 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008914 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008915 Register temp_reg = IP;
8916 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
8917 // the immediate, because IP is used as the destination register. For the other
8918 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
8919 // and they can be encoded in the instruction without making use of IP register.
8920 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
8921
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008922 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008923 // Jump to successors[0] if value == lower_bound.
8924 __ b(codegen_->GetLabelOf(successors[0]), EQ);
8925 int32_t last_index = 0;
8926 for (; num_entries - last_index > 2; last_index += 2) {
8927 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
8928 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8929 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
8930 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8931 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
8932 }
8933 if (num_entries - last_index == 2) {
8934 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00008935 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008936 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008937 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008938
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008939 // And the default for any other value.
8940 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
8941 __ b(codegen_->GetLabelOf(default_block));
8942 }
8943 } else {
8944 // Create a table lookup.
8945 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8946
8947 // Materialize a pointer to the switch table
8948 std::vector<Label*> labels(num_entries);
8949 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
8950 for (uint32_t i = 0; i < num_entries; i++) {
8951 labels[i] = codegen_->GetLabelOf(successors[i]);
8952 }
8953 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
8954
8955 // Remove the bias.
8956 Register key_reg;
8957 if (lower_bound != 0) {
8958 key_reg = locations->GetTemp(1).AsRegister<Register>();
8959 __ AddConstant(key_reg, value_reg, -lower_bound);
8960 } else {
8961 key_reg = value_reg;
8962 }
8963
8964 // Check whether the value is in the table, jump to default block if not.
8965 __ CmpConstant(key_reg, num_entries - 1);
8966 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
8967
8968 // Load the displacement from the table.
8969 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
8970
8971 // Dispatch is a direct add to the PC (for Thumb2).
8972 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008973 }
8974}
8975
Vladimir Markob4536b72015-11-24 13:45:23 +00008976void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8977 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8978 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00008979}
8980
8981void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8982 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008983 CodeGeneratorARM::PcRelativePatchInfo* labels =
8984 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00008985 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008986 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008987 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008988 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008989 __ BindTrackedLabel(&labels->add_pc_label);
8990 __ add(base_reg, base_reg, ShifterOperand(PC));
8991}
8992
Andreas Gampe85b62f22015-09-09 13:15:38 -07008993void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
8994 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00008995 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008996 return;
8997 }
8998
8999 DCHECK_NE(type, Primitive::kPrimVoid);
9000
9001 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
9002 if (return_loc.Equals(trg)) {
9003 return;
9004 }
9005
9006 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9007 // with the last branch.
9008 if (type == Primitive::kPrimLong) {
9009 HParallelMove parallel_move(GetGraph()->GetArena());
9010 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
9011 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
9012 GetMoveResolver()->EmitNativeCode(&parallel_move);
9013 } else if (type == Primitive::kPrimDouble) {
9014 HParallelMove parallel_move(GetGraph()->GetArena());
9015 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
9016 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
9017 GetMoveResolver()->EmitNativeCode(&parallel_move);
9018 } else {
9019 // Let the parallel move resolver take care of all of this.
9020 HParallelMove parallel_move(GetGraph()->GetArena());
9021 parallel_move.AddMove(return_loc, trg, type, nullptr);
9022 GetMoveResolver()->EmitNativeCode(&parallel_move);
9023 }
9024}
9025
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009026void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
9027 LocationSummary* locations =
9028 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9029 locations->SetInAt(0, Location::RequiresRegister());
9030 locations->SetOut(Location::RequiresRegister());
9031}
9032
9033void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
9034 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009035 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009036 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009037 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009038 __ LoadFromOffset(kLoadWord,
9039 locations->Out().AsRegister<Register>(),
9040 locations->InAt(0).AsRegister<Register>(),
9041 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009042 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009043 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009044 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009045 __ LoadFromOffset(kLoadWord,
9046 locations->Out().AsRegister<Register>(),
9047 locations->InAt(0).AsRegister<Register>(),
9048 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9049 __ LoadFromOffset(kLoadWord,
9050 locations->Out().AsRegister<Register>(),
9051 locations->Out().AsRegister<Register>(),
9052 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009053 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009054}
9055
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009056static void PatchJitRootUse(uint8_t* code,
9057 const uint8_t* roots_data,
9058 Literal* literal,
9059 uint64_t index_in_table) {
9060 DCHECK(literal->GetLabel()->IsBound());
9061 uint32_t literal_offset = literal->GetLabel()->Position();
9062 uintptr_t address =
9063 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9064 uint8_t* data = code + literal_offset;
9065 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9066}
9067
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009068void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9069 for (const auto& entry : jit_string_patches_) {
9070 const auto& it = jit_string_roots_.find(entry.first);
9071 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009072 PatchJitRootUse(code, roots_data, entry.second, it->second);
9073 }
9074 for (const auto& entry : jit_class_patches_) {
9075 const auto& it = jit_class_roots_.find(entry.first);
9076 DCHECK(it != jit_class_roots_.end());
9077 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009078 }
9079}
9080
Roland Levillain4d027112015-07-01 15:41:14 +01009081#undef __
9082#undef QUICK_ENTRY_POINT
9083
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00009084} // namespace arm
9085} // namespace art