blob: 713d370c8797af4dcdf45fb30ee42d9f3b6ef95d [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)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002012 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002013 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00002014 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01002015 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00002016 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00002017 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2018 jit_class_patches_(TypeReferenceValueComparator(),
2019 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07002020 // Always save the LR register to mimic Quick.
2021 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01002022}
2023
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002024void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
2025 // Ensure that we fix up branches and literal loads and emit the literal pool.
2026 __ FinalizeCode();
2027
2028 // Adjust native pc offsets in stack maps.
2029 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08002030 uint32_t old_position =
2031 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002032 uint32_t new_position = __ GetAdjustedPosition(old_position);
2033 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
2034 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01002035 // Adjust pc offsets for the disassembly information.
2036 if (disasm_info_ != nullptr) {
2037 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
2038 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
2039 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
2040 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
2041 it.second.start = __ GetAdjustedPosition(it.second.start);
2042 it.second.end = __ GetAdjustedPosition(it.second.end);
2043 }
2044 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
2045 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
2046 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
2047 }
2048 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002049
2050 CodeGenerator::Finalize(allocator);
2051}
2052
David Brazdil58282f42016-01-14 12:45:10 +00002053void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002054 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002055 blocked_core_registers_[SP] = true;
2056 blocked_core_registers_[LR] = true;
2057 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002058
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002059 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002060 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002061
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002062 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002063 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002064
David Brazdil58282f42016-01-14 12:45:10 +00002065 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01002066 // Stubs do not save callee-save floating point registers. If the graph
2067 // is debuggable, we need to deal with these registers differently. For
2068 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002069 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
2070 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
2071 }
2072 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002073}
2074
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002075InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002076 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002077 assembler_(codegen->GetAssembler()),
2078 codegen_(codegen) {}
2079
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002080void CodeGeneratorARM::ComputeSpillMask() {
2081 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2082 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00002083 // There is no easy instruction to restore just the PC on thumb2. We spill and
2084 // restore another arbitrary register.
2085 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002086 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2087 // We use vpush and vpop for saving and restoring floating point registers, which take
2088 // a SRegister and the number of registers to save/restore after that SRegister. We
2089 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2090 // but in the range.
2091 if (fpu_spill_mask_ != 0) {
2092 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2093 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2094 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2095 fpu_spill_mask_ |= (1 << i);
2096 }
2097 }
2098}
2099
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002100static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002101 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002102}
2103
2104static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002105 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002106}
2107
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002108void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00002109 bool skip_overflow_check =
2110 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002111 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002112 __ Bind(&frame_entry_label_);
2113
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002114 if (HasEmptyFrame()) {
2115 return;
2116 }
2117
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002118 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002119 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
2120 __ LoadFromOffset(kLoadWord, IP, IP, 0);
2121 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002122 }
2123
Andreas Gampe501fd632015-09-10 16:11:06 -07002124 __ PushList(core_spill_mask_);
2125 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2126 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002127 if (fpu_spill_mask_ != 0) {
2128 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2129 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002130 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01002131 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002132 }
Mingyao Yang063fc772016-08-02 11:02:54 -07002133
2134 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2135 // Initialize should_deoptimize flag to 0.
2136 __ mov(IP, ShifterOperand(0));
2137 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
2138 }
2139
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002140 int adjust = GetFrameSize() - FrameEntrySpillSize();
2141 __ AddConstant(SP, -adjust);
2142 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002143
2144 // Save the current method if we need it. Note that we do not
2145 // do this in HCurrentMethod, as the instruction might have been removed
2146 // in the SSA graph.
2147 if (RequiresCurrentMethod()) {
2148 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
2149 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002150}
2151
2152void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002153 if (HasEmptyFrame()) {
2154 __ bx(LR);
2155 return;
2156 }
David Srbeckyc34dc932015-04-12 09:27:43 +01002157 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002158 int adjust = GetFrameSize() - FrameEntrySpillSize();
2159 __ AddConstant(SP, adjust);
2160 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002161 if (fpu_spill_mask_ != 0) {
2162 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2163 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07002164 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002165 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002166 }
Andreas Gampe501fd632015-09-10 16:11:06 -07002167 // Pop LR into PC to return.
2168 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
2169 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
2170 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01002171 __ cfi().RestoreState();
2172 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002173}
2174
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002175void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07002176 Label* label = GetLabelOf(block);
2177 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002178}
2179
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002180Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002181 switch (type) {
2182 case Primitive::kPrimBoolean:
2183 case Primitive::kPrimByte:
2184 case Primitive::kPrimChar:
2185 case Primitive::kPrimShort:
2186 case Primitive::kPrimInt:
2187 case Primitive::kPrimNot: {
2188 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002189 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002190 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002191 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002192 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002193 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002194 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002195 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002196
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002197 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002198 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002199 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002200 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002201 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002202 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002203 if (calling_convention.GetRegisterAt(index) == R1) {
2204 // Skip R1, and use R2_R3 instead.
2205 gp_index_++;
2206 index++;
2207 }
2208 }
2209 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2210 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002211 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01002212
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002213 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002214 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002215 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002216 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2217 }
2218 }
2219
2220 case Primitive::kPrimFloat: {
2221 uint32_t stack_index = stack_index_++;
2222 if (float_index_ % 2 == 0) {
2223 float_index_ = std::max(double_index_, float_index_);
2224 }
2225 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2226 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
2227 } else {
2228 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2229 }
2230 }
2231
2232 case Primitive::kPrimDouble: {
2233 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2234 uint32_t stack_index = stack_index_;
2235 stack_index_ += 2;
2236 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2237 uint32_t index = double_index_;
2238 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002239 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002240 calling_convention.GetFpuRegisterAt(index),
2241 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002242 DCHECK(ExpectedPairLayout(result));
2243 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002244 } else {
2245 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002246 }
2247 }
2248
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002249 case Primitive::kPrimVoid:
2250 LOG(FATAL) << "Unexpected parameter type " << type;
2251 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002252 }
Roland Levillain3b359c72015-11-17 19:35:12 +00002253 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002254}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002255
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002256Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002257 switch (type) {
2258 case Primitive::kPrimBoolean:
2259 case Primitive::kPrimByte:
2260 case Primitive::kPrimChar:
2261 case Primitive::kPrimShort:
2262 case Primitive::kPrimInt:
2263 case Primitive::kPrimNot: {
2264 return Location::RegisterLocation(R0);
2265 }
2266
2267 case Primitive::kPrimFloat: {
2268 return Location::FpuRegisterLocation(S0);
2269 }
2270
2271 case Primitive::kPrimLong: {
2272 return Location::RegisterPairLocation(R0, R1);
2273 }
2274
2275 case Primitive::kPrimDouble: {
2276 return Location::FpuRegisterPairLocation(S0, S1);
2277 }
2278
2279 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00002280 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002281 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002282
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002283 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002284}
2285
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002286Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
2287 return Location::RegisterLocation(kMethodRegisterArgument);
2288}
2289
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002290void CodeGeneratorARM::Move32(Location destination, Location source) {
2291 if (source.Equals(destination)) {
2292 return;
2293 }
2294 if (destination.IsRegister()) {
2295 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002296 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002297 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002298 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002299 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002300 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002301 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002302 } else if (destination.IsFpuRegister()) {
2303 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002304 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002305 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002306 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002307 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002308 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002309 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002310 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002311 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002312 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002313 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002314 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002315 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002316 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002317 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002318 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
2319 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002320 }
2321 }
2322}
2323
2324void CodeGeneratorARM::Move64(Location destination, Location source) {
2325 if (source.Equals(destination)) {
2326 return;
2327 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002328 if (destination.IsRegisterPair()) {
2329 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002330 EmitParallelMoves(
2331 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
2332 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002333 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002334 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002335 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
2336 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002337 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002338 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002339 } else if (source.IsFpuRegisterPair()) {
2340 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
2341 destination.AsRegisterPairHigh<Register>(),
2342 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002343 } else {
2344 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002345 DCHECK(ExpectedPairLayout(destination));
2346 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
2347 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002348 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002349 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002350 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002351 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2352 SP,
2353 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01002354 } else if (source.IsRegisterPair()) {
2355 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2356 source.AsRegisterPairLow<Register>(),
2357 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002358 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002359 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002360 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002361 } else {
2362 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002363 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002364 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002365 if (source.AsRegisterPairLow<Register>() == R1) {
2366 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002367 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
2368 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002369 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002370 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002371 SP, destination.GetStackIndex());
2372 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002373 } else if (source.IsFpuRegisterPair()) {
2374 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
2375 SP,
2376 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002377 } else {
2378 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002379 EmitParallelMoves(
2380 Location::StackSlot(source.GetStackIndex()),
2381 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002382 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002383 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002384 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
2385 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002386 }
2387 }
2388}
2389
Calin Juravle175dc732015-08-25 15:42:32 +01002390void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
2391 DCHECK(location.IsRegister());
2392 __ LoadImmediate(location.AsRegister<Register>(), value);
2393}
2394
Calin Juravlee460d1d2015-09-29 04:52:17 +01002395void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002396 HParallelMove move(GetGraph()->GetArena());
2397 move.AddMove(src, dst, dst_type, nullptr);
2398 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002399}
2400
2401void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
2402 if (location.IsRegister()) {
2403 locations->AddTemp(location);
2404 } else if (location.IsRegisterPair()) {
2405 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
2406 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
2407 } else {
2408 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2409 }
2410}
2411
Calin Juravle175dc732015-08-25 15:42:32 +01002412void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
2413 HInstruction* instruction,
2414 uint32_t dex_pc,
2415 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002416 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002417 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002418 if (EntrypointRequiresStackMap(entrypoint)) {
2419 RecordPcInfo(instruction, dex_pc, slow_path);
2420 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002421}
2422
Roland Levillaindec8f632016-07-22 17:10:06 +01002423void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2424 HInstruction* instruction,
2425 SlowPathCode* slow_path) {
2426 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002427 GenerateInvokeRuntime(entry_point_offset);
2428}
2429
2430void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01002431 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2432 __ blx(LR);
2433}
2434
David Brazdilfc6a86a2015-06-26 10:33:45 +00002435void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002436 DCHECK(!successor->IsExitBlock());
2437
2438 HBasicBlock* block = got->GetBlock();
2439 HInstruction* previous = got->GetPrevious();
2440
2441 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00002442 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002443 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2444 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2445 return;
2446 }
2447
2448 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2449 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2450 }
2451 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002452 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002453 }
2454}
2455
David Brazdilfc6a86a2015-06-26 10:33:45 +00002456void LocationsBuilderARM::VisitGoto(HGoto* got) {
2457 got->SetLocations(nullptr);
2458}
2459
2460void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
2461 HandleGoto(got, got->GetSuccessor());
2462}
2463
2464void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2465 try_boundary->SetLocations(nullptr);
2466}
2467
2468void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2469 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2470 if (!successor->IsExitBlock()) {
2471 HandleGoto(try_boundary, successor);
2472 }
2473}
2474
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002475void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002476 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002477}
2478
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002479void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002480}
2481
Nicolas Geoffray30826612017-05-10 11:59:26 +00002482void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
2483 Label* true_label,
2484 Label* false_label) {
2485 LocationSummary* locations = cond->GetLocations();
2486 Location left = locations->InAt(0);
2487 Location right = locations->InAt(1);
2488 IfCondition if_cond = cond->GetCondition();
2489
2490 Register left_high = left.AsRegisterPairHigh<Register>();
2491 Register left_low = left.AsRegisterPairLow<Register>();
2492 IfCondition true_high_cond = if_cond;
2493 IfCondition false_high_cond = cond->GetOppositeCondition();
2494 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
2495
2496 // Set the conditions for the test, remembering that == needs to be
2497 // decided using the low words.
2498 switch (if_cond) {
2499 case kCondEQ:
2500 case kCondNE:
2501 // Nothing to do.
2502 break;
2503 case kCondLT:
2504 false_high_cond = kCondGT;
2505 break;
2506 case kCondLE:
2507 true_high_cond = kCondLT;
2508 break;
2509 case kCondGT:
2510 false_high_cond = kCondLT;
2511 break;
2512 case kCondGE:
2513 true_high_cond = kCondGT;
2514 break;
2515 case kCondB:
2516 false_high_cond = kCondA;
2517 break;
2518 case kCondBE:
2519 true_high_cond = kCondB;
2520 break;
2521 case kCondA:
2522 false_high_cond = kCondB;
2523 break;
2524 case kCondAE:
2525 true_high_cond = kCondA;
2526 break;
2527 }
2528 if (right.IsConstant()) {
2529 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
2530 int32_t val_low = Low32Bits(value);
2531 int32_t val_high = High32Bits(value);
2532
2533 __ CmpConstant(left_high, val_high);
2534 if (if_cond == kCondNE) {
2535 __ b(true_label, ARMCondition(true_high_cond));
2536 } else if (if_cond == kCondEQ) {
2537 __ b(false_label, ARMCondition(false_high_cond));
2538 } else {
2539 __ b(true_label, ARMCondition(true_high_cond));
2540 __ b(false_label, ARMCondition(false_high_cond));
2541 }
2542 // Must be equal high, so compare the lows.
2543 __ CmpConstant(left_low, val_low);
2544 } else {
2545 Register right_high = right.AsRegisterPairHigh<Register>();
2546 Register right_low = right.AsRegisterPairLow<Register>();
2547
2548 __ cmp(left_high, ShifterOperand(right_high));
2549 if (if_cond == kCondNE) {
2550 __ b(true_label, ARMCondition(true_high_cond));
2551 } else if (if_cond == kCondEQ) {
2552 __ b(false_label, ARMCondition(false_high_cond));
2553 } else {
2554 __ b(true_label, ARMCondition(true_high_cond));
2555 __ b(false_label, ARMCondition(false_high_cond));
2556 }
2557 // Must be equal high, so compare the lows.
2558 __ cmp(left_low, ShifterOperand(right_low));
2559 }
2560 // The last comparison might be unsigned.
2561 // TODO: optimize cases where this is always true/false
2562 __ b(true_label, final_condition);
2563}
2564
David Brazdil0debae72015-11-12 18:37:00 +00002565void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
2566 Label* true_target_in,
2567 Label* false_target_in) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002568 if (CanGenerateTest(condition, codegen_->GetAssembler())) {
2569 Label* non_fallthrough_target;
2570 bool invert;
2571
2572 if (true_target_in == nullptr) {
2573 DCHECK(false_target_in != nullptr);
2574 non_fallthrough_target = false_target_in;
2575 invert = true;
2576 } else {
2577 non_fallthrough_target = true_target_in;
2578 invert = false;
2579 }
2580
2581 const auto cond = GenerateTest(condition, invert, codegen_);
2582
2583 __ b(non_fallthrough_target, cond.first);
2584
2585 if (false_target_in != nullptr && false_target_in != non_fallthrough_target) {
2586 __ b(false_target_in);
2587 }
2588
2589 return;
2590 }
2591
David Brazdil0debae72015-11-12 18:37:00 +00002592 // Generated branching requires both targets to be explicit. If either of the
2593 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2594 Label fallthrough_target;
2595 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
2596 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
2597
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002598 DCHECK_EQ(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
Nicolas Geoffray30826612017-05-10 11:59:26 +00002599 GenerateLongComparesAndJumps(condition, true_target, false_target);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002600
David Brazdil0debae72015-11-12 18:37:00 +00002601 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002602 __ b(false_target);
2603 }
David Brazdil0debae72015-11-12 18:37:00 +00002604
2605 if (fallthrough_target.IsLinked()) {
2606 __ Bind(&fallthrough_target);
2607 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002608}
2609
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002610void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002611 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002612 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002613 Label* false_target) {
2614 HInstruction* cond = instruction->InputAt(condition_input_index);
2615
2616 if (true_target == nullptr && false_target == nullptr) {
2617 // Nothing to do. The code always falls through.
2618 return;
2619 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002620 // Constant condition, statically compared against "true" (integer value 1).
2621 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002622 if (true_target != nullptr) {
2623 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002624 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002625 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002626 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002627 if (false_target != nullptr) {
2628 __ b(false_target);
2629 }
2630 }
2631 return;
2632 }
2633
2634 // The following code generates these patterns:
2635 // (1) true_target == nullptr && false_target != nullptr
2636 // - opposite condition true => branch to false_target
2637 // (2) true_target != nullptr && false_target == nullptr
2638 // - condition true => branch to true_target
2639 // (3) true_target != nullptr && false_target != nullptr
2640 // - condition true => branch to true_target
2641 // - branch to false_target
2642 if (IsBooleanValueOrMaterializedCondition(cond)) {
2643 // Condition has been materialized, compare the output to 0.
2644 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2645 DCHECK(cond_val.IsRegister());
2646 if (true_target == nullptr) {
2647 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
2648 } else {
2649 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002650 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002651 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002652 // Condition has not been materialized. Use its inputs as the comparison and
2653 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002654 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002655
2656 // If this is a long or FP comparison that has been folded into
2657 // the HCondition, generate the comparison directly.
2658 Primitive::Type type = condition->InputAt(0)->GetType();
2659 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2660 GenerateCompareTestAndBranch(condition, true_target, false_target);
2661 return;
2662 }
2663
Donghui Bai426b49c2016-11-08 14:55:38 +08002664 Label* non_fallthrough_target;
2665 Condition arm_cond;
David Brazdil0debae72015-11-12 18:37:00 +00002666 LocationSummary* locations = cond->GetLocations();
2667 DCHECK(locations->InAt(0).IsRegister());
2668 Register left = locations->InAt(0).AsRegister<Register>();
2669 Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08002670
David Brazdil0debae72015-11-12 18:37:00 +00002671 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002672 arm_cond = ARMCondition(condition->GetOppositeCondition());
2673 non_fallthrough_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002674 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002675 arm_cond = ARMCondition(condition->GetCondition());
2676 non_fallthrough_target = true_target;
2677 }
2678
2679 if (right.IsConstant() && (arm_cond == NE || arm_cond == EQ) &&
2680 CodeGenerator::GetInt32ValueOf(right.GetConstant()) == 0) {
2681 if (arm_cond == EQ) {
2682 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2683 } else {
2684 DCHECK_EQ(arm_cond, NE);
2685 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2686 }
2687 } else {
2688 if (right.IsRegister()) {
2689 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
2690 } else {
2691 DCHECK(right.IsConstant());
2692 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2693 }
2694
2695 __ b(non_fallthrough_target, arm_cond);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002696 }
Dave Allison20dfc792014-06-16 20:44:29 -07002697 }
David Brazdil0debae72015-11-12 18:37:00 +00002698
2699 // If neither branch falls through (case 3), the conditional branch to `true_target`
2700 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2701 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002702 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002703 }
2704}
2705
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002706void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002707 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2708 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002709 locations->SetInAt(0, Location::RequiresRegister());
2710 }
2711}
2712
2713void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002714 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2715 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2716 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2717 nullptr : codegen_->GetLabelOf(true_successor);
2718 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2719 nullptr : codegen_->GetLabelOf(false_successor);
2720 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002721}
2722
2723void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
2724 LocationSummary* locations = new (GetGraph()->GetArena())
2725 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01002726 InvokeRuntimeCallingConvention calling_convention;
2727 RegisterSet caller_saves = RegisterSet::Empty();
2728 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2729 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00002730 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002731 locations->SetInAt(0, Location::RequiresRegister());
2732 }
2733}
2734
2735void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01002736 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002737 GenerateTestAndBranch(deoptimize,
2738 /* condition_input_index */ 0,
2739 slow_path->GetEntryLabel(),
2740 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002741}
Dave Allison20dfc792014-06-16 20:44:29 -07002742
Mingyao Yang063fc772016-08-02 11:02:54 -07002743void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2744 LocationSummary* locations = new (GetGraph()->GetArena())
2745 LocationSummary(flag, LocationSummary::kNoCall);
2746 locations->SetOut(Location::RequiresRegister());
2747}
2748
2749void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2750 __ LoadFromOffset(kLoadWord,
2751 flag->GetLocations()->Out().AsRegister<Register>(),
2752 SP,
2753 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2754}
2755
David Brazdil74eb1b22015-12-14 11:44:01 +00002756void LocationsBuilderARM::VisitSelect(HSelect* select) {
2757 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08002758 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
2759
2760 if (is_floating_point) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002761 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002762 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002763 } else {
2764 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002765 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002766 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002767
David Brazdil74eb1b22015-12-14 11:44:01 +00002768 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002769 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2770 // The code generator handles overlap with the values, but not with the condition.
2771 locations->SetOut(Location::SameAsFirstInput());
2772 } else if (is_floating_point) {
2773 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2774 } else {
2775 if (!locations->InAt(1).IsConstant()) {
2776 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2777 }
2778
2779 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00002780 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002781}
2782
2783void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002784 HInstruction* const condition = select->GetCondition();
2785 const LocationSummary* const locations = select->GetLocations();
2786 const Primitive::Type type = select->GetType();
2787 const Location first = locations->InAt(0);
2788 const Location out = locations->Out();
2789 const Location second = locations->InAt(1);
2790 Location src;
2791
2792 if (condition->IsIntConstant()) {
2793 if (condition->AsIntConstant()->IsFalse()) {
2794 src = first;
2795 } else {
2796 src = second;
2797 }
2798
2799 codegen_->MoveLocation(out, src, type);
2800 return;
2801 }
2802
2803 if (!Primitive::IsFloatingPointType(type) &&
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002804 (IsBooleanValueOrMaterializedCondition(condition) ||
2805 CanGenerateTest(condition->AsCondition(), codegen_->GetAssembler()))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002806 bool invert = false;
2807
2808 if (out.Equals(second)) {
2809 src = first;
2810 invert = true;
2811 } else if (out.Equals(first)) {
2812 src = second;
2813 } else if (second.IsConstant()) {
2814 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2815 src = second;
2816 } else if (first.IsConstant()) {
2817 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2818 src = first;
2819 invert = true;
2820 } else {
2821 src = second;
2822 }
2823
2824 if (CanGenerateConditionalMove(out, src)) {
2825 if (!out.Equals(first) && !out.Equals(second)) {
2826 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2827 }
2828
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002829 std::pair<Condition, Condition> cond;
2830
2831 if (IsBooleanValueOrMaterializedCondition(condition)) {
2832 __ CmpConstant(locations->InAt(2).AsRegister<Register>(), 0);
2833 cond = invert ? std::make_pair(EQ, NE) : std::make_pair(NE, EQ);
2834 } else {
2835 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2836 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002837
2838 if (out.IsRegister()) {
2839 ShifterOperand operand;
2840
2841 if (src.IsConstant()) {
2842 operand = ShifterOperand(CodeGenerator::GetInt32ValueOf(src.GetConstant()));
2843 } else {
2844 DCHECK(src.IsRegister());
2845 operand = ShifterOperand(src.AsRegister<Register>());
2846 }
2847
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002848 __ it(cond.first);
2849 __ mov(out.AsRegister<Register>(), operand, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08002850 } else {
2851 DCHECK(out.IsRegisterPair());
2852
2853 ShifterOperand operand_high;
2854 ShifterOperand operand_low;
2855
2856 if (src.IsConstant()) {
2857 const int64_t value = src.GetConstant()->AsLongConstant()->GetValue();
2858
2859 operand_high = ShifterOperand(High32Bits(value));
2860 operand_low = ShifterOperand(Low32Bits(value));
2861 } else {
2862 DCHECK(src.IsRegisterPair());
2863 operand_high = ShifterOperand(src.AsRegisterPairHigh<Register>());
2864 operand_low = ShifterOperand(src.AsRegisterPairLow<Register>());
2865 }
2866
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002867 __ it(cond.first);
2868 __ mov(out.AsRegisterPairLow<Register>(), operand_low, cond.first);
2869 __ it(cond.first);
2870 __ mov(out.AsRegisterPairHigh<Register>(), operand_high, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08002871 }
2872
2873 return;
2874 }
2875 }
2876
2877 Label* false_target = nullptr;
2878 Label* true_target = nullptr;
2879 Label select_end;
2880 Label* target = codegen_->GetFinalLabel(select, &select_end);
2881
2882 if (out.Equals(second)) {
2883 true_target = target;
2884 src = first;
2885 } else {
2886 false_target = target;
2887 src = second;
2888
2889 if (!out.Equals(first)) {
2890 codegen_->MoveLocation(out, first, type);
2891 }
2892 }
2893
2894 GenerateTestAndBranch(select, 2, true_target, false_target);
2895 codegen_->MoveLocation(out, src, type);
2896
2897 if (select_end.IsLinked()) {
2898 __ Bind(&select_end);
2899 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002900}
2901
David Srbecky0cf44932015-12-09 14:09:59 +00002902void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2903 new (GetGraph()->GetArena()) LocationSummary(info);
2904}
2905
David Srbeckyd28f4a02016-03-14 17:14:24 +00002906void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2907 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002908}
2909
2910void CodeGeneratorARM::GenerateNop() {
2911 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002912}
2913
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002914void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002915 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002916 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002917 // Handle the long/FP comparisons made in instruction simplification.
2918 switch (cond->InputAt(0)->GetType()) {
2919 case Primitive::kPrimLong:
2920 locations->SetInAt(0, Location::RequiresRegister());
2921 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002922 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002923 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002924 }
2925 break;
2926
2927 case Primitive::kPrimFloat:
2928 case Primitive::kPrimDouble:
2929 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002930 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002931 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002932 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2933 }
2934 break;
2935
2936 default:
2937 locations->SetInAt(0, Location::RequiresRegister());
2938 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002939 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002940 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2941 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002942 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002943}
2944
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002945void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002946 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002947 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002948 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002949
Nicolas Geoffray30826612017-05-10 11:59:26 +00002950 const Register out = cond->GetLocations()->Out().AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002951
Nicolas Geoffray30826612017-05-10 11:59:26 +00002952 if (ArmAssembler::IsLowRegister(out) && CanGenerateTest(cond, codegen_->GetAssembler())) {
2953 const auto condition = GenerateTest(cond, false, codegen_);
2954
2955 __ it(condition.first);
2956 __ mov(out, ShifterOperand(1), condition.first);
2957 __ it(condition.second);
2958 __ mov(out, ShifterOperand(0), condition.second);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002959 return;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002960 }
2961
Nicolas Geoffray30826612017-05-10 11:59:26 +00002962 // Convert the jumps into the result.
2963 Label done_label;
2964 Label* const final_label = codegen_->GetFinalLabel(cond, &done_label);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002965
Nicolas Geoffray30826612017-05-10 11:59:26 +00002966 if (cond->InputAt(0)->GetType() == Primitive::kPrimLong) {
2967 Label true_label, false_label;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002968
Nicolas Geoffray30826612017-05-10 11:59:26 +00002969 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002970
Nicolas Geoffray30826612017-05-10 11:59:26 +00002971 // False case: result = 0.
2972 __ Bind(&false_label);
2973 __ LoadImmediate(out, 0);
2974 __ b(final_label);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002975
Nicolas Geoffray30826612017-05-10 11:59:26 +00002976 // True case: result = 1.
2977 __ Bind(&true_label);
2978 __ LoadImmediate(out, 1);
2979 } else {
2980 DCHECK(CanGenerateTest(cond, codegen_->GetAssembler()));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002981
Nicolas Geoffray30826612017-05-10 11:59:26 +00002982 const auto condition = GenerateTest(cond, false, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002983
Nicolas Geoffray30826612017-05-10 11:59:26 +00002984 __ mov(out, ShifterOperand(0), AL, kCcKeep);
2985 __ b(final_label, condition.second);
2986 __ LoadImmediate(out, 1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002987 }
Anton Kirilov6f644202017-02-27 18:29:45 +00002988
Nicolas Geoffray30826612017-05-10 11:59:26 +00002989 if (done_label.IsLinked()) {
2990 __ Bind(&done_label);
2991 }
Dave Allison20dfc792014-06-16 20:44:29 -07002992}
2993
2994void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002995 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002996}
2997
2998void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002999 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003000}
3001
3002void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003003 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003004}
3005
3006void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003007 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003008}
3009
3010void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003011 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003012}
3013
3014void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003015 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003016}
3017
3018void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003019 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003020}
3021
3022void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003023 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003024}
3025
3026void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003027 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003028}
3029
3030void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003031 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003032}
3033
3034void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003035 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003036}
3037
3038void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003039 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003040}
3041
Aart Bike9f37602015-10-09 11:15:55 -07003042void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003043 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003044}
3045
3046void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003047 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003048}
3049
3050void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003051 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003052}
3053
3054void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003055 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003056}
3057
3058void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003059 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003060}
3061
3062void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003063 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003064}
3065
3066void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003067 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003068}
3069
3070void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003071 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003072}
3073
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003074void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003075 LocationSummary* locations =
3076 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003077 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003078}
3079
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003080void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01003081 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003082}
3083
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003084void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
3085 LocationSummary* locations =
3086 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3087 locations->SetOut(Location::ConstantLocation(constant));
3088}
3089
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003090void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003091 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003092}
3093
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003094void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003095 LocationSummary* locations =
3096 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003097 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003098}
3099
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003100void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003101 // Will be generated at use site.
3102}
3103
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003104void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
3105 LocationSummary* locations =
3106 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3107 locations->SetOut(Location::ConstantLocation(constant));
3108}
3109
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003110void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003111 // Will be generated at use site.
3112}
3113
3114void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
3115 LocationSummary* locations =
3116 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3117 locations->SetOut(Location::ConstantLocation(constant));
3118}
3119
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003120void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003121 // Will be generated at use site.
3122}
3123
Igor Murashkind01745e2017-04-05 16:40:31 -07003124void LocationsBuilderARM::VisitConstructorFence(HConstructorFence* constructor_fence) {
3125 constructor_fence->SetLocations(nullptr);
3126}
3127
3128void InstructionCodeGeneratorARM::VisitConstructorFence(
3129 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3130 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3131}
3132
Calin Juravle27df7582015-04-17 19:12:31 +01003133void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3134 memory_barrier->SetLocations(nullptr);
3135}
3136
3137void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00003138 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01003139}
3140
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003141void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003142 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003143}
3144
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003145void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003146 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003147}
3148
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003149void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003150 LocationSummary* locations =
3151 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003152 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003153}
3154
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003155void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003156 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003157}
3158
Calin Juravle175dc732015-08-25 15:42:32 +01003159void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3160 // The trampoline uses the same calling convention as dex calling conventions,
3161 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3162 // the method_idx.
3163 HandleInvoke(invoke);
3164}
3165
3166void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3167 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3168}
3169
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003170void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003171 // Explicit clinit checks triggered by static invokes must have been pruned by
3172 // art::PrepareForRegisterAllocation.
3173 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003174
Vladimir Marko68c981f2016-08-26 13:13:33 +01003175 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003176 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00003177 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3178 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3179 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003180 return;
3181 }
3182
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003183 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00003184
3185 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3186 if (invoke->HasPcRelativeDexCache()) {
3187 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3188 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003189}
3190
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003191static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
3192 if (invoke->GetLocations()->Intrinsified()) {
3193 IntrinsicCodeGeneratorARM intrinsic(codegen);
3194 intrinsic.Dispatch(invoke);
3195 return true;
3196 }
3197 return false;
3198}
3199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003200void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003201 // Explicit clinit checks triggered by static invokes must have been pruned by
3202 // art::PrepareForRegisterAllocation.
3203 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003204
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003205 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3206 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003207 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003208
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003209 LocationSummary* locations = invoke->GetLocations();
3210 codegen_->GenerateStaticOrDirectCall(
3211 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003212 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003213}
3214
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003215void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003216 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003217 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003218}
3219
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003220void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01003221 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003222 if (intrinsic.TryDispatch(invoke)) {
3223 return;
3224 }
3225
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003226 HandleInvoke(invoke);
3227}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003228
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003229void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003230 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3231 return;
3232 }
3233
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003234 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003235 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003236 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003237}
3238
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003239void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3240 HandleInvoke(invoke);
3241 // Add the hidden argument.
3242 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
3243}
3244
3245void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3246 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00003247 LocationSummary* locations = invoke->GetLocations();
3248 Register temp = locations->GetTemp(0).AsRegister<Register>();
3249 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003250 Location receiver = locations->InAt(0);
3251 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3252
Roland Levillain3b359c72015-11-17 19:35:12 +00003253 // Set the hidden argument. This is safe to do this here, as R12
3254 // won't be modified thereafter, before the `blx` (call) instruction.
3255 DCHECK_EQ(R12, hidden_reg);
3256 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003257
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003258 if (receiver.IsStackSlot()) {
3259 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00003260 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003261 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3262 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00003263 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00003264 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003265 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003266 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00003267 // Instead of simply (possibly) unpoisoning `temp` here, we should
3268 // emit a read barrier for the previous class reference load.
3269 // However this is not required in practice, as this is an
3270 // intermediate/temporary reference and because the current
3271 // concurrent copying collector keeps the from-space memory
3272 // intact/accessible until the end of the marking phase (the
3273 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003274 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003275 __ LoadFromOffset(kLoadWord, temp, temp,
3276 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3277 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003278 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003279 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003280 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00003281 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07003282 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003283 // LR = temp->GetEntryPoint();
3284 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
3285 // LR();
3286 __ blx(LR);
3287 DCHECK(!codegen_->IsLeafMethod());
3288 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3289}
3290
Orion Hodsonac141392017-01-13 11:53:47 +00003291void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3292 HandleInvoke(invoke);
3293}
3294
3295void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3296 codegen_->GenerateInvokePolymorphicCall(invoke);
3297}
3298
Roland Levillain88cb1752014-10-20 16:36:47 +01003299void LocationsBuilderARM::VisitNeg(HNeg* neg) {
3300 LocationSummary* locations =
3301 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3302 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003303 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01003304 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3306 break;
3307 }
3308 case Primitive::kPrimLong: {
3309 locations->SetInAt(0, Location::RequiresRegister());
3310 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003311 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003312 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003313
Roland Levillain88cb1752014-10-20 16:36:47 +01003314 case Primitive::kPrimFloat:
3315 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003316 locations->SetInAt(0, Location::RequiresFpuRegister());
3317 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003318 break;
3319
3320 default:
3321 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3322 }
3323}
3324
3325void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
3326 LocationSummary* locations = neg->GetLocations();
3327 Location out = locations->Out();
3328 Location in = locations->InAt(0);
3329 switch (neg->GetResultType()) {
3330 case Primitive::kPrimInt:
3331 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003332 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01003333 break;
3334
3335 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003336 DCHECK(in.IsRegisterPair());
3337 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3338 __ rsbs(out.AsRegisterPairLow<Register>(),
3339 in.AsRegisterPairLow<Register>(),
3340 ShifterOperand(0));
3341 // We cannot emit an RSC (Reverse Subtract with Carry)
3342 // instruction here, as it does not exist in the Thumb-2
3343 // instruction set. We use the following approach
3344 // using SBC and SUB instead.
3345 //
3346 // out.hi = -C
3347 __ sbc(out.AsRegisterPairHigh<Register>(),
3348 out.AsRegisterPairHigh<Register>(),
3349 ShifterOperand(out.AsRegisterPairHigh<Register>()));
3350 // out.hi = out.hi - in.hi
3351 __ sub(out.AsRegisterPairHigh<Register>(),
3352 out.AsRegisterPairHigh<Register>(),
3353 ShifterOperand(in.AsRegisterPairHigh<Register>()));
3354 break;
3355
Roland Levillain88cb1752014-10-20 16:36:47 +01003356 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003357 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003358 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00003359 break;
3360
Roland Levillain88cb1752014-10-20 16:36:47 +01003361 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003362 DCHECK(in.IsFpuRegisterPair());
3363 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3364 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01003365 break;
3366
3367 default:
3368 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3369 }
3370}
3371
Roland Levillaindff1f282014-11-05 14:15:05 +00003372void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003373 Primitive::Type result_type = conversion->GetResultType();
3374 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003375 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00003376
Roland Levillain5b3ee562015-04-14 16:02:41 +01003377 // The float-to-long, double-to-long and long-to-float type conversions
3378 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003379 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01003380 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3381 && result_type == Primitive::kPrimLong)
3382 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003383 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00003384 : LocationSummary::kNoCall;
3385 LocationSummary* locations =
3386 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3387
David Brazdilb2bd1c52015-03-25 11:17:37 +00003388 // The Java language does not allow treating boolean as an integral type but
3389 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00003390
Roland Levillaindff1f282014-11-05 14:15:05 +00003391 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003392 case Primitive::kPrimByte:
3393 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003394 case Primitive::kPrimLong:
3395 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003396 case Primitive::kPrimBoolean:
3397 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003398 case Primitive::kPrimShort:
3399 case Primitive::kPrimInt:
3400 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003401 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003402 locations->SetInAt(0, Location::RequiresRegister());
3403 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3404 break;
3405
3406 default:
3407 LOG(FATAL) << "Unexpected type conversion from " << input_type
3408 << " to " << result_type;
3409 }
3410 break;
3411
Roland Levillain01a8d712014-11-14 16:27:39 +00003412 case Primitive::kPrimShort:
3413 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003414 case Primitive::kPrimLong:
3415 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003416 case Primitive::kPrimBoolean:
3417 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003418 case Primitive::kPrimByte:
3419 case Primitive::kPrimInt:
3420 case Primitive::kPrimChar:
3421 // Processing a Dex `int-to-short' instruction.
3422 locations->SetInAt(0, Location::RequiresRegister());
3423 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3424 break;
3425
3426 default:
3427 LOG(FATAL) << "Unexpected type conversion from " << input_type
3428 << " to " << result_type;
3429 }
3430 break;
3431
Roland Levillain946e1432014-11-11 17:35:19 +00003432 case Primitive::kPrimInt:
3433 switch (input_type) {
3434 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003435 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003436 locations->SetInAt(0, Location::Any());
3437 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3438 break;
3439
3440 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00003441 // Processing a Dex `float-to-int' instruction.
3442 locations->SetInAt(0, Location::RequiresFpuRegister());
3443 locations->SetOut(Location::RequiresRegister());
3444 locations->AddTemp(Location::RequiresFpuRegister());
3445 break;
3446
Roland Levillain946e1432014-11-11 17:35:19 +00003447 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003448 // Processing a Dex `double-to-int' instruction.
3449 locations->SetInAt(0, Location::RequiresFpuRegister());
3450 locations->SetOut(Location::RequiresRegister());
3451 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00003452 break;
3453
3454 default:
3455 LOG(FATAL) << "Unexpected type conversion from " << input_type
3456 << " to " << result_type;
3457 }
3458 break;
3459
Roland Levillaindff1f282014-11-05 14:15:05 +00003460 case Primitive::kPrimLong:
3461 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003462 case Primitive::kPrimBoolean:
3463 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003464 case Primitive::kPrimByte:
3465 case Primitive::kPrimShort:
3466 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003467 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003468 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003469 locations->SetInAt(0, Location::RequiresRegister());
3470 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3471 break;
3472
Roland Levillain624279f2014-12-04 11:54:28 +00003473 case Primitive::kPrimFloat: {
3474 // Processing a Dex `float-to-long' instruction.
3475 InvokeRuntimeCallingConvention calling_convention;
3476 locations->SetInAt(0, Location::FpuRegisterLocation(
3477 calling_convention.GetFpuRegisterAt(0)));
3478 locations->SetOut(Location::RegisterPairLocation(R0, R1));
3479 break;
3480 }
3481
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003482 case Primitive::kPrimDouble: {
3483 // Processing a Dex `double-to-long' instruction.
3484 InvokeRuntimeCallingConvention calling_convention;
3485 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3486 calling_convention.GetFpuRegisterAt(0),
3487 calling_convention.GetFpuRegisterAt(1)));
3488 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00003489 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003490 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003491
3492 default:
3493 LOG(FATAL) << "Unexpected type conversion from " << input_type
3494 << " to " << result_type;
3495 }
3496 break;
3497
Roland Levillain981e4542014-11-14 11:47:14 +00003498 case Primitive::kPrimChar:
3499 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003500 case Primitive::kPrimLong:
3501 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003502 case Primitive::kPrimBoolean:
3503 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003504 case Primitive::kPrimByte:
3505 case Primitive::kPrimShort:
3506 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003507 // Processing a Dex `int-to-char' instruction.
3508 locations->SetInAt(0, Location::RequiresRegister());
3509 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3510 break;
3511
3512 default:
3513 LOG(FATAL) << "Unexpected type conversion from " << input_type
3514 << " to " << result_type;
3515 }
3516 break;
3517
Roland Levillaindff1f282014-11-05 14:15:05 +00003518 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003519 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003520 case Primitive::kPrimBoolean:
3521 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003522 case Primitive::kPrimByte:
3523 case Primitive::kPrimShort:
3524 case Primitive::kPrimInt:
3525 case Primitive::kPrimChar:
3526 // Processing a Dex `int-to-float' instruction.
3527 locations->SetInAt(0, Location::RequiresRegister());
3528 locations->SetOut(Location::RequiresFpuRegister());
3529 break;
3530
Roland Levillain5b3ee562015-04-14 16:02:41 +01003531 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00003532 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01003533 InvokeRuntimeCallingConvention calling_convention;
3534 locations->SetInAt(0, Location::RegisterPairLocation(
3535 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3536 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00003537 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01003538 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003539
Roland Levillaincff13742014-11-17 14:32:17 +00003540 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003541 // Processing a Dex `double-to-float' instruction.
3542 locations->SetInAt(0, Location::RequiresFpuRegister());
3543 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003544 break;
3545
3546 default:
3547 LOG(FATAL) << "Unexpected type conversion from " << input_type
3548 << " to " << result_type;
3549 };
3550 break;
3551
Roland Levillaindff1f282014-11-05 14:15:05 +00003552 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003553 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003554 case Primitive::kPrimBoolean:
3555 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003556 case Primitive::kPrimByte:
3557 case Primitive::kPrimShort:
3558 case Primitive::kPrimInt:
3559 case Primitive::kPrimChar:
3560 // Processing a Dex `int-to-double' instruction.
3561 locations->SetInAt(0, Location::RequiresRegister());
3562 locations->SetOut(Location::RequiresFpuRegister());
3563 break;
3564
3565 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003566 // Processing a Dex `long-to-double' instruction.
3567 locations->SetInAt(0, Location::RequiresRegister());
3568 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01003569 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00003570 locations->AddTemp(Location::RequiresFpuRegister());
3571 break;
3572
Roland Levillaincff13742014-11-17 14:32:17 +00003573 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003574 // Processing a Dex `float-to-double' instruction.
3575 locations->SetInAt(0, Location::RequiresFpuRegister());
3576 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003577 break;
3578
3579 default:
3580 LOG(FATAL) << "Unexpected type conversion from " << input_type
3581 << " to " << result_type;
3582 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003583 break;
3584
3585 default:
3586 LOG(FATAL) << "Unexpected type conversion from " << input_type
3587 << " to " << result_type;
3588 }
3589}
3590
3591void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
3592 LocationSummary* locations = conversion->GetLocations();
3593 Location out = locations->Out();
3594 Location in = locations->InAt(0);
3595 Primitive::Type result_type = conversion->GetResultType();
3596 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003597 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00003598 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003599 case Primitive::kPrimByte:
3600 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003601 case Primitive::kPrimLong:
3602 // Type conversion from long to byte is a result of code transformations.
3603 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
3604 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003605 case Primitive::kPrimBoolean:
3606 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003607 case Primitive::kPrimShort:
3608 case Primitive::kPrimInt:
3609 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003610 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003611 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00003612 break;
3613
3614 default:
3615 LOG(FATAL) << "Unexpected type conversion from " << input_type
3616 << " to " << result_type;
3617 }
3618 break;
3619
Roland Levillain01a8d712014-11-14 16:27:39 +00003620 case Primitive::kPrimShort:
3621 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003622 case Primitive::kPrimLong:
3623 // Type conversion from long to short is a result of code transformations.
3624 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3625 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003626 case Primitive::kPrimBoolean:
3627 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003628 case Primitive::kPrimByte:
3629 case Primitive::kPrimInt:
3630 case Primitive::kPrimChar:
3631 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003632 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00003633 break;
3634
3635 default:
3636 LOG(FATAL) << "Unexpected type conversion from " << input_type
3637 << " to " << result_type;
3638 }
3639 break;
3640
Roland Levillain946e1432014-11-11 17:35:19 +00003641 case Primitive::kPrimInt:
3642 switch (input_type) {
3643 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003644 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003645 DCHECK(out.IsRegister());
3646 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003647 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003648 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003649 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00003650 } else {
3651 DCHECK(in.IsConstant());
3652 DCHECK(in.GetConstant()->IsLongConstant());
3653 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003654 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00003655 }
3656 break;
3657
Roland Levillain3f8f9362014-12-02 17:45:01 +00003658 case Primitive::kPrimFloat: {
3659 // Processing a Dex `float-to-int' instruction.
3660 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003661 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00003662 __ vmovrs(out.AsRegister<Register>(), temp);
3663 break;
3664 }
3665
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003666 case Primitive::kPrimDouble: {
3667 // Processing a Dex `double-to-int' instruction.
3668 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003669 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003670 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00003671 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003672 }
Roland Levillain946e1432014-11-11 17:35:19 +00003673
3674 default:
3675 LOG(FATAL) << "Unexpected type conversion from " << input_type
3676 << " to " << result_type;
3677 }
3678 break;
3679
Roland Levillaindff1f282014-11-05 14:15:05 +00003680 case Primitive::kPrimLong:
3681 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003682 case Primitive::kPrimBoolean:
3683 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003684 case Primitive::kPrimByte:
3685 case Primitive::kPrimShort:
3686 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003687 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003688 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003689 DCHECK(out.IsRegisterPair());
3690 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003691 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00003692 // Sign extension.
3693 __ Asr(out.AsRegisterPairHigh<Register>(),
3694 out.AsRegisterPairLow<Register>(),
3695 31);
3696 break;
3697
3698 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00003699 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003700 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003701 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003702 break;
3703
Roland Levillaindff1f282014-11-05 14:15:05 +00003704 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003705 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003706 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003707 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003708 break;
3709
3710 default:
3711 LOG(FATAL) << "Unexpected type conversion from " << input_type
3712 << " to " << result_type;
3713 }
3714 break;
3715
Roland Levillain981e4542014-11-14 11:47:14 +00003716 case Primitive::kPrimChar:
3717 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003718 case Primitive::kPrimLong:
3719 // Type conversion from long to char is a result of code transformations.
3720 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3721 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003722 case Primitive::kPrimBoolean:
3723 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003724 case Primitive::kPrimByte:
3725 case Primitive::kPrimShort:
3726 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003727 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003728 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00003729 break;
3730
3731 default:
3732 LOG(FATAL) << "Unexpected type conversion from " << input_type
3733 << " to " << result_type;
3734 }
3735 break;
3736
Roland Levillaindff1f282014-11-05 14:15:05 +00003737 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003738 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003739 case Primitive::kPrimBoolean:
3740 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003741 case Primitive::kPrimByte:
3742 case Primitive::kPrimShort:
3743 case Primitive::kPrimInt:
3744 case Primitive::kPrimChar: {
3745 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003746 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
3747 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003748 break;
3749 }
3750
Roland Levillain5b3ee562015-04-14 16:02:41 +01003751 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003752 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003753 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003754 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00003755 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003756
Roland Levillaincff13742014-11-17 14:32:17 +00003757 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003758 // Processing a Dex `double-to-float' instruction.
3759 __ vcvtsd(out.AsFpuRegister<SRegister>(),
3760 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00003761 break;
3762
3763 default:
3764 LOG(FATAL) << "Unexpected type conversion from " << input_type
3765 << " to " << result_type;
3766 };
3767 break;
3768
Roland Levillaindff1f282014-11-05 14:15:05 +00003769 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003770 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003771 case Primitive::kPrimBoolean:
3772 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003773 case Primitive::kPrimByte:
3774 case Primitive::kPrimShort:
3775 case Primitive::kPrimInt:
3776 case Primitive::kPrimChar: {
3777 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003778 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003779 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3780 out.AsFpuRegisterPairLow<SRegister>());
3781 break;
3782 }
3783
Roland Levillain647b9ed2014-11-27 12:06:00 +00003784 case Primitive::kPrimLong: {
3785 // Processing a Dex `long-to-double' instruction.
3786 Register low = in.AsRegisterPairLow<Register>();
3787 Register high = in.AsRegisterPairHigh<Register>();
3788 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
3789 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003790 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00003791 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003792 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
3793 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003794
Roland Levillain682393c2015-04-14 15:57:52 +01003795 // temp_d = int-to-double(high)
3796 __ vmovsr(temp_s, high);
3797 __ vcvtdi(temp_d, temp_s);
3798 // constant_d = k2Pow32EncodingForDouble
3799 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
3800 // out_d = unsigned-to-double(low)
3801 __ vmovsr(out_s, low);
3802 __ vcvtdu(out_d, out_s);
3803 // out_d += temp_d * constant_d
3804 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003805 break;
3806 }
3807
Roland Levillaincff13742014-11-17 14:32:17 +00003808 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003809 // Processing a Dex `float-to-double' instruction.
3810 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3811 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003812 break;
3813
3814 default:
3815 LOG(FATAL) << "Unexpected type conversion from " << input_type
3816 << " to " << result_type;
3817 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003818 break;
3819
3820 default:
3821 LOG(FATAL) << "Unexpected type conversion from " << input_type
3822 << " to " << result_type;
3823 }
3824}
3825
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003826void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003827 LocationSummary* locations =
3828 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003829 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003830 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003831 locations->SetInAt(0, Location::RequiresRegister());
3832 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003833 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3834 break;
3835 }
3836
3837 case Primitive::kPrimLong: {
3838 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003839 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003840 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003841 break;
3842 }
3843
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003844 case Primitive::kPrimFloat:
3845 case Primitive::kPrimDouble: {
3846 locations->SetInAt(0, Location::RequiresFpuRegister());
3847 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003848 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003849 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003850 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003851
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003852 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003853 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003854 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003855}
3856
3857void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
3858 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003859 Location out = locations->Out();
3860 Location first = locations->InAt(0);
3861 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003862 switch (add->GetResultType()) {
3863 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003864 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003865 __ add(out.AsRegister<Register>(),
3866 first.AsRegister<Register>(),
3867 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003868 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003869 __ AddConstant(out.AsRegister<Register>(),
3870 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003871 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003872 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003873 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003874
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003875 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003876 if (second.IsConstant()) {
3877 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3878 GenerateAddLongConst(out, first, value);
3879 } else {
3880 DCHECK(second.IsRegisterPair());
3881 __ adds(out.AsRegisterPairLow<Register>(),
3882 first.AsRegisterPairLow<Register>(),
3883 ShifterOperand(second.AsRegisterPairLow<Register>()));
3884 __ adc(out.AsRegisterPairHigh<Register>(),
3885 first.AsRegisterPairHigh<Register>(),
3886 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3887 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003888 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003889 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003890
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003891 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00003892 __ vadds(out.AsFpuRegister<SRegister>(),
3893 first.AsFpuRegister<SRegister>(),
3894 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003895 break;
3896
3897 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003898 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3899 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3900 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003901 break;
3902
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003903 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003904 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003905 }
3906}
3907
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003908void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003909 LocationSummary* locations =
3910 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003911 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003912 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003913 locations->SetInAt(0, Location::RequiresRegister());
3914 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003915 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3916 break;
3917 }
3918
3919 case Primitive::kPrimLong: {
3920 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003921 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003922 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003923 break;
3924 }
Calin Juravle11351682014-10-23 15:38:15 +01003925 case Primitive::kPrimFloat:
3926 case Primitive::kPrimDouble: {
3927 locations->SetInAt(0, Location::RequiresFpuRegister());
3928 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003929 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003930 break;
Calin Juravle11351682014-10-23 15:38:15 +01003931 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003932 default:
Calin Juravle11351682014-10-23 15:38:15 +01003933 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003934 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003935}
3936
3937void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3938 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003939 Location out = locations->Out();
3940 Location first = locations->InAt(0);
3941 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003942 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003943 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003944 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003945 __ sub(out.AsRegister<Register>(),
3946 first.AsRegister<Register>(),
3947 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003948 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003949 __ AddConstant(out.AsRegister<Register>(),
3950 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003951 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003952 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003953 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003954 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003955
Calin Juravle11351682014-10-23 15:38:15 +01003956 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003957 if (second.IsConstant()) {
3958 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3959 GenerateAddLongConst(out, first, -value);
3960 } else {
3961 DCHECK(second.IsRegisterPair());
3962 __ subs(out.AsRegisterPairLow<Register>(),
3963 first.AsRegisterPairLow<Register>(),
3964 ShifterOperand(second.AsRegisterPairLow<Register>()));
3965 __ sbc(out.AsRegisterPairHigh<Register>(),
3966 first.AsRegisterPairHigh<Register>(),
3967 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3968 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003969 break;
Calin Juravle11351682014-10-23 15:38:15 +01003970 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003971
Calin Juravle11351682014-10-23 15:38:15 +01003972 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003973 __ vsubs(out.AsFpuRegister<SRegister>(),
3974 first.AsFpuRegister<SRegister>(),
3975 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003976 break;
Calin Juravle11351682014-10-23 15:38:15 +01003977 }
3978
3979 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003980 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3981 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3982 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003983 break;
3984 }
3985
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003986
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003987 default:
Calin Juravle11351682014-10-23 15:38:15 +01003988 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003989 }
3990}
3991
Calin Juravle34bacdf2014-10-07 20:23:36 +01003992void LocationsBuilderARM::VisitMul(HMul* mul) {
3993 LocationSummary* locations =
3994 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3995 switch (mul->GetResultType()) {
3996 case Primitive::kPrimInt:
3997 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003998 locations->SetInAt(0, Location::RequiresRegister());
3999 locations->SetInAt(1, Location::RequiresRegister());
4000 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004001 break;
4002 }
4003
Calin Juravleb5bfa962014-10-21 18:02:24 +01004004 case Primitive::kPrimFloat:
4005 case Primitive::kPrimDouble: {
4006 locations->SetInAt(0, Location::RequiresFpuRegister());
4007 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004008 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004009 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004010 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004011
4012 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004013 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004014 }
4015}
4016
4017void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
4018 LocationSummary* locations = mul->GetLocations();
4019 Location out = locations->Out();
4020 Location first = locations->InAt(0);
4021 Location second = locations->InAt(1);
4022 switch (mul->GetResultType()) {
4023 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00004024 __ mul(out.AsRegister<Register>(),
4025 first.AsRegister<Register>(),
4026 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004027 break;
4028 }
4029 case Primitive::kPrimLong: {
4030 Register out_hi = out.AsRegisterPairHigh<Register>();
4031 Register out_lo = out.AsRegisterPairLow<Register>();
4032 Register in1_hi = first.AsRegisterPairHigh<Register>();
4033 Register in1_lo = first.AsRegisterPairLow<Register>();
4034 Register in2_hi = second.AsRegisterPairHigh<Register>();
4035 Register in2_lo = second.AsRegisterPairLow<Register>();
4036
4037 // Extra checks to protect caused by the existence of R1_R2.
4038 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4039 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
4040 DCHECK_NE(out_hi, in1_lo);
4041 DCHECK_NE(out_hi, in2_lo);
4042
4043 // input: in1 - 64 bits, in2 - 64 bits
4044 // output: out
4045 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4046 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4047 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4048
4049 // IP <- in1.lo * in2.hi
4050 __ mul(IP, in1_lo, in2_hi);
4051 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4052 __ mla(out_hi, in1_hi, in2_lo, IP);
4053 // out.lo <- (in1.lo * in2.lo)[31:0];
4054 __ umull(out_lo, IP, in1_lo, in2_lo);
4055 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
4056 __ add(out_hi, out_hi, ShifterOperand(IP));
4057 break;
4058 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01004059
4060 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004061 __ vmuls(out.AsFpuRegister<SRegister>(),
4062 first.AsFpuRegister<SRegister>(),
4063 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004064 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004065 }
4066
4067 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004068 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4069 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4070 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01004071 break;
4072 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004073
4074 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004075 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004076 }
4077}
4078
Zheng Xuc6667102015-05-15 16:08:45 +08004079void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4080 DCHECK(instruction->IsDiv() || instruction->IsRem());
4081 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4082
4083 LocationSummary* locations = instruction->GetLocations();
4084 Location second = locations->InAt(1);
4085 DCHECK(second.IsConstant());
4086
4087 Register out = locations->Out().AsRegister<Register>();
4088 Register dividend = locations->InAt(0).AsRegister<Register>();
4089 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4090 DCHECK(imm == 1 || imm == -1);
4091
4092 if (instruction->IsRem()) {
4093 __ LoadImmediate(out, 0);
4094 } else {
4095 if (imm == 1) {
4096 __ Mov(out, dividend);
4097 } else {
4098 __ rsb(out, dividend, ShifterOperand(0));
4099 }
4100 }
4101}
4102
4103void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4104 DCHECK(instruction->IsDiv() || instruction->IsRem());
4105 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4106
4107 LocationSummary* locations = instruction->GetLocations();
4108 Location second = locations->InAt(1);
4109 DCHECK(second.IsConstant());
4110
4111 Register out = locations->Out().AsRegister<Register>();
4112 Register dividend = locations->InAt(0).AsRegister<Register>();
4113 Register temp = locations->GetTemp(0).AsRegister<Register>();
4114 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004115 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08004116 int ctz_imm = CTZ(abs_imm);
4117
4118 if (ctz_imm == 1) {
4119 __ Lsr(temp, dividend, 32 - ctz_imm);
4120 } else {
4121 __ Asr(temp, dividend, 31);
4122 __ Lsr(temp, temp, 32 - ctz_imm);
4123 }
4124 __ add(out, temp, ShifterOperand(dividend));
4125
4126 if (instruction->IsDiv()) {
4127 __ Asr(out, out, ctz_imm);
4128 if (imm < 0) {
4129 __ rsb(out, out, ShifterOperand(0));
4130 }
4131 } else {
4132 __ ubfx(out, out, 0, ctz_imm);
4133 __ sub(out, out, ShifterOperand(temp));
4134 }
4135}
4136
4137void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4138 DCHECK(instruction->IsDiv() || instruction->IsRem());
4139 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4140
4141 LocationSummary* locations = instruction->GetLocations();
4142 Location second = locations->InAt(1);
4143 DCHECK(second.IsConstant());
4144
4145 Register out = locations->Out().AsRegister<Register>();
4146 Register dividend = locations->InAt(0).AsRegister<Register>();
4147 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4148 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4149 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4150
4151 int64_t magic;
4152 int shift;
4153 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4154
4155 __ LoadImmediate(temp1, magic);
4156 __ smull(temp2, temp1, dividend, temp1);
4157
4158 if (imm > 0 && magic < 0) {
4159 __ add(temp1, temp1, ShifterOperand(dividend));
4160 } else if (imm < 0 && magic > 0) {
4161 __ sub(temp1, temp1, ShifterOperand(dividend));
4162 }
4163
4164 if (shift != 0) {
4165 __ Asr(temp1, temp1, shift);
4166 }
4167
4168 if (instruction->IsDiv()) {
4169 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
4170 } else {
4171 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
4172 // TODO: Strength reduction for mls.
4173 __ LoadImmediate(temp2, imm);
4174 __ mls(out, temp1, temp2, dividend);
4175 }
4176}
4177
4178void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
4179 DCHECK(instruction->IsDiv() || instruction->IsRem());
4180 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4181
4182 LocationSummary* locations = instruction->GetLocations();
4183 Location second = locations->InAt(1);
4184 DCHECK(second.IsConstant());
4185
4186 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4187 if (imm == 0) {
4188 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4189 } else if (imm == 1 || imm == -1) {
4190 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004191 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004192 DivRemByPowerOfTwo(instruction);
4193 } else {
4194 DCHECK(imm <= -2 || imm >= 2);
4195 GenerateDivRemWithAnyConstant(instruction);
4196 }
4197}
4198
Calin Juravle7c4954d2014-10-28 16:57:40 +00004199void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004200 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4201 if (div->GetResultType() == Primitive::kPrimLong) {
4202 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004203 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004204 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4205 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004206 } else if (div->GetResultType() == Primitive::kPrimInt &&
4207 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4208 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004209 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004210 }
4211
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004212 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4213
Calin Juravle7c4954d2014-10-28 16:57:40 +00004214 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004215 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004216 if (div->InputAt(1)->IsConstant()) {
4217 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004218 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004219 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004220 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
4221 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004222 // No temp register required.
4223 } else {
4224 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004225 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004226 locations->AddTemp(Location::RequiresRegister());
4227 }
4228 }
4229 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004230 locations->SetInAt(0, Location::RequiresRegister());
4231 locations->SetInAt(1, Location::RequiresRegister());
4232 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4233 } else {
4234 InvokeRuntimeCallingConvention calling_convention;
4235 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4236 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004237 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004238 // we only need the former.
4239 locations->SetOut(Location::RegisterLocation(R0));
4240 }
Calin Juravled0d48522014-11-04 16:40:20 +00004241 break;
4242 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004243 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004244 InvokeRuntimeCallingConvention calling_convention;
4245 locations->SetInAt(0, Location::RegisterPairLocation(
4246 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4247 locations->SetInAt(1, Location::RegisterPairLocation(
4248 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004249 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004250 break;
4251 }
4252 case Primitive::kPrimFloat:
4253 case Primitive::kPrimDouble: {
4254 locations->SetInAt(0, Location::RequiresFpuRegister());
4255 locations->SetInAt(1, Location::RequiresFpuRegister());
4256 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4257 break;
4258 }
4259
4260 default:
4261 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4262 }
4263}
4264
4265void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
4266 LocationSummary* locations = div->GetLocations();
4267 Location out = locations->Out();
4268 Location first = locations->InAt(0);
4269 Location second = locations->InAt(1);
4270
4271 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004272 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004273 if (second.IsConstant()) {
4274 GenerateDivRemConstantIntegral(div);
4275 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004276 __ sdiv(out.AsRegister<Register>(),
4277 first.AsRegister<Register>(),
4278 second.AsRegister<Register>());
4279 } else {
4280 InvokeRuntimeCallingConvention calling_convention;
4281 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4282 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4283 DCHECK_EQ(R0, out.AsRegister<Register>());
4284
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004285 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004286 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004287 }
Calin Juravled0d48522014-11-04 16:40:20 +00004288 break;
4289 }
4290
Calin Juravle7c4954d2014-10-28 16:57:40 +00004291 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004292 InvokeRuntimeCallingConvention calling_convention;
4293 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4294 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4295 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4296 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4297 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004298 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004299
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004300 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004301 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00004302 break;
4303 }
4304
4305 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004306 __ vdivs(out.AsFpuRegister<SRegister>(),
4307 first.AsFpuRegister<SRegister>(),
4308 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004309 break;
4310 }
4311
4312 case Primitive::kPrimDouble: {
4313 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4314 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4315 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
4316 break;
4317 }
4318
4319 default:
4320 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4321 }
4322}
4323
Calin Juravlebacfec32014-11-14 15:54:36 +00004324void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004325 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004326
4327 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004328 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004329 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4330 // sdiv will be replaced by other instruction sequence.
4331 call_kind = LocationSummary::kNoCall;
4332 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4333 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004334 // Have hardware divide instruction for int, do it with three instructions.
4335 call_kind = LocationSummary::kNoCall;
4336 }
4337
Calin Juravlebacfec32014-11-14 15:54:36 +00004338 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4339
Calin Juravled2ec87d2014-12-08 14:24:46 +00004340 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004341 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004342 if (rem->InputAt(1)->IsConstant()) {
4343 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004344 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004345 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004346 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
4347 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004348 // No temp register required.
4349 } else {
4350 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004351 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004352 locations->AddTemp(Location::RequiresRegister());
4353 }
4354 }
4355 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004356 locations->SetInAt(0, Location::RequiresRegister());
4357 locations->SetInAt(1, Location::RequiresRegister());
4358 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4359 locations->AddTemp(Location::RequiresRegister());
4360 } else {
4361 InvokeRuntimeCallingConvention calling_convention;
4362 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4363 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004364 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004365 // we only need the latter.
4366 locations->SetOut(Location::RegisterLocation(R1));
4367 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004368 break;
4369 }
4370 case Primitive::kPrimLong: {
4371 InvokeRuntimeCallingConvention calling_convention;
4372 locations->SetInAt(0, Location::RegisterPairLocation(
4373 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4374 locations->SetInAt(1, Location::RegisterPairLocation(
4375 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4376 // The runtime helper puts the output in R2,R3.
4377 locations->SetOut(Location::RegisterPairLocation(R2, R3));
4378 break;
4379 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00004380 case Primitive::kPrimFloat: {
4381 InvokeRuntimeCallingConvention calling_convention;
4382 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4383 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4384 locations->SetOut(Location::FpuRegisterLocation(S0));
4385 break;
4386 }
4387
Calin Juravlebacfec32014-11-14 15:54:36 +00004388 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004389 InvokeRuntimeCallingConvention calling_convention;
4390 locations->SetInAt(0, Location::FpuRegisterPairLocation(
4391 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4392 locations->SetInAt(1, Location::FpuRegisterPairLocation(
4393 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4394 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00004395 break;
4396 }
4397
4398 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004399 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004400 }
4401}
4402
4403void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
4404 LocationSummary* locations = rem->GetLocations();
4405 Location out = locations->Out();
4406 Location first = locations->InAt(0);
4407 Location second = locations->InAt(1);
4408
Calin Juravled2ec87d2014-12-08 14:24:46 +00004409 Primitive::Type type = rem->GetResultType();
4410 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004411 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004412 if (second.IsConstant()) {
4413 GenerateDivRemConstantIntegral(rem);
4414 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004415 Register reg1 = first.AsRegister<Register>();
4416 Register reg2 = second.AsRegister<Register>();
4417 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004418
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004419 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004420 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004421 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004422 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004423 } else {
4424 InvokeRuntimeCallingConvention calling_convention;
4425 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4426 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4427 DCHECK_EQ(R1, out.AsRegister<Register>());
4428
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004429 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004430 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004431 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004432 break;
4433 }
4434
4435 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004436 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004437 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004438 break;
4439 }
4440
Calin Juravled2ec87d2014-12-08 14:24:46 +00004441 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004442 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004443 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00004444 break;
4445 }
4446
Calin Juravlebacfec32014-11-14 15:54:36 +00004447 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004448 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004449 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004450 break;
4451 }
4452
4453 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004454 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004455 }
4456}
4457
Calin Juravled0d48522014-11-04 16:40:20 +00004458void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004459 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004460 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00004461}
4462
4463void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004464 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004465 codegen_->AddSlowPath(slow_path);
4466
4467 LocationSummary* locations = instruction->GetLocations();
4468 Location value = locations->InAt(0);
4469
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004470 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00004471 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004472 case Primitive::kPrimByte:
4473 case Primitive::kPrimChar:
4474 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004475 case Primitive::kPrimInt: {
4476 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004477 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004478 } else {
4479 DCHECK(value.IsConstant()) << value;
4480 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4481 __ b(slow_path->GetEntryLabel());
4482 }
4483 }
4484 break;
4485 }
4486 case Primitive::kPrimLong: {
4487 if (value.IsRegisterPair()) {
4488 __ orrs(IP,
4489 value.AsRegisterPairLow<Register>(),
4490 ShifterOperand(value.AsRegisterPairHigh<Register>()));
4491 __ b(slow_path->GetEntryLabel(), EQ);
4492 } else {
4493 DCHECK(value.IsConstant()) << value;
4494 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4495 __ b(slow_path->GetEntryLabel());
4496 }
4497 }
4498 break;
4499 default:
4500 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4501 }
4502 }
Calin Juravled0d48522014-11-04 16:40:20 +00004503}
4504
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004505void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
4506 Register in = locations->InAt(0).AsRegister<Register>();
4507 Location rhs = locations->InAt(1);
4508 Register out = locations->Out().AsRegister<Register>();
4509
4510 if (rhs.IsConstant()) {
4511 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4512 // so map all rotations to a +ve. equivalent in that range.
4513 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4514 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4515 if (rot) {
4516 // Rotate, mapping left rotations to right equivalents if necessary.
4517 // (e.g. left by 2 bits == right by 30.)
4518 __ Ror(out, in, rot);
4519 } else if (out != in) {
4520 __ Mov(out, in);
4521 }
4522 } else {
4523 __ Ror(out, in, rhs.AsRegister<Register>());
4524 }
4525}
4526
4527// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4528// rotates by swapping input regs (effectively rotating by the first 32-bits of
4529// a larger rotation) or flipping direction (thus treating larger right/left
4530// rotations as sub-word sized rotations in the other direction) as appropriate.
Anton Kirilov6f644202017-02-27 18:29:45 +00004531void InstructionCodeGeneratorARM::HandleLongRotate(HRor* ror) {
4532 LocationSummary* locations = ror->GetLocations();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004533 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
4534 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
4535 Location rhs = locations->InAt(1);
4536 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
4537 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
4538
4539 if (rhs.IsConstant()) {
4540 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4541 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00004542 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004543 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4544 // logic below to a simple pair of binary orr.
4545 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4546 if (rot >= kArmBitsPerWord) {
4547 rot -= kArmBitsPerWord;
4548 std::swap(in_reg_hi, in_reg_lo);
4549 }
4550 // Rotate, or mov to out for zero or word size rotations.
4551 if (rot != 0u) {
4552 __ Lsr(out_reg_hi, in_reg_hi, rot);
4553 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
4554 __ Lsr(out_reg_lo, in_reg_lo, rot);
4555 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
4556 } else {
4557 __ Mov(out_reg_lo, in_reg_lo);
4558 __ Mov(out_reg_hi, in_reg_hi);
4559 }
4560 } else {
4561 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
4562 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
4563 Label end;
4564 Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004565 Label* final_label = codegen_->GetFinalLabel(ror, &end);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004566
4567 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
4568 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
4569 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
4570 __ b(&shift_by_32_plus_shift_right, CC);
4571
4572 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4573 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4574 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4575 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4576 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4577 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4578 __ Lsr(shift_left, in_reg_hi, shift_right);
4579 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
Anton Kirilov6f644202017-02-27 18:29:45 +00004580 __ b(final_label);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004581
4582 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4583 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4584 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4585 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4586 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4587 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4588 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4589 __ Lsl(shift_right, in_reg_hi, shift_left);
4590 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
4591
Anton Kirilov6f644202017-02-27 18:29:45 +00004592 if (end.IsLinked()) {
4593 __ Bind(&end);
4594 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004595 }
4596}
Roland Levillain22c49222016-03-18 14:04:28 +00004597
4598void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004599 LocationSummary* locations =
4600 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4601 switch (ror->GetResultType()) {
4602 case Primitive::kPrimInt: {
4603 locations->SetInAt(0, Location::RequiresRegister());
4604 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4605 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4606 break;
4607 }
4608 case Primitive::kPrimLong: {
4609 locations->SetInAt(0, Location::RequiresRegister());
4610 if (ror->InputAt(1)->IsConstant()) {
4611 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4612 } else {
4613 locations->SetInAt(1, Location::RequiresRegister());
4614 locations->AddTemp(Location::RequiresRegister());
4615 locations->AddTemp(Location::RequiresRegister());
4616 }
4617 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4618 break;
4619 }
4620 default:
4621 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4622 }
4623}
4624
Roland Levillain22c49222016-03-18 14:04:28 +00004625void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004626 LocationSummary* locations = ror->GetLocations();
4627 Primitive::Type type = ror->GetResultType();
4628 switch (type) {
4629 case Primitive::kPrimInt: {
4630 HandleIntegerRotate(locations);
4631 break;
4632 }
4633 case Primitive::kPrimLong: {
Anton Kirilov6f644202017-02-27 18:29:45 +00004634 HandleLongRotate(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004635 break;
4636 }
4637 default:
4638 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004639 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004640 }
4641}
4642
Calin Juravle9aec02f2014-11-18 23:06:35 +00004643void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
4644 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4645
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004646 LocationSummary* locations =
4647 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004648
4649 switch (op->GetResultType()) {
4650 case Primitive::kPrimInt: {
4651 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004652 if (op->InputAt(1)->IsConstant()) {
4653 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4654 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4655 } else {
4656 locations->SetInAt(1, Location::RequiresRegister());
4657 // Make the output overlap, as it will be used to hold the masked
4658 // second input.
4659 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4660 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004661 break;
4662 }
4663 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004664 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004665 if (op->InputAt(1)->IsConstant()) {
4666 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4667 // For simplicity, use kOutputOverlap even though we only require that low registers
4668 // don't clash with high registers which the register allocator currently guarantees.
4669 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4670 } else {
4671 locations->SetInAt(1, Location::RequiresRegister());
4672 locations->AddTemp(Location::RequiresRegister());
4673 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4674 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004675 break;
4676 }
4677 default:
4678 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4679 }
4680}
4681
4682void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
4683 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4684
4685 LocationSummary* locations = op->GetLocations();
4686 Location out = locations->Out();
4687 Location first = locations->InAt(0);
4688 Location second = locations->InAt(1);
4689
4690 Primitive::Type type = op->GetResultType();
4691 switch (type) {
4692 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004693 Register out_reg = out.AsRegister<Register>();
4694 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004695 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004696 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00004697 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004698 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004699 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004700 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004701 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004702 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004703 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004704 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004705 }
4706 } else {
4707 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004708 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00004709 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00004710 __ Mov(out_reg, first_reg);
4711 } else if (op->IsShl()) {
4712 __ Lsl(out_reg, first_reg, shift_value);
4713 } else if (op->IsShr()) {
4714 __ Asr(out_reg, first_reg, shift_value);
4715 } else {
4716 __ Lsr(out_reg, first_reg, shift_value);
4717 }
4718 }
4719 break;
4720 }
4721 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004722 Register o_h = out.AsRegisterPairHigh<Register>();
4723 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004724
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004725 Register high = first.AsRegisterPairHigh<Register>();
4726 Register low = first.AsRegisterPairLow<Register>();
4727
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004728 if (second.IsRegister()) {
4729 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004730
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004731 Register second_reg = second.AsRegister<Register>();
4732
4733 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004734 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004735 // Shift the high part
4736 __ Lsl(o_h, high, o_l);
4737 // Shift the low part and `or` what overflew on the high part
4738 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
4739 __ Lsr(temp, low, temp);
4740 __ orr(o_h, o_h, ShifterOperand(temp));
4741 // If the shift is > 32 bits, override the high part
4742 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
4743 __ it(PL);
4744 __ Lsl(o_h, low, temp, PL);
4745 // Shift the low part
4746 __ Lsl(o_l, low, o_l);
4747 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004748 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004749 // Shift the low part
4750 __ Lsr(o_l, low, o_h);
4751 // Shift the high part and `or` what underflew on the low part
4752 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4753 __ Lsl(temp, high, temp);
4754 __ orr(o_l, o_l, ShifterOperand(temp));
4755 // If the shift is > 32 bits, override the low part
4756 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4757 __ it(PL);
4758 __ Asr(o_l, high, temp, PL);
4759 // Shift the high part
4760 __ Asr(o_h, high, o_h);
4761 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004762 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004763 // same as Shr except we use `Lsr`s and not `Asr`s
4764 __ Lsr(o_l, low, o_h);
4765 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4766 __ Lsl(temp, high, temp);
4767 __ orr(o_l, o_l, ShifterOperand(temp));
4768 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4769 __ it(PL);
4770 __ Lsr(o_l, high, temp, PL);
4771 __ Lsr(o_h, high, o_h);
4772 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004773 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004774 // Register allocator doesn't create partial overlap.
4775 DCHECK_NE(o_l, high);
4776 DCHECK_NE(o_h, low);
4777 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004778 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004779 if (shift_value > 32) {
4780 if (op->IsShl()) {
4781 __ Lsl(o_h, low, shift_value - 32);
4782 __ LoadImmediate(o_l, 0);
4783 } else if (op->IsShr()) {
4784 __ Asr(o_l, high, shift_value - 32);
4785 __ Asr(o_h, high, 31);
4786 } else {
4787 __ Lsr(o_l, high, shift_value - 32);
4788 __ LoadImmediate(o_h, 0);
4789 }
4790 } else if (shift_value == 32) {
4791 if (op->IsShl()) {
4792 __ mov(o_h, ShifterOperand(low));
4793 __ LoadImmediate(o_l, 0);
4794 } else if (op->IsShr()) {
4795 __ mov(o_l, ShifterOperand(high));
4796 __ Asr(o_h, high, 31);
4797 } else {
4798 __ mov(o_l, ShifterOperand(high));
4799 __ LoadImmediate(o_h, 0);
4800 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00004801 } else if (shift_value == 1) {
4802 if (op->IsShl()) {
4803 __ Lsls(o_l, low, 1);
4804 __ adc(o_h, high, ShifterOperand(high));
4805 } else if (op->IsShr()) {
4806 __ Asrs(o_h, high, 1);
4807 __ Rrx(o_l, low);
4808 } else {
4809 __ Lsrs(o_h, high, 1);
4810 __ Rrx(o_l, low);
4811 }
4812 } else {
4813 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004814 if (op->IsShl()) {
4815 __ Lsl(o_h, high, shift_value);
4816 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
4817 __ Lsl(o_l, low, shift_value);
4818 } else if (op->IsShr()) {
4819 __ Lsr(o_l, low, shift_value);
4820 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4821 __ Asr(o_h, high, shift_value);
4822 } else {
4823 __ Lsr(o_l, low, shift_value);
4824 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4825 __ Lsr(o_h, high, shift_value);
4826 }
4827 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004828 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004829 break;
4830 }
4831 default:
4832 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004833 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004834 }
4835}
4836
4837void LocationsBuilderARM::VisitShl(HShl* shl) {
4838 HandleShift(shl);
4839}
4840
4841void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
4842 HandleShift(shl);
4843}
4844
4845void LocationsBuilderARM::VisitShr(HShr* shr) {
4846 HandleShift(shr);
4847}
4848
4849void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
4850 HandleShift(shr);
4851}
4852
4853void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
4854 HandleShift(ushr);
4855}
4856
4857void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
4858 HandleShift(ushr);
4859}
4860
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004861void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004862 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004863 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00004864 if (instruction->IsStringAlloc()) {
4865 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4866 } else {
4867 InvokeRuntimeCallingConvention calling_convention;
4868 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004869 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004870 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004871}
4872
4873void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004874 // Note: if heap poisoning is enabled, the entry point takes cares
4875 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004876 if (instruction->IsStringAlloc()) {
4877 // String is allocated through StringFactory. Call NewEmptyString entry point.
4878 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004879 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004880 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4881 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
4882 __ blx(LR);
4883 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4884 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004885 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004886 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004887 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004888}
4889
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004890void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
4891 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004892 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004893 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004894 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004895 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4896 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004897}
4898
4899void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004900 // Note: if heap poisoning is enabled, the entry point takes cares
4901 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004902 QuickEntrypointEnum entrypoint =
4903 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4904 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004905 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004906 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004907}
4908
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004909void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004910 LocationSummary* locations =
4911 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004912 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4913 if (location.IsStackSlot()) {
4914 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4915 } else if (location.IsDoubleStackSlot()) {
4916 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004917 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004918 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004919}
4920
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004921void InstructionCodeGeneratorARM::VisitParameterValue(
4922 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004923 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004924}
4925
4926void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4927 LocationSummary* locations =
4928 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4929 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4930}
4931
4932void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4933 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004934}
4935
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004936void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004937 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004938 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004939 locations->SetInAt(0, Location::RequiresRegister());
4940 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004941}
4942
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004943void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4944 LocationSummary* locations = not_->GetLocations();
4945 Location out = locations->Out();
4946 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004947 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004948 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004949 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004950 break;
4951
4952 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004953 __ mvn(out.AsRegisterPairLow<Register>(),
4954 ShifterOperand(in.AsRegisterPairLow<Register>()));
4955 __ mvn(out.AsRegisterPairHigh<Register>(),
4956 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004957 break;
4958
4959 default:
4960 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4961 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004962}
4963
David Brazdil66d126e2015-04-03 16:02:44 +01004964void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4965 LocationSummary* locations =
4966 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4967 locations->SetInAt(0, Location::RequiresRegister());
4968 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4969}
4970
4971void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004972 LocationSummary* locations = bool_not->GetLocations();
4973 Location out = locations->Out();
4974 Location in = locations->InAt(0);
4975 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4976}
4977
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004978void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004979 LocationSummary* locations =
4980 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004981 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004982 case Primitive::kPrimBoolean:
4983 case Primitive::kPrimByte:
4984 case Primitive::kPrimShort:
4985 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004986 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004987 case Primitive::kPrimLong: {
4988 locations->SetInAt(0, Location::RequiresRegister());
4989 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004990 // Output overlaps because it is written before doing the low comparison.
4991 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004992 break;
4993 }
4994 case Primitive::kPrimFloat:
4995 case Primitive::kPrimDouble: {
4996 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004997 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004998 locations->SetOut(Location::RequiresRegister());
4999 break;
5000 }
5001 default:
5002 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5003 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005004}
5005
5006void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005007 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005008 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005009 Location left = locations->InAt(0);
5010 Location right = locations->InAt(1);
5011
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005012 Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005013 Label* final_label = codegen_->GetFinalLabel(compare, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00005014 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00005015 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00005016 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00005017 case Primitive::kPrimBoolean:
5018 case Primitive::kPrimByte:
5019 case Primitive::kPrimShort:
5020 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08005021 case Primitive::kPrimInt: {
5022 __ LoadImmediate(out, 0);
5023 __ cmp(left.AsRegister<Register>(),
5024 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
5025 less_cond = LT;
5026 break;
5027 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005028 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005029 __ cmp(left.AsRegisterPairHigh<Register>(),
5030 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005031 __ b(&less, LT);
5032 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01005033 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00005034 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005035 __ cmp(left.AsRegisterPairLow<Register>(),
5036 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005037 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00005038 break;
5039 }
5040 case Primitive::kPrimFloat:
5041 case Primitive::kPrimDouble: {
5042 __ LoadImmediate(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005043 GenerateVcmp(compare, codegen_);
Calin Juravleddb7df22014-11-25 20:56:51 +00005044 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005045 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005046 break;
5047 }
5048 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005049 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00005050 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005051 }
Aart Bika19616e2016-02-01 18:57:58 -08005052
Anton Kirilov6f644202017-02-27 18:29:45 +00005053 __ b(final_label, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00005054 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00005055
5056 __ Bind(&greater);
5057 __ LoadImmediate(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005058 __ b(final_label);
Calin Juravleddb7df22014-11-25 20:56:51 +00005059
5060 __ Bind(&less);
5061 __ LoadImmediate(out, -1);
5062
Anton Kirilov6f644202017-02-27 18:29:45 +00005063 if (done.IsLinked()) {
5064 __ Bind(&done);
5065 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005066}
5067
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005068void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005069 LocationSummary* locations =
5070 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005071 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005072 locations->SetInAt(i, Location::Any());
5073 }
5074 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005075}
5076
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005077void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005078 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005079}
5080
Roland Levillainc9285912015-12-18 10:38:42 +00005081void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
5082 // TODO (ported from quick): revisit ARM barrier kinds.
5083 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00005084 switch (kind) {
5085 case MemBarrierKind::kAnyStore:
5086 case MemBarrierKind::kLoadAny:
5087 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005088 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00005089 break;
5090 }
5091 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005092 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00005093 break;
5094 }
5095 default:
5096 LOG(FATAL) << "Unexpected memory barrier " << kind;
5097 }
Kenny Root1d8199d2015-06-02 11:01:10 -07005098 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00005099}
5100
5101void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
5102 uint32_t offset,
5103 Register out_lo,
5104 Register out_hi) {
5105 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005106 // Ensure `out_lo` is different from `addr`, so that loading
5107 // `offset` into `out_lo` does not clutter `addr`.
5108 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00005109 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005110 __ add(IP, addr, ShifterOperand(out_lo));
5111 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005112 }
5113 __ ldrexd(out_lo, out_hi, addr);
5114}
5115
5116void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
5117 uint32_t offset,
5118 Register value_lo,
5119 Register value_hi,
5120 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00005121 Register temp2,
5122 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005123 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00005124 if (offset != 0) {
5125 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005126 __ add(IP, addr, ShifterOperand(temp1));
5127 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005128 }
5129 __ Bind(&fail);
5130 // We need a load followed by store. (The address used in a STREX instruction must
5131 // be the same as the address in the most recently executed LDREX instruction.)
5132 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00005133 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005134 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005135 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00005136}
5137
5138void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5139 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5140
Nicolas Geoffray39468442014-09-02 15:17:15 +01005141 LocationSummary* locations =
5142 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005143 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005144
Calin Juravle52c48962014-12-16 17:02:57 +00005145 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005146 if (Primitive::IsFloatingPointType(field_type)) {
5147 locations->SetInAt(1, Location::RequiresFpuRegister());
5148 } else {
5149 locations->SetInAt(1, Location::RequiresRegister());
5150 }
5151
Calin Juravle52c48962014-12-16 17:02:57 +00005152 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00005153 bool generate_volatile = field_info.IsVolatile()
5154 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005155 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01005156 bool needs_write_barrier =
5157 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005158 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00005159 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01005160 if (needs_write_barrier) {
5161 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005162 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005163 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005164 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005165 // - registers need to be consecutive
5166 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005167 // We don't test for ARM yet, and the assertion makes sure that we
5168 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005169 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5170
5171 locations->AddTemp(Location::RequiresRegister());
5172 locations->AddTemp(Location::RequiresRegister());
5173 if (field_type == Primitive::kPrimDouble) {
5174 // For doubles we need two more registers to copy the value.
5175 locations->AddTemp(Location::RegisterLocation(R2));
5176 locations->AddTemp(Location::RegisterLocation(R3));
5177 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005178 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005179}
5180
Calin Juravle52c48962014-12-16 17:02:57 +00005181void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005182 const FieldInfo& field_info,
5183 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005184 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5185
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005186 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00005187 Register base = locations->InAt(0).AsRegister<Register>();
5188 Location value = locations->InAt(1);
5189
5190 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005191 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005192 Primitive::Type field_type = field_info.GetFieldType();
5193 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005194 bool needs_write_barrier =
5195 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005196
5197 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005198 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005199 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005200
5201 switch (field_type) {
5202 case Primitive::kPrimBoolean:
5203 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00005204 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005205 break;
5206 }
5207
5208 case Primitive::kPrimShort:
5209 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00005210 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005211 break;
5212 }
5213
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005214 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005215 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01005216 if (kPoisonHeapReferences && needs_write_barrier) {
5217 // Note that in the case where `value` is a null reference,
5218 // we do not enter this block, as a null reference does not
5219 // need poisoning.
5220 DCHECK_EQ(field_type, Primitive::kPrimNot);
5221 Register temp = locations->GetTemp(0).AsRegister<Register>();
5222 __ Mov(temp, value.AsRegister<Register>());
5223 __ PoisonHeapReference(temp);
5224 __ StoreToOffset(kStoreWord, temp, base, offset);
5225 } else {
5226 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
5227 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005228 break;
5229 }
5230
5231 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00005232 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005233 GenerateWideAtomicStore(base, offset,
5234 value.AsRegisterPairLow<Register>(),
5235 value.AsRegisterPairHigh<Register>(),
5236 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005237 locations->GetTemp(1).AsRegister<Register>(),
5238 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005239 } else {
5240 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005241 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005242 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005243 break;
5244 }
5245
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005246 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00005247 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005248 break;
5249 }
5250
5251 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005252 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005253 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005254 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
5255 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
5256
5257 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
5258
5259 GenerateWideAtomicStore(base, offset,
5260 value_reg_lo,
5261 value_reg_hi,
5262 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005263 locations->GetTemp(3).AsRegister<Register>(),
5264 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005265 } else {
5266 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005267 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005268 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005269 break;
5270 }
5271
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005272 case Primitive::kPrimVoid:
5273 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005274 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005275 }
Calin Juravle52c48962014-12-16 17:02:57 +00005276
Calin Juravle77520bc2015-01-12 18:45:46 +00005277 // Longs and doubles are handled in the switch.
5278 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
5279 codegen_->MaybeRecordImplicitNullCheck(instruction);
5280 }
5281
5282 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5283 Register temp = locations->GetTemp(0).AsRegister<Register>();
5284 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005285 codegen_->MarkGCCard(
5286 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005287 }
5288
Calin Juravle52c48962014-12-16 17:02:57 +00005289 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005290 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005291 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005292}
5293
Calin Juravle52c48962014-12-16 17:02:57 +00005294void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5295 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00005296
5297 bool object_field_get_with_read_barrier =
5298 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005299 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005300 new (GetGraph()->GetArena()) LocationSummary(instruction,
5301 object_field_get_with_read_barrier ?
5302 LocationSummary::kCallOnSlowPath :
5303 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005304 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005305 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005306 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005307 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00005308
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005309 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00005310 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005311 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00005312 // The output overlaps in case of volatile long: we don't want the
5313 // code generated by GenerateWideAtomicLoad to overwrite the
5314 // object's location. Likewise, in the case of an object field get
5315 // with read barriers enabled, we do not want the load to overwrite
5316 // the object's location, as we need it to emit the read barrier.
5317 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5318 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01005319
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005320 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5321 locations->SetOut(Location::RequiresFpuRegister());
5322 } else {
5323 locations->SetOut(Location::RequiresRegister(),
5324 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5325 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005326 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00005327 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005328 // - registers need to be consecutive
5329 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005330 // We don't test for ARM yet, and the assertion makes sure that we
5331 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005332 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5333 locations->AddTemp(Location::RequiresRegister());
5334 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00005335 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5336 // We need a temporary register for the read barrier marking slow
5337 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005338 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5339 !Runtime::Current()->UseJitCompilation()) {
5340 // If link-time thunks for the Baker read barrier are enabled, for AOT
5341 // loads we need a temporary only if the offset is too big.
5342 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5343 locations->AddTemp(Location::RequiresRegister());
5344 }
5345 // And we always need the reserved entrypoint register.
5346 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5347 } else {
5348 locations->AddTemp(Location::RequiresRegister());
5349 }
Calin Juravle52c48962014-12-16 17:02:57 +00005350 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005351}
5352
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005353Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5354 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
5355 << input->GetType();
5356 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5357 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5358 return Location::ConstantLocation(input->AsConstant());
5359 } else {
5360 return Location::RequiresFpuRegister();
5361 }
5362}
5363
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005364Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
5365 Opcode opcode) {
5366 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5367 if (constant->IsConstant() &&
5368 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5369 return Location::ConstantLocation(constant->AsConstant());
5370 }
5371 return Location::RequiresRegister();
5372}
5373
5374bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
5375 Opcode opcode) {
5376 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5377 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01005378 Opcode high_opcode = opcode;
5379 SetCc low_set_cc = kCcDontCare;
5380 switch (opcode) {
5381 case SUB:
5382 // Flip the operation to an ADD.
5383 value = -value;
5384 opcode = ADD;
5385 FALLTHROUGH_INTENDED;
5386 case ADD:
5387 if (Low32Bits(value) == 0u) {
5388 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5389 }
5390 high_opcode = ADC;
5391 low_set_cc = kCcSet;
5392 break;
5393 default:
5394 break;
5395 }
5396 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5397 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005398 } else {
5399 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5400 }
5401}
5402
Vladimir Marko59751a72016-08-05 14:37:27 +01005403bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
5404 Opcode opcode,
5405 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005406 ShifterOperand so;
5407 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01005408 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005409 return true;
5410 }
5411 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005412 uint32_t neg_value = 0;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005413 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005414 case AND: neg_opcode = BIC; neg_value = ~value; break;
5415 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5416 case ADD: neg_opcode = SUB; neg_value = -value; break;
5417 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5418 case SUB: neg_opcode = ADD; neg_value = -value; break;
5419 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5420 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005421 default:
5422 return false;
5423 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005424
5425 if (assembler->ShifterOperandCanHold(kNoRegister,
5426 kNoRegister,
5427 neg_opcode,
5428 neg_value,
5429 set_cc,
5430 &so)) {
5431 return true;
5432 }
5433
5434 return opcode == AND && IsPowerOfTwo(value + 1);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005435}
5436
Calin Juravle52c48962014-12-16 17:02:57 +00005437void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
5438 const FieldInfo& field_info) {
5439 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005440
Calin Juravle52c48962014-12-16 17:02:57 +00005441 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005442 Location base_loc = locations->InAt(0);
5443 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005444 Location out = locations->Out();
5445 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005446 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005447 Primitive::Type field_type = field_info.GetFieldType();
5448 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5449
5450 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00005451 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00005452 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005453 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005454
Roland Levillainc9285912015-12-18 10:38:42 +00005455 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00005456 __ LoadFromOffset(kLoadSignedByte, 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::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00005460 __ LoadFromOffset(kLoadSignedHalfword, 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::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00005464 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005465 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005466
5467 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00005468 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005469 break;
Roland Levillainc9285912015-12-18 10:38:42 +00005470
5471 case Primitive::kPrimNot: {
5472 // /* HeapReference<Object> */ out = *(base + offset)
5473 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5474 Location temp_loc = locations->GetTemp(0);
5475 // Note that a potential implicit null check is handled in this
5476 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
5477 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5478 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5479 if (is_volatile) {
5480 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5481 }
5482 } else {
5483 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
5484 codegen_->MaybeRecordImplicitNullCheck(instruction);
5485 if (is_volatile) {
5486 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5487 }
5488 // If read barriers are enabled, emit read barriers other than
5489 // Baker's using a slow path (and also unpoison the loaded
5490 // reference, if heap poisoning is enabled).
5491 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5492 }
5493 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005494 }
5495
Roland Levillainc9285912015-12-18 10:38:42 +00005496 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00005497 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005498 GenerateWideAtomicLoad(base, offset,
5499 out.AsRegisterPairLow<Register>(),
5500 out.AsRegisterPairHigh<Register>());
5501 } else {
5502 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
5503 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005504 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005505
Roland Levillainc9285912015-12-18 10:38:42 +00005506 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00005507 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005508 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005509
5510 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005511 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005512 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005513 Register lo = locations->GetTemp(0).AsRegister<Register>();
5514 Register hi = locations->GetTemp(1).AsRegister<Register>();
5515 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00005516 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005517 __ vmovdrr(out_reg, lo, hi);
5518 } else {
5519 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005520 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005521 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005522 break;
5523 }
5524
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005525 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005526 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005527 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005528 }
Calin Juravle52c48962014-12-16 17:02:57 +00005529
Roland Levillainc9285912015-12-18 10:38:42 +00005530 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5531 // Potential implicit null checks, in the case of reference or
5532 // double fields, are handled in the previous switch statement.
5533 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005534 codegen_->MaybeRecordImplicitNullCheck(instruction);
5535 }
5536
Calin Juravle52c48962014-12-16 17:02:57 +00005537 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005538 if (field_type == Primitive::kPrimNot) {
5539 // Memory barriers, in the case of references, are also handled
5540 // in the previous switch statement.
5541 } else {
5542 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5543 }
Roland Levillain4d027112015-07-01 15:41:14 +01005544 }
Calin Juravle52c48962014-12-16 17:02:57 +00005545}
5546
5547void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5548 HandleFieldSet(instruction, instruction->GetFieldInfo());
5549}
5550
5551void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005552 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005553}
5554
5555void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5556 HandleFieldGet(instruction, instruction->GetFieldInfo());
5557}
5558
5559void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5560 HandleFieldGet(instruction, instruction->GetFieldInfo());
5561}
5562
5563void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5564 HandleFieldGet(instruction, instruction->GetFieldInfo());
5565}
5566
5567void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5568 HandleFieldGet(instruction, instruction->GetFieldInfo());
5569}
5570
5571void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5572 HandleFieldSet(instruction, instruction->GetFieldInfo());
5573}
5574
5575void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005576 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005577}
5578
Calin Juravlee460d1d2015-09-29 04:52:17 +01005579void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
5580 HUnresolvedInstanceFieldGet* instruction) {
5581 FieldAccessCallingConventionARM calling_convention;
5582 codegen_->CreateUnresolvedFieldLocationSummary(
5583 instruction, instruction->GetFieldType(), calling_convention);
5584}
5585
5586void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
5587 HUnresolvedInstanceFieldGet* instruction) {
5588 FieldAccessCallingConventionARM calling_convention;
5589 codegen_->GenerateUnresolvedFieldAccess(instruction,
5590 instruction->GetFieldType(),
5591 instruction->GetFieldIndex(),
5592 instruction->GetDexPc(),
5593 calling_convention);
5594}
5595
5596void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
5597 HUnresolvedInstanceFieldSet* instruction) {
5598 FieldAccessCallingConventionARM calling_convention;
5599 codegen_->CreateUnresolvedFieldLocationSummary(
5600 instruction, instruction->GetFieldType(), calling_convention);
5601}
5602
5603void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
5604 HUnresolvedInstanceFieldSet* instruction) {
5605 FieldAccessCallingConventionARM calling_convention;
5606 codegen_->GenerateUnresolvedFieldAccess(instruction,
5607 instruction->GetFieldType(),
5608 instruction->GetFieldIndex(),
5609 instruction->GetDexPc(),
5610 calling_convention);
5611}
5612
5613void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
5614 HUnresolvedStaticFieldGet* instruction) {
5615 FieldAccessCallingConventionARM calling_convention;
5616 codegen_->CreateUnresolvedFieldLocationSummary(
5617 instruction, instruction->GetFieldType(), calling_convention);
5618}
5619
5620void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
5621 HUnresolvedStaticFieldGet* instruction) {
5622 FieldAccessCallingConventionARM calling_convention;
5623 codegen_->GenerateUnresolvedFieldAccess(instruction,
5624 instruction->GetFieldType(),
5625 instruction->GetFieldIndex(),
5626 instruction->GetDexPc(),
5627 calling_convention);
5628}
5629
5630void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
5631 HUnresolvedStaticFieldSet* instruction) {
5632 FieldAccessCallingConventionARM calling_convention;
5633 codegen_->CreateUnresolvedFieldLocationSummary(
5634 instruction, instruction->GetFieldType(), calling_convention);
5635}
5636
5637void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
5638 HUnresolvedStaticFieldSet* instruction) {
5639 FieldAccessCallingConventionARM calling_convention;
5640 codegen_->GenerateUnresolvedFieldAccess(instruction,
5641 instruction->GetFieldType(),
5642 instruction->GetFieldIndex(),
5643 instruction->GetDexPc(),
5644 calling_convention);
5645}
5646
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005647void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005648 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5649 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005650}
5651
Calin Juravle2ae48182016-03-16 14:05:09 +00005652void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
5653 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005654 return;
5655 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005656 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005657
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005658 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005659 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005660}
5661
Calin Juravle2ae48182016-03-16 14:05:09 +00005662void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01005663 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005664 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005665
5666 LocationSummary* locations = instruction->GetLocations();
5667 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005668
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005669 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005670}
5671
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005672void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005673 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005674}
5675
Artem Serov6c916792016-07-11 14:02:34 +01005676static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5677 switch (type) {
5678 case Primitive::kPrimNot:
5679 return kLoadWord;
5680 case Primitive::kPrimBoolean:
5681 return kLoadUnsignedByte;
5682 case Primitive::kPrimByte:
5683 return kLoadSignedByte;
5684 case Primitive::kPrimChar:
5685 return kLoadUnsignedHalfword;
5686 case Primitive::kPrimShort:
5687 return kLoadSignedHalfword;
5688 case Primitive::kPrimInt:
5689 return kLoadWord;
5690 case Primitive::kPrimLong:
5691 return kLoadWordPair;
5692 case Primitive::kPrimFloat:
5693 return kLoadSWord;
5694 case Primitive::kPrimDouble:
5695 return kLoadDWord;
5696 default:
5697 LOG(FATAL) << "Unreachable type " << type;
5698 UNREACHABLE();
5699 }
5700}
5701
5702static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5703 switch (type) {
5704 case Primitive::kPrimNot:
5705 return kStoreWord;
5706 case Primitive::kPrimBoolean:
5707 case Primitive::kPrimByte:
5708 return kStoreByte;
5709 case Primitive::kPrimChar:
5710 case Primitive::kPrimShort:
5711 return kStoreHalfword;
5712 case Primitive::kPrimInt:
5713 return kStoreWord;
5714 case Primitive::kPrimLong:
5715 return kStoreWordPair;
5716 case Primitive::kPrimFloat:
5717 return kStoreSWord;
5718 case Primitive::kPrimDouble:
5719 return kStoreDWord;
5720 default:
5721 LOG(FATAL) << "Unreachable type " << type;
5722 UNREACHABLE();
5723 }
5724}
5725
5726void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
5727 Location out_loc,
5728 Register base,
5729 Register reg_offset,
5730 Condition cond) {
5731 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5732 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5733
5734 switch (type) {
5735 case Primitive::kPrimByte:
5736 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
5737 break;
5738 case Primitive::kPrimBoolean:
5739 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
5740 break;
5741 case Primitive::kPrimShort:
5742 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
5743 break;
5744 case Primitive::kPrimChar:
5745 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
5746 break;
5747 case Primitive::kPrimNot:
5748 case Primitive::kPrimInt:
5749 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
5750 break;
5751 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5752 case Primitive::kPrimLong:
5753 case Primitive::kPrimFloat:
5754 case Primitive::kPrimDouble:
5755 default:
5756 LOG(FATAL) << "Unreachable type " << type;
5757 UNREACHABLE();
5758 }
5759}
5760
5761void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
5762 Location loc,
5763 Register base,
5764 Register reg_offset,
5765 Condition cond) {
5766 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5767 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5768
5769 switch (type) {
5770 case Primitive::kPrimByte:
5771 case Primitive::kPrimBoolean:
5772 __ strb(loc.AsRegister<Register>(), mem_address, cond);
5773 break;
5774 case Primitive::kPrimShort:
5775 case Primitive::kPrimChar:
5776 __ strh(loc.AsRegister<Register>(), mem_address, cond);
5777 break;
5778 case Primitive::kPrimNot:
5779 case Primitive::kPrimInt:
5780 __ str(loc.AsRegister<Register>(), mem_address, cond);
5781 break;
5782 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5783 case Primitive::kPrimLong:
5784 case Primitive::kPrimFloat:
5785 case Primitive::kPrimDouble:
5786 default:
5787 LOG(FATAL) << "Unreachable type " << type;
5788 UNREACHABLE();
5789 }
5790}
5791
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005792void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005793 bool object_array_get_with_read_barrier =
5794 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005795 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005796 new (GetGraph()->GetArena()) LocationSummary(instruction,
5797 object_array_get_with_read_barrier ?
5798 LocationSummary::kCallOnSlowPath :
5799 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005800 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005801 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005802 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005803 locations->SetInAt(0, Location::RequiresRegister());
5804 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005805 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5806 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5807 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005808 // The output overlaps in the case of an object array get with
5809 // read barriers enabled: we do not want the move to overwrite the
5810 // array's location, as we need it to emit the read barrier.
5811 locations->SetOut(
5812 Location::RequiresRegister(),
5813 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005814 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005815 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5816 // We need a temporary register for the read barrier marking slow
5817 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
5818 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5819 !Runtime::Current()->UseJitCompilation() &&
5820 instruction->GetIndex()->IsConstant()) {
5821 // Array loads with constant index are treated as field loads.
5822 // If link-time thunks for the Baker read barrier are enabled, for AOT
5823 // constant index loads we need a temporary only if the offset is too big.
5824 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
5825 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
5826 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
5827 if (offset >= kReferenceLoadMinFarOffset) {
5828 locations->AddTemp(Location::RequiresRegister());
5829 }
5830 // And we always need the reserved entrypoint register.
5831 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5832 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
5833 !Runtime::Current()->UseJitCompilation() &&
5834 !instruction->GetIndex()->IsConstant()) {
5835 // We need a non-scratch temporary for the array data pointer.
5836 locations->AddTemp(Location::RequiresRegister());
5837 // And we always need the reserved entrypoint register.
5838 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5839 } else {
5840 locations->AddTemp(Location::RequiresRegister());
5841 }
5842 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5843 // Also need a temporary for String compression feature.
Roland Levillainc9285912015-12-18 10:38:42 +00005844 locations->AddTemp(Location::RequiresRegister());
5845 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005846}
5847
5848void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
5849 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005850 Location obj_loc = locations->InAt(0);
5851 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005852 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00005853 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005854 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00005855 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07005856 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5857 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01005858 HInstruction* array_instr = instruction->GetArray();
5859 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01005860
Roland Levillain4d027112015-07-01 15:41:14 +01005861 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01005862 case Primitive::kPrimBoolean:
5863 case Primitive::kPrimByte:
5864 case Primitive::kPrimShort:
5865 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00005866 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005867 Register length;
5868 if (maybe_compressed_char_at) {
5869 length = locations->GetTemp(0).AsRegister<Register>();
5870 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5871 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
5872 codegen_->MaybeRecordImplicitNullCheck(instruction);
5873 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005874 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005875 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07005876 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07005877 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005878 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005879 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5880 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5881 "Expecting 0=compressed, 1=uncompressed");
5882 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005883 __ LoadFromOffset(kLoadUnsignedByte,
5884 out_loc.AsRegister<Register>(),
5885 obj,
5886 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00005887 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07005888 __ Bind(&uncompressed_load);
5889 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5890 out_loc.AsRegister<Register>(),
5891 obj,
5892 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005893 if (done.IsLinked()) {
5894 __ Bind(&done);
5895 }
jessicahandojo05765752016-09-09 19:01:32 -07005896 } else {
5897 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01005898
jessicahandojo05765752016-09-09 19:01:32 -07005899 LoadOperandType load_type = GetLoadOperandType(type);
5900 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
5901 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005902 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005903 Register temp = IP;
5904
5905 if (has_intermediate_address) {
5906 // We do not need to compute the intermediate address from the array: the
5907 // input instruction has done it already. See the comment in
5908 // `TryExtractArrayAccessAddress()`.
5909 if (kIsDebugBuild) {
5910 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5911 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5912 }
5913 temp = obj;
5914 } else {
5915 __ add(temp, obj, ShifterOperand(data_offset));
5916 }
jessicahandojo05765752016-09-09 19:01:32 -07005917 if (maybe_compressed_char_at) {
5918 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005919 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005920 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5921 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5922 "Expecting 0=compressed, 1=uncompressed");
5923 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005924 __ ldrb(out_loc.AsRegister<Register>(),
5925 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00005926 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07005927 __ Bind(&uncompressed_load);
5928 __ ldrh(out_loc.AsRegister<Register>(),
5929 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005930 if (done.IsLinked()) {
5931 __ Bind(&done);
5932 }
jessicahandojo05765752016-09-09 19:01:32 -07005933 } else {
5934 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
5935 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005936 }
5937 break;
5938 }
5939
Roland Levillainc9285912015-12-18 10:38:42 +00005940 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00005941 // The read barrier instrumentation of object ArrayGet
5942 // instructions does not support the HIntermediateAddress
5943 // instruction.
5944 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5945
Roland Levillainc9285912015-12-18 10:38:42 +00005946 static_assert(
5947 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5948 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00005949 // /* HeapReference<Object> */ out =
5950 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5951 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5952 Location temp = locations->GetTemp(0);
5953 // Note that a potential implicit null check is handled in this
5954 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005955 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
5956 if (index.IsConstant()) {
5957 // Array load with a constant index can be treated as a field load.
5958 data_offset += helpers::Int32ConstantFrom(index) << Primitive::ComponentSizeShift(type);
5959 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5960 out_loc,
5961 obj,
5962 data_offset,
5963 locations->GetTemp(0),
5964 /* needs_null_check */ false);
5965 } else {
5966 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5967 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
5968 }
Roland Levillainc9285912015-12-18 10:38:42 +00005969 } else {
5970 Register out = out_loc.AsRegister<Register>();
5971 if (index.IsConstant()) {
5972 size_t offset =
5973 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5974 __ LoadFromOffset(kLoadWord, out, obj, offset);
5975 codegen_->MaybeRecordImplicitNullCheck(instruction);
5976 // If read barriers are enabled, emit read barriers other than
5977 // Baker's using a slow path (and also unpoison the loaded
5978 // reference, if heap poisoning is enabled).
5979 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5980 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005981 Register temp = IP;
5982
5983 if (has_intermediate_address) {
5984 // We do not need to compute the intermediate address from the array: the
5985 // input instruction has done it already. See the comment in
5986 // `TryExtractArrayAccessAddress()`.
5987 if (kIsDebugBuild) {
5988 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5989 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5990 }
5991 temp = obj;
5992 } else {
5993 __ add(temp, obj, ShifterOperand(data_offset));
5994 }
5995 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005996
Roland Levillainc9285912015-12-18 10:38:42 +00005997 codegen_->MaybeRecordImplicitNullCheck(instruction);
5998 // If read barriers are enabled, emit read barriers other than
5999 // Baker's using a slow path (and also unpoison the loaded
6000 // reference, if heap poisoning is enabled).
6001 codegen_->MaybeGenerateReadBarrierSlow(
6002 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6003 }
6004 }
6005 break;
6006 }
6007
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006008 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006009 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006010 size_t offset =
6011 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006012 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006013 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006014 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00006015 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006016 }
6017 break;
6018 }
6019
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006020 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00006021 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006022 if (index.IsConstant()) {
6023 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006024 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006025 } else {
6026 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00006027 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006028 }
6029 break;
6030 }
6031
6032 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00006033 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006034 if (index.IsConstant()) {
6035 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006036 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006037 } else {
6038 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00006039 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006040 }
6041 break;
6042 }
6043
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006044 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01006045 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006046 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006047 }
Roland Levillain4d027112015-07-01 15:41:14 +01006048
6049 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00006050 // Potential implicit null checks, in the case of reference
6051 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07006052 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00006053 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01006054 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006055}
6056
6057void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006058 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006059
6060 bool needs_write_barrier =
6061 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00006062 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006063
Nicolas Geoffray39468442014-09-02 15:17:15 +01006064 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006065 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01006066 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00006067 LocationSummary::kCallOnSlowPath :
6068 LocationSummary::kNoCall);
6069
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006070 locations->SetInAt(0, Location::RequiresRegister());
6071 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6072 if (Primitive::IsFloatingPointType(value_type)) {
6073 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006074 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006075 locations->SetInAt(2, Location::RequiresRegister());
6076 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006077 if (needs_write_barrier) {
6078 // Temporary registers for the write barrier.
6079 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006080 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006081 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006082}
6083
6084void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
6085 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006086 Location array_loc = locations->InAt(0);
6087 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006088 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006089 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00006090 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006091 bool needs_write_barrier =
6092 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01006093 uint32_t data_offset =
6094 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6095 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01006096 HInstruction* array_instr = instruction->GetArray();
6097 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006098
6099 switch (value_type) {
6100 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01006101 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006102 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01006103 case Primitive::kPrimChar:
6104 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006105 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01006106 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
6107 uint32_t full_offset =
6108 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6109 StoreOperandType store_type = GetStoreOperandType(value_type);
6110 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006111 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006112 Register temp = IP;
6113
6114 if (has_intermediate_address) {
6115 // We do not need to compute the intermediate address from the array: the
6116 // input instruction has done it already. See the comment in
6117 // `TryExtractArrayAccessAddress()`.
6118 if (kIsDebugBuild) {
6119 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6120 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
6121 }
6122 temp = array;
6123 } else {
6124 __ add(temp, array, ShifterOperand(data_offset));
6125 }
Artem Serov6c916792016-07-11 14:02:34 +01006126 codegen_->StoreToShiftedRegOffset(value_type,
6127 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01006128 temp,
Artem Serov6c916792016-07-11 14:02:34 +01006129 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006130 }
6131 break;
6132 }
6133
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006134 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00006135 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01006136 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6137 // See the comment in instruction_simplifier_shared.cc.
6138 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006139
6140 if (instruction->InputAt(2)->IsNullConstant()) {
6141 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006142 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006143 size_t offset =
6144 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01006145 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006146 } else {
6147 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006148 __ add(IP, array, ShifterOperand(data_offset));
6149 codegen_->StoreToShiftedRegOffset(value_type,
6150 value_loc,
6151 IP,
6152 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006153 }
Roland Levillain1407ee72016-01-08 15:56:19 +00006154 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00006155 DCHECK(!needs_write_barrier);
6156 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006157 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006158 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006159
6160 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01006161 Location temp1_loc = locations->GetTemp(0);
6162 Register temp1 = temp1_loc.AsRegister<Register>();
6163 Location temp2_loc = locations->GetTemp(1);
6164 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006165 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6166 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6167 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6168 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006169 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006170 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006171
Roland Levillain3b359c72015-11-17 19:35:12 +00006172 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006173 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
6174 codegen_->AddSlowPath(slow_path);
6175 if (instruction->GetValueCanBeNull()) {
6176 Label non_zero;
6177 __ CompareAndBranchIfNonZero(value, &non_zero);
6178 if (index.IsConstant()) {
6179 size_t offset =
6180 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6181 __ StoreToOffset(kStoreWord, value, array, offset);
6182 } else {
6183 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006184 __ add(IP, array, ShifterOperand(data_offset));
6185 codegen_->StoreToShiftedRegOffset(value_type,
6186 value_loc,
6187 IP,
6188 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006189 }
6190 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006191 __ b(final_label);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006192 __ Bind(&non_zero);
6193 }
6194
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006195 // Note that when read barriers are enabled, the type checks
6196 // are performed without read barriers. This is fine, even in
6197 // the case where a class object is in the from-space after
6198 // the flip, as a comparison involving such a type would not
6199 // produce a false positive; it may of course produce a false
6200 // negative, in which case we would take the ArraySet slow
6201 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006202
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006203 // /* HeapReference<Class> */ temp1 = array->klass_
6204 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
6205 codegen_->MaybeRecordImplicitNullCheck(instruction);
6206 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01006207
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006208 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6209 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6210 // /* HeapReference<Class> */ temp2 = value->klass_
6211 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
6212 // If heap poisoning is enabled, no need to unpoison `temp1`
6213 // nor `temp2`, as we are comparing two poisoned references.
6214 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01006215
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006216 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6217 Label do_put;
6218 __ b(&do_put, EQ);
6219 // If heap poisoning is enabled, the `temp1` reference has
6220 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00006221 __ MaybeUnpoisonHeapReference(temp1);
6222
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006223 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6224 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6225 // If heap poisoning is enabled, no need to unpoison
6226 // `temp1`, as we are comparing against null below.
6227 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
6228 __ Bind(&do_put);
6229 } else {
6230 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006231 }
6232 }
6233
Artem Serov6c916792016-07-11 14:02:34 +01006234 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006235 if (kPoisonHeapReferences) {
6236 // Note that in the case where `value` is a null reference,
6237 // we do not enter this block, as a null reference does not
6238 // need poisoning.
6239 DCHECK_EQ(value_type, Primitive::kPrimNot);
6240 __ Mov(temp1, value);
6241 __ PoisonHeapReference(temp1);
6242 source = temp1;
6243 }
6244
6245 if (index.IsConstant()) {
6246 size_t offset =
6247 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6248 __ StoreToOffset(kStoreWord, source, array, offset);
6249 } else {
6250 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006251
6252 __ add(IP, array, ShifterOperand(data_offset));
6253 codegen_->StoreToShiftedRegOffset(value_type,
6254 Location::RegisterLocation(source),
6255 IP,
6256 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006257 }
6258
Roland Levillain3b359c72015-11-17 19:35:12 +00006259 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006260 codegen_->MaybeRecordImplicitNullCheck(instruction);
6261 }
6262
6263 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6264
6265 if (done.IsLinked()) {
6266 __ Bind(&done);
6267 }
6268
6269 if (slow_path != nullptr) {
6270 __ Bind(slow_path->GetExitLabel());
6271 }
6272
6273 break;
6274 }
6275
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006276 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006277 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006278 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006279 size_t offset =
6280 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006281 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006282 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006283 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006284 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006285 }
6286 break;
6287 }
6288
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006289 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006290 Location value = locations->InAt(2);
6291 DCHECK(value.IsFpuRegister());
6292 if (index.IsConstant()) {
6293 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006294 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006295 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006296 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006297 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
6298 }
6299 break;
6300 }
6301
6302 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006303 Location value = locations->InAt(2);
6304 DCHECK(value.IsFpuRegisterPair());
6305 if (index.IsConstant()) {
6306 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006307 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006308 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006309 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006310 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
6311 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006312
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006313 break;
6314 }
6315
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006316 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006317 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006318 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006319 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006320
Roland Levillain80e67092016-01-08 16:04:55 +00006321 // Objects are handled in the switch.
6322 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006323 codegen_->MaybeRecordImplicitNullCheck(instruction);
6324 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006325}
6326
6327void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006328 LocationSummary* locations =
6329 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006330 locations->SetInAt(0, Location::RequiresRegister());
6331 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006332}
6333
6334void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
6335 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006336 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006337 Register obj = locations->InAt(0).AsRegister<Register>();
6338 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006339 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00006340 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07006341 // Mask out compression flag from String's array length.
6342 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006343 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07006344 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006345}
6346
Artem Serov328429f2016-07-06 16:23:04 +01006347void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01006348 LocationSummary* locations =
6349 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6350
6351 locations->SetInAt(0, Location::RequiresRegister());
6352 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6354}
6355
6356void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6357 LocationSummary* locations = instruction->GetLocations();
6358 Location out = locations->Out();
6359 Location first = locations->InAt(0);
6360 Location second = locations->InAt(1);
6361
Artem Serov328429f2016-07-06 16:23:04 +01006362 if (second.IsRegister()) {
6363 __ add(out.AsRegister<Register>(),
6364 first.AsRegister<Register>(),
6365 ShifterOperand(second.AsRegister<Register>()));
6366 } else {
6367 __ AddConstant(out.AsRegister<Register>(),
6368 first.AsRegister<Register>(),
6369 second.GetConstant()->AsIntConstant()->GetValue());
6370 }
6371}
6372
Artem Serove1811ed2017-04-27 16:50:47 +01006373void LocationsBuilderARM::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
6374 LOG(FATAL) << "Unreachable " << instruction->GetId();
6375}
6376
6377void InstructionCodeGeneratorARM::VisitIntermediateAddressIndex(
6378 HIntermediateAddressIndex* instruction) {
6379 LOG(FATAL) << "Unreachable " << instruction->GetId();
6380}
6381
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006382void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006383 RegisterSet caller_saves = RegisterSet::Empty();
6384 InvokeRuntimeCallingConvention calling_convention;
6385 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6386 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6387 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006388
6389 HInstruction* index = instruction->InputAt(0);
6390 HInstruction* length = instruction->InputAt(1);
6391 // If both index and length are constants we can statically check the bounds. But if at least one
6392 // of them is not encodable ArmEncodableConstantOrRegister will create
6393 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6394 // locations.
6395 bool both_const = index->IsConstant() && length->IsConstant();
6396 locations->SetInAt(0, both_const
6397 ? Location::ConstantLocation(index->AsConstant())
6398 : ArmEncodableConstantOrRegister(index, CMP));
6399 locations->SetInAt(1, both_const
6400 ? Location::ConstantLocation(length->AsConstant())
6401 : ArmEncodableConstantOrRegister(length, CMP));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006402}
6403
6404void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
6405 LocationSummary* locations = instruction->GetLocations();
Artem Serov2dd053d2017-03-08 14:54:06 +00006406 Location index_loc = locations->InAt(0);
6407 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006408
Artem Serov2dd053d2017-03-08 14:54:06 +00006409 if (length_loc.IsConstant()) {
6410 int32_t length = helpers::Int32ConstantFrom(length_loc);
6411 if (index_loc.IsConstant()) {
6412 // BCE will remove the bounds check if we are guaranteed to pass.
6413 int32_t index = helpers::Int32ConstantFrom(index_loc);
6414 if (index < 0 || index >= length) {
6415 SlowPathCodeARM* slow_path =
6416 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6417 codegen_->AddSlowPath(slow_path);
6418 __ b(slow_path->GetEntryLabel());
6419 } else {
6420 // Some optimization after BCE may have generated this, and we should not
6421 // generate a bounds check if it is a valid range.
6422 }
6423 return;
6424 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006425
Artem Serov2dd053d2017-03-08 14:54:06 +00006426 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6427 __ cmp(index_loc.AsRegister<Register>(), ShifterOperand(length));
6428 codegen_->AddSlowPath(slow_path);
6429 __ b(slow_path->GetEntryLabel(), HS);
6430 } else {
6431 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6432 if (index_loc.IsConstant()) {
6433 int32_t index = helpers::Int32ConstantFrom(index_loc);
6434 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index));
6435 } else {
6436 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index_loc.AsRegister<Register>()));
6437 }
6438 codegen_->AddSlowPath(slow_path);
6439 __ b(slow_path->GetEntryLabel(), LS);
6440 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006441}
6442
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006443void CodeGeneratorARM::MarkGCCard(Register temp,
6444 Register card,
6445 Register object,
6446 Register value,
6447 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006448 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006449 if (can_be_null) {
6450 __ CompareAndBranchIfZero(value, &is_null);
6451 }
Andreas Gampe542451c2016-07-26 09:02:02 -07006452 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006453 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
6454 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006455 if (can_be_null) {
6456 __ Bind(&is_null);
6457 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006458}
6459
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006460void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006461 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006462}
6463
6464void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006465 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6466}
6467
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006468void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006469 LocationSummary* locations =
6470 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006471 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006472}
6473
6474void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006475 HBasicBlock* block = instruction->GetBlock();
6476 if (block->GetLoopInformation() != nullptr) {
6477 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6478 // The back edge will generate the suspend check.
6479 return;
6480 }
6481 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6482 // The goto will generate the suspend check.
6483 return;
6484 }
6485 GenerateSuspendCheck(instruction, nullptr);
6486}
6487
6488void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
6489 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006490 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006491 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
6492 if (slow_path == nullptr) {
6493 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
6494 instruction->SetSlowPath(slow_path);
6495 codegen_->AddSlowPath(slow_path);
6496 if (successor != nullptr) {
6497 DCHECK(successor->IsLoopHeader());
6498 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6499 }
6500 } else {
6501 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6502 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006503
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00006504 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006505 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006506 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006507 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006508 __ Bind(slow_path->GetReturnLabel());
6509 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006510 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006511 __ b(slow_path->GetEntryLabel());
6512 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006513}
6514
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006515ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
6516 return codegen_->GetAssembler();
6517}
6518
6519void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006520 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006521 Location source = move->GetSource();
6522 Location destination = move->GetDestination();
6523
6524 if (source.IsRegister()) {
6525 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006526 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006527 } else if (destination.IsFpuRegister()) {
6528 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006529 } else {
6530 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006531 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006532 SP, destination.GetStackIndex());
6533 }
6534 } else if (source.IsStackSlot()) {
6535 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006536 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006537 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006538 } else if (destination.IsFpuRegister()) {
6539 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006540 } else {
6541 DCHECK(destination.IsStackSlot());
6542 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
6543 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6544 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006545 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006546 if (destination.IsRegister()) {
6547 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
6548 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006549 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006550 } else {
6551 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006552 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
6553 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006554 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006555 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006556 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
6557 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006558 } else if (destination.IsRegisterPair()) {
6559 DCHECK(ExpectedPairLayout(destination));
6560 __ LoadFromOffset(
6561 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
6562 } else {
6563 DCHECK(destination.IsFpuRegisterPair()) << destination;
6564 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6565 SP,
6566 source.GetStackIndex());
6567 }
6568 } else if (source.IsRegisterPair()) {
6569 if (destination.IsRegisterPair()) {
6570 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6571 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006572 } else if (destination.IsFpuRegisterPair()) {
6573 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6574 source.AsRegisterPairLow<Register>(),
6575 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006576 } else {
6577 DCHECK(destination.IsDoubleStackSlot()) << destination;
6578 DCHECK(ExpectedPairLayout(source));
6579 __ StoreToOffset(
6580 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
6581 }
6582 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006583 if (destination.IsRegisterPair()) {
6584 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6585 destination.AsRegisterPairHigh<Register>(),
6586 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6587 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006588 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6589 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6590 } else {
6591 DCHECK(destination.IsDoubleStackSlot()) << destination;
6592 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
6593 SP,
6594 destination.GetStackIndex());
6595 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006596 } else {
6597 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006598 HConstant* constant = source.GetConstant();
6599 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6600 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006601 if (destination.IsRegister()) {
6602 __ LoadImmediate(destination.AsRegister<Register>(), value);
6603 } else {
6604 DCHECK(destination.IsStackSlot());
6605 __ LoadImmediate(IP, value);
6606 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6607 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006608 } else if (constant->IsLongConstant()) {
6609 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006610 if (destination.IsRegisterPair()) {
6611 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
6612 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006613 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006614 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006615 __ LoadImmediate(IP, Low32Bits(value));
6616 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6617 __ LoadImmediate(IP, High32Bits(value));
6618 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6619 }
6620 } else if (constant->IsDoubleConstant()) {
6621 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006622 if (destination.IsFpuRegisterPair()) {
6623 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006624 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006625 DCHECK(destination.IsDoubleStackSlot()) << destination;
6626 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006627 __ LoadImmediate(IP, Low32Bits(int_value));
6628 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6629 __ LoadImmediate(IP, High32Bits(int_value));
6630 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6631 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006632 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006633 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006634 float value = constant->AsFloatConstant()->GetValue();
6635 if (destination.IsFpuRegister()) {
6636 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
6637 } else {
6638 DCHECK(destination.IsStackSlot());
6639 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
6640 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6641 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006642 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006643 }
6644}
6645
6646void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
6647 __ Mov(IP, reg);
6648 __ LoadFromOffset(kLoadWord, reg, SP, mem);
6649 __ StoreToOffset(kStoreWord, IP, SP, mem);
6650}
6651
6652void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
6653 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
6654 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6655 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
6656 SP, mem1 + stack_offset);
6657 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
6658 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
6659 SP, mem2 + stack_offset);
6660 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
6661}
6662
6663void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006664 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006665 Location source = move->GetSource();
6666 Location destination = move->GetDestination();
6667
6668 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006669 DCHECK_NE(source.AsRegister<Register>(), IP);
6670 DCHECK_NE(destination.AsRegister<Register>(), IP);
6671 __ Mov(IP, source.AsRegister<Register>());
6672 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
6673 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006674 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006675 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006676 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006677 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006678 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
6679 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006680 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006681 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006682 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006683 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006684 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006685 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006686 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006687 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006688 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6689 destination.AsRegisterPairHigh<Register>(),
6690 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006691 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006692 Register low_reg = source.IsRegisterPair()
6693 ? source.AsRegisterPairLow<Register>()
6694 : destination.AsRegisterPairLow<Register>();
6695 int mem = source.IsRegisterPair()
6696 ? destination.GetStackIndex()
6697 : source.GetStackIndex();
6698 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006699 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006700 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006701 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006702 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006703 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
6704 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006705 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006706 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006707 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006708 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
6709 DRegister reg = source.IsFpuRegisterPair()
6710 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
6711 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
6712 int mem = source.IsFpuRegisterPair()
6713 ? destination.GetStackIndex()
6714 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006715 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006716 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006717 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006718 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
6719 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
6720 : destination.AsFpuRegister<SRegister>();
6721 int mem = source.IsFpuRegister()
6722 ? destination.GetStackIndex()
6723 : source.GetStackIndex();
6724
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006725 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006726 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006727 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006728 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006729 Exchange(source.GetStackIndex(), destination.GetStackIndex());
6730 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006731 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006732 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006733 }
6734}
6735
6736void ParallelMoveResolverARM::SpillScratch(int reg) {
6737 __ Push(static_cast<Register>(reg));
6738}
6739
6740void ParallelMoveResolverARM::RestoreScratch(int reg) {
6741 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006742}
6743
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006744HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
6745 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006746 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006747 case HLoadClass::LoadKind::kInvalid:
6748 LOG(FATAL) << "UNREACHABLE";
6749 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006750 case HLoadClass::LoadKind::kReferrersClass:
6751 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006752 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006753 case HLoadClass::LoadKind::kBssEntry:
6754 DCHECK(!Runtime::Current()->UseJitCompilation());
6755 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006756 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006757 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006758 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006759 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006760 case HLoadClass::LoadKind::kDexCacheViaMethod:
6761 break;
6762 }
6763 return desired_class_load_kind;
6764}
6765
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006766void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006767 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6768 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006769 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006770 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006771 cls,
6772 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006773 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006774 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006775 return;
6776 }
Vladimir Marko41559982017-01-06 14:04:23 +00006777 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006778
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006779 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6780 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006781 ? LocationSummary::kCallOnSlowPath
6782 : LocationSummary::kNoCall;
6783 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006784 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006785 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006786 }
6787
Vladimir Marko41559982017-01-06 14:04:23 +00006788 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006789 locations->SetInAt(0, Location::RequiresRegister());
6790 }
6791 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006792 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6793 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6794 // Rely on the type resolution or initialization and marking to save everything we need.
6795 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6796 // to the custom calling convention) or by marking, so we request a different temp.
6797 locations->AddTemp(Location::RequiresRegister());
6798 RegisterSet caller_saves = RegisterSet::Empty();
6799 InvokeRuntimeCallingConvention calling_convention;
6800 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6801 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6802 // that the the kPrimNot result register is the same as the first argument register.
6803 locations->SetCustomSlowPathCallerSaves(caller_saves);
6804 } else {
6805 // For non-Baker read barrier we have a temp-clobbering call.
6806 }
6807 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006808 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
6809 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
6810 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
6811 !Runtime::Current()->UseJitCompilation())) {
6812 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6813 }
6814 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006815}
6816
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006817// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6818// move.
6819void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006820 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6821 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6822 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006823 return;
6824 }
Vladimir Marko41559982017-01-06 14:04:23 +00006825 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006826
Vladimir Marko41559982017-01-06 14:04:23 +00006827 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006828 Location out_loc = locations->Out();
6829 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006830
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006831 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6832 ? kWithoutReadBarrier
6833 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006834 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006835 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006836 case HLoadClass::LoadKind::kReferrersClass: {
6837 DCHECK(!cls->CanCallRuntime());
6838 DCHECK(!cls->MustGenerateClinitCheck());
6839 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6840 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006841 GenerateGcRootFieldLoad(cls,
6842 out_loc,
6843 current_method,
6844 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006845 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006846 break;
6847 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006848 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006849 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006850 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006851 CodeGeneratorARM::PcRelativePatchInfo* labels =
6852 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6853 __ BindTrackedLabel(&labels->movw_label);
6854 __ movw(out, /* placeholder */ 0u);
6855 __ BindTrackedLabel(&labels->movt_label);
6856 __ movt(out, /* placeholder */ 0u);
6857 __ BindTrackedLabel(&labels->add_pc_label);
6858 __ add(out, out, ShifterOperand(PC));
6859 break;
6860 }
6861 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006862 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006863 uint32_t address = dchecked_integral_cast<uint32_t>(
6864 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6865 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006866 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6867 break;
6868 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006869 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006870 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6871 ? locations->GetTemp(0).AsRegister<Register>()
6872 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006873 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006874 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006875 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006876 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006877 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006878 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006879 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006880 __ add(temp, temp, ShifterOperand(PC));
6881 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006882 generate_null_check = true;
6883 break;
6884 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006885 case HLoadClass::LoadKind::kJitTableAddress: {
6886 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6887 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006888 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006889 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006890 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006891 break;
6892 }
Vladimir Marko41559982017-01-06 14:04:23 +00006893 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006894 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006895 LOG(FATAL) << "UNREACHABLE";
6896 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006897 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006898
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006899 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6900 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01006901 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006902 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6903 codegen_->AddSlowPath(slow_path);
6904 if (generate_null_check) {
6905 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6906 }
6907 if (cls->MustGenerateClinitCheck()) {
6908 GenerateClassInitializationCheck(slow_path, out);
6909 } else {
6910 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006911 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006912 }
6913}
6914
6915void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
6916 LocationSummary* locations =
6917 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6918 locations->SetInAt(0, Location::RequiresRegister());
6919 if (check->HasUses()) {
6920 locations->SetOut(Location::SameAsFirstInput());
6921 }
6922}
6923
6924void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006925 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006926 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006927 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006928 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006929 GenerateClassInitializationCheck(slow_path,
6930 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006931}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006932
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006933void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01006934 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006935 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
6936 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
6937 __ b(slow_path->GetEntryLabel(), LT);
6938 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6939 // properly. Therefore, we do a memory fence.
6940 __ dmb(ISH);
6941 __ Bind(slow_path->GetExitLabel());
6942}
6943
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006944HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
6945 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006946 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006947 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00006948 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006949 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006950 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006951 case HLoadString::LoadKind::kJitTableAddress:
6952 DCHECK(Runtime::Current()->UseJitCompilation());
6953 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01006954 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006955 case HLoadString::LoadKind::kDexCacheViaMethod:
6956 break;
6957 }
6958 return desired_string_load_kind;
6959}
6960
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006961void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006962 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006963 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006964 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006965 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006966 locations->SetOut(Location::RegisterLocation(R0));
6967 } else {
6968 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006969 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6970 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006971 // Rely on the pResolveString and marking to save everything we need, including temps.
6972 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6973 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006974 locations->AddTemp(Location::RequiresRegister());
6975 RegisterSet caller_saves = RegisterSet::Empty();
6976 InvokeRuntimeCallingConvention calling_convention;
6977 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6978 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6979 // that the the kPrimNot result register is the same as the first argument register.
6980 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006981 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
6982 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6983 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006984 } else {
6985 // For non-Baker read barrier we have a temp-clobbering call.
6986 }
6987 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006988 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006989}
6990
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006991// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6992// move.
6993void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006994 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006995 Location out_loc = locations->Out();
6996 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006997 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00006998
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006999 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007000 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007001 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007002 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007003 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007004 __ BindTrackedLabel(&labels->movw_label);
7005 __ movw(out, /* placeholder */ 0u);
7006 __ BindTrackedLabel(&labels->movt_label);
7007 __ movt(out, /* placeholder */ 0u);
7008 __ BindTrackedLabel(&labels->add_pc_label);
7009 __ add(out, out, ShifterOperand(PC));
7010 return; // No dex cache slow path.
7011 }
7012 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007013 uint32_t address = dchecked_integral_cast<uint32_t>(
7014 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7015 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007016 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7017 return; // No dex cache slow path.
7018 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007019 case HLoadString::LoadKind::kBssEntry: {
7020 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007021 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7022 ? locations->GetTemp(0).AsRegister<Register>()
7023 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00007024 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007025 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007026 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007027 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007028 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007029 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007030 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007031 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007032 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007033 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
7034 codegen_->AddSlowPath(slow_path);
7035 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7036 __ Bind(slow_path->GetExitLabel());
7037 return;
7038 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007039 case HLoadString::LoadKind::kJitTableAddress: {
7040 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007041 load->GetStringIndex(),
7042 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007043 // /* GcRoot<mirror::String> */ out = *out
7044 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7045 return;
7046 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007047 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007048 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007049 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007050
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007051 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
7052 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
7053 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007054 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007055 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007056 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7057 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007058}
7059
David Brazdilcb1c0552015-08-04 16:22:25 +01007060static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007061 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01007062}
7063
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007064void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
7065 LocationSummary* locations =
7066 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7067 locations->SetOut(Location::RequiresRegister());
7068}
7069
7070void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007071 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01007072 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7073}
7074
7075void LocationsBuilderARM::VisitClearException(HClearException* clear) {
7076 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7077}
7078
7079void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007080 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01007081 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007082}
7083
7084void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
7085 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007086 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007087 InvokeRuntimeCallingConvention calling_convention;
7088 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7089}
7090
7091void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007092 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007093 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007094}
7095
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007096// Temp is used for read barrier.
7097static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7098 if (kEmitCompilerReadBarrier &&
7099 (kUseBakerReadBarrier ||
7100 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7101 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7102 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7103 return 1;
7104 }
7105 return 0;
7106}
7107
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007108// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007109// interface pointer, one for loading the current interface.
7110// The other checks have one temp for loading the object's class.
7111static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7112 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7113 return 3;
7114 }
7115 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00007116}
7117
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007118void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007119 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00007120 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007121 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00007122 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007123 case TypeCheckKind::kExactCheck:
7124 case TypeCheckKind::kAbstractClassCheck:
7125 case TypeCheckKind::kClassHierarchyCheck:
7126 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007127 call_kind =
7128 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01007129 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007130 break;
7131 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007132 case TypeCheckKind::kUnresolvedCheck:
7133 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007134 call_kind = LocationSummary::kCallOnSlowPath;
7135 break;
7136 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007137
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007138 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007139 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007140 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007141 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007142 locations->SetInAt(0, Location::RequiresRegister());
7143 locations->SetInAt(1, Location::RequiresRegister());
7144 // The "out" register is used as a temporary, so it overlaps with the inputs.
7145 // Note that TypeCheckSlowPathARM uses this register too.
7146 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007147 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007148 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7149 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7150 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007151}
7152
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007153void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007154 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007155 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007156 Location obj_loc = locations->InAt(0);
7157 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007158 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007159 Location out_loc = locations->Out();
7160 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007161 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7162 DCHECK_LE(num_temps, 1u);
7163 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007164 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007165 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7166 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7167 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007168 Label done;
7169 Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01007170 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007171
7172 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007173 // avoid null check if we know obj is not null.
7174 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007175 DCHECK_NE(out, obj);
7176 __ LoadImmediate(out, 0);
7177 __ CompareAndBranchIfZero(obj, final_label);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007178 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007179
Roland Levillainc9285912015-12-18 10:38:42 +00007180 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007181 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007182 // /* HeapReference<Class> */ out = obj->klass_
7183 GenerateReferenceLoadTwoRegisters(instruction,
7184 out_loc,
7185 obj_loc,
7186 class_offset,
7187 maybe_temp_loc,
7188 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007189 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007190 __ cmp(out, ShifterOperand(cls));
7191 // We speculatively set the result to false without changing the condition
7192 // flags, which allows us to avoid some branching later.
7193 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7194
7195 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7196 // we check that the output is in a low register, so that a 16-bit MOV
7197 // encoding can be used.
7198 if (ArmAssembler::IsLowRegister(out)) {
7199 __ it(EQ);
7200 __ mov(out, ShifterOperand(1), EQ);
7201 } else {
7202 __ b(final_label, NE);
7203 __ LoadImmediate(out, 1);
7204 }
7205
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007206 break;
7207 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007208
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007209 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007210 // /* HeapReference<Class> */ out = obj->klass_
7211 GenerateReferenceLoadTwoRegisters(instruction,
7212 out_loc,
7213 obj_loc,
7214 class_offset,
7215 maybe_temp_loc,
7216 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007217 // If the class is abstract, we eagerly fetch the super class of the
7218 // object to avoid doing a comparison we know will fail.
7219 Label loop;
7220 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007221 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007222 GenerateReferenceLoadOneRegister(instruction,
7223 out_loc,
7224 super_offset,
7225 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007226 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007227 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007228 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007229 __ cmp(out, ShifterOperand(cls));
7230 __ b(&loop, NE);
7231 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007232 break;
7233 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007234
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007235 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007236 // /* HeapReference<Class> */ out = obj->klass_
7237 GenerateReferenceLoadTwoRegisters(instruction,
7238 out_loc,
7239 obj_loc,
7240 class_offset,
7241 maybe_temp_loc,
7242 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007243 // Walk over the class hierarchy to find a match.
7244 Label loop, success;
7245 __ Bind(&loop);
7246 __ cmp(out, ShifterOperand(cls));
7247 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007248 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007249 GenerateReferenceLoadOneRegister(instruction,
7250 out_loc,
7251 super_offset,
7252 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007253 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007254 // This is essentially a null check, but it sets the condition flags to the
7255 // proper value for the code that follows the loop, i.e. not `EQ`.
7256 __ cmp(out, ShifterOperand(1));
7257 __ b(&loop, HS);
7258
7259 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7260 // we check that the output is in a low register, so that a 16-bit MOV
7261 // encoding can be used.
7262 if (ArmAssembler::IsLowRegister(out)) {
7263 // If `out` is null, we use it for the result, and the condition flags
7264 // have already been set to `NE`, so the IT block that comes afterwards
7265 // (and which handles the successful case) turns into a NOP (instead of
7266 // overwriting `out`).
7267 __ Bind(&success);
7268 // There is only one branch to the `success` label (which is bound to this
7269 // IT block), and it has the same condition, `EQ`, so in that case the MOV
7270 // is executed.
7271 __ it(EQ);
7272 __ mov(out, ShifterOperand(1), EQ);
7273 } else {
7274 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007275 __ b(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007276 __ Bind(&success);
7277 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007278 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007279
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007280 break;
7281 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007282
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007283 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007284 // /* HeapReference<Class> */ out = obj->klass_
7285 GenerateReferenceLoadTwoRegisters(instruction,
7286 out_loc,
7287 obj_loc,
7288 class_offset,
7289 maybe_temp_loc,
7290 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007291 // Do an exact check.
7292 Label exact_check;
7293 __ cmp(out, ShifterOperand(cls));
7294 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007295 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007296 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007297 GenerateReferenceLoadOneRegister(instruction,
7298 out_loc,
7299 component_offset,
7300 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007301 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007302 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007303 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007304 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7305 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007306 __ cmp(out, ShifterOperand(0));
7307 // We speculatively set the result to false without changing the condition
7308 // flags, which allows us to avoid some branching later.
7309 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7310
7311 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7312 // we check that the output is in a low register, so that a 16-bit MOV
7313 // encoding can be used.
7314 if (ArmAssembler::IsLowRegister(out)) {
7315 __ Bind(&exact_check);
7316 __ it(EQ);
7317 __ mov(out, ShifterOperand(1), EQ);
7318 } else {
7319 __ b(final_label, NE);
7320 __ Bind(&exact_check);
7321 __ LoadImmediate(out, 1);
7322 }
7323
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007324 break;
7325 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007326
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007327 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007328 // No read barrier since the slow path will retry upon failure.
7329 // /* HeapReference<Class> */ out = obj->klass_
7330 GenerateReferenceLoadTwoRegisters(instruction,
7331 out_loc,
7332 obj_loc,
7333 class_offset,
7334 maybe_temp_loc,
7335 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007336 __ cmp(out, ShifterOperand(cls));
7337 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00007338 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7339 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007340 codegen_->AddSlowPath(slow_path);
7341 __ b(slow_path->GetEntryLabel(), NE);
7342 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007343 break;
7344 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007345
Calin Juravle98893e12015-10-02 21:05:03 +01007346 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007347 case TypeCheckKind::kInterfaceCheck: {
7348 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007349 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00007350 // cases.
7351 //
7352 // We cannot directly call the InstanceofNonTrivial runtime
7353 // entry point without resorting to a type checking slow path
7354 // here (i.e. by calling InvokeRuntime directly), as it would
7355 // require to assign fixed registers for the inputs of this
7356 // HInstanceOf instruction (following the runtime calling
7357 // convention), which might be cluttered by the potential first
7358 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00007359 //
7360 // TODO: Introduce a new runtime entry point taking the object
7361 // to test (instead of its class) as argument, and let it deal
7362 // with the read barrier issues. This will let us refactor this
7363 // case of the `switch` code as it was previously (with a direct
7364 // call to the runtime not using a type checking slow path).
7365 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00007366 DCHECK(locations->OnlyCallsOnSlowPath());
7367 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7368 /* is_fatal */ false);
7369 codegen_->AddSlowPath(slow_path);
7370 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007371 break;
7372 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007373 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007374
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007375 if (done.IsLinked()) {
7376 __ Bind(&done);
7377 }
7378
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007379 if (slow_path != nullptr) {
7380 __ Bind(slow_path->GetExitLabel());
7381 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007382}
7383
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007384void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007385 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7386 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7387
Roland Levillain3b359c72015-11-17 19:35:12 +00007388 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7389 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007390 case TypeCheckKind::kExactCheck:
7391 case TypeCheckKind::kAbstractClassCheck:
7392 case TypeCheckKind::kClassHierarchyCheck:
7393 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007394 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7395 LocationSummary::kCallOnSlowPath :
7396 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007397 break;
7398 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007399 case TypeCheckKind::kUnresolvedCheck:
7400 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007401 call_kind = LocationSummary::kCallOnSlowPath;
7402 break;
7403 }
7404
Roland Levillain3b359c72015-11-17 19:35:12 +00007405 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7406 locations->SetInAt(0, Location::RequiresRegister());
7407 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007408 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007409}
7410
7411void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007412 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007413 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007414 Location obj_loc = locations->InAt(0);
7415 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007416 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007417 Location temp_loc = locations->GetTemp(0);
7418 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007419 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7420 DCHECK_LE(num_temps, 3u);
7421 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7422 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7423 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7424 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7425 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7426 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7427 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7428 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7429 const uint32_t object_array_data_offset =
7430 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007431
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007432 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7433 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7434 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007435 bool is_type_check_slow_path_fatal = false;
7436 if (!kEmitCompilerReadBarrier) {
7437 is_type_check_slow_path_fatal =
7438 (type_check_kind == TypeCheckKind::kExactCheck ||
7439 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7440 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7441 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7442 !instruction->CanThrowIntoCatchBlock();
7443 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01007444 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007445 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7446 is_type_check_slow_path_fatal);
7447 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007448
7449 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007450 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007451 // Avoid null check if we know obj is not null.
7452 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007453 __ CompareAndBranchIfZero(obj, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007454 }
7455
Roland Levillain3b359c72015-11-17 19:35:12 +00007456 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007457 case TypeCheckKind::kExactCheck:
7458 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007459 // /* HeapReference<Class> */ temp = obj->klass_
7460 GenerateReferenceLoadTwoRegisters(instruction,
7461 temp_loc,
7462 obj_loc,
7463 class_offset,
7464 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007465 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007466
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007467 __ cmp(temp, ShifterOperand(cls));
7468 // Jump to slow path for throwing the exception or doing a
7469 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00007470 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007471 break;
7472 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007473
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007474 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007475 // /* HeapReference<Class> */ temp = obj->klass_
7476 GenerateReferenceLoadTwoRegisters(instruction,
7477 temp_loc,
7478 obj_loc,
7479 class_offset,
7480 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007481 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007482
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007483 // If the class is abstract, we eagerly fetch the super class of the
7484 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007485 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007486 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007487 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007488 GenerateReferenceLoadOneRegister(instruction,
7489 temp_loc,
7490 super_offset,
7491 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007492 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007493
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007494 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7495 // exception.
7496 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00007497
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007498 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007499 __ cmp(temp, ShifterOperand(cls));
7500 __ b(&loop, NE);
7501 break;
7502 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007503
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007504 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007505 // /* HeapReference<Class> */ temp = obj->klass_
7506 GenerateReferenceLoadTwoRegisters(instruction,
7507 temp_loc,
7508 obj_loc,
7509 class_offset,
7510 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007511 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007512
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007513 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007514 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007515 __ Bind(&loop);
7516 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007517 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007518
Roland Levillain3b359c72015-11-17 19:35:12 +00007519 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007520 GenerateReferenceLoadOneRegister(instruction,
7521 temp_loc,
7522 super_offset,
7523 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007524 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007525
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007526 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7527 // exception.
7528 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7529 // Otherwise, jump to the beginning of the loop.
7530 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007531 break;
7532 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007533
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007534 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007535 // /* HeapReference<Class> */ temp = obj->klass_
7536 GenerateReferenceLoadTwoRegisters(instruction,
7537 temp_loc,
7538 obj_loc,
7539 class_offset,
7540 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007541 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007542
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007543 // Do an exact check.
7544 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007545 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007546
7547 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007548 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007549 GenerateReferenceLoadOneRegister(instruction,
7550 temp_loc,
7551 component_offset,
7552 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007553 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007554 // If the component type is null, jump to the slow path to throw the exception.
7555 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7556 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7557 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007558 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00007559 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007560 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007561 break;
7562 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007563
Calin Juravle98893e12015-10-02 21:05:03 +01007564 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007565 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00007566 // We cannot directly call the CheckCast runtime entry point
7567 // without resorting to a type checking slow path here (i.e. by
7568 // calling InvokeRuntime directly), as it would require to
7569 // assign fixed registers for the inputs of this HInstanceOf
7570 // instruction (following the runtime calling convention), which
7571 // might be cluttered by the potential first read barrier
7572 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007573
Roland Levillain3b359c72015-11-17 19:35:12 +00007574 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007575 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007576
7577 case TypeCheckKind::kInterfaceCheck: {
7578 // Avoid read barriers to improve performance of the fast path. We can not get false
7579 // positives by doing this.
7580 // /* HeapReference<Class> */ temp = obj->klass_
7581 GenerateReferenceLoadTwoRegisters(instruction,
7582 temp_loc,
7583 obj_loc,
7584 class_offset,
7585 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007586 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007587
7588 // /* HeapReference<Class> */ temp = temp->iftable_
7589 GenerateReferenceLoadTwoRegisters(instruction,
7590 temp_loc,
7591 temp_loc,
7592 iftable_offset,
7593 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007594 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007595 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007596 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08007597 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007598 Label start_loop;
7599 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007600 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
7601 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007602 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
7603 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007604 // Go to next interface.
7605 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
7606 __ sub(maybe_temp2_loc.AsRegister<Register>(),
7607 maybe_temp2_loc.AsRegister<Register>(),
7608 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007609 // Compare the classes and continue the loop if they do not match.
7610 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
7611 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007612 break;
7613 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007614 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007615
7616 if (done.IsLinked()) {
7617 __ Bind(&done);
7618 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007619
Roland Levillain3b359c72015-11-17 19:35:12 +00007620 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007621}
7622
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007623void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
7624 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007625 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007626 InvokeRuntimeCallingConvention calling_convention;
7627 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7628}
7629
7630void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007631 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7632 instruction,
7633 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007634 if (instruction->IsEnter()) {
7635 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7636 } else {
7637 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7638 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007639}
7640
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007641void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
7642void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
7643void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007644
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007645void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007646 LocationSummary* locations =
7647 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7648 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7649 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007650 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007651 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007652 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00007653 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007654}
7655
7656void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
7657 HandleBitwiseOperation(instruction);
7658}
7659
7660void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
7661 HandleBitwiseOperation(instruction);
7662}
7663
7664void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
7665 HandleBitwiseOperation(instruction);
7666}
7667
Artem Serov7fc63502016-02-09 17:15:29 +00007668
7669void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7670 LocationSummary* locations =
7671 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7672 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7673 || instruction->GetResultType() == Primitive::kPrimLong);
7674
7675 locations->SetInAt(0, Location::RequiresRegister());
7676 locations->SetInAt(1, Location::RequiresRegister());
7677 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7678}
7679
7680void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7681 LocationSummary* locations = instruction->GetLocations();
7682 Location first = locations->InAt(0);
7683 Location second = locations->InAt(1);
7684 Location out = locations->Out();
7685
7686 if (instruction->GetResultType() == Primitive::kPrimInt) {
7687 Register first_reg = first.AsRegister<Register>();
7688 ShifterOperand second_reg(second.AsRegister<Register>());
7689 Register out_reg = out.AsRegister<Register>();
7690
7691 switch (instruction->GetOpKind()) {
7692 case HInstruction::kAnd:
7693 __ bic(out_reg, first_reg, second_reg);
7694 break;
7695 case HInstruction::kOr:
7696 __ orn(out_reg, first_reg, second_reg);
7697 break;
7698 // There is no EON on arm.
7699 case HInstruction::kXor:
7700 default:
7701 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7702 UNREACHABLE();
7703 }
7704 return;
7705
7706 } else {
7707 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7708 Register first_low = first.AsRegisterPairLow<Register>();
7709 Register first_high = first.AsRegisterPairHigh<Register>();
7710 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7711 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7712 Register out_low = out.AsRegisterPairLow<Register>();
7713 Register out_high = out.AsRegisterPairHigh<Register>();
7714
7715 switch (instruction->GetOpKind()) {
7716 case HInstruction::kAnd:
7717 __ bic(out_low, first_low, second_low);
7718 __ bic(out_high, first_high, second_high);
7719 break;
7720 case HInstruction::kOr:
7721 __ orn(out_low, first_low, second_low);
7722 __ orn(out_high, first_high, second_high);
7723 break;
7724 // There is no EON on arm.
7725 case HInstruction::kXor:
7726 default:
7727 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7728 UNREACHABLE();
7729 }
7730 }
7731}
7732
Anton Kirilov74234da2017-01-13 14:42:47 +00007733void LocationsBuilderARM::VisitDataProcWithShifterOp(
7734 HDataProcWithShifterOp* instruction) {
7735 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
7736 instruction->GetType() == Primitive::kPrimLong);
7737 LocationSummary* locations =
7738 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7739 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
7740 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7741
7742 locations->SetInAt(0, Location::RequiresRegister());
7743 locations->SetInAt(1, Location::RequiresRegister());
7744 locations->SetOut(Location::RequiresRegister(),
7745 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7746}
7747
7748void InstructionCodeGeneratorARM::VisitDataProcWithShifterOp(
7749 HDataProcWithShifterOp* instruction) {
7750 const LocationSummary* const locations = instruction->GetLocations();
7751 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
7752 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
7753 const Location left = locations->InAt(0);
7754 const Location right = locations->InAt(1);
7755 const Location out = locations->Out();
7756
7757 if (instruction->GetType() == Primitive::kPrimInt) {
7758 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
7759
7760 const Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
7761 ? right.AsRegisterPairLow<Register>()
7762 : right.AsRegister<Register>();
7763
7764 GenerateDataProcInstruction(kind,
7765 out.AsRegister<Register>(),
7766 left.AsRegister<Register>(),
7767 ShifterOperand(second,
7768 ShiftFromOpKind(op_kind),
7769 instruction->GetShiftAmount()),
7770 codegen_);
7771 } else {
7772 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7773
7774 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7775 const Register second = right.AsRegister<Register>();
7776
7777 DCHECK_NE(out.AsRegisterPairLow<Register>(), second);
7778 GenerateDataProc(kind,
7779 out,
7780 left,
7781 ShifterOperand(second),
7782 ShifterOperand(second, ASR, 31),
7783 codegen_);
7784 } else {
7785 GenerateLongDataProc(instruction, codegen_);
7786 }
7787 }
7788}
7789
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007790void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
7791 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7792 if (value == 0xffffffffu) {
7793 if (out != first) {
7794 __ mov(out, ShifterOperand(first));
7795 }
7796 return;
7797 }
7798 if (value == 0u) {
7799 __ mov(out, ShifterOperand(0));
7800 return;
7801 }
7802 ShifterOperand so;
7803 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
7804 __ and_(out, first, so);
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007805 } else if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007806 __ bic(out, first, ShifterOperand(~value));
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007807 } else {
7808 DCHECK(IsPowerOfTwo(value + 1));
7809 __ ubfx(out, first, 0, WhichPowerOf2(value + 1));
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007810 }
7811}
7812
7813void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
7814 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7815 if (value == 0u) {
7816 if (out != first) {
7817 __ mov(out, ShifterOperand(first));
7818 }
7819 return;
7820 }
7821 if (value == 0xffffffffu) {
7822 __ mvn(out, ShifterOperand(0));
7823 return;
7824 }
7825 ShifterOperand so;
7826 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
7827 __ orr(out, first, so);
7828 } else {
7829 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
7830 __ orn(out, first, ShifterOperand(~value));
7831 }
7832}
7833
7834void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
7835 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7836 if (value == 0u) {
7837 if (out != first) {
7838 __ mov(out, ShifterOperand(first));
7839 }
7840 return;
7841 }
7842 __ eor(out, first, ShifterOperand(value));
7843}
7844
Vladimir Marko59751a72016-08-05 14:37:27 +01007845void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
7846 Location first,
7847 uint64_t value) {
7848 Register out_low = out.AsRegisterPairLow<Register>();
7849 Register out_high = out.AsRegisterPairHigh<Register>();
7850 Register first_low = first.AsRegisterPairLow<Register>();
7851 Register first_high = first.AsRegisterPairHigh<Register>();
7852 uint32_t value_low = Low32Bits(value);
7853 uint32_t value_high = High32Bits(value);
7854 if (value_low == 0u) {
7855 if (out_low != first_low) {
7856 __ mov(out_low, ShifterOperand(first_low));
7857 }
7858 __ AddConstant(out_high, first_high, value_high);
7859 return;
7860 }
7861 __ AddConstantSetFlags(out_low, first_low, value_low);
7862 ShifterOperand so;
7863 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
7864 __ adc(out_high, first_high, so);
7865 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
7866 __ sbc(out_high, first_high, so);
7867 } else {
7868 LOG(FATAL) << "Unexpected constant " << value_high;
7869 UNREACHABLE();
7870 }
7871}
7872
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007873void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
7874 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007875 Location first = locations->InAt(0);
7876 Location second = locations->InAt(1);
7877 Location out = locations->Out();
7878
7879 if (second.IsConstant()) {
7880 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7881 uint32_t value_low = Low32Bits(value);
7882 if (instruction->GetResultType() == Primitive::kPrimInt) {
7883 Register first_reg = first.AsRegister<Register>();
7884 Register out_reg = out.AsRegister<Register>();
7885 if (instruction->IsAnd()) {
7886 GenerateAndConst(out_reg, first_reg, value_low);
7887 } else if (instruction->IsOr()) {
7888 GenerateOrrConst(out_reg, first_reg, value_low);
7889 } else {
7890 DCHECK(instruction->IsXor());
7891 GenerateEorConst(out_reg, first_reg, value_low);
7892 }
7893 } else {
7894 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7895 uint32_t value_high = High32Bits(value);
7896 Register first_low = first.AsRegisterPairLow<Register>();
7897 Register first_high = first.AsRegisterPairHigh<Register>();
7898 Register out_low = out.AsRegisterPairLow<Register>();
7899 Register out_high = out.AsRegisterPairHigh<Register>();
7900 if (instruction->IsAnd()) {
7901 GenerateAndConst(out_low, first_low, value_low);
7902 GenerateAndConst(out_high, first_high, value_high);
7903 } else if (instruction->IsOr()) {
7904 GenerateOrrConst(out_low, first_low, value_low);
7905 GenerateOrrConst(out_high, first_high, value_high);
7906 } else {
7907 DCHECK(instruction->IsXor());
7908 GenerateEorConst(out_low, first_low, value_low);
7909 GenerateEorConst(out_high, first_high, value_high);
7910 }
7911 }
7912 return;
7913 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007914
7915 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007916 Register first_reg = first.AsRegister<Register>();
7917 ShifterOperand second_reg(second.AsRegister<Register>());
7918 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007919 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007920 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007921 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007922 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007923 } else {
7924 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007925 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007926 }
7927 } else {
7928 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007929 Register first_low = first.AsRegisterPairLow<Register>();
7930 Register first_high = first.AsRegisterPairHigh<Register>();
7931 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7932 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7933 Register out_low = out.AsRegisterPairLow<Register>();
7934 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007935 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007936 __ and_(out_low, first_low, second_low);
7937 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007938 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007939 __ orr(out_low, first_low, second_low);
7940 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007941 } else {
7942 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007943 __ eor(out_low, first_low, second_low);
7944 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007945 }
7946 }
7947}
7948
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007949void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
7950 HInstruction* instruction,
7951 Location out,
7952 uint32_t offset,
7953 Location maybe_temp,
7954 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007955 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007956 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007957 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007958 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007959 if (kUseBakerReadBarrier) {
7960 // Load with fast path based Baker's read barrier.
7961 // /* HeapReference<Object> */ out = *(out + offset)
7962 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007963 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007964 } else {
7965 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007966 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00007967 // in the following move operation, as we will need it for the
7968 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007969 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00007970 // /* HeapReference<Object> */ out = *(out + offset)
7971 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007972 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00007973 }
7974 } else {
7975 // Plain load with no read barrier.
7976 // /* HeapReference<Object> */ out = *(out + offset)
7977 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7978 __ MaybeUnpoisonHeapReference(out_reg);
7979 }
7980}
7981
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007982void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
7983 HInstruction* instruction,
7984 Location out,
7985 Location obj,
7986 uint32_t offset,
7987 Location maybe_temp,
7988 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007989 Register out_reg = out.AsRegister<Register>();
7990 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007991 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007992 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007993 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007994 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007995 // Load with fast path based Baker's read barrier.
7996 // /* HeapReference<Object> */ out = *(obj + offset)
7997 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007998 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007999 } else {
8000 // Load with slow path based read barrier.
8001 // /* HeapReference<Object> */ out = *(obj + offset)
8002 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8003 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8004 }
8005 } else {
8006 // Plain load with no read barrier.
8007 // /* HeapReference<Object> */ out = *(obj + offset)
8008 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8009 __ MaybeUnpoisonHeapReference(out_reg);
8010 }
8011}
8012
8013void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
8014 Location root,
8015 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008016 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008017 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008018 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008019 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008020 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00008021 if (kUseBakerReadBarrier) {
8022 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008023 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008024 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8025 !Runtime::Current()->UseJitCompilation()) {
8026 // Note that we do not actually check the value of `GetIsGcMarking()`
8027 // to decide whether to mark the loaded GC root or not. Instead, we
8028 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8029 // barrier mark introspection entrypoint. If `temp` is null, it means
8030 // that `GetIsGcMarking()` is false, and vice versa.
8031 //
8032 // We use link-time generated thunks for the slow path. That thunk
8033 // checks the reference and jumps to the entrypoint if needed.
8034 //
8035 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8036 // lr = &return_address;
8037 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8038 // if (temp != nullptr) {
8039 // goto gc_root_thunk<root_reg>(lr)
8040 // }
8041 // return_address:
Roland Levillainc9285912015-12-18 10:38:42 +00008042
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008043 CheckLastTempIsBakerCcEntrypointRegister(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008044 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008045 uint32_t custom_data =
Vladimir Marko88abba22017-05-03 17:09:25 +01008046 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg, narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008047 Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008048
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008049 // entrypoint_reg =
8050 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8051 DCHECK_EQ(IP, 12);
8052 const int32_t entry_point_offset =
8053 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8054 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008055
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008056 Label return_address;
8057 __ AdrCode(LR, &return_address);
8058 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008059 // Currently the offset is always within range. If that changes,
8060 // we shall have to split the load the same way as for fields.
8061 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008062 DCHECK(!down_cast<Thumb2Assembler*>(GetAssembler())->IsForced32Bit());
8063 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()), !narrow);
8064 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008065 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8066 EmitPlaceholderBne(codegen_, bne_label);
8067 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008068 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8069 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8070 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008071 } else {
8072 // Note that we do not actually check the value of
8073 // `GetIsGcMarking()` to decide whether to mark the loaded GC
8074 // root or not. Instead, we load into `temp` the read barrier
8075 // mark entry point corresponding to register `root`. If `temp`
8076 // is null, it means that `GetIsGcMarking()` is false, and vice
8077 // versa.
8078 //
8079 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8080 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8081 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8082 // // Slow path.
8083 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
8084 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008085
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008086 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
8087 Location temp = Location::RegisterLocation(LR);
8088 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
8089 instruction, root, /* entrypoint */ temp);
8090 codegen_->AddSlowPath(slow_path);
8091
8092 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8093 const int32_t entry_point_offset =
8094 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
8095 // Loading the entrypoint does not require a load acquire since it is only changed when
8096 // threads are suspended or running a checkpoint.
8097 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
8098
8099 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8100 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8101 static_assert(
8102 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8103 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8104 "have different sizes.");
8105 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8106 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8107 "have different sizes.");
8108
8109 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8110 // checking GetIsGcMarking.
8111 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
8112 __ Bind(slow_path->GetExitLabel());
8113 }
Roland Levillainc9285912015-12-18 10:38:42 +00008114 } else {
8115 // GC root loaded through a slow path for read barriers other
8116 // than Baker's.
8117 // /* GcRoot<mirror::Object>* */ root = obj + offset
8118 __ AddConstant(root_reg, obj, offset);
8119 // /* mirror::Object* */ root = root->Read()
8120 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8121 }
8122 } else {
8123 // Plain GC root load with no read barrier.
8124 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8125 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8126 // Note that GC roots are not affected by heap poisoning, thus we
8127 // do not have to unpoison `root_reg` here.
8128 }
8129}
8130
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008131void CodeGeneratorARM::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8132 DCHECK(kEmitCompilerReadBarrier);
8133 DCHECK(kUseBakerReadBarrier);
8134 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8135 if (!Runtime::Current()->UseJitCompilation()) {
8136 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
8137 }
8138 }
8139}
8140
Roland Levillainc9285912015-12-18 10:38:42 +00008141void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8142 Location ref,
8143 Register obj,
8144 uint32_t offset,
8145 Location temp,
8146 bool needs_null_check) {
8147 DCHECK(kEmitCompilerReadBarrier);
8148 DCHECK(kUseBakerReadBarrier);
8149
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008150 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8151 !Runtime::Current()->UseJitCompilation()) {
8152 // Note that we do not actually check the value of `GetIsGcMarking()`
8153 // to decide whether to mark the loaded reference or not. Instead, we
8154 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8155 // barrier mark introspection entrypoint. If `temp` is null, it means
8156 // that `GetIsGcMarking()` is false, and vice versa.
8157 //
8158 // We use link-time generated thunks for the slow path. That thunk checks
8159 // the holder and jumps to the entrypoint if needed. If the holder is not
8160 // gray, it creates a fake dependency and returns to the LDR instruction.
8161 //
8162 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8163 // lr = &gray_return_address;
8164 // if (temp != nullptr) {
8165 // goto field_thunk<holder_reg, base_reg>(lr)
8166 // }
8167 // not_gray_return_address:
8168 // // Original reference load. If the offset is too large to fit
8169 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008170 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008171 // gray_return_address:
8172
8173 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko88abba22017-05-03 17:09:25 +01008174 Register ref_reg = ref.AsRegister<Register>();
8175 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008176 Register base = obj;
8177 if (offset >= kReferenceLoadMinFarOffset) {
8178 base = temp.AsRegister<Register>();
8179 DCHECK_NE(base, kBakerCcEntrypointRegister);
8180 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8181 __ AddConstant(base, obj, offset & ~(kReferenceLoadMinFarOffset - 1u));
8182 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008183 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8184 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8185 // increase the overall code size when taking the generated thunks into account.
8186 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008187 }
8188 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8189 uint32_t custom_data =
Vladimir Marko88abba22017-05-03 17:09:25 +01008190 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(base, obj, narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008191 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8192
8193 // entrypoint_reg =
8194 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8195 DCHECK_EQ(IP, 12);
8196 const int32_t entry_point_offset =
8197 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8198 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8199
8200 Label return_address;
8201 __ AdrCode(LR, &return_address);
8202 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008203 EmitPlaceholderBne(this, bne_label);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008204 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008205 DCHECK(!down_cast<Thumb2Assembler*>(GetAssembler())->IsForced32Bit());
8206 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()), !narrow);
8207 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008208 __ LoadFromOffset(kLoadWord, ref_reg, base, offset);
8209 if (needs_null_check) {
8210 MaybeRecordImplicitNullCheck(instruction);
8211 }
8212 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8213 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008214 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8215 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8216 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008217 return;
8218 }
8219
Roland Levillainc9285912015-12-18 10:38:42 +00008220 // /* HeapReference<Object> */ ref = *(obj + offset)
8221 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01008222 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00008223 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008224 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008225}
8226
8227void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8228 Location ref,
8229 Register obj,
8230 uint32_t data_offset,
8231 Location index,
8232 Location temp,
8233 bool needs_null_check) {
8234 DCHECK(kEmitCompilerReadBarrier);
8235 DCHECK(kUseBakerReadBarrier);
8236
Roland Levillainbfea3352016-06-23 13:48:47 +01008237 static_assert(
8238 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8239 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008240 ScaleFactor scale_factor = TIMES_4;
8241
8242 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8243 !Runtime::Current()->UseJitCompilation()) {
8244 // Note that we do not actually check the value of `GetIsGcMarking()`
8245 // to decide whether to mark the loaded reference or not. Instead, we
8246 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8247 // barrier mark introspection entrypoint. If `temp` is null, it means
8248 // that `GetIsGcMarking()` is false, and vice versa.
8249 //
8250 // We use link-time generated thunks for the slow path. That thunk checks
8251 // the holder and jumps to the entrypoint if needed. If the holder is not
8252 // gray, it creates a fake dependency and returns to the LDR instruction.
8253 //
8254 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8255 // lr = &gray_return_address;
8256 // if (temp != nullptr) {
8257 // goto field_thunk<holder_reg, base_reg>(lr)
8258 // }
8259 // not_gray_return_address:
8260 // // Original reference load. If the offset is too large to fit
8261 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008262 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008263 // gray_return_address:
8264
8265 DCHECK(index.IsValid());
8266 Register index_reg = index.AsRegister<Register>();
8267 Register ref_reg = ref.AsRegister<Register>();
8268 Register data_reg = temp.AsRegister<Register>();
8269 DCHECK_NE(data_reg, kBakerCcEntrypointRegister);
8270
8271 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8272 uint32_t custom_data =
8273 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg);
8274 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8275
8276 // entrypoint_reg =
8277 // Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
8278 DCHECK_EQ(IP, 12);
8279 const int32_t entry_point_offset =
8280 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8281 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8282 __ AddConstant(data_reg, obj, data_offset);
8283
8284 Label return_address;
8285 __ AdrCode(LR, &return_address);
8286 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008287 EmitPlaceholderBne(this, bne_label);
Vladimir Marko88abba22017-05-03 17:09:25 +01008288 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()));
8289 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008290 __ ldr(ref_reg, Address(data_reg, index_reg, LSL, scale_factor));
8291 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8292 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8293 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008294 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8295 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008296 return;
8297 }
8298
Roland Levillainc9285912015-12-18 10:38:42 +00008299 // /* HeapReference<Object> */ ref =
8300 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
8301 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008302 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008303}
8304
8305void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8306 Location ref,
8307 Register obj,
8308 uint32_t offset,
8309 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01008310 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00008311 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008312 bool needs_null_check) {
Roland Levillainc9285912015-12-18 10:38:42 +00008313 DCHECK(kEmitCompilerReadBarrier);
8314 DCHECK(kUseBakerReadBarrier);
8315
Roland Levillain54f869e2017-03-06 13:54:11 +00008316 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8317 // whether we need to enter the slow path to mark the reference.
8318 // Then, in the slow path, check the gray bit in the lock word of
8319 // the reference's holder (`obj`) to decide whether to mark `ref` or
8320 // not.
Roland Levillainc9285912015-12-18 10:38:42 +00008321 //
Roland Levillainba650a42017-03-06 13:52:32 +00008322 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainff487002017-03-07 16:50:01 +00008323 // instead, we load into `temp2` the read barrier mark entry point
8324 // corresponding to register `ref`. If `temp2` is null, it means
8325 // that `GetIsGcMarking()` is false, and vice versa.
8326 //
8327 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8328 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8329 // // Slow path.
8330 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8331 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8332 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8333 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8334 // if (is_gray) {
8335 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8336 // }
8337 // } else {
8338 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8339 // }
8340
8341 Register temp_reg = temp.AsRegister<Register>();
8342
8343 // Slow path marking the object `ref` when the GC is marking. The
8344 // entrypoint will already be loaded in `temp2`.
8345 Location temp2 = Location::RegisterLocation(LR);
8346 SlowPathCodeARM* slow_path =
8347 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM(
8348 instruction,
8349 ref,
8350 obj,
8351 offset,
8352 index,
8353 scale_factor,
8354 needs_null_check,
8355 temp_reg,
8356 /* entrypoint */ temp2);
8357 AddSlowPath(slow_path);
8358
8359 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8360 const int32_t entry_point_offset =
8361 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8362 // Loading the entrypoint does not require a load acquire since it is only changed when
8363 // threads are suspended or running a checkpoint.
8364 __ LoadFromOffset(kLoadWord, temp2.AsRegister<Register>(), TR, entry_point_offset);
8365 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8366 // checking GetIsGcMarking.
8367 __ CompareAndBranchIfNonZero(temp2.AsRegister<Register>(), slow_path->GetEntryLabel());
8368 // Fast path: the GC is not marking: just load the reference.
8369 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8370 __ Bind(slow_path->GetExitLabel());
8371}
8372
8373void CodeGeneratorARM::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8374 Location ref,
8375 Register obj,
8376 Location field_offset,
8377 Location temp,
8378 bool needs_null_check,
8379 Register temp2) {
8380 DCHECK(kEmitCompilerReadBarrier);
8381 DCHECK(kUseBakerReadBarrier);
8382
8383 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8384 // whether we need to enter the slow path to update the reference
8385 // field within `obj`. Then, in the slow path, check the gray bit
8386 // in the lock word of the reference's holder (`obj`) to decide
8387 // whether to mark `ref` and update the field or not.
8388 //
8389 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainba650a42017-03-06 13:52:32 +00008390 // instead, we load into `temp3` the read barrier mark entry point
8391 // corresponding to register `ref`. If `temp3` is null, it means
8392 // that `GetIsGcMarking()` is false, and vice versa.
8393 //
8394 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008395 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8396 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008397 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8398 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8399 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8400 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8401 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008402 // old_ref = ref;
Roland Levillain54f869e2017-03-06 13:54:11 +00008403 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008404 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008405 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008406 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008407
Roland Levillain35345a52017-02-27 14:32:08 +00008408 Register temp_reg = temp.AsRegister<Register>();
Roland Levillain1372c9f2017-01-13 11:47:39 +00008409
Roland Levillainff487002017-03-07 16:50:01 +00008410 // Slow path updating the object reference at address `obj +
8411 // field_offset` when the GC is marking. The entrypoint will already
8412 // be loaded in `temp3`.
Roland Levillainba650a42017-03-06 13:52:32 +00008413 Location temp3 = Location::RegisterLocation(LR);
Roland Levillainff487002017-03-07 16:50:01 +00008414 SlowPathCodeARM* slow_path =
8415 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(
8416 instruction,
8417 ref,
8418 obj,
8419 /* offset */ 0u,
8420 /* index */ field_offset,
8421 /* scale_factor */ ScaleFactor::TIMES_1,
8422 needs_null_check,
8423 temp_reg,
8424 temp2,
8425 /* entrypoint */ temp3);
Roland Levillainba650a42017-03-06 13:52:32 +00008426 AddSlowPath(slow_path);
Roland Levillain35345a52017-02-27 14:32:08 +00008427
Roland Levillainba650a42017-03-06 13:52:32 +00008428 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8429 const int32_t entry_point_offset =
8430 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8431 // Loading the entrypoint does not require a load acquire since it is only changed when
8432 // threads are suspended or running a checkpoint.
8433 __ LoadFromOffset(kLoadWord, temp3.AsRegister<Register>(), TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008434 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8435 // checking GetIsGcMarking.
8436 __ CompareAndBranchIfNonZero(temp3.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008437 // Fast path: the GC is not marking: nothing to do (the field is
8438 // up-to-date, and we don't need to load the reference).
Roland Levillainba650a42017-03-06 13:52:32 +00008439 __ Bind(slow_path->GetExitLabel());
8440}
Roland Levillain35345a52017-02-27 14:32:08 +00008441
Roland Levillainba650a42017-03-06 13:52:32 +00008442void CodeGeneratorARM::GenerateRawReferenceLoad(HInstruction* instruction,
8443 Location ref,
8444 Register obj,
8445 uint32_t offset,
8446 Location index,
8447 ScaleFactor scale_factor,
8448 bool needs_null_check) {
8449 Register ref_reg = ref.AsRegister<Register>();
8450
Roland Levillainc9285912015-12-18 10:38:42 +00008451 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008452 // Load types involving an "index": ArrayGet,
8453 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8454 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008455 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00008456 if (index.IsConstant()) {
8457 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01008458 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00008459 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
8460 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01008461 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008462 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8463 // intrinsics, which use a register pair as index ("long
8464 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01008465 Register index_reg = index.IsRegisterPair()
8466 ? index.AsRegisterPairLow<Register>()
8467 : index.AsRegister<Register>();
8468 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00008469 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
8470 }
8471 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008472 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillainc9285912015-12-18 10:38:42 +00008473 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
8474 }
8475
Roland Levillainba650a42017-03-06 13:52:32 +00008476 if (needs_null_check) {
8477 MaybeRecordImplicitNullCheck(instruction);
8478 }
8479
Roland Levillainc9285912015-12-18 10:38:42 +00008480 // Object* ref = ref_addr->AsMirrorPtr()
8481 __ MaybeUnpoisonHeapReference(ref_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00008482}
8483
8484void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
8485 Location out,
8486 Location ref,
8487 Location obj,
8488 uint32_t offset,
8489 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008490 DCHECK(kEmitCompilerReadBarrier);
8491
Roland Levillainc9285912015-12-18 10:38:42 +00008492 // Insert a slow path based read barrier *after* the reference load.
8493 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008494 // If heap poisoning is enabled, the unpoisoning of the loaded
8495 // reference will be carried out by the runtime within the slow
8496 // path.
8497 //
8498 // Note that `ref` currently does not get unpoisoned (when heap
8499 // poisoning is enabled), which is alright as the `ref` argument is
8500 // not used by the artReadBarrierSlow entry point.
8501 //
8502 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008503 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00008504 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
8505 AddSlowPath(slow_path);
8506
Roland Levillain3b359c72015-11-17 19:35:12 +00008507 __ b(slow_path->GetEntryLabel());
8508 __ Bind(slow_path->GetExitLabel());
8509}
8510
Roland Levillainc9285912015-12-18 10:38:42 +00008511void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8512 Location out,
8513 Location ref,
8514 Location obj,
8515 uint32_t offset,
8516 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008517 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00008518 // Baker's read barriers shall be handled by the fast path
8519 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
8520 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00008521 // If heap poisoning is enabled, unpoisoning will be taken care of
8522 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00008523 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00008524 } else if (kPoisonHeapReferences) {
8525 __ UnpoisonHeapReference(out.AsRegister<Register>());
8526 }
8527}
8528
Roland Levillainc9285912015-12-18 10:38:42 +00008529void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8530 Location out,
8531 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008532 DCHECK(kEmitCompilerReadBarrier);
8533
Roland Levillainc9285912015-12-18 10:38:42 +00008534 // Insert a slow path based read barrier *after* the GC root load.
8535 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008536 // Note that GC roots are not affected by heap poisoning, so we do
8537 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008538 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00008539 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
8540 AddSlowPath(slow_path);
8541
Roland Levillain3b359c72015-11-17 19:35:12 +00008542 __ b(slow_path->GetEntryLabel());
8543 __ Bind(slow_path->GetExitLabel());
8544}
8545
Vladimir Markodc151b22015-10-15 18:02:30 +01008546HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
8547 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008548 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008549 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01008550}
8551
Vladimir Markob4536b72015-11-24 13:45:23 +00008552Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
8553 Register temp) {
8554 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8555 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8556 if (!invoke->GetLocations()->Intrinsified()) {
8557 return location.AsRegister<Register>();
8558 }
8559 // For intrinsics we allow any location, so it may be on the stack.
8560 if (!location.IsRegister()) {
8561 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
8562 return temp;
8563 }
8564 // For register locations, check if the register was saved. If so, get it from the stack.
8565 // Note: There is a chance that the register was saved but not overwritten, so we could
8566 // save one load. However, since this is just an intrinsic slow path we prefer this
8567 // simple and more robust approach rather that trying to determine if that's the case.
8568 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08008569 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00008570 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
8571 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
8572 return temp;
8573 }
8574 return location.AsRegister<Register>();
8575}
8576
TatWai Chongd8c052a2016-11-02 16:12:48 +08008577Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
8578 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00008579 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
8580 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008581 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8582 uint32_t offset =
8583 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00008584 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008585 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00008586 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008587 }
Vladimir Marko58155012015-08-19 12:49:41 +00008588 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00008589 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008590 break;
8591 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8592 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
8593 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00008594 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
8595 HArmDexCacheArraysBase* base =
8596 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
8597 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
8598 temp.AsRegister<Register>());
8599 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
8600 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
8601 break;
8602 }
Vladimir Marko58155012015-08-19 12:49:41 +00008603 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00008604 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008605 Register method_reg;
8606 Register reg = temp.AsRegister<Register>();
8607 if (current_method.IsRegister()) {
8608 method_reg = current_method.AsRegister<Register>();
8609 } else {
8610 DCHECK(invoke->GetLocations()->Intrinsified());
8611 DCHECK(!current_method.IsValid());
8612 method_reg = reg;
8613 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
8614 }
Roland Levillain3b359c72015-11-17 19:35:12 +00008615 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
8616 __ LoadFromOffset(kLoadWord,
8617 reg,
8618 method_reg,
8619 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01008620 // temp = temp[index_in_cache];
8621 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
8622 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00008623 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
8624 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01008625 }
Vladimir Marko58155012015-08-19 12:49:41 +00008626 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08008627 return callee_method;
8628}
8629
8630void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
8631 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00008632
8633 switch (invoke->GetCodePtrLocation()) {
8634 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
8635 __ bl(GetFrameEntryLabel());
8636 break;
Vladimir Marko58155012015-08-19 12:49:41 +00008637 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8638 // LR = callee_method->entry_point_from_quick_compiled_code_
8639 __ LoadFromOffset(
8640 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07008641 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00008642 // LR()
8643 __ blx(LR);
8644 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08008645 }
8646
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08008647 DCHECK(!IsLeafMethod());
8648}
8649
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008650void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
8651 Register temp = temp_location.AsRegister<Register>();
8652 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8653 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00008654
8655 // Use the calling convention instead of the location of the receiver, as
8656 // intrinsics may have put the receiver in a different register. In the intrinsics
8657 // slow path, the arguments have been moved to the right place, so here we are
8658 // guaranteed that the receiver is the first register of the calling convention.
8659 InvokeDexCallingConvention calling_convention;
8660 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008661 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00008662 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00008663 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008664 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00008665 // Instead of simply (possibly) unpoisoning `temp` here, we should
8666 // emit a read barrier for the previous class reference load.
8667 // However this is not required in practice, as this is an
8668 // intermediate/temporary reference and because the current
8669 // concurrent copying collector keeps the from-space memory
8670 // intact/accessible until the end of the marking phase (the
8671 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008672 __ MaybeUnpoisonHeapReference(temp);
8673 // temp = temp->GetMethodAt(method_offset);
8674 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07008675 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008676 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
8677 // LR = temp->GetEntryPoint();
8678 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
8679 // LR();
8680 __ blx(LR);
8681}
8682
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008683CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008684 const DexFile& dex_file, dex::StringIndex string_index) {
8685 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008686}
8687
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008688CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08008689 const DexFile& dex_file, dex::TypeIndex type_index) {
8690 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008691}
8692
Vladimir Marko1998cd02017-01-13 13:02:58 +00008693CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
8694 const DexFile& dex_file, dex::TypeIndex type_index) {
8695 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8696}
8697
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008698CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
8699 const DexFile& dex_file, uint32_t element_offset) {
8700 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
8701}
8702
8703CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
8704 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8705 patches->emplace_back(dex_file, offset_or_index);
8706 return &patches->back();
8707}
8708
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008709Label* CodeGeneratorARM::NewBakerReadBarrierPatch(uint32_t custom_data) {
8710 baker_read_barrier_patches_.emplace_back(custom_data);
8711 return &baker_read_barrier_patches_.back().label;
8712}
8713
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008714Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00008715 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008716}
8717
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008718Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008719 dex::StringIndex string_index,
8720 Handle<mirror::String> handle) {
8721 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
8722 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008723 return jit_string_patches_.GetOrCreate(
8724 StringReference(&dex_file, string_index),
8725 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8726}
8727
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008728Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
8729 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008730 Handle<mirror::Class> handle) {
8731 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
8732 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008733 return jit_class_patches_.GetOrCreate(
8734 TypeReference(&dex_file, type_index),
8735 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8736}
8737
Vladimir Markoaad75c62016-10-03 08:46:48 +00008738template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
8739inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
8740 const ArenaDeque<PcRelativePatchInfo>& infos,
8741 ArenaVector<LinkerPatch>* linker_patches) {
8742 for (const PcRelativePatchInfo& info : infos) {
8743 const DexFile& dex_file = info.target_dex_file;
8744 size_t offset_or_index = info.offset_or_index;
8745 DCHECK(info.add_pc_label.IsBound());
8746 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
8747 // Add MOVW patch.
8748 DCHECK(info.movw_label.IsBound());
8749 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
8750 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
8751 // Add MOVT patch.
8752 DCHECK(info.movt_label.IsBound());
8753 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
8754 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
8755 }
8756}
8757
Vladimir Marko58155012015-08-19 12:49:41 +00008758void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
8759 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00008760 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00008761 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008762 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008763 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008764 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
8765 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00008766 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008767 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
8768 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008769 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00008770 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00008771 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
8772 linker_patches);
8773 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008774 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
8775 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008776 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
8777 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008778 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008779 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
8780 linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008781 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
8782 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.Position(),
8783 info.custom_data));
8784 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008785 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008786}
8787
8788Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
8789 return map->GetOrCreate(
8790 value,
8791 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00008792}
8793
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03008794void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8795 LocationSummary* locations =
8796 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
8797 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
8798 Location::RequiresRegister());
8799 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
8800 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
8801 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8802}
8803
8804void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8805 LocationSummary* locations = instr->GetLocations();
8806 Register res = locations->Out().AsRegister<Register>();
8807 Register accumulator =
8808 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
8809 Register mul_left =
8810 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
8811 Register mul_right =
8812 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
8813
8814 if (instr->GetOpKind() == HInstruction::kAdd) {
8815 __ mla(res, mul_left, mul_right, accumulator);
8816 } else {
8817 __ mls(res, mul_left, mul_right, accumulator);
8818 }
8819}
8820
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008821void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008822 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008823 LOG(FATAL) << "Unreachable";
8824}
8825
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008826void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008827 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008828 LOG(FATAL) << "Unreachable";
8829}
8830
Mark Mendellfe57faa2015-09-18 09:26:15 -04008831// Simple implementation of packed switch - generate cascaded compare/jumps.
8832void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8833 LocationSummary* locations =
8834 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8835 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008836 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008837 codegen_->GetAssembler()->IsThumb()) {
8838 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
8839 if (switch_instr->GetStartValue() != 0) {
8840 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
8841 }
8842 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008843}
8844
8845void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8846 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008847 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04008848 LocationSummary* locations = switch_instr->GetLocations();
8849 Register value_reg = locations->InAt(0).AsRegister<Register>();
8850 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8851
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008852 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008853 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008854 Register temp_reg = IP;
8855 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
8856 // the immediate, because IP is used as the destination register. For the other
8857 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
8858 // and they can be encoded in the instruction without making use of IP register.
8859 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
8860
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008861 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008862 // Jump to successors[0] if value == lower_bound.
8863 __ b(codegen_->GetLabelOf(successors[0]), EQ);
8864 int32_t last_index = 0;
8865 for (; num_entries - last_index > 2; last_index += 2) {
8866 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
8867 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8868 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
8869 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8870 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
8871 }
8872 if (num_entries - last_index == 2) {
8873 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00008874 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008875 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008876 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008877
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008878 // And the default for any other value.
8879 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
8880 __ b(codegen_->GetLabelOf(default_block));
8881 }
8882 } else {
8883 // Create a table lookup.
8884 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8885
8886 // Materialize a pointer to the switch table
8887 std::vector<Label*> labels(num_entries);
8888 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
8889 for (uint32_t i = 0; i < num_entries; i++) {
8890 labels[i] = codegen_->GetLabelOf(successors[i]);
8891 }
8892 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
8893
8894 // Remove the bias.
8895 Register key_reg;
8896 if (lower_bound != 0) {
8897 key_reg = locations->GetTemp(1).AsRegister<Register>();
8898 __ AddConstant(key_reg, value_reg, -lower_bound);
8899 } else {
8900 key_reg = value_reg;
8901 }
8902
8903 // Check whether the value is in the table, jump to default block if not.
8904 __ CmpConstant(key_reg, num_entries - 1);
8905 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
8906
8907 // Load the displacement from the table.
8908 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
8909
8910 // Dispatch is a direct add to the PC (for Thumb2).
8911 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008912 }
8913}
8914
Vladimir Markob4536b72015-11-24 13:45:23 +00008915void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8916 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8917 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00008918}
8919
8920void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8921 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008922 CodeGeneratorARM::PcRelativePatchInfo* labels =
8923 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00008924 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008925 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008926 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008927 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008928 __ BindTrackedLabel(&labels->add_pc_label);
8929 __ add(base_reg, base_reg, ShifterOperand(PC));
8930}
8931
Andreas Gampe85b62f22015-09-09 13:15:38 -07008932void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
8933 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00008934 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008935 return;
8936 }
8937
8938 DCHECK_NE(type, Primitive::kPrimVoid);
8939
8940 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
8941 if (return_loc.Equals(trg)) {
8942 return;
8943 }
8944
8945 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8946 // with the last branch.
8947 if (type == Primitive::kPrimLong) {
8948 HParallelMove parallel_move(GetGraph()->GetArena());
8949 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
8950 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
8951 GetMoveResolver()->EmitNativeCode(&parallel_move);
8952 } else if (type == Primitive::kPrimDouble) {
8953 HParallelMove parallel_move(GetGraph()->GetArena());
8954 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
8955 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
8956 GetMoveResolver()->EmitNativeCode(&parallel_move);
8957 } else {
8958 // Let the parallel move resolver take care of all of this.
8959 HParallelMove parallel_move(GetGraph()->GetArena());
8960 parallel_move.AddMove(return_loc, trg, type, nullptr);
8961 GetMoveResolver()->EmitNativeCode(&parallel_move);
8962 }
8963}
8964
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008965void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
8966 LocationSummary* locations =
8967 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8968 locations->SetInAt(0, Location::RequiresRegister());
8969 locations->SetOut(Location::RequiresRegister());
8970}
8971
8972void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
8973 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008974 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008975 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008976 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008977 __ LoadFromOffset(kLoadWord,
8978 locations->Out().AsRegister<Register>(),
8979 locations->InAt(0).AsRegister<Register>(),
8980 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008981 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008982 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008983 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008984 __ LoadFromOffset(kLoadWord,
8985 locations->Out().AsRegister<Register>(),
8986 locations->InAt(0).AsRegister<Register>(),
8987 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
8988 __ LoadFromOffset(kLoadWord,
8989 locations->Out().AsRegister<Register>(),
8990 locations->Out().AsRegister<Register>(),
8991 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008992 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008993}
8994
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008995static void PatchJitRootUse(uint8_t* code,
8996 const uint8_t* roots_data,
8997 Literal* literal,
8998 uint64_t index_in_table) {
8999 DCHECK(literal->GetLabel()->IsBound());
9000 uint32_t literal_offset = literal->GetLabel()->Position();
9001 uintptr_t address =
9002 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9003 uint8_t* data = code + literal_offset;
9004 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9005}
9006
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009007void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9008 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009009 const StringReference& string_reference = entry.first;
9010 Literal* table_entry_literal = entry.second;
9011 const auto it = jit_string_roots_.find(string_reference);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009012 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009013 uint64_t index_in_table = it->second;
9014 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009015 }
9016 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009017 const TypeReference& type_reference = entry.first;
9018 Literal* table_entry_literal = entry.second;
9019 const auto it = jit_class_roots_.find(type_reference);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009020 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009021 uint64_t index_in_table = it->second;
9022 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009023 }
9024}
9025
Roland Levillain4d027112015-07-01 15:41:14 +01009026#undef __
9027#undef QUICK_ENTRY_POINT
9028
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00009029} // namespace arm
9030} // namespace art