blob: 8650aee8190d434d982bf1b64eb70f6c50ea29fd [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());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100625 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000626 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700627 }
628
Alexandre Rames9931f312015-06-19 14:47:01 +0100629 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
630
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700631 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700632 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
633};
634
Artem Serovf4d6aee2016-07-11 10:41:45 +0100635class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100636 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100637 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100638
639 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
640 LocationSummary* locations = instruction_->GetLocations();
641 __ Bind(GetEntryLabel());
642 SaveLiveRegisters(codegen, locations);
643
644 InvokeRuntimeCallingConvention calling_convention;
645 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
646 parallel_move.AddMove(
647 locations->InAt(0),
648 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
649 Primitive::kPrimNot,
650 nullptr);
651 parallel_move.AddMove(
652 locations->InAt(1),
653 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
654 Primitive::kPrimInt,
655 nullptr);
656 parallel_move.AddMove(
657 locations->InAt(2),
658 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
659 Primitive::kPrimNot,
660 nullptr);
661 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
662
663 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100664 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000665 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100666 RestoreLiveRegisters(codegen, locations);
667 __ b(GetExitLabel());
668 }
669
670 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
671
672 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100673 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
674};
675
Roland Levillain54f869e2017-03-06 13:54:11 +0000676// Abstract base class for read barrier slow paths marking a reference
677// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000678//
Roland Levillain54f869e2017-03-06 13:54:11 +0000679// Argument `entrypoint` must be a register location holding the read
680// barrier marking runtime entry point to be invoked.
681class ReadBarrierMarkSlowPathBaseARM : public SlowPathCodeARM {
682 protected:
683 ReadBarrierMarkSlowPathBaseARM(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000684 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
685 DCHECK(kEmitCompilerReadBarrier);
686 }
687
Roland Levillain54f869e2017-03-06 13:54:11 +0000688 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000689
Roland Levillain54f869e2017-03-06 13:54:11 +0000690 // Generate assembly code calling the read barrier marking runtime
691 // entry point (ReadBarrierMarkRegX).
692 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000693 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain47b3ab22017-02-27 14:31:35 +0000694
Roland Levillain47b3ab22017-02-27 14:31:35 +0000695 // No need to save live registers; it's taken care of by the
696 // entrypoint. Also, there is no need to update the stack mask,
697 // as this runtime call will not trigger a garbage collection.
698 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
699 DCHECK_NE(ref_reg, SP);
700 DCHECK_NE(ref_reg, LR);
701 DCHECK_NE(ref_reg, PC);
702 // IP is used internally by the ReadBarrierMarkRegX entry point
703 // as a temporary, it cannot be the entry point's input/output.
704 DCHECK_NE(ref_reg, IP);
705 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
706 // "Compact" slow path, saving two moves.
707 //
708 // Instead of using the standard runtime calling convention (input
709 // and output in R0):
710 //
711 // R0 <- ref
712 // R0 <- ReadBarrierMark(R0)
713 // ref <- R0
714 //
715 // we just use rX (the register containing `ref`) as input and output
716 // of a dedicated entrypoint:
717 //
718 // rX <- ReadBarrierMarkRegX(rX)
719 //
720 if (entrypoint_.IsValid()) {
721 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
722 __ blx(entrypoint_.AsRegister<Register>());
723 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000724 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000725 int32_t entry_point_offset =
726 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
727 // This runtime call does not require a stack map.
728 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
729 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000730 }
731
732 // The location (register) of the marked object reference.
733 const Location ref_;
734
735 // The location of the entrypoint if it is already loaded.
736 const Location entrypoint_;
737
738 private:
739 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM);
740};
741
Dave Allison20dfc792014-06-16 20:44:29 -0700742// Slow path marking an object reference `ref` during a read
743// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000744// reference does not get updated by this slow path after marking.
Dave Allison20dfc792014-06-16 20:44:29 -0700745//
746// This means that after the execution of this slow path, `ref` will
747// always be up-to-date, but `obj.field` may not; i.e., after the
748// flip, `ref` will be a to-space reference, but `obj.field` will
749// probably still be a from-space reference (unless it gets updated by
750// another thread, or if another thread installed another object
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000751// reference (different from `ref`) in `obj.field`).
752//
753// If `entrypoint` is a valid location it is assumed to already be
754// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000755// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000756class ReadBarrierMarkSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000757 public:
758 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
759 Location ref,
760 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000761 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000762 DCHECK(kEmitCompilerReadBarrier);
763 }
764
765 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
766
767 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
768 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000769 DCHECK(locations->CanCall());
770 if (kIsDebugBuild) {
771 Register ref_reg = ref_.AsRegister<Register>();
772 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
773 }
774 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
775 << "Unexpected instruction in read barrier marking slow path: "
776 << instruction_->DebugName();
777
778 __ Bind(GetEntryLabel());
779 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000780 __ b(GetExitLabel());
781 }
782
783 private:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000784 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
785};
786
Roland Levillain54f869e2017-03-06 13:54:11 +0000787// Slow path loading `obj`'s lock word, loading a reference from
788// object `*(obj + offset + (index << scale_factor))` into `ref`, and
789// marking `ref` if `obj` is gray according to the lock word (Baker
790// read barrier). The field `obj.field` in the object `obj` holding
791// this reference does not get updated by this slow path after marking
792// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
793// below for that).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000794//
Roland Levillain54f869e2017-03-06 13:54:11 +0000795// This means that after the execution of this slow path, `ref` will
796// always be up-to-date, but `obj.field` may not; i.e., after the
797// flip, `ref` will be a to-space reference, but `obj.field` will
798// probably still be a from-space reference (unless it gets updated by
799// another thread, or if another thread installed another object
800// reference (different from `ref`) in `obj.field`).
801//
802// Argument `entrypoint` must be a register location holding the read
803// barrier marking runtime entry point to be invoked.
804class LoadReferenceWithBakerReadBarrierSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000805 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000806 LoadReferenceWithBakerReadBarrierSlowPathARM(HInstruction* instruction,
807 Location ref,
808 Register obj,
809 uint32_t offset,
810 Location index,
811 ScaleFactor scale_factor,
812 bool needs_null_check,
813 Register temp,
814 Location entrypoint)
815 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000816 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000817 offset_(offset),
818 index_(index),
819 scale_factor_(scale_factor),
820 needs_null_check_(needs_null_check),
821 temp_(temp) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000822 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000823 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000824 }
825
Roland Levillain54f869e2017-03-06 13:54:11 +0000826 const char* GetDescription() const OVERRIDE {
827 return "LoadReferenceWithBakerReadBarrierSlowPathARM";
828 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000829
830 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
831 LocationSummary* locations = instruction_->GetLocations();
832 Register ref_reg = ref_.AsRegister<Register>();
833 DCHECK(locations->CanCall());
834 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000835 DCHECK_NE(ref_reg, temp_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000836 DCHECK(instruction_->IsInstanceFieldGet() ||
837 instruction_->IsStaticFieldGet() ||
838 instruction_->IsArrayGet() ||
839 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000840 instruction_->IsInstanceOf() ||
841 instruction_->IsCheckCast() ||
842 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
843 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
844 << "Unexpected instruction in read barrier marking slow path: "
845 << instruction_->DebugName();
846 // The read barrier instrumentation of object ArrayGet
847 // instructions does not support the HIntermediateAddress
848 // instruction.
849 DCHECK(!(instruction_->IsArrayGet() &&
850 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
851
852 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000853
854 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
855 // inserted after the original load. However, in fast path based
856 // Baker's read barriers, we need to perform the load of
857 // mirror::Object::monitor_ *before* the original reference load.
858 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000859 // The slow path (for Baker's algorithm) should look like:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000860 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000861 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
862 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
863 // HeapReference<mirror::Object> ref = *src; // Original reference load.
864 // bool is_gray = (rb_state == ReadBarrier::GrayState());
865 // if (is_gray) {
866 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
867 // }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000868 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000869 // Note: the original implementation in ReadBarrier::Barrier is
870 // slightly more complex as it performs additional checks that we do
871 // not do here for performance reasons.
872
873 // /* int32_t */ monitor = obj->monitor_
874 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
875 __ LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
876 if (needs_null_check_) {
877 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000878 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000879 // /* LockWord */ lock_word = LockWord(monitor)
880 static_assert(sizeof(LockWord) == sizeof(int32_t),
881 "art::LockWord and int32_t have different sizes.");
882
883 // Introduce a dependency on the lock_word including the rb_state,
884 // which shall prevent load-load reordering without using
885 // a memory barrier (which would be more expensive).
886 // `obj` is unchanged by this operation, but its value now depends
887 // on `temp`.
888 __ add(obj_, obj_, ShifterOperand(temp_, LSR, 32));
889
890 // The actual reference load.
891 // A possible implicit null check has already been handled above.
892 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
893 arm_codegen->GenerateRawReferenceLoad(
894 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
895
896 // Mark the object `ref` when `obj` is gray.
897 //
898 // if (rb_state == ReadBarrier::GrayState())
899 // ref = ReadBarrier::Mark(ref);
900 //
901 // Given the numeric representation, it's enough to check the low bit of the
902 // rb_state. We do that by shifting the bit out of the lock word with LSRS
903 // which can be a 16-bit instruction unlike the TST immediate.
904 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
905 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
906 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
907 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
908 GenerateReadBarrierMarkRuntimeCall(codegen);
909
Roland Levillain47b3ab22017-02-27 14:31:35 +0000910 __ b(GetExitLabel());
911 }
912
913 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000914 // The register containing the object holding the marked object reference field.
915 Register obj_;
916 // The offset, index and scale factor to access the reference in `obj_`.
917 uint32_t offset_;
918 Location index_;
919 ScaleFactor scale_factor_;
920 // Is a null check required?
921 bool needs_null_check_;
922 // A temporary register used to hold the lock word of `obj_`.
923 Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000924
Roland Levillain54f869e2017-03-06 13:54:11 +0000925 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000926};
927
Roland Levillain54f869e2017-03-06 13:54:11 +0000928// Slow path loading `obj`'s lock word, loading a reference from
929// object `*(obj + offset + (index << scale_factor))` into `ref`, and
930// marking `ref` if `obj` is gray according to the lock word (Baker
931// read barrier). If needed, this slow path also atomically updates
932// the field `obj.field` in the object `obj` holding this reference
933// after marking (contrary to
934// LoadReferenceWithBakerReadBarrierSlowPathARM above, which never
935// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000936//
937// This means that after the execution of this slow path, both `ref`
938// and `obj.field` will be up-to-date; i.e., after the flip, both will
939// hold the same to-space reference (unless another thread installed
940// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000941//
Roland Levillain54f869e2017-03-06 13:54:11 +0000942// Argument `entrypoint` must be a register location holding the read
943// barrier marking runtime entry point to be invoked.
944class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
945 : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000946 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000947 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(HInstruction* instruction,
948 Location ref,
949 Register obj,
950 uint32_t offset,
951 Location index,
952 ScaleFactor scale_factor,
953 bool needs_null_check,
954 Register temp1,
955 Register temp2,
956 Location entrypoint)
957 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000958 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000959 offset_(offset),
960 index_(index),
961 scale_factor_(scale_factor),
962 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000963 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +0000964 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000965 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000966 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000967 }
968
Roland Levillain54f869e2017-03-06 13:54:11 +0000969 const char* GetDescription() const OVERRIDE {
970 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM";
971 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000972
973 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
974 LocationSummary* locations = instruction_->GetLocations();
975 Register ref_reg = ref_.AsRegister<Register>();
976 DCHECK(locations->CanCall());
977 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000978 DCHECK_NE(ref_reg, temp1_);
979
980 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000981 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
982 << "Unexpected instruction in read barrier marking and field updating slow path: "
983 << instruction_->DebugName();
984 DCHECK(instruction_->GetLocations()->Intrinsified());
985 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000986 DCHECK_EQ(offset_, 0u);
987 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
988 // The location of the offset of the marked reference field within `obj_`.
989 Location field_offset = index_;
990 DCHECK(field_offset.IsRegisterPair()) << field_offset;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000991
992 __ Bind(GetEntryLabel());
993
Roland Levillainff487002017-03-07 16:50:01 +0000994 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM's:
995 //
996 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
997 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
998 // HeapReference<mirror::Object> ref = *src; // Original reference load.
999 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1000 // if (is_gray) {
1001 // old_ref = ref;
1002 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1003 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1004 // }
1005
Roland Levillain54f869e2017-03-06 13:54:11 +00001006 // /* int32_t */ monitor = obj->monitor_
1007 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1008 __ LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1009 if (needs_null_check_) {
1010 codegen->MaybeRecordImplicitNullCheck(instruction_);
1011 }
1012 // /* LockWord */ lock_word = LockWord(monitor)
1013 static_assert(sizeof(LockWord) == sizeof(int32_t),
1014 "art::LockWord and int32_t have different sizes.");
1015
1016 // Introduce a dependency on the lock_word including the rb_state,
1017 // which shall prevent load-load reordering without using
1018 // a memory barrier (which would be more expensive).
1019 // `obj` is unchanged by this operation, but its value now depends
1020 // on `temp1`.
1021 __ add(obj_, obj_, ShifterOperand(temp1_, LSR, 32));
1022
1023 // The actual reference load.
1024 // A possible implicit null check has already been handled above.
1025 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1026 arm_codegen->GenerateRawReferenceLoad(
1027 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1028
1029 // Mark the object `ref` when `obj` is gray.
1030 //
1031 // if (rb_state == ReadBarrier::GrayState())
1032 // ref = ReadBarrier::Mark(ref);
1033 //
1034 // Given the numeric representation, it's enough to check the low bit of the
1035 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1036 // which can be a 16-bit instruction unlike the TST immediate.
1037 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1038 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1039 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1040 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
1041
1042 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001043 // Note that we cannot use IP to save the old reference, as IP is
1044 // used internally by the ReadBarrierMarkRegX entry point, and we
1045 // need the old reference after the call to that entry point.
1046 DCHECK_NE(temp1_, IP);
1047 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001048
Roland Levillain54f869e2017-03-06 13:54:11 +00001049 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001050
1051 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001052 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001053 //
1054 // Note that this field could also hold a different object, if
1055 // another thread had concurrently changed it. In that case, the
1056 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1057 // (CAS) operation below would abort the CAS, leaving the field
1058 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001059 __ cmp(temp1_, ShifterOperand(ref_reg));
Roland Levillain54f869e2017-03-06 13:54:11 +00001060 __ b(GetExitLabel(), EQ);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001061
1062 // Update the the holder's field atomically. This may fail if
1063 // mutator updates before us, but it's OK. This is achieved
1064 // using a strong compare-and-set (CAS) operation with relaxed
1065 // memory synchronization ordering, where the expected value is
1066 // the old reference and the desired value is the new reference.
1067
1068 // Convenience aliases.
1069 Register base = obj_;
1070 // The UnsafeCASObject intrinsic uses a register pair as field
1071 // offset ("long offset"), of which only the low part contains
1072 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001073 Register offset = field_offset.AsRegisterPairLow<Register>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001074 Register expected = temp1_;
1075 Register value = ref_reg;
1076 Register tmp_ptr = IP; // Pointer to actual memory.
1077 Register tmp = temp2_; // Value in memory.
1078
1079 __ add(tmp_ptr, base, ShifterOperand(offset));
1080
1081 if (kPoisonHeapReferences) {
1082 __ PoisonHeapReference(expected);
1083 if (value == expected) {
1084 // Do not poison `value`, as it is the same register as
1085 // `expected`, which has just been poisoned.
1086 } else {
1087 __ PoisonHeapReference(value);
1088 }
1089 }
1090
1091 // do {
1092 // tmp = [r_ptr] - expected;
1093 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1094
Roland Levillain24a4d112016-10-26 13:10:46 +01001095 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001096 __ Bind(&loop_head);
1097
1098 __ ldrex(tmp, tmp_ptr);
1099
1100 __ subs(tmp, tmp, ShifterOperand(expected));
1101
Roland Levillain24a4d112016-10-26 13:10:46 +01001102 __ it(NE);
1103 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001104
Roland Levillain24a4d112016-10-26 13:10:46 +01001105 __ b(&exit_loop, NE);
1106
1107 __ strex(tmp, value, tmp_ptr);
1108 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001109 __ b(&loop_head, EQ);
1110
Roland Levillain24a4d112016-10-26 13:10:46 +01001111 __ Bind(&exit_loop);
1112
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001113 if (kPoisonHeapReferences) {
1114 __ UnpoisonHeapReference(expected);
1115 if (value == expected) {
1116 // Do not unpoison `value`, as it is the same register as
1117 // `expected`, which has just been unpoisoned.
1118 } else {
1119 __ UnpoisonHeapReference(value);
1120 }
1121 }
1122
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001123 __ b(GetExitLabel());
1124 }
1125
1126 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001127 // The register containing the object holding the marked object reference field.
1128 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001129 // The offset, index and scale factor to access the reference in `obj_`.
1130 uint32_t offset_;
1131 Location index_;
1132 ScaleFactor scale_factor_;
1133 // Is a null check required?
1134 bool needs_null_check_;
1135 // A temporary register used to hold the lock word of `obj_`; and
1136 // also to hold the original reference value, when the reference is
1137 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001138 const Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001139 // A temporary register used in the implementation of the CAS, to
1140 // update the object's reference field.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001141 const Register temp2_;
1142
Roland Levillain54f869e2017-03-06 13:54:11 +00001143 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001144};
1145
Roland Levillain3b359c72015-11-17 19:35:12 +00001146// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001147class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001148 public:
1149 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
1150 Location out,
1151 Location ref,
1152 Location obj,
1153 uint32_t offset,
1154 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001155 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +00001156 out_(out),
1157 ref_(ref),
1158 obj_(obj),
1159 offset_(offset),
1160 index_(index) {
1161 DCHECK(kEmitCompilerReadBarrier);
1162 // If `obj` is equal to `out` or `ref`, it means the initial object
1163 // has been overwritten by (or after) the heap object reference load
1164 // to be instrumented, e.g.:
1165 //
1166 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00001167 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001168 //
1169 // In that case, we have lost the information about the original
1170 // object, and the emitted read barrier cannot work properly.
1171 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1172 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1173 }
1174
1175 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1176 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1177 LocationSummary* locations = instruction_->GetLocations();
1178 Register reg_out = out_.AsRegister<Register>();
1179 DCHECK(locations->CanCall());
1180 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +01001181 DCHECK(instruction_->IsInstanceFieldGet() ||
1182 instruction_->IsStaticFieldGet() ||
1183 instruction_->IsArrayGet() ||
1184 instruction_->IsInstanceOf() ||
1185 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001186 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +00001187 << "Unexpected instruction in read barrier for heap reference slow path: "
1188 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001189 // The read barrier instrumentation of object ArrayGet
1190 // instructions does not support the HIntermediateAddress
1191 // instruction.
1192 DCHECK(!(instruction_->IsArrayGet() &&
1193 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +00001194
1195 __ Bind(GetEntryLabel());
1196 SaveLiveRegisters(codegen, locations);
1197
1198 // We may have to change the index's value, but as `index_` is a
1199 // constant member (like other "inputs" of this slow path),
1200 // introduce a copy of it, `index`.
1201 Location index = index_;
1202 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001203 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +00001204 if (instruction_->IsArrayGet()) {
1205 // Compute the actual memory offset and store it in `index`.
1206 Register index_reg = index_.AsRegister<Register>();
1207 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
1208 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
1209 // We are about to change the value of `index_reg` (see the
1210 // calls to art::arm::Thumb2Assembler::Lsl and
1211 // art::arm::Thumb2Assembler::AddConstant below), but it has
1212 // not been saved by the previous call to
1213 // art::SlowPathCode::SaveLiveRegisters, as it is a
1214 // callee-save register --
1215 // art::SlowPathCode::SaveLiveRegisters does not consider
1216 // callee-save registers, as it has been designed with the
1217 // assumption that callee-save registers are supposed to be
1218 // handled by the called function. So, as a callee-save
1219 // register, `index_reg` _would_ eventually be saved onto
1220 // the stack, but it would be too late: we would have
1221 // changed its value earlier. Therefore, we manually save
1222 // it here into another freely available register,
1223 // `free_reg`, chosen of course among the caller-save
1224 // registers (as a callee-save `free_reg` register would
1225 // exhibit the same problem).
1226 //
1227 // Note we could have requested a temporary register from
1228 // the register allocator instead; but we prefer not to, as
1229 // this is a slow path, and we know we can find a
1230 // caller-save register that is available.
1231 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1232 __ Mov(free_reg, index_reg);
1233 index_reg = free_reg;
1234 index = Location::RegisterLocation(index_reg);
1235 } else {
1236 // The initial register stored in `index_` has already been
1237 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1238 // (as it is not a callee-save register), so we can freely
1239 // use it.
1240 }
1241 // Shifting the index value contained in `index_reg` by the scale
1242 // factor (2) cannot overflow in practice, as the runtime is
1243 // unable to allocate object arrays with a size larger than
1244 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1245 __ Lsl(index_reg, index_reg, TIMES_4);
1246 static_assert(
1247 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1248 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1249 __ AddConstant(index_reg, index_reg, offset_);
1250 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001251 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1252 // intrinsics, `index_` is not shifted by a scale factor of 2
1253 // (as in the case of ArrayGet), as it is actually an offset
1254 // to an object field within an object.
1255 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001256 DCHECK(instruction_->GetLocations()->Intrinsified());
1257 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1258 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1259 << instruction_->AsInvoke()->GetIntrinsic();
1260 DCHECK_EQ(offset_, 0U);
1261 DCHECK(index_.IsRegisterPair());
1262 // UnsafeGet's offset location is a register pair, the low
1263 // part contains the correct offset.
1264 index = index_.ToLow();
1265 }
1266 }
1267
1268 // We're moving two or three locations to locations that could
1269 // overlap, so we need a parallel move resolver.
1270 InvokeRuntimeCallingConvention calling_convention;
1271 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1272 parallel_move.AddMove(ref_,
1273 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1274 Primitive::kPrimNot,
1275 nullptr);
1276 parallel_move.AddMove(obj_,
1277 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1278 Primitive::kPrimNot,
1279 nullptr);
1280 if (index.IsValid()) {
1281 parallel_move.AddMove(index,
1282 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1283 Primitive::kPrimInt,
1284 nullptr);
1285 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1286 } else {
1287 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1288 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1289 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001290 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001291 CheckEntrypointTypes<
1292 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1293 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1294
1295 RestoreLiveRegisters(codegen, locations);
1296 __ b(GetExitLabel());
1297 }
1298
1299 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1300
1301 private:
1302 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1303 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1304 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1305 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1306 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1307 return static_cast<Register>(i);
1308 }
1309 }
1310 // We shall never fail to find a free caller-save register, as
1311 // there are more than two core caller-save registers on ARM
1312 // (meaning it is possible to find one which is different from
1313 // `ref` and `obj`).
1314 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1315 LOG(FATAL) << "Could not find a free caller-save register";
1316 UNREACHABLE();
1317 }
1318
Roland Levillain3b359c72015-11-17 19:35:12 +00001319 const Location out_;
1320 const Location ref_;
1321 const Location obj_;
1322 const uint32_t offset_;
1323 // An additional location containing an index to an array.
1324 // Only used for HArrayGet and the UnsafeGetObject &
1325 // UnsafeGetObjectVolatile intrinsics.
1326 const Location index_;
1327
1328 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1329};
1330
1331// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001332class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001333 public:
1334 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001335 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001336 DCHECK(kEmitCompilerReadBarrier);
1337 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001338
1339 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1340 LocationSummary* locations = instruction_->GetLocations();
1341 Register reg_out = out_.AsRegister<Register>();
1342 DCHECK(locations->CanCall());
1343 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001344 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1345 << "Unexpected instruction in read barrier for GC root slow path: "
1346 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001347
1348 __ Bind(GetEntryLabel());
1349 SaveLiveRegisters(codegen, locations);
1350
1351 InvokeRuntimeCallingConvention calling_convention;
1352 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1353 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001354 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001355 instruction_,
1356 instruction_->GetDexPc(),
1357 this);
1358 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1359 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1360
1361 RestoreLiveRegisters(codegen, locations);
1362 __ b(GetExitLabel());
1363 }
1364
1365 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1366
1367 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001368 const Location out_;
1369 const Location root_;
1370
1371 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1372};
1373
Aart Bike9f37602015-10-09 11:15:55 -07001374inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001375 switch (cond) {
1376 case kCondEQ: return EQ;
1377 case kCondNE: return NE;
1378 case kCondLT: return LT;
1379 case kCondLE: return LE;
1380 case kCondGT: return GT;
1381 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001382 case kCondB: return LO;
1383 case kCondBE: return LS;
1384 case kCondA: return HI;
1385 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001386 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001387 LOG(FATAL) << "Unreachable";
1388 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001389}
1390
Aart Bike9f37602015-10-09 11:15:55 -07001391// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001392inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001393 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001394 case kCondEQ: return EQ;
1395 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001396 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001397 case kCondLT: return LO;
1398 case kCondLE: return LS;
1399 case kCondGT: return HI;
1400 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001401 // Unsigned remain unchanged.
1402 case kCondB: return LO;
1403 case kCondBE: return LS;
1404 case kCondA: return HI;
1405 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001406 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001407 LOG(FATAL) << "Unreachable";
1408 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001409}
1410
Vladimir Markod6e069b2016-01-18 11:11:01 +00001411inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1412 // The ARM condition codes can express all the necessary branches, see the
1413 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1414 // There is no dex instruction or HIR that would need the missing conditions
1415 // "equal or unordered" or "not equal".
1416 switch (cond) {
1417 case kCondEQ: return EQ;
1418 case kCondNE: return NE /* unordered */;
1419 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1420 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1421 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1422 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1423 default:
1424 LOG(FATAL) << "UNREACHABLE";
1425 UNREACHABLE();
1426 }
1427}
1428
Anton Kirilov74234da2017-01-13 14:42:47 +00001429inline Shift ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1430 switch (op_kind) {
1431 case HDataProcWithShifterOp::kASR: return ASR;
1432 case HDataProcWithShifterOp::kLSL: return LSL;
1433 case HDataProcWithShifterOp::kLSR: return LSR;
1434 default:
1435 LOG(FATAL) << "Unexpected op kind " << op_kind;
1436 UNREACHABLE();
1437 }
1438}
1439
1440static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1441 Register out,
1442 Register first,
1443 const ShifterOperand& second,
1444 CodeGeneratorARM* codegen) {
1445 if (second.IsImmediate() && second.GetImmediate() == 0) {
1446 const ShifterOperand in = kind == HInstruction::kAnd
1447 ? ShifterOperand(0)
1448 : ShifterOperand(first);
1449
1450 __ mov(out, in);
1451 } else {
1452 switch (kind) {
1453 case HInstruction::kAdd:
1454 __ add(out, first, second);
1455 break;
1456 case HInstruction::kAnd:
1457 __ and_(out, first, second);
1458 break;
1459 case HInstruction::kOr:
1460 __ orr(out, first, second);
1461 break;
1462 case HInstruction::kSub:
1463 __ sub(out, first, second);
1464 break;
1465 case HInstruction::kXor:
1466 __ eor(out, first, second);
1467 break;
1468 default:
1469 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1470 UNREACHABLE();
1471 }
1472 }
1473}
1474
1475static void GenerateDataProc(HInstruction::InstructionKind kind,
1476 const Location& out,
1477 const Location& first,
1478 const ShifterOperand& second_lo,
1479 const ShifterOperand& second_hi,
1480 CodeGeneratorARM* codegen) {
1481 const Register first_hi = first.AsRegisterPairHigh<Register>();
1482 const Register first_lo = first.AsRegisterPairLow<Register>();
1483 const Register out_hi = out.AsRegisterPairHigh<Register>();
1484 const Register out_lo = out.AsRegisterPairLow<Register>();
1485
1486 if (kind == HInstruction::kAdd) {
1487 __ adds(out_lo, first_lo, second_lo);
1488 __ adc(out_hi, first_hi, second_hi);
1489 } else if (kind == HInstruction::kSub) {
1490 __ subs(out_lo, first_lo, second_lo);
1491 __ sbc(out_hi, first_hi, second_hi);
1492 } else {
1493 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1494 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1495 }
1496}
1497
1498static ShifterOperand GetShifterOperand(Register rm, Shift shift, uint32_t shift_imm) {
1499 return shift_imm == 0 ? ShifterOperand(rm) : ShifterOperand(rm, shift, shift_imm);
1500}
1501
1502static void GenerateLongDataProc(HDataProcWithShifterOp* instruction, CodeGeneratorARM* codegen) {
1503 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1504 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1505
1506 const LocationSummary* const locations = instruction->GetLocations();
1507 const uint32_t shift_value = instruction->GetShiftAmount();
1508 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1509 const Location first = locations->InAt(0);
1510 const Location second = locations->InAt(1);
1511 const Location out = locations->Out();
1512 const Register first_hi = first.AsRegisterPairHigh<Register>();
1513 const Register first_lo = first.AsRegisterPairLow<Register>();
1514 const Register out_hi = out.AsRegisterPairHigh<Register>();
1515 const Register out_lo = out.AsRegisterPairLow<Register>();
1516 const Register second_hi = second.AsRegisterPairHigh<Register>();
1517 const Register second_lo = second.AsRegisterPairLow<Register>();
1518 const Shift shift = ShiftFromOpKind(instruction->GetOpKind());
1519
1520 if (shift_value >= 32) {
1521 if (shift == LSL) {
1522 GenerateDataProcInstruction(kind,
1523 out_hi,
1524 first_hi,
1525 ShifterOperand(second_lo, LSL, shift_value - 32),
1526 codegen);
1527 GenerateDataProcInstruction(kind,
1528 out_lo,
1529 first_lo,
1530 ShifterOperand(0),
1531 codegen);
1532 } else if (shift == ASR) {
1533 GenerateDataProc(kind,
1534 out,
1535 first,
1536 GetShifterOperand(second_hi, ASR, shift_value - 32),
1537 ShifterOperand(second_hi, ASR, 31),
1538 codegen);
1539 } else {
1540 DCHECK_EQ(shift, LSR);
1541 GenerateDataProc(kind,
1542 out,
1543 first,
1544 GetShifterOperand(second_hi, LSR, shift_value - 32),
1545 ShifterOperand(0),
1546 codegen);
1547 }
1548 } else {
1549 DCHECK_GT(shift_value, 1U);
1550 DCHECK_LT(shift_value, 32U);
1551
1552 if (shift == LSL) {
1553 // We are not doing this for HInstruction::kAdd because the output will require
1554 // Location::kOutputOverlap; not applicable to other cases.
1555 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1556 GenerateDataProcInstruction(kind,
1557 out_hi,
1558 first_hi,
1559 ShifterOperand(second_hi, LSL, shift_value),
1560 codegen);
1561 GenerateDataProcInstruction(kind,
1562 out_hi,
1563 out_hi,
1564 ShifterOperand(second_lo, LSR, 32 - shift_value),
1565 codegen);
1566 GenerateDataProcInstruction(kind,
1567 out_lo,
1568 first_lo,
1569 ShifterOperand(second_lo, LSL, shift_value),
1570 codegen);
1571 } else {
1572 __ Lsl(IP, second_hi, shift_value);
1573 __ orr(IP, IP, ShifterOperand(second_lo, LSR, 32 - shift_value));
1574 GenerateDataProc(kind,
1575 out,
1576 first,
1577 ShifterOperand(second_lo, LSL, shift_value),
1578 ShifterOperand(IP),
1579 codegen);
1580 }
1581 } else {
1582 DCHECK(shift == ASR || shift == LSR);
1583
1584 // We are not doing this for HInstruction::kAdd because the output will require
1585 // Location::kOutputOverlap; not applicable to other cases.
1586 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1587 GenerateDataProcInstruction(kind,
1588 out_lo,
1589 first_lo,
1590 ShifterOperand(second_lo, LSR, shift_value),
1591 codegen);
1592 GenerateDataProcInstruction(kind,
1593 out_lo,
1594 out_lo,
1595 ShifterOperand(second_hi, LSL, 32 - shift_value),
1596 codegen);
1597 GenerateDataProcInstruction(kind,
1598 out_hi,
1599 first_hi,
1600 ShifterOperand(second_hi, shift, shift_value),
1601 codegen);
1602 } else {
1603 __ Lsr(IP, second_lo, shift_value);
1604 __ orr(IP, IP, ShifterOperand(second_hi, LSL, 32 - shift_value));
1605 GenerateDataProc(kind,
1606 out,
1607 first,
1608 ShifterOperand(IP),
1609 ShifterOperand(second_hi, shift, shift_value),
1610 codegen);
1611 }
1612 }
1613 }
1614}
1615
Donghui Bai426b49c2016-11-08 14:55:38 +08001616static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARM* codegen) {
1617 Primitive::Type type = instruction->InputAt(0)->GetType();
1618 Location lhs_loc = instruction->GetLocations()->InAt(0);
1619 Location rhs_loc = instruction->GetLocations()->InAt(1);
1620 if (rhs_loc.IsConstant()) {
1621 // 0.0 is the only immediate that can be encoded directly in
1622 // a VCMP instruction.
1623 //
1624 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1625 // specify that in a floating-point comparison, positive zero
1626 // and negative zero are considered equal, so we can use the
1627 // literal 0.0 for both cases here.
1628 //
1629 // Note however that some methods (Float.equal, Float.compare,
1630 // Float.compareTo, Double.equal, Double.compare,
1631 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1632 // StrictMath.min) consider 0.0 to be (strictly) greater than
1633 // -0.0. So if we ever translate calls to these methods into a
1634 // HCompare instruction, we must handle the -0.0 case with
1635 // care here.
1636 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1637 if (type == Primitive::kPrimFloat) {
1638 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1639 } else {
1640 DCHECK_EQ(type, Primitive::kPrimDouble);
1641 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1642 }
1643 } else {
1644 if (type == Primitive::kPrimFloat) {
1645 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1646 } else {
1647 DCHECK_EQ(type, Primitive::kPrimDouble);
1648 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1649 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1650 }
1651 }
1652}
1653
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001654static std::pair<Condition, Condition> GenerateLongTestConstant(HCondition* condition,
1655 bool invert,
1656 CodeGeneratorARM* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001657 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1658
1659 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001660 IfCondition cond = condition->GetCondition();
1661 IfCondition opposite = condition->GetOppositeCondition();
1662
1663 if (invert) {
1664 std::swap(cond, opposite);
1665 }
1666
1667 std::pair<Condition, Condition> ret;
Donghui Bai426b49c2016-11-08 14:55:38 +08001668 const Location left = locations->InAt(0);
1669 const Location right = locations->InAt(1);
1670
1671 DCHECK(right.IsConstant());
1672
1673 const Register left_high = left.AsRegisterPairHigh<Register>();
1674 const Register left_low = left.AsRegisterPairLow<Register>();
1675 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1676
1677 switch (cond) {
1678 case kCondEQ:
1679 case kCondNE:
1680 case kCondB:
1681 case kCondBE:
1682 case kCondA:
1683 case kCondAE:
1684 __ CmpConstant(left_high, High32Bits(value));
1685 __ it(EQ);
1686 __ cmp(left_low, ShifterOperand(Low32Bits(value)), EQ);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001687 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001688 break;
1689 case kCondLE:
1690 case kCondGT:
1691 // Trivially true or false.
1692 if (value == std::numeric_limits<int64_t>::max()) {
1693 __ cmp(left_low, ShifterOperand(left_low));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001694 ret = cond == kCondLE ? std::make_pair(EQ, NE) : std::make_pair(NE, EQ);
Donghui Bai426b49c2016-11-08 14:55:38 +08001695 break;
1696 }
1697
1698 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001699 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001700 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001701 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001702 } else {
1703 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001704 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001705 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001706 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001707 }
1708
1709 value++;
1710 FALLTHROUGH_INTENDED;
1711 case kCondGE:
1712 case kCondLT:
1713 __ CmpConstant(left_low, Low32Bits(value));
1714 __ sbcs(IP, left_high, ShifterOperand(High32Bits(value)));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001715 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001716 break;
1717 default:
1718 LOG(FATAL) << "Unreachable";
1719 UNREACHABLE();
1720 }
1721
1722 return ret;
1723}
1724
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001725static std::pair<Condition, Condition> GenerateLongTest(HCondition* condition,
1726 bool invert,
1727 CodeGeneratorARM* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001728 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1729
1730 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001731 IfCondition cond = condition->GetCondition();
1732 IfCondition opposite = condition->GetOppositeCondition();
1733
1734 if (invert) {
1735 std::swap(cond, opposite);
1736 }
1737
1738 std::pair<Condition, Condition> ret;
Donghui Bai426b49c2016-11-08 14:55:38 +08001739 Location left = locations->InAt(0);
1740 Location right = locations->InAt(1);
1741
1742 DCHECK(right.IsRegisterPair());
1743
1744 switch (cond) {
1745 case kCondEQ:
1746 case kCondNE:
1747 case kCondB:
1748 case kCondBE:
1749 case kCondA:
1750 case kCondAE:
1751 __ cmp(left.AsRegisterPairHigh<Register>(),
1752 ShifterOperand(right.AsRegisterPairHigh<Register>()));
1753 __ it(EQ);
1754 __ cmp(left.AsRegisterPairLow<Register>(),
1755 ShifterOperand(right.AsRegisterPairLow<Register>()),
1756 EQ);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001757 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001758 break;
1759 case kCondLE:
1760 case kCondGT:
1761 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001762 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001763 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001764 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001765 } else {
1766 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001767 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001768 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001769 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001770 }
1771
1772 std::swap(left, right);
1773 FALLTHROUGH_INTENDED;
1774 case kCondGE:
1775 case kCondLT:
1776 __ cmp(left.AsRegisterPairLow<Register>(),
1777 ShifterOperand(right.AsRegisterPairLow<Register>()));
1778 __ sbcs(IP,
1779 left.AsRegisterPairHigh<Register>(),
1780 ShifterOperand(right.AsRegisterPairHigh<Register>()));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001781 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001782 break;
1783 default:
1784 LOG(FATAL) << "Unreachable";
1785 UNREACHABLE();
1786 }
1787
1788 return ret;
1789}
1790
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001791static std::pair<Condition, Condition> GenerateTest(HCondition* condition,
1792 bool invert,
1793 CodeGeneratorARM* codegen) {
1794 const LocationSummary* const locations = condition->GetLocations();
1795 const Primitive::Type type = condition->GetLeft()->GetType();
1796 IfCondition cond = condition->GetCondition();
1797 IfCondition opposite = condition->GetOppositeCondition();
1798 std::pair<Condition, Condition> ret;
1799 const Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08001800
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001801 if (invert) {
1802 std::swap(cond, opposite);
1803 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001804
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001805 if (type == Primitive::kPrimLong) {
1806 ret = locations->InAt(1).IsConstant()
1807 ? GenerateLongTestConstant(condition, invert, codegen)
1808 : GenerateLongTest(condition, invert, codegen);
1809 } else if (Primitive::IsFloatingPointType(type)) {
1810 GenerateVcmp(condition, codegen);
1811 __ vmstat();
1812 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1813 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001814 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001815 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08001816
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001817 const Register left = locations->InAt(0).AsRegister<Register>();
1818
1819 if (right.IsRegister()) {
1820 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001821 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001822 DCHECK(right.IsConstant());
1823 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001824 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001825
1826 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001827 }
1828
1829 return ret;
1830}
1831
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001832static bool CanGenerateTest(HCondition* condition, ArmAssembler* assembler) {
1833 if (condition->GetLeft()->GetType() == Primitive::kPrimLong) {
1834 const LocationSummary* const locations = condition->GetLocations();
1835 const IfCondition c = condition->GetCondition();
Donghui Bai426b49c2016-11-08 14:55:38 +08001836
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001837 if (locations->InAt(1).IsConstant()) {
1838 const int64_t value = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
1839 ShifterOperand so;
Donghui Bai426b49c2016-11-08 14:55:38 +08001840
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001841 if (c < kCondLT || c > kCondGE) {
1842 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1843 // we check that the least significant half of the first input to be compared
1844 // is in a low register (the other half is read outside an IT block), and
1845 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
1846 // encoding can be used.
1847 if (!ArmAssembler::IsLowRegister(locations->InAt(0).AsRegisterPairLow<Register>()) ||
1848 !IsUint<8>(Low32Bits(value))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001849 return false;
1850 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001851 } else if (c == kCondLE || c == kCondGT) {
1852 if (value < std::numeric_limits<int64_t>::max() &&
1853 !assembler->ShifterOperandCanHold(kNoRegister,
1854 kNoRegister,
1855 SBC,
1856 High32Bits(value + 1),
1857 kCcSet,
1858 &so)) {
1859 return false;
1860 }
1861 } else if (!assembler->ShifterOperandCanHold(kNoRegister,
1862 kNoRegister,
1863 SBC,
1864 High32Bits(value),
1865 kCcSet,
1866 &so)) {
1867 return false;
Donghui Bai426b49c2016-11-08 14:55:38 +08001868 }
1869 }
1870 }
1871
1872 return true;
1873}
1874
1875static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
1876 const Primitive::Type type = constant->GetType();
1877 bool ret = false;
1878
1879 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1880
1881 if (type == Primitive::kPrimLong) {
1882 const uint64_t value = constant->AsLongConstant()->GetValueAsUint64();
1883
1884 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1885 } else {
1886 ret = IsUint<8>(CodeGenerator::GetInt32ValueOf(constant));
1887 }
1888
1889 return ret;
1890}
1891
1892static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
1893 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
1894
1895 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1896 return Location::ConstantLocation(constant->AsConstant());
1897 }
1898
1899 return Location::RequiresRegister();
1900}
1901
1902static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1903 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1904 // we check that we are not dealing with floating-point output (there is no
1905 // 16-bit VMOV encoding).
1906 if (!out.IsRegister() && !out.IsRegisterPair()) {
1907 return false;
1908 }
1909
1910 // For constants, we also check that the output is in one or two low registers,
1911 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1912 // MOV encoding can be used.
1913 if (src.IsConstant()) {
1914 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1915 return false;
1916 }
1917
1918 if (out.IsRegister()) {
1919 if (!ArmAssembler::IsLowRegister(out.AsRegister<Register>())) {
1920 return false;
1921 }
1922 } else {
1923 DCHECK(out.IsRegisterPair());
1924
1925 if (!ArmAssembler::IsLowRegister(out.AsRegisterPairHigh<Register>())) {
1926 return false;
1927 }
1928 }
1929 }
1930
1931 return true;
1932}
1933
Anton Kirilov74234da2017-01-13 14:42:47 +00001934#undef __
1935// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1936#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
1937
Donghui Bai426b49c2016-11-08 14:55:38 +08001938Label* CodeGeneratorARM::GetFinalLabel(HInstruction* instruction, Label* final_label) {
1939 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001940 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001941
1942 const HBasicBlock* const block = instruction->GetBlock();
1943 const HLoopInformation* const info = block->GetLoopInformation();
1944 HInstruction* const next = instruction->GetNext();
1945
1946 // Avoid a branch to a branch.
1947 if (next->IsGoto() && (info == nullptr ||
1948 !info->IsBackEdge(*block) ||
1949 !info->HasSuspendCheck())) {
1950 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1951 }
1952
1953 return final_label;
1954}
1955
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001956void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001957 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001958}
1959
1960void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001961 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001962}
1963
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001964size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1965 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1966 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001967}
1968
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001969size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1970 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1971 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001972}
1973
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001974size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1975 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1976 return kArmWordSize;
1977}
1978
1979size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1980 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1981 return kArmWordSize;
1982}
1983
Calin Juravle34166012014-12-19 17:22:29 +00001984CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001985 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001986 const CompilerOptions& compiler_options,
1987 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001988 : CodeGenerator(graph,
1989 kNumberOfCoreRegisters,
1990 kNumberOfSRegisters,
1991 kNumberOfRegisterPairs,
1992 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1993 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001994 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1995 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001996 compiler_options,
1997 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001998 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001999 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002000 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01002001 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01002002 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00002003 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002004 uint32_literals_(std::less<uint32_t>(),
2005 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002006 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2007 boot_image_string_patches_(StringReferenceValueComparator(),
2008 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2009 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002010 boot_image_type_patches_(TypeReferenceValueComparator(),
2011 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2012 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00002013 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01002014 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00002015 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00002016 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2017 jit_class_patches_(TypeReferenceValueComparator(),
2018 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07002019 // Always save the LR register to mimic Quick.
2020 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01002021}
2022
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002023void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
2024 // Ensure that we fix up branches and literal loads and emit the literal pool.
2025 __ FinalizeCode();
2026
2027 // Adjust native pc offsets in stack maps.
2028 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08002029 uint32_t old_position =
2030 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002031 uint32_t new_position = __ GetAdjustedPosition(old_position);
2032 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
2033 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01002034 // Adjust pc offsets for the disassembly information.
2035 if (disasm_info_ != nullptr) {
2036 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
2037 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
2038 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
2039 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
2040 it.second.start = __ GetAdjustedPosition(it.second.start);
2041 it.second.end = __ GetAdjustedPosition(it.second.end);
2042 }
2043 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
2044 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
2045 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
2046 }
2047 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002048
2049 CodeGenerator::Finalize(allocator);
2050}
2051
David Brazdil58282f42016-01-14 12:45:10 +00002052void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002053 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002054 blocked_core_registers_[SP] = true;
2055 blocked_core_registers_[LR] = true;
2056 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002057
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002058 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002059 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002060
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002061 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002062 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002063
David Brazdil58282f42016-01-14 12:45:10 +00002064 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01002065 // Stubs do not save callee-save floating point registers. If the graph
2066 // is debuggable, we need to deal with these registers differently. For
2067 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002068 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
2069 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
2070 }
2071 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002072}
2073
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002074InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002075 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002076 assembler_(codegen->GetAssembler()),
2077 codegen_(codegen) {}
2078
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002079void CodeGeneratorARM::ComputeSpillMask() {
2080 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2081 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00002082 // There is no easy instruction to restore just the PC on thumb2. We spill and
2083 // restore another arbitrary register.
2084 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002085 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2086 // We use vpush and vpop for saving and restoring floating point registers, which take
2087 // a SRegister and the number of registers to save/restore after that SRegister. We
2088 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2089 // but in the range.
2090 if (fpu_spill_mask_ != 0) {
2091 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2092 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2093 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2094 fpu_spill_mask_ |= (1 << i);
2095 }
2096 }
2097}
2098
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002099static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002100 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002101}
2102
2103static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002104 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002105}
2106
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002107void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00002108 bool skip_overflow_check =
2109 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002110 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002111 __ Bind(&frame_entry_label_);
2112
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002113 if (HasEmptyFrame()) {
2114 return;
2115 }
2116
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002117 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002118 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
2119 __ LoadFromOffset(kLoadWord, IP, IP, 0);
2120 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002121 }
2122
Andreas Gampe501fd632015-09-10 16:11:06 -07002123 __ PushList(core_spill_mask_);
2124 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2125 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002126 if (fpu_spill_mask_ != 0) {
2127 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2128 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002129 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01002130 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002131 }
Mingyao Yang063fc772016-08-02 11:02:54 -07002132
2133 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2134 // Initialize should_deoptimize flag to 0.
2135 __ mov(IP, ShifterOperand(0));
2136 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
2137 }
2138
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002139 int adjust = GetFrameSize() - FrameEntrySpillSize();
2140 __ AddConstant(SP, -adjust);
2141 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002142
2143 // Save the current method if we need it. Note that we do not
2144 // do this in HCurrentMethod, as the instruction might have been removed
2145 // in the SSA graph.
2146 if (RequiresCurrentMethod()) {
2147 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
2148 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002149}
2150
2151void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002152 if (HasEmptyFrame()) {
2153 __ bx(LR);
2154 return;
2155 }
David Srbeckyc34dc932015-04-12 09:27:43 +01002156 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002157 int adjust = GetFrameSize() - FrameEntrySpillSize();
2158 __ AddConstant(SP, adjust);
2159 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002160 if (fpu_spill_mask_ != 0) {
2161 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2162 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07002163 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002164 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002165 }
Andreas Gampe501fd632015-09-10 16:11:06 -07002166 // Pop LR into PC to return.
2167 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
2168 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
2169 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01002170 __ cfi().RestoreState();
2171 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002172}
2173
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002174void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07002175 Label* label = GetLabelOf(block);
2176 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002177}
2178
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002179Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002180 switch (type) {
2181 case Primitive::kPrimBoolean:
2182 case Primitive::kPrimByte:
2183 case Primitive::kPrimChar:
2184 case Primitive::kPrimShort:
2185 case Primitive::kPrimInt:
2186 case Primitive::kPrimNot: {
2187 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002188 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002189 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002190 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002191 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002192 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002193 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002194 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002195
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002196 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002197 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002198 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002199 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002200 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002201 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002202 if (calling_convention.GetRegisterAt(index) == R1) {
2203 // Skip R1, and use R2_R3 instead.
2204 gp_index_++;
2205 index++;
2206 }
2207 }
2208 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2209 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002210 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01002211
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002212 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002213 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002214 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002215 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2216 }
2217 }
2218
2219 case Primitive::kPrimFloat: {
2220 uint32_t stack_index = stack_index_++;
2221 if (float_index_ % 2 == 0) {
2222 float_index_ = std::max(double_index_, float_index_);
2223 }
2224 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2225 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
2226 } else {
2227 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2228 }
2229 }
2230
2231 case Primitive::kPrimDouble: {
2232 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2233 uint32_t stack_index = stack_index_;
2234 stack_index_ += 2;
2235 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2236 uint32_t index = double_index_;
2237 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002238 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002239 calling_convention.GetFpuRegisterAt(index),
2240 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002241 DCHECK(ExpectedPairLayout(result));
2242 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002243 } else {
2244 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002245 }
2246 }
2247
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002248 case Primitive::kPrimVoid:
2249 LOG(FATAL) << "Unexpected parameter type " << type;
2250 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002251 }
Roland Levillain3b359c72015-11-17 19:35:12 +00002252 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002253}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002254
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002255Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002256 switch (type) {
2257 case Primitive::kPrimBoolean:
2258 case Primitive::kPrimByte:
2259 case Primitive::kPrimChar:
2260 case Primitive::kPrimShort:
2261 case Primitive::kPrimInt:
2262 case Primitive::kPrimNot: {
2263 return Location::RegisterLocation(R0);
2264 }
2265
2266 case Primitive::kPrimFloat: {
2267 return Location::FpuRegisterLocation(S0);
2268 }
2269
2270 case Primitive::kPrimLong: {
2271 return Location::RegisterPairLocation(R0, R1);
2272 }
2273
2274 case Primitive::kPrimDouble: {
2275 return Location::FpuRegisterPairLocation(S0, S1);
2276 }
2277
2278 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00002279 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002280 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002281
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002282 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002283}
2284
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002285Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
2286 return Location::RegisterLocation(kMethodRegisterArgument);
2287}
2288
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002289void CodeGeneratorARM::Move32(Location destination, Location source) {
2290 if (source.Equals(destination)) {
2291 return;
2292 }
2293 if (destination.IsRegister()) {
2294 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002295 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002296 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002297 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002298 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002299 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002300 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002301 } else if (destination.IsFpuRegister()) {
2302 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002303 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002304 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002305 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002306 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002307 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002308 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002309 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002310 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002311 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002312 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002313 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002314 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002315 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002316 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002317 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
2318 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002319 }
2320 }
2321}
2322
2323void CodeGeneratorARM::Move64(Location destination, Location source) {
2324 if (source.Equals(destination)) {
2325 return;
2326 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002327 if (destination.IsRegisterPair()) {
2328 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002329 EmitParallelMoves(
2330 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
2331 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002332 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002333 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002334 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
2335 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002336 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002337 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002338 } else if (source.IsFpuRegisterPair()) {
2339 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
2340 destination.AsRegisterPairHigh<Register>(),
2341 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002342 } else {
2343 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002344 DCHECK(ExpectedPairLayout(destination));
2345 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
2346 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002347 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002348 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002349 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002350 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2351 SP,
2352 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01002353 } else if (source.IsRegisterPair()) {
2354 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2355 source.AsRegisterPairLow<Register>(),
2356 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002357 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002358 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002359 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002360 } else {
2361 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002362 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002363 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002364 if (source.AsRegisterPairLow<Register>() == R1) {
2365 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002366 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
2367 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002368 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002369 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002370 SP, destination.GetStackIndex());
2371 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002372 } else if (source.IsFpuRegisterPair()) {
2373 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
2374 SP,
2375 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002376 } else {
2377 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002378 EmitParallelMoves(
2379 Location::StackSlot(source.GetStackIndex()),
2380 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002381 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002382 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002383 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
2384 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002385 }
2386 }
2387}
2388
Calin Juravle175dc732015-08-25 15:42:32 +01002389void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
2390 DCHECK(location.IsRegister());
2391 __ LoadImmediate(location.AsRegister<Register>(), value);
2392}
2393
Calin Juravlee460d1d2015-09-29 04:52:17 +01002394void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002395 HParallelMove move(GetGraph()->GetArena());
2396 move.AddMove(src, dst, dst_type, nullptr);
2397 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002398}
2399
2400void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
2401 if (location.IsRegister()) {
2402 locations->AddTemp(location);
2403 } else if (location.IsRegisterPair()) {
2404 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
2405 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
2406 } else {
2407 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2408 }
2409}
2410
Calin Juravle175dc732015-08-25 15:42:32 +01002411void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
2412 HInstruction* instruction,
2413 uint32_t dex_pc,
2414 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002415 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002416 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002417 if (EntrypointRequiresStackMap(entrypoint)) {
2418 RecordPcInfo(instruction, dex_pc, slow_path);
2419 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002420}
2421
Roland Levillaindec8f632016-07-22 17:10:06 +01002422void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2423 HInstruction* instruction,
2424 SlowPathCode* slow_path) {
2425 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002426 GenerateInvokeRuntime(entry_point_offset);
2427}
2428
2429void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01002430 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2431 __ blx(LR);
2432}
2433
David Brazdilfc6a86a2015-06-26 10:33:45 +00002434void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002435 DCHECK(!successor->IsExitBlock());
2436
2437 HBasicBlock* block = got->GetBlock();
2438 HInstruction* previous = got->GetPrevious();
2439
2440 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00002441 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002442 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2443 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2444 return;
2445 }
2446
2447 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2448 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2449 }
2450 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002451 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002452 }
2453}
2454
David Brazdilfc6a86a2015-06-26 10:33:45 +00002455void LocationsBuilderARM::VisitGoto(HGoto* got) {
2456 got->SetLocations(nullptr);
2457}
2458
2459void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
2460 HandleGoto(got, got->GetSuccessor());
2461}
2462
2463void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2464 try_boundary->SetLocations(nullptr);
2465}
2466
2467void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2468 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2469 if (!successor->IsExitBlock()) {
2470 HandleGoto(try_boundary, successor);
2471 }
2472}
2473
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002474void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002475 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002476}
2477
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002478void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002479}
2480
Roland Levillain4fa13f62015-07-06 18:11:54 +01002481void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
2482 Label* true_label,
2483 Label* false_label) {
2484 LocationSummary* locations = cond->GetLocations();
2485 Location left = locations->InAt(0);
2486 Location right = locations->InAt(1);
2487 IfCondition if_cond = cond->GetCondition();
2488
2489 Register left_high = left.AsRegisterPairHigh<Register>();
2490 Register left_low = left.AsRegisterPairLow<Register>();
2491 IfCondition true_high_cond = if_cond;
2492 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07002493 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01002494
2495 // Set the conditions for the test, remembering that == needs to be
2496 // decided using the low words.
2497 switch (if_cond) {
2498 case kCondEQ:
2499 case kCondNE:
2500 // Nothing to do.
2501 break;
2502 case kCondLT:
2503 false_high_cond = kCondGT;
2504 break;
2505 case kCondLE:
2506 true_high_cond = kCondLT;
2507 break;
2508 case kCondGT:
2509 false_high_cond = kCondLT;
2510 break;
2511 case kCondGE:
2512 true_high_cond = kCondGT;
2513 break;
Aart Bike9f37602015-10-09 11:15:55 -07002514 case kCondB:
2515 false_high_cond = kCondA;
2516 break;
2517 case kCondBE:
2518 true_high_cond = kCondB;
2519 break;
2520 case kCondA:
2521 false_high_cond = kCondB;
2522 break;
2523 case kCondAE:
2524 true_high_cond = kCondA;
2525 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002526 }
2527 if (right.IsConstant()) {
2528 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
2529 int32_t val_low = Low32Bits(value);
2530 int32_t val_high = High32Bits(value);
2531
Vladimir Markoac6ac102015-12-17 12:14:00 +00002532 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002533 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002534 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002535 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002536 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002537 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002538 __ b(true_label, ARMCondition(true_high_cond));
2539 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002540 }
2541 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00002542 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002543 } else {
2544 Register right_high = right.AsRegisterPairHigh<Register>();
2545 Register right_low = right.AsRegisterPairLow<Register>();
2546
2547 __ cmp(left_high, ShifterOperand(right_high));
2548 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002549 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002550 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002551 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002552 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002553 __ b(true_label, ARMCondition(true_high_cond));
2554 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002555 }
2556 // Must be equal high, so compare the lows.
2557 __ cmp(left_low, ShifterOperand(right_low));
2558 }
2559 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07002560 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01002561 __ b(true_label, final_condition);
2562}
2563
David Brazdil0debae72015-11-12 18:37:00 +00002564void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
2565 Label* true_target_in,
2566 Label* false_target_in) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002567 if (CanGenerateTest(condition, codegen_->GetAssembler())) {
2568 Label* non_fallthrough_target;
2569 bool invert;
2570
2571 if (true_target_in == nullptr) {
2572 DCHECK(false_target_in != nullptr);
2573 non_fallthrough_target = false_target_in;
2574 invert = true;
2575 } else {
2576 non_fallthrough_target = true_target_in;
2577 invert = false;
2578 }
2579
2580 const auto cond = GenerateTest(condition, invert, codegen_);
2581
2582 __ b(non_fallthrough_target, cond.first);
2583
2584 if (false_target_in != nullptr && false_target_in != non_fallthrough_target) {
2585 __ b(false_target_in);
2586 }
2587
2588 return;
2589 }
2590
David Brazdil0debae72015-11-12 18:37:00 +00002591 // Generated branching requires both targets to be explicit. If either of the
2592 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2593 Label fallthrough_target;
2594 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
2595 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
2596
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002597 DCHECK_EQ(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
2598 GenerateLongComparesAndJumps(condition, true_target, false_target);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002599
David Brazdil0debae72015-11-12 18:37:00 +00002600 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002601 __ b(false_target);
2602 }
David Brazdil0debae72015-11-12 18:37:00 +00002603
2604 if (fallthrough_target.IsLinked()) {
2605 __ Bind(&fallthrough_target);
2606 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002607}
2608
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002609void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002610 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002611 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002612 Label* false_target) {
2613 HInstruction* cond = instruction->InputAt(condition_input_index);
2614
2615 if (true_target == nullptr && false_target == nullptr) {
2616 // Nothing to do. The code always falls through.
2617 return;
2618 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002619 // Constant condition, statically compared against "true" (integer value 1).
2620 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002621 if (true_target != nullptr) {
2622 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002623 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002624 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002625 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002626 if (false_target != nullptr) {
2627 __ b(false_target);
2628 }
2629 }
2630 return;
2631 }
2632
2633 // The following code generates these patterns:
2634 // (1) true_target == nullptr && false_target != nullptr
2635 // - opposite condition true => branch to false_target
2636 // (2) true_target != nullptr && false_target == nullptr
2637 // - condition true => branch to true_target
2638 // (3) true_target != nullptr && false_target != nullptr
2639 // - condition true => branch to true_target
2640 // - branch to false_target
2641 if (IsBooleanValueOrMaterializedCondition(cond)) {
2642 // Condition has been materialized, compare the output to 0.
2643 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2644 DCHECK(cond_val.IsRegister());
2645 if (true_target == nullptr) {
2646 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
2647 } else {
2648 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002649 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002650 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002651 // Condition has not been materialized. Use its inputs as the comparison and
2652 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002653 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002654
2655 // If this is a long or FP comparison that has been folded into
2656 // the HCondition, generate the comparison directly.
2657 Primitive::Type type = condition->InputAt(0)->GetType();
2658 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2659 GenerateCompareTestAndBranch(condition, true_target, false_target);
2660 return;
2661 }
2662
Donghui Bai426b49c2016-11-08 14:55:38 +08002663 Label* non_fallthrough_target;
2664 Condition arm_cond;
David Brazdil0debae72015-11-12 18:37:00 +00002665 LocationSummary* locations = cond->GetLocations();
2666 DCHECK(locations->InAt(0).IsRegister());
2667 Register left = locations->InAt(0).AsRegister<Register>();
2668 Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08002669
David Brazdil0debae72015-11-12 18:37:00 +00002670 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002671 arm_cond = ARMCondition(condition->GetOppositeCondition());
2672 non_fallthrough_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002673 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002674 arm_cond = ARMCondition(condition->GetCondition());
2675 non_fallthrough_target = true_target;
2676 }
2677
2678 if (right.IsConstant() && (arm_cond == NE || arm_cond == EQ) &&
2679 CodeGenerator::GetInt32ValueOf(right.GetConstant()) == 0) {
2680 if (arm_cond == EQ) {
2681 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2682 } else {
2683 DCHECK_EQ(arm_cond, NE);
2684 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2685 }
2686 } else {
2687 if (right.IsRegister()) {
2688 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
2689 } else {
2690 DCHECK(right.IsConstant());
2691 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2692 }
2693
2694 __ b(non_fallthrough_target, arm_cond);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002695 }
Dave Allison20dfc792014-06-16 20:44:29 -07002696 }
David Brazdil0debae72015-11-12 18:37:00 +00002697
2698 // If neither branch falls through (case 3), the conditional branch to `true_target`
2699 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2700 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002701 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002702 }
2703}
2704
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002705void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002706 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2707 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002708 locations->SetInAt(0, Location::RequiresRegister());
2709 }
2710}
2711
2712void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002713 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2714 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2715 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2716 nullptr : codegen_->GetLabelOf(true_successor);
2717 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2718 nullptr : codegen_->GetLabelOf(false_successor);
2719 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002720}
2721
2722void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
2723 LocationSummary* locations = new (GetGraph()->GetArena())
2724 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002725 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002726 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002727 locations->SetInAt(0, Location::RequiresRegister());
2728 }
2729}
2730
2731void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01002732 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002733 GenerateTestAndBranch(deoptimize,
2734 /* condition_input_index */ 0,
2735 slow_path->GetEntryLabel(),
2736 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002737}
Dave Allison20dfc792014-06-16 20:44:29 -07002738
Mingyao Yang063fc772016-08-02 11:02:54 -07002739void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2740 LocationSummary* locations = new (GetGraph()->GetArena())
2741 LocationSummary(flag, LocationSummary::kNoCall);
2742 locations->SetOut(Location::RequiresRegister());
2743}
2744
2745void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2746 __ LoadFromOffset(kLoadWord,
2747 flag->GetLocations()->Out().AsRegister<Register>(),
2748 SP,
2749 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2750}
2751
David Brazdil74eb1b22015-12-14 11:44:01 +00002752void LocationsBuilderARM::VisitSelect(HSelect* select) {
2753 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08002754 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
2755
2756 if (is_floating_point) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002757 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002758 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002759 } else {
2760 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002761 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002762 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002763
David Brazdil74eb1b22015-12-14 11:44:01 +00002764 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002765 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2766 // The code generator handles overlap with the values, but not with the condition.
2767 locations->SetOut(Location::SameAsFirstInput());
2768 } else if (is_floating_point) {
2769 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2770 } else {
2771 if (!locations->InAt(1).IsConstant()) {
2772 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2773 }
2774
2775 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00002776 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002777}
2778
2779void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002780 HInstruction* const condition = select->GetCondition();
2781 const LocationSummary* const locations = select->GetLocations();
2782 const Primitive::Type type = select->GetType();
2783 const Location first = locations->InAt(0);
2784 const Location out = locations->Out();
2785 const Location second = locations->InAt(1);
2786 Location src;
2787
2788 if (condition->IsIntConstant()) {
2789 if (condition->AsIntConstant()->IsFalse()) {
2790 src = first;
2791 } else {
2792 src = second;
2793 }
2794
2795 codegen_->MoveLocation(out, src, type);
2796 return;
2797 }
2798
2799 if (!Primitive::IsFloatingPointType(type) &&
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002800 (IsBooleanValueOrMaterializedCondition(condition) ||
2801 CanGenerateTest(condition->AsCondition(), codegen_->GetAssembler()))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002802 bool invert = false;
2803
2804 if (out.Equals(second)) {
2805 src = first;
2806 invert = true;
2807 } else if (out.Equals(first)) {
2808 src = second;
2809 } else if (second.IsConstant()) {
2810 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2811 src = second;
2812 } else if (first.IsConstant()) {
2813 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2814 src = first;
2815 invert = true;
2816 } else {
2817 src = second;
2818 }
2819
2820 if (CanGenerateConditionalMove(out, src)) {
2821 if (!out.Equals(first) && !out.Equals(second)) {
2822 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2823 }
2824
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002825 std::pair<Condition, Condition> cond;
2826
2827 if (IsBooleanValueOrMaterializedCondition(condition)) {
2828 __ CmpConstant(locations->InAt(2).AsRegister<Register>(), 0);
2829 cond = invert ? std::make_pair(EQ, NE) : std::make_pair(NE, EQ);
2830 } else {
2831 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2832 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002833
2834 if (out.IsRegister()) {
2835 ShifterOperand operand;
2836
2837 if (src.IsConstant()) {
2838 operand = ShifterOperand(CodeGenerator::GetInt32ValueOf(src.GetConstant()));
2839 } else {
2840 DCHECK(src.IsRegister());
2841 operand = ShifterOperand(src.AsRegister<Register>());
2842 }
2843
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002844 __ it(cond.first);
2845 __ mov(out.AsRegister<Register>(), operand, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08002846 } else {
2847 DCHECK(out.IsRegisterPair());
2848
2849 ShifterOperand operand_high;
2850 ShifterOperand operand_low;
2851
2852 if (src.IsConstant()) {
2853 const int64_t value = src.GetConstant()->AsLongConstant()->GetValue();
2854
2855 operand_high = ShifterOperand(High32Bits(value));
2856 operand_low = ShifterOperand(Low32Bits(value));
2857 } else {
2858 DCHECK(src.IsRegisterPair());
2859 operand_high = ShifterOperand(src.AsRegisterPairHigh<Register>());
2860 operand_low = ShifterOperand(src.AsRegisterPairLow<Register>());
2861 }
2862
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002863 __ it(cond.first);
2864 __ mov(out.AsRegisterPairLow<Register>(), operand_low, cond.first);
2865 __ it(cond.first);
2866 __ mov(out.AsRegisterPairHigh<Register>(), operand_high, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08002867 }
2868
2869 return;
2870 }
2871 }
2872
2873 Label* false_target = nullptr;
2874 Label* true_target = nullptr;
2875 Label select_end;
2876 Label* target = codegen_->GetFinalLabel(select, &select_end);
2877
2878 if (out.Equals(second)) {
2879 true_target = target;
2880 src = first;
2881 } else {
2882 false_target = target;
2883 src = second;
2884
2885 if (!out.Equals(first)) {
2886 codegen_->MoveLocation(out, first, type);
2887 }
2888 }
2889
2890 GenerateTestAndBranch(select, 2, true_target, false_target);
2891 codegen_->MoveLocation(out, src, type);
2892
2893 if (select_end.IsLinked()) {
2894 __ Bind(&select_end);
2895 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002896}
2897
David Srbecky0cf44932015-12-09 14:09:59 +00002898void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2899 new (GetGraph()->GetArena()) LocationSummary(info);
2900}
2901
David Srbeckyd28f4a02016-03-14 17:14:24 +00002902void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2903 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002904}
2905
2906void CodeGeneratorARM::GenerateNop() {
2907 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002908}
2909
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002910void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002911 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002912 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002913 // Handle the long/FP comparisons made in instruction simplification.
2914 switch (cond->InputAt(0)->GetType()) {
2915 case Primitive::kPrimLong:
2916 locations->SetInAt(0, Location::RequiresRegister());
2917 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002918 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002919 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002920 }
2921 break;
2922
2923 case Primitive::kPrimFloat:
2924 case Primitive::kPrimDouble:
2925 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002926 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002927 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002928 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2929 }
2930 break;
2931
2932 default:
2933 locations->SetInAt(0, Location::RequiresRegister());
2934 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002935 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002936 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2937 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002938 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002939}
2940
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002941void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002942 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002943 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002944 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002945
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002946 const Register out = cond->GetLocations()->Out().AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002947
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002948 if (ArmAssembler::IsLowRegister(out) && CanGenerateTest(cond, codegen_->GetAssembler())) {
2949 const auto condition = GenerateTest(cond, false, codegen_);
2950
2951 __ it(condition.first);
2952 __ mov(out, ShifterOperand(1), condition.first);
2953 __ it(condition.second);
2954 __ mov(out, ShifterOperand(0), condition.second);
2955 return;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002956 }
2957
2958 // Convert the jumps into the result.
2959 Label done_label;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002960 Label* const final_label = codegen_->GetFinalLabel(cond, &done_label);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002961
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002962 if (cond->InputAt(0)->GetType() == Primitive::kPrimLong) {
2963 Label true_label, false_label;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002964
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002965 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2966
2967 // False case: result = 0.
2968 __ Bind(&false_label);
2969 __ LoadImmediate(out, 0);
2970 __ b(final_label);
2971
2972 // True case: result = 1.
2973 __ Bind(&true_label);
2974 __ LoadImmediate(out, 1);
2975 } else {
2976 DCHECK(CanGenerateTest(cond, codegen_->GetAssembler()));
2977
2978 const auto condition = GenerateTest(cond, false, codegen_);
2979
2980 __ mov(out, ShifterOperand(0), AL, kCcKeep);
2981 __ b(final_label, condition.second);
2982 __ LoadImmediate(out, 1);
2983 }
Anton Kirilov6f644202017-02-27 18:29:45 +00002984
2985 if (done_label.IsLinked()) {
2986 __ Bind(&done_label);
2987 }
Dave Allison20dfc792014-06-16 20:44:29 -07002988}
2989
2990void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002991 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002992}
2993
2994void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002995 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002996}
2997
2998void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002999 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003000}
3001
3002void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003003 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003004}
3005
3006void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003007 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003008}
3009
3010void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003011 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003012}
3013
3014void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003015 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003016}
3017
3018void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003019 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003020}
3021
3022void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003023 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003024}
3025
3026void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003027 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003028}
3029
3030void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003031 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003032}
3033
3034void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003035 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003036}
3037
Aart Bike9f37602015-10-09 11:15:55 -07003038void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003039 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003040}
3041
3042void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003043 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003044}
3045
3046void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003047 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003048}
3049
3050void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003051 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003052}
3053
3054void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003055 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003056}
3057
3058void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003059 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003060}
3061
3062void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003063 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003064}
3065
3066void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003067 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003068}
3069
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003070void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003071 LocationSummary* locations =
3072 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003073 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003074}
3075
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003076void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01003077 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003078}
3079
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003080void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
3081 LocationSummary* locations =
3082 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3083 locations->SetOut(Location::ConstantLocation(constant));
3084}
3085
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003086void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003087 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003088}
3089
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003090void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003091 LocationSummary* locations =
3092 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003093 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003094}
3095
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003096void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003097 // Will be generated at use site.
3098}
3099
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003100void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
3101 LocationSummary* locations =
3102 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3103 locations->SetOut(Location::ConstantLocation(constant));
3104}
3105
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003106void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003107 // Will be generated at use site.
3108}
3109
3110void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
3111 LocationSummary* locations =
3112 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3113 locations->SetOut(Location::ConstantLocation(constant));
3114}
3115
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003116void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003117 // Will be generated at use site.
3118}
3119
Igor Murashkind01745e2017-04-05 16:40:31 -07003120void LocationsBuilderARM::VisitConstructorFence(HConstructorFence* constructor_fence) {
3121 constructor_fence->SetLocations(nullptr);
3122}
3123
3124void InstructionCodeGeneratorARM::VisitConstructorFence(
3125 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3126 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3127}
3128
Calin Juravle27df7582015-04-17 19:12:31 +01003129void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3130 memory_barrier->SetLocations(nullptr);
3131}
3132
3133void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00003134 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01003135}
3136
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003137void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003138 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003139}
3140
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003141void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003142 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003143}
3144
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003145void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003146 LocationSummary* locations =
3147 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003148 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003149}
3150
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003151void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003152 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003153}
3154
Calin Juravle175dc732015-08-25 15:42:32 +01003155void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3156 // The trampoline uses the same calling convention as dex calling conventions,
3157 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3158 // the method_idx.
3159 HandleInvoke(invoke);
3160}
3161
3162void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3163 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3164}
3165
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003166void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003167 // Explicit clinit checks triggered by static invokes must have been pruned by
3168 // art::PrepareForRegisterAllocation.
3169 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003170
Vladimir Marko68c981f2016-08-26 13:13:33 +01003171 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003172 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00003173 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3174 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3175 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003176 return;
3177 }
3178
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003179 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00003180
3181 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3182 if (invoke->HasPcRelativeDexCache()) {
3183 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3184 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003185}
3186
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003187static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
3188 if (invoke->GetLocations()->Intrinsified()) {
3189 IntrinsicCodeGeneratorARM intrinsic(codegen);
3190 intrinsic.Dispatch(invoke);
3191 return true;
3192 }
3193 return false;
3194}
3195
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003196void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003197 // Explicit clinit checks triggered by static invokes must have been pruned by
3198 // art::PrepareForRegisterAllocation.
3199 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003200
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003201 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3202 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003203 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003204
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003205 LocationSummary* locations = invoke->GetLocations();
3206 codegen_->GenerateStaticOrDirectCall(
3207 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003208 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003209}
3210
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003211void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003212 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003213 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003214}
3215
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003216void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01003217 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003218 if (intrinsic.TryDispatch(invoke)) {
3219 return;
3220 }
3221
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003222 HandleInvoke(invoke);
3223}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003224
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003225void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003226 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3227 return;
3228 }
3229
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003230 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003231 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003232 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003233}
3234
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003235void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3236 HandleInvoke(invoke);
3237 // Add the hidden argument.
3238 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
3239}
3240
3241void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3242 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00003243 LocationSummary* locations = invoke->GetLocations();
3244 Register temp = locations->GetTemp(0).AsRegister<Register>();
3245 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003246 Location receiver = locations->InAt(0);
3247 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3248
Roland Levillain3b359c72015-11-17 19:35:12 +00003249 // Set the hidden argument. This is safe to do this here, as R12
3250 // won't be modified thereafter, before the `blx` (call) instruction.
3251 DCHECK_EQ(R12, hidden_reg);
3252 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003253
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003254 if (receiver.IsStackSlot()) {
3255 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00003256 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003257 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3258 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00003259 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00003260 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003261 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003262 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00003263 // Instead of simply (possibly) unpoisoning `temp` here, we should
3264 // emit a read barrier for the previous class reference load.
3265 // However this is not required in practice, as this is an
3266 // intermediate/temporary reference and because the current
3267 // concurrent copying collector keeps the from-space memory
3268 // intact/accessible until the end of the marking phase (the
3269 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003270 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003271 __ LoadFromOffset(kLoadWord, temp, temp,
3272 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3273 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003274 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003275 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003276 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00003277 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07003278 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003279 // LR = temp->GetEntryPoint();
3280 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
3281 // LR();
3282 __ blx(LR);
3283 DCHECK(!codegen_->IsLeafMethod());
3284 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3285}
3286
Orion Hodsonac141392017-01-13 11:53:47 +00003287void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3288 HandleInvoke(invoke);
3289}
3290
3291void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3292 codegen_->GenerateInvokePolymorphicCall(invoke);
3293}
3294
Roland Levillain88cb1752014-10-20 16:36:47 +01003295void LocationsBuilderARM::VisitNeg(HNeg* neg) {
3296 LocationSummary* locations =
3297 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3298 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003299 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01003300 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003301 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3302 break;
3303 }
3304 case Primitive::kPrimLong: {
3305 locations->SetInAt(0, Location::RequiresRegister());
3306 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003307 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003308 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003309
Roland Levillain88cb1752014-10-20 16:36:47 +01003310 case Primitive::kPrimFloat:
3311 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003312 locations->SetInAt(0, Location::RequiresFpuRegister());
3313 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003314 break;
3315
3316 default:
3317 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3318 }
3319}
3320
3321void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
3322 LocationSummary* locations = neg->GetLocations();
3323 Location out = locations->Out();
3324 Location in = locations->InAt(0);
3325 switch (neg->GetResultType()) {
3326 case Primitive::kPrimInt:
3327 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003328 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01003329 break;
3330
3331 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003332 DCHECK(in.IsRegisterPair());
3333 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3334 __ rsbs(out.AsRegisterPairLow<Register>(),
3335 in.AsRegisterPairLow<Register>(),
3336 ShifterOperand(0));
3337 // We cannot emit an RSC (Reverse Subtract with Carry)
3338 // instruction here, as it does not exist in the Thumb-2
3339 // instruction set. We use the following approach
3340 // using SBC and SUB instead.
3341 //
3342 // out.hi = -C
3343 __ sbc(out.AsRegisterPairHigh<Register>(),
3344 out.AsRegisterPairHigh<Register>(),
3345 ShifterOperand(out.AsRegisterPairHigh<Register>()));
3346 // out.hi = out.hi - in.hi
3347 __ sub(out.AsRegisterPairHigh<Register>(),
3348 out.AsRegisterPairHigh<Register>(),
3349 ShifterOperand(in.AsRegisterPairHigh<Register>()));
3350 break;
3351
Roland Levillain88cb1752014-10-20 16:36:47 +01003352 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003353 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003354 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00003355 break;
3356
Roland Levillain88cb1752014-10-20 16:36:47 +01003357 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003358 DCHECK(in.IsFpuRegisterPair());
3359 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3360 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01003361 break;
3362
3363 default:
3364 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3365 }
3366}
3367
Roland Levillaindff1f282014-11-05 14:15:05 +00003368void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003369 Primitive::Type result_type = conversion->GetResultType();
3370 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003371 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00003372
Roland Levillain5b3ee562015-04-14 16:02:41 +01003373 // The float-to-long, double-to-long and long-to-float type conversions
3374 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003375 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01003376 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3377 && result_type == Primitive::kPrimLong)
3378 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003379 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00003380 : LocationSummary::kNoCall;
3381 LocationSummary* locations =
3382 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3383
David Brazdilb2bd1c52015-03-25 11:17:37 +00003384 // The Java language does not allow treating boolean as an integral type but
3385 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00003386
Roland Levillaindff1f282014-11-05 14:15:05 +00003387 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003388 case Primitive::kPrimByte:
3389 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003390 case Primitive::kPrimLong:
3391 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003392 case Primitive::kPrimBoolean:
3393 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003394 case Primitive::kPrimShort:
3395 case Primitive::kPrimInt:
3396 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003397 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003398 locations->SetInAt(0, Location::RequiresRegister());
3399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3400 break;
3401
3402 default:
3403 LOG(FATAL) << "Unexpected type conversion from " << input_type
3404 << " to " << result_type;
3405 }
3406 break;
3407
Roland Levillain01a8d712014-11-14 16:27:39 +00003408 case Primitive::kPrimShort:
3409 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003410 case Primitive::kPrimLong:
3411 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003412 case Primitive::kPrimBoolean:
3413 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003414 case Primitive::kPrimByte:
3415 case Primitive::kPrimInt:
3416 case Primitive::kPrimChar:
3417 // Processing a Dex `int-to-short' instruction.
3418 locations->SetInAt(0, Location::RequiresRegister());
3419 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3420 break;
3421
3422 default:
3423 LOG(FATAL) << "Unexpected type conversion from " << input_type
3424 << " to " << result_type;
3425 }
3426 break;
3427
Roland Levillain946e1432014-11-11 17:35:19 +00003428 case Primitive::kPrimInt:
3429 switch (input_type) {
3430 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003431 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003432 locations->SetInAt(0, Location::Any());
3433 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3434 break;
3435
3436 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00003437 // Processing a Dex `float-to-int' instruction.
3438 locations->SetInAt(0, Location::RequiresFpuRegister());
3439 locations->SetOut(Location::RequiresRegister());
3440 locations->AddTemp(Location::RequiresFpuRegister());
3441 break;
3442
Roland Levillain946e1432014-11-11 17:35:19 +00003443 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003444 // Processing a Dex `double-to-int' instruction.
3445 locations->SetInAt(0, Location::RequiresFpuRegister());
3446 locations->SetOut(Location::RequiresRegister());
3447 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00003448 break;
3449
3450 default:
3451 LOG(FATAL) << "Unexpected type conversion from " << input_type
3452 << " to " << result_type;
3453 }
3454 break;
3455
Roland Levillaindff1f282014-11-05 14:15:05 +00003456 case Primitive::kPrimLong:
3457 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003458 case Primitive::kPrimBoolean:
3459 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003460 case Primitive::kPrimByte:
3461 case Primitive::kPrimShort:
3462 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003463 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003464 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003465 locations->SetInAt(0, Location::RequiresRegister());
3466 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3467 break;
3468
Roland Levillain624279f2014-12-04 11:54:28 +00003469 case Primitive::kPrimFloat: {
3470 // Processing a Dex `float-to-long' instruction.
3471 InvokeRuntimeCallingConvention calling_convention;
3472 locations->SetInAt(0, Location::FpuRegisterLocation(
3473 calling_convention.GetFpuRegisterAt(0)));
3474 locations->SetOut(Location::RegisterPairLocation(R0, R1));
3475 break;
3476 }
3477
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003478 case Primitive::kPrimDouble: {
3479 // Processing a Dex `double-to-long' instruction.
3480 InvokeRuntimeCallingConvention calling_convention;
3481 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3482 calling_convention.GetFpuRegisterAt(0),
3483 calling_convention.GetFpuRegisterAt(1)));
3484 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00003485 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003486 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003487
3488 default:
3489 LOG(FATAL) << "Unexpected type conversion from " << input_type
3490 << " to " << result_type;
3491 }
3492 break;
3493
Roland Levillain981e4542014-11-14 11:47:14 +00003494 case Primitive::kPrimChar:
3495 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003496 case Primitive::kPrimLong:
3497 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003498 case Primitive::kPrimBoolean:
3499 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003500 case Primitive::kPrimByte:
3501 case Primitive::kPrimShort:
3502 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003503 // Processing a Dex `int-to-char' instruction.
3504 locations->SetInAt(0, Location::RequiresRegister());
3505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3506 break;
3507
3508 default:
3509 LOG(FATAL) << "Unexpected type conversion from " << input_type
3510 << " to " << result_type;
3511 }
3512 break;
3513
Roland Levillaindff1f282014-11-05 14:15:05 +00003514 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003515 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003516 case Primitive::kPrimBoolean:
3517 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003518 case Primitive::kPrimByte:
3519 case Primitive::kPrimShort:
3520 case Primitive::kPrimInt:
3521 case Primitive::kPrimChar:
3522 // Processing a Dex `int-to-float' instruction.
3523 locations->SetInAt(0, Location::RequiresRegister());
3524 locations->SetOut(Location::RequiresFpuRegister());
3525 break;
3526
Roland Levillain5b3ee562015-04-14 16:02:41 +01003527 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00003528 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01003529 InvokeRuntimeCallingConvention calling_convention;
3530 locations->SetInAt(0, Location::RegisterPairLocation(
3531 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3532 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00003533 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01003534 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003535
Roland Levillaincff13742014-11-17 14:32:17 +00003536 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003537 // Processing a Dex `double-to-float' instruction.
3538 locations->SetInAt(0, Location::RequiresFpuRegister());
3539 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003540 break;
3541
3542 default:
3543 LOG(FATAL) << "Unexpected type conversion from " << input_type
3544 << " to " << result_type;
3545 };
3546 break;
3547
Roland Levillaindff1f282014-11-05 14:15:05 +00003548 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003549 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003550 case Primitive::kPrimBoolean:
3551 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003552 case Primitive::kPrimByte:
3553 case Primitive::kPrimShort:
3554 case Primitive::kPrimInt:
3555 case Primitive::kPrimChar:
3556 // Processing a Dex `int-to-double' instruction.
3557 locations->SetInAt(0, Location::RequiresRegister());
3558 locations->SetOut(Location::RequiresFpuRegister());
3559 break;
3560
3561 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003562 // Processing a Dex `long-to-double' instruction.
3563 locations->SetInAt(0, Location::RequiresRegister());
3564 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01003565 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00003566 locations->AddTemp(Location::RequiresFpuRegister());
3567 break;
3568
Roland Levillaincff13742014-11-17 14:32:17 +00003569 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003570 // Processing a Dex `float-to-double' instruction.
3571 locations->SetInAt(0, Location::RequiresFpuRegister());
3572 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003573 break;
3574
3575 default:
3576 LOG(FATAL) << "Unexpected type conversion from " << input_type
3577 << " to " << result_type;
3578 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003579 break;
3580
3581 default:
3582 LOG(FATAL) << "Unexpected type conversion from " << input_type
3583 << " to " << result_type;
3584 }
3585}
3586
3587void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
3588 LocationSummary* locations = conversion->GetLocations();
3589 Location out = locations->Out();
3590 Location in = locations->InAt(0);
3591 Primitive::Type result_type = conversion->GetResultType();
3592 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003593 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00003594 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003595 case Primitive::kPrimByte:
3596 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003597 case Primitive::kPrimLong:
3598 // Type conversion from long to byte is a result of code transformations.
3599 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
3600 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003601 case Primitive::kPrimBoolean:
3602 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003603 case Primitive::kPrimShort:
3604 case Primitive::kPrimInt:
3605 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003606 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003607 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00003608 break;
3609
3610 default:
3611 LOG(FATAL) << "Unexpected type conversion from " << input_type
3612 << " to " << result_type;
3613 }
3614 break;
3615
Roland Levillain01a8d712014-11-14 16:27:39 +00003616 case Primitive::kPrimShort:
3617 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003618 case Primitive::kPrimLong:
3619 // Type conversion from long to short is a result of code transformations.
3620 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3621 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003622 case Primitive::kPrimBoolean:
3623 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003624 case Primitive::kPrimByte:
3625 case Primitive::kPrimInt:
3626 case Primitive::kPrimChar:
3627 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003628 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00003629 break;
3630
3631 default:
3632 LOG(FATAL) << "Unexpected type conversion from " << input_type
3633 << " to " << result_type;
3634 }
3635 break;
3636
Roland Levillain946e1432014-11-11 17:35:19 +00003637 case Primitive::kPrimInt:
3638 switch (input_type) {
3639 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003640 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003641 DCHECK(out.IsRegister());
3642 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003643 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003644 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003645 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00003646 } else {
3647 DCHECK(in.IsConstant());
3648 DCHECK(in.GetConstant()->IsLongConstant());
3649 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003650 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00003651 }
3652 break;
3653
Roland Levillain3f8f9362014-12-02 17:45:01 +00003654 case Primitive::kPrimFloat: {
3655 // Processing a Dex `float-to-int' instruction.
3656 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003657 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00003658 __ vmovrs(out.AsRegister<Register>(), temp);
3659 break;
3660 }
3661
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003662 case Primitive::kPrimDouble: {
3663 // Processing a Dex `double-to-int' instruction.
3664 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003665 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003666 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00003667 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003668 }
Roland Levillain946e1432014-11-11 17:35:19 +00003669
3670 default:
3671 LOG(FATAL) << "Unexpected type conversion from " << input_type
3672 << " to " << result_type;
3673 }
3674 break;
3675
Roland Levillaindff1f282014-11-05 14:15:05 +00003676 case Primitive::kPrimLong:
3677 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003678 case Primitive::kPrimBoolean:
3679 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003680 case Primitive::kPrimByte:
3681 case Primitive::kPrimShort:
3682 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003683 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003684 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003685 DCHECK(out.IsRegisterPair());
3686 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003687 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00003688 // Sign extension.
3689 __ Asr(out.AsRegisterPairHigh<Register>(),
3690 out.AsRegisterPairLow<Register>(),
3691 31);
3692 break;
3693
3694 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00003695 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003696 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003697 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003698 break;
3699
Roland Levillaindff1f282014-11-05 14:15:05 +00003700 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003701 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003702 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003703 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003704 break;
3705
3706 default:
3707 LOG(FATAL) << "Unexpected type conversion from " << input_type
3708 << " to " << result_type;
3709 }
3710 break;
3711
Roland Levillain981e4542014-11-14 11:47:14 +00003712 case Primitive::kPrimChar:
3713 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003714 case Primitive::kPrimLong:
3715 // Type conversion from long to char is a result of code transformations.
3716 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3717 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003718 case Primitive::kPrimBoolean:
3719 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003720 case Primitive::kPrimByte:
3721 case Primitive::kPrimShort:
3722 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003723 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003724 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00003725 break;
3726
3727 default:
3728 LOG(FATAL) << "Unexpected type conversion from " << input_type
3729 << " to " << result_type;
3730 }
3731 break;
3732
Roland Levillaindff1f282014-11-05 14:15:05 +00003733 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003734 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003735 case Primitive::kPrimBoolean:
3736 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003737 case Primitive::kPrimByte:
3738 case Primitive::kPrimShort:
3739 case Primitive::kPrimInt:
3740 case Primitive::kPrimChar: {
3741 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003742 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
3743 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003744 break;
3745 }
3746
Roland Levillain5b3ee562015-04-14 16:02:41 +01003747 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003748 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003749 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003750 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00003751 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003752
Roland Levillaincff13742014-11-17 14:32:17 +00003753 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003754 // Processing a Dex `double-to-float' instruction.
3755 __ vcvtsd(out.AsFpuRegister<SRegister>(),
3756 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00003757 break;
3758
3759 default:
3760 LOG(FATAL) << "Unexpected type conversion from " << input_type
3761 << " to " << result_type;
3762 };
3763 break;
3764
Roland Levillaindff1f282014-11-05 14:15:05 +00003765 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003766 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003767 case Primitive::kPrimBoolean:
3768 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003769 case Primitive::kPrimByte:
3770 case Primitive::kPrimShort:
3771 case Primitive::kPrimInt:
3772 case Primitive::kPrimChar: {
3773 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003774 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003775 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3776 out.AsFpuRegisterPairLow<SRegister>());
3777 break;
3778 }
3779
Roland Levillain647b9ed2014-11-27 12:06:00 +00003780 case Primitive::kPrimLong: {
3781 // Processing a Dex `long-to-double' instruction.
3782 Register low = in.AsRegisterPairLow<Register>();
3783 Register high = in.AsRegisterPairHigh<Register>();
3784 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
3785 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003786 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00003787 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003788 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
3789 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003790
Roland Levillain682393c2015-04-14 15:57:52 +01003791 // temp_d = int-to-double(high)
3792 __ vmovsr(temp_s, high);
3793 __ vcvtdi(temp_d, temp_s);
3794 // constant_d = k2Pow32EncodingForDouble
3795 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
3796 // out_d = unsigned-to-double(low)
3797 __ vmovsr(out_s, low);
3798 __ vcvtdu(out_d, out_s);
3799 // out_d += temp_d * constant_d
3800 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003801 break;
3802 }
3803
Roland Levillaincff13742014-11-17 14:32:17 +00003804 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003805 // Processing a Dex `float-to-double' instruction.
3806 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3807 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003808 break;
3809
3810 default:
3811 LOG(FATAL) << "Unexpected type conversion from " << input_type
3812 << " to " << result_type;
3813 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003814 break;
3815
3816 default:
3817 LOG(FATAL) << "Unexpected type conversion from " << input_type
3818 << " to " << result_type;
3819 }
3820}
3821
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003822void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003823 LocationSummary* locations =
3824 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003825 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003826 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003827 locations->SetInAt(0, Location::RequiresRegister());
3828 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003829 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3830 break;
3831 }
3832
3833 case Primitive::kPrimLong: {
3834 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003835 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003836 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003837 break;
3838 }
3839
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003840 case Primitive::kPrimFloat:
3841 case Primitive::kPrimDouble: {
3842 locations->SetInAt(0, Location::RequiresFpuRegister());
3843 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003844 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003845 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003846 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003847
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003848 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003849 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003850 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003851}
3852
3853void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
3854 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003855 Location out = locations->Out();
3856 Location first = locations->InAt(0);
3857 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003858 switch (add->GetResultType()) {
3859 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003860 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003861 __ add(out.AsRegister<Register>(),
3862 first.AsRegister<Register>(),
3863 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003864 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003865 __ AddConstant(out.AsRegister<Register>(),
3866 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003867 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003868 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003869 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003870
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003871 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003872 if (second.IsConstant()) {
3873 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3874 GenerateAddLongConst(out, first, value);
3875 } else {
3876 DCHECK(second.IsRegisterPair());
3877 __ adds(out.AsRegisterPairLow<Register>(),
3878 first.AsRegisterPairLow<Register>(),
3879 ShifterOperand(second.AsRegisterPairLow<Register>()));
3880 __ adc(out.AsRegisterPairHigh<Register>(),
3881 first.AsRegisterPairHigh<Register>(),
3882 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3883 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003884 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003885 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003886
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003887 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00003888 __ vadds(out.AsFpuRegister<SRegister>(),
3889 first.AsFpuRegister<SRegister>(),
3890 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003891 break;
3892
3893 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003894 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3895 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3896 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003897 break;
3898
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003899 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003900 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003901 }
3902}
3903
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003904void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003905 LocationSummary* locations =
3906 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003907 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003908 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003909 locations->SetInAt(0, Location::RequiresRegister());
3910 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003911 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3912 break;
3913 }
3914
3915 case Primitive::kPrimLong: {
3916 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003917 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003918 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003919 break;
3920 }
Calin Juravle11351682014-10-23 15:38:15 +01003921 case Primitive::kPrimFloat:
3922 case Primitive::kPrimDouble: {
3923 locations->SetInAt(0, Location::RequiresFpuRegister());
3924 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003925 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003926 break;
Calin Juravle11351682014-10-23 15:38:15 +01003927 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003928 default:
Calin Juravle11351682014-10-23 15:38:15 +01003929 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003930 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003931}
3932
3933void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3934 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003935 Location out = locations->Out();
3936 Location first = locations->InAt(0);
3937 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003938 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003939 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003940 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003941 __ sub(out.AsRegister<Register>(),
3942 first.AsRegister<Register>(),
3943 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003944 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003945 __ AddConstant(out.AsRegister<Register>(),
3946 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003947 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003948 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003949 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003950 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003951
Calin Juravle11351682014-10-23 15:38:15 +01003952 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003953 if (second.IsConstant()) {
3954 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3955 GenerateAddLongConst(out, first, -value);
3956 } else {
3957 DCHECK(second.IsRegisterPair());
3958 __ subs(out.AsRegisterPairLow<Register>(),
3959 first.AsRegisterPairLow<Register>(),
3960 ShifterOperand(second.AsRegisterPairLow<Register>()));
3961 __ sbc(out.AsRegisterPairHigh<Register>(),
3962 first.AsRegisterPairHigh<Register>(),
3963 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3964 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003965 break;
Calin Juravle11351682014-10-23 15:38:15 +01003966 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003967
Calin Juravle11351682014-10-23 15:38:15 +01003968 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003969 __ vsubs(out.AsFpuRegister<SRegister>(),
3970 first.AsFpuRegister<SRegister>(),
3971 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003972 break;
Calin Juravle11351682014-10-23 15:38:15 +01003973 }
3974
3975 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003976 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3977 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3978 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003979 break;
3980 }
3981
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003982
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003983 default:
Calin Juravle11351682014-10-23 15:38:15 +01003984 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003985 }
3986}
3987
Calin Juravle34bacdf2014-10-07 20:23:36 +01003988void LocationsBuilderARM::VisitMul(HMul* mul) {
3989 LocationSummary* locations =
3990 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3991 switch (mul->GetResultType()) {
3992 case Primitive::kPrimInt:
3993 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003994 locations->SetInAt(0, Location::RequiresRegister());
3995 locations->SetInAt(1, Location::RequiresRegister());
3996 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003997 break;
3998 }
3999
Calin Juravleb5bfa962014-10-21 18:02:24 +01004000 case Primitive::kPrimFloat:
4001 case Primitive::kPrimDouble: {
4002 locations->SetInAt(0, Location::RequiresFpuRegister());
4003 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004004 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004005 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004006 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004007
4008 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004009 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004010 }
4011}
4012
4013void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
4014 LocationSummary* locations = mul->GetLocations();
4015 Location out = locations->Out();
4016 Location first = locations->InAt(0);
4017 Location second = locations->InAt(1);
4018 switch (mul->GetResultType()) {
4019 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00004020 __ mul(out.AsRegister<Register>(),
4021 first.AsRegister<Register>(),
4022 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004023 break;
4024 }
4025 case Primitive::kPrimLong: {
4026 Register out_hi = out.AsRegisterPairHigh<Register>();
4027 Register out_lo = out.AsRegisterPairLow<Register>();
4028 Register in1_hi = first.AsRegisterPairHigh<Register>();
4029 Register in1_lo = first.AsRegisterPairLow<Register>();
4030 Register in2_hi = second.AsRegisterPairHigh<Register>();
4031 Register in2_lo = second.AsRegisterPairLow<Register>();
4032
4033 // Extra checks to protect caused by the existence of R1_R2.
4034 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4035 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
4036 DCHECK_NE(out_hi, in1_lo);
4037 DCHECK_NE(out_hi, in2_lo);
4038
4039 // input: in1 - 64 bits, in2 - 64 bits
4040 // output: out
4041 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4042 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4043 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4044
4045 // IP <- in1.lo * in2.hi
4046 __ mul(IP, in1_lo, in2_hi);
4047 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4048 __ mla(out_hi, in1_hi, in2_lo, IP);
4049 // out.lo <- (in1.lo * in2.lo)[31:0];
4050 __ umull(out_lo, IP, in1_lo, in2_lo);
4051 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
4052 __ add(out_hi, out_hi, ShifterOperand(IP));
4053 break;
4054 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01004055
4056 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004057 __ vmuls(out.AsFpuRegister<SRegister>(),
4058 first.AsFpuRegister<SRegister>(),
4059 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004060 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004061 }
4062
4063 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004064 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4065 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4066 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01004067 break;
4068 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004069
4070 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004071 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004072 }
4073}
4074
Zheng Xuc6667102015-05-15 16:08:45 +08004075void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4076 DCHECK(instruction->IsDiv() || instruction->IsRem());
4077 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4078
4079 LocationSummary* locations = instruction->GetLocations();
4080 Location second = locations->InAt(1);
4081 DCHECK(second.IsConstant());
4082
4083 Register out = locations->Out().AsRegister<Register>();
4084 Register dividend = locations->InAt(0).AsRegister<Register>();
4085 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4086 DCHECK(imm == 1 || imm == -1);
4087
4088 if (instruction->IsRem()) {
4089 __ LoadImmediate(out, 0);
4090 } else {
4091 if (imm == 1) {
4092 __ Mov(out, dividend);
4093 } else {
4094 __ rsb(out, dividend, ShifterOperand(0));
4095 }
4096 }
4097}
4098
4099void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4100 DCHECK(instruction->IsDiv() || instruction->IsRem());
4101 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4102
4103 LocationSummary* locations = instruction->GetLocations();
4104 Location second = locations->InAt(1);
4105 DCHECK(second.IsConstant());
4106
4107 Register out = locations->Out().AsRegister<Register>();
4108 Register dividend = locations->InAt(0).AsRegister<Register>();
4109 Register temp = locations->GetTemp(0).AsRegister<Register>();
4110 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004111 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08004112 int ctz_imm = CTZ(abs_imm);
4113
4114 if (ctz_imm == 1) {
4115 __ Lsr(temp, dividend, 32 - ctz_imm);
4116 } else {
4117 __ Asr(temp, dividend, 31);
4118 __ Lsr(temp, temp, 32 - ctz_imm);
4119 }
4120 __ add(out, temp, ShifterOperand(dividend));
4121
4122 if (instruction->IsDiv()) {
4123 __ Asr(out, out, ctz_imm);
4124 if (imm < 0) {
4125 __ rsb(out, out, ShifterOperand(0));
4126 }
4127 } else {
4128 __ ubfx(out, out, 0, ctz_imm);
4129 __ sub(out, out, ShifterOperand(temp));
4130 }
4131}
4132
4133void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4134 DCHECK(instruction->IsDiv() || instruction->IsRem());
4135 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4136
4137 LocationSummary* locations = instruction->GetLocations();
4138 Location second = locations->InAt(1);
4139 DCHECK(second.IsConstant());
4140
4141 Register out = locations->Out().AsRegister<Register>();
4142 Register dividend = locations->InAt(0).AsRegister<Register>();
4143 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4144 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4145 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4146
4147 int64_t magic;
4148 int shift;
4149 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4150
4151 __ LoadImmediate(temp1, magic);
4152 __ smull(temp2, temp1, dividend, temp1);
4153
4154 if (imm > 0 && magic < 0) {
4155 __ add(temp1, temp1, ShifterOperand(dividend));
4156 } else if (imm < 0 && magic > 0) {
4157 __ sub(temp1, temp1, ShifterOperand(dividend));
4158 }
4159
4160 if (shift != 0) {
4161 __ Asr(temp1, temp1, shift);
4162 }
4163
4164 if (instruction->IsDiv()) {
4165 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
4166 } else {
4167 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
4168 // TODO: Strength reduction for mls.
4169 __ LoadImmediate(temp2, imm);
4170 __ mls(out, temp1, temp2, dividend);
4171 }
4172}
4173
4174void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
4175 DCHECK(instruction->IsDiv() || instruction->IsRem());
4176 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4177
4178 LocationSummary* locations = instruction->GetLocations();
4179 Location second = locations->InAt(1);
4180 DCHECK(second.IsConstant());
4181
4182 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4183 if (imm == 0) {
4184 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4185 } else if (imm == 1 || imm == -1) {
4186 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004187 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004188 DivRemByPowerOfTwo(instruction);
4189 } else {
4190 DCHECK(imm <= -2 || imm >= 2);
4191 GenerateDivRemWithAnyConstant(instruction);
4192 }
4193}
4194
Calin Juravle7c4954d2014-10-28 16:57:40 +00004195void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004196 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4197 if (div->GetResultType() == Primitive::kPrimLong) {
4198 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004199 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004200 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4201 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004202 } else if (div->GetResultType() == Primitive::kPrimInt &&
4203 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4204 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004205 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004206 }
4207
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004208 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4209
Calin Juravle7c4954d2014-10-28 16:57:40 +00004210 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004211 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004212 if (div->InputAt(1)->IsConstant()) {
4213 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004214 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004215 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004216 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
4217 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004218 // No temp register required.
4219 } else {
4220 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004221 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004222 locations->AddTemp(Location::RequiresRegister());
4223 }
4224 }
4225 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004226 locations->SetInAt(0, Location::RequiresRegister());
4227 locations->SetInAt(1, Location::RequiresRegister());
4228 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4229 } else {
4230 InvokeRuntimeCallingConvention calling_convention;
4231 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4232 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004233 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004234 // we only need the former.
4235 locations->SetOut(Location::RegisterLocation(R0));
4236 }
Calin Juravled0d48522014-11-04 16:40:20 +00004237 break;
4238 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004239 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004240 InvokeRuntimeCallingConvention calling_convention;
4241 locations->SetInAt(0, Location::RegisterPairLocation(
4242 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4243 locations->SetInAt(1, Location::RegisterPairLocation(
4244 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004245 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004246 break;
4247 }
4248 case Primitive::kPrimFloat:
4249 case Primitive::kPrimDouble: {
4250 locations->SetInAt(0, Location::RequiresFpuRegister());
4251 locations->SetInAt(1, Location::RequiresFpuRegister());
4252 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4253 break;
4254 }
4255
4256 default:
4257 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4258 }
4259}
4260
4261void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
4262 LocationSummary* locations = div->GetLocations();
4263 Location out = locations->Out();
4264 Location first = locations->InAt(0);
4265 Location second = locations->InAt(1);
4266
4267 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004268 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004269 if (second.IsConstant()) {
4270 GenerateDivRemConstantIntegral(div);
4271 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004272 __ sdiv(out.AsRegister<Register>(),
4273 first.AsRegister<Register>(),
4274 second.AsRegister<Register>());
4275 } else {
4276 InvokeRuntimeCallingConvention calling_convention;
4277 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4278 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4279 DCHECK_EQ(R0, out.AsRegister<Register>());
4280
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004281 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004282 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004283 }
Calin Juravled0d48522014-11-04 16:40:20 +00004284 break;
4285 }
4286
Calin Juravle7c4954d2014-10-28 16:57:40 +00004287 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004288 InvokeRuntimeCallingConvention calling_convention;
4289 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4290 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4291 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4292 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4293 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004294 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004295
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004296 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004297 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00004298 break;
4299 }
4300
4301 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004302 __ vdivs(out.AsFpuRegister<SRegister>(),
4303 first.AsFpuRegister<SRegister>(),
4304 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004305 break;
4306 }
4307
4308 case Primitive::kPrimDouble: {
4309 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4310 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4311 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
4312 break;
4313 }
4314
4315 default:
4316 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4317 }
4318}
4319
Calin Juravlebacfec32014-11-14 15:54:36 +00004320void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004321 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004322
4323 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004324 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004325 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4326 // sdiv will be replaced by other instruction sequence.
4327 call_kind = LocationSummary::kNoCall;
4328 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4329 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004330 // Have hardware divide instruction for int, do it with three instructions.
4331 call_kind = LocationSummary::kNoCall;
4332 }
4333
Calin Juravlebacfec32014-11-14 15:54:36 +00004334 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4335
Calin Juravled2ec87d2014-12-08 14:24:46 +00004336 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004337 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004338 if (rem->InputAt(1)->IsConstant()) {
4339 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004340 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004341 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004342 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
4343 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004344 // No temp register required.
4345 } else {
4346 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004347 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004348 locations->AddTemp(Location::RequiresRegister());
4349 }
4350 }
4351 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004352 locations->SetInAt(0, Location::RequiresRegister());
4353 locations->SetInAt(1, Location::RequiresRegister());
4354 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4355 locations->AddTemp(Location::RequiresRegister());
4356 } else {
4357 InvokeRuntimeCallingConvention calling_convention;
4358 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4359 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004360 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004361 // we only need the latter.
4362 locations->SetOut(Location::RegisterLocation(R1));
4363 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004364 break;
4365 }
4366 case Primitive::kPrimLong: {
4367 InvokeRuntimeCallingConvention calling_convention;
4368 locations->SetInAt(0, Location::RegisterPairLocation(
4369 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4370 locations->SetInAt(1, Location::RegisterPairLocation(
4371 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4372 // The runtime helper puts the output in R2,R3.
4373 locations->SetOut(Location::RegisterPairLocation(R2, R3));
4374 break;
4375 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00004376 case Primitive::kPrimFloat: {
4377 InvokeRuntimeCallingConvention calling_convention;
4378 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4379 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4380 locations->SetOut(Location::FpuRegisterLocation(S0));
4381 break;
4382 }
4383
Calin Juravlebacfec32014-11-14 15:54:36 +00004384 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004385 InvokeRuntimeCallingConvention calling_convention;
4386 locations->SetInAt(0, Location::FpuRegisterPairLocation(
4387 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4388 locations->SetInAt(1, Location::FpuRegisterPairLocation(
4389 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4390 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00004391 break;
4392 }
4393
4394 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004395 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004396 }
4397}
4398
4399void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
4400 LocationSummary* locations = rem->GetLocations();
4401 Location out = locations->Out();
4402 Location first = locations->InAt(0);
4403 Location second = locations->InAt(1);
4404
Calin Juravled2ec87d2014-12-08 14:24:46 +00004405 Primitive::Type type = rem->GetResultType();
4406 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004407 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004408 if (second.IsConstant()) {
4409 GenerateDivRemConstantIntegral(rem);
4410 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004411 Register reg1 = first.AsRegister<Register>();
4412 Register reg2 = second.AsRegister<Register>();
4413 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004414
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004415 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004416 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004417 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004418 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004419 } else {
4420 InvokeRuntimeCallingConvention calling_convention;
4421 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4422 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4423 DCHECK_EQ(R1, out.AsRegister<Register>());
4424
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004425 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004426 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004427 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004428 break;
4429 }
4430
4431 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004432 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004433 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004434 break;
4435 }
4436
Calin Juravled2ec87d2014-12-08 14:24:46 +00004437 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004438 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004439 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00004440 break;
4441 }
4442
Calin Juravlebacfec32014-11-14 15:54:36 +00004443 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004444 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004445 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004446 break;
4447 }
4448
4449 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004450 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004451 }
4452}
4453
Calin Juravled0d48522014-11-04 16:40:20 +00004454void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004455 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004456 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00004457}
4458
4459void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004460 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004461 codegen_->AddSlowPath(slow_path);
4462
4463 LocationSummary* locations = instruction->GetLocations();
4464 Location value = locations->InAt(0);
4465
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004466 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00004467 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004468 case Primitive::kPrimByte:
4469 case Primitive::kPrimChar:
4470 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004471 case Primitive::kPrimInt: {
4472 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004473 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004474 } else {
4475 DCHECK(value.IsConstant()) << value;
4476 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4477 __ b(slow_path->GetEntryLabel());
4478 }
4479 }
4480 break;
4481 }
4482 case Primitive::kPrimLong: {
4483 if (value.IsRegisterPair()) {
4484 __ orrs(IP,
4485 value.AsRegisterPairLow<Register>(),
4486 ShifterOperand(value.AsRegisterPairHigh<Register>()));
4487 __ b(slow_path->GetEntryLabel(), EQ);
4488 } else {
4489 DCHECK(value.IsConstant()) << value;
4490 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4491 __ b(slow_path->GetEntryLabel());
4492 }
4493 }
4494 break;
4495 default:
4496 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4497 }
4498 }
Calin Juravled0d48522014-11-04 16:40:20 +00004499}
4500
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004501void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
4502 Register in = locations->InAt(0).AsRegister<Register>();
4503 Location rhs = locations->InAt(1);
4504 Register out = locations->Out().AsRegister<Register>();
4505
4506 if (rhs.IsConstant()) {
4507 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4508 // so map all rotations to a +ve. equivalent in that range.
4509 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4510 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4511 if (rot) {
4512 // Rotate, mapping left rotations to right equivalents if necessary.
4513 // (e.g. left by 2 bits == right by 30.)
4514 __ Ror(out, in, rot);
4515 } else if (out != in) {
4516 __ Mov(out, in);
4517 }
4518 } else {
4519 __ Ror(out, in, rhs.AsRegister<Register>());
4520 }
4521}
4522
4523// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4524// rotates by swapping input regs (effectively rotating by the first 32-bits of
4525// a larger rotation) or flipping direction (thus treating larger right/left
4526// rotations as sub-word sized rotations in the other direction) as appropriate.
Anton Kirilov6f644202017-02-27 18:29:45 +00004527void InstructionCodeGeneratorARM::HandleLongRotate(HRor* ror) {
4528 LocationSummary* locations = ror->GetLocations();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004529 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
4530 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
4531 Location rhs = locations->InAt(1);
4532 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
4533 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
4534
4535 if (rhs.IsConstant()) {
4536 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4537 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00004538 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004539 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4540 // logic below to a simple pair of binary orr.
4541 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4542 if (rot >= kArmBitsPerWord) {
4543 rot -= kArmBitsPerWord;
4544 std::swap(in_reg_hi, in_reg_lo);
4545 }
4546 // Rotate, or mov to out for zero or word size rotations.
4547 if (rot != 0u) {
4548 __ Lsr(out_reg_hi, in_reg_hi, rot);
4549 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
4550 __ Lsr(out_reg_lo, in_reg_lo, rot);
4551 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
4552 } else {
4553 __ Mov(out_reg_lo, in_reg_lo);
4554 __ Mov(out_reg_hi, in_reg_hi);
4555 }
4556 } else {
4557 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
4558 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
4559 Label end;
4560 Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004561 Label* final_label = codegen_->GetFinalLabel(ror, &end);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004562
4563 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
4564 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
4565 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
4566 __ b(&shift_by_32_plus_shift_right, CC);
4567
4568 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4569 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4570 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4571 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4572 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4573 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4574 __ Lsr(shift_left, in_reg_hi, shift_right);
4575 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
Anton Kirilov6f644202017-02-27 18:29:45 +00004576 __ b(final_label);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004577
4578 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4579 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4580 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4581 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4582 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4583 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4584 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4585 __ Lsl(shift_right, in_reg_hi, shift_left);
4586 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
4587
Anton Kirilov6f644202017-02-27 18:29:45 +00004588 if (end.IsLinked()) {
4589 __ Bind(&end);
4590 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004591 }
4592}
Roland Levillain22c49222016-03-18 14:04:28 +00004593
4594void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004595 LocationSummary* locations =
4596 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4597 switch (ror->GetResultType()) {
4598 case Primitive::kPrimInt: {
4599 locations->SetInAt(0, Location::RequiresRegister());
4600 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4601 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4602 break;
4603 }
4604 case Primitive::kPrimLong: {
4605 locations->SetInAt(0, Location::RequiresRegister());
4606 if (ror->InputAt(1)->IsConstant()) {
4607 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4608 } else {
4609 locations->SetInAt(1, Location::RequiresRegister());
4610 locations->AddTemp(Location::RequiresRegister());
4611 locations->AddTemp(Location::RequiresRegister());
4612 }
4613 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4614 break;
4615 }
4616 default:
4617 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4618 }
4619}
4620
Roland Levillain22c49222016-03-18 14:04:28 +00004621void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004622 LocationSummary* locations = ror->GetLocations();
4623 Primitive::Type type = ror->GetResultType();
4624 switch (type) {
4625 case Primitive::kPrimInt: {
4626 HandleIntegerRotate(locations);
4627 break;
4628 }
4629 case Primitive::kPrimLong: {
Anton Kirilov6f644202017-02-27 18:29:45 +00004630 HandleLongRotate(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004631 break;
4632 }
4633 default:
4634 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004635 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004636 }
4637}
4638
Calin Juravle9aec02f2014-11-18 23:06:35 +00004639void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
4640 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4641
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004642 LocationSummary* locations =
4643 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004644
4645 switch (op->GetResultType()) {
4646 case Primitive::kPrimInt: {
4647 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004648 if (op->InputAt(1)->IsConstant()) {
4649 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4650 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4651 } else {
4652 locations->SetInAt(1, Location::RequiresRegister());
4653 // Make the output overlap, as it will be used to hold the masked
4654 // second input.
4655 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4656 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004657 break;
4658 }
4659 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004660 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004661 if (op->InputAt(1)->IsConstant()) {
4662 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4663 // For simplicity, use kOutputOverlap even though we only require that low registers
4664 // don't clash with high registers which the register allocator currently guarantees.
4665 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4666 } else {
4667 locations->SetInAt(1, Location::RequiresRegister());
4668 locations->AddTemp(Location::RequiresRegister());
4669 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4670 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004671 break;
4672 }
4673 default:
4674 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4675 }
4676}
4677
4678void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
4679 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4680
4681 LocationSummary* locations = op->GetLocations();
4682 Location out = locations->Out();
4683 Location first = locations->InAt(0);
4684 Location second = locations->InAt(1);
4685
4686 Primitive::Type type = op->GetResultType();
4687 switch (type) {
4688 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004689 Register out_reg = out.AsRegister<Register>();
4690 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004691 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004692 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00004693 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004694 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004695 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004696 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004697 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004698 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004699 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004700 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004701 }
4702 } else {
4703 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004704 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00004705 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00004706 __ Mov(out_reg, first_reg);
4707 } else if (op->IsShl()) {
4708 __ Lsl(out_reg, first_reg, shift_value);
4709 } else if (op->IsShr()) {
4710 __ Asr(out_reg, first_reg, shift_value);
4711 } else {
4712 __ Lsr(out_reg, first_reg, shift_value);
4713 }
4714 }
4715 break;
4716 }
4717 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004718 Register o_h = out.AsRegisterPairHigh<Register>();
4719 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004720
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004721 Register high = first.AsRegisterPairHigh<Register>();
4722 Register low = first.AsRegisterPairLow<Register>();
4723
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004724 if (second.IsRegister()) {
4725 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004726
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004727 Register second_reg = second.AsRegister<Register>();
4728
4729 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004730 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004731 // Shift the high part
4732 __ Lsl(o_h, high, o_l);
4733 // Shift the low part and `or` what overflew on the high part
4734 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
4735 __ Lsr(temp, low, temp);
4736 __ orr(o_h, o_h, ShifterOperand(temp));
4737 // If the shift is > 32 bits, override the high part
4738 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
4739 __ it(PL);
4740 __ Lsl(o_h, low, temp, PL);
4741 // Shift the low part
4742 __ Lsl(o_l, low, o_l);
4743 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004744 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004745 // Shift the low part
4746 __ Lsr(o_l, low, o_h);
4747 // Shift the high part and `or` what underflew on the low part
4748 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4749 __ Lsl(temp, high, temp);
4750 __ orr(o_l, o_l, ShifterOperand(temp));
4751 // If the shift is > 32 bits, override the low part
4752 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4753 __ it(PL);
4754 __ Asr(o_l, high, temp, PL);
4755 // Shift the high part
4756 __ Asr(o_h, high, o_h);
4757 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004758 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004759 // same as Shr except we use `Lsr`s and not `Asr`s
4760 __ Lsr(o_l, low, o_h);
4761 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4762 __ Lsl(temp, high, temp);
4763 __ orr(o_l, o_l, ShifterOperand(temp));
4764 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4765 __ it(PL);
4766 __ Lsr(o_l, high, temp, PL);
4767 __ Lsr(o_h, high, o_h);
4768 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004769 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004770 // Register allocator doesn't create partial overlap.
4771 DCHECK_NE(o_l, high);
4772 DCHECK_NE(o_h, low);
4773 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004774 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004775 if (shift_value > 32) {
4776 if (op->IsShl()) {
4777 __ Lsl(o_h, low, shift_value - 32);
4778 __ LoadImmediate(o_l, 0);
4779 } else if (op->IsShr()) {
4780 __ Asr(o_l, high, shift_value - 32);
4781 __ Asr(o_h, high, 31);
4782 } else {
4783 __ Lsr(o_l, high, shift_value - 32);
4784 __ LoadImmediate(o_h, 0);
4785 }
4786 } else if (shift_value == 32) {
4787 if (op->IsShl()) {
4788 __ mov(o_h, ShifterOperand(low));
4789 __ LoadImmediate(o_l, 0);
4790 } else if (op->IsShr()) {
4791 __ mov(o_l, ShifterOperand(high));
4792 __ Asr(o_h, high, 31);
4793 } else {
4794 __ mov(o_l, ShifterOperand(high));
4795 __ LoadImmediate(o_h, 0);
4796 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00004797 } else if (shift_value == 1) {
4798 if (op->IsShl()) {
4799 __ Lsls(o_l, low, 1);
4800 __ adc(o_h, high, ShifterOperand(high));
4801 } else if (op->IsShr()) {
4802 __ Asrs(o_h, high, 1);
4803 __ Rrx(o_l, low);
4804 } else {
4805 __ Lsrs(o_h, high, 1);
4806 __ Rrx(o_l, low);
4807 }
4808 } else {
4809 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004810 if (op->IsShl()) {
4811 __ Lsl(o_h, high, shift_value);
4812 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
4813 __ Lsl(o_l, low, shift_value);
4814 } else if (op->IsShr()) {
4815 __ Lsr(o_l, low, shift_value);
4816 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4817 __ Asr(o_h, high, shift_value);
4818 } else {
4819 __ Lsr(o_l, low, shift_value);
4820 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4821 __ Lsr(o_h, high, shift_value);
4822 }
4823 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004824 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004825 break;
4826 }
4827 default:
4828 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004829 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004830 }
4831}
4832
4833void LocationsBuilderARM::VisitShl(HShl* shl) {
4834 HandleShift(shl);
4835}
4836
4837void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
4838 HandleShift(shl);
4839}
4840
4841void LocationsBuilderARM::VisitShr(HShr* shr) {
4842 HandleShift(shr);
4843}
4844
4845void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
4846 HandleShift(shr);
4847}
4848
4849void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
4850 HandleShift(ushr);
4851}
4852
4853void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
4854 HandleShift(ushr);
4855}
4856
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004857void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004858 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004859 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00004860 if (instruction->IsStringAlloc()) {
4861 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4862 } else {
4863 InvokeRuntimeCallingConvention calling_convention;
4864 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004865 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004866 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004867}
4868
4869void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004870 // Note: if heap poisoning is enabled, the entry point takes cares
4871 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004872 if (instruction->IsStringAlloc()) {
4873 // String is allocated through StringFactory. Call NewEmptyString entry point.
4874 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004875 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004876 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4877 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
4878 __ blx(LR);
4879 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4880 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004881 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004882 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004883 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004884}
4885
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004886void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
4887 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004888 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004889 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004890 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004891 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4892 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004893}
4894
4895void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004896 // Note: if heap poisoning is enabled, the entry point takes cares
4897 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004898 QuickEntrypointEnum entrypoint =
4899 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4900 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004901 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004902 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004903}
4904
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004905void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004906 LocationSummary* locations =
4907 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004908 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4909 if (location.IsStackSlot()) {
4910 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4911 } else if (location.IsDoubleStackSlot()) {
4912 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004913 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004914 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004915}
4916
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004917void InstructionCodeGeneratorARM::VisitParameterValue(
4918 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004919 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004920}
4921
4922void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4923 LocationSummary* locations =
4924 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4925 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4926}
4927
4928void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4929 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004930}
4931
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004932void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004933 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004934 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004935 locations->SetInAt(0, Location::RequiresRegister());
4936 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004937}
4938
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004939void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4940 LocationSummary* locations = not_->GetLocations();
4941 Location out = locations->Out();
4942 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004943 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004944 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004945 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004946 break;
4947
4948 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004949 __ mvn(out.AsRegisterPairLow<Register>(),
4950 ShifterOperand(in.AsRegisterPairLow<Register>()));
4951 __ mvn(out.AsRegisterPairHigh<Register>(),
4952 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004953 break;
4954
4955 default:
4956 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4957 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004958}
4959
David Brazdil66d126e2015-04-03 16:02:44 +01004960void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4961 LocationSummary* locations =
4962 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4963 locations->SetInAt(0, Location::RequiresRegister());
4964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4965}
4966
4967void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004968 LocationSummary* locations = bool_not->GetLocations();
4969 Location out = locations->Out();
4970 Location in = locations->InAt(0);
4971 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4972}
4973
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004974void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004975 LocationSummary* locations =
4976 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004977 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004978 case Primitive::kPrimBoolean:
4979 case Primitive::kPrimByte:
4980 case Primitive::kPrimShort:
4981 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004982 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004983 case Primitive::kPrimLong: {
4984 locations->SetInAt(0, Location::RequiresRegister());
4985 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004986 // Output overlaps because it is written before doing the low comparison.
4987 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004988 break;
4989 }
4990 case Primitive::kPrimFloat:
4991 case Primitive::kPrimDouble: {
4992 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004993 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004994 locations->SetOut(Location::RequiresRegister());
4995 break;
4996 }
4997 default:
4998 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4999 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005000}
5001
5002void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005003 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005004 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005005 Location left = locations->InAt(0);
5006 Location right = locations->InAt(1);
5007
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005008 Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005009 Label* final_label = codegen_->GetFinalLabel(compare, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00005010 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00005011 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00005012 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00005013 case Primitive::kPrimBoolean:
5014 case Primitive::kPrimByte:
5015 case Primitive::kPrimShort:
5016 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08005017 case Primitive::kPrimInt: {
5018 __ LoadImmediate(out, 0);
5019 __ cmp(left.AsRegister<Register>(),
5020 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
5021 less_cond = LT;
5022 break;
5023 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005024 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005025 __ cmp(left.AsRegisterPairHigh<Register>(),
5026 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005027 __ b(&less, LT);
5028 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01005029 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00005030 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005031 __ cmp(left.AsRegisterPairLow<Register>(),
5032 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005033 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00005034 break;
5035 }
5036 case Primitive::kPrimFloat:
5037 case Primitive::kPrimDouble: {
5038 __ LoadImmediate(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005039 GenerateVcmp(compare, codegen_);
Calin Juravleddb7df22014-11-25 20:56:51 +00005040 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005041 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005042 break;
5043 }
5044 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005045 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00005046 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005047 }
Aart Bika19616e2016-02-01 18:57:58 -08005048
Anton Kirilov6f644202017-02-27 18:29:45 +00005049 __ b(final_label, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00005050 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00005051
5052 __ Bind(&greater);
5053 __ LoadImmediate(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005054 __ b(final_label);
Calin Juravleddb7df22014-11-25 20:56:51 +00005055
5056 __ Bind(&less);
5057 __ LoadImmediate(out, -1);
5058
Anton Kirilov6f644202017-02-27 18:29:45 +00005059 if (done.IsLinked()) {
5060 __ Bind(&done);
5061 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005062}
5063
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005064void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005065 LocationSummary* locations =
5066 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005067 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005068 locations->SetInAt(i, Location::Any());
5069 }
5070 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005071}
5072
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005073void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005074 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005075}
5076
Roland Levillainc9285912015-12-18 10:38:42 +00005077void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
5078 // TODO (ported from quick): revisit ARM barrier kinds.
5079 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00005080 switch (kind) {
5081 case MemBarrierKind::kAnyStore:
5082 case MemBarrierKind::kLoadAny:
5083 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005084 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00005085 break;
5086 }
5087 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005088 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00005089 break;
5090 }
5091 default:
5092 LOG(FATAL) << "Unexpected memory barrier " << kind;
5093 }
Kenny Root1d8199d2015-06-02 11:01:10 -07005094 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00005095}
5096
5097void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
5098 uint32_t offset,
5099 Register out_lo,
5100 Register out_hi) {
5101 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005102 // Ensure `out_lo` is different from `addr`, so that loading
5103 // `offset` into `out_lo` does not clutter `addr`.
5104 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00005105 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005106 __ add(IP, addr, ShifterOperand(out_lo));
5107 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005108 }
5109 __ ldrexd(out_lo, out_hi, addr);
5110}
5111
5112void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
5113 uint32_t offset,
5114 Register value_lo,
5115 Register value_hi,
5116 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00005117 Register temp2,
5118 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005119 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00005120 if (offset != 0) {
5121 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005122 __ add(IP, addr, ShifterOperand(temp1));
5123 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005124 }
5125 __ Bind(&fail);
5126 // We need a load followed by store. (The address used in a STREX instruction must
5127 // be the same as the address in the most recently executed LDREX instruction.)
5128 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00005129 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005130 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005131 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00005132}
5133
5134void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5135 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5136
Nicolas Geoffray39468442014-09-02 15:17:15 +01005137 LocationSummary* locations =
5138 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005139 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005140
Calin Juravle52c48962014-12-16 17:02:57 +00005141 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005142 if (Primitive::IsFloatingPointType(field_type)) {
5143 locations->SetInAt(1, Location::RequiresFpuRegister());
5144 } else {
5145 locations->SetInAt(1, Location::RequiresRegister());
5146 }
5147
Calin Juravle52c48962014-12-16 17:02:57 +00005148 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00005149 bool generate_volatile = field_info.IsVolatile()
5150 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005151 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01005152 bool needs_write_barrier =
5153 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005154 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00005155 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01005156 if (needs_write_barrier) {
5157 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005158 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005159 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005160 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005161 // - registers need to be consecutive
5162 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005163 // We don't test for ARM yet, and the assertion makes sure that we
5164 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005165 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5166
5167 locations->AddTemp(Location::RequiresRegister());
5168 locations->AddTemp(Location::RequiresRegister());
5169 if (field_type == Primitive::kPrimDouble) {
5170 // For doubles we need two more registers to copy the value.
5171 locations->AddTemp(Location::RegisterLocation(R2));
5172 locations->AddTemp(Location::RegisterLocation(R3));
5173 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005174 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005175}
5176
Calin Juravle52c48962014-12-16 17:02:57 +00005177void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005178 const FieldInfo& field_info,
5179 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005180 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5181
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005182 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00005183 Register base = locations->InAt(0).AsRegister<Register>();
5184 Location value = locations->InAt(1);
5185
5186 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005187 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005188 Primitive::Type field_type = field_info.GetFieldType();
5189 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005190 bool needs_write_barrier =
5191 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005192
5193 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005194 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005195 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005196
5197 switch (field_type) {
5198 case Primitive::kPrimBoolean:
5199 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00005200 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005201 break;
5202 }
5203
5204 case Primitive::kPrimShort:
5205 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00005206 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005207 break;
5208 }
5209
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005210 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005211 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01005212 if (kPoisonHeapReferences && needs_write_barrier) {
5213 // Note that in the case where `value` is a null reference,
5214 // we do not enter this block, as a null reference does not
5215 // need poisoning.
5216 DCHECK_EQ(field_type, Primitive::kPrimNot);
5217 Register temp = locations->GetTemp(0).AsRegister<Register>();
5218 __ Mov(temp, value.AsRegister<Register>());
5219 __ PoisonHeapReference(temp);
5220 __ StoreToOffset(kStoreWord, temp, base, offset);
5221 } else {
5222 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
5223 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005224 break;
5225 }
5226
5227 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00005228 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005229 GenerateWideAtomicStore(base, offset,
5230 value.AsRegisterPairLow<Register>(),
5231 value.AsRegisterPairHigh<Register>(),
5232 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005233 locations->GetTemp(1).AsRegister<Register>(),
5234 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005235 } else {
5236 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005237 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005238 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005239 break;
5240 }
5241
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005242 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00005243 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005244 break;
5245 }
5246
5247 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005248 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005249 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005250 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
5251 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
5252
5253 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
5254
5255 GenerateWideAtomicStore(base, offset,
5256 value_reg_lo,
5257 value_reg_hi,
5258 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005259 locations->GetTemp(3).AsRegister<Register>(),
5260 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005261 } else {
5262 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005263 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005264 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005265 break;
5266 }
5267
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005268 case Primitive::kPrimVoid:
5269 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005270 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005271 }
Calin Juravle52c48962014-12-16 17:02:57 +00005272
Calin Juravle77520bc2015-01-12 18:45:46 +00005273 // Longs and doubles are handled in the switch.
5274 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
5275 codegen_->MaybeRecordImplicitNullCheck(instruction);
5276 }
5277
5278 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5279 Register temp = locations->GetTemp(0).AsRegister<Register>();
5280 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005281 codegen_->MarkGCCard(
5282 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005283 }
5284
Calin Juravle52c48962014-12-16 17:02:57 +00005285 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005286 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005287 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005288}
5289
Calin Juravle52c48962014-12-16 17:02:57 +00005290void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5291 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00005292
5293 bool object_field_get_with_read_barrier =
5294 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005295 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005296 new (GetGraph()->GetArena()) LocationSummary(instruction,
5297 object_field_get_with_read_barrier ?
5298 LocationSummary::kCallOnSlowPath :
5299 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005300 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005301 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005302 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005303 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00005304
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005305 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00005306 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005307 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00005308 // The output overlaps in case of volatile long: we don't want the
5309 // code generated by GenerateWideAtomicLoad to overwrite the
5310 // object's location. Likewise, in the case of an object field get
5311 // with read barriers enabled, we do not want the load to overwrite
5312 // the object's location, as we need it to emit the read barrier.
5313 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5314 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01005315
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005316 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5317 locations->SetOut(Location::RequiresFpuRegister());
5318 } else {
5319 locations->SetOut(Location::RequiresRegister(),
5320 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5321 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005322 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00005323 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005324 // - registers need to be consecutive
5325 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005326 // We don't test for ARM yet, and the assertion makes sure that we
5327 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005328 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5329 locations->AddTemp(Location::RequiresRegister());
5330 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00005331 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5332 // We need a temporary register for the read barrier marking slow
5333 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005334 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5335 !Runtime::Current()->UseJitCompilation()) {
5336 // If link-time thunks for the Baker read barrier are enabled, for AOT
5337 // loads we need a temporary only if the offset is too big.
5338 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5339 locations->AddTemp(Location::RequiresRegister());
5340 }
5341 // And we always need the reserved entrypoint register.
5342 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5343 } else {
5344 locations->AddTemp(Location::RequiresRegister());
5345 }
Calin Juravle52c48962014-12-16 17:02:57 +00005346 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005347}
5348
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005349Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5350 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
5351 << input->GetType();
5352 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5353 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5354 return Location::ConstantLocation(input->AsConstant());
5355 } else {
5356 return Location::RequiresFpuRegister();
5357 }
5358}
5359
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005360Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
5361 Opcode opcode) {
5362 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5363 if (constant->IsConstant() &&
5364 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5365 return Location::ConstantLocation(constant->AsConstant());
5366 }
5367 return Location::RequiresRegister();
5368}
5369
5370bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
5371 Opcode opcode) {
5372 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5373 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01005374 Opcode high_opcode = opcode;
5375 SetCc low_set_cc = kCcDontCare;
5376 switch (opcode) {
5377 case SUB:
5378 // Flip the operation to an ADD.
5379 value = -value;
5380 opcode = ADD;
5381 FALLTHROUGH_INTENDED;
5382 case ADD:
5383 if (Low32Bits(value) == 0u) {
5384 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5385 }
5386 high_opcode = ADC;
5387 low_set_cc = kCcSet;
5388 break;
5389 default:
5390 break;
5391 }
5392 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5393 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005394 } else {
5395 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5396 }
5397}
5398
Vladimir Marko59751a72016-08-05 14:37:27 +01005399bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
5400 Opcode opcode,
5401 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005402 ShifterOperand so;
5403 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01005404 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005405 return true;
5406 }
5407 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005408 uint32_t neg_value = 0;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005409 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005410 case AND: neg_opcode = BIC; neg_value = ~value; break;
5411 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5412 case ADD: neg_opcode = SUB; neg_value = -value; break;
5413 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5414 case SUB: neg_opcode = ADD; neg_value = -value; break;
5415 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5416 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005417 default:
5418 return false;
5419 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005420
5421 if (assembler->ShifterOperandCanHold(kNoRegister,
5422 kNoRegister,
5423 neg_opcode,
5424 neg_value,
5425 set_cc,
5426 &so)) {
5427 return true;
5428 }
5429
5430 return opcode == AND && IsPowerOfTwo(value + 1);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005431}
5432
Calin Juravle52c48962014-12-16 17:02:57 +00005433void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
5434 const FieldInfo& field_info) {
5435 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005436
Calin Juravle52c48962014-12-16 17:02:57 +00005437 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005438 Location base_loc = locations->InAt(0);
5439 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005440 Location out = locations->Out();
5441 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005442 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005443 Primitive::Type field_type = field_info.GetFieldType();
5444 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5445
5446 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00005447 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00005448 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005449 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005450
Roland Levillainc9285912015-12-18 10:38:42 +00005451 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00005452 __ LoadFromOffset(kLoadSignedByte, 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::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00005456 __ LoadFromOffset(kLoadSignedHalfword, 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::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00005460 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005461 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005462
5463 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00005464 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005465 break;
Roland Levillainc9285912015-12-18 10:38:42 +00005466
5467 case Primitive::kPrimNot: {
5468 // /* HeapReference<Object> */ out = *(base + offset)
5469 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5470 Location temp_loc = locations->GetTemp(0);
5471 // Note that a potential implicit null check is handled in this
5472 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
5473 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5474 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5475 if (is_volatile) {
5476 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5477 }
5478 } else {
5479 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
5480 codegen_->MaybeRecordImplicitNullCheck(instruction);
5481 if (is_volatile) {
5482 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5483 }
5484 // If read barriers are enabled, emit read barriers other than
5485 // Baker's using a slow path (and also unpoison the loaded
5486 // reference, if heap poisoning is enabled).
5487 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5488 }
5489 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005490 }
5491
Roland Levillainc9285912015-12-18 10:38:42 +00005492 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00005493 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005494 GenerateWideAtomicLoad(base, offset,
5495 out.AsRegisterPairLow<Register>(),
5496 out.AsRegisterPairHigh<Register>());
5497 } else {
5498 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
5499 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005500 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005501
Roland Levillainc9285912015-12-18 10:38:42 +00005502 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00005503 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005504 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005505
5506 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005507 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005508 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005509 Register lo = locations->GetTemp(0).AsRegister<Register>();
5510 Register hi = locations->GetTemp(1).AsRegister<Register>();
5511 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00005512 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005513 __ vmovdrr(out_reg, lo, hi);
5514 } else {
5515 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005516 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005517 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005518 break;
5519 }
5520
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005521 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005522 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005523 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005524 }
Calin Juravle52c48962014-12-16 17:02:57 +00005525
Roland Levillainc9285912015-12-18 10:38:42 +00005526 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5527 // Potential implicit null checks, in the case of reference or
5528 // double fields, are handled in the previous switch statement.
5529 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005530 codegen_->MaybeRecordImplicitNullCheck(instruction);
5531 }
5532
Calin Juravle52c48962014-12-16 17:02:57 +00005533 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005534 if (field_type == Primitive::kPrimNot) {
5535 // Memory barriers, in the case of references, are also handled
5536 // in the previous switch statement.
5537 } else {
5538 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5539 }
Roland Levillain4d027112015-07-01 15:41:14 +01005540 }
Calin Juravle52c48962014-12-16 17:02:57 +00005541}
5542
5543void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5544 HandleFieldSet(instruction, instruction->GetFieldInfo());
5545}
5546
5547void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005548 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005549}
5550
5551void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5552 HandleFieldGet(instruction, instruction->GetFieldInfo());
5553}
5554
5555void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5556 HandleFieldGet(instruction, instruction->GetFieldInfo());
5557}
5558
5559void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5560 HandleFieldGet(instruction, instruction->GetFieldInfo());
5561}
5562
5563void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5564 HandleFieldGet(instruction, instruction->GetFieldInfo());
5565}
5566
5567void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5568 HandleFieldSet(instruction, instruction->GetFieldInfo());
5569}
5570
5571void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005572 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005573}
5574
Calin Juravlee460d1d2015-09-29 04:52:17 +01005575void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
5576 HUnresolvedInstanceFieldGet* instruction) {
5577 FieldAccessCallingConventionARM calling_convention;
5578 codegen_->CreateUnresolvedFieldLocationSummary(
5579 instruction, instruction->GetFieldType(), calling_convention);
5580}
5581
5582void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
5583 HUnresolvedInstanceFieldGet* instruction) {
5584 FieldAccessCallingConventionARM calling_convention;
5585 codegen_->GenerateUnresolvedFieldAccess(instruction,
5586 instruction->GetFieldType(),
5587 instruction->GetFieldIndex(),
5588 instruction->GetDexPc(),
5589 calling_convention);
5590}
5591
5592void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
5593 HUnresolvedInstanceFieldSet* instruction) {
5594 FieldAccessCallingConventionARM calling_convention;
5595 codegen_->CreateUnresolvedFieldLocationSummary(
5596 instruction, instruction->GetFieldType(), calling_convention);
5597}
5598
5599void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
5600 HUnresolvedInstanceFieldSet* instruction) {
5601 FieldAccessCallingConventionARM calling_convention;
5602 codegen_->GenerateUnresolvedFieldAccess(instruction,
5603 instruction->GetFieldType(),
5604 instruction->GetFieldIndex(),
5605 instruction->GetDexPc(),
5606 calling_convention);
5607}
5608
5609void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
5610 HUnresolvedStaticFieldGet* instruction) {
5611 FieldAccessCallingConventionARM calling_convention;
5612 codegen_->CreateUnresolvedFieldLocationSummary(
5613 instruction, instruction->GetFieldType(), calling_convention);
5614}
5615
5616void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
5617 HUnresolvedStaticFieldGet* instruction) {
5618 FieldAccessCallingConventionARM calling_convention;
5619 codegen_->GenerateUnresolvedFieldAccess(instruction,
5620 instruction->GetFieldType(),
5621 instruction->GetFieldIndex(),
5622 instruction->GetDexPc(),
5623 calling_convention);
5624}
5625
5626void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
5627 HUnresolvedStaticFieldSet* instruction) {
5628 FieldAccessCallingConventionARM calling_convention;
5629 codegen_->CreateUnresolvedFieldLocationSummary(
5630 instruction, instruction->GetFieldType(), calling_convention);
5631}
5632
5633void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
5634 HUnresolvedStaticFieldSet* instruction) {
5635 FieldAccessCallingConventionARM calling_convention;
5636 codegen_->GenerateUnresolvedFieldAccess(instruction,
5637 instruction->GetFieldType(),
5638 instruction->GetFieldIndex(),
5639 instruction->GetDexPc(),
5640 calling_convention);
5641}
5642
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005643void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005644 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5645 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005646}
5647
Calin Juravle2ae48182016-03-16 14:05:09 +00005648void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
5649 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005650 return;
5651 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005652 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005653
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005654 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005655 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005656}
5657
Calin Juravle2ae48182016-03-16 14:05:09 +00005658void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01005659 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005660 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005661
5662 LocationSummary* locations = instruction->GetLocations();
5663 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005664
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005665 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005666}
5667
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005668void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005669 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005670}
5671
Artem Serov6c916792016-07-11 14:02:34 +01005672static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5673 switch (type) {
5674 case Primitive::kPrimNot:
5675 return kLoadWord;
5676 case Primitive::kPrimBoolean:
5677 return kLoadUnsignedByte;
5678 case Primitive::kPrimByte:
5679 return kLoadSignedByte;
5680 case Primitive::kPrimChar:
5681 return kLoadUnsignedHalfword;
5682 case Primitive::kPrimShort:
5683 return kLoadSignedHalfword;
5684 case Primitive::kPrimInt:
5685 return kLoadWord;
5686 case Primitive::kPrimLong:
5687 return kLoadWordPair;
5688 case Primitive::kPrimFloat:
5689 return kLoadSWord;
5690 case Primitive::kPrimDouble:
5691 return kLoadDWord;
5692 default:
5693 LOG(FATAL) << "Unreachable type " << type;
5694 UNREACHABLE();
5695 }
5696}
5697
5698static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5699 switch (type) {
5700 case Primitive::kPrimNot:
5701 return kStoreWord;
5702 case Primitive::kPrimBoolean:
5703 case Primitive::kPrimByte:
5704 return kStoreByte;
5705 case Primitive::kPrimChar:
5706 case Primitive::kPrimShort:
5707 return kStoreHalfword;
5708 case Primitive::kPrimInt:
5709 return kStoreWord;
5710 case Primitive::kPrimLong:
5711 return kStoreWordPair;
5712 case Primitive::kPrimFloat:
5713 return kStoreSWord;
5714 case Primitive::kPrimDouble:
5715 return kStoreDWord;
5716 default:
5717 LOG(FATAL) << "Unreachable type " << type;
5718 UNREACHABLE();
5719 }
5720}
5721
5722void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
5723 Location out_loc,
5724 Register base,
5725 Register reg_offset,
5726 Condition cond) {
5727 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5728 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5729
5730 switch (type) {
5731 case Primitive::kPrimByte:
5732 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
5733 break;
5734 case Primitive::kPrimBoolean:
5735 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
5736 break;
5737 case Primitive::kPrimShort:
5738 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
5739 break;
5740 case Primitive::kPrimChar:
5741 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
5742 break;
5743 case Primitive::kPrimNot:
5744 case Primitive::kPrimInt:
5745 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
5746 break;
5747 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5748 case Primitive::kPrimLong:
5749 case Primitive::kPrimFloat:
5750 case Primitive::kPrimDouble:
5751 default:
5752 LOG(FATAL) << "Unreachable type " << type;
5753 UNREACHABLE();
5754 }
5755}
5756
5757void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
5758 Location loc,
5759 Register base,
5760 Register reg_offset,
5761 Condition cond) {
5762 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5763 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5764
5765 switch (type) {
5766 case Primitive::kPrimByte:
5767 case Primitive::kPrimBoolean:
5768 __ strb(loc.AsRegister<Register>(), mem_address, cond);
5769 break;
5770 case Primitive::kPrimShort:
5771 case Primitive::kPrimChar:
5772 __ strh(loc.AsRegister<Register>(), mem_address, cond);
5773 break;
5774 case Primitive::kPrimNot:
5775 case Primitive::kPrimInt:
5776 __ str(loc.AsRegister<Register>(), mem_address, cond);
5777 break;
5778 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5779 case Primitive::kPrimLong:
5780 case Primitive::kPrimFloat:
5781 case Primitive::kPrimDouble:
5782 default:
5783 LOG(FATAL) << "Unreachable type " << type;
5784 UNREACHABLE();
5785 }
5786}
5787
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005788void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005789 bool object_array_get_with_read_barrier =
5790 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005791 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005792 new (GetGraph()->GetArena()) LocationSummary(instruction,
5793 object_array_get_with_read_barrier ?
5794 LocationSummary::kCallOnSlowPath :
5795 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005796 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005797 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005798 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005799 locations->SetInAt(0, Location::RequiresRegister());
5800 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005801 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5802 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5803 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005804 // The output overlaps in the case of an object array get with
5805 // read barriers enabled: we do not want the move to overwrite the
5806 // array's location, as we need it to emit the read barrier.
5807 locations->SetOut(
5808 Location::RequiresRegister(),
5809 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005810 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005811 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5812 // We need a temporary register for the read barrier marking slow
5813 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
5814 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5815 !Runtime::Current()->UseJitCompilation() &&
5816 instruction->GetIndex()->IsConstant()) {
5817 // Array loads with constant index are treated as field loads.
5818 // If link-time thunks for the Baker read barrier are enabled, for AOT
5819 // constant index loads we need a temporary only if the offset is too big.
5820 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
5821 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
5822 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
5823 if (offset >= kReferenceLoadMinFarOffset) {
5824 locations->AddTemp(Location::RequiresRegister());
5825 }
5826 // And we always need the reserved entrypoint register.
5827 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5828 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
5829 !Runtime::Current()->UseJitCompilation() &&
5830 !instruction->GetIndex()->IsConstant()) {
5831 // We need a non-scratch temporary for the array data pointer.
5832 locations->AddTemp(Location::RequiresRegister());
5833 // And we always need the reserved entrypoint register.
5834 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5835 } else {
5836 locations->AddTemp(Location::RequiresRegister());
5837 }
5838 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5839 // Also need a temporary for String compression feature.
Roland Levillainc9285912015-12-18 10:38:42 +00005840 locations->AddTemp(Location::RequiresRegister());
5841 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005842}
5843
5844void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
5845 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005846 Location obj_loc = locations->InAt(0);
5847 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005848 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00005849 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005850 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00005851 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07005852 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5853 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01005854 HInstruction* array_instr = instruction->GetArray();
5855 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01005856
Roland Levillain4d027112015-07-01 15:41:14 +01005857 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01005858 case Primitive::kPrimBoolean:
5859 case Primitive::kPrimByte:
5860 case Primitive::kPrimShort:
5861 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00005862 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005863 Register length;
5864 if (maybe_compressed_char_at) {
5865 length = locations->GetTemp(0).AsRegister<Register>();
5866 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5867 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
5868 codegen_->MaybeRecordImplicitNullCheck(instruction);
5869 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005870 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005871 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07005872 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07005873 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005874 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005875 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5876 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5877 "Expecting 0=compressed, 1=uncompressed");
5878 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005879 __ LoadFromOffset(kLoadUnsignedByte,
5880 out_loc.AsRegister<Register>(),
5881 obj,
5882 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00005883 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07005884 __ Bind(&uncompressed_load);
5885 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5886 out_loc.AsRegister<Register>(),
5887 obj,
5888 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005889 if (done.IsLinked()) {
5890 __ Bind(&done);
5891 }
jessicahandojo05765752016-09-09 19:01:32 -07005892 } else {
5893 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01005894
jessicahandojo05765752016-09-09 19:01:32 -07005895 LoadOperandType load_type = GetLoadOperandType(type);
5896 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
5897 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005898 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005899 Register temp = IP;
5900
5901 if (has_intermediate_address) {
5902 // We do not need to compute the intermediate address from the array: the
5903 // input instruction has done it already. See the comment in
5904 // `TryExtractArrayAccessAddress()`.
5905 if (kIsDebugBuild) {
5906 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5907 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5908 }
5909 temp = obj;
5910 } else {
5911 __ add(temp, obj, ShifterOperand(data_offset));
5912 }
jessicahandojo05765752016-09-09 19:01:32 -07005913 if (maybe_compressed_char_at) {
5914 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005915 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005916 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5917 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5918 "Expecting 0=compressed, 1=uncompressed");
5919 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005920 __ ldrb(out_loc.AsRegister<Register>(),
5921 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00005922 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07005923 __ Bind(&uncompressed_load);
5924 __ ldrh(out_loc.AsRegister<Register>(),
5925 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005926 if (done.IsLinked()) {
5927 __ Bind(&done);
5928 }
jessicahandojo05765752016-09-09 19:01:32 -07005929 } else {
5930 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
5931 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005932 }
5933 break;
5934 }
5935
Roland Levillainc9285912015-12-18 10:38:42 +00005936 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00005937 // The read barrier instrumentation of object ArrayGet
5938 // instructions does not support the HIntermediateAddress
5939 // instruction.
5940 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5941
Roland Levillainc9285912015-12-18 10:38:42 +00005942 static_assert(
5943 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5944 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00005945 // /* HeapReference<Object> */ out =
5946 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5947 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5948 Location temp = locations->GetTemp(0);
5949 // Note that a potential implicit null check is handled in this
5950 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005951 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
5952 if (index.IsConstant()) {
5953 // Array load with a constant index can be treated as a field load.
5954 data_offset += helpers::Int32ConstantFrom(index) << Primitive::ComponentSizeShift(type);
5955 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5956 out_loc,
5957 obj,
5958 data_offset,
5959 locations->GetTemp(0),
5960 /* needs_null_check */ false);
5961 } else {
5962 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5963 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
5964 }
Roland Levillainc9285912015-12-18 10:38:42 +00005965 } else {
5966 Register out = out_loc.AsRegister<Register>();
5967 if (index.IsConstant()) {
5968 size_t offset =
5969 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5970 __ LoadFromOffset(kLoadWord, out, obj, offset);
5971 codegen_->MaybeRecordImplicitNullCheck(instruction);
5972 // If read barriers are enabled, emit read barriers other than
5973 // Baker's using a slow path (and also unpoison the loaded
5974 // reference, if heap poisoning is enabled).
5975 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5976 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005977 Register temp = IP;
5978
5979 if (has_intermediate_address) {
5980 // We do not need to compute the intermediate address from the array: the
5981 // input instruction has done it already. See the comment in
5982 // `TryExtractArrayAccessAddress()`.
5983 if (kIsDebugBuild) {
5984 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5985 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5986 }
5987 temp = obj;
5988 } else {
5989 __ add(temp, obj, ShifterOperand(data_offset));
5990 }
5991 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005992
Roland Levillainc9285912015-12-18 10:38:42 +00005993 codegen_->MaybeRecordImplicitNullCheck(instruction);
5994 // If read barriers are enabled, emit read barriers other than
5995 // Baker's using a slow path (and also unpoison the loaded
5996 // reference, if heap poisoning is enabled).
5997 codegen_->MaybeGenerateReadBarrierSlow(
5998 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5999 }
6000 }
6001 break;
6002 }
6003
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006004 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006005 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006006 size_t offset =
6007 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006008 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006009 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006010 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00006011 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006012 }
6013 break;
6014 }
6015
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006016 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00006017 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006018 if (index.IsConstant()) {
6019 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006020 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006021 } else {
6022 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00006023 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006024 }
6025 break;
6026 }
6027
6028 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00006029 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006030 if (index.IsConstant()) {
6031 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006032 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006033 } else {
6034 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00006035 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006036 }
6037 break;
6038 }
6039
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006040 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01006041 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006042 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006043 }
Roland Levillain4d027112015-07-01 15:41:14 +01006044
6045 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00006046 // Potential implicit null checks, in the case of reference
6047 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07006048 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00006049 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01006050 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006051}
6052
6053void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006054 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006055
6056 bool needs_write_barrier =
6057 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00006058 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006059
Nicolas Geoffray39468442014-09-02 15:17:15 +01006060 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006061 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01006062 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00006063 LocationSummary::kCallOnSlowPath :
6064 LocationSummary::kNoCall);
6065
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006066 locations->SetInAt(0, Location::RequiresRegister());
6067 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6068 if (Primitive::IsFloatingPointType(value_type)) {
6069 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006070 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006071 locations->SetInAt(2, Location::RequiresRegister());
6072 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006073 if (needs_write_barrier) {
6074 // Temporary registers for the write barrier.
6075 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006076 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006077 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006078}
6079
6080void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
6081 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006082 Location array_loc = locations->InAt(0);
6083 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006084 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006085 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00006086 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006087 bool needs_write_barrier =
6088 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01006089 uint32_t data_offset =
6090 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6091 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01006092 HInstruction* array_instr = instruction->GetArray();
6093 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006094
6095 switch (value_type) {
6096 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01006097 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006098 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01006099 case Primitive::kPrimChar:
6100 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006101 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01006102 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
6103 uint32_t full_offset =
6104 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6105 StoreOperandType store_type = GetStoreOperandType(value_type);
6106 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006107 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006108 Register temp = IP;
6109
6110 if (has_intermediate_address) {
6111 // We do not need to compute the intermediate address from the array: the
6112 // input instruction has done it already. See the comment in
6113 // `TryExtractArrayAccessAddress()`.
6114 if (kIsDebugBuild) {
6115 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6116 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
6117 }
6118 temp = array;
6119 } else {
6120 __ add(temp, array, ShifterOperand(data_offset));
6121 }
Artem Serov6c916792016-07-11 14:02:34 +01006122 codegen_->StoreToShiftedRegOffset(value_type,
6123 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01006124 temp,
Artem Serov6c916792016-07-11 14:02:34 +01006125 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006126 }
6127 break;
6128 }
6129
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006130 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00006131 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01006132 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6133 // See the comment in instruction_simplifier_shared.cc.
6134 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006135
6136 if (instruction->InputAt(2)->IsNullConstant()) {
6137 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006138 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006139 size_t offset =
6140 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01006141 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006142 } else {
6143 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006144 __ add(IP, array, ShifterOperand(data_offset));
6145 codegen_->StoreToShiftedRegOffset(value_type,
6146 value_loc,
6147 IP,
6148 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006149 }
Roland Levillain1407ee72016-01-08 15:56:19 +00006150 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00006151 DCHECK(!needs_write_barrier);
6152 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006153 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006154 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006155
6156 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01006157 Location temp1_loc = locations->GetTemp(0);
6158 Register temp1 = temp1_loc.AsRegister<Register>();
6159 Location temp2_loc = locations->GetTemp(1);
6160 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006161 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6162 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6163 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6164 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006165 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006166 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006167
Roland Levillain3b359c72015-11-17 19:35:12 +00006168 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006169 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
6170 codegen_->AddSlowPath(slow_path);
6171 if (instruction->GetValueCanBeNull()) {
6172 Label non_zero;
6173 __ CompareAndBranchIfNonZero(value, &non_zero);
6174 if (index.IsConstant()) {
6175 size_t offset =
6176 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6177 __ StoreToOffset(kStoreWord, value, array, offset);
6178 } else {
6179 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006180 __ add(IP, array, ShifterOperand(data_offset));
6181 codegen_->StoreToShiftedRegOffset(value_type,
6182 value_loc,
6183 IP,
6184 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006185 }
6186 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006187 __ b(final_label);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006188 __ Bind(&non_zero);
6189 }
6190
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006191 // Note that when read barriers are enabled, the type checks
6192 // are performed without read barriers. This is fine, even in
6193 // the case where a class object is in the from-space after
6194 // the flip, as a comparison involving such a type would not
6195 // produce a false positive; it may of course produce a false
6196 // negative, in which case we would take the ArraySet slow
6197 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006198
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006199 // /* HeapReference<Class> */ temp1 = array->klass_
6200 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
6201 codegen_->MaybeRecordImplicitNullCheck(instruction);
6202 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01006203
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006204 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6205 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6206 // /* HeapReference<Class> */ temp2 = value->klass_
6207 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
6208 // If heap poisoning is enabled, no need to unpoison `temp1`
6209 // nor `temp2`, as we are comparing two poisoned references.
6210 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01006211
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006212 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6213 Label do_put;
6214 __ b(&do_put, EQ);
6215 // If heap poisoning is enabled, the `temp1` reference has
6216 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00006217 __ MaybeUnpoisonHeapReference(temp1);
6218
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006219 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6220 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6221 // If heap poisoning is enabled, no need to unpoison
6222 // `temp1`, as we are comparing against null below.
6223 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
6224 __ Bind(&do_put);
6225 } else {
6226 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006227 }
6228 }
6229
Artem Serov6c916792016-07-11 14:02:34 +01006230 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006231 if (kPoisonHeapReferences) {
6232 // Note that in the case where `value` is a null reference,
6233 // we do not enter this block, as a null reference does not
6234 // need poisoning.
6235 DCHECK_EQ(value_type, Primitive::kPrimNot);
6236 __ Mov(temp1, value);
6237 __ PoisonHeapReference(temp1);
6238 source = temp1;
6239 }
6240
6241 if (index.IsConstant()) {
6242 size_t offset =
6243 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6244 __ StoreToOffset(kStoreWord, source, array, offset);
6245 } else {
6246 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006247
6248 __ add(IP, array, ShifterOperand(data_offset));
6249 codegen_->StoreToShiftedRegOffset(value_type,
6250 Location::RegisterLocation(source),
6251 IP,
6252 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006253 }
6254
Roland Levillain3b359c72015-11-17 19:35:12 +00006255 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006256 codegen_->MaybeRecordImplicitNullCheck(instruction);
6257 }
6258
6259 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6260
6261 if (done.IsLinked()) {
6262 __ Bind(&done);
6263 }
6264
6265 if (slow_path != nullptr) {
6266 __ Bind(slow_path->GetExitLabel());
6267 }
6268
6269 break;
6270 }
6271
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006272 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006273 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006274 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006275 size_t offset =
6276 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006277 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006278 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006279 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006280 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006281 }
6282 break;
6283 }
6284
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006285 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006286 Location value = locations->InAt(2);
6287 DCHECK(value.IsFpuRegister());
6288 if (index.IsConstant()) {
6289 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006290 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006291 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006292 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006293 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
6294 }
6295 break;
6296 }
6297
6298 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006299 Location value = locations->InAt(2);
6300 DCHECK(value.IsFpuRegisterPair());
6301 if (index.IsConstant()) {
6302 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006303 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006304 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006305 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006306 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
6307 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006308
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006309 break;
6310 }
6311
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006312 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006313 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006314 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006315 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006316
Roland Levillain80e67092016-01-08 16:04:55 +00006317 // Objects are handled in the switch.
6318 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006319 codegen_->MaybeRecordImplicitNullCheck(instruction);
6320 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006321}
6322
6323void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006324 LocationSummary* locations =
6325 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006326 locations->SetInAt(0, Location::RequiresRegister());
6327 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006328}
6329
6330void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
6331 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006332 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006333 Register obj = locations->InAt(0).AsRegister<Register>();
6334 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006335 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00006336 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07006337 // Mask out compression flag from String's array length.
6338 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006339 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07006340 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006341}
6342
Artem Serov328429f2016-07-06 16:23:04 +01006343void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01006344 LocationSummary* locations =
6345 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6346
6347 locations->SetInAt(0, Location::RequiresRegister());
6348 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6349 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6350}
6351
6352void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6353 LocationSummary* locations = instruction->GetLocations();
6354 Location out = locations->Out();
6355 Location first = locations->InAt(0);
6356 Location second = locations->InAt(1);
6357
Artem Serov328429f2016-07-06 16:23:04 +01006358 if (second.IsRegister()) {
6359 __ add(out.AsRegister<Register>(),
6360 first.AsRegister<Register>(),
6361 ShifterOperand(second.AsRegister<Register>()));
6362 } else {
6363 __ AddConstant(out.AsRegister<Register>(),
6364 first.AsRegister<Register>(),
6365 second.GetConstant()->AsIntConstant()->GetValue());
6366 }
6367}
6368
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006369void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006370 RegisterSet caller_saves = RegisterSet::Empty();
6371 InvokeRuntimeCallingConvention calling_convention;
6372 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6373 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6374 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006375
6376 HInstruction* index = instruction->InputAt(0);
6377 HInstruction* length = instruction->InputAt(1);
6378 // If both index and length are constants we can statically check the bounds. But if at least one
6379 // of them is not encodable ArmEncodableConstantOrRegister will create
6380 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6381 // locations.
6382 bool both_const = index->IsConstant() && length->IsConstant();
6383 locations->SetInAt(0, both_const
6384 ? Location::ConstantLocation(index->AsConstant())
6385 : ArmEncodableConstantOrRegister(index, CMP));
6386 locations->SetInAt(1, both_const
6387 ? Location::ConstantLocation(length->AsConstant())
6388 : ArmEncodableConstantOrRegister(length, CMP));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006389}
6390
6391void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
6392 LocationSummary* locations = instruction->GetLocations();
Artem Serov2dd053d2017-03-08 14:54:06 +00006393 Location index_loc = locations->InAt(0);
6394 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006395
Artem Serov2dd053d2017-03-08 14:54:06 +00006396 if (length_loc.IsConstant()) {
6397 int32_t length = helpers::Int32ConstantFrom(length_loc);
6398 if (index_loc.IsConstant()) {
6399 // BCE will remove the bounds check if we are guaranteed to pass.
6400 int32_t index = helpers::Int32ConstantFrom(index_loc);
6401 if (index < 0 || index >= length) {
6402 SlowPathCodeARM* slow_path =
6403 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6404 codegen_->AddSlowPath(slow_path);
6405 __ b(slow_path->GetEntryLabel());
6406 } else {
6407 // Some optimization after BCE may have generated this, and we should not
6408 // generate a bounds check if it is a valid range.
6409 }
6410 return;
6411 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006412
Artem Serov2dd053d2017-03-08 14:54:06 +00006413 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6414 __ cmp(index_loc.AsRegister<Register>(), ShifterOperand(length));
6415 codegen_->AddSlowPath(slow_path);
6416 __ b(slow_path->GetEntryLabel(), HS);
6417 } else {
6418 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6419 if (index_loc.IsConstant()) {
6420 int32_t index = helpers::Int32ConstantFrom(index_loc);
6421 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index));
6422 } else {
6423 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index_loc.AsRegister<Register>()));
6424 }
6425 codegen_->AddSlowPath(slow_path);
6426 __ b(slow_path->GetEntryLabel(), LS);
6427 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006428}
6429
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006430void CodeGeneratorARM::MarkGCCard(Register temp,
6431 Register card,
6432 Register object,
6433 Register value,
6434 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006435 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006436 if (can_be_null) {
6437 __ CompareAndBranchIfZero(value, &is_null);
6438 }
Andreas Gampe542451c2016-07-26 09:02:02 -07006439 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006440 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
6441 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006442 if (can_be_null) {
6443 __ Bind(&is_null);
6444 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006445}
6446
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006447void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006448 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006449}
6450
6451void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006452 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6453}
6454
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006455void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006456 LocationSummary* locations =
6457 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006458 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006459}
6460
6461void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006462 HBasicBlock* block = instruction->GetBlock();
6463 if (block->GetLoopInformation() != nullptr) {
6464 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6465 // The back edge will generate the suspend check.
6466 return;
6467 }
6468 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6469 // The goto will generate the suspend check.
6470 return;
6471 }
6472 GenerateSuspendCheck(instruction, nullptr);
6473}
6474
6475void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
6476 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006477 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006478 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
6479 if (slow_path == nullptr) {
6480 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
6481 instruction->SetSlowPath(slow_path);
6482 codegen_->AddSlowPath(slow_path);
6483 if (successor != nullptr) {
6484 DCHECK(successor->IsLoopHeader());
6485 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6486 }
6487 } else {
6488 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6489 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006490
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00006491 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006492 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006493 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006494 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006495 __ Bind(slow_path->GetReturnLabel());
6496 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006497 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006498 __ b(slow_path->GetEntryLabel());
6499 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006500}
6501
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006502ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
6503 return codegen_->GetAssembler();
6504}
6505
6506void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006507 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006508 Location source = move->GetSource();
6509 Location destination = move->GetDestination();
6510
6511 if (source.IsRegister()) {
6512 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006513 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006514 } else if (destination.IsFpuRegister()) {
6515 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006516 } else {
6517 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006518 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006519 SP, destination.GetStackIndex());
6520 }
6521 } else if (source.IsStackSlot()) {
6522 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006523 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006524 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006525 } else if (destination.IsFpuRegister()) {
6526 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006527 } else {
6528 DCHECK(destination.IsStackSlot());
6529 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
6530 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6531 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006532 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006533 if (destination.IsRegister()) {
6534 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
6535 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006536 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006537 } else {
6538 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006539 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
6540 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006541 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006542 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006543 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
6544 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006545 } else if (destination.IsRegisterPair()) {
6546 DCHECK(ExpectedPairLayout(destination));
6547 __ LoadFromOffset(
6548 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
6549 } else {
6550 DCHECK(destination.IsFpuRegisterPair()) << destination;
6551 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6552 SP,
6553 source.GetStackIndex());
6554 }
6555 } else if (source.IsRegisterPair()) {
6556 if (destination.IsRegisterPair()) {
6557 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6558 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006559 } else if (destination.IsFpuRegisterPair()) {
6560 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6561 source.AsRegisterPairLow<Register>(),
6562 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006563 } else {
6564 DCHECK(destination.IsDoubleStackSlot()) << destination;
6565 DCHECK(ExpectedPairLayout(source));
6566 __ StoreToOffset(
6567 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
6568 }
6569 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006570 if (destination.IsRegisterPair()) {
6571 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6572 destination.AsRegisterPairHigh<Register>(),
6573 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6574 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006575 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6576 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6577 } else {
6578 DCHECK(destination.IsDoubleStackSlot()) << destination;
6579 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
6580 SP,
6581 destination.GetStackIndex());
6582 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006583 } else {
6584 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006585 HConstant* constant = source.GetConstant();
6586 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6587 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006588 if (destination.IsRegister()) {
6589 __ LoadImmediate(destination.AsRegister<Register>(), value);
6590 } else {
6591 DCHECK(destination.IsStackSlot());
6592 __ LoadImmediate(IP, value);
6593 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6594 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006595 } else if (constant->IsLongConstant()) {
6596 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006597 if (destination.IsRegisterPair()) {
6598 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
6599 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006600 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006601 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006602 __ LoadImmediate(IP, Low32Bits(value));
6603 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6604 __ LoadImmediate(IP, High32Bits(value));
6605 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6606 }
6607 } else if (constant->IsDoubleConstant()) {
6608 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006609 if (destination.IsFpuRegisterPair()) {
6610 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006611 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006612 DCHECK(destination.IsDoubleStackSlot()) << destination;
6613 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006614 __ LoadImmediate(IP, Low32Bits(int_value));
6615 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6616 __ LoadImmediate(IP, High32Bits(int_value));
6617 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6618 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006619 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006620 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006621 float value = constant->AsFloatConstant()->GetValue();
6622 if (destination.IsFpuRegister()) {
6623 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
6624 } else {
6625 DCHECK(destination.IsStackSlot());
6626 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
6627 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6628 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006629 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006630 }
6631}
6632
6633void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
6634 __ Mov(IP, reg);
6635 __ LoadFromOffset(kLoadWord, reg, SP, mem);
6636 __ StoreToOffset(kStoreWord, IP, SP, mem);
6637}
6638
6639void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
6640 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
6641 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6642 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
6643 SP, mem1 + stack_offset);
6644 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
6645 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
6646 SP, mem2 + stack_offset);
6647 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
6648}
6649
6650void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006651 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006652 Location source = move->GetSource();
6653 Location destination = move->GetDestination();
6654
6655 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006656 DCHECK_NE(source.AsRegister<Register>(), IP);
6657 DCHECK_NE(destination.AsRegister<Register>(), IP);
6658 __ Mov(IP, source.AsRegister<Register>());
6659 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
6660 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006661 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006662 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006663 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006664 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006665 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
6666 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006667 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006668 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006669 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006670 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006671 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006672 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006673 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006674 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006675 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6676 destination.AsRegisterPairHigh<Register>(),
6677 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006678 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006679 Register low_reg = source.IsRegisterPair()
6680 ? source.AsRegisterPairLow<Register>()
6681 : destination.AsRegisterPairLow<Register>();
6682 int mem = source.IsRegisterPair()
6683 ? destination.GetStackIndex()
6684 : source.GetStackIndex();
6685 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006686 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006687 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006688 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006689 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006690 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
6691 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006692 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006693 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006694 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006695 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
6696 DRegister reg = source.IsFpuRegisterPair()
6697 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
6698 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
6699 int mem = source.IsFpuRegisterPair()
6700 ? destination.GetStackIndex()
6701 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006702 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006703 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006704 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006705 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
6706 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
6707 : destination.AsFpuRegister<SRegister>();
6708 int mem = source.IsFpuRegister()
6709 ? destination.GetStackIndex()
6710 : source.GetStackIndex();
6711
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006712 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006713 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006714 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006715 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006716 Exchange(source.GetStackIndex(), destination.GetStackIndex());
6717 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006718 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006719 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006720 }
6721}
6722
6723void ParallelMoveResolverARM::SpillScratch(int reg) {
6724 __ Push(static_cast<Register>(reg));
6725}
6726
6727void ParallelMoveResolverARM::RestoreScratch(int reg) {
6728 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006729}
6730
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006731HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
6732 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006733 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006734 case HLoadClass::LoadKind::kInvalid:
6735 LOG(FATAL) << "UNREACHABLE";
6736 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006737 case HLoadClass::LoadKind::kReferrersClass:
6738 break;
6739 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6740 DCHECK(!GetCompilerOptions().GetCompilePic());
6741 break;
6742 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6743 DCHECK(GetCompilerOptions().GetCompilePic());
6744 break;
6745 case HLoadClass::LoadKind::kBootImageAddress:
6746 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006747 case HLoadClass::LoadKind::kBssEntry:
6748 DCHECK(!Runtime::Current()->UseJitCompilation());
6749 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006750 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006751 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006752 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006753 case HLoadClass::LoadKind::kDexCacheViaMethod:
6754 break;
6755 }
6756 return desired_class_load_kind;
6757}
6758
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006759void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006760 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6761 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006762 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006763 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006764 cls,
6765 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006766 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006767 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006768 return;
6769 }
Vladimir Marko41559982017-01-06 14:04:23 +00006770 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006771
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006772 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6773 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006774 ? LocationSummary::kCallOnSlowPath
6775 : LocationSummary::kNoCall;
6776 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006777 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006778 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006779 }
6780
Vladimir Marko41559982017-01-06 14:04:23 +00006781 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006782 locations->SetInAt(0, Location::RequiresRegister());
6783 }
6784 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006785 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6786 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6787 // Rely on the type resolution or initialization and marking to save everything we need.
6788 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6789 // to the custom calling convention) or by marking, so we request a different temp.
6790 locations->AddTemp(Location::RequiresRegister());
6791 RegisterSet caller_saves = RegisterSet::Empty();
6792 InvokeRuntimeCallingConvention calling_convention;
6793 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6794 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6795 // that the the kPrimNot result register is the same as the first argument register.
6796 locations->SetCustomSlowPathCallerSaves(caller_saves);
6797 } else {
6798 // For non-Baker read barrier we have a temp-clobbering call.
6799 }
6800 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006801 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
6802 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
6803 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
6804 !Runtime::Current()->UseJitCompilation())) {
6805 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6806 }
6807 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006808}
6809
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006810// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6811// move.
6812void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006813 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6814 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6815 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006816 return;
6817 }
Vladimir Marko41559982017-01-06 14:04:23 +00006818 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006819
Vladimir Marko41559982017-01-06 14:04:23 +00006820 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006821 Location out_loc = locations->Out();
6822 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006823
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006824 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6825 ? kWithoutReadBarrier
6826 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006827 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006828 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006829 case HLoadClass::LoadKind::kReferrersClass: {
6830 DCHECK(!cls->CanCallRuntime());
6831 DCHECK(!cls->MustGenerateClinitCheck());
6832 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6833 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006834 GenerateGcRootFieldLoad(cls,
6835 out_loc,
6836 current_method,
6837 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006838 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006839 break;
6840 }
6841 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006842 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006843 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006844 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6845 cls->GetTypeIndex()));
6846 break;
6847 }
6848 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) {
6947 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6948 DCHECK(!GetCompilerOptions().GetCompilePic());
6949 break;
6950 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6951 DCHECK(GetCompilerOptions().GetCompilePic());
6952 break;
6953 case HLoadString::LoadKind::kBootImageAddress:
6954 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006955 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006956 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006957 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006958 case HLoadString::LoadKind::kJitTableAddress:
6959 DCHECK(Runtime::Current()->UseJitCompilation());
6960 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006961 case HLoadString::LoadKind::kDexCacheViaMethod:
6962 break;
6963 }
6964 return desired_string_load_kind;
6965}
6966
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006967void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006968 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006969 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006970 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006971 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006972 locations->SetOut(Location::RegisterLocation(R0));
6973 } else {
6974 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006975 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6976 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006977 // Rely on the pResolveString and marking to save everything we need, including temps.
6978 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6979 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006980 locations->AddTemp(Location::RequiresRegister());
6981 RegisterSet caller_saves = RegisterSet::Empty();
6982 InvokeRuntimeCallingConvention calling_convention;
6983 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6984 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6985 // that the the kPrimNot result register is the same as the first argument register.
6986 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006987 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
6988 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6989 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006990 } else {
6991 // For non-Baker read barrier we have a temp-clobbering call.
6992 }
6993 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006994 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006995}
6996
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006997// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6998// move.
6999void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007000 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007001 Location out_loc = locations->Out();
7002 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007003 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00007004
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007005 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007006 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007007 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007008 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
7009 load->GetStringIndex()));
7010 return; // No dex cache slow path.
7011 }
7012 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007013 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007014 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007015 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007016 __ BindTrackedLabel(&labels->movw_label);
7017 __ movw(out, /* placeholder */ 0u);
7018 __ BindTrackedLabel(&labels->movt_label);
7019 __ movt(out, /* placeholder */ 0u);
7020 __ BindTrackedLabel(&labels->add_pc_label);
7021 __ add(out, out, ShifterOperand(PC));
7022 return; // No dex cache slow path.
7023 }
7024 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007025 uint32_t address = dchecked_integral_cast<uint32_t>(
7026 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7027 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007028 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7029 return; // No dex cache slow path.
7030 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007031 case HLoadString::LoadKind::kBssEntry: {
7032 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007033 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7034 ? locations->GetTemp(0).AsRegister<Register>()
7035 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00007036 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007037 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007038 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007039 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007040 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007041 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007042 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007043 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007044 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007045 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
7046 codegen_->AddSlowPath(slow_path);
7047 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7048 __ Bind(slow_path->GetExitLabel());
7049 return;
7050 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007051 case HLoadString::LoadKind::kJitTableAddress: {
7052 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007053 load->GetStringIndex(),
7054 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007055 // /* GcRoot<mirror::String> */ out = *out
7056 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7057 return;
7058 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007059 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007060 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007061 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007062
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007063 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
7064 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
7065 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007066 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007067 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007068 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7069 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007070}
7071
David Brazdilcb1c0552015-08-04 16:22:25 +01007072static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007073 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01007074}
7075
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007076void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
7077 LocationSummary* locations =
7078 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7079 locations->SetOut(Location::RequiresRegister());
7080}
7081
7082void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007083 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01007084 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7085}
7086
7087void LocationsBuilderARM::VisitClearException(HClearException* clear) {
7088 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7089}
7090
7091void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007092 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01007093 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007094}
7095
7096void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
7097 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007098 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007099 InvokeRuntimeCallingConvention calling_convention;
7100 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7101}
7102
7103void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007104 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007105 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007106}
7107
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007108// Temp is used for read barrier.
7109static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7110 if (kEmitCompilerReadBarrier &&
7111 (kUseBakerReadBarrier ||
7112 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7113 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7114 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7115 return 1;
7116 }
7117 return 0;
7118}
7119
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007120// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007121// interface pointer, one for loading the current interface.
7122// The other checks have one temp for loading the object's class.
7123static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7124 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7125 return 3;
7126 }
7127 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00007128}
7129
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007130void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007131 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00007132 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007133 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00007134 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007135 case TypeCheckKind::kExactCheck:
7136 case TypeCheckKind::kAbstractClassCheck:
7137 case TypeCheckKind::kClassHierarchyCheck:
7138 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007139 call_kind =
7140 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01007141 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007142 break;
7143 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007144 case TypeCheckKind::kUnresolvedCheck:
7145 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007146 call_kind = LocationSummary::kCallOnSlowPath;
7147 break;
7148 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007149
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007150 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007151 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007152 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007153 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007154 locations->SetInAt(0, Location::RequiresRegister());
7155 locations->SetInAt(1, Location::RequiresRegister());
7156 // The "out" register is used as a temporary, so it overlaps with the inputs.
7157 // Note that TypeCheckSlowPathARM uses this register too.
7158 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007159 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007160 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7161 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7162 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007163}
7164
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007165void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007166 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007167 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007168 Location obj_loc = locations->InAt(0);
7169 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007170 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007171 Location out_loc = locations->Out();
7172 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007173 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7174 DCHECK_LE(num_temps, 1u);
7175 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007176 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007177 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7178 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7179 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007180 Label done;
7181 Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01007182 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007183
7184 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007185 // avoid null check if we know obj is not null.
7186 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007187 DCHECK_NE(out, obj);
7188 __ LoadImmediate(out, 0);
7189 __ CompareAndBranchIfZero(obj, final_label);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007190 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007191
Roland Levillainc9285912015-12-18 10:38:42 +00007192 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007193 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007194 // /* HeapReference<Class> */ out = obj->klass_
7195 GenerateReferenceLoadTwoRegisters(instruction,
7196 out_loc,
7197 obj_loc,
7198 class_offset,
7199 maybe_temp_loc,
7200 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007201 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007202 __ cmp(out, ShifterOperand(cls));
7203 // We speculatively set the result to false without changing the condition
7204 // flags, which allows us to avoid some branching later.
7205 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7206
7207 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7208 // we check that the output is in a low register, so that a 16-bit MOV
7209 // encoding can be used.
7210 if (ArmAssembler::IsLowRegister(out)) {
7211 __ it(EQ);
7212 __ mov(out, ShifterOperand(1), EQ);
7213 } else {
7214 __ b(final_label, NE);
7215 __ LoadImmediate(out, 1);
7216 }
7217
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007218 break;
7219 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007220
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007221 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007222 // /* HeapReference<Class> */ out = obj->klass_
7223 GenerateReferenceLoadTwoRegisters(instruction,
7224 out_loc,
7225 obj_loc,
7226 class_offset,
7227 maybe_temp_loc,
7228 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007229 // If the class is abstract, we eagerly fetch the super class of the
7230 // object to avoid doing a comparison we know will fail.
7231 Label loop;
7232 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007233 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007234 GenerateReferenceLoadOneRegister(instruction,
7235 out_loc,
7236 super_offset,
7237 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007238 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007239 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007240 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007241 __ cmp(out, ShifterOperand(cls));
7242 __ b(&loop, NE);
7243 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007244 break;
7245 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007246
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007247 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007248 // /* HeapReference<Class> */ out = obj->klass_
7249 GenerateReferenceLoadTwoRegisters(instruction,
7250 out_loc,
7251 obj_loc,
7252 class_offset,
7253 maybe_temp_loc,
7254 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007255 // Walk over the class hierarchy to find a match.
7256 Label loop, success;
7257 __ Bind(&loop);
7258 __ cmp(out, ShifterOperand(cls));
7259 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007260 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007261 GenerateReferenceLoadOneRegister(instruction,
7262 out_loc,
7263 super_offset,
7264 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007265 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007266 // This is essentially a null check, but it sets the condition flags to the
7267 // proper value for the code that follows the loop, i.e. not `EQ`.
7268 __ cmp(out, ShifterOperand(1));
7269 __ b(&loop, HS);
7270
7271 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7272 // we check that the output is in a low register, so that a 16-bit MOV
7273 // encoding can be used.
7274 if (ArmAssembler::IsLowRegister(out)) {
7275 // If `out` is null, we use it for the result, and the condition flags
7276 // have already been set to `NE`, so the IT block that comes afterwards
7277 // (and which handles the successful case) turns into a NOP (instead of
7278 // overwriting `out`).
7279 __ Bind(&success);
7280 // There is only one branch to the `success` label (which is bound to this
7281 // IT block), and it has the same condition, `EQ`, so in that case the MOV
7282 // is executed.
7283 __ it(EQ);
7284 __ mov(out, ShifterOperand(1), EQ);
7285 } else {
7286 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007287 __ b(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007288 __ Bind(&success);
7289 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007290 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007291
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007292 break;
7293 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007294
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007295 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007296 // /* HeapReference<Class> */ out = obj->klass_
7297 GenerateReferenceLoadTwoRegisters(instruction,
7298 out_loc,
7299 obj_loc,
7300 class_offset,
7301 maybe_temp_loc,
7302 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007303 // Do an exact check.
7304 Label exact_check;
7305 __ cmp(out, ShifterOperand(cls));
7306 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007307 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007308 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007309 GenerateReferenceLoadOneRegister(instruction,
7310 out_loc,
7311 component_offset,
7312 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007313 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007314 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007315 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007316 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7317 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007318 __ cmp(out, ShifterOperand(0));
7319 // We speculatively set the result to false without changing the condition
7320 // flags, which allows us to avoid some branching later.
7321 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7322
7323 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7324 // we check that the output is in a low register, so that a 16-bit MOV
7325 // encoding can be used.
7326 if (ArmAssembler::IsLowRegister(out)) {
7327 __ Bind(&exact_check);
7328 __ it(EQ);
7329 __ mov(out, ShifterOperand(1), EQ);
7330 } else {
7331 __ b(final_label, NE);
7332 __ Bind(&exact_check);
7333 __ LoadImmediate(out, 1);
7334 }
7335
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007336 break;
7337 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007338
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007339 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007340 // No read barrier since the slow path will retry upon failure.
7341 // /* HeapReference<Class> */ out = obj->klass_
7342 GenerateReferenceLoadTwoRegisters(instruction,
7343 out_loc,
7344 obj_loc,
7345 class_offset,
7346 maybe_temp_loc,
7347 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007348 __ cmp(out, ShifterOperand(cls));
7349 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00007350 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7351 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007352 codegen_->AddSlowPath(slow_path);
7353 __ b(slow_path->GetEntryLabel(), NE);
7354 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007355 break;
7356 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007357
Calin Juravle98893e12015-10-02 21:05:03 +01007358 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007359 case TypeCheckKind::kInterfaceCheck: {
7360 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007361 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00007362 // cases.
7363 //
7364 // We cannot directly call the InstanceofNonTrivial runtime
7365 // entry point without resorting to a type checking slow path
7366 // here (i.e. by calling InvokeRuntime directly), as it would
7367 // require to assign fixed registers for the inputs of this
7368 // HInstanceOf instruction (following the runtime calling
7369 // convention), which might be cluttered by the potential first
7370 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00007371 //
7372 // TODO: Introduce a new runtime entry point taking the object
7373 // to test (instead of its class) as argument, and let it deal
7374 // with the read barrier issues. This will let us refactor this
7375 // case of the `switch` code as it was previously (with a direct
7376 // call to the runtime not using a type checking slow path).
7377 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00007378 DCHECK(locations->OnlyCallsOnSlowPath());
7379 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7380 /* is_fatal */ false);
7381 codegen_->AddSlowPath(slow_path);
7382 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007383 break;
7384 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007385 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007386
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007387 if (done.IsLinked()) {
7388 __ Bind(&done);
7389 }
7390
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007391 if (slow_path != nullptr) {
7392 __ Bind(slow_path->GetExitLabel());
7393 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007394}
7395
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007396void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007397 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7398 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7399
Roland Levillain3b359c72015-11-17 19:35:12 +00007400 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7401 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007402 case TypeCheckKind::kExactCheck:
7403 case TypeCheckKind::kAbstractClassCheck:
7404 case TypeCheckKind::kClassHierarchyCheck:
7405 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007406 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7407 LocationSummary::kCallOnSlowPath :
7408 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007409 break;
7410 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007411 case TypeCheckKind::kUnresolvedCheck:
7412 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007413 call_kind = LocationSummary::kCallOnSlowPath;
7414 break;
7415 }
7416
Roland Levillain3b359c72015-11-17 19:35:12 +00007417 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7418 locations->SetInAt(0, Location::RequiresRegister());
7419 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007420 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007421}
7422
7423void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007424 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007425 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007426 Location obj_loc = locations->InAt(0);
7427 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007428 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007429 Location temp_loc = locations->GetTemp(0);
7430 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007431 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7432 DCHECK_LE(num_temps, 3u);
7433 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7434 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7435 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7436 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7437 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7438 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7439 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7440 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7441 const uint32_t object_array_data_offset =
7442 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007443
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007444 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7445 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7446 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007447 bool is_type_check_slow_path_fatal = false;
7448 if (!kEmitCompilerReadBarrier) {
7449 is_type_check_slow_path_fatal =
7450 (type_check_kind == TypeCheckKind::kExactCheck ||
7451 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7452 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7453 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7454 !instruction->CanThrowIntoCatchBlock();
7455 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01007456 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007457 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7458 is_type_check_slow_path_fatal);
7459 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007460
7461 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007462 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007463 // Avoid null check if we know obj is not null.
7464 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007465 __ CompareAndBranchIfZero(obj, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007466 }
7467
Roland Levillain3b359c72015-11-17 19:35:12 +00007468 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007469 case TypeCheckKind::kExactCheck:
7470 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007471 // /* HeapReference<Class> */ temp = obj->klass_
7472 GenerateReferenceLoadTwoRegisters(instruction,
7473 temp_loc,
7474 obj_loc,
7475 class_offset,
7476 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007477 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007478
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007479 __ cmp(temp, ShifterOperand(cls));
7480 // Jump to slow path for throwing the exception or doing a
7481 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00007482 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007483 break;
7484 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007485
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007486 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007487 // /* HeapReference<Class> */ temp = obj->klass_
7488 GenerateReferenceLoadTwoRegisters(instruction,
7489 temp_loc,
7490 obj_loc,
7491 class_offset,
7492 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007493 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007494
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007495 // If the class is abstract, we eagerly fetch the super class of the
7496 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007497 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007498 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007499 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007500 GenerateReferenceLoadOneRegister(instruction,
7501 temp_loc,
7502 super_offset,
7503 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007504 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007505
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007506 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7507 // exception.
7508 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00007509
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007510 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007511 __ cmp(temp, ShifterOperand(cls));
7512 __ b(&loop, NE);
7513 break;
7514 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007515
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007516 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007517 // /* HeapReference<Class> */ temp = obj->klass_
7518 GenerateReferenceLoadTwoRegisters(instruction,
7519 temp_loc,
7520 obj_loc,
7521 class_offset,
7522 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007523 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007524
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007525 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007526 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007527 __ Bind(&loop);
7528 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007529 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007530
Roland Levillain3b359c72015-11-17 19:35:12 +00007531 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007532 GenerateReferenceLoadOneRegister(instruction,
7533 temp_loc,
7534 super_offset,
7535 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007536 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007537
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007538 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7539 // exception.
7540 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7541 // Otherwise, jump to the beginning of the loop.
7542 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007543 break;
7544 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007545
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007546 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007547 // /* HeapReference<Class> */ temp = obj->klass_
7548 GenerateReferenceLoadTwoRegisters(instruction,
7549 temp_loc,
7550 obj_loc,
7551 class_offset,
7552 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007553 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007554
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007555 // Do an exact check.
7556 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007557 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007558
7559 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007560 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007561 GenerateReferenceLoadOneRegister(instruction,
7562 temp_loc,
7563 component_offset,
7564 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007565 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007566 // If the component type is null, jump to the slow path to throw the exception.
7567 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7568 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7569 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007570 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00007571 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007572 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007573 break;
7574 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007575
Calin Juravle98893e12015-10-02 21:05:03 +01007576 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007577 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00007578 // We cannot directly call the CheckCast runtime entry point
7579 // without resorting to a type checking slow path here (i.e. by
7580 // calling InvokeRuntime directly), as it would require to
7581 // assign fixed registers for the inputs of this HInstanceOf
7582 // instruction (following the runtime calling convention), which
7583 // might be cluttered by the potential first read barrier
7584 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007585
Roland Levillain3b359c72015-11-17 19:35:12 +00007586 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007587 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007588
7589 case TypeCheckKind::kInterfaceCheck: {
7590 // Avoid read barriers to improve performance of the fast path. We can not get false
7591 // positives by doing this.
7592 // /* HeapReference<Class> */ temp = obj->klass_
7593 GenerateReferenceLoadTwoRegisters(instruction,
7594 temp_loc,
7595 obj_loc,
7596 class_offset,
7597 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007598 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007599
7600 // /* HeapReference<Class> */ temp = temp->iftable_
7601 GenerateReferenceLoadTwoRegisters(instruction,
7602 temp_loc,
7603 temp_loc,
7604 iftable_offset,
7605 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007606 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007607 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007608 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08007609 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007610 Label start_loop;
7611 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007612 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
7613 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007614 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
7615 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007616 // Go to next interface.
7617 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
7618 __ sub(maybe_temp2_loc.AsRegister<Register>(),
7619 maybe_temp2_loc.AsRegister<Register>(),
7620 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007621 // Compare the classes and continue the loop if they do not match.
7622 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
7623 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007624 break;
7625 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007626 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007627
7628 if (done.IsLinked()) {
7629 __ Bind(&done);
7630 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007631
Roland Levillain3b359c72015-11-17 19:35:12 +00007632 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007633}
7634
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007635void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
7636 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007637 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007638 InvokeRuntimeCallingConvention calling_convention;
7639 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7640}
7641
7642void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007643 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7644 instruction,
7645 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007646 if (instruction->IsEnter()) {
7647 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7648 } else {
7649 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7650 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007651}
7652
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007653void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
7654void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
7655void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007656
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007657void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007658 LocationSummary* locations =
7659 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7660 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7661 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007662 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007663 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007664 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00007665 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007666}
7667
7668void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
7669 HandleBitwiseOperation(instruction);
7670}
7671
7672void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
7673 HandleBitwiseOperation(instruction);
7674}
7675
7676void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
7677 HandleBitwiseOperation(instruction);
7678}
7679
Artem Serov7fc63502016-02-09 17:15:29 +00007680
7681void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7682 LocationSummary* locations =
7683 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7684 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7685 || instruction->GetResultType() == Primitive::kPrimLong);
7686
7687 locations->SetInAt(0, Location::RequiresRegister());
7688 locations->SetInAt(1, Location::RequiresRegister());
7689 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7690}
7691
7692void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7693 LocationSummary* locations = instruction->GetLocations();
7694 Location first = locations->InAt(0);
7695 Location second = locations->InAt(1);
7696 Location out = locations->Out();
7697
7698 if (instruction->GetResultType() == Primitive::kPrimInt) {
7699 Register first_reg = first.AsRegister<Register>();
7700 ShifterOperand second_reg(second.AsRegister<Register>());
7701 Register out_reg = out.AsRegister<Register>();
7702
7703 switch (instruction->GetOpKind()) {
7704 case HInstruction::kAnd:
7705 __ bic(out_reg, first_reg, second_reg);
7706 break;
7707 case HInstruction::kOr:
7708 __ orn(out_reg, first_reg, second_reg);
7709 break;
7710 // There is no EON on arm.
7711 case HInstruction::kXor:
7712 default:
7713 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7714 UNREACHABLE();
7715 }
7716 return;
7717
7718 } else {
7719 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7720 Register first_low = first.AsRegisterPairLow<Register>();
7721 Register first_high = first.AsRegisterPairHigh<Register>();
7722 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7723 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7724 Register out_low = out.AsRegisterPairLow<Register>();
7725 Register out_high = out.AsRegisterPairHigh<Register>();
7726
7727 switch (instruction->GetOpKind()) {
7728 case HInstruction::kAnd:
7729 __ bic(out_low, first_low, second_low);
7730 __ bic(out_high, first_high, second_high);
7731 break;
7732 case HInstruction::kOr:
7733 __ orn(out_low, first_low, second_low);
7734 __ orn(out_high, first_high, second_high);
7735 break;
7736 // There is no EON on arm.
7737 case HInstruction::kXor:
7738 default:
7739 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7740 UNREACHABLE();
7741 }
7742 }
7743}
7744
Anton Kirilov74234da2017-01-13 14:42:47 +00007745void LocationsBuilderARM::VisitDataProcWithShifterOp(
7746 HDataProcWithShifterOp* instruction) {
7747 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
7748 instruction->GetType() == Primitive::kPrimLong);
7749 LocationSummary* locations =
7750 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7751 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
7752 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7753
7754 locations->SetInAt(0, Location::RequiresRegister());
7755 locations->SetInAt(1, Location::RequiresRegister());
7756 locations->SetOut(Location::RequiresRegister(),
7757 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7758}
7759
7760void InstructionCodeGeneratorARM::VisitDataProcWithShifterOp(
7761 HDataProcWithShifterOp* instruction) {
7762 const LocationSummary* const locations = instruction->GetLocations();
7763 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
7764 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
7765 const Location left = locations->InAt(0);
7766 const Location right = locations->InAt(1);
7767 const Location out = locations->Out();
7768
7769 if (instruction->GetType() == Primitive::kPrimInt) {
7770 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
7771
7772 const Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
7773 ? right.AsRegisterPairLow<Register>()
7774 : right.AsRegister<Register>();
7775
7776 GenerateDataProcInstruction(kind,
7777 out.AsRegister<Register>(),
7778 left.AsRegister<Register>(),
7779 ShifterOperand(second,
7780 ShiftFromOpKind(op_kind),
7781 instruction->GetShiftAmount()),
7782 codegen_);
7783 } else {
7784 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7785
7786 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7787 const Register second = right.AsRegister<Register>();
7788
7789 DCHECK_NE(out.AsRegisterPairLow<Register>(), second);
7790 GenerateDataProc(kind,
7791 out,
7792 left,
7793 ShifterOperand(second),
7794 ShifterOperand(second, ASR, 31),
7795 codegen_);
7796 } else {
7797 GenerateLongDataProc(instruction, codegen_);
7798 }
7799 }
7800}
7801
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007802void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
7803 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7804 if (value == 0xffffffffu) {
7805 if (out != first) {
7806 __ mov(out, ShifterOperand(first));
7807 }
7808 return;
7809 }
7810 if (value == 0u) {
7811 __ mov(out, ShifterOperand(0));
7812 return;
7813 }
7814 ShifterOperand so;
7815 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
7816 __ and_(out, first, so);
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007817 } else if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007818 __ bic(out, first, ShifterOperand(~value));
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007819 } else {
7820 DCHECK(IsPowerOfTwo(value + 1));
7821 __ ubfx(out, first, 0, WhichPowerOf2(value + 1));
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007822 }
7823}
7824
7825void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
7826 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7827 if (value == 0u) {
7828 if (out != first) {
7829 __ mov(out, ShifterOperand(first));
7830 }
7831 return;
7832 }
7833 if (value == 0xffffffffu) {
7834 __ mvn(out, ShifterOperand(0));
7835 return;
7836 }
7837 ShifterOperand so;
7838 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
7839 __ orr(out, first, so);
7840 } else {
7841 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
7842 __ orn(out, first, ShifterOperand(~value));
7843 }
7844}
7845
7846void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
7847 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7848 if (value == 0u) {
7849 if (out != first) {
7850 __ mov(out, ShifterOperand(first));
7851 }
7852 return;
7853 }
7854 __ eor(out, first, ShifterOperand(value));
7855}
7856
Vladimir Marko59751a72016-08-05 14:37:27 +01007857void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
7858 Location first,
7859 uint64_t value) {
7860 Register out_low = out.AsRegisterPairLow<Register>();
7861 Register out_high = out.AsRegisterPairHigh<Register>();
7862 Register first_low = first.AsRegisterPairLow<Register>();
7863 Register first_high = first.AsRegisterPairHigh<Register>();
7864 uint32_t value_low = Low32Bits(value);
7865 uint32_t value_high = High32Bits(value);
7866 if (value_low == 0u) {
7867 if (out_low != first_low) {
7868 __ mov(out_low, ShifterOperand(first_low));
7869 }
7870 __ AddConstant(out_high, first_high, value_high);
7871 return;
7872 }
7873 __ AddConstantSetFlags(out_low, first_low, value_low);
7874 ShifterOperand so;
7875 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
7876 __ adc(out_high, first_high, so);
7877 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
7878 __ sbc(out_high, first_high, so);
7879 } else {
7880 LOG(FATAL) << "Unexpected constant " << value_high;
7881 UNREACHABLE();
7882 }
7883}
7884
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007885void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
7886 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007887 Location first = locations->InAt(0);
7888 Location second = locations->InAt(1);
7889 Location out = locations->Out();
7890
7891 if (second.IsConstant()) {
7892 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7893 uint32_t value_low = Low32Bits(value);
7894 if (instruction->GetResultType() == Primitive::kPrimInt) {
7895 Register first_reg = first.AsRegister<Register>();
7896 Register out_reg = out.AsRegister<Register>();
7897 if (instruction->IsAnd()) {
7898 GenerateAndConst(out_reg, first_reg, value_low);
7899 } else if (instruction->IsOr()) {
7900 GenerateOrrConst(out_reg, first_reg, value_low);
7901 } else {
7902 DCHECK(instruction->IsXor());
7903 GenerateEorConst(out_reg, first_reg, value_low);
7904 }
7905 } else {
7906 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7907 uint32_t value_high = High32Bits(value);
7908 Register first_low = first.AsRegisterPairLow<Register>();
7909 Register first_high = first.AsRegisterPairHigh<Register>();
7910 Register out_low = out.AsRegisterPairLow<Register>();
7911 Register out_high = out.AsRegisterPairHigh<Register>();
7912 if (instruction->IsAnd()) {
7913 GenerateAndConst(out_low, first_low, value_low);
7914 GenerateAndConst(out_high, first_high, value_high);
7915 } else if (instruction->IsOr()) {
7916 GenerateOrrConst(out_low, first_low, value_low);
7917 GenerateOrrConst(out_high, first_high, value_high);
7918 } else {
7919 DCHECK(instruction->IsXor());
7920 GenerateEorConst(out_low, first_low, value_low);
7921 GenerateEorConst(out_high, first_high, value_high);
7922 }
7923 }
7924 return;
7925 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007926
7927 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007928 Register first_reg = first.AsRegister<Register>();
7929 ShifterOperand second_reg(second.AsRegister<Register>());
7930 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007931 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007932 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007933 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007934 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007935 } else {
7936 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007937 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007938 }
7939 } else {
7940 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007941 Register first_low = first.AsRegisterPairLow<Register>();
7942 Register first_high = first.AsRegisterPairHigh<Register>();
7943 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7944 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7945 Register out_low = out.AsRegisterPairLow<Register>();
7946 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007947 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007948 __ and_(out_low, first_low, second_low);
7949 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007950 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007951 __ orr(out_low, first_low, second_low);
7952 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007953 } else {
7954 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007955 __ eor(out_low, first_low, second_low);
7956 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007957 }
7958 }
7959}
7960
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007961void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
7962 HInstruction* instruction,
7963 Location out,
7964 uint32_t offset,
7965 Location maybe_temp,
7966 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007967 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007968 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007969 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007970 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007971 if (kUseBakerReadBarrier) {
7972 // Load with fast path based Baker's read barrier.
7973 // /* HeapReference<Object> */ out = *(out + offset)
7974 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007975 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007976 } else {
7977 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007978 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00007979 // in the following move operation, as we will need it for the
7980 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007981 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00007982 // /* HeapReference<Object> */ out = *(out + offset)
7983 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007984 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00007985 }
7986 } else {
7987 // Plain load with no read barrier.
7988 // /* HeapReference<Object> */ out = *(out + offset)
7989 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7990 __ MaybeUnpoisonHeapReference(out_reg);
7991 }
7992}
7993
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007994void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
7995 HInstruction* instruction,
7996 Location out,
7997 Location obj,
7998 uint32_t offset,
7999 Location maybe_temp,
8000 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008001 Register out_reg = out.AsRegister<Register>();
8002 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008003 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08008004 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00008005 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008006 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00008007 // Load with fast path based Baker's read barrier.
8008 // /* HeapReference<Object> */ out = *(obj + offset)
8009 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008010 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00008011 } else {
8012 // Load with slow path based read barrier.
8013 // /* HeapReference<Object> */ out = *(obj + offset)
8014 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8015 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8016 }
8017 } else {
8018 // Plain load with no read barrier.
8019 // /* HeapReference<Object> */ out = *(obj + offset)
8020 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8021 __ MaybeUnpoisonHeapReference(out_reg);
8022 }
8023}
8024
8025void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
8026 Location root,
8027 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008028 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008029 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008030 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008031 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008032 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00008033 if (kUseBakerReadBarrier) {
8034 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008035 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008036 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8037 !Runtime::Current()->UseJitCompilation()) {
8038 // Note that we do not actually check the value of `GetIsGcMarking()`
8039 // to decide whether to mark the loaded GC root or not. Instead, we
8040 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8041 // barrier mark introspection entrypoint. If `temp` is null, it means
8042 // that `GetIsGcMarking()` is false, and vice versa.
8043 //
8044 // We use link-time generated thunks for the slow path. That thunk
8045 // checks the reference and jumps to the entrypoint if needed.
8046 //
8047 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8048 // lr = &return_address;
8049 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8050 // if (temp != nullptr) {
8051 // goto gc_root_thunk<root_reg>(lr)
8052 // }
8053 // return_address:
Roland Levillainc9285912015-12-18 10:38:42 +00008054
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008055 CheckLastTempIsBakerCcEntrypointRegister(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008056 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008057 uint32_t custom_data =
Vladimir Marko88abba22017-05-03 17:09:25 +01008058 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg, narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008059 Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008060
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008061 // entrypoint_reg =
8062 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8063 DCHECK_EQ(IP, 12);
8064 const int32_t entry_point_offset =
8065 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8066 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008067
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008068 Label return_address;
8069 __ AdrCode(LR, &return_address);
8070 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008071 // Currently the offset is always within range. If that changes,
8072 // we shall have to split the load the same way as for fields.
8073 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008074 DCHECK(!down_cast<Thumb2Assembler*>(GetAssembler())->IsForced32Bit());
8075 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()), !narrow);
8076 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008077 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8078 EmitPlaceholderBne(codegen_, bne_label);
8079 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008080 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8081 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8082 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008083 } else {
8084 // Note that we do not actually check the value of
8085 // `GetIsGcMarking()` to decide whether to mark the loaded GC
8086 // root or not. Instead, we load into `temp` the read barrier
8087 // mark entry point corresponding to register `root`. If `temp`
8088 // is null, it means that `GetIsGcMarking()` is false, and vice
8089 // versa.
8090 //
8091 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8092 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8093 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8094 // // Slow path.
8095 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
8096 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008097
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008098 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
8099 Location temp = Location::RegisterLocation(LR);
8100 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
8101 instruction, root, /* entrypoint */ temp);
8102 codegen_->AddSlowPath(slow_path);
8103
8104 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8105 const int32_t entry_point_offset =
8106 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
8107 // Loading the entrypoint does not require a load acquire since it is only changed when
8108 // threads are suspended or running a checkpoint.
8109 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
8110
8111 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8112 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8113 static_assert(
8114 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8115 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8116 "have different sizes.");
8117 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8118 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8119 "have different sizes.");
8120
8121 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8122 // checking GetIsGcMarking.
8123 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
8124 __ Bind(slow_path->GetExitLabel());
8125 }
Roland Levillainc9285912015-12-18 10:38:42 +00008126 } else {
8127 // GC root loaded through a slow path for read barriers other
8128 // than Baker's.
8129 // /* GcRoot<mirror::Object>* */ root = obj + offset
8130 __ AddConstant(root_reg, obj, offset);
8131 // /* mirror::Object* */ root = root->Read()
8132 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8133 }
8134 } else {
8135 // Plain GC root load with no read barrier.
8136 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8137 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8138 // Note that GC roots are not affected by heap poisoning, thus we
8139 // do not have to unpoison `root_reg` here.
8140 }
8141}
8142
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008143void CodeGeneratorARM::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8144 DCHECK(kEmitCompilerReadBarrier);
8145 DCHECK(kUseBakerReadBarrier);
8146 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8147 if (!Runtime::Current()->UseJitCompilation()) {
8148 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
8149 }
8150 }
8151}
8152
Roland Levillainc9285912015-12-18 10:38:42 +00008153void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8154 Location ref,
8155 Register obj,
8156 uint32_t offset,
8157 Location temp,
8158 bool needs_null_check) {
8159 DCHECK(kEmitCompilerReadBarrier);
8160 DCHECK(kUseBakerReadBarrier);
8161
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008162 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8163 !Runtime::Current()->UseJitCompilation()) {
8164 // Note that we do not actually check the value of `GetIsGcMarking()`
8165 // to decide whether to mark the loaded reference or not. Instead, we
8166 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8167 // barrier mark introspection entrypoint. If `temp` is null, it means
8168 // that `GetIsGcMarking()` is false, and vice versa.
8169 //
8170 // We use link-time generated thunks for the slow path. That thunk checks
8171 // the holder and jumps to the entrypoint if needed. If the holder is not
8172 // gray, it creates a fake dependency and returns to the LDR instruction.
8173 //
8174 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8175 // lr = &gray_return_address;
8176 // if (temp != nullptr) {
8177 // goto field_thunk<holder_reg, base_reg>(lr)
8178 // }
8179 // not_gray_return_address:
8180 // // Original reference load. If the offset is too large to fit
8181 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008182 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008183 // gray_return_address:
8184
8185 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko88abba22017-05-03 17:09:25 +01008186 Register ref_reg = ref.AsRegister<Register>();
8187 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008188 Register base = obj;
8189 if (offset >= kReferenceLoadMinFarOffset) {
8190 base = temp.AsRegister<Register>();
8191 DCHECK_NE(base, kBakerCcEntrypointRegister);
8192 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8193 __ AddConstant(base, obj, offset & ~(kReferenceLoadMinFarOffset - 1u));
8194 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008195 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8196 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8197 // increase the overall code size when taking the generated thunks into account.
8198 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008199 }
8200 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8201 uint32_t custom_data =
Vladimir Marko88abba22017-05-03 17:09:25 +01008202 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(base, obj, narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008203 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8204
8205 // entrypoint_reg =
8206 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8207 DCHECK_EQ(IP, 12);
8208 const int32_t entry_point_offset =
8209 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8210 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8211
8212 Label return_address;
8213 __ AdrCode(LR, &return_address);
8214 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008215 EmitPlaceholderBne(this, bne_label);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008216 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008217 DCHECK(!down_cast<Thumb2Assembler*>(GetAssembler())->IsForced32Bit());
8218 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()), !narrow);
8219 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008220 __ LoadFromOffset(kLoadWord, ref_reg, base, offset);
8221 if (needs_null_check) {
8222 MaybeRecordImplicitNullCheck(instruction);
8223 }
8224 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8225 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008226 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8227 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8228 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008229 return;
8230 }
8231
Roland Levillainc9285912015-12-18 10:38:42 +00008232 // /* HeapReference<Object> */ ref = *(obj + offset)
8233 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01008234 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00008235 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008236 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008237}
8238
8239void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8240 Location ref,
8241 Register obj,
8242 uint32_t data_offset,
8243 Location index,
8244 Location temp,
8245 bool needs_null_check) {
8246 DCHECK(kEmitCompilerReadBarrier);
8247 DCHECK(kUseBakerReadBarrier);
8248
Roland Levillainbfea3352016-06-23 13:48:47 +01008249 static_assert(
8250 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8251 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008252 ScaleFactor scale_factor = TIMES_4;
8253
8254 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8255 !Runtime::Current()->UseJitCompilation()) {
8256 // Note that we do not actually check the value of `GetIsGcMarking()`
8257 // to decide whether to mark the loaded reference or not. Instead, we
8258 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8259 // barrier mark introspection entrypoint. If `temp` is null, it means
8260 // that `GetIsGcMarking()` is false, and vice versa.
8261 //
8262 // We use link-time generated thunks for the slow path. That thunk checks
8263 // the holder and jumps to the entrypoint if needed. If the holder is not
8264 // gray, it creates a fake dependency and returns to the LDR instruction.
8265 //
8266 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8267 // lr = &gray_return_address;
8268 // if (temp != nullptr) {
8269 // goto field_thunk<holder_reg, base_reg>(lr)
8270 // }
8271 // not_gray_return_address:
8272 // // Original reference load. If the offset is too large to fit
8273 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008274 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008275 // gray_return_address:
8276
8277 DCHECK(index.IsValid());
8278 Register index_reg = index.AsRegister<Register>();
8279 Register ref_reg = ref.AsRegister<Register>();
8280 Register data_reg = temp.AsRegister<Register>();
8281 DCHECK_NE(data_reg, kBakerCcEntrypointRegister);
8282
8283 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8284 uint32_t custom_data =
8285 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg);
8286 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8287
8288 // entrypoint_reg =
8289 // Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
8290 DCHECK_EQ(IP, 12);
8291 const int32_t entry_point_offset =
8292 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8293 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8294 __ AddConstant(data_reg, obj, data_offset);
8295
8296 Label return_address;
8297 __ AdrCode(LR, &return_address);
8298 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008299 EmitPlaceholderBne(this, bne_label);
Vladimir Marko88abba22017-05-03 17:09:25 +01008300 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()));
8301 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008302 __ ldr(ref_reg, Address(data_reg, index_reg, LSL, scale_factor));
8303 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8304 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8305 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008306 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8307 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008308 return;
8309 }
8310
Roland Levillainc9285912015-12-18 10:38:42 +00008311 // /* HeapReference<Object> */ ref =
8312 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
8313 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008314 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008315}
8316
8317void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8318 Location ref,
8319 Register obj,
8320 uint32_t offset,
8321 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01008322 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00008323 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008324 bool needs_null_check) {
Roland Levillainc9285912015-12-18 10:38:42 +00008325 DCHECK(kEmitCompilerReadBarrier);
8326 DCHECK(kUseBakerReadBarrier);
8327
Roland Levillain54f869e2017-03-06 13:54:11 +00008328 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8329 // whether we need to enter the slow path to mark the reference.
8330 // Then, in the slow path, check the gray bit in the lock word of
8331 // the reference's holder (`obj`) to decide whether to mark `ref` or
8332 // not.
Roland Levillainc9285912015-12-18 10:38:42 +00008333 //
Roland Levillainba650a42017-03-06 13:52:32 +00008334 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainff487002017-03-07 16:50:01 +00008335 // instead, we load into `temp2` the read barrier mark entry point
8336 // corresponding to register `ref`. If `temp2` is null, it means
8337 // that `GetIsGcMarking()` is false, and vice versa.
8338 //
8339 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8340 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8341 // // Slow path.
8342 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8343 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8344 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8345 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8346 // if (is_gray) {
8347 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8348 // }
8349 // } else {
8350 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8351 // }
8352
8353 Register temp_reg = temp.AsRegister<Register>();
8354
8355 // Slow path marking the object `ref` when the GC is marking. The
8356 // entrypoint will already be loaded in `temp2`.
8357 Location temp2 = Location::RegisterLocation(LR);
8358 SlowPathCodeARM* slow_path =
8359 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM(
8360 instruction,
8361 ref,
8362 obj,
8363 offset,
8364 index,
8365 scale_factor,
8366 needs_null_check,
8367 temp_reg,
8368 /* entrypoint */ temp2);
8369 AddSlowPath(slow_path);
8370
8371 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8372 const int32_t entry_point_offset =
8373 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8374 // Loading the entrypoint does not require a load acquire since it is only changed when
8375 // threads are suspended or running a checkpoint.
8376 __ LoadFromOffset(kLoadWord, temp2.AsRegister<Register>(), TR, entry_point_offset);
8377 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8378 // checking GetIsGcMarking.
8379 __ CompareAndBranchIfNonZero(temp2.AsRegister<Register>(), slow_path->GetEntryLabel());
8380 // Fast path: the GC is not marking: just load the reference.
8381 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8382 __ Bind(slow_path->GetExitLabel());
8383}
8384
8385void CodeGeneratorARM::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8386 Location ref,
8387 Register obj,
8388 Location field_offset,
8389 Location temp,
8390 bool needs_null_check,
8391 Register temp2) {
8392 DCHECK(kEmitCompilerReadBarrier);
8393 DCHECK(kUseBakerReadBarrier);
8394
8395 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8396 // whether we need to enter the slow path to update the reference
8397 // field within `obj`. Then, in the slow path, check the gray bit
8398 // in the lock word of the reference's holder (`obj`) to decide
8399 // whether to mark `ref` and update the field or not.
8400 //
8401 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainba650a42017-03-06 13:52:32 +00008402 // instead, we load into `temp3` the read barrier mark entry point
8403 // corresponding to register `ref`. If `temp3` is null, it means
8404 // that `GetIsGcMarking()` is false, and vice versa.
8405 //
8406 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008407 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8408 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008409 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8410 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8411 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8412 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8413 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008414 // old_ref = ref;
Roland Levillain54f869e2017-03-06 13:54:11 +00008415 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008416 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008417 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008418 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008419
Roland Levillain35345a52017-02-27 14:32:08 +00008420 Register temp_reg = temp.AsRegister<Register>();
Roland Levillain1372c9f2017-01-13 11:47:39 +00008421
Roland Levillainff487002017-03-07 16:50:01 +00008422 // Slow path updating the object reference at address `obj +
8423 // field_offset` when the GC is marking. The entrypoint will already
8424 // be loaded in `temp3`.
Roland Levillainba650a42017-03-06 13:52:32 +00008425 Location temp3 = Location::RegisterLocation(LR);
Roland Levillainff487002017-03-07 16:50:01 +00008426 SlowPathCodeARM* slow_path =
8427 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(
8428 instruction,
8429 ref,
8430 obj,
8431 /* offset */ 0u,
8432 /* index */ field_offset,
8433 /* scale_factor */ ScaleFactor::TIMES_1,
8434 needs_null_check,
8435 temp_reg,
8436 temp2,
8437 /* entrypoint */ temp3);
Roland Levillainba650a42017-03-06 13:52:32 +00008438 AddSlowPath(slow_path);
Roland Levillain35345a52017-02-27 14:32:08 +00008439
Roland Levillainba650a42017-03-06 13:52:32 +00008440 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8441 const int32_t entry_point_offset =
8442 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8443 // Loading the entrypoint does not require a load acquire since it is only changed when
8444 // threads are suspended or running a checkpoint.
8445 __ LoadFromOffset(kLoadWord, temp3.AsRegister<Register>(), TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008446 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8447 // checking GetIsGcMarking.
8448 __ CompareAndBranchIfNonZero(temp3.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008449 // Fast path: the GC is not marking: nothing to do (the field is
8450 // up-to-date, and we don't need to load the reference).
Roland Levillainba650a42017-03-06 13:52:32 +00008451 __ Bind(slow_path->GetExitLabel());
8452}
Roland Levillain35345a52017-02-27 14:32:08 +00008453
Roland Levillainba650a42017-03-06 13:52:32 +00008454void CodeGeneratorARM::GenerateRawReferenceLoad(HInstruction* instruction,
8455 Location ref,
8456 Register obj,
8457 uint32_t offset,
8458 Location index,
8459 ScaleFactor scale_factor,
8460 bool needs_null_check) {
8461 Register ref_reg = ref.AsRegister<Register>();
8462
Roland Levillainc9285912015-12-18 10:38:42 +00008463 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008464 // Load types involving an "index": ArrayGet,
8465 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8466 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008467 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00008468 if (index.IsConstant()) {
8469 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01008470 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00008471 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
8472 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01008473 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008474 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8475 // intrinsics, which use a register pair as index ("long
8476 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01008477 Register index_reg = index.IsRegisterPair()
8478 ? index.AsRegisterPairLow<Register>()
8479 : index.AsRegister<Register>();
8480 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00008481 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
8482 }
8483 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008484 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillainc9285912015-12-18 10:38:42 +00008485 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
8486 }
8487
Roland Levillainba650a42017-03-06 13:52:32 +00008488 if (needs_null_check) {
8489 MaybeRecordImplicitNullCheck(instruction);
8490 }
8491
Roland Levillainc9285912015-12-18 10:38:42 +00008492 // Object* ref = ref_addr->AsMirrorPtr()
8493 __ MaybeUnpoisonHeapReference(ref_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00008494}
8495
8496void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
8497 Location out,
8498 Location ref,
8499 Location obj,
8500 uint32_t offset,
8501 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008502 DCHECK(kEmitCompilerReadBarrier);
8503
Roland Levillainc9285912015-12-18 10:38:42 +00008504 // Insert a slow path based read barrier *after* the reference load.
8505 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008506 // If heap poisoning is enabled, the unpoisoning of the loaded
8507 // reference will be carried out by the runtime within the slow
8508 // path.
8509 //
8510 // Note that `ref` currently does not get unpoisoned (when heap
8511 // poisoning is enabled), which is alright as the `ref` argument is
8512 // not used by the artReadBarrierSlow entry point.
8513 //
8514 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008515 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00008516 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
8517 AddSlowPath(slow_path);
8518
Roland Levillain3b359c72015-11-17 19:35:12 +00008519 __ b(slow_path->GetEntryLabel());
8520 __ Bind(slow_path->GetExitLabel());
8521}
8522
Roland Levillainc9285912015-12-18 10:38:42 +00008523void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8524 Location out,
8525 Location ref,
8526 Location obj,
8527 uint32_t offset,
8528 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008529 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00008530 // Baker's read barriers shall be handled by the fast path
8531 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
8532 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00008533 // If heap poisoning is enabled, unpoisoning will be taken care of
8534 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00008535 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00008536 } else if (kPoisonHeapReferences) {
8537 __ UnpoisonHeapReference(out.AsRegister<Register>());
8538 }
8539}
8540
Roland Levillainc9285912015-12-18 10:38:42 +00008541void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8542 Location out,
8543 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008544 DCHECK(kEmitCompilerReadBarrier);
8545
Roland Levillainc9285912015-12-18 10:38:42 +00008546 // Insert a slow path based read barrier *after* the GC root load.
8547 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008548 // Note that GC roots are not affected by heap poisoning, so we do
8549 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008550 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00008551 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
8552 AddSlowPath(slow_path);
8553
Roland Levillain3b359c72015-11-17 19:35:12 +00008554 __ b(slow_path->GetEntryLabel());
8555 __ Bind(slow_path->GetExitLabel());
8556}
8557
Vladimir Markodc151b22015-10-15 18:02:30 +01008558HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
8559 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008560 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008561 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01008562}
8563
Vladimir Markob4536b72015-11-24 13:45:23 +00008564Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
8565 Register temp) {
8566 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8567 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8568 if (!invoke->GetLocations()->Intrinsified()) {
8569 return location.AsRegister<Register>();
8570 }
8571 // For intrinsics we allow any location, so it may be on the stack.
8572 if (!location.IsRegister()) {
8573 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
8574 return temp;
8575 }
8576 // For register locations, check if the register was saved. If so, get it from the stack.
8577 // Note: There is a chance that the register was saved but not overwritten, so we could
8578 // save one load. However, since this is just an intrinsic slow path we prefer this
8579 // simple and more robust approach rather that trying to determine if that's the case.
8580 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08008581 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00008582 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
8583 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
8584 return temp;
8585 }
8586 return location.AsRegister<Register>();
8587}
8588
TatWai Chongd8c052a2016-11-02 16:12:48 +08008589Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
8590 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00008591 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
8592 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008593 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8594 uint32_t offset =
8595 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00008596 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008597 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00008598 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008599 }
Vladimir Marko58155012015-08-19 12:49:41 +00008600 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00008601 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008602 break;
8603 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8604 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
8605 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00008606 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
8607 HArmDexCacheArraysBase* base =
8608 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
8609 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
8610 temp.AsRegister<Register>());
8611 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
8612 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
8613 break;
8614 }
Vladimir Marko58155012015-08-19 12:49:41 +00008615 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00008616 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008617 Register method_reg;
8618 Register reg = temp.AsRegister<Register>();
8619 if (current_method.IsRegister()) {
8620 method_reg = current_method.AsRegister<Register>();
8621 } else {
8622 DCHECK(invoke->GetLocations()->Intrinsified());
8623 DCHECK(!current_method.IsValid());
8624 method_reg = reg;
8625 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
8626 }
Roland Levillain3b359c72015-11-17 19:35:12 +00008627 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
8628 __ LoadFromOffset(kLoadWord,
8629 reg,
8630 method_reg,
8631 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01008632 // temp = temp[index_in_cache];
8633 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
8634 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00008635 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
8636 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01008637 }
Vladimir Marko58155012015-08-19 12:49:41 +00008638 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08008639 return callee_method;
8640}
8641
8642void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
8643 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00008644
8645 switch (invoke->GetCodePtrLocation()) {
8646 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
8647 __ bl(GetFrameEntryLabel());
8648 break;
Vladimir Marko58155012015-08-19 12:49:41 +00008649 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8650 // LR = callee_method->entry_point_from_quick_compiled_code_
8651 __ LoadFromOffset(
8652 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07008653 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00008654 // LR()
8655 __ blx(LR);
8656 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08008657 }
8658
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08008659 DCHECK(!IsLeafMethod());
8660}
8661
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008662void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
8663 Register temp = temp_location.AsRegister<Register>();
8664 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8665 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00008666
8667 // Use the calling convention instead of the location of the receiver, as
8668 // intrinsics may have put the receiver in a different register. In the intrinsics
8669 // slow path, the arguments have been moved to the right place, so here we are
8670 // guaranteed that the receiver is the first register of the calling convention.
8671 InvokeDexCallingConvention calling_convention;
8672 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008673 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00008674 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00008675 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008676 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00008677 // Instead of simply (possibly) unpoisoning `temp` here, we should
8678 // emit a read barrier for the previous class reference load.
8679 // However this is not required in practice, as this is an
8680 // intermediate/temporary reference and because the current
8681 // concurrent copying collector keeps the from-space memory
8682 // intact/accessible until the end of the marking phase (the
8683 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008684 __ MaybeUnpoisonHeapReference(temp);
8685 // temp = temp->GetMethodAt(method_offset);
8686 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07008687 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008688 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
8689 // LR = temp->GetEntryPoint();
8690 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
8691 // LR();
8692 __ blx(LR);
8693}
8694
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008695CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008696 const DexFile& dex_file, dex::StringIndex string_index) {
8697 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008698}
8699
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008700CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08008701 const DexFile& dex_file, dex::TypeIndex type_index) {
8702 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008703}
8704
Vladimir Marko1998cd02017-01-13 13:02:58 +00008705CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
8706 const DexFile& dex_file, dex::TypeIndex type_index) {
8707 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8708}
8709
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008710CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
8711 const DexFile& dex_file, uint32_t element_offset) {
8712 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
8713}
8714
8715CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
8716 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8717 patches->emplace_back(dex_file, offset_or_index);
8718 return &patches->back();
8719}
8720
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008721Label* CodeGeneratorARM::NewBakerReadBarrierPatch(uint32_t custom_data) {
8722 baker_read_barrier_patches_.emplace_back(custom_data);
8723 return &baker_read_barrier_patches_.back().label;
8724}
8725
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008726Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008727 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008728 return boot_image_string_patches_.GetOrCreate(
8729 StringReference(&dex_file, string_index),
8730 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8731}
8732
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008733Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08008734 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008735 return boot_image_type_patches_.GetOrCreate(
8736 TypeReference(&dex_file, type_index),
8737 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8738}
8739
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008740Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00008741 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008742}
8743
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008744Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008745 dex::StringIndex string_index,
8746 Handle<mirror::String> handle) {
8747 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
8748 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008749 return jit_string_patches_.GetOrCreate(
8750 StringReference(&dex_file, string_index),
8751 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8752}
8753
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008754Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
8755 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008756 Handle<mirror::Class> handle) {
8757 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
8758 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008759 return jit_class_patches_.GetOrCreate(
8760 TypeReference(&dex_file, type_index),
8761 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8762}
8763
Vladimir Markoaad75c62016-10-03 08:46:48 +00008764template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
8765inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
8766 const ArenaDeque<PcRelativePatchInfo>& infos,
8767 ArenaVector<LinkerPatch>* linker_patches) {
8768 for (const PcRelativePatchInfo& info : infos) {
8769 const DexFile& dex_file = info.target_dex_file;
8770 size_t offset_or_index = info.offset_or_index;
8771 DCHECK(info.add_pc_label.IsBound());
8772 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
8773 // Add MOVW patch.
8774 DCHECK(info.movw_label.IsBound());
8775 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
8776 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
8777 // Add MOVT patch.
8778 DCHECK(info.movt_label.IsBound());
8779 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
8780 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
8781 }
8782}
8783
Vladimir Marko58155012015-08-19 12:49:41 +00008784void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
8785 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00008786 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00008787 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008788 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008789 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008790 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008791 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008792 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
8793 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00008794 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008795 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
8796 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008797 for (const auto& entry : boot_image_string_patches_) {
8798 const StringReference& target_string = entry.first;
8799 Literal* literal = entry.second;
8800 DCHECK(literal->GetLabel()->IsBound());
8801 uint32_t literal_offset = literal->GetLabel()->Position();
8802 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
8803 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008804 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008805 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00008806 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00008807 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00008808 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
8809 linker_patches);
8810 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008811 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
8812 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008813 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
8814 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008815 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008816 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
8817 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008818 for (const auto& entry : boot_image_type_patches_) {
8819 const TypeReference& target_type = entry.first;
8820 Literal* literal = entry.second;
8821 DCHECK(literal->GetLabel()->IsBound());
8822 uint32_t literal_offset = literal->GetLabel()->Position();
8823 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
8824 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08008825 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008826 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008827 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
8828 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.Position(),
8829 info.custom_data));
8830 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008831 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008832}
8833
8834Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
8835 return map->GetOrCreate(
8836 value,
8837 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00008838}
8839
8840Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
8841 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008842 return map->GetOrCreate(
8843 target_method,
8844 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00008845}
8846
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03008847void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8848 LocationSummary* locations =
8849 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
8850 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
8851 Location::RequiresRegister());
8852 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
8853 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
8854 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8855}
8856
8857void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8858 LocationSummary* locations = instr->GetLocations();
8859 Register res = locations->Out().AsRegister<Register>();
8860 Register accumulator =
8861 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
8862 Register mul_left =
8863 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
8864 Register mul_right =
8865 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
8866
8867 if (instr->GetOpKind() == HInstruction::kAdd) {
8868 __ mla(res, mul_left, mul_right, accumulator);
8869 } else {
8870 __ mls(res, mul_left, mul_right, accumulator);
8871 }
8872}
8873
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008874void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008875 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008876 LOG(FATAL) << "Unreachable";
8877}
8878
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008879void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008880 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008881 LOG(FATAL) << "Unreachable";
8882}
8883
Mark Mendellfe57faa2015-09-18 09:26:15 -04008884// Simple implementation of packed switch - generate cascaded compare/jumps.
8885void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8886 LocationSummary* locations =
8887 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8888 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008889 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008890 codegen_->GetAssembler()->IsThumb()) {
8891 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
8892 if (switch_instr->GetStartValue() != 0) {
8893 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
8894 }
8895 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008896}
8897
8898void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8899 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008900 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04008901 LocationSummary* locations = switch_instr->GetLocations();
8902 Register value_reg = locations->InAt(0).AsRegister<Register>();
8903 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8904
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008905 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008906 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008907 Register temp_reg = IP;
8908 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
8909 // the immediate, because IP is used as the destination register. For the other
8910 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
8911 // and they can be encoded in the instruction without making use of IP register.
8912 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
8913
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008914 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008915 // Jump to successors[0] if value == lower_bound.
8916 __ b(codegen_->GetLabelOf(successors[0]), EQ);
8917 int32_t last_index = 0;
8918 for (; num_entries - last_index > 2; last_index += 2) {
8919 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
8920 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8921 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
8922 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8923 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
8924 }
8925 if (num_entries - last_index == 2) {
8926 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00008927 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008928 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008929 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008930
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008931 // And the default for any other value.
8932 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
8933 __ b(codegen_->GetLabelOf(default_block));
8934 }
8935 } else {
8936 // Create a table lookup.
8937 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8938
8939 // Materialize a pointer to the switch table
8940 std::vector<Label*> labels(num_entries);
8941 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
8942 for (uint32_t i = 0; i < num_entries; i++) {
8943 labels[i] = codegen_->GetLabelOf(successors[i]);
8944 }
8945 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
8946
8947 // Remove the bias.
8948 Register key_reg;
8949 if (lower_bound != 0) {
8950 key_reg = locations->GetTemp(1).AsRegister<Register>();
8951 __ AddConstant(key_reg, value_reg, -lower_bound);
8952 } else {
8953 key_reg = value_reg;
8954 }
8955
8956 // Check whether the value is in the table, jump to default block if not.
8957 __ CmpConstant(key_reg, num_entries - 1);
8958 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
8959
8960 // Load the displacement from the table.
8961 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
8962
8963 // Dispatch is a direct add to the PC (for Thumb2).
8964 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008965 }
8966}
8967
Vladimir Markob4536b72015-11-24 13:45:23 +00008968void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8969 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8970 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00008971}
8972
8973void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8974 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008975 CodeGeneratorARM::PcRelativePatchInfo* labels =
8976 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00008977 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008978 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008979 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008980 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008981 __ BindTrackedLabel(&labels->add_pc_label);
8982 __ add(base_reg, base_reg, ShifterOperand(PC));
8983}
8984
Andreas Gampe85b62f22015-09-09 13:15:38 -07008985void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
8986 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00008987 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008988 return;
8989 }
8990
8991 DCHECK_NE(type, Primitive::kPrimVoid);
8992
8993 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
8994 if (return_loc.Equals(trg)) {
8995 return;
8996 }
8997
8998 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8999 // with the last branch.
9000 if (type == Primitive::kPrimLong) {
9001 HParallelMove parallel_move(GetGraph()->GetArena());
9002 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
9003 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
9004 GetMoveResolver()->EmitNativeCode(&parallel_move);
9005 } else if (type == Primitive::kPrimDouble) {
9006 HParallelMove parallel_move(GetGraph()->GetArena());
9007 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
9008 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
9009 GetMoveResolver()->EmitNativeCode(&parallel_move);
9010 } else {
9011 // Let the parallel move resolver take care of all of this.
9012 HParallelMove parallel_move(GetGraph()->GetArena());
9013 parallel_move.AddMove(return_loc, trg, type, nullptr);
9014 GetMoveResolver()->EmitNativeCode(&parallel_move);
9015 }
9016}
9017
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009018void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
9019 LocationSummary* locations =
9020 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9021 locations->SetInAt(0, Location::RequiresRegister());
9022 locations->SetOut(Location::RequiresRegister());
9023}
9024
9025void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
9026 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009027 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009028 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009029 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009030 __ LoadFromOffset(kLoadWord,
9031 locations->Out().AsRegister<Register>(),
9032 locations->InAt(0).AsRegister<Register>(),
9033 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009034 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009035 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009036 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009037 __ LoadFromOffset(kLoadWord,
9038 locations->Out().AsRegister<Register>(),
9039 locations->InAt(0).AsRegister<Register>(),
9040 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9041 __ LoadFromOffset(kLoadWord,
9042 locations->Out().AsRegister<Register>(),
9043 locations->Out().AsRegister<Register>(),
9044 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009045 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009046}
9047
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009048static void PatchJitRootUse(uint8_t* code,
9049 const uint8_t* roots_data,
9050 Literal* literal,
9051 uint64_t index_in_table) {
9052 DCHECK(literal->GetLabel()->IsBound());
9053 uint32_t literal_offset = literal->GetLabel()->Position();
9054 uintptr_t address =
9055 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9056 uint8_t* data = code + literal_offset;
9057 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9058}
9059
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009060void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9061 for (const auto& entry : jit_string_patches_) {
9062 const auto& it = jit_string_roots_.find(entry.first);
9063 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009064 PatchJitRootUse(code, roots_data, entry.second, it->second);
9065 }
9066 for (const auto& entry : jit_class_patches_) {
9067 const auto& it = jit_class_roots_.find(entry.first);
9068 DCHECK(it != jit_class_roots_.end());
9069 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009070 }
9071}
9072
Roland Levillain4d027112015-07-01 15:41:14 +01009073#undef __
9074#undef QUICK_ENTRY_POINT
9075
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00009076} // namespace arm
9077} // namespace art