blob: c66bd77d6b2f3b199e43d00787984a6a4c4ab990 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_arm.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000018
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010019#include "arch/arm/asm_support_arm.h"
Calin Juravle34166012014-12-19 17:22:29 +000020#include "arch/arm/instruction_set_features_arm.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Zheng Xuc6667102015-05-15 16:08:45 +080022#include "code_generator_utils.h"
Anton Kirilov74234da2017-01-13 14:42:47 +000023#include "common_arm.h"
Vladimir Marko58155012015-08-19 12:49:41 +000024#include "compiled_method.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070025#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010026#include "gc/accounting/card_table.h"
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -080027#include "intrinsics.h"
28#include "intrinsics_arm.h"
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010029#include "linker/arm/relative_patcher_thumb2.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070030#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "mirror/class-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070032#include "thread.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010033#include "utils/arm/assembler_arm.h"
34#include "utils/arm/managed_register_arm.h"
Roland Levillain946e1432014-11-11 17:35:19 +000035#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010036#include "utils/stack_checks.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000037
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010039
Roland Levillain3b359c72015-11-17 19:35:12 +000040template<class MirrorType>
41class GcRoot;
42
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000043namespace arm {
44
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +000045static bool ExpectedPairLayout(Location location) {
46 // We expected this for both core and fpu register pairs.
47 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high());
48}
49
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010050static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010051static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010052
David Brazdil58282f42016-01-14 12:45:10 +000053static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000054static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070055 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000056static constexpr SRegister kFpuCalleeSaves[] =
57 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010058
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000059// D31 cannot be split into two S registers, and the register allocator only works on
60// S registers. Therefore there is no need to block it.
61static constexpr DRegister DTMP = D31;
62
Vladimir Markof3e0ee22015-12-17 15:23:13 +000063static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070064
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010065// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
66// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
67// For the Baker read barrier implementation using link-generated thunks we need to split
68// the offset explicitly.
69constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
70
71// Flags controlling the use of link-time generated thunks for Baker read barriers.
72constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
73constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
74constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
75
76// The reserved entrypoint register for link-time generated thunks.
77const Register kBakerCcEntrypointRegister = R4;
78
Roland Levillain7cbd27f2016-08-11 23:53:33 +010079// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
80#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070081#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010082
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010083static inline void CheckLastTempIsBakerCcEntrypointRegister(HInstruction* instruction) {
84 DCHECK_EQ(static_cast<uint32_t>(kBakerCcEntrypointRegister),
85 linker::Thumb2RelativePatcher::kBakerCcEntrypointRegister);
86 DCHECK_NE(instruction->GetLocations()->GetTempCount(), 0u);
87 DCHECK_EQ(kBakerCcEntrypointRegister,
88 instruction->GetLocations()->GetTemp(
89 instruction->GetLocations()->GetTempCount() - 1u).AsRegister<Register>());
90}
91
92static inline void EmitPlaceholderBne(CodeGeneratorARM* codegen, Label* bne_label) {
Vladimir Marko88abba22017-05-03 17:09:25 +010093 ScopedForce32Bit force_32bit(down_cast<Thumb2Assembler*>(codegen->GetAssembler()));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010094 __ BindTrackedLabel(bne_label);
95 Label placeholder_label;
96 __ b(&placeholder_label, NE); // Placeholder, patched at link-time.
97 __ Bind(&placeholder_label);
98}
99
Vladimir Marko88abba22017-05-03 17:09:25 +0100100static inline bool CanEmitNarrowLdr(Register rt, Register rn, uint32_t offset) {
101 return ArmAssembler::IsLowRegister(rt) && ArmAssembler::IsLowRegister(rn) && offset < 32u;
102}
103
Artem Serovf4d6aee2016-07-11 10:41:45 +0100104static constexpr int kRegListThreshold = 4;
105
Artem Serovd300d8f2016-07-15 14:00:56 +0100106// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
107// for each live D registers they treat two corresponding S registers as live ones.
108//
109// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
110// from a list of contiguous S registers a list of contiguous D registers (processing first/last
111// S registers corner cases) and save/restore this new list treating them as D registers.
112// - decreasing code size
113// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
114// restored and then used in regular non SlowPath code as D register.
115//
116// For the following example (v means the S register is live):
117// D names: | D0 | D1 | D2 | D4 | ...
118// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
119// Live? | | v | v | v | v | v | v | | ...
120//
121// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
122// as D registers.
123static size_t SaveContiguousSRegisterList(size_t first,
124 size_t last,
125 CodeGenerator* codegen,
126 size_t stack_offset) {
127 DCHECK_LE(first, last);
128 if ((first == last) && (first == 0)) {
129 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first);
130 return stack_offset;
131 }
132 if (first % 2 == 1) {
133 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first++);
134 }
135
136 bool save_last = false;
137 if (last % 2 == 0) {
138 save_last = true;
139 --last;
140 }
141
142 if (first < last) {
143 DRegister d_reg = static_cast<DRegister>(first / 2);
144 DCHECK_EQ((last - first + 1) % 2, 0u);
145 size_t number_of_d_regs = (last - first + 1) / 2;
146
147 if (number_of_d_regs == 1) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100148 __ StoreDToOffset(d_reg, SP, stack_offset);
Artem Serovd300d8f2016-07-15 14:00:56 +0100149 } else if (number_of_d_regs > 1) {
150 __ add(IP, SP, ShifterOperand(stack_offset));
151 __ vstmiad(IP, d_reg, number_of_d_regs);
152 }
153 stack_offset += number_of_d_regs * kArmWordSize * 2;
154 }
155
156 if (save_last) {
157 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, last + 1);
158 }
159
160 return stack_offset;
161}
162
163static size_t RestoreContiguousSRegisterList(size_t first,
164 size_t last,
165 CodeGenerator* codegen,
166 size_t stack_offset) {
167 DCHECK_LE(first, last);
168 if ((first == last) && (first == 0)) {
169 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first);
170 return stack_offset;
171 }
172 if (first % 2 == 1) {
173 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first++);
174 }
175
176 bool restore_last = false;
177 if (last % 2 == 0) {
178 restore_last = true;
179 --last;
180 }
181
182 if (first < last) {
183 DRegister d_reg = static_cast<DRegister>(first / 2);
184 DCHECK_EQ((last - first + 1) % 2, 0u);
185 size_t number_of_d_regs = (last - first + 1) / 2;
186 if (number_of_d_regs == 1) {
187 __ LoadDFromOffset(d_reg, SP, stack_offset);
188 } else if (number_of_d_regs > 1) {
189 __ add(IP, SP, ShifterOperand(stack_offset));
190 __ vldmiad(IP, d_reg, number_of_d_regs);
191 }
192 stack_offset += number_of_d_regs * kArmWordSize * 2;
193 }
194
195 if (restore_last) {
196 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, last + 1);
197 }
198
199 return stack_offset;
200}
201
Artem Serovf4d6aee2016-07-11 10:41:45 +0100202void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
203 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
204 size_t orig_offset = stack_offset;
205
206 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
207 for (uint32_t i : LowToHighBits(core_spills)) {
208 // If the register holds an object, update the stack mask.
209 if (locations->RegisterContainsObject(i)) {
210 locations->SetStackBit(stack_offset / kVRegSize);
211 }
212 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
213 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
214 saved_core_stack_offsets_[i] = stack_offset;
215 stack_offset += kArmWordSize;
216 }
217
218 int reg_num = POPCOUNT(core_spills);
219 if (reg_num != 0) {
220 if (reg_num > kRegListThreshold) {
221 __ StoreList(RegList(core_spills), orig_offset);
222 } else {
223 stack_offset = orig_offset;
224 for (uint32_t i : LowToHighBits(core_spills)) {
225 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
226 }
227 }
228 }
229
Artem Serovd300d8f2016-07-15 14:00:56 +0100230 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
231 orig_offset = stack_offset;
Vladimir Marko804b03f2016-09-14 16:26:36 +0100232 for (uint32_t i : LowToHighBits(fp_spills)) {
Artem Serovf4d6aee2016-07-11 10:41:45 +0100233 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
234 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serovd300d8f2016-07-15 14:00:56 +0100235 stack_offset += kArmWordSize;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100236 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100237
238 stack_offset = orig_offset;
239 while (fp_spills != 0u) {
240 uint32_t begin = CTZ(fp_spills);
241 uint32_t tmp = fp_spills + (1u << begin);
242 fp_spills &= tmp; // Clear the contiguous range of 1s.
243 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
244 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
245 }
246 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100247}
248
249void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
250 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
251 size_t orig_offset = stack_offset;
252
253 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
254 for (uint32_t i : LowToHighBits(core_spills)) {
255 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
256 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
257 stack_offset += kArmWordSize;
258 }
259
260 int reg_num = POPCOUNT(core_spills);
261 if (reg_num != 0) {
262 if (reg_num > kRegListThreshold) {
263 __ LoadList(RegList(core_spills), orig_offset);
264 } else {
265 stack_offset = orig_offset;
266 for (uint32_t i : LowToHighBits(core_spills)) {
267 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
268 }
269 }
270 }
271
Artem Serovd300d8f2016-07-15 14:00:56 +0100272 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
273 while (fp_spills != 0u) {
274 uint32_t begin = CTZ(fp_spills);
275 uint32_t tmp = fp_spills + (1u << begin);
276 fp_spills &= tmp; // Clear the contiguous range of 1s.
277 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
278 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
Artem Serovf4d6aee2016-07-11 10:41:45 +0100279 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100280 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100281}
282
283class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100284 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100285 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100286
Alexandre Rames67555f72014-11-18 10:55:16 +0000287 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100288 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100289 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000290 if (instruction_->CanThrowIntoCatchBlock()) {
291 // Live registers will be restored in the catch block if caught.
292 SaveLiveRegisters(codegen, instruction_->GetLocations());
293 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100294 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
295 instruction_,
296 instruction_->GetDexPc(),
297 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000298 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100299 }
300
Alexandre Rames8158f282015-08-07 10:26:17 +0100301 bool IsFatal() const OVERRIDE { return true; }
302
Alexandre Rames9931f312015-06-19 14:47:01 +0100303 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
304
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100305 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100306 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
307};
308
Artem Serovf4d6aee2016-07-11 10:41:45 +0100309class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000310 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100311 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000312
Alexandre Rames67555f72014-11-18 10:55:16 +0000313 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000314 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
315 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100316 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000317 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000318 }
319
Alexandre Rames8158f282015-08-07 10:26:17 +0100320 bool IsFatal() const OVERRIDE { return true; }
321
Alexandre Rames9931f312015-06-19 14:47:01 +0100322 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
323
Calin Juravled0d48522014-11-04 16:40:20 +0000324 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000325 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
326};
327
Artem Serovf4d6aee2016-07-11 10:41:45 +0100328class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000329 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000330 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100331 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000332
Alexandre Rames67555f72014-11-18 10:55:16 +0000333 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100334 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000335 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100336 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000337 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100338 if (successor_ == nullptr) {
339 __ b(GetReturnLabel());
340 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100341 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100342 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000343 }
344
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100345 Label* GetReturnLabel() {
346 DCHECK(successor_ == nullptr);
347 return &return_label_;
348 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000349
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100350 HBasicBlock* GetSuccessor() const {
351 return successor_;
352 }
353
Alexandre Rames9931f312015-06-19 14:47:01 +0100354 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
355
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000356 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100357 // If not null, the block to branch to after the suspend check.
358 HBasicBlock* const successor_;
359
360 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000361 Label return_label_;
362
363 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
364};
365
Artem Serovf4d6aee2016-07-11 10:41:45 +0100366class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100367 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100368 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100369 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100370
Alexandre Rames67555f72014-11-18 10:55:16 +0000371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100372 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100373 LocationSummary* locations = instruction_->GetLocations();
374
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100375 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000376 if (instruction_->CanThrowIntoCatchBlock()) {
377 // Live registers will be restored in the catch block if caught.
378 SaveLiveRegisters(codegen, instruction_->GetLocations());
379 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000380 // We're moving two locations to locations that could overlap, so we need a parallel
381 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100382 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000383 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100384 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000385 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100386 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100387 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100388 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
389 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100390 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
391 ? kQuickThrowStringBounds
392 : kQuickThrowArrayBounds;
393 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100394 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000395 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100396 }
397
Alexandre Rames8158f282015-08-07 10:26:17 +0100398 bool IsFatal() const OVERRIDE { return true; }
399
Alexandre Rames9931f312015-06-19 14:47:01 +0100400 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
401
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100402 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100403 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
404};
405
Artem Serovf4d6aee2016-07-11 10:41:45 +0100406class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100407 public:
Vladimir Markoea4c1262017-02-06 19:59:33 +0000408 LoadClassSlowPathARM(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000409 : SlowPathCodeARM(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000410 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
411 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100412
Alexandre Rames67555f72014-11-18 10:55:16 +0000413 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000414 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000415 Location out = locations->Out();
416 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000417
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100418 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
419 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000420 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100421
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100422 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000423 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
424 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
425 bool is_load_class_bss_entry =
426 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
427 Register entry_address = kNoRegister;
428 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
429 Register temp = locations->GetTemp(0).AsRegister<Register>();
430 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
431 // the kSaveEverything call.
432 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
433 entry_address = temp_is_r0 ? out.AsRegister<Register>() : temp;
434 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
435 if (temp_is_r0) {
436 __ mov(entry_address, ShifterOperand(temp));
437 }
438 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000439 dex::TypeIndex type_index = cls_->GetTypeIndex();
440 __ LoadImmediate(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100441 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
442 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000443 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000444 if (do_clinit_) {
445 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
446 } else {
447 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
448 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000449
Vladimir Markoea4c1262017-02-06 19:59:33 +0000450 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
451 if (is_load_class_bss_entry) {
452 if (call_saves_everything_except_r0) {
453 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
454 __ str(R0, Address(entry_address));
455 } else {
456 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
457 Register temp = IP;
458 CodeGeneratorARM::PcRelativePatchInfo* labels =
459 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
460 __ BindTrackedLabel(&labels->movw_label);
461 __ movw(temp, /* placeholder */ 0u);
462 __ BindTrackedLabel(&labels->movt_label);
463 __ movt(temp, /* placeholder */ 0u);
464 __ BindTrackedLabel(&labels->add_pc_label);
465 __ add(temp, temp, ShifterOperand(PC));
466 __ str(R0, Address(temp));
467 }
468 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000469 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000470 if (out.IsValid()) {
471 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000472 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
473 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000474 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100475 __ b(GetExitLabel());
476 }
477
Alexandre Rames9931f312015-06-19 14:47:01 +0100478 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
479
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100480 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000481 // The class this slow path will load.
482 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100483
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000484 // The dex PC of `at_`.
485 const uint32_t dex_pc_;
486
487 // Whether to initialize the class.
488 const bool do_clinit_;
489
490 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100491};
492
Vladimir Markoaad75c62016-10-03 08:46:48 +0000493class LoadStringSlowPathARM : public SlowPathCodeARM {
494 public:
495 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
496
497 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000498 DCHECK(instruction_->IsLoadString());
499 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000500 LocationSummary* locations = instruction_->GetLocations();
501 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100502 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000503 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100504 Register out = locations->Out().AsRegister<Register>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100505 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000506
507 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
508 __ Bind(GetEntryLabel());
509 SaveLiveRegisters(codegen, locations);
510
511 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100512 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000513 // the kSaveEverything call.
514 Register entry_address = kNoRegister;
515 if (call_saves_everything_except_r0) {
516 Register temp = locations->GetTemp(0).AsRegister<Register>();
517 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
518 entry_address = temp_is_r0 ? out : temp;
519 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
520 if (temp_is_r0) {
521 __ mov(entry_address, ShifterOperand(temp));
522 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100523 }
524
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000525 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000526 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
527 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100528
529 // Store the resolved String to the .bss entry.
530 if (call_saves_everything_except_r0) {
531 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
532 __ str(R0, Address(entry_address));
533 } else {
534 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000535 Register temp = IP;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100536 CodeGeneratorARM::PcRelativePatchInfo* labels =
537 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
538 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000539 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100540 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000541 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100542 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000543 __ add(temp, temp, ShifterOperand(PC));
544 __ str(R0, Address(temp));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100545 }
546
Vladimir Markoaad75c62016-10-03 08:46:48 +0000547 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000548 RestoreLiveRegisters(codegen, locations);
549
Vladimir Markoaad75c62016-10-03 08:46:48 +0000550 __ b(GetExitLabel());
551 }
552
553 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
554
555 private:
556 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
557};
558
Artem Serovf4d6aee2016-07-11 10:41:45 +0100559class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000560 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000561 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100562 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000563
Alexandre Rames67555f72014-11-18 10:55:16 +0000564 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000565 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000566 DCHECK(instruction_->IsCheckCast()
567 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000568
569 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
570 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000571
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000572 if (!is_fatal_) {
573 SaveLiveRegisters(codegen, locations);
574 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000575
576 // We're moving two locations to locations that could overlap, so we need a parallel
577 // move resolver.
578 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800579 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800580 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
581 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800582 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800583 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
584 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000585 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100586 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100587 instruction_,
588 instruction_->GetDexPc(),
589 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800590 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000591 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
592 } else {
593 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800594 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
595 instruction_,
596 instruction_->GetDexPc(),
597 this);
598 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000599 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000600
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000601 if (!is_fatal_) {
602 RestoreLiveRegisters(codegen, locations);
603 __ b(GetExitLabel());
604 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000605 }
606
Alexandre Rames9931f312015-06-19 14:47:01 +0100607 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
608
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000609 bool IsFatal() const OVERRIDE { return is_fatal_; }
610
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000611 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000612 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000613
614 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
615};
616
Artem Serovf4d6aee2016-07-11 10:41:45 +0100617class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700618 public:
Aart Bik42249c32016-01-07 15:33:50 -0800619 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100620 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700621
622 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800623 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700624 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100625 LocationSummary* locations = instruction_->GetLocations();
626 SaveLiveRegisters(codegen, locations);
627 InvokeRuntimeCallingConvention calling_convention;
628 __ LoadImmediate(calling_convention.GetRegisterAt(0),
629 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100630 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100631 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700632 }
633
Alexandre Rames9931f312015-06-19 14:47:01 +0100634 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
635
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700636 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700637 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
638};
639
Artem Serovf4d6aee2016-07-11 10:41:45 +0100640class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100641 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100642 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100643
644 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
645 LocationSummary* locations = instruction_->GetLocations();
646 __ Bind(GetEntryLabel());
647 SaveLiveRegisters(codegen, locations);
648
649 InvokeRuntimeCallingConvention calling_convention;
650 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
651 parallel_move.AddMove(
652 locations->InAt(0),
653 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
654 Primitive::kPrimNot,
655 nullptr);
656 parallel_move.AddMove(
657 locations->InAt(1),
658 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
659 Primitive::kPrimInt,
660 nullptr);
661 parallel_move.AddMove(
662 locations->InAt(2),
663 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
664 Primitive::kPrimNot,
665 nullptr);
666 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
667
668 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100669 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000670 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100671 RestoreLiveRegisters(codegen, locations);
672 __ b(GetExitLabel());
673 }
674
675 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
676
677 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100678 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
679};
680
Roland Levillain54f869e2017-03-06 13:54:11 +0000681// Abstract base class for read barrier slow paths marking a reference
682// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000683//
Roland Levillain54f869e2017-03-06 13:54:11 +0000684// Argument `entrypoint` must be a register location holding the read
685// barrier marking runtime entry point to be invoked.
686class ReadBarrierMarkSlowPathBaseARM : public SlowPathCodeARM {
687 protected:
688 ReadBarrierMarkSlowPathBaseARM(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000689 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
690 DCHECK(kEmitCompilerReadBarrier);
691 }
692
Roland Levillain54f869e2017-03-06 13:54:11 +0000693 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000694
Roland Levillain54f869e2017-03-06 13:54:11 +0000695 // Generate assembly code calling the read barrier marking runtime
696 // entry point (ReadBarrierMarkRegX).
697 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000698 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain47b3ab22017-02-27 14:31:35 +0000699
Roland Levillain47b3ab22017-02-27 14:31:35 +0000700 // No need to save live registers; it's taken care of by the
701 // entrypoint. Also, there is no need to update the stack mask,
702 // as this runtime call will not trigger a garbage collection.
703 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
704 DCHECK_NE(ref_reg, SP);
705 DCHECK_NE(ref_reg, LR);
706 DCHECK_NE(ref_reg, PC);
707 // IP is used internally by the ReadBarrierMarkRegX entry point
708 // as a temporary, it cannot be the entry point's input/output.
709 DCHECK_NE(ref_reg, IP);
710 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
711 // "Compact" slow path, saving two moves.
712 //
713 // Instead of using the standard runtime calling convention (input
714 // and output in R0):
715 //
716 // R0 <- ref
717 // R0 <- ReadBarrierMark(R0)
718 // ref <- R0
719 //
720 // we just use rX (the register containing `ref`) as input and output
721 // of a dedicated entrypoint:
722 //
723 // rX <- ReadBarrierMarkRegX(rX)
724 //
725 if (entrypoint_.IsValid()) {
726 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
727 __ blx(entrypoint_.AsRegister<Register>());
728 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000729 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000730 int32_t entry_point_offset =
731 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
732 // This runtime call does not require a stack map.
733 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
734 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000735 }
736
737 // The location (register) of the marked object reference.
738 const Location ref_;
739
740 // The location of the entrypoint if it is already loaded.
741 const Location entrypoint_;
742
743 private:
744 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM);
745};
746
Dave Allison20dfc792014-06-16 20:44:29 -0700747// Slow path marking an object reference `ref` during a read
748// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000749// reference does not get updated by this slow path after marking.
Dave Allison20dfc792014-06-16 20:44:29 -0700750//
751// This means that after the execution of this slow path, `ref` will
752// always be up-to-date, but `obj.field` may not; i.e., after the
753// flip, `ref` will be a to-space reference, but `obj.field` will
754// probably still be a from-space reference (unless it gets updated by
755// another thread, or if another thread installed another object
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000756// reference (different from `ref`) in `obj.field`).
757//
758// If `entrypoint` is a valid location it is assumed to already be
759// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000760// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000761class ReadBarrierMarkSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000762 public:
763 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
764 Location ref,
765 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000766 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000767 DCHECK(kEmitCompilerReadBarrier);
768 }
769
770 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
771
772 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
773 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000774 DCHECK(locations->CanCall());
775 if (kIsDebugBuild) {
776 Register ref_reg = ref_.AsRegister<Register>();
777 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
778 }
779 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
780 << "Unexpected instruction in read barrier marking slow path: "
781 << instruction_->DebugName();
782
783 __ Bind(GetEntryLabel());
784 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000785 __ b(GetExitLabel());
786 }
787
788 private:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000789 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
790};
791
Roland Levillain54f869e2017-03-06 13:54:11 +0000792// Slow path loading `obj`'s lock word, loading a reference from
793// object `*(obj + offset + (index << scale_factor))` into `ref`, and
794// marking `ref` if `obj` is gray according to the lock word (Baker
795// read barrier). The field `obj.field` in the object `obj` holding
796// this reference does not get updated by this slow path after marking
797// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
798// below for that).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000799//
Roland Levillain54f869e2017-03-06 13:54:11 +0000800// This means that after the execution of this slow path, `ref` will
801// always be up-to-date, but `obj.field` may not; i.e., after the
802// flip, `ref` will be a to-space reference, but `obj.field` will
803// probably still be a from-space reference (unless it gets updated by
804// another thread, or if another thread installed another object
805// reference (different from `ref`) in `obj.field`).
806//
807// Argument `entrypoint` must be a register location holding the read
808// barrier marking runtime entry point to be invoked.
809class LoadReferenceWithBakerReadBarrierSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000810 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000811 LoadReferenceWithBakerReadBarrierSlowPathARM(HInstruction* instruction,
812 Location ref,
813 Register obj,
814 uint32_t offset,
815 Location index,
816 ScaleFactor scale_factor,
817 bool needs_null_check,
818 Register temp,
819 Location entrypoint)
820 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000821 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000822 offset_(offset),
823 index_(index),
824 scale_factor_(scale_factor),
825 needs_null_check_(needs_null_check),
826 temp_(temp) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000827 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000828 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000829 }
830
Roland Levillain54f869e2017-03-06 13:54:11 +0000831 const char* GetDescription() const OVERRIDE {
832 return "LoadReferenceWithBakerReadBarrierSlowPathARM";
833 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000834
835 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
836 LocationSummary* locations = instruction_->GetLocations();
837 Register ref_reg = ref_.AsRegister<Register>();
838 DCHECK(locations->CanCall());
839 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000840 DCHECK_NE(ref_reg, temp_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000841 DCHECK(instruction_->IsInstanceFieldGet() ||
842 instruction_->IsStaticFieldGet() ||
843 instruction_->IsArrayGet() ||
844 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000845 instruction_->IsInstanceOf() ||
846 instruction_->IsCheckCast() ||
847 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
848 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
849 << "Unexpected instruction in read barrier marking slow path: "
850 << instruction_->DebugName();
851 // The read barrier instrumentation of object ArrayGet
852 // instructions does not support the HIntermediateAddress
853 // instruction.
854 DCHECK(!(instruction_->IsArrayGet() &&
855 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
856
857 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000858
859 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
860 // inserted after the original load. However, in fast path based
861 // Baker's read barriers, we need to perform the load of
862 // mirror::Object::monitor_ *before* the original reference load.
863 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000864 // The slow path (for Baker's algorithm) should look like:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000865 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000866 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
867 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
868 // HeapReference<mirror::Object> ref = *src; // Original reference load.
869 // bool is_gray = (rb_state == ReadBarrier::GrayState());
870 // if (is_gray) {
871 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
872 // }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000873 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000874 // Note: the original implementation in ReadBarrier::Barrier is
875 // slightly more complex as it performs additional checks that we do
876 // not do here for performance reasons.
877
878 // /* int32_t */ monitor = obj->monitor_
879 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
880 __ LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
881 if (needs_null_check_) {
882 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000883 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000884 // /* LockWord */ lock_word = LockWord(monitor)
885 static_assert(sizeof(LockWord) == sizeof(int32_t),
886 "art::LockWord and int32_t have different sizes.");
887
888 // Introduce a dependency on the lock_word including the rb_state,
889 // which shall prevent load-load reordering without using
890 // a memory barrier (which would be more expensive).
891 // `obj` is unchanged by this operation, but its value now depends
892 // on `temp`.
893 __ add(obj_, obj_, ShifterOperand(temp_, LSR, 32));
894
895 // The actual reference load.
896 // A possible implicit null check has already been handled above.
897 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
898 arm_codegen->GenerateRawReferenceLoad(
899 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
900
901 // Mark the object `ref` when `obj` is gray.
902 //
903 // if (rb_state == ReadBarrier::GrayState())
904 // ref = ReadBarrier::Mark(ref);
905 //
906 // Given the numeric representation, it's enough to check the low bit of the
907 // rb_state. We do that by shifting the bit out of the lock word with LSRS
908 // which can be a 16-bit instruction unlike the TST immediate.
909 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
910 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
911 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
912 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
913 GenerateReadBarrierMarkRuntimeCall(codegen);
914
Roland Levillain47b3ab22017-02-27 14:31:35 +0000915 __ b(GetExitLabel());
916 }
917
918 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000919 // The register containing the object holding the marked object reference field.
920 Register obj_;
921 // The offset, index and scale factor to access the reference in `obj_`.
922 uint32_t offset_;
923 Location index_;
924 ScaleFactor scale_factor_;
925 // Is a null check required?
926 bool needs_null_check_;
927 // A temporary register used to hold the lock word of `obj_`.
928 Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000929
Roland Levillain54f869e2017-03-06 13:54:11 +0000930 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000931};
932
Roland Levillain54f869e2017-03-06 13:54:11 +0000933// Slow path loading `obj`'s lock word, loading a reference from
934// object `*(obj + offset + (index << scale_factor))` into `ref`, and
935// marking `ref` if `obj` is gray according to the lock word (Baker
936// read barrier). If needed, this slow path also atomically updates
937// the field `obj.field` in the object `obj` holding this reference
938// after marking (contrary to
939// LoadReferenceWithBakerReadBarrierSlowPathARM above, which never
940// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000941//
942// This means that after the execution of this slow path, both `ref`
943// and `obj.field` will be up-to-date; i.e., after the flip, both will
944// hold the same to-space reference (unless another thread installed
945// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000946//
Roland Levillain54f869e2017-03-06 13:54:11 +0000947// Argument `entrypoint` must be a register location holding the read
948// barrier marking runtime entry point to be invoked.
949class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
950 : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000951 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000952 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(HInstruction* instruction,
953 Location ref,
954 Register obj,
955 uint32_t offset,
956 Location index,
957 ScaleFactor scale_factor,
958 bool needs_null_check,
959 Register temp1,
960 Register temp2,
961 Location entrypoint)
962 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000963 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000964 offset_(offset),
965 index_(index),
966 scale_factor_(scale_factor),
967 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000968 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +0000969 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000970 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000971 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000972 }
973
Roland Levillain54f869e2017-03-06 13:54:11 +0000974 const char* GetDescription() const OVERRIDE {
975 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM";
976 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000977
978 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
979 LocationSummary* locations = instruction_->GetLocations();
980 Register ref_reg = ref_.AsRegister<Register>();
981 DCHECK(locations->CanCall());
982 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000983 DCHECK_NE(ref_reg, temp1_);
984
985 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000986 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
987 << "Unexpected instruction in read barrier marking and field updating slow path: "
988 << instruction_->DebugName();
989 DCHECK(instruction_->GetLocations()->Intrinsified());
990 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000991 DCHECK_EQ(offset_, 0u);
992 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
993 // The location of the offset of the marked reference field within `obj_`.
994 Location field_offset = index_;
995 DCHECK(field_offset.IsRegisterPair()) << field_offset;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000996
997 __ Bind(GetEntryLabel());
998
Roland Levillainff487002017-03-07 16:50:01 +0000999 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM's:
1000 //
1001 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1002 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1003 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1004 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1005 // if (is_gray) {
1006 // old_ref = ref;
1007 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1008 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1009 // }
1010
Roland Levillain54f869e2017-03-06 13:54:11 +00001011 // /* int32_t */ monitor = obj->monitor_
1012 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1013 __ LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1014 if (needs_null_check_) {
1015 codegen->MaybeRecordImplicitNullCheck(instruction_);
1016 }
1017 // /* LockWord */ lock_word = LockWord(monitor)
1018 static_assert(sizeof(LockWord) == sizeof(int32_t),
1019 "art::LockWord and int32_t have different sizes.");
1020
1021 // Introduce a dependency on the lock_word including the rb_state,
1022 // which shall prevent load-load reordering without using
1023 // a memory barrier (which would be more expensive).
1024 // `obj` is unchanged by this operation, but its value now depends
1025 // on `temp1`.
1026 __ add(obj_, obj_, ShifterOperand(temp1_, LSR, 32));
1027
1028 // The actual reference load.
1029 // A possible implicit null check has already been handled above.
1030 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1031 arm_codegen->GenerateRawReferenceLoad(
1032 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1033
1034 // Mark the object `ref` when `obj` is gray.
1035 //
1036 // if (rb_state == ReadBarrier::GrayState())
1037 // ref = ReadBarrier::Mark(ref);
1038 //
1039 // Given the numeric representation, it's enough to check the low bit of the
1040 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1041 // which can be a 16-bit instruction unlike the TST immediate.
1042 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1043 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1044 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1045 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
1046
1047 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001048 // Note that we cannot use IP to save the old reference, as IP is
1049 // used internally by the ReadBarrierMarkRegX entry point, and we
1050 // need the old reference after the call to that entry point.
1051 DCHECK_NE(temp1_, IP);
1052 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001053
Roland Levillain54f869e2017-03-06 13:54:11 +00001054 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001055
1056 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001057 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001058 //
1059 // Note that this field could also hold a different object, if
1060 // another thread had concurrently changed it. In that case, the
1061 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1062 // (CAS) operation below would abort the CAS, leaving the field
1063 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001064 __ cmp(temp1_, ShifterOperand(ref_reg));
Roland Levillain54f869e2017-03-06 13:54:11 +00001065 __ b(GetExitLabel(), EQ);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001066
1067 // Update the the holder's field atomically. This may fail if
1068 // mutator updates before us, but it's OK. This is achieved
1069 // using a strong compare-and-set (CAS) operation with relaxed
1070 // memory synchronization ordering, where the expected value is
1071 // the old reference and the desired value is the new reference.
1072
1073 // Convenience aliases.
1074 Register base = obj_;
1075 // The UnsafeCASObject intrinsic uses a register pair as field
1076 // offset ("long offset"), of which only the low part contains
1077 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001078 Register offset = field_offset.AsRegisterPairLow<Register>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001079 Register expected = temp1_;
1080 Register value = ref_reg;
1081 Register tmp_ptr = IP; // Pointer to actual memory.
1082 Register tmp = temp2_; // Value in memory.
1083
1084 __ add(tmp_ptr, base, ShifterOperand(offset));
1085
1086 if (kPoisonHeapReferences) {
1087 __ PoisonHeapReference(expected);
1088 if (value == expected) {
1089 // Do not poison `value`, as it is the same register as
1090 // `expected`, which has just been poisoned.
1091 } else {
1092 __ PoisonHeapReference(value);
1093 }
1094 }
1095
1096 // do {
1097 // tmp = [r_ptr] - expected;
1098 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1099
Roland Levillain24a4d112016-10-26 13:10:46 +01001100 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001101 __ Bind(&loop_head);
1102
1103 __ ldrex(tmp, tmp_ptr);
1104
1105 __ subs(tmp, tmp, ShifterOperand(expected));
1106
Roland Levillain24a4d112016-10-26 13:10:46 +01001107 __ it(NE);
1108 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001109
Roland Levillain24a4d112016-10-26 13:10:46 +01001110 __ b(&exit_loop, NE);
1111
1112 __ strex(tmp, value, tmp_ptr);
1113 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001114 __ b(&loop_head, EQ);
1115
Roland Levillain24a4d112016-10-26 13:10:46 +01001116 __ Bind(&exit_loop);
1117
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001118 if (kPoisonHeapReferences) {
1119 __ UnpoisonHeapReference(expected);
1120 if (value == expected) {
1121 // Do not unpoison `value`, as it is the same register as
1122 // `expected`, which has just been unpoisoned.
1123 } else {
1124 __ UnpoisonHeapReference(value);
1125 }
1126 }
1127
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001128 __ b(GetExitLabel());
1129 }
1130
1131 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001132 // The register containing the object holding the marked object reference field.
1133 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001134 // The offset, index and scale factor to access the reference in `obj_`.
1135 uint32_t offset_;
1136 Location index_;
1137 ScaleFactor scale_factor_;
1138 // Is a null check required?
1139 bool needs_null_check_;
1140 // A temporary register used to hold the lock word of `obj_`; and
1141 // also to hold the original reference value, when the reference is
1142 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001143 const Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001144 // A temporary register used in the implementation of the CAS, to
1145 // update the object's reference field.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001146 const Register temp2_;
1147
Roland Levillain54f869e2017-03-06 13:54:11 +00001148 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001149};
1150
Roland Levillain3b359c72015-11-17 19:35:12 +00001151// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001152class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001153 public:
1154 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
1155 Location out,
1156 Location ref,
1157 Location obj,
1158 uint32_t offset,
1159 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001160 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +00001161 out_(out),
1162 ref_(ref),
1163 obj_(obj),
1164 offset_(offset),
1165 index_(index) {
1166 DCHECK(kEmitCompilerReadBarrier);
1167 // If `obj` is equal to `out` or `ref`, it means the initial object
1168 // has been overwritten by (or after) the heap object reference load
1169 // to be instrumented, e.g.:
1170 //
1171 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00001172 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001173 //
1174 // In that case, we have lost the information about the original
1175 // object, and the emitted read barrier cannot work properly.
1176 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1177 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1178 }
1179
1180 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1181 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1182 LocationSummary* locations = instruction_->GetLocations();
1183 Register reg_out = out_.AsRegister<Register>();
1184 DCHECK(locations->CanCall());
1185 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +01001186 DCHECK(instruction_->IsInstanceFieldGet() ||
1187 instruction_->IsStaticFieldGet() ||
1188 instruction_->IsArrayGet() ||
1189 instruction_->IsInstanceOf() ||
1190 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001191 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +00001192 << "Unexpected instruction in read barrier for heap reference slow path: "
1193 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001194 // The read barrier instrumentation of object ArrayGet
1195 // instructions does not support the HIntermediateAddress
1196 // instruction.
1197 DCHECK(!(instruction_->IsArrayGet() &&
1198 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +00001199
1200 __ Bind(GetEntryLabel());
1201 SaveLiveRegisters(codegen, locations);
1202
1203 // We may have to change the index's value, but as `index_` is a
1204 // constant member (like other "inputs" of this slow path),
1205 // introduce a copy of it, `index`.
1206 Location index = index_;
1207 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001208 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +00001209 if (instruction_->IsArrayGet()) {
1210 // Compute the actual memory offset and store it in `index`.
1211 Register index_reg = index_.AsRegister<Register>();
1212 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
1213 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
1214 // We are about to change the value of `index_reg` (see the
1215 // calls to art::arm::Thumb2Assembler::Lsl and
1216 // art::arm::Thumb2Assembler::AddConstant below), but it has
1217 // not been saved by the previous call to
1218 // art::SlowPathCode::SaveLiveRegisters, as it is a
1219 // callee-save register --
1220 // art::SlowPathCode::SaveLiveRegisters does not consider
1221 // callee-save registers, as it has been designed with the
1222 // assumption that callee-save registers are supposed to be
1223 // handled by the called function. So, as a callee-save
1224 // register, `index_reg` _would_ eventually be saved onto
1225 // the stack, but it would be too late: we would have
1226 // changed its value earlier. Therefore, we manually save
1227 // it here into another freely available register,
1228 // `free_reg`, chosen of course among the caller-save
1229 // registers (as a callee-save `free_reg` register would
1230 // exhibit the same problem).
1231 //
1232 // Note we could have requested a temporary register from
1233 // the register allocator instead; but we prefer not to, as
1234 // this is a slow path, and we know we can find a
1235 // caller-save register that is available.
1236 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1237 __ Mov(free_reg, index_reg);
1238 index_reg = free_reg;
1239 index = Location::RegisterLocation(index_reg);
1240 } else {
1241 // The initial register stored in `index_` has already been
1242 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1243 // (as it is not a callee-save register), so we can freely
1244 // use it.
1245 }
1246 // Shifting the index value contained in `index_reg` by the scale
1247 // factor (2) cannot overflow in practice, as the runtime is
1248 // unable to allocate object arrays with a size larger than
1249 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1250 __ Lsl(index_reg, index_reg, TIMES_4);
1251 static_assert(
1252 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1253 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1254 __ AddConstant(index_reg, index_reg, offset_);
1255 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001256 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1257 // intrinsics, `index_` is not shifted by a scale factor of 2
1258 // (as in the case of ArrayGet), as it is actually an offset
1259 // to an object field within an object.
1260 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001261 DCHECK(instruction_->GetLocations()->Intrinsified());
1262 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1263 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1264 << instruction_->AsInvoke()->GetIntrinsic();
1265 DCHECK_EQ(offset_, 0U);
1266 DCHECK(index_.IsRegisterPair());
1267 // UnsafeGet's offset location is a register pair, the low
1268 // part contains the correct offset.
1269 index = index_.ToLow();
1270 }
1271 }
1272
1273 // We're moving two or three locations to locations that could
1274 // overlap, so we need a parallel move resolver.
1275 InvokeRuntimeCallingConvention calling_convention;
1276 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1277 parallel_move.AddMove(ref_,
1278 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1279 Primitive::kPrimNot,
1280 nullptr);
1281 parallel_move.AddMove(obj_,
1282 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1283 Primitive::kPrimNot,
1284 nullptr);
1285 if (index.IsValid()) {
1286 parallel_move.AddMove(index,
1287 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1288 Primitive::kPrimInt,
1289 nullptr);
1290 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1291 } else {
1292 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1293 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1294 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001295 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001296 CheckEntrypointTypes<
1297 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1298 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1299
1300 RestoreLiveRegisters(codegen, locations);
1301 __ b(GetExitLabel());
1302 }
1303
1304 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1305
1306 private:
1307 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1308 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1309 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1310 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1311 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1312 return static_cast<Register>(i);
1313 }
1314 }
1315 // We shall never fail to find a free caller-save register, as
1316 // there are more than two core caller-save registers on ARM
1317 // (meaning it is possible to find one which is different from
1318 // `ref` and `obj`).
1319 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1320 LOG(FATAL) << "Could not find a free caller-save register";
1321 UNREACHABLE();
1322 }
1323
Roland Levillain3b359c72015-11-17 19:35:12 +00001324 const Location out_;
1325 const Location ref_;
1326 const Location obj_;
1327 const uint32_t offset_;
1328 // An additional location containing an index to an array.
1329 // Only used for HArrayGet and the UnsafeGetObject &
1330 // UnsafeGetObjectVolatile intrinsics.
1331 const Location index_;
1332
1333 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1334};
1335
1336// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001337class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001338 public:
1339 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001340 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001341 DCHECK(kEmitCompilerReadBarrier);
1342 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001343
1344 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1345 LocationSummary* locations = instruction_->GetLocations();
1346 Register reg_out = out_.AsRegister<Register>();
1347 DCHECK(locations->CanCall());
1348 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001349 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1350 << "Unexpected instruction in read barrier for GC root slow path: "
1351 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001352
1353 __ Bind(GetEntryLabel());
1354 SaveLiveRegisters(codegen, locations);
1355
1356 InvokeRuntimeCallingConvention calling_convention;
1357 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1358 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001359 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001360 instruction_,
1361 instruction_->GetDexPc(),
1362 this);
1363 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1364 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1365
1366 RestoreLiveRegisters(codegen, locations);
1367 __ b(GetExitLabel());
1368 }
1369
1370 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1371
1372 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001373 const Location out_;
1374 const Location root_;
1375
1376 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1377};
1378
Aart Bike9f37602015-10-09 11:15:55 -07001379inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001380 switch (cond) {
1381 case kCondEQ: return EQ;
1382 case kCondNE: return NE;
1383 case kCondLT: return LT;
1384 case kCondLE: return LE;
1385 case kCondGT: return GT;
1386 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001387 case kCondB: return LO;
1388 case kCondBE: return LS;
1389 case kCondA: return HI;
1390 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001391 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001392 LOG(FATAL) << "Unreachable";
1393 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001394}
1395
Aart Bike9f37602015-10-09 11:15:55 -07001396// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001397inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001398 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001399 case kCondEQ: return EQ;
1400 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001401 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001402 case kCondLT: return LO;
1403 case kCondLE: return LS;
1404 case kCondGT: return HI;
1405 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001406 // Unsigned remain unchanged.
1407 case kCondB: return LO;
1408 case kCondBE: return LS;
1409 case kCondA: return HI;
1410 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001411 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001412 LOG(FATAL) << "Unreachable";
1413 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001414}
1415
Vladimir Markod6e069b2016-01-18 11:11:01 +00001416inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1417 // The ARM condition codes can express all the necessary branches, see the
1418 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1419 // There is no dex instruction or HIR that would need the missing conditions
1420 // "equal or unordered" or "not equal".
1421 switch (cond) {
1422 case kCondEQ: return EQ;
1423 case kCondNE: return NE /* unordered */;
1424 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1425 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1426 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1427 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1428 default:
1429 LOG(FATAL) << "UNREACHABLE";
1430 UNREACHABLE();
1431 }
1432}
1433
Anton Kirilov74234da2017-01-13 14:42:47 +00001434inline Shift ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1435 switch (op_kind) {
1436 case HDataProcWithShifterOp::kASR: return ASR;
1437 case HDataProcWithShifterOp::kLSL: return LSL;
1438 case HDataProcWithShifterOp::kLSR: return LSR;
1439 default:
1440 LOG(FATAL) << "Unexpected op kind " << op_kind;
1441 UNREACHABLE();
1442 }
1443}
1444
1445static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1446 Register out,
1447 Register first,
1448 const ShifterOperand& second,
1449 CodeGeneratorARM* codegen) {
1450 if (second.IsImmediate() && second.GetImmediate() == 0) {
1451 const ShifterOperand in = kind == HInstruction::kAnd
1452 ? ShifterOperand(0)
1453 : ShifterOperand(first);
1454
1455 __ mov(out, in);
1456 } else {
1457 switch (kind) {
1458 case HInstruction::kAdd:
1459 __ add(out, first, second);
1460 break;
1461 case HInstruction::kAnd:
1462 __ and_(out, first, second);
1463 break;
1464 case HInstruction::kOr:
1465 __ orr(out, first, second);
1466 break;
1467 case HInstruction::kSub:
1468 __ sub(out, first, second);
1469 break;
1470 case HInstruction::kXor:
1471 __ eor(out, first, second);
1472 break;
1473 default:
1474 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1475 UNREACHABLE();
1476 }
1477 }
1478}
1479
1480static void GenerateDataProc(HInstruction::InstructionKind kind,
1481 const Location& out,
1482 const Location& first,
1483 const ShifterOperand& second_lo,
1484 const ShifterOperand& second_hi,
1485 CodeGeneratorARM* codegen) {
1486 const Register first_hi = first.AsRegisterPairHigh<Register>();
1487 const Register first_lo = first.AsRegisterPairLow<Register>();
1488 const Register out_hi = out.AsRegisterPairHigh<Register>();
1489 const Register out_lo = out.AsRegisterPairLow<Register>();
1490
1491 if (kind == HInstruction::kAdd) {
1492 __ adds(out_lo, first_lo, second_lo);
1493 __ adc(out_hi, first_hi, second_hi);
1494 } else if (kind == HInstruction::kSub) {
1495 __ subs(out_lo, first_lo, second_lo);
1496 __ sbc(out_hi, first_hi, second_hi);
1497 } else {
1498 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1499 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1500 }
1501}
1502
1503static ShifterOperand GetShifterOperand(Register rm, Shift shift, uint32_t shift_imm) {
1504 return shift_imm == 0 ? ShifterOperand(rm) : ShifterOperand(rm, shift, shift_imm);
1505}
1506
1507static void GenerateLongDataProc(HDataProcWithShifterOp* instruction, CodeGeneratorARM* codegen) {
1508 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1509 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1510
1511 const LocationSummary* const locations = instruction->GetLocations();
1512 const uint32_t shift_value = instruction->GetShiftAmount();
1513 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1514 const Location first = locations->InAt(0);
1515 const Location second = locations->InAt(1);
1516 const Location out = locations->Out();
1517 const Register first_hi = first.AsRegisterPairHigh<Register>();
1518 const Register first_lo = first.AsRegisterPairLow<Register>();
1519 const Register out_hi = out.AsRegisterPairHigh<Register>();
1520 const Register out_lo = out.AsRegisterPairLow<Register>();
1521 const Register second_hi = second.AsRegisterPairHigh<Register>();
1522 const Register second_lo = second.AsRegisterPairLow<Register>();
1523 const Shift shift = ShiftFromOpKind(instruction->GetOpKind());
1524
1525 if (shift_value >= 32) {
1526 if (shift == LSL) {
1527 GenerateDataProcInstruction(kind,
1528 out_hi,
1529 first_hi,
1530 ShifterOperand(second_lo, LSL, shift_value - 32),
1531 codegen);
1532 GenerateDataProcInstruction(kind,
1533 out_lo,
1534 first_lo,
1535 ShifterOperand(0),
1536 codegen);
1537 } else if (shift == ASR) {
1538 GenerateDataProc(kind,
1539 out,
1540 first,
1541 GetShifterOperand(second_hi, ASR, shift_value - 32),
1542 ShifterOperand(second_hi, ASR, 31),
1543 codegen);
1544 } else {
1545 DCHECK_EQ(shift, LSR);
1546 GenerateDataProc(kind,
1547 out,
1548 first,
1549 GetShifterOperand(second_hi, LSR, shift_value - 32),
1550 ShifterOperand(0),
1551 codegen);
1552 }
1553 } else {
1554 DCHECK_GT(shift_value, 1U);
1555 DCHECK_LT(shift_value, 32U);
1556
1557 if (shift == LSL) {
1558 // We are not doing this for HInstruction::kAdd because the output will require
1559 // Location::kOutputOverlap; not applicable to other cases.
1560 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1561 GenerateDataProcInstruction(kind,
1562 out_hi,
1563 first_hi,
1564 ShifterOperand(second_hi, LSL, shift_value),
1565 codegen);
1566 GenerateDataProcInstruction(kind,
1567 out_hi,
1568 out_hi,
1569 ShifterOperand(second_lo, LSR, 32 - shift_value),
1570 codegen);
1571 GenerateDataProcInstruction(kind,
1572 out_lo,
1573 first_lo,
1574 ShifterOperand(second_lo, LSL, shift_value),
1575 codegen);
1576 } else {
1577 __ Lsl(IP, second_hi, shift_value);
1578 __ orr(IP, IP, ShifterOperand(second_lo, LSR, 32 - shift_value));
1579 GenerateDataProc(kind,
1580 out,
1581 first,
1582 ShifterOperand(second_lo, LSL, shift_value),
1583 ShifterOperand(IP),
1584 codegen);
1585 }
1586 } else {
1587 DCHECK(shift == ASR || shift == LSR);
1588
1589 // We are not doing this for HInstruction::kAdd because the output will require
1590 // Location::kOutputOverlap; not applicable to other cases.
1591 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1592 GenerateDataProcInstruction(kind,
1593 out_lo,
1594 first_lo,
1595 ShifterOperand(second_lo, LSR, shift_value),
1596 codegen);
1597 GenerateDataProcInstruction(kind,
1598 out_lo,
1599 out_lo,
1600 ShifterOperand(second_hi, LSL, 32 - shift_value),
1601 codegen);
1602 GenerateDataProcInstruction(kind,
1603 out_hi,
1604 first_hi,
1605 ShifterOperand(second_hi, shift, shift_value),
1606 codegen);
1607 } else {
1608 __ Lsr(IP, second_lo, shift_value);
1609 __ orr(IP, IP, ShifterOperand(second_hi, LSL, 32 - shift_value));
1610 GenerateDataProc(kind,
1611 out,
1612 first,
1613 ShifterOperand(IP),
1614 ShifterOperand(second_hi, shift, shift_value),
1615 codegen);
1616 }
1617 }
1618 }
1619}
1620
Donghui Bai426b49c2016-11-08 14:55:38 +08001621static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARM* codegen) {
1622 Primitive::Type type = instruction->InputAt(0)->GetType();
1623 Location lhs_loc = instruction->GetLocations()->InAt(0);
1624 Location rhs_loc = instruction->GetLocations()->InAt(1);
1625 if (rhs_loc.IsConstant()) {
1626 // 0.0 is the only immediate that can be encoded directly in
1627 // a VCMP instruction.
1628 //
1629 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1630 // specify that in a floating-point comparison, positive zero
1631 // and negative zero are considered equal, so we can use the
1632 // literal 0.0 for both cases here.
1633 //
1634 // Note however that some methods (Float.equal, Float.compare,
1635 // Float.compareTo, Double.equal, Double.compare,
1636 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1637 // StrictMath.min) consider 0.0 to be (strictly) greater than
1638 // -0.0. So if we ever translate calls to these methods into a
1639 // HCompare instruction, we must handle the -0.0 case with
1640 // care here.
1641 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1642 if (type == Primitive::kPrimFloat) {
1643 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1644 } else {
1645 DCHECK_EQ(type, Primitive::kPrimDouble);
1646 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1647 }
1648 } else {
1649 if (type == Primitive::kPrimFloat) {
1650 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1651 } else {
1652 DCHECK_EQ(type, Primitive::kPrimDouble);
1653 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1654 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1655 }
1656 }
1657}
1658
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001659static int64_t AdjustConstantForCondition(int64_t value,
1660 IfCondition* condition,
1661 IfCondition* opposite) {
1662 if (value == 1) {
1663 if (*condition == kCondB) {
1664 value = 0;
1665 *condition = kCondEQ;
1666 *opposite = kCondNE;
1667 } else if (*condition == kCondAE) {
1668 value = 0;
1669 *condition = kCondNE;
1670 *opposite = kCondEQ;
1671 }
1672 } else if (value == -1) {
1673 if (*condition == kCondGT) {
1674 value = 0;
1675 *condition = kCondGE;
1676 *opposite = kCondLT;
1677 } else if (*condition == kCondLE) {
1678 value = 0;
1679 *condition = kCondLT;
1680 *opposite = kCondGE;
1681 }
1682 }
1683
1684 return value;
1685}
1686
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001687static std::pair<Condition, Condition> GenerateLongTestConstant(HCondition* condition,
1688 bool invert,
1689 CodeGeneratorARM* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001690 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1691
1692 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001693 IfCondition cond = condition->GetCondition();
1694 IfCondition opposite = condition->GetOppositeCondition();
1695
1696 if (invert) {
1697 std::swap(cond, opposite);
1698 }
1699
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001700 std::pair<Condition, Condition> ret(EQ, NE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001701 const Location left = locations->InAt(0);
1702 const Location right = locations->InAt(1);
1703
1704 DCHECK(right.IsConstant());
1705
1706 const Register left_high = left.AsRegisterPairHigh<Register>();
1707 const Register left_low = left.AsRegisterPairLow<Register>();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001708 int64_t value = AdjustConstantForCondition(right.GetConstant()->AsLongConstant()->GetValue(),
1709 &cond,
1710 &opposite);
1711
1712 // Comparisons against 0 are common enough to deserve special attention.
1713 if (value == 0) {
1714 switch (cond) {
1715 case kCondNE:
1716 // x > 0 iff x != 0 when the comparison is unsigned.
1717 case kCondA:
1718 ret = std::make_pair(NE, EQ);
1719 FALLTHROUGH_INTENDED;
1720 case kCondEQ:
1721 // x <= 0 iff x == 0 when the comparison is unsigned.
1722 case kCondBE:
1723 __ orrs(IP, left_low, ShifterOperand(left_high));
1724 return ret;
1725 case kCondLT:
1726 case kCondGE:
1727 __ cmp(left_high, ShifterOperand(0));
1728 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1729 // Trivially true or false.
1730 case kCondB:
1731 ret = std::make_pair(NE, EQ);
1732 FALLTHROUGH_INTENDED;
1733 case kCondAE:
1734 __ cmp(left_low, ShifterOperand(left_low));
1735 return ret;
1736 default:
1737 break;
1738 }
1739 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001740
1741 switch (cond) {
1742 case kCondEQ:
1743 case kCondNE:
1744 case kCondB:
1745 case kCondBE:
1746 case kCondA:
1747 case kCondAE:
1748 __ CmpConstant(left_high, High32Bits(value));
1749 __ it(EQ);
1750 __ cmp(left_low, ShifterOperand(Low32Bits(value)), EQ);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001751 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001752 break;
1753 case kCondLE:
1754 case kCondGT:
1755 // Trivially true or false.
1756 if (value == std::numeric_limits<int64_t>::max()) {
1757 __ cmp(left_low, ShifterOperand(left_low));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001758 ret = cond == kCondLE ? std::make_pair(EQ, NE) : std::make_pair(NE, EQ);
Donghui Bai426b49c2016-11-08 14:55:38 +08001759 break;
1760 }
1761
1762 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001763 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001764 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001765 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001766 } else {
1767 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001768 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001769 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001770 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001771 }
1772
1773 value++;
1774 FALLTHROUGH_INTENDED;
1775 case kCondGE:
1776 case kCondLT:
1777 __ CmpConstant(left_low, Low32Bits(value));
1778 __ sbcs(IP, left_high, ShifterOperand(High32Bits(value)));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001779 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001780 break;
1781 default:
1782 LOG(FATAL) << "Unreachable";
1783 UNREACHABLE();
1784 }
1785
1786 return ret;
1787}
1788
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001789static std::pair<Condition, Condition> GenerateLongTest(HCondition* condition,
1790 bool invert,
1791 CodeGeneratorARM* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001792 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1793
1794 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001795 IfCondition cond = condition->GetCondition();
1796 IfCondition opposite = condition->GetOppositeCondition();
1797
1798 if (invert) {
1799 std::swap(cond, opposite);
1800 }
1801
1802 std::pair<Condition, Condition> ret;
Donghui Bai426b49c2016-11-08 14:55:38 +08001803 Location left = locations->InAt(0);
1804 Location right = locations->InAt(1);
1805
1806 DCHECK(right.IsRegisterPair());
1807
1808 switch (cond) {
1809 case kCondEQ:
1810 case kCondNE:
1811 case kCondB:
1812 case kCondBE:
1813 case kCondA:
1814 case kCondAE:
1815 __ cmp(left.AsRegisterPairHigh<Register>(),
1816 ShifterOperand(right.AsRegisterPairHigh<Register>()));
1817 __ it(EQ);
1818 __ cmp(left.AsRegisterPairLow<Register>(),
1819 ShifterOperand(right.AsRegisterPairLow<Register>()),
1820 EQ);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001821 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001822 break;
1823 case kCondLE:
1824 case kCondGT:
1825 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001826 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001827 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001828 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001829 } else {
1830 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001831 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001832 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001833 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001834 }
1835
1836 std::swap(left, right);
1837 FALLTHROUGH_INTENDED;
1838 case kCondGE:
1839 case kCondLT:
1840 __ cmp(left.AsRegisterPairLow<Register>(),
1841 ShifterOperand(right.AsRegisterPairLow<Register>()));
1842 __ sbcs(IP,
1843 left.AsRegisterPairHigh<Register>(),
1844 ShifterOperand(right.AsRegisterPairHigh<Register>()));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001845 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001846 break;
1847 default:
1848 LOG(FATAL) << "Unreachable";
1849 UNREACHABLE();
1850 }
1851
1852 return ret;
1853}
1854
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001855static std::pair<Condition, Condition> GenerateTest(HCondition* condition,
1856 bool invert,
1857 CodeGeneratorARM* codegen) {
1858 const LocationSummary* const locations = condition->GetLocations();
1859 const Primitive::Type type = condition->GetLeft()->GetType();
1860 IfCondition cond = condition->GetCondition();
1861 IfCondition opposite = condition->GetOppositeCondition();
1862 std::pair<Condition, Condition> ret;
1863 const Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08001864
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001865 if (invert) {
1866 std::swap(cond, opposite);
1867 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001868
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001869 if (type == Primitive::kPrimLong) {
1870 ret = locations->InAt(1).IsConstant()
1871 ? GenerateLongTestConstant(condition, invert, codegen)
1872 : GenerateLongTest(condition, invert, codegen);
1873 } else if (Primitive::IsFloatingPointType(type)) {
1874 GenerateVcmp(condition, codegen);
1875 __ vmstat();
1876 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1877 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001878 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001879 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08001880
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001881 const Register left = locations->InAt(0).AsRegister<Register>();
1882
1883 if (right.IsRegister()) {
1884 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001885 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001886 DCHECK(right.IsConstant());
1887 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001888 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001889
1890 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001891 }
1892
1893 return ret;
1894}
1895
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001896static bool CanGenerateTest(HCondition* condition, ArmAssembler* assembler) {
1897 if (condition->GetLeft()->GetType() == Primitive::kPrimLong) {
1898 const LocationSummary* const locations = condition->GetLocations();
Donghui Bai426b49c2016-11-08 14:55:38 +08001899
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001900 if (locations->InAt(1).IsConstant()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001901 IfCondition c = condition->GetCondition();
1902 IfCondition opposite = condition->GetOppositeCondition();
1903 const int64_t value = AdjustConstantForCondition(
1904 Int64FromConstant(locations->InAt(1).GetConstant()),
1905 &c,
1906 &opposite);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001907 ShifterOperand so;
Donghui Bai426b49c2016-11-08 14:55:38 +08001908
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001909 if (c < kCondLT || c > kCondGE) {
1910 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1911 // we check that the least significant half of the first input to be compared
1912 // is in a low register (the other half is read outside an IT block), and
1913 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001914 // encoding can be used; 0 is always handled, no matter what registers are
1915 // used by the first input.
1916 if (value != 0 &&
1917 (!ArmAssembler::IsLowRegister(locations->InAt(0).AsRegisterPairLow<Register>()) ||
1918 !IsUint<8>(Low32Bits(value)))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001919 return false;
1920 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001921 } else if (c == kCondLE || c == kCondGT) {
1922 if (value < std::numeric_limits<int64_t>::max() &&
1923 !assembler->ShifterOperandCanHold(kNoRegister,
1924 kNoRegister,
1925 SBC,
1926 High32Bits(value + 1),
1927 kCcSet,
1928 &so)) {
1929 return false;
1930 }
1931 } else if (!assembler->ShifterOperandCanHold(kNoRegister,
1932 kNoRegister,
1933 SBC,
1934 High32Bits(value),
1935 kCcSet,
1936 &so)) {
1937 return false;
Donghui Bai426b49c2016-11-08 14:55:38 +08001938 }
1939 }
1940 }
1941
1942 return true;
1943}
1944
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001945static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARM* codegen) {
1946 DCHECK(CanGenerateTest(cond, codegen->GetAssembler()));
1947
1948 const Register out = cond->GetLocations()->Out().AsRegister<Register>();
1949 const auto condition = GenerateTest(cond, false, codegen);
1950
1951 __ mov(out, ShifterOperand(0), AL, kCcKeep);
1952
1953 if (ArmAssembler::IsLowRegister(out)) {
1954 __ it(condition.first);
1955 __ mov(out, ShifterOperand(1), condition.first);
1956 } else {
1957 Label done_label;
1958 Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
1959
1960 __ b(final_label, condition.second);
1961 __ LoadImmediate(out, 1);
1962
1963 if (done_label.IsLinked()) {
1964 __ Bind(&done_label);
1965 }
1966 }
1967}
1968
1969static void GenerateEqualLong(HCondition* cond, CodeGeneratorARM* codegen) {
1970 DCHECK_EQ(cond->GetLeft()->GetType(), Primitive::kPrimLong);
1971
1972 const LocationSummary* const locations = cond->GetLocations();
1973 IfCondition condition = cond->GetCondition();
1974 const Register out = locations->Out().AsRegister<Register>();
1975 const Location left = locations->InAt(0);
1976 const Location right = locations->InAt(1);
1977 Register left_high = left.AsRegisterPairHigh<Register>();
1978 Register left_low = left.AsRegisterPairLow<Register>();
1979
1980 if (right.IsConstant()) {
1981 IfCondition opposite = cond->GetOppositeCondition();
1982 const int64_t value = AdjustConstantForCondition(Int64FromConstant(right.GetConstant()),
1983 &condition,
1984 &opposite);
1985 int32_t value_high = -High32Bits(value);
1986 int32_t value_low = -Low32Bits(value);
1987
1988 // The output uses Location::kNoOutputOverlap.
1989 if (out == left_high) {
1990 std::swap(left_low, left_high);
1991 std::swap(value_low, value_high);
1992 }
1993
1994 __ AddConstant(out, left_low, value_low);
1995 __ AddConstant(IP, left_high, value_high);
1996 } else {
1997 DCHECK(right.IsRegisterPair());
1998 __ sub(IP, left_high, ShifterOperand(right.AsRegisterPairHigh<Register>()));
1999 __ sub(out, left_low, ShifterOperand(right.AsRegisterPairLow<Register>()));
2000 }
2001
2002 // Need to check after calling AdjustConstantForCondition().
2003 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
2004
2005 if (condition == kCondNE && ArmAssembler::IsLowRegister(out)) {
2006 __ orrs(out, out, ShifterOperand(IP));
2007 __ it(NE);
2008 __ mov(out, ShifterOperand(1), NE);
2009 } else {
2010 __ orr(out, out, ShifterOperand(IP));
2011 codegen->GenerateConditionWithZero(condition, out, out, IP);
2012 }
2013}
2014
2015static void GenerateLongComparesAndJumps(HCondition* cond,
2016 Label* true_label,
2017 Label* false_label,
2018 CodeGeneratorARM* codegen) {
2019 LocationSummary* locations = cond->GetLocations();
2020 Location left = locations->InAt(0);
2021 Location right = locations->InAt(1);
2022 IfCondition if_cond = cond->GetCondition();
2023
2024 Register left_high = left.AsRegisterPairHigh<Register>();
2025 Register left_low = left.AsRegisterPairLow<Register>();
2026 IfCondition true_high_cond = if_cond;
2027 IfCondition false_high_cond = cond->GetOppositeCondition();
2028 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
2029
2030 // Set the conditions for the test, remembering that == needs to be
2031 // decided using the low words.
2032 switch (if_cond) {
2033 case kCondEQ:
2034 case kCondNE:
2035 // Nothing to do.
2036 break;
2037 case kCondLT:
2038 false_high_cond = kCondGT;
2039 break;
2040 case kCondLE:
2041 true_high_cond = kCondLT;
2042 break;
2043 case kCondGT:
2044 false_high_cond = kCondLT;
2045 break;
2046 case kCondGE:
2047 true_high_cond = kCondGT;
2048 break;
2049 case kCondB:
2050 false_high_cond = kCondA;
2051 break;
2052 case kCondBE:
2053 true_high_cond = kCondB;
2054 break;
2055 case kCondA:
2056 false_high_cond = kCondB;
2057 break;
2058 case kCondAE:
2059 true_high_cond = kCondA;
2060 break;
2061 }
2062 if (right.IsConstant()) {
2063 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
2064 int32_t val_low = Low32Bits(value);
2065 int32_t val_high = High32Bits(value);
2066
2067 __ CmpConstant(left_high, val_high);
2068 if (if_cond == kCondNE) {
2069 __ b(true_label, ARMCondition(true_high_cond));
2070 } else if (if_cond == kCondEQ) {
2071 __ b(false_label, ARMCondition(false_high_cond));
2072 } else {
2073 __ b(true_label, ARMCondition(true_high_cond));
2074 __ b(false_label, ARMCondition(false_high_cond));
2075 }
2076 // Must be equal high, so compare the lows.
2077 __ CmpConstant(left_low, val_low);
2078 } else {
2079 Register right_high = right.AsRegisterPairHigh<Register>();
2080 Register right_low = right.AsRegisterPairLow<Register>();
2081
2082 __ cmp(left_high, ShifterOperand(right_high));
2083 if (if_cond == kCondNE) {
2084 __ b(true_label, ARMCondition(true_high_cond));
2085 } else if (if_cond == kCondEQ) {
2086 __ b(false_label, ARMCondition(false_high_cond));
2087 } else {
2088 __ b(true_label, ARMCondition(true_high_cond));
2089 __ b(false_label, ARMCondition(false_high_cond));
2090 }
2091 // Must be equal high, so compare the lows.
2092 __ cmp(left_low, ShifterOperand(right_low));
2093 }
2094 // The last comparison might be unsigned.
2095 // TODO: optimize cases where this is always true/false
2096 __ b(true_label, final_condition);
2097}
2098
2099static void GenerateConditionLong(HCondition* cond, CodeGeneratorARM* codegen) {
2100 DCHECK_EQ(cond->GetLeft()->GetType(), Primitive::kPrimLong);
2101
2102 const LocationSummary* const locations = cond->GetLocations();
2103 IfCondition condition = cond->GetCondition();
2104 const Register out = locations->Out().AsRegister<Register>();
2105 const Location left = locations->InAt(0);
2106 const Location right = locations->InAt(1);
2107
2108 if (right.IsConstant()) {
2109 IfCondition opposite = cond->GetOppositeCondition();
2110
2111 // Comparisons against 0 are common enough to deserve special attention.
2112 if (AdjustConstantForCondition(Int64FromConstant(right.GetConstant()),
2113 &condition,
2114 &opposite) == 0) {
2115 switch (condition) {
2116 case kCondNE:
2117 case kCondA:
2118 if (ArmAssembler::IsLowRegister(out)) {
2119 // We only care if both input registers are 0 or not.
2120 __ orrs(out,
2121 left.AsRegisterPairLow<Register>(),
2122 ShifterOperand(left.AsRegisterPairHigh<Register>()));
2123 __ it(NE);
2124 __ mov(out, ShifterOperand(1), NE);
2125 return;
2126 }
2127
2128 FALLTHROUGH_INTENDED;
2129 case kCondEQ:
2130 case kCondBE:
2131 // We only care if both input registers are 0 or not.
2132 __ orr(out,
2133 left.AsRegisterPairLow<Register>(),
2134 ShifterOperand(left.AsRegisterPairHigh<Register>()));
2135 codegen->GenerateConditionWithZero(condition, out, out);
2136 return;
2137 case kCondLT:
2138 case kCondGE:
2139 // We only care about the sign bit.
2140 FALLTHROUGH_INTENDED;
2141 case kCondAE:
2142 case kCondB:
2143 codegen->GenerateConditionWithZero(condition, out, left.AsRegisterPairHigh<Register>());
2144 return;
2145 case kCondLE:
2146 case kCondGT:
2147 default:
2148 break;
2149 }
2150 }
2151 }
2152
2153 if ((condition == kCondEQ || condition == kCondNE) &&
2154 // If `out` is a low register, then the GenerateConditionGeneric()
2155 // function generates a shorter code sequence that is still branchless.
2156 (!ArmAssembler::IsLowRegister(out) || !CanGenerateTest(cond, codegen->GetAssembler()))) {
2157 GenerateEqualLong(cond, codegen);
2158 return;
2159 }
2160
2161 if (CanGenerateTest(cond, codegen->GetAssembler())) {
2162 GenerateConditionGeneric(cond, codegen);
2163 return;
2164 }
2165
2166 // Convert the jumps into the result.
2167 Label done_label;
2168 Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
2169 Label true_label, false_label;
2170
2171 GenerateLongComparesAndJumps(cond, &true_label, &false_label, codegen);
2172
2173 // False case: result = 0.
2174 __ Bind(&false_label);
2175 __ mov(out, ShifterOperand(0));
2176 __ b(final_label);
2177
2178 // True case: result = 1.
2179 __ Bind(&true_label);
2180 __ mov(out, ShifterOperand(1));
2181
2182 if (done_label.IsLinked()) {
2183 __ Bind(&done_label);
2184 }
2185}
2186
2187static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond, CodeGeneratorARM* codegen) {
2188 const Primitive::Type type = cond->GetLeft()->GetType();
2189
2190 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
2191
2192 if (type == Primitive::kPrimLong) {
2193 GenerateConditionLong(cond, codegen);
2194 return;
2195 }
2196
2197 const LocationSummary* const locations = cond->GetLocations();
2198 IfCondition condition = cond->GetCondition();
2199 Register in = locations->InAt(0).AsRegister<Register>();
2200 const Register out = locations->Out().AsRegister<Register>();
2201 const Location right = cond->GetLocations()->InAt(1);
2202 int64_t value;
2203
2204 if (right.IsConstant()) {
2205 IfCondition opposite = cond->GetOppositeCondition();
2206
2207 value = AdjustConstantForCondition(Int64FromConstant(right.GetConstant()),
2208 &condition,
2209 &opposite);
2210
2211 // Comparisons against 0 are common enough to deserve special attention.
2212 if (value == 0) {
2213 switch (condition) {
2214 case kCondNE:
2215 case kCondA:
2216 if (ArmAssembler::IsLowRegister(out) && out == in) {
2217 __ cmp(out, ShifterOperand(0));
2218 __ it(NE);
2219 __ mov(out, ShifterOperand(1), NE);
2220 return;
2221 }
2222
2223 FALLTHROUGH_INTENDED;
2224 case kCondEQ:
2225 case kCondBE:
2226 case kCondLT:
2227 case kCondGE:
2228 case kCondAE:
2229 case kCondB:
2230 codegen->GenerateConditionWithZero(condition, out, in);
2231 return;
2232 case kCondLE:
2233 case kCondGT:
2234 default:
2235 break;
2236 }
2237 }
2238 }
2239
2240 if (condition == kCondEQ || condition == kCondNE) {
2241 ShifterOperand operand;
2242
2243 if (right.IsConstant()) {
2244 operand = ShifterOperand(value);
2245 } else if (out == right.AsRegister<Register>()) {
2246 // Avoid 32-bit instructions if possible.
2247 operand = ShifterOperand(in);
2248 in = right.AsRegister<Register>();
2249 } else {
2250 operand = ShifterOperand(right.AsRegister<Register>());
2251 }
2252
2253 if (condition == kCondNE && ArmAssembler::IsLowRegister(out)) {
2254 __ subs(out, in, operand);
2255 __ it(NE);
2256 __ mov(out, ShifterOperand(1), NE);
2257 } else {
2258 __ sub(out, in, operand);
2259 codegen->GenerateConditionWithZero(condition, out, out);
2260 }
2261
2262 return;
2263 }
2264
2265 GenerateConditionGeneric(cond, codegen);
2266}
2267
Donghui Bai426b49c2016-11-08 14:55:38 +08002268static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
2269 const Primitive::Type type = constant->GetType();
2270 bool ret = false;
2271
2272 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
2273
2274 if (type == Primitive::kPrimLong) {
2275 const uint64_t value = constant->AsLongConstant()->GetValueAsUint64();
2276
2277 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
2278 } else {
2279 ret = IsUint<8>(CodeGenerator::GetInt32ValueOf(constant));
2280 }
2281
2282 return ret;
2283}
2284
2285static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
2286 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
2287
2288 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
2289 return Location::ConstantLocation(constant->AsConstant());
2290 }
2291
2292 return Location::RequiresRegister();
2293}
2294
2295static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
2296 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2297 // we check that we are not dealing with floating-point output (there is no
2298 // 16-bit VMOV encoding).
2299 if (!out.IsRegister() && !out.IsRegisterPair()) {
2300 return false;
2301 }
2302
2303 // For constants, we also check that the output is in one or two low registers,
2304 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
2305 // MOV encoding can be used.
2306 if (src.IsConstant()) {
2307 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
2308 return false;
2309 }
2310
2311 if (out.IsRegister()) {
2312 if (!ArmAssembler::IsLowRegister(out.AsRegister<Register>())) {
2313 return false;
2314 }
2315 } else {
2316 DCHECK(out.IsRegisterPair());
2317
2318 if (!ArmAssembler::IsLowRegister(out.AsRegisterPairHigh<Register>())) {
2319 return false;
2320 }
2321 }
2322 }
2323
2324 return true;
2325}
2326
Anton Kirilov74234da2017-01-13 14:42:47 +00002327#undef __
2328// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
2329#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
2330
Donghui Bai426b49c2016-11-08 14:55:38 +08002331Label* CodeGeneratorARM::GetFinalLabel(HInstruction* instruction, Label* final_label) {
2332 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00002333 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08002334
2335 const HBasicBlock* const block = instruction->GetBlock();
2336 const HLoopInformation* const info = block->GetLoopInformation();
2337 HInstruction* const next = instruction->GetNext();
2338
2339 // Avoid a branch to a branch.
2340 if (next->IsGoto() && (info == nullptr ||
2341 !info->IsBackEdge(*block) ||
2342 !info->HasSuspendCheck())) {
2343 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
2344 }
2345
2346 return final_label;
2347}
2348
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002349void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01002350 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002351}
2352
2353void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01002354 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002355}
2356
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002357size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
2358 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
2359 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01002360}
2361
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002362size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
2363 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
2364 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01002365}
2366
Nicolas Geoffray840e5462015-01-07 16:01:24 +00002367size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
2368 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
2369 return kArmWordSize;
2370}
2371
2372size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
2373 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
2374 return kArmWordSize;
2375}
2376
Calin Juravle34166012014-12-19 17:22:29 +00002377CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002378 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01002379 const CompilerOptions& compiler_options,
2380 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002381 : CodeGenerator(graph,
2382 kNumberOfCoreRegisters,
2383 kNumberOfSRegisters,
2384 kNumberOfRegisterPairs,
2385 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
2386 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00002387 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
2388 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01002389 compiler_options,
2390 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01002391 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002392 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002393 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01002394 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01002395 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00002396 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002397 uint32_literals_(std::less<uint32_t>(),
2398 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002399 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01002400 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002401 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00002402 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01002403 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01002404 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00002405 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00002406 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2407 jit_class_patches_(TypeReferenceValueComparator(),
2408 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07002409 // Always save the LR register to mimic Quick.
2410 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01002411}
2412
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002413void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
2414 // Ensure that we fix up branches and literal loads and emit the literal pool.
2415 __ FinalizeCode();
2416
2417 // Adjust native pc offsets in stack maps.
2418 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08002419 uint32_t old_position =
2420 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002421 uint32_t new_position = __ GetAdjustedPosition(old_position);
2422 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
2423 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01002424 // Adjust pc offsets for the disassembly information.
2425 if (disasm_info_ != nullptr) {
2426 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
2427 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
2428 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
2429 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
2430 it.second.start = __ GetAdjustedPosition(it.second.start);
2431 it.second.end = __ GetAdjustedPosition(it.second.end);
2432 }
2433 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
2434 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
2435 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
2436 }
2437 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002438
2439 CodeGenerator::Finalize(allocator);
2440}
2441
David Brazdil58282f42016-01-14 12:45:10 +00002442void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002443 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002444 blocked_core_registers_[SP] = true;
2445 blocked_core_registers_[LR] = true;
2446 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002447
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002448 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002449 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002450
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002451 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002452 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002453
David Brazdil58282f42016-01-14 12:45:10 +00002454 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01002455 // Stubs do not save callee-save floating point registers. If the graph
2456 // is debuggable, we need to deal with these registers differently. For
2457 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002458 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
2459 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
2460 }
2461 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002462}
2463
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002464InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002465 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002466 assembler_(codegen->GetAssembler()),
2467 codegen_(codegen) {}
2468
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002469void CodeGeneratorARM::ComputeSpillMask() {
2470 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2471 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00002472 // There is no easy instruction to restore just the PC on thumb2. We spill and
2473 // restore another arbitrary register.
2474 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002475 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2476 // We use vpush and vpop for saving and restoring floating point registers, which take
2477 // a SRegister and the number of registers to save/restore after that SRegister. We
2478 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2479 // but in the range.
2480 if (fpu_spill_mask_ != 0) {
2481 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2482 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2483 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2484 fpu_spill_mask_ |= (1 << i);
2485 }
2486 }
2487}
2488
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002489static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002490 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002491}
2492
2493static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002494 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002495}
2496
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002497void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00002498 bool skip_overflow_check =
2499 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002500 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002501 __ Bind(&frame_entry_label_);
2502
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002503 if (HasEmptyFrame()) {
2504 return;
2505 }
2506
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002507 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002508 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
2509 __ LoadFromOffset(kLoadWord, IP, IP, 0);
2510 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002511 }
2512
Andreas Gampe501fd632015-09-10 16:11:06 -07002513 __ PushList(core_spill_mask_);
2514 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2515 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002516 if (fpu_spill_mask_ != 0) {
2517 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2518 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002519 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01002520 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002521 }
Mingyao Yang063fc772016-08-02 11:02:54 -07002522
2523 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2524 // Initialize should_deoptimize flag to 0.
2525 __ mov(IP, ShifterOperand(0));
2526 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
2527 }
2528
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002529 int adjust = GetFrameSize() - FrameEntrySpillSize();
2530 __ AddConstant(SP, -adjust);
2531 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002532
2533 // Save the current method if we need it. Note that we do not
2534 // do this in HCurrentMethod, as the instruction might have been removed
2535 // in the SSA graph.
2536 if (RequiresCurrentMethod()) {
2537 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
2538 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002539}
2540
2541void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002542 if (HasEmptyFrame()) {
2543 __ bx(LR);
2544 return;
2545 }
David Srbeckyc34dc932015-04-12 09:27:43 +01002546 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002547 int adjust = GetFrameSize() - FrameEntrySpillSize();
2548 __ AddConstant(SP, adjust);
2549 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002550 if (fpu_spill_mask_ != 0) {
2551 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2552 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07002553 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002554 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002555 }
Andreas Gampe501fd632015-09-10 16:11:06 -07002556 // Pop LR into PC to return.
2557 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
2558 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
2559 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01002560 __ cfi().RestoreState();
2561 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002562}
2563
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002564void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07002565 Label* label = GetLabelOf(block);
2566 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002567}
2568
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002569Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002570 switch (type) {
2571 case Primitive::kPrimBoolean:
2572 case Primitive::kPrimByte:
2573 case Primitive::kPrimChar:
2574 case Primitive::kPrimShort:
2575 case Primitive::kPrimInt:
2576 case Primitive::kPrimNot: {
2577 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002578 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002579 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002580 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002581 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002582 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002583 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002584 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002585
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002586 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002587 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002588 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002589 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002590 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002591 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002592 if (calling_convention.GetRegisterAt(index) == R1) {
2593 // Skip R1, and use R2_R3 instead.
2594 gp_index_++;
2595 index++;
2596 }
2597 }
2598 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2599 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002600 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01002601
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002602 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002603 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002604 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002605 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2606 }
2607 }
2608
2609 case Primitive::kPrimFloat: {
2610 uint32_t stack_index = stack_index_++;
2611 if (float_index_ % 2 == 0) {
2612 float_index_ = std::max(double_index_, float_index_);
2613 }
2614 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2615 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
2616 } else {
2617 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2618 }
2619 }
2620
2621 case Primitive::kPrimDouble: {
2622 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2623 uint32_t stack_index = stack_index_;
2624 stack_index_ += 2;
2625 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2626 uint32_t index = double_index_;
2627 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002628 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002629 calling_convention.GetFpuRegisterAt(index),
2630 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002631 DCHECK(ExpectedPairLayout(result));
2632 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002633 } else {
2634 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002635 }
2636 }
2637
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002638 case Primitive::kPrimVoid:
2639 LOG(FATAL) << "Unexpected parameter type " << type;
2640 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002641 }
Roland Levillain3b359c72015-11-17 19:35:12 +00002642 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002643}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002644
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002645Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002646 switch (type) {
2647 case Primitive::kPrimBoolean:
2648 case Primitive::kPrimByte:
2649 case Primitive::kPrimChar:
2650 case Primitive::kPrimShort:
2651 case Primitive::kPrimInt:
2652 case Primitive::kPrimNot: {
2653 return Location::RegisterLocation(R0);
2654 }
2655
2656 case Primitive::kPrimFloat: {
2657 return Location::FpuRegisterLocation(S0);
2658 }
2659
2660 case Primitive::kPrimLong: {
2661 return Location::RegisterPairLocation(R0, R1);
2662 }
2663
2664 case Primitive::kPrimDouble: {
2665 return Location::FpuRegisterPairLocation(S0, S1);
2666 }
2667
2668 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00002669 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002670 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002671
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002672 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002673}
2674
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002675Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
2676 return Location::RegisterLocation(kMethodRegisterArgument);
2677}
2678
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002679void CodeGeneratorARM::Move32(Location destination, Location source) {
2680 if (source.Equals(destination)) {
2681 return;
2682 }
2683 if (destination.IsRegister()) {
2684 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002685 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002686 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002687 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002688 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002689 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002690 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002691 } else if (destination.IsFpuRegister()) {
2692 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002693 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002694 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002695 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002696 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002697 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002698 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002699 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002700 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002701 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002702 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002703 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002704 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002705 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002706 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002707 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
2708 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002709 }
2710 }
2711}
2712
2713void CodeGeneratorARM::Move64(Location destination, Location source) {
2714 if (source.Equals(destination)) {
2715 return;
2716 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002717 if (destination.IsRegisterPair()) {
2718 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002719 EmitParallelMoves(
2720 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
2721 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002722 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002723 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002724 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
2725 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002726 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002727 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002728 } else if (source.IsFpuRegisterPair()) {
2729 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
2730 destination.AsRegisterPairHigh<Register>(),
2731 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002732 } else {
2733 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002734 DCHECK(ExpectedPairLayout(destination));
2735 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
2736 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002737 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002738 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002739 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002740 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2741 SP,
2742 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01002743 } else if (source.IsRegisterPair()) {
2744 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2745 source.AsRegisterPairLow<Register>(),
2746 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002747 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002748 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002749 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002750 } else {
2751 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002752 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002753 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002754 if (source.AsRegisterPairLow<Register>() == R1) {
2755 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002756 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
2757 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002758 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002759 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002760 SP, destination.GetStackIndex());
2761 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002762 } else if (source.IsFpuRegisterPair()) {
2763 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
2764 SP,
2765 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002766 } else {
2767 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002768 EmitParallelMoves(
2769 Location::StackSlot(source.GetStackIndex()),
2770 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002771 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002772 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002773 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
2774 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002775 }
2776 }
2777}
2778
Calin Juravle175dc732015-08-25 15:42:32 +01002779void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
2780 DCHECK(location.IsRegister());
2781 __ LoadImmediate(location.AsRegister<Register>(), value);
2782}
2783
Calin Juravlee460d1d2015-09-29 04:52:17 +01002784void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002785 HParallelMove move(GetGraph()->GetArena());
2786 move.AddMove(src, dst, dst_type, nullptr);
2787 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002788}
2789
2790void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
2791 if (location.IsRegister()) {
2792 locations->AddTemp(location);
2793 } else if (location.IsRegisterPair()) {
2794 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
2795 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
2796 } else {
2797 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2798 }
2799}
2800
Calin Juravle175dc732015-08-25 15:42:32 +01002801void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
2802 HInstruction* instruction,
2803 uint32_t dex_pc,
2804 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002805 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002806 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002807 if (EntrypointRequiresStackMap(entrypoint)) {
2808 RecordPcInfo(instruction, dex_pc, slow_path);
2809 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002810}
2811
Roland Levillaindec8f632016-07-22 17:10:06 +01002812void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2813 HInstruction* instruction,
2814 SlowPathCode* slow_path) {
2815 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002816 GenerateInvokeRuntime(entry_point_offset);
2817}
2818
2819void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01002820 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2821 __ blx(LR);
2822}
2823
David Brazdilfc6a86a2015-06-26 10:33:45 +00002824void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002825 DCHECK(!successor->IsExitBlock());
2826
2827 HBasicBlock* block = got->GetBlock();
2828 HInstruction* previous = got->GetPrevious();
2829
2830 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00002831 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002832 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2833 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2834 return;
2835 }
2836
2837 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2838 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2839 }
2840 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002841 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002842 }
2843}
2844
David Brazdilfc6a86a2015-06-26 10:33:45 +00002845void LocationsBuilderARM::VisitGoto(HGoto* got) {
2846 got->SetLocations(nullptr);
2847}
2848
2849void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
2850 HandleGoto(got, got->GetSuccessor());
2851}
2852
2853void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2854 try_boundary->SetLocations(nullptr);
2855}
2856
2857void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2858 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2859 if (!successor->IsExitBlock()) {
2860 HandleGoto(try_boundary, successor);
2861 }
2862}
2863
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002864void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002865 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002866}
2867
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002868void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002869}
2870
David Brazdil0debae72015-11-12 18:37:00 +00002871void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
2872 Label* true_target_in,
2873 Label* false_target_in) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002874 if (CanGenerateTest(condition, codegen_->GetAssembler())) {
2875 Label* non_fallthrough_target;
2876 bool invert;
2877
2878 if (true_target_in == nullptr) {
2879 DCHECK(false_target_in != nullptr);
2880 non_fallthrough_target = false_target_in;
2881 invert = true;
2882 } else {
2883 non_fallthrough_target = true_target_in;
2884 invert = false;
2885 }
2886
2887 const auto cond = GenerateTest(condition, invert, codegen_);
2888
2889 __ b(non_fallthrough_target, cond.first);
2890
2891 if (false_target_in != nullptr && false_target_in != non_fallthrough_target) {
2892 __ b(false_target_in);
2893 }
2894
2895 return;
2896 }
2897
David Brazdil0debae72015-11-12 18:37:00 +00002898 // Generated branching requires both targets to be explicit. If either of the
2899 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2900 Label fallthrough_target;
2901 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
2902 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
2903
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002904 DCHECK_EQ(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002905 GenerateLongComparesAndJumps(condition, true_target, false_target, codegen_);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002906
David Brazdil0debae72015-11-12 18:37:00 +00002907 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002908 __ b(false_target);
2909 }
David Brazdil0debae72015-11-12 18:37:00 +00002910
2911 if (fallthrough_target.IsLinked()) {
2912 __ Bind(&fallthrough_target);
2913 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002914}
2915
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002916void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002917 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002918 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002919 Label* false_target) {
2920 HInstruction* cond = instruction->InputAt(condition_input_index);
2921
2922 if (true_target == nullptr && false_target == nullptr) {
2923 // Nothing to do. The code always falls through.
2924 return;
2925 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002926 // Constant condition, statically compared against "true" (integer value 1).
2927 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002928 if (true_target != nullptr) {
2929 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002930 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002931 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002932 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002933 if (false_target != nullptr) {
2934 __ b(false_target);
2935 }
2936 }
2937 return;
2938 }
2939
2940 // The following code generates these patterns:
2941 // (1) true_target == nullptr && false_target != nullptr
2942 // - opposite condition true => branch to false_target
2943 // (2) true_target != nullptr && false_target == nullptr
2944 // - condition true => branch to true_target
2945 // (3) true_target != nullptr && false_target != nullptr
2946 // - condition true => branch to true_target
2947 // - branch to false_target
2948 if (IsBooleanValueOrMaterializedCondition(cond)) {
2949 // Condition has been materialized, compare the output to 0.
2950 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2951 DCHECK(cond_val.IsRegister());
2952 if (true_target == nullptr) {
2953 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
2954 } else {
2955 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002956 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002957 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002958 // Condition has not been materialized. Use its inputs as the comparison and
2959 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002960 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002961
2962 // If this is a long or FP comparison that has been folded into
2963 // the HCondition, generate the comparison directly.
2964 Primitive::Type type = condition->InputAt(0)->GetType();
2965 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2966 GenerateCompareTestAndBranch(condition, true_target, false_target);
2967 return;
2968 }
2969
Donghui Bai426b49c2016-11-08 14:55:38 +08002970 Label* non_fallthrough_target;
2971 Condition arm_cond;
David Brazdil0debae72015-11-12 18:37:00 +00002972 LocationSummary* locations = cond->GetLocations();
2973 DCHECK(locations->InAt(0).IsRegister());
2974 Register left = locations->InAt(0).AsRegister<Register>();
2975 Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08002976
David Brazdil0debae72015-11-12 18:37:00 +00002977 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002978 arm_cond = ARMCondition(condition->GetOppositeCondition());
2979 non_fallthrough_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002980 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002981 arm_cond = ARMCondition(condition->GetCondition());
2982 non_fallthrough_target = true_target;
2983 }
2984
2985 if (right.IsConstant() && (arm_cond == NE || arm_cond == EQ) &&
2986 CodeGenerator::GetInt32ValueOf(right.GetConstant()) == 0) {
2987 if (arm_cond == EQ) {
2988 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2989 } else {
2990 DCHECK_EQ(arm_cond, NE);
2991 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2992 }
2993 } else {
2994 if (right.IsRegister()) {
2995 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
2996 } else {
2997 DCHECK(right.IsConstant());
2998 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2999 }
3000
3001 __ b(non_fallthrough_target, arm_cond);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01003002 }
Dave Allison20dfc792014-06-16 20:44:29 -07003003 }
David Brazdil0debae72015-11-12 18:37:00 +00003004
3005 // If neither branch falls through (case 3), the conditional branch to `true_target`
3006 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3007 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003008 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003009 }
3010}
3011
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003012void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003013 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
3014 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003015 locations->SetInAt(0, Location::RequiresRegister());
3016 }
3017}
3018
3019void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003020 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3021 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3022 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3023 nullptr : codegen_->GetLabelOf(true_successor);
3024 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3025 nullptr : codegen_->GetLabelOf(false_successor);
3026 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003027}
3028
3029void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
3030 LocationSummary* locations = new (GetGraph()->GetArena())
3031 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003032 InvokeRuntimeCallingConvention calling_convention;
3033 RegisterSet caller_saves = RegisterSet::Empty();
3034 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3035 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003036 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003037 locations->SetInAt(0, Location::RequiresRegister());
3038 }
3039}
3040
3041void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003042 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003043 GenerateTestAndBranch(deoptimize,
3044 /* condition_input_index */ 0,
3045 slow_path->GetEntryLabel(),
3046 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003047}
Dave Allison20dfc792014-06-16 20:44:29 -07003048
Mingyao Yang063fc772016-08-02 11:02:54 -07003049void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3050 LocationSummary* locations = new (GetGraph()->GetArena())
3051 LocationSummary(flag, LocationSummary::kNoCall);
3052 locations->SetOut(Location::RequiresRegister());
3053}
3054
3055void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3056 __ LoadFromOffset(kLoadWord,
3057 flag->GetLocations()->Out().AsRegister<Register>(),
3058 SP,
3059 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
3060}
3061
David Brazdil74eb1b22015-12-14 11:44:01 +00003062void LocationsBuilderARM::VisitSelect(HSelect* select) {
3063 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08003064 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
3065
3066 if (is_floating_point) {
David Brazdil74eb1b22015-12-14 11:44:01 +00003067 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003068 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00003069 } else {
3070 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003071 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00003072 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003073
David Brazdil74eb1b22015-12-14 11:44:01 +00003074 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003075 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
3076 // The code generator handles overlap with the values, but not with the condition.
3077 locations->SetOut(Location::SameAsFirstInput());
3078 } else if (is_floating_point) {
3079 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3080 } else {
3081 if (!locations->InAt(1).IsConstant()) {
3082 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
3083 }
3084
3085 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003086 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003087}
3088
3089void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003090 HInstruction* const condition = select->GetCondition();
3091 const LocationSummary* const locations = select->GetLocations();
3092 const Primitive::Type type = select->GetType();
3093 const Location first = locations->InAt(0);
3094 const Location out = locations->Out();
3095 const Location second = locations->InAt(1);
3096 Location src;
3097
3098 if (condition->IsIntConstant()) {
3099 if (condition->AsIntConstant()->IsFalse()) {
3100 src = first;
3101 } else {
3102 src = second;
3103 }
3104
3105 codegen_->MoveLocation(out, src, type);
3106 return;
3107 }
3108
3109 if (!Primitive::IsFloatingPointType(type) &&
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003110 (IsBooleanValueOrMaterializedCondition(condition) ||
3111 CanGenerateTest(condition->AsCondition(), codegen_->GetAssembler()))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003112 bool invert = false;
3113
3114 if (out.Equals(second)) {
3115 src = first;
3116 invert = true;
3117 } else if (out.Equals(first)) {
3118 src = second;
3119 } else if (second.IsConstant()) {
3120 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
3121 src = second;
3122 } else if (first.IsConstant()) {
3123 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
3124 src = first;
3125 invert = true;
3126 } else {
3127 src = second;
3128 }
3129
3130 if (CanGenerateConditionalMove(out, src)) {
3131 if (!out.Equals(first) && !out.Equals(second)) {
3132 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3133 }
3134
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003135 std::pair<Condition, Condition> cond;
3136
3137 if (IsBooleanValueOrMaterializedCondition(condition)) {
3138 __ CmpConstant(locations->InAt(2).AsRegister<Register>(), 0);
3139 cond = invert ? std::make_pair(EQ, NE) : std::make_pair(NE, EQ);
3140 } else {
3141 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3142 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003143
3144 if (out.IsRegister()) {
3145 ShifterOperand operand;
3146
3147 if (src.IsConstant()) {
3148 operand = ShifterOperand(CodeGenerator::GetInt32ValueOf(src.GetConstant()));
3149 } else {
3150 DCHECK(src.IsRegister());
3151 operand = ShifterOperand(src.AsRegister<Register>());
3152 }
3153
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003154 __ it(cond.first);
3155 __ mov(out.AsRegister<Register>(), operand, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08003156 } else {
3157 DCHECK(out.IsRegisterPair());
3158
3159 ShifterOperand operand_high;
3160 ShifterOperand operand_low;
3161
3162 if (src.IsConstant()) {
3163 const int64_t value = src.GetConstant()->AsLongConstant()->GetValue();
3164
3165 operand_high = ShifterOperand(High32Bits(value));
3166 operand_low = ShifterOperand(Low32Bits(value));
3167 } else {
3168 DCHECK(src.IsRegisterPair());
3169 operand_high = ShifterOperand(src.AsRegisterPairHigh<Register>());
3170 operand_low = ShifterOperand(src.AsRegisterPairLow<Register>());
3171 }
3172
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003173 __ it(cond.first);
3174 __ mov(out.AsRegisterPairLow<Register>(), operand_low, cond.first);
3175 __ it(cond.first);
3176 __ mov(out.AsRegisterPairHigh<Register>(), operand_high, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08003177 }
3178
3179 return;
3180 }
3181 }
3182
3183 Label* false_target = nullptr;
3184 Label* true_target = nullptr;
3185 Label select_end;
3186 Label* target = codegen_->GetFinalLabel(select, &select_end);
3187
3188 if (out.Equals(second)) {
3189 true_target = target;
3190 src = first;
3191 } else {
3192 false_target = target;
3193 src = second;
3194
3195 if (!out.Equals(first)) {
3196 codegen_->MoveLocation(out, first, type);
3197 }
3198 }
3199
3200 GenerateTestAndBranch(select, 2, true_target, false_target);
3201 codegen_->MoveLocation(out, src, type);
3202
3203 if (select_end.IsLinked()) {
3204 __ Bind(&select_end);
3205 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003206}
3207
David Srbecky0cf44932015-12-09 14:09:59 +00003208void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3209 new (GetGraph()->GetArena()) LocationSummary(info);
3210}
3211
David Srbeckyd28f4a02016-03-14 17:14:24 +00003212void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
3213 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003214}
3215
3216void CodeGeneratorARM::GenerateNop() {
3217 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003218}
3219
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003220// `temp` is an extra temporary register that is used for some conditions;
3221// callers may not specify it, in which case the method will use a scratch
3222// register instead.
3223void CodeGeneratorARM::GenerateConditionWithZero(IfCondition condition,
3224 Register out,
3225 Register in,
3226 Register temp) {
3227 switch (condition) {
3228 case kCondEQ:
3229 // x <= 0 iff x == 0 when the comparison is unsigned.
3230 case kCondBE:
3231 if (temp == kNoRegister || (ArmAssembler::IsLowRegister(out) && out != in)) {
3232 temp = out;
3233 }
3234
3235 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3236 // different as well.
3237 if (ArmAssembler::IsLowRegister(in) && ArmAssembler::IsLowRegister(temp) && in != temp) {
3238 // temp = - in; only 0 sets the carry flag.
3239 __ rsbs(temp, in, ShifterOperand(0));
3240
3241 if (out == in) {
3242 std::swap(in, temp);
3243 }
3244
3245 // out = - in + in + carry = carry
3246 __ adc(out, temp, ShifterOperand(in));
3247 } else {
3248 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3249 __ clz(out, in);
3250 // Any number less than 32 logically shifted right by 5 bits results in 0;
3251 // the same operation on 32 yields 1.
3252 __ Lsr(out, out, 5);
3253 }
3254
3255 break;
3256 case kCondNE:
3257 // x > 0 iff x != 0 when the comparison is unsigned.
3258 case kCondA:
3259 if (out == in) {
3260 if (temp == kNoRegister || in == temp) {
3261 temp = IP;
3262 }
3263 } else if (temp == kNoRegister || !ArmAssembler::IsLowRegister(temp)) {
3264 temp = out;
3265 }
3266
3267 // temp = in - 1; only 0 does not set the carry flag.
3268 __ subs(temp, in, ShifterOperand(1));
3269 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3270 __ sbc(out, in, ShifterOperand(temp));
3271 break;
3272 case kCondGE:
3273 __ mvn(out, ShifterOperand(in));
3274 in = out;
3275 FALLTHROUGH_INTENDED;
3276 case kCondLT:
3277 // We only care about the sign bit.
3278 __ Lsr(out, in, 31);
3279 break;
3280 case kCondAE:
3281 // Trivially true.
3282 __ mov(out, ShifterOperand(1));
3283 break;
3284 case kCondB:
3285 // Trivially false.
3286 __ mov(out, ShifterOperand(0));
3287 break;
3288 default:
3289 LOG(FATAL) << "Unexpected condition " << condition;
3290 UNREACHABLE();
3291 }
3292}
3293
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003294void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003295 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01003296 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003297 // Handle the long/FP comparisons made in instruction simplification.
3298 switch (cond->InputAt(0)->GetType()) {
3299 case Primitive::kPrimLong:
3300 locations->SetInAt(0, Location::RequiresRegister());
3301 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00003302 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003303 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003304 }
3305 break;
3306
3307 case Primitive::kPrimFloat:
3308 case Primitive::kPrimDouble:
3309 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003310 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00003311 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01003312 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3313 }
3314 break;
3315
3316 default:
3317 locations->SetInAt(0, Location::RequiresRegister());
3318 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00003319 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01003320 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3321 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003322 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003323}
3324
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003325void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003326 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01003327 return;
Dave Allison20dfc792014-06-16 20:44:29 -07003328 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01003329
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003330 const Primitive::Type type = cond->GetLeft()->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003331
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003332 if (Primitive::IsFloatingPointType(type)) {
3333 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003334 return;
Roland Levillain4fa13f62015-07-06 18:11:54 +01003335 }
3336
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003337 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
Roland Levillain4fa13f62015-07-06 18:11:54 +01003338
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003339 const IfCondition condition = cond->GetCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003340
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003341 // A condition with only one boolean input, or two boolean inputs without being equality or
3342 // inequality results from transformations done by the instruction simplifier, and is handled
3343 // as a regular condition with integral inputs.
3344 if (type == Primitive::kPrimBoolean &&
3345 cond->GetRight()->GetType() == Primitive::kPrimBoolean &&
3346 (condition == kCondEQ || condition == kCondNE)) {
3347 const LocationSummary* const locations = cond->GetLocations();
3348 Register left = locations->InAt(0).AsRegister<Register>();
3349 const Register out = locations->Out().AsRegister<Register>();
3350 const Location right_loc = locations->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003351
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003352 // The constant case is handled by the instruction simplifier.
3353 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003354
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003355 Register right = right_loc.AsRegister<Register>();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003356
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003357 // Avoid 32-bit instructions if possible.
3358 if (out == right) {
3359 std::swap(left, right);
3360 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003361
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003362 __ eor(out, left, ShifterOperand(right));
3363
3364 if (condition == kCondEQ) {
3365 __ eor(out, out, ShifterOperand(1));
3366 }
3367
3368 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003369 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003370
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003371 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Dave Allison20dfc792014-06-16 20:44:29 -07003372}
3373
3374void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003375 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003376}
3377
3378void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003379 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003380}
3381
3382void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003383 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003384}
3385
3386void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003387 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003388}
3389
3390void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003391 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003392}
3393
3394void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003395 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003396}
3397
3398void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003399 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003400}
3401
3402void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003403 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003404}
3405
3406void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003407 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003408}
3409
3410void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003411 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003412}
3413
3414void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003415 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003416}
3417
3418void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003419 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003420}
3421
Aart Bike9f37602015-10-09 11:15:55 -07003422void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003423 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003424}
3425
3426void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003427 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003428}
3429
3430void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003431 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003432}
3433
3434void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003435 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003436}
3437
3438void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003439 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003440}
3441
3442void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003443 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003444}
3445
3446void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003447 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003448}
3449
3450void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003451 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003452}
3453
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003454void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003455 LocationSummary* locations =
3456 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003457 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003458}
3459
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003460void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01003461 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003462}
3463
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003464void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
3465 LocationSummary* locations =
3466 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3467 locations->SetOut(Location::ConstantLocation(constant));
3468}
3469
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003470void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003471 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003472}
3473
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003474void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003475 LocationSummary* locations =
3476 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003477 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003478}
3479
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003480void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003481 // Will be generated at use site.
3482}
3483
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003484void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
3485 LocationSummary* locations =
3486 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3487 locations->SetOut(Location::ConstantLocation(constant));
3488}
3489
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003490void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003491 // Will be generated at use site.
3492}
3493
3494void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
3495 LocationSummary* locations =
3496 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3497 locations->SetOut(Location::ConstantLocation(constant));
3498}
3499
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003500void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003501 // Will be generated at use site.
3502}
3503
Igor Murashkind01745e2017-04-05 16:40:31 -07003504void LocationsBuilderARM::VisitConstructorFence(HConstructorFence* constructor_fence) {
3505 constructor_fence->SetLocations(nullptr);
3506}
3507
3508void InstructionCodeGeneratorARM::VisitConstructorFence(
3509 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3510 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3511}
3512
Calin Juravle27df7582015-04-17 19:12:31 +01003513void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3514 memory_barrier->SetLocations(nullptr);
3515}
3516
3517void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00003518 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01003519}
3520
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003521void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003522 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003523}
3524
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003525void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003526 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003527}
3528
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003529void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003530 LocationSummary* locations =
3531 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003532 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003533}
3534
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003535void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003536 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003537}
3538
Calin Juravle175dc732015-08-25 15:42:32 +01003539void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3540 // The trampoline uses the same calling convention as dex calling conventions,
3541 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3542 // the method_idx.
3543 HandleInvoke(invoke);
3544}
3545
3546void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3547 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3548}
3549
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003550void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003551 // Explicit clinit checks triggered by static invokes must have been pruned by
3552 // art::PrepareForRegisterAllocation.
3553 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003554
Vladimir Marko68c981f2016-08-26 13:13:33 +01003555 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003556 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00003557 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3558 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3559 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003560 return;
3561 }
3562
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003563 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00003564
3565 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3566 if (invoke->HasPcRelativeDexCache()) {
3567 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3568 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003569}
3570
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003571static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
3572 if (invoke->GetLocations()->Intrinsified()) {
3573 IntrinsicCodeGeneratorARM intrinsic(codegen);
3574 intrinsic.Dispatch(invoke);
3575 return true;
3576 }
3577 return false;
3578}
3579
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003580void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003581 // Explicit clinit checks triggered by static invokes must have been pruned by
3582 // art::PrepareForRegisterAllocation.
3583 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003584
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003585 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3586 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003587 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003588
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003589 LocationSummary* locations = invoke->GetLocations();
3590 codegen_->GenerateStaticOrDirectCall(
3591 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003592 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003593}
3594
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003595void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003596 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003597 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003598}
3599
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003600void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01003601 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003602 if (intrinsic.TryDispatch(invoke)) {
3603 return;
3604 }
3605
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003606 HandleInvoke(invoke);
3607}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003608
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003609void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003610 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3611 return;
3612 }
3613
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003614 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003615 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003616 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003617}
3618
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003619void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3620 HandleInvoke(invoke);
3621 // Add the hidden argument.
3622 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
3623}
3624
3625void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3626 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00003627 LocationSummary* locations = invoke->GetLocations();
3628 Register temp = locations->GetTemp(0).AsRegister<Register>();
3629 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003630 Location receiver = locations->InAt(0);
3631 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3632
Roland Levillain3b359c72015-11-17 19:35:12 +00003633 // Set the hidden argument. This is safe to do this here, as R12
3634 // won't be modified thereafter, before the `blx` (call) instruction.
3635 DCHECK_EQ(R12, hidden_reg);
3636 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003637
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003638 if (receiver.IsStackSlot()) {
3639 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00003640 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003641 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3642 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00003643 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00003644 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003645 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003646 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00003647 // Instead of simply (possibly) unpoisoning `temp` here, we should
3648 // emit a read barrier for the previous class reference load.
3649 // However this is not required in practice, as this is an
3650 // intermediate/temporary reference and because the current
3651 // concurrent copying collector keeps the from-space memory
3652 // intact/accessible until the end of the marking phase (the
3653 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003654 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003655 __ LoadFromOffset(kLoadWord, temp, temp,
3656 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3657 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003658 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003659 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003660 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00003661 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07003662 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003663 // LR = temp->GetEntryPoint();
3664 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
3665 // LR();
3666 __ blx(LR);
3667 DCHECK(!codegen_->IsLeafMethod());
3668 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3669}
3670
Orion Hodsonac141392017-01-13 11:53:47 +00003671void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3672 HandleInvoke(invoke);
3673}
3674
3675void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3676 codegen_->GenerateInvokePolymorphicCall(invoke);
3677}
3678
Roland Levillain88cb1752014-10-20 16:36:47 +01003679void LocationsBuilderARM::VisitNeg(HNeg* neg) {
3680 LocationSummary* locations =
3681 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3682 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003683 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01003684 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003685 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3686 break;
3687 }
3688 case Primitive::kPrimLong: {
3689 locations->SetInAt(0, Location::RequiresRegister());
3690 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003691 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003692 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003693
Roland Levillain88cb1752014-10-20 16:36:47 +01003694 case Primitive::kPrimFloat:
3695 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003696 locations->SetInAt(0, Location::RequiresFpuRegister());
3697 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003698 break;
3699
3700 default:
3701 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3702 }
3703}
3704
3705void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
3706 LocationSummary* locations = neg->GetLocations();
3707 Location out = locations->Out();
3708 Location in = locations->InAt(0);
3709 switch (neg->GetResultType()) {
3710 case Primitive::kPrimInt:
3711 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003712 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01003713 break;
3714
3715 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003716 DCHECK(in.IsRegisterPair());
3717 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3718 __ rsbs(out.AsRegisterPairLow<Register>(),
3719 in.AsRegisterPairLow<Register>(),
3720 ShifterOperand(0));
3721 // We cannot emit an RSC (Reverse Subtract with Carry)
3722 // instruction here, as it does not exist in the Thumb-2
3723 // instruction set. We use the following approach
3724 // using SBC and SUB instead.
3725 //
3726 // out.hi = -C
3727 __ sbc(out.AsRegisterPairHigh<Register>(),
3728 out.AsRegisterPairHigh<Register>(),
3729 ShifterOperand(out.AsRegisterPairHigh<Register>()));
3730 // out.hi = out.hi - in.hi
3731 __ sub(out.AsRegisterPairHigh<Register>(),
3732 out.AsRegisterPairHigh<Register>(),
3733 ShifterOperand(in.AsRegisterPairHigh<Register>()));
3734 break;
3735
Roland Levillain88cb1752014-10-20 16:36:47 +01003736 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003737 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003738 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00003739 break;
3740
Roland Levillain88cb1752014-10-20 16:36:47 +01003741 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003742 DCHECK(in.IsFpuRegisterPair());
3743 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3744 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01003745 break;
3746
3747 default:
3748 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3749 }
3750}
3751
Roland Levillaindff1f282014-11-05 14:15:05 +00003752void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003753 Primitive::Type result_type = conversion->GetResultType();
3754 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003755 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00003756
Roland Levillain5b3ee562015-04-14 16:02:41 +01003757 // The float-to-long, double-to-long and long-to-float type conversions
3758 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003759 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01003760 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3761 && result_type == Primitive::kPrimLong)
3762 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003763 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00003764 : LocationSummary::kNoCall;
3765 LocationSummary* locations =
3766 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3767
David Brazdilb2bd1c52015-03-25 11:17:37 +00003768 // The Java language does not allow treating boolean as an integral type but
3769 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00003770
Roland Levillaindff1f282014-11-05 14:15:05 +00003771 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003772 case Primitive::kPrimByte:
3773 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003774 case Primitive::kPrimLong:
3775 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003776 case Primitive::kPrimBoolean:
3777 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003778 case Primitive::kPrimShort:
3779 case Primitive::kPrimInt:
3780 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003781 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003782 locations->SetInAt(0, Location::RequiresRegister());
3783 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3784 break;
3785
3786 default:
3787 LOG(FATAL) << "Unexpected type conversion from " << input_type
3788 << " to " << result_type;
3789 }
3790 break;
3791
Roland Levillain01a8d712014-11-14 16:27:39 +00003792 case Primitive::kPrimShort:
3793 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003794 case Primitive::kPrimLong:
3795 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003796 case Primitive::kPrimBoolean:
3797 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003798 case Primitive::kPrimByte:
3799 case Primitive::kPrimInt:
3800 case Primitive::kPrimChar:
3801 // Processing a Dex `int-to-short' instruction.
3802 locations->SetInAt(0, Location::RequiresRegister());
3803 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3804 break;
3805
3806 default:
3807 LOG(FATAL) << "Unexpected type conversion from " << input_type
3808 << " to " << result_type;
3809 }
3810 break;
3811
Roland Levillain946e1432014-11-11 17:35:19 +00003812 case Primitive::kPrimInt:
3813 switch (input_type) {
3814 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003815 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003816 locations->SetInAt(0, Location::Any());
3817 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3818 break;
3819
3820 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00003821 // Processing a Dex `float-to-int' instruction.
3822 locations->SetInAt(0, Location::RequiresFpuRegister());
3823 locations->SetOut(Location::RequiresRegister());
3824 locations->AddTemp(Location::RequiresFpuRegister());
3825 break;
3826
Roland Levillain946e1432014-11-11 17:35:19 +00003827 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003828 // Processing a Dex `double-to-int' instruction.
3829 locations->SetInAt(0, Location::RequiresFpuRegister());
3830 locations->SetOut(Location::RequiresRegister());
3831 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00003832 break;
3833
3834 default:
3835 LOG(FATAL) << "Unexpected type conversion from " << input_type
3836 << " to " << result_type;
3837 }
3838 break;
3839
Roland Levillaindff1f282014-11-05 14:15:05 +00003840 case Primitive::kPrimLong:
3841 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003842 case Primitive::kPrimBoolean:
3843 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003844 case Primitive::kPrimByte:
3845 case Primitive::kPrimShort:
3846 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003847 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003848 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003849 locations->SetInAt(0, Location::RequiresRegister());
3850 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3851 break;
3852
Roland Levillain624279f2014-12-04 11:54:28 +00003853 case Primitive::kPrimFloat: {
3854 // Processing a Dex `float-to-long' instruction.
3855 InvokeRuntimeCallingConvention calling_convention;
3856 locations->SetInAt(0, Location::FpuRegisterLocation(
3857 calling_convention.GetFpuRegisterAt(0)));
3858 locations->SetOut(Location::RegisterPairLocation(R0, R1));
3859 break;
3860 }
3861
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003862 case Primitive::kPrimDouble: {
3863 // Processing a Dex `double-to-long' instruction.
3864 InvokeRuntimeCallingConvention calling_convention;
3865 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3866 calling_convention.GetFpuRegisterAt(0),
3867 calling_convention.GetFpuRegisterAt(1)));
3868 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00003869 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003870 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003871
3872 default:
3873 LOG(FATAL) << "Unexpected type conversion from " << input_type
3874 << " to " << result_type;
3875 }
3876 break;
3877
Roland Levillain981e4542014-11-14 11:47:14 +00003878 case Primitive::kPrimChar:
3879 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003880 case Primitive::kPrimLong:
3881 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003882 case Primitive::kPrimBoolean:
3883 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003884 case Primitive::kPrimByte:
3885 case Primitive::kPrimShort:
3886 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003887 // Processing a Dex `int-to-char' instruction.
3888 locations->SetInAt(0, Location::RequiresRegister());
3889 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3890 break;
3891
3892 default:
3893 LOG(FATAL) << "Unexpected type conversion from " << input_type
3894 << " to " << result_type;
3895 }
3896 break;
3897
Roland Levillaindff1f282014-11-05 14:15:05 +00003898 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003899 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003900 case Primitive::kPrimBoolean:
3901 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003902 case Primitive::kPrimByte:
3903 case Primitive::kPrimShort:
3904 case Primitive::kPrimInt:
3905 case Primitive::kPrimChar:
3906 // Processing a Dex `int-to-float' instruction.
3907 locations->SetInAt(0, Location::RequiresRegister());
3908 locations->SetOut(Location::RequiresFpuRegister());
3909 break;
3910
Roland Levillain5b3ee562015-04-14 16:02:41 +01003911 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00003912 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01003913 InvokeRuntimeCallingConvention calling_convention;
3914 locations->SetInAt(0, Location::RegisterPairLocation(
3915 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3916 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00003917 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01003918 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003919
Roland Levillaincff13742014-11-17 14:32:17 +00003920 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003921 // Processing a Dex `double-to-float' instruction.
3922 locations->SetInAt(0, Location::RequiresFpuRegister());
3923 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003924 break;
3925
3926 default:
3927 LOG(FATAL) << "Unexpected type conversion from " << input_type
3928 << " to " << result_type;
3929 };
3930 break;
3931
Roland Levillaindff1f282014-11-05 14:15:05 +00003932 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003933 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003934 case Primitive::kPrimBoolean:
3935 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003936 case Primitive::kPrimByte:
3937 case Primitive::kPrimShort:
3938 case Primitive::kPrimInt:
3939 case Primitive::kPrimChar:
3940 // Processing a Dex `int-to-double' instruction.
3941 locations->SetInAt(0, Location::RequiresRegister());
3942 locations->SetOut(Location::RequiresFpuRegister());
3943 break;
3944
3945 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003946 // Processing a Dex `long-to-double' instruction.
3947 locations->SetInAt(0, Location::RequiresRegister());
3948 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01003949 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00003950 locations->AddTemp(Location::RequiresFpuRegister());
3951 break;
3952
Roland Levillaincff13742014-11-17 14:32:17 +00003953 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003954 // Processing a Dex `float-to-double' instruction.
3955 locations->SetInAt(0, Location::RequiresFpuRegister());
3956 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003957 break;
3958
3959 default:
3960 LOG(FATAL) << "Unexpected type conversion from " << input_type
3961 << " to " << result_type;
3962 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003963 break;
3964
3965 default:
3966 LOG(FATAL) << "Unexpected type conversion from " << input_type
3967 << " to " << result_type;
3968 }
3969}
3970
3971void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
3972 LocationSummary* locations = conversion->GetLocations();
3973 Location out = locations->Out();
3974 Location in = locations->InAt(0);
3975 Primitive::Type result_type = conversion->GetResultType();
3976 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003977 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00003978 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003979 case Primitive::kPrimByte:
3980 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003981 case Primitive::kPrimLong:
3982 // Type conversion from long to byte is a result of code transformations.
3983 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
3984 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003985 case Primitive::kPrimBoolean:
3986 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003987 case Primitive::kPrimShort:
3988 case Primitive::kPrimInt:
3989 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003990 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003991 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00003992 break;
3993
3994 default:
3995 LOG(FATAL) << "Unexpected type conversion from " << input_type
3996 << " to " << result_type;
3997 }
3998 break;
3999
Roland Levillain01a8d712014-11-14 16:27:39 +00004000 case Primitive::kPrimShort:
4001 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00004002 case Primitive::kPrimLong:
4003 // Type conversion from long to short is a result of code transformations.
4004 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
4005 break;
David Brazdil46e2a392015-03-16 17:31:52 +00004006 case Primitive::kPrimBoolean:
4007 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00004008 case Primitive::kPrimByte:
4009 case Primitive::kPrimInt:
4010 case Primitive::kPrimChar:
4011 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004012 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00004013 break;
4014
4015 default:
4016 LOG(FATAL) << "Unexpected type conversion from " << input_type
4017 << " to " << result_type;
4018 }
4019 break;
4020
Roland Levillain946e1432014-11-11 17:35:19 +00004021 case Primitive::kPrimInt:
4022 switch (input_type) {
4023 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00004024 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00004025 DCHECK(out.IsRegister());
4026 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004027 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00004028 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004029 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00004030 } else {
4031 DCHECK(in.IsConstant());
4032 DCHECK(in.GetConstant()->IsLongConstant());
4033 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004034 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00004035 }
4036 break;
4037
Roland Levillain3f8f9362014-12-02 17:45:01 +00004038 case Primitive::kPrimFloat: {
4039 // Processing a Dex `float-to-int' instruction.
4040 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01004041 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00004042 __ vmovrs(out.AsRegister<Register>(), temp);
4043 break;
4044 }
4045
Roland Levillain4c0b61f2014-12-05 12:06:01 +00004046 case Primitive::kPrimDouble: {
4047 // Processing a Dex `double-to-int' instruction.
4048 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01004049 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00004050 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00004051 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00004052 }
Roland Levillain946e1432014-11-11 17:35:19 +00004053
4054 default:
4055 LOG(FATAL) << "Unexpected type conversion from " << input_type
4056 << " to " << result_type;
4057 }
4058 break;
4059
Roland Levillaindff1f282014-11-05 14:15:05 +00004060 case Primitive::kPrimLong:
4061 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00004062 case Primitive::kPrimBoolean:
4063 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00004064 case Primitive::kPrimByte:
4065 case Primitive::kPrimShort:
4066 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00004067 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00004068 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00004069 DCHECK(out.IsRegisterPair());
4070 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004071 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00004072 // Sign extension.
4073 __ Asr(out.AsRegisterPairHigh<Register>(),
4074 out.AsRegisterPairLow<Register>(),
4075 31);
4076 break;
4077
4078 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00004079 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004080 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004081 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00004082 break;
4083
Roland Levillaindff1f282014-11-05 14:15:05 +00004084 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00004085 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004086 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004087 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00004088 break;
4089
4090 default:
4091 LOG(FATAL) << "Unexpected type conversion from " << input_type
4092 << " to " << result_type;
4093 }
4094 break;
4095
Roland Levillain981e4542014-11-14 11:47:14 +00004096 case Primitive::kPrimChar:
4097 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00004098 case Primitive::kPrimLong:
4099 // Type conversion from long to char is a result of code transformations.
4100 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
4101 break;
David Brazdil46e2a392015-03-16 17:31:52 +00004102 case Primitive::kPrimBoolean:
4103 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00004104 case Primitive::kPrimByte:
4105 case Primitive::kPrimShort:
4106 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00004107 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004108 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00004109 break;
4110
4111 default:
4112 LOG(FATAL) << "Unexpected type conversion from " << input_type
4113 << " to " << result_type;
4114 }
4115 break;
4116
Roland Levillaindff1f282014-11-05 14:15:05 +00004117 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00004118 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00004119 case Primitive::kPrimBoolean:
4120 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00004121 case Primitive::kPrimByte:
4122 case Primitive::kPrimShort:
4123 case Primitive::kPrimInt:
4124 case Primitive::kPrimChar: {
4125 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004126 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
4127 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00004128 break;
4129 }
4130
Roland Levillain5b3ee562015-04-14 16:02:41 +01004131 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00004132 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004133 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004134 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00004135 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00004136
Roland Levillaincff13742014-11-17 14:32:17 +00004137 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00004138 // Processing a Dex `double-to-float' instruction.
4139 __ vcvtsd(out.AsFpuRegister<SRegister>(),
4140 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00004141 break;
4142
4143 default:
4144 LOG(FATAL) << "Unexpected type conversion from " << input_type
4145 << " to " << result_type;
4146 };
4147 break;
4148
Roland Levillaindff1f282014-11-05 14:15:05 +00004149 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00004150 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00004151 case Primitive::kPrimBoolean:
4152 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00004153 case Primitive::kPrimByte:
4154 case Primitive::kPrimShort:
4155 case Primitive::kPrimInt:
4156 case Primitive::kPrimChar: {
4157 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004158 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00004159 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4160 out.AsFpuRegisterPairLow<SRegister>());
4161 break;
4162 }
4163
Roland Levillain647b9ed2014-11-27 12:06:00 +00004164 case Primitive::kPrimLong: {
4165 // Processing a Dex `long-to-double' instruction.
4166 Register low = in.AsRegisterPairLow<Register>();
4167 Register high = in.AsRegisterPairHigh<Register>();
4168 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
4169 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01004170 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00004171 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01004172 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
4173 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00004174
Roland Levillain682393c2015-04-14 15:57:52 +01004175 // temp_d = int-to-double(high)
4176 __ vmovsr(temp_s, high);
4177 __ vcvtdi(temp_d, temp_s);
4178 // constant_d = k2Pow32EncodingForDouble
4179 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
4180 // out_d = unsigned-to-double(low)
4181 __ vmovsr(out_s, low);
4182 __ vcvtdu(out_d, out_s);
4183 // out_d += temp_d * constant_d
4184 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00004185 break;
4186 }
4187
Roland Levillaincff13742014-11-17 14:32:17 +00004188 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00004189 // Processing a Dex `float-to-double' instruction.
4190 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4191 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00004192 break;
4193
4194 default:
4195 LOG(FATAL) << "Unexpected type conversion from " << input_type
4196 << " to " << result_type;
4197 };
Roland Levillaindff1f282014-11-05 14:15:05 +00004198 break;
4199
4200 default:
4201 LOG(FATAL) << "Unexpected type conversion from " << input_type
4202 << " to " << result_type;
4203 }
4204}
4205
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004206void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004207 LocationSummary* locations =
4208 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004209 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004210 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004211 locations->SetInAt(0, Location::RequiresRegister());
4212 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004213 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4214 break;
4215 }
4216
4217 case Primitive::kPrimLong: {
4218 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01004219 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004220 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004221 break;
4222 }
4223
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004224 case Primitive::kPrimFloat:
4225 case Primitive::kPrimDouble: {
4226 locations->SetInAt(0, Location::RequiresFpuRegister());
4227 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004228 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004229 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004230 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004231
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004232 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004233 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004234 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004235}
4236
4237void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
4238 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004239 Location out = locations->Out();
4240 Location first = locations->InAt(0);
4241 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004242 switch (add->GetResultType()) {
4243 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004244 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004245 __ add(out.AsRegister<Register>(),
4246 first.AsRegister<Register>(),
4247 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004248 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004249 __ AddConstant(out.AsRegister<Register>(),
4250 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004251 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004252 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004253 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004254
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004255 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01004256 if (second.IsConstant()) {
4257 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4258 GenerateAddLongConst(out, first, value);
4259 } else {
4260 DCHECK(second.IsRegisterPair());
4261 __ adds(out.AsRegisterPairLow<Register>(),
4262 first.AsRegisterPairLow<Register>(),
4263 ShifterOperand(second.AsRegisterPairLow<Register>()));
4264 __ adc(out.AsRegisterPairHigh<Register>(),
4265 first.AsRegisterPairHigh<Register>(),
4266 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4267 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004268 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004269 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004270
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004271 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00004272 __ vadds(out.AsFpuRegister<SRegister>(),
4273 first.AsFpuRegister<SRegister>(),
4274 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004275 break;
4276
4277 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004278 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4279 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4280 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004281 break;
4282
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004283 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004284 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004285 }
4286}
4287
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004288void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004289 LocationSummary* locations =
4290 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004291 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004292 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004293 locations->SetInAt(0, Location::RequiresRegister());
4294 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004295 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4296 break;
4297 }
4298
4299 case Primitive::kPrimLong: {
4300 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01004301 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004302 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004303 break;
4304 }
Calin Juravle11351682014-10-23 15:38:15 +01004305 case Primitive::kPrimFloat:
4306 case Primitive::kPrimDouble: {
4307 locations->SetInAt(0, Location::RequiresFpuRegister());
4308 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004309 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004310 break;
Calin Juravle11351682014-10-23 15:38:15 +01004311 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004312 default:
Calin Juravle11351682014-10-23 15:38:15 +01004313 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004314 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004315}
4316
4317void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
4318 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01004319 Location out = locations->Out();
4320 Location first = locations->InAt(0);
4321 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004322 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004323 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01004324 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004325 __ sub(out.AsRegister<Register>(),
4326 first.AsRegister<Register>(),
4327 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004328 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004329 __ AddConstant(out.AsRegister<Register>(),
4330 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01004331 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004332 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004333 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004334 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004335
Calin Juravle11351682014-10-23 15:38:15 +01004336 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01004337 if (second.IsConstant()) {
4338 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4339 GenerateAddLongConst(out, first, -value);
4340 } else {
4341 DCHECK(second.IsRegisterPair());
4342 __ subs(out.AsRegisterPairLow<Register>(),
4343 first.AsRegisterPairLow<Register>(),
4344 ShifterOperand(second.AsRegisterPairLow<Register>()));
4345 __ sbc(out.AsRegisterPairHigh<Register>(),
4346 first.AsRegisterPairHigh<Register>(),
4347 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4348 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004349 break;
Calin Juravle11351682014-10-23 15:38:15 +01004350 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004351
Calin Juravle11351682014-10-23 15:38:15 +01004352 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004353 __ vsubs(out.AsFpuRegister<SRegister>(),
4354 first.AsFpuRegister<SRegister>(),
4355 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004356 break;
Calin Juravle11351682014-10-23 15:38:15 +01004357 }
4358
4359 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004360 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4361 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4362 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01004363 break;
4364 }
4365
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004366
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004367 default:
Calin Juravle11351682014-10-23 15:38:15 +01004368 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004369 }
4370}
4371
Calin Juravle34bacdf2014-10-07 20:23:36 +01004372void LocationsBuilderARM::VisitMul(HMul* mul) {
4373 LocationSummary* locations =
4374 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4375 switch (mul->GetResultType()) {
4376 case Primitive::kPrimInt:
4377 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004378 locations->SetInAt(0, Location::RequiresRegister());
4379 locations->SetInAt(1, Location::RequiresRegister());
4380 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004381 break;
4382 }
4383
Calin Juravleb5bfa962014-10-21 18:02:24 +01004384 case Primitive::kPrimFloat:
4385 case Primitive::kPrimDouble: {
4386 locations->SetInAt(0, Location::RequiresFpuRegister());
4387 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004388 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004389 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004390 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004391
4392 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004393 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004394 }
4395}
4396
4397void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
4398 LocationSummary* locations = mul->GetLocations();
4399 Location out = locations->Out();
4400 Location first = locations->InAt(0);
4401 Location second = locations->InAt(1);
4402 switch (mul->GetResultType()) {
4403 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00004404 __ mul(out.AsRegister<Register>(),
4405 first.AsRegister<Register>(),
4406 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004407 break;
4408 }
4409 case Primitive::kPrimLong: {
4410 Register out_hi = out.AsRegisterPairHigh<Register>();
4411 Register out_lo = out.AsRegisterPairLow<Register>();
4412 Register in1_hi = first.AsRegisterPairHigh<Register>();
4413 Register in1_lo = first.AsRegisterPairLow<Register>();
4414 Register in2_hi = second.AsRegisterPairHigh<Register>();
4415 Register in2_lo = second.AsRegisterPairLow<Register>();
4416
4417 // Extra checks to protect caused by the existence of R1_R2.
4418 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4419 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
4420 DCHECK_NE(out_hi, in1_lo);
4421 DCHECK_NE(out_hi, in2_lo);
4422
4423 // input: in1 - 64 bits, in2 - 64 bits
4424 // output: out
4425 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4426 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4427 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4428
4429 // IP <- in1.lo * in2.hi
4430 __ mul(IP, in1_lo, in2_hi);
4431 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4432 __ mla(out_hi, in1_hi, in2_lo, IP);
4433 // out.lo <- (in1.lo * in2.lo)[31:0];
4434 __ umull(out_lo, IP, in1_lo, in2_lo);
4435 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
4436 __ add(out_hi, out_hi, ShifterOperand(IP));
4437 break;
4438 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01004439
4440 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004441 __ vmuls(out.AsFpuRegister<SRegister>(),
4442 first.AsFpuRegister<SRegister>(),
4443 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004444 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004445 }
4446
4447 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004448 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4449 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4450 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01004451 break;
4452 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004453
4454 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004455 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004456 }
4457}
4458
Zheng Xuc6667102015-05-15 16:08:45 +08004459void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4460 DCHECK(instruction->IsDiv() || instruction->IsRem());
4461 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4462
4463 LocationSummary* locations = instruction->GetLocations();
4464 Location second = locations->InAt(1);
4465 DCHECK(second.IsConstant());
4466
4467 Register out = locations->Out().AsRegister<Register>();
4468 Register dividend = locations->InAt(0).AsRegister<Register>();
4469 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4470 DCHECK(imm == 1 || imm == -1);
4471
4472 if (instruction->IsRem()) {
4473 __ LoadImmediate(out, 0);
4474 } else {
4475 if (imm == 1) {
4476 __ Mov(out, dividend);
4477 } else {
4478 __ rsb(out, dividend, ShifterOperand(0));
4479 }
4480 }
4481}
4482
4483void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4484 DCHECK(instruction->IsDiv() || instruction->IsRem());
4485 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4486
4487 LocationSummary* locations = instruction->GetLocations();
4488 Location second = locations->InAt(1);
4489 DCHECK(second.IsConstant());
4490
4491 Register out = locations->Out().AsRegister<Register>();
4492 Register dividend = locations->InAt(0).AsRegister<Register>();
4493 Register temp = locations->GetTemp(0).AsRegister<Register>();
4494 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004495 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08004496 int ctz_imm = CTZ(abs_imm);
4497
4498 if (ctz_imm == 1) {
4499 __ Lsr(temp, dividend, 32 - ctz_imm);
4500 } else {
4501 __ Asr(temp, dividend, 31);
4502 __ Lsr(temp, temp, 32 - ctz_imm);
4503 }
4504 __ add(out, temp, ShifterOperand(dividend));
4505
4506 if (instruction->IsDiv()) {
4507 __ Asr(out, out, ctz_imm);
4508 if (imm < 0) {
4509 __ rsb(out, out, ShifterOperand(0));
4510 }
4511 } else {
4512 __ ubfx(out, out, 0, ctz_imm);
4513 __ sub(out, out, ShifterOperand(temp));
4514 }
4515}
4516
4517void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4518 DCHECK(instruction->IsDiv() || instruction->IsRem());
4519 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4520
4521 LocationSummary* locations = instruction->GetLocations();
4522 Location second = locations->InAt(1);
4523 DCHECK(second.IsConstant());
4524
4525 Register out = locations->Out().AsRegister<Register>();
4526 Register dividend = locations->InAt(0).AsRegister<Register>();
4527 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4528 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4529 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4530
4531 int64_t magic;
4532 int shift;
4533 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4534
4535 __ LoadImmediate(temp1, magic);
4536 __ smull(temp2, temp1, dividend, temp1);
4537
4538 if (imm > 0 && magic < 0) {
4539 __ add(temp1, temp1, ShifterOperand(dividend));
4540 } else if (imm < 0 && magic > 0) {
4541 __ sub(temp1, temp1, ShifterOperand(dividend));
4542 }
4543
4544 if (shift != 0) {
4545 __ Asr(temp1, temp1, shift);
4546 }
4547
4548 if (instruction->IsDiv()) {
4549 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
4550 } else {
4551 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
4552 // TODO: Strength reduction for mls.
4553 __ LoadImmediate(temp2, imm);
4554 __ mls(out, temp1, temp2, dividend);
4555 }
4556}
4557
4558void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
4559 DCHECK(instruction->IsDiv() || instruction->IsRem());
4560 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4561
4562 LocationSummary* locations = instruction->GetLocations();
4563 Location second = locations->InAt(1);
4564 DCHECK(second.IsConstant());
4565
4566 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4567 if (imm == 0) {
4568 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4569 } else if (imm == 1 || imm == -1) {
4570 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004571 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004572 DivRemByPowerOfTwo(instruction);
4573 } else {
4574 DCHECK(imm <= -2 || imm >= 2);
4575 GenerateDivRemWithAnyConstant(instruction);
4576 }
4577}
4578
Calin Juravle7c4954d2014-10-28 16:57:40 +00004579void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004580 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4581 if (div->GetResultType() == Primitive::kPrimLong) {
4582 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004583 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004584 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4585 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004586 } else if (div->GetResultType() == Primitive::kPrimInt &&
4587 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4588 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004589 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004590 }
4591
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004592 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4593
Calin Juravle7c4954d2014-10-28 16:57:40 +00004594 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004595 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004596 if (div->InputAt(1)->IsConstant()) {
4597 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004598 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004599 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004600 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
4601 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004602 // No temp register required.
4603 } else {
4604 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004605 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004606 locations->AddTemp(Location::RequiresRegister());
4607 }
4608 }
4609 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004610 locations->SetInAt(0, Location::RequiresRegister());
4611 locations->SetInAt(1, Location::RequiresRegister());
4612 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4613 } else {
4614 InvokeRuntimeCallingConvention calling_convention;
4615 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4616 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004617 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004618 // we only need the former.
4619 locations->SetOut(Location::RegisterLocation(R0));
4620 }
Calin Juravled0d48522014-11-04 16:40:20 +00004621 break;
4622 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004623 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004624 InvokeRuntimeCallingConvention calling_convention;
4625 locations->SetInAt(0, Location::RegisterPairLocation(
4626 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4627 locations->SetInAt(1, Location::RegisterPairLocation(
4628 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004629 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004630 break;
4631 }
4632 case Primitive::kPrimFloat:
4633 case Primitive::kPrimDouble: {
4634 locations->SetInAt(0, Location::RequiresFpuRegister());
4635 locations->SetInAt(1, Location::RequiresFpuRegister());
4636 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4637 break;
4638 }
4639
4640 default:
4641 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4642 }
4643}
4644
4645void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
4646 LocationSummary* locations = div->GetLocations();
4647 Location out = locations->Out();
4648 Location first = locations->InAt(0);
4649 Location second = locations->InAt(1);
4650
4651 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004652 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004653 if (second.IsConstant()) {
4654 GenerateDivRemConstantIntegral(div);
4655 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004656 __ sdiv(out.AsRegister<Register>(),
4657 first.AsRegister<Register>(),
4658 second.AsRegister<Register>());
4659 } else {
4660 InvokeRuntimeCallingConvention calling_convention;
4661 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4662 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4663 DCHECK_EQ(R0, out.AsRegister<Register>());
4664
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004665 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004666 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004667 }
Calin Juravled0d48522014-11-04 16:40:20 +00004668 break;
4669 }
4670
Calin Juravle7c4954d2014-10-28 16:57:40 +00004671 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004672 InvokeRuntimeCallingConvention calling_convention;
4673 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4674 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4675 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4676 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4677 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004678 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004679
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004680 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004681 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00004682 break;
4683 }
4684
4685 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004686 __ vdivs(out.AsFpuRegister<SRegister>(),
4687 first.AsFpuRegister<SRegister>(),
4688 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004689 break;
4690 }
4691
4692 case Primitive::kPrimDouble: {
4693 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4694 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4695 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
4696 break;
4697 }
4698
4699 default:
4700 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4701 }
4702}
4703
Calin Juravlebacfec32014-11-14 15:54:36 +00004704void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004705 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004706
4707 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004708 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004709 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4710 // sdiv will be replaced by other instruction sequence.
4711 call_kind = LocationSummary::kNoCall;
4712 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4713 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004714 // Have hardware divide instruction for int, do it with three instructions.
4715 call_kind = LocationSummary::kNoCall;
4716 }
4717
Calin Juravlebacfec32014-11-14 15:54:36 +00004718 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4719
Calin Juravled2ec87d2014-12-08 14:24:46 +00004720 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004721 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004722 if (rem->InputAt(1)->IsConstant()) {
4723 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004724 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004725 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004726 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
4727 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004728 // No temp register required.
4729 } else {
4730 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004731 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004732 locations->AddTemp(Location::RequiresRegister());
4733 }
4734 }
4735 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004736 locations->SetInAt(0, Location::RequiresRegister());
4737 locations->SetInAt(1, Location::RequiresRegister());
4738 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4739 locations->AddTemp(Location::RequiresRegister());
4740 } else {
4741 InvokeRuntimeCallingConvention calling_convention;
4742 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4743 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004744 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004745 // we only need the latter.
4746 locations->SetOut(Location::RegisterLocation(R1));
4747 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004748 break;
4749 }
4750 case Primitive::kPrimLong: {
4751 InvokeRuntimeCallingConvention calling_convention;
4752 locations->SetInAt(0, Location::RegisterPairLocation(
4753 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4754 locations->SetInAt(1, Location::RegisterPairLocation(
4755 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4756 // The runtime helper puts the output in R2,R3.
4757 locations->SetOut(Location::RegisterPairLocation(R2, R3));
4758 break;
4759 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00004760 case Primitive::kPrimFloat: {
4761 InvokeRuntimeCallingConvention calling_convention;
4762 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4763 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4764 locations->SetOut(Location::FpuRegisterLocation(S0));
4765 break;
4766 }
4767
Calin Juravlebacfec32014-11-14 15:54:36 +00004768 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004769 InvokeRuntimeCallingConvention calling_convention;
4770 locations->SetInAt(0, Location::FpuRegisterPairLocation(
4771 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4772 locations->SetInAt(1, Location::FpuRegisterPairLocation(
4773 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4774 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00004775 break;
4776 }
4777
4778 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004779 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004780 }
4781}
4782
4783void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
4784 LocationSummary* locations = rem->GetLocations();
4785 Location out = locations->Out();
4786 Location first = locations->InAt(0);
4787 Location second = locations->InAt(1);
4788
Calin Juravled2ec87d2014-12-08 14:24:46 +00004789 Primitive::Type type = rem->GetResultType();
4790 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004791 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004792 if (second.IsConstant()) {
4793 GenerateDivRemConstantIntegral(rem);
4794 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004795 Register reg1 = first.AsRegister<Register>();
4796 Register reg2 = second.AsRegister<Register>();
4797 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004798
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004799 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004800 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004801 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004802 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004803 } else {
4804 InvokeRuntimeCallingConvention calling_convention;
4805 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4806 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4807 DCHECK_EQ(R1, out.AsRegister<Register>());
4808
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004809 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004810 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004811 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004812 break;
4813 }
4814
4815 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004816 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004817 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004818 break;
4819 }
4820
Calin Juravled2ec87d2014-12-08 14:24:46 +00004821 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004822 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004823 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00004824 break;
4825 }
4826
Calin Juravlebacfec32014-11-14 15:54:36 +00004827 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004828 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004829 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004830 break;
4831 }
4832
4833 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004834 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004835 }
4836}
4837
Calin Juravled0d48522014-11-04 16:40:20 +00004838void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004839 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004840 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00004841}
4842
4843void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004844 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004845 codegen_->AddSlowPath(slow_path);
4846
4847 LocationSummary* locations = instruction->GetLocations();
4848 Location value = locations->InAt(0);
4849
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004850 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00004851 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004852 case Primitive::kPrimByte:
4853 case Primitive::kPrimChar:
4854 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004855 case Primitive::kPrimInt: {
4856 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004857 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004858 } else {
4859 DCHECK(value.IsConstant()) << value;
4860 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4861 __ b(slow_path->GetEntryLabel());
4862 }
4863 }
4864 break;
4865 }
4866 case Primitive::kPrimLong: {
4867 if (value.IsRegisterPair()) {
4868 __ orrs(IP,
4869 value.AsRegisterPairLow<Register>(),
4870 ShifterOperand(value.AsRegisterPairHigh<Register>()));
4871 __ b(slow_path->GetEntryLabel(), EQ);
4872 } else {
4873 DCHECK(value.IsConstant()) << value;
4874 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4875 __ b(slow_path->GetEntryLabel());
4876 }
4877 }
4878 break;
4879 default:
4880 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4881 }
4882 }
Calin Juravled0d48522014-11-04 16:40:20 +00004883}
4884
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004885void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
4886 Register in = locations->InAt(0).AsRegister<Register>();
4887 Location rhs = locations->InAt(1);
4888 Register out = locations->Out().AsRegister<Register>();
4889
4890 if (rhs.IsConstant()) {
4891 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4892 // so map all rotations to a +ve. equivalent in that range.
4893 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4894 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4895 if (rot) {
4896 // Rotate, mapping left rotations to right equivalents if necessary.
4897 // (e.g. left by 2 bits == right by 30.)
4898 __ Ror(out, in, rot);
4899 } else if (out != in) {
4900 __ Mov(out, in);
4901 }
4902 } else {
4903 __ Ror(out, in, rhs.AsRegister<Register>());
4904 }
4905}
4906
4907// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4908// rotates by swapping input regs (effectively rotating by the first 32-bits of
4909// a larger rotation) or flipping direction (thus treating larger right/left
4910// rotations as sub-word sized rotations in the other direction) as appropriate.
Anton Kirilov6f644202017-02-27 18:29:45 +00004911void InstructionCodeGeneratorARM::HandleLongRotate(HRor* ror) {
4912 LocationSummary* locations = ror->GetLocations();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004913 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
4914 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
4915 Location rhs = locations->InAt(1);
4916 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
4917 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
4918
4919 if (rhs.IsConstant()) {
4920 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4921 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00004922 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004923 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4924 // logic below to a simple pair of binary orr.
4925 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4926 if (rot >= kArmBitsPerWord) {
4927 rot -= kArmBitsPerWord;
4928 std::swap(in_reg_hi, in_reg_lo);
4929 }
4930 // Rotate, or mov to out for zero or word size rotations.
4931 if (rot != 0u) {
4932 __ Lsr(out_reg_hi, in_reg_hi, rot);
4933 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
4934 __ Lsr(out_reg_lo, in_reg_lo, rot);
4935 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
4936 } else {
4937 __ Mov(out_reg_lo, in_reg_lo);
4938 __ Mov(out_reg_hi, in_reg_hi);
4939 }
4940 } else {
4941 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
4942 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
4943 Label end;
4944 Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004945 Label* final_label = codegen_->GetFinalLabel(ror, &end);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004946
4947 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
4948 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
4949 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
4950 __ b(&shift_by_32_plus_shift_right, CC);
4951
4952 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4953 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4954 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4955 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4956 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4957 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4958 __ Lsr(shift_left, in_reg_hi, shift_right);
4959 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
Anton Kirilov6f644202017-02-27 18:29:45 +00004960 __ b(final_label);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004961
4962 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4963 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4964 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4965 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4966 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4967 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4968 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4969 __ Lsl(shift_right, in_reg_hi, shift_left);
4970 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
4971
Anton Kirilov6f644202017-02-27 18:29:45 +00004972 if (end.IsLinked()) {
4973 __ Bind(&end);
4974 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004975 }
4976}
Roland Levillain22c49222016-03-18 14:04:28 +00004977
4978void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004979 LocationSummary* locations =
4980 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4981 switch (ror->GetResultType()) {
4982 case Primitive::kPrimInt: {
4983 locations->SetInAt(0, Location::RequiresRegister());
4984 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4985 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4986 break;
4987 }
4988 case Primitive::kPrimLong: {
4989 locations->SetInAt(0, Location::RequiresRegister());
4990 if (ror->InputAt(1)->IsConstant()) {
4991 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4992 } else {
4993 locations->SetInAt(1, Location::RequiresRegister());
4994 locations->AddTemp(Location::RequiresRegister());
4995 locations->AddTemp(Location::RequiresRegister());
4996 }
4997 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4998 break;
4999 }
5000 default:
5001 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
5002 }
5003}
5004
Roland Levillain22c49222016-03-18 14:04:28 +00005005void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005006 LocationSummary* locations = ror->GetLocations();
5007 Primitive::Type type = ror->GetResultType();
5008 switch (type) {
5009 case Primitive::kPrimInt: {
5010 HandleIntegerRotate(locations);
5011 break;
5012 }
5013 case Primitive::kPrimLong: {
Anton Kirilov6f644202017-02-27 18:29:45 +00005014 HandleLongRotate(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005015 break;
5016 }
5017 default:
5018 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005019 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005020 }
5021}
5022
Calin Juravle9aec02f2014-11-18 23:06:35 +00005023void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
5024 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5025
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005026 LocationSummary* locations =
5027 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005028
5029 switch (op->GetResultType()) {
5030 case Primitive::kPrimInt: {
5031 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005032 if (op->InputAt(1)->IsConstant()) {
5033 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5034 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5035 } else {
5036 locations->SetInAt(1, Location::RequiresRegister());
5037 // Make the output overlap, as it will be used to hold the masked
5038 // second input.
5039 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5040 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005041 break;
5042 }
5043 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005044 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005045 if (op->InputAt(1)->IsConstant()) {
5046 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5047 // For simplicity, use kOutputOverlap even though we only require that low registers
5048 // don't clash with high registers which the register allocator currently guarantees.
5049 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5050 } else {
5051 locations->SetInAt(1, Location::RequiresRegister());
5052 locations->AddTemp(Location::RequiresRegister());
5053 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5054 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005055 break;
5056 }
5057 default:
5058 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
5059 }
5060}
5061
5062void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
5063 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5064
5065 LocationSummary* locations = op->GetLocations();
5066 Location out = locations->Out();
5067 Location first = locations->InAt(0);
5068 Location second = locations->InAt(1);
5069
5070 Primitive::Type type = op->GetResultType();
5071 switch (type) {
5072 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005073 Register out_reg = out.AsRegister<Register>();
5074 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005075 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005076 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00005077 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00005078 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005079 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01005080 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005081 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01005082 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005083 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01005084 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005085 }
5086 } else {
5087 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00005088 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00005089 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00005090 __ Mov(out_reg, first_reg);
5091 } else if (op->IsShl()) {
5092 __ Lsl(out_reg, first_reg, shift_value);
5093 } else if (op->IsShr()) {
5094 __ Asr(out_reg, first_reg, shift_value);
5095 } else {
5096 __ Lsr(out_reg, first_reg, shift_value);
5097 }
5098 }
5099 break;
5100 }
5101 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005102 Register o_h = out.AsRegisterPairHigh<Register>();
5103 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005104
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005105 Register high = first.AsRegisterPairHigh<Register>();
5106 Register low = first.AsRegisterPairLow<Register>();
5107
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005108 if (second.IsRegister()) {
5109 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005110
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005111 Register second_reg = second.AsRegister<Register>();
5112
5113 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005114 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005115 // Shift the high part
5116 __ Lsl(o_h, high, o_l);
5117 // Shift the low part and `or` what overflew on the high part
5118 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
5119 __ Lsr(temp, low, temp);
5120 __ orr(o_h, o_h, ShifterOperand(temp));
5121 // If the shift is > 32 bits, override the high part
5122 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
5123 __ it(PL);
5124 __ Lsl(o_h, low, temp, PL);
5125 // Shift the low part
5126 __ Lsl(o_l, low, o_l);
5127 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005128 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005129 // Shift the low part
5130 __ Lsr(o_l, low, o_h);
5131 // Shift the high part and `or` what underflew on the low part
5132 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
5133 __ Lsl(temp, high, temp);
5134 __ orr(o_l, o_l, ShifterOperand(temp));
5135 // If the shift is > 32 bits, override the low part
5136 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
5137 __ it(PL);
5138 __ Asr(o_l, high, temp, PL);
5139 // Shift the high part
5140 __ Asr(o_h, high, o_h);
5141 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005142 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005143 // same as Shr except we use `Lsr`s and not `Asr`s
5144 __ Lsr(o_l, low, o_h);
5145 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
5146 __ Lsl(temp, high, temp);
5147 __ orr(o_l, o_l, ShifterOperand(temp));
5148 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
5149 __ it(PL);
5150 __ Lsr(o_l, high, temp, PL);
5151 __ Lsr(o_h, high, o_h);
5152 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005153 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005154 // Register allocator doesn't create partial overlap.
5155 DCHECK_NE(o_l, high);
5156 DCHECK_NE(o_h, low);
5157 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00005158 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005159 if (shift_value > 32) {
5160 if (op->IsShl()) {
5161 __ Lsl(o_h, low, shift_value - 32);
5162 __ LoadImmediate(o_l, 0);
5163 } else if (op->IsShr()) {
5164 __ Asr(o_l, high, shift_value - 32);
5165 __ Asr(o_h, high, 31);
5166 } else {
5167 __ Lsr(o_l, high, shift_value - 32);
5168 __ LoadImmediate(o_h, 0);
5169 }
5170 } else if (shift_value == 32) {
5171 if (op->IsShl()) {
5172 __ mov(o_h, ShifterOperand(low));
5173 __ LoadImmediate(o_l, 0);
5174 } else if (op->IsShr()) {
5175 __ mov(o_l, ShifterOperand(high));
5176 __ Asr(o_h, high, 31);
5177 } else {
5178 __ mov(o_l, ShifterOperand(high));
5179 __ LoadImmediate(o_h, 0);
5180 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00005181 } else if (shift_value == 1) {
5182 if (op->IsShl()) {
5183 __ Lsls(o_l, low, 1);
5184 __ adc(o_h, high, ShifterOperand(high));
5185 } else if (op->IsShr()) {
5186 __ Asrs(o_h, high, 1);
5187 __ Rrx(o_l, low);
5188 } else {
5189 __ Lsrs(o_h, high, 1);
5190 __ Rrx(o_l, low);
5191 }
5192 } else {
5193 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005194 if (op->IsShl()) {
5195 __ Lsl(o_h, high, shift_value);
5196 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
5197 __ Lsl(o_l, low, shift_value);
5198 } else if (op->IsShr()) {
5199 __ Lsr(o_l, low, shift_value);
5200 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
5201 __ Asr(o_h, high, shift_value);
5202 } else {
5203 __ Lsr(o_l, low, shift_value);
5204 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
5205 __ Lsr(o_h, high, shift_value);
5206 }
5207 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005208 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005209 break;
5210 }
5211 default:
5212 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005213 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005214 }
5215}
5216
5217void LocationsBuilderARM::VisitShl(HShl* shl) {
5218 HandleShift(shl);
5219}
5220
5221void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
5222 HandleShift(shl);
5223}
5224
5225void LocationsBuilderARM::VisitShr(HShr* shr) {
5226 HandleShift(shr);
5227}
5228
5229void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
5230 HandleShift(shr);
5231}
5232
5233void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
5234 HandleShift(ushr);
5235}
5236
5237void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
5238 HandleShift(ushr);
5239}
5240
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005241void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005242 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005243 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00005244 if (instruction->IsStringAlloc()) {
5245 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5246 } else {
5247 InvokeRuntimeCallingConvention calling_convention;
5248 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005249 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005250 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005251}
5252
5253void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005254 // Note: if heap poisoning is enabled, the entry point takes cares
5255 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005256 if (instruction->IsStringAlloc()) {
5257 // String is allocated through StringFactory. Call NewEmptyString entry point.
5258 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005259 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005260 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5261 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
5262 __ blx(LR);
5263 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5264 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005265 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005266 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005267 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005268}
5269
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005270void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
5271 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005272 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005273 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005274 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005275 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5276 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005277}
5278
5279void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005280 // Note: if heap poisoning is enabled, the entry point takes cares
5281 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00005282 QuickEntrypointEnum entrypoint =
5283 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5284 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005285 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00005286 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005287}
5288
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005289void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005290 LocationSummary* locations =
5291 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01005292 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5293 if (location.IsStackSlot()) {
5294 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5295 } else if (location.IsDoubleStackSlot()) {
5296 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005297 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01005298 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005299}
5300
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005301void InstructionCodeGeneratorARM::VisitParameterValue(
5302 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01005303 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005304}
5305
5306void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
5307 LocationSummary* locations =
5308 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5309 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5310}
5311
5312void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5313 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005314}
5315
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005316void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005317 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005318 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005319 locations->SetInAt(0, Location::RequiresRegister());
5320 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005321}
5322
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005323void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
5324 LocationSummary* locations = not_->GetLocations();
5325 Location out = locations->Out();
5326 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005327 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005328 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00005329 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005330 break;
5331
5332 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01005333 __ mvn(out.AsRegisterPairLow<Register>(),
5334 ShifterOperand(in.AsRegisterPairLow<Register>()));
5335 __ mvn(out.AsRegisterPairHigh<Register>(),
5336 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005337 break;
5338
5339 default:
5340 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5341 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005342}
5343
David Brazdil66d126e2015-04-03 16:02:44 +01005344void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
5345 LocationSummary* locations =
5346 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
5347 locations->SetInAt(0, Location::RequiresRegister());
5348 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5349}
5350
5351void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01005352 LocationSummary* locations = bool_not->GetLocations();
5353 Location out = locations->Out();
5354 Location in = locations->InAt(0);
5355 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
5356}
5357
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005358void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005359 LocationSummary* locations =
5360 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00005361 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00005362 case Primitive::kPrimBoolean:
5363 case Primitive::kPrimByte:
5364 case Primitive::kPrimShort:
5365 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08005366 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00005367 case Primitive::kPrimLong: {
5368 locations->SetInAt(0, Location::RequiresRegister());
5369 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005370 // Output overlaps because it is written before doing the low comparison.
5371 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00005372 break;
5373 }
5374 case Primitive::kPrimFloat:
5375 case Primitive::kPrimDouble: {
5376 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005377 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00005378 locations->SetOut(Location::RequiresRegister());
5379 break;
5380 }
5381 default:
5382 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5383 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005384}
5385
5386void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005387 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005388 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005389 Location left = locations->InAt(0);
5390 Location right = locations->InAt(1);
5391
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005392 Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005393 Label* final_label = codegen_->GetFinalLabel(compare, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00005394 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00005395 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00005396 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00005397 case Primitive::kPrimBoolean:
5398 case Primitive::kPrimByte:
5399 case Primitive::kPrimShort:
5400 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08005401 case Primitive::kPrimInt: {
5402 __ LoadImmediate(out, 0);
5403 __ cmp(left.AsRegister<Register>(),
5404 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
5405 less_cond = LT;
5406 break;
5407 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005408 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005409 __ cmp(left.AsRegisterPairHigh<Register>(),
5410 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005411 __ b(&less, LT);
5412 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01005413 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00005414 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005415 __ cmp(left.AsRegisterPairLow<Register>(),
5416 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005417 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00005418 break;
5419 }
5420 case Primitive::kPrimFloat:
5421 case Primitive::kPrimDouble: {
5422 __ LoadImmediate(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005423 GenerateVcmp(compare, codegen_);
Calin Juravleddb7df22014-11-25 20:56:51 +00005424 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005425 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005426 break;
5427 }
5428 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005429 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00005430 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005431 }
Aart Bika19616e2016-02-01 18:57:58 -08005432
Anton Kirilov6f644202017-02-27 18:29:45 +00005433 __ b(final_label, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00005434 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00005435
5436 __ Bind(&greater);
5437 __ LoadImmediate(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005438 __ b(final_label);
Calin Juravleddb7df22014-11-25 20:56:51 +00005439
5440 __ Bind(&less);
5441 __ LoadImmediate(out, -1);
5442
Anton Kirilov6f644202017-02-27 18:29:45 +00005443 if (done.IsLinked()) {
5444 __ Bind(&done);
5445 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005446}
5447
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005448void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005449 LocationSummary* locations =
5450 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005451 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005452 locations->SetInAt(i, Location::Any());
5453 }
5454 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005455}
5456
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005457void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005458 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005459}
5460
Roland Levillainc9285912015-12-18 10:38:42 +00005461void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
5462 // TODO (ported from quick): revisit ARM barrier kinds.
5463 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00005464 switch (kind) {
5465 case MemBarrierKind::kAnyStore:
5466 case MemBarrierKind::kLoadAny:
5467 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005468 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00005469 break;
5470 }
5471 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005472 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00005473 break;
5474 }
5475 default:
5476 LOG(FATAL) << "Unexpected memory barrier " << kind;
5477 }
Kenny Root1d8199d2015-06-02 11:01:10 -07005478 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00005479}
5480
5481void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
5482 uint32_t offset,
5483 Register out_lo,
5484 Register out_hi) {
5485 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005486 // Ensure `out_lo` is different from `addr`, so that loading
5487 // `offset` into `out_lo` does not clutter `addr`.
5488 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00005489 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005490 __ add(IP, addr, ShifterOperand(out_lo));
5491 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005492 }
5493 __ ldrexd(out_lo, out_hi, addr);
5494}
5495
5496void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
5497 uint32_t offset,
5498 Register value_lo,
5499 Register value_hi,
5500 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00005501 Register temp2,
5502 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005503 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00005504 if (offset != 0) {
5505 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005506 __ add(IP, addr, ShifterOperand(temp1));
5507 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005508 }
5509 __ Bind(&fail);
5510 // We need a load followed by store. (The address used in a STREX instruction must
5511 // be the same as the address in the most recently executed LDREX instruction.)
5512 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00005513 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005514 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005515 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00005516}
5517
5518void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5519 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5520
Nicolas Geoffray39468442014-09-02 15:17:15 +01005521 LocationSummary* locations =
5522 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005523 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005524
Calin Juravle52c48962014-12-16 17:02:57 +00005525 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005526 if (Primitive::IsFloatingPointType(field_type)) {
5527 locations->SetInAt(1, Location::RequiresFpuRegister());
5528 } else {
5529 locations->SetInAt(1, Location::RequiresRegister());
5530 }
5531
Calin Juravle52c48962014-12-16 17:02:57 +00005532 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00005533 bool generate_volatile = field_info.IsVolatile()
5534 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005535 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01005536 bool needs_write_barrier =
5537 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005538 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00005539 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01005540 if (needs_write_barrier) {
5541 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005542 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005543 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005544 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005545 // - registers need to be consecutive
5546 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005547 // We don't test for ARM yet, and the assertion makes sure that we
5548 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005549 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5550
5551 locations->AddTemp(Location::RequiresRegister());
5552 locations->AddTemp(Location::RequiresRegister());
5553 if (field_type == Primitive::kPrimDouble) {
5554 // For doubles we need two more registers to copy the value.
5555 locations->AddTemp(Location::RegisterLocation(R2));
5556 locations->AddTemp(Location::RegisterLocation(R3));
5557 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005558 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005559}
5560
Calin Juravle52c48962014-12-16 17:02:57 +00005561void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005562 const FieldInfo& field_info,
5563 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005564 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5565
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005566 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00005567 Register base = locations->InAt(0).AsRegister<Register>();
5568 Location value = locations->InAt(1);
5569
5570 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005571 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005572 Primitive::Type field_type = field_info.GetFieldType();
5573 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005574 bool needs_write_barrier =
5575 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005576
5577 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005578 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005579 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005580
5581 switch (field_type) {
5582 case Primitive::kPrimBoolean:
5583 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00005584 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005585 break;
5586 }
5587
5588 case Primitive::kPrimShort:
5589 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00005590 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005591 break;
5592 }
5593
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005594 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005595 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01005596 if (kPoisonHeapReferences && needs_write_barrier) {
5597 // Note that in the case where `value` is a null reference,
5598 // we do not enter this block, as a null reference does not
5599 // need poisoning.
5600 DCHECK_EQ(field_type, Primitive::kPrimNot);
5601 Register temp = locations->GetTemp(0).AsRegister<Register>();
5602 __ Mov(temp, value.AsRegister<Register>());
5603 __ PoisonHeapReference(temp);
5604 __ StoreToOffset(kStoreWord, temp, base, offset);
5605 } else {
5606 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
5607 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005608 break;
5609 }
5610
5611 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00005612 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005613 GenerateWideAtomicStore(base, offset,
5614 value.AsRegisterPairLow<Register>(),
5615 value.AsRegisterPairHigh<Register>(),
5616 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005617 locations->GetTemp(1).AsRegister<Register>(),
5618 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005619 } else {
5620 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005621 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005622 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005623 break;
5624 }
5625
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005626 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00005627 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005628 break;
5629 }
5630
5631 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005632 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005633 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005634 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
5635 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
5636
5637 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
5638
5639 GenerateWideAtomicStore(base, offset,
5640 value_reg_lo,
5641 value_reg_hi,
5642 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005643 locations->GetTemp(3).AsRegister<Register>(),
5644 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005645 } else {
5646 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005647 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005648 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005649 break;
5650 }
5651
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005652 case Primitive::kPrimVoid:
5653 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005654 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005655 }
Calin Juravle52c48962014-12-16 17:02:57 +00005656
Calin Juravle77520bc2015-01-12 18:45:46 +00005657 // Longs and doubles are handled in the switch.
5658 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
5659 codegen_->MaybeRecordImplicitNullCheck(instruction);
5660 }
5661
5662 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5663 Register temp = locations->GetTemp(0).AsRegister<Register>();
5664 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005665 codegen_->MarkGCCard(
5666 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005667 }
5668
Calin Juravle52c48962014-12-16 17:02:57 +00005669 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005670 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005671 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005672}
5673
Calin Juravle52c48962014-12-16 17:02:57 +00005674void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5675 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00005676
5677 bool object_field_get_with_read_barrier =
5678 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005679 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005680 new (GetGraph()->GetArena()) LocationSummary(instruction,
5681 object_field_get_with_read_barrier ?
5682 LocationSummary::kCallOnSlowPath :
5683 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005684 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005685 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005686 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005687 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00005688
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005689 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00005690 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005691 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00005692 // The output overlaps in case of volatile long: we don't want the
5693 // code generated by GenerateWideAtomicLoad to overwrite the
5694 // object's location. Likewise, in the case of an object field get
5695 // with read barriers enabled, we do not want the load to overwrite
5696 // the object's location, as we need it to emit the read barrier.
5697 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5698 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01005699
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005700 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5701 locations->SetOut(Location::RequiresFpuRegister());
5702 } else {
5703 locations->SetOut(Location::RequiresRegister(),
5704 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5705 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005706 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00005707 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005708 // - registers need to be consecutive
5709 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005710 // We don't test for ARM yet, and the assertion makes sure that we
5711 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005712 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5713 locations->AddTemp(Location::RequiresRegister());
5714 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00005715 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5716 // We need a temporary register for the read barrier marking slow
5717 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005718 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5719 !Runtime::Current()->UseJitCompilation()) {
5720 // If link-time thunks for the Baker read barrier are enabled, for AOT
5721 // loads we need a temporary only if the offset is too big.
5722 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5723 locations->AddTemp(Location::RequiresRegister());
5724 }
5725 // And we always need the reserved entrypoint register.
5726 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5727 } else {
5728 locations->AddTemp(Location::RequiresRegister());
5729 }
Calin Juravle52c48962014-12-16 17:02:57 +00005730 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005731}
5732
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005733Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5734 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
5735 << input->GetType();
5736 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5737 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5738 return Location::ConstantLocation(input->AsConstant());
5739 } else {
5740 return Location::RequiresFpuRegister();
5741 }
5742}
5743
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005744Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
5745 Opcode opcode) {
5746 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5747 if (constant->IsConstant() &&
5748 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5749 return Location::ConstantLocation(constant->AsConstant());
5750 }
5751 return Location::RequiresRegister();
5752}
5753
5754bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
5755 Opcode opcode) {
5756 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5757 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01005758 Opcode high_opcode = opcode;
5759 SetCc low_set_cc = kCcDontCare;
5760 switch (opcode) {
5761 case SUB:
5762 // Flip the operation to an ADD.
5763 value = -value;
5764 opcode = ADD;
5765 FALLTHROUGH_INTENDED;
5766 case ADD:
5767 if (Low32Bits(value) == 0u) {
5768 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5769 }
5770 high_opcode = ADC;
5771 low_set_cc = kCcSet;
5772 break;
5773 default:
5774 break;
5775 }
5776 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5777 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005778 } else {
5779 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5780 }
5781}
5782
Vladimir Marko59751a72016-08-05 14:37:27 +01005783bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
5784 Opcode opcode,
5785 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005786 ShifterOperand so;
5787 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01005788 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005789 return true;
5790 }
5791 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005792 uint32_t neg_value = 0;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005793 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005794 case AND: neg_opcode = BIC; neg_value = ~value; break;
5795 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5796 case ADD: neg_opcode = SUB; neg_value = -value; break;
5797 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5798 case SUB: neg_opcode = ADD; neg_value = -value; break;
5799 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5800 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005801 default:
5802 return false;
5803 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005804
5805 if (assembler->ShifterOperandCanHold(kNoRegister,
5806 kNoRegister,
5807 neg_opcode,
5808 neg_value,
5809 set_cc,
5810 &so)) {
5811 return true;
5812 }
5813
5814 return opcode == AND && IsPowerOfTwo(value + 1);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005815}
5816
Calin Juravle52c48962014-12-16 17:02:57 +00005817void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
5818 const FieldInfo& field_info) {
5819 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005820
Calin Juravle52c48962014-12-16 17:02:57 +00005821 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005822 Location base_loc = locations->InAt(0);
5823 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005824 Location out = locations->Out();
5825 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005826 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005827 Primitive::Type field_type = field_info.GetFieldType();
5828 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5829
5830 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00005831 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00005832 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005833 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005834
Roland Levillainc9285912015-12-18 10:38:42 +00005835 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00005836 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005837 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005838
Roland Levillainc9285912015-12-18 10:38:42 +00005839 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00005840 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005841 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005842
Roland Levillainc9285912015-12-18 10:38:42 +00005843 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00005844 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005845 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005846
5847 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00005848 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005849 break;
Roland Levillainc9285912015-12-18 10:38:42 +00005850
5851 case Primitive::kPrimNot: {
5852 // /* HeapReference<Object> */ out = *(base + offset)
5853 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5854 Location temp_loc = locations->GetTemp(0);
5855 // Note that a potential implicit null check is handled in this
5856 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
5857 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5858 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5859 if (is_volatile) {
5860 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5861 }
5862 } else {
5863 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
5864 codegen_->MaybeRecordImplicitNullCheck(instruction);
5865 if (is_volatile) {
5866 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5867 }
5868 // If read barriers are enabled, emit read barriers other than
5869 // Baker's using a slow path (and also unpoison the loaded
5870 // reference, if heap poisoning is enabled).
5871 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5872 }
5873 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005874 }
5875
Roland Levillainc9285912015-12-18 10:38:42 +00005876 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00005877 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005878 GenerateWideAtomicLoad(base, offset,
5879 out.AsRegisterPairLow<Register>(),
5880 out.AsRegisterPairHigh<Register>());
5881 } else {
5882 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
5883 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005884 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005885
Roland Levillainc9285912015-12-18 10:38:42 +00005886 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00005887 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005888 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005889
5890 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005891 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005892 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005893 Register lo = locations->GetTemp(0).AsRegister<Register>();
5894 Register hi = locations->GetTemp(1).AsRegister<Register>();
5895 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00005896 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005897 __ vmovdrr(out_reg, lo, hi);
5898 } else {
5899 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005900 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005901 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005902 break;
5903 }
5904
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005905 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005906 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005907 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005908 }
Calin Juravle52c48962014-12-16 17:02:57 +00005909
Roland Levillainc9285912015-12-18 10:38:42 +00005910 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5911 // Potential implicit null checks, in the case of reference or
5912 // double fields, are handled in the previous switch statement.
5913 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005914 codegen_->MaybeRecordImplicitNullCheck(instruction);
5915 }
5916
Calin Juravle52c48962014-12-16 17:02:57 +00005917 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005918 if (field_type == Primitive::kPrimNot) {
5919 // Memory barriers, in the case of references, are also handled
5920 // in the previous switch statement.
5921 } else {
5922 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5923 }
Roland Levillain4d027112015-07-01 15:41:14 +01005924 }
Calin Juravle52c48962014-12-16 17:02:57 +00005925}
5926
5927void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5928 HandleFieldSet(instruction, instruction->GetFieldInfo());
5929}
5930
5931void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005932 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005933}
5934
5935void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5936 HandleFieldGet(instruction, instruction->GetFieldInfo());
5937}
5938
5939void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5940 HandleFieldGet(instruction, instruction->GetFieldInfo());
5941}
5942
5943void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5944 HandleFieldGet(instruction, instruction->GetFieldInfo());
5945}
5946
5947void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5948 HandleFieldGet(instruction, instruction->GetFieldInfo());
5949}
5950
5951void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5952 HandleFieldSet(instruction, instruction->GetFieldInfo());
5953}
5954
5955void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005956 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005957}
5958
Calin Juravlee460d1d2015-09-29 04:52:17 +01005959void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
5960 HUnresolvedInstanceFieldGet* instruction) {
5961 FieldAccessCallingConventionARM calling_convention;
5962 codegen_->CreateUnresolvedFieldLocationSummary(
5963 instruction, instruction->GetFieldType(), calling_convention);
5964}
5965
5966void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
5967 HUnresolvedInstanceFieldGet* instruction) {
5968 FieldAccessCallingConventionARM calling_convention;
5969 codegen_->GenerateUnresolvedFieldAccess(instruction,
5970 instruction->GetFieldType(),
5971 instruction->GetFieldIndex(),
5972 instruction->GetDexPc(),
5973 calling_convention);
5974}
5975
5976void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
5977 HUnresolvedInstanceFieldSet* instruction) {
5978 FieldAccessCallingConventionARM calling_convention;
5979 codegen_->CreateUnresolvedFieldLocationSummary(
5980 instruction, instruction->GetFieldType(), calling_convention);
5981}
5982
5983void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
5984 HUnresolvedInstanceFieldSet* instruction) {
5985 FieldAccessCallingConventionARM calling_convention;
5986 codegen_->GenerateUnresolvedFieldAccess(instruction,
5987 instruction->GetFieldType(),
5988 instruction->GetFieldIndex(),
5989 instruction->GetDexPc(),
5990 calling_convention);
5991}
5992
5993void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
5994 HUnresolvedStaticFieldGet* instruction) {
5995 FieldAccessCallingConventionARM calling_convention;
5996 codegen_->CreateUnresolvedFieldLocationSummary(
5997 instruction, instruction->GetFieldType(), calling_convention);
5998}
5999
6000void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
6001 HUnresolvedStaticFieldGet* instruction) {
6002 FieldAccessCallingConventionARM calling_convention;
6003 codegen_->GenerateUnresolvedFieldAccess(instruction,
6004 instruction->GetFieldType(),
6005 instruction->GetFieldIndex(),
6006 instruction->GetDexPc(),
6007 calling_convention);
6008}
6009
6010void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
6011 HUnresolvedStaticFieldSet* instruction) {
6012 FieldAccessCallingConventionARM calling_convention;
6013 codegen_->CreateUnresolvedFieldLocationSummary(
6014 instruction, instruction->GetFieldType(), calling_convention);
6015}
6016
6017void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
6018 HUnresolvedStaticFieldSet* instruction) {
6019 FieldAccessCallingConventionARM calling_convention;
6020 codegen_->GenerateUnresolvedFieldAccess(instruction,
6021 instruction->GetFieldType(),
6022 instruction->GetFieldIndex(),
6023 instruction->GetDexPc(),
6024 calling_convention);
6025}
6026
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006027void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006028 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6029 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006030}
6031
Calin Juravle2ae48182016-03-16 14:05:09 +00006032void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
6033 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006034 return;
6035 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006036 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00006037
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006038 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006039 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006040}
6041
Calin Juravle2ae48182016-03-16 14:05:09 +00006042void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01006043 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006044 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006045
6046 LocationSummary* locations = instruction->GetLocations();
6047 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006048
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006049 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006050}
6051
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006052void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006053 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006054}
6055
Artem Serov6c916792016-07-11 14:02:34 +01006056static LoadOperandType GetLoadOperandType(Primitive::Type type) {
6057 switch (type) {
6058 case Primitive::kPrimNot:
6059 return kLoadWord;
6060 case Primitive::kPrimBoolean:
6061 return kLoadUnsignedByte;
6062 case Primitive::kPrimByte:
6063 return kLoadSignedByte;
6064 case Primitive::kPrimChar:
6065 return kLoadUnsignedHalfword;
6066 case Primitive::kPrimShort:
6067 return kLoadSignedHalfword;
6068 case Primitive::kPrimInt:
6069 return kLoadWord;
6070 case Primitive::kPrimLong:
6071 return kLoadWordPair;
6072 case Primitive::kPrimFloat:
6073 return kLoadSWord;
6074 case Primitive::kPrimDouble:
6075 return kLoadDWord;
6076 default:
6077 LOG(FATAL) << "Unreachable type " << type;
6078 UNREACHABLE();
6079 }
6080}
6081
6082static StoreOperandType GetStoreOperandType(Primitive::Type type) {
6083 switch (type) {
6084 case Primitive::kPrimNot:
6085 return kStoreWord;
6086 case Primitive::kPrimBoolean:
6087 case Primitive::kPrimByte:
6088 return kStoreByte;
6089 case Primitive::kPrimChar:
6090 case Primitive::kPrimShort:
6091 return kStoreHalfword;
6092 case Primitive::kPrimInt:
6093 return kStoreWord;
6094 case Primitive::kPrimLong:
6095 return kStoreWordPair;
6096 case Primitive::kPrimFloat:
6097 return kStoreSWord;
6098 case Primitive::kPrimDouble:
6099 return kStoreDWord;
6100 default:
6101 LOG(FATAL) << "Unreachable type " << type;
6102 UNREACHABLE();
6103 }
6104}
6105
6106void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
6107 Location out_loc,
6108 Register base,
6109 Register reg_offset,
6110 Condition cond) {
6111 uint32_t shift_count = Primitive::ComponentSizeShift(type);
6112 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
6113
6114 switch (type) {
6115 case Primitive::kPrimByte:
6116 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
6117 break;
6118 case Primitive::kPrimBoolean:
6119 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
6120 break;
6121 case Primitive::kPrimShort:
6122 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
6123 break;
6124 case Primitive::kPrimChar:
6125 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
6126 break;
6127 case Primitive::kPrimNot:
6128 case Primitive::kPrimInt:
6129 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
6130 break;
6131 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
6132 case Primitive::kPrimLong:
6133 case Primitive::kPrimFloat:
6134 case Primitive::kPrimDouble:
6135 default:
6136 LOG(FATAL) << "Unreachable type " << type;
6137 UNREACHABLE();
6138 }
6139}
6140
6141void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
6142 Location loc,
6143 Register base,
6144 Register reg_offset,
6145 Condition cond) {
6146 uint32_t shift_count = Primitive::ComponentSizeShift(type);
6147 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
6148
6149 switch (type) {
6150 case Primitive::kPrimByte:
6151 case Primitive::kPrimBoolean:
6152 __ strb(loc.AsRegister<Register>(), mem_address, cond);
6153 break;
6154 case Primitive::kPrimShort:
6155 case Primitive::kPrimChar:
6156 __ strh(loc.AsRegister<Register>(), mem_address, cond);
6157 break;
6158 case Primitive::kPrimNot:
6159 case Primitive::kPrimInt:
6160 __ str(loc.AsRegister<Register>(), mem_address, cond);
6161 break;
6162 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
6163 case Primitive::kPrimLong:
6164 case Primitive::kPrimFloat:
6165 case Primitive::kPrimDouble:
6166 default:
6167 LOG(FATAL) << "Unreachable type " << type;
6168 UNREACHABLE();
6169 }
6170}
6171
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006172void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006173 bool object_array_get_with_read_barrier =
6174 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006175 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00006176 new (GetGraph()->GetArena()) LocationSummary(instruction,
6177 object_array_get_with_read_barrier ?
6178 LocationSummary::kCallOnSlowPath :
6179 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01006180 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006181 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006182 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006183 locations->SetInAt(0, Location::RequiresRegister());
6184 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006185 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6186 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6187 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00006188 // The output overlaps in the case of an object array get with
6189 // read barriers enabled: we do not want the move to overwrite the
6190 // array's location, as we need it to emit the read barrier.
6191 locations->SetOut(
6192 Location::RequiresRegister(),
6193 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006194 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006195 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
6196 // We need a temporary register for the read barrier marking slow
6197 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
6198 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6199 !Runtime::Current()->UseJitCompilation() &&
6200 instruction->GetIndex()->IsConstant()) {
6201 // Array loads with constant index are treated as field loads.
6202 // If link-time thunks for the Baker read barrier are enabled, for AOT
6203 // constant index loads we need a temporary only if the offset is too big.
6204 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6205 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
6206 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
6207 if (offset >= kReferenceLoadMinFarOffset) {
6208 locations->AddTemp(Location::RequiresRegister());
6209 }
6210 // And we always need the reserved entrypoint register.
6211 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6212 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6213 !Runtime::Current()->UseJitCompilation() &&
6214 !instruction->GetIndex()->IsConstant()) {
6215 // We need a non-scratch temporary for the array data pointer.
6216 locations->AddTemp(Location::RequiresRegister());
6217 // And we always need the reserved entrypoint register.
6218 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6219 } else {
6220 locations->AddTemp(Location::RequiresRegister());
6221 }
6222 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6223 // Also need a temporary for String compression feature.
Roland Levillainc9285912015-12-18 10:38:42 +00006224 locations->AddTemp(Location::RequiresRegister());
6225 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006226}
6227
6228void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
6229 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006230 Location obj_loc = locations->InAt(0);
6231 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006232 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00006233 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006234 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00006235 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07006236 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6237 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01006238 HInstruction* array_instr = instruction->GetArray();
6239 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01006240
Roland Levillain4d027112015-07-01 15:41:14 +01006241 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01006242 case Primitive::kPrimBoolean:
6243 case Primitive::kPrimByte:
6244 case Primitive::kPrimShort:
6245 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00006246 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006247 Register length;
6248 if (maybe_compressed_char_at) {
6249 length = locations->GetTemp(0).AsRegister<Register>();
6250 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6251 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
6252 codegen_->MaybeRecordImplicitNullCheck(instruction);
6253 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006254 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01006255 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07006256 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07006257 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006258 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006259 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6260 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6261 "Expecting 0=compressed, 1=uncompressed");
6262 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07006263 __ LoadFromOffset(kLoadUnsignedByte,
6264 out_loc.AsRegister<Register>(),
6265 obj,
6266 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006267 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07006268 __ Bind(&uncompressed_load);
6269 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
6270 out_loc.AsRegister<Register>(),
6271 obj,
6272 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006273 if (done.IsLinked()) {
6274 __ Bind(&done);
6275 }
jessicahandojo05765752016-09-09 19:01:32 -07006276 } else {
6277 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01006278
jessicahandojo05765752016-09-09 19:01:32 -07006279 LoadOperandType load_type = GetLoadOperandType(type);
6280 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
6281 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006282 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006283 Register temp = IP;
6284
6285 if (has_intermediate_address) {
6286 // We do not need to compute the intermediate address from the array: the
6287 // input instruction has done it already. See the comment in
6288 // `TryExtractArrayAccessAddress()`.
6289 if (kIsDebugBuild) {
6290 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6291 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6292 }
6293 temp = obj;
6294 } else {
6295 __ add(temp, obj, ShifterOperand(data_offset));
6296 }
jessicahandojo05765752016-09-09 19:01:32 -07006297 if (maybe_compressed_char_at) {
6298 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006299 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006300 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6301 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6302 "Expecting 0=compressed, 1=uncompressed");
6303 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07006304 __ ldrb(out_loc.AsRegister<Register>(),
6305 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006306 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07006307 __ Bind(&uncompressed_load);
6308 __ ldrh(out_loc.AsRegister<Register>(),
6309 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006310 if (done.IsLinked()) {
6311 __ Bind(&done);
6312 }
jessicahandojo05765752016-09-09 19:01:32 -07006313 } else {
6314 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
6315 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006316 }
6317 break;
6318 }
6319
Roland Levillainc9285912015-12-18 10:38:42 +00006320 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00006321 // The read barrier instrumentation of object ArrayGet
6322 // instructions does not support the HIntermediateAddress
6323 // instruction.
6324 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6325
Roland Levillainc9285912015-12-18 10:38:42 +00006326 static_assert(
6327 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6328 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006329 // /* HeapReference<Object> */ out =
6330 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6331 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
6332 Location temp = locations->GetTemp(0);
6333 // Note that a potential implicit null check is handled in this
6334 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006335 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6336 if (index.IsConstant()) {
6337 // Array load with a constant index can be treated as a field load.
6338 data_offset += helpers::Int32ConstantFrom(index) << Primitive::ComponentSizeShift(type);
6339 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6340 out_loc,
6341 obj,
6342 data_offset,
6343 locations->GetTemp(0),
6344 /* needs_null_check */ false);
6345 } else {
6346 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6347 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6348 }
Roland Levillainc9285912015-12-18 10:38:42 +00006349 } else {
6350 Register out = out_loc.AsRegister<Register>();
6351 if (index.IsConstant()) {
6352 size_t offset =
6353 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6354 __ LoadFromOffset(kLoadWord, out, obj, offset);
6355 codegen_->MaybeRecordImplicitNullCheck(instruction);
6356 // If read barriers are enabled, emit read barriers other than
6357 // Baker's using a slow path (and also unpoison the loaded
6358 // reference, if heap poisoning is enabled).
6359 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6360 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006361 Register temp = IP;
6362
6363 if (has_intermediate_address) {
6364 // We do not need to compute the intermediate address from the array: the
6365 // input instruction has done it already. See the comment in
6366 // `TryExtractArrayAccessAddress()`.
6367 if (kIsDebugBuild) {
6368 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6369 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6370 }
6371 temp = obj;
6372 } else {
6373 __ add(temp, obj, ShifterOperand(data_offset));
6374 }
6375 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01006376
Roland Levillainc9285912015-12-18 10:38:42 +00006377 codegen_->MaybeRecordImplicitNullCheck(instruction);
6378 // If read barriers are enabled, emit read barriers other than
6379 // Baker's using a slow path (and also unpoison the loaded
6380 // reference, if heap poisoning is enabled).
6381 codegen_->MaybeGenerateReadBarrierSlow(
6382 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6383 }
6384 }
6385 break;
6386 }
6387
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006388 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006389 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006390 size_t offset =
6391 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006392 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006393 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006394 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00006395 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006396 }
6397 break;
6398 }
6399
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006400 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00006401 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006402 if (index.IsConstant()) {
6403 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006404 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006405 } else {
6406 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00006407 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006408 }
6409 break;
6410 }
6411
6412 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00006413 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006414 if (index.IsConstant()) {
6415 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006416 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006417 } else {
6418 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00006419 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006420 }
6421 break;
6422 }
6423
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006424 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01006425 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006426 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006427 }
Roland Levillain4d027112015-07-01 15:41:14 +01006428
6429 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00006430 // Potential implicit null checks, in the case of reference
6431 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07006432 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00006433 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01006434 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006435}
6436
6437void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006438 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006439
6440 bool needs_write_barrier =
6441 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00006442 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006443
Nicolas Geoffray39468442014-09-02 15:17:15 +01006444 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006445 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01006446 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00006447 LocationSummary::kCallOnSlowPath :
6448 LocationSummary::kNoCall);
6449
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006450 locations->SetInAt(0, Location::RequiresRegister());
6451 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6452 if (Primitive::IsFloatingPointType(value_type)) {
6453 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006454 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006455 locations->SetInAt(2, Location::RequiresRegister());
6456 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006457 if (needs_write_barrier) {
6458 // Temporary registers for the write barrier.
6459 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006460 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006461 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006462}
6463
6464void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
6465 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006466 Location array_loc = locations->InAt(0);
6467 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006468 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006469 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00006470 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006471 bool needs_write_barrier =
6472 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01006473 uint32_t data_offset =
6474 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6475 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01006476 HInstruction* array_instr = instruction->GetArray();
6477 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006478
6479 switch (value_type) {
6480 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01006481 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006482 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01006483 case Primitive::kPrimChar:
6484 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006485 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01006486 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
6487 uint32_t full_offset =
6488 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6489 StoreOperandType store_type = GetStoreOperandType(value_type);
6490 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006491 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006492 Register temp = IP;
6493
6494 if (has_intermediate_address) {
6495 // We do not need to compute the intermediate address from the array: the
6496 // input instruction has done it already. See the comment in
6497 // `TryExtractArrayAccessAddress()`.
6498 if (kIsDebugBuild) {
6499 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6500 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
6501 }
6502 temp = array;
6503 } else {
6504 __ add(temp, array, ShifterOperand(data_offset));
6505 }
Artem Serov6c916792016-07-11 14:02:34 +01006506 codegen_->StoreToShiftedRegOffset(value_type,
6507 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01006508 temp,
Artem Serov6c916792016-07-11 14:02:34 +01006509 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006510 }
6511 break;
6512 }
6513
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006514 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00006515 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01006516 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6517 // See the comment in instruction_simplifier_shared.cc.
6518 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006519
6520 if (instruction->InputAt(2)->IsNullConstant()) {
6521 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006522 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006523 size_t offset =
6524 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01006525 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006526 } else {
6527 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006528 __ add(IP, array, ShifterOperand(data_offset));
6529 codegen_->StoreToShiftedRegOffset(value_type,
6530 value_loc,
6531 IP,
6532 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006533 }
Roland Levillain1407ee72016-01-08 15:56:19 +00006534 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00006535 DCHECK(!needs_write_barrier);
6536 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006537 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006538 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006539
6540 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01006541 Location temp1_loc = locations->GetTemp(0);
6542 Register temp1 = temp1_loc.AsRegister<Register>();
6543 Location temp2_loc = locations->GetTemp(1);
6544 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006545 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6546 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6547 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6548 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006549 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006550 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006551
Roland Levillain3b359c72015-11-17 19:35:12 +00006552 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006553 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
6554 codegen_->AddSlowPath(slow_path);
6555 if (instruction->GetValueCanBeNull()) {
6556 Label non_zero;
6557 __ CompareAndBranchIfNonZero(value, &non_zero);
6558 if (index.IsConstant()) {
6559 size_t offset =
6560 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6561 __ StoreToOffset(kStoreWord, value, array, offset);
6562 } else {
6563 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006564 __ add(IP, array, ShifterOperand(data_offset));
6565 codegen_->StoreToShiftedRegOffset(value_type,
6566 value_loc,
6567 IP,
6568 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006569 }
6570 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006571 __ b(final_label);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006572 __ Bind(&non_zero);
6573 }
6574
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006575 // Note that when read barriers are enabled, the type checks
6576 // are performed without read barriers. This is fine, even in
6577 // the case where a class object is in the from-space after
6578 // the flip, as a comparison involving such a type would not
6579 // produce a false positive; it may of course produce a false
6580 // negative, in which case we would take the ArraySet slow
6581 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006582
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006583 // /* HeapReference<Class> */ temp1 = array->klass_
6584 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
6585 codegen_->MaybeRecordImplicitNullCheck(instruction);
6586 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01006587
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006588 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6589 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6590 // /* HeapReference<Class> */ temp2 = value->klass_
6591 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
6592 // If heap poisoning is enabled, no need to unpoison `temp1`
6593 // nor `temp2`, as we are comparing two poisoned references.
6594 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01006595
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006596 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6597 Label do_put;
6598 __ b(&do_put, EQ);
6599 // If heap poisoning is enabled, the `temp1` reference has
6600 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00006601 __ MaybeUnpoisonHeapReference(temp1);
6602
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006603 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6604 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6605 // If heap poisoning is enabled, no need to unpoison
6606 // `temp1`, as we are comparing against null below.
6607 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
6608 __ Bind(&do_put);
6609 } else {
6610 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006611 }
6612 }
6613
Artem Serov6c916792016-07-11 14:02:34 +01006614 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006615 if (kPoisonHeapReferences) {
6616 // Note that in the case where `value` is a null reference,
6617 // we do not enter this block, as a null reference does not
6618 // need poisoning.
6619 DCHECK_EQ(value_type, Primitive::kPrimNot);
6620 __ Mov(temp1, value);
6621 __ PoisonHeapReference(temp1);
6622 source = temp1;
6623 }
6624
6625 if (index.IsConstant()) {
6626 size_t offset =
6627 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6628 __ StoreToOffset(kStoreWord, source, array, offset);
6629 } else {
6630 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006631
6632 __ add(IP, array, ShifterOperand(data_offset));
6633 codegen_->StoreToShiftedRegOffset(value_type,
6634 Location::RegisterLocation(source),
6635 IP,
6636 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006637 }
6638
Roland Levillain3b359c72015-11-17 19:35:12 +00006639 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006640 codegen_->MaybeRecordImplicitNullCheck(instruction);
6641 }
6642
6643 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6644
6645 if (done.IsLinked()) {
6646 __ Bind(&done);
6647 }
6648
6649 if (slow_path != nullptr) {
6650 __ Bind(slow_path->GetExitLabel());
6651 }
6652
6653 break;
6654 }
6655
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006656 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006657 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006658 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006659 size_t offset =
6660 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006661 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006662 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006663 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006664 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006665 }
6666 break;
6667 }
6668
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006669 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006670 Location value = locations->InAt(2);
6671 DCHECK(value.IsFpuRegister());
6672 if (index.IsConstant()) {
6673 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006674 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006675 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006676 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006677 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
6678 }
6679 break;
6680 }
6681
6682 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006683 Location value = locations->InAt(2);
6684 DCHECK(value.IsFpuRegisterPair());
6685 if (index.IsConstant()) {
6686 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006687 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006688 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006689 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006690 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
6691 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006692
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006693 break;
6694 }
6695
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006696 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006697 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006698 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006699 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006700
Roland Levillain80e67092016-01-08 16:04:55 +00006701 // Objects are handled in the switch.
6702 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006703 codegen_->MaybeRecordImplicitNullCheck(instruction);
6704 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006705}
6706
6707void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006708 LocationSummary* locations =
6709 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006710 locations->SetInAt(0, Location::RequiresRegister());
6711 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006712}
6713
6714void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
6715 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006716 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006717 Register obj = locations->InAt(0).AsRegister<Register>();
6718 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006719 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00006720 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07006721 // Mask out compression flag from String's array length.
6722 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006723 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07006724 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006725}
6726
Artem Serov328429f2016-07-06 16:23:04 +01006727void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01006728 LocationSummary* locations =
6729 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6730
6731 locations->SetInAt(0, Location::RequiresRegister());
6732 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6733 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6734}
6735
6736void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6737 LocationSummary* locations = instruction->GetLocations();
6738 Location out = locations->Out();
6739 Location first = locations->InAt(0);
6740 Location second = locations->InAt(1);
6741
Artem Serov328429f2016-07-06 16:23:04 +01006742 if (second.IsRegister()) {
6743 __ add(out.AsRegister<Register>(),
6744 first.AsRegister<Register>(),
6745 ShifterOperand(second.AsRegister<Register>()));
6746 } else {
6747 __ AddConstant(out.AsRegister<Register>(),
6748 first.AsRegister<Register>(),
6749 second.GetConstant()->AsIntConstant()->GetValue());
6750 }
6751}
6752
Artem Serove1811ed2017-04-27 16:50:47 +01006753void LocationsBuilderARM::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
6754 LOG(FATAL) << "Unreachable " << instruction->GetId();
6755}
6756
6757void InstructionCodeGeneratorARM::VisitIntermediateAddressIndex(
6758 HIntermediateAddressIndex* instruction) {
6759 LOG(FATAL) << "Unreachable " << instruction->GetId();
6760}
6761
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006762void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006763 RegisterSet caller_saves = RegisterSet::Empty();
6764 InvokeRuntimeCallingConvention calling_convention;
6765 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6766 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6767 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006768
6769 HInstruction* index = instruction->InputAt(0);
6770 HInstruction* length = instruction->InputAt(1);
6771 // If both index and length are constants we can statically check the bounds. But if at least one
6772 // of them is not encodable ArmEncodableConstantOrRegister will create
6773 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6774 // locations.
6775 bool both_const = index->IsConstant() && length->IsConstant();
6776 locations->SetInAt(0, both_const
6777 ? Location::ConstantLocation(index->AsConstant())
6778 : ArmEncodableConstantOrRegister(index, CMP));
6779 locations->SetInAt(1, both_const
6780 ? Location::ConstantLocation(length->AsConstant())
6781 : ArmEncodableConstantOrRegister(length, CMP));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006782}
6783
6784void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
6785 LocationSummary* locations = instruction->GetLocations();
Artem Serov2dd053d2017-03-08 14:54:06 +00006786 Location index_loc = locations->InAt(0);
6787 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006788
Artem Serov2dd053d2017-03-08 14:54:06 +00006789 if (length_loc.IsConstant()) {
6790 int32_t length = helpers::Int32ConstantFrom(length_loc);
6791 if (index_loc.IsConstant()) {
6792 // BCE will remove the bounds check if we are guaranteed to pass.
6793 int32_t index = helpers::Int32ConstantFrom(index_loc);
6794 if (index < 0 || index >= length) {
6795 SlowPathCodeARM* slow_path =
6796 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6797 codegen_->AddSlowPath(slow_path);
6798 __ b(slow_path->GetEntryLabel());
6799 } else {
6800 // Some optimization after BCE may have generated this, and we should not
6801 // generate a bounds check if it is a valid range.
6802 }
6803 return;
6804 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006805
Artem Serov2dd053d2017-03-08 14:54:06 +00006806 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6807 __ cmp(index_loc.AsRegister<Register>(), ShifterOperand(length));
6808 codegen_->AddSlowPath(slow_path);
6809 __ b(slow_path->GetEntryLabel(), HS);
6810 } else {
6811 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6812 if (index_loc.IsConstant()) {
6813 int32_t index = helpers::Int32ConstantFrom(index_loc);
6814 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index));
6815 } else {
6816 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index_loc.AsRegister<Register>()));
6817 }
6818 codegen_->AddSlowPath(slow_path);
6819 __ b(slow_path->GetEntryLabel(), LS);
6820 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006821}
6822
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006823void CodeGeneratorARM::MarkGCCard(Register temp,
6824 Register card,
6825 Register object,
6826 Register value,
6827 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006828 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006829 if (can_be_null) {
6830 __ CompareAndBranchIfZero(value, &is_null);
6831 }
Andreas Gampe542451c2016-07-26 09:02:02 -07006832 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006833 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
6834 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006835 if (can_be_null) {
6836 __ Bind(&is_null);
6837 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006838}
6839
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006840void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006841 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006842}
6843
6844void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006845 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6846}
6847
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006848void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006849 LocationSummary* locations =
6850 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006851 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006852}
6853
6854void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006855 HBasicBlock* block = instruction->GetBlock();
6856 if (block->GetLoopInformation() != nullptr) {
6857 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6858 // The back edge will generate the suspend check.
6859 return;
6860 }
6861 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6862 // The goto will generate the suspend check.
6863 return;
6864 }
6865 GenerateSuspendCheck(instruction, nullptr);
6866}
6867
6868void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
6869 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006870 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006871 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
6872 if (slow_path == nullptr) {
6873 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
6874 instruction->SetSlowPath(slow_path);
6875 codegen_->AddSlowPath(slow_path);
6876 if (successor != nullptr) {
6877 DCHECK(successor->IsLoopHeader());
6878 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6879 }
6880 } else {
6881 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6882 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006883
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00006884 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006885 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006886 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006887 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006888 __ Bind(slow_path->GetReturnLabel());
6889 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006890 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006891 __ b(slow_path->GetEntryLabel());
6892 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006893}
6894
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006895ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
6896 return codegen_->GetAssembler();
6897}
6898
6899void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006900 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006901 Location source = move->GetSource();
6902 Location destination = move->GetDestination();
6903
6904 if (source.IsRegister()) {
6905 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006906 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006907 } else if (destination.IsFpuRegister()) {
6908 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006909 } else {
6910 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006911 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006912 SP, destination.GetStackIndex());
6913 }
6914 } else if (source.IsStackSlot()) {
6915 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006916 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006917 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006918 } else if (destination.IsFpuRegister()) {
6919 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006920 } else {
6921 DCHECK(destination.IsStackSlot());
6922 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
6923 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6924 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006925 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006926 if (destination.IsRegister()) {
6927 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
6928 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006929 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006930 } else {
6931 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006932 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
6933 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006934 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006935 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006936 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
6937 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006938 } else if (destination.IsRegisterPair()) {
6939 DCHECK(ExpectedPairLayout(destination));
6940 __ LoadFromOffset(
6941 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
6942 } else {
6943 DCHECK(destination.IsFpuRegisterPair()) << destination;
6944 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6945 SP,
6946 source.GetStackIndex());
6947 }
6948 } else if (source.IsRegisterPair()) {
6949 if (destination.IsRegisterPair()) {
6950 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6951 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006952 } else if (destination.IsFpuRegisterPair()) {
6953 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6954 source.AsRegisterPairLow<Register>(),
6955 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006956 } else {
6957 DCHECK(destination.IsDoubleStackSlot()) << destination;
6958 DCHECK(ExpectedPairLayout(source));
6959 __ StoreToOffset(
6960 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
6961 }
6962 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006963 if (destination.IsRegisterPair()) {
6964 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6965 destination.AsRegisterPairHigh<Register>(),
6966 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6967 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006968 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6969 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6970 } else {
6971 DCHECK(destination.IsDoubleStackSlot()) << destination;
6972 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
6973 SP,
6974 destination.GetStackIndex());
6975 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006976 } else {
6977 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006978 HConstant* constant = source.GetConstant();
6979 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6980 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006981 if (destination.IsRegister()) {
6982 __ LoadImmediate(destination.AsRegister<Register>(), value);
6983 } else {
6984 DCHECK(destination.IsStackSlot());
6985 __ LoadImmediate(IP, value);
6986 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6987 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006988 } else if (constant->IsLongConstant()) {
6989 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006990 if (destination.IsRegisterPair()) {
6991 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
6992 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006993 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006994 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006995 __ LoadImmediate(IP, Low32Bits(value));
6996 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6997 __ LoadImmediate(IP, High32Bits(value));
6998 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6999 }
7000 } else if (constant->IsDoubleConstant()) {
7001 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007002 if (destination.IsFpuRegisterPair()) {
7003 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00007004 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007005 DCHECK(destination.IsDoubleStackSlot()) << destination;
7006 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00007007 __ LoadImmediate(IP, Low32Bits(int_value));
7008 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
7009 __ LoadImmediate(IP, High32Bits(int_value));
7010 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
7011 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007012 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00007013 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007014 float value = constant->AsFloatConstant()->GetValue();
7015 if (destination.IsFpuRegister()) {
7016 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
7017 } else {
7018 DCHECK(destination.IsStackSlot());
7019 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
7020 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
7021 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01007022 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007023 }
7024}
7025
7026void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
7027 __ Mov(IP, reg);
7028 __ LoadFromOffset(kLoadWord, reg, SP, mem);
7029 __ StoreToOffset(kStoreWord, IP, SP, mem);
7030}
7031
7032void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
7033 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
7034 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7035 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
7036 SP, mem1 + stack_offset);
7037 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
7038 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
7039 SP, mem2 + stack_offset);
7040 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
7041}
7042
7043void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007044 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007045 Location source = move->GetSource();
7046 Location destination = move->GetDestination();
7047
7048 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007049 DCHECK_NE(source.AsRegister<Register>(), IP);
7050 DCHECK_NE(destination.AsRegister<Register>(), IP);
7051 __ Mov(IP, source.AsRegister<Register>());
7052 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
7053 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007054 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007055 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007056 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007057 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007058 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
7059 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007060 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00007061 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007062 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00007063 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007064 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007065 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007066 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007067 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007068 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
7069 destination.AsRegisterPairHigh<Register>(),
7070 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007071 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007072 Register low_reg = source.IsRegisterPair()
7073 ? source.AsRegisterPairLow<Register>()
7074 : destination.AsRegisterPairLow<Register>();
7075 int mem = source.IsRegisterPair()
7076 ? destination.GetStackIndex()
7077 : source.GetStackIndex();
7078 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007079 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007080 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007081 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007082 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007083 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
7084 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007085 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007086 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007087 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007088 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
7089 DRegister reg = source.IsFpuRegisterPair()
7090 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
7091 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
7092 int mem = source.IsFpuRegisterPair()
7093 ? destination.GetStackIndex()
7094 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007095 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007096 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007097 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007098 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
7099 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
7100 : destination.AsFpuRegister<SRegister>();
7101 int mem = source.IsFpuRegister()
7102 ? destination.GetStackIndex()
7103 : source.GetStackIndex();
7104
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00007105 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007106 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00007107 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00007108 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00007109 Exchange(source.GetStackIndex(), destination.GetStackIndex());
7110 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007111 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00007112 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007113 }
7114}
7115
7116void ParallelMoveResolverARM::SpillScratch(int reg) {
7117 __ Push(static_cast<Register>(reg));
7118}
7119
7120void ParallelMoveResolverARM::RestoreScratch(int reg) {
7121 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007122}
7123
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007124HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
7125 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007126 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007127 case HLoadClass::LoadKind::kInvalid:
7128 LOG(FATAL) << "UNREACHABLE";
7129 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007130 case HLoadClass::LoadKind::kReferrersClass:
7131 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007132 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007133 case HLoadClass::LoadKind::kBssEntry:
7134 DCHECK(!Runtime::Current()->UseJitCompilation());
7135 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007136 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007137 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007138 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007139 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007140 case HLoadClass::LoadKind::kDexCacheViaMethod:
7141 break;
7142 }
7143 return desired_class_load_kind;
7144}
7145
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007146void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007147 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7148 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007149 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007150 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007151 cls,
7152 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007153 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007154 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007155 return;
7156 }
Vladimir Marko41559982017-01-06 14:04:23 +00007157 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007158
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007159 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7160 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007161 ? LocationSummary::kCallOnSlowPath
7162 : LocationSummary::kNoCall;
7163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007164 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007165 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007166 }
7167
Vladimir Marko41559982017-01-06 14:04:23 +00007168 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007169 locations->SetInAt(0, Location::RequiresRegister());
7170 }
7171 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007172 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7173 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7174 // Rely on the type resolution or initialization and marking to save everything we need.
7175 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7176 // to the custom calling convention) or by marking, so we request a different temp.
7177 locations->AddTemp(Location::RequiresRegister());
7178 RegisterSet caller_saves = RegisterSet::Empty();
7179 InvokeRuntimeCallingConvention calling_convention;
7180 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7181 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7182 // that the the kPrimNot result register is the same as the first argument register.
7183 locations->SetCustomSlowPathCallerSaves(caller_saves);
7184 } else {
7185 // For non-Baker read barrier we have a temp-clobbering call.
7186 }
7187 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007188 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7189 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7190 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7191 !Runtime::Current()->UseJitCompilation())) {
7192 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
7193 }
7194 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007195}
7196
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007197// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7198// move.
7199void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007200 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
7201 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
7202 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01007203 return;
7204 }
Vladimir Marko41559982017-01-06 14:04:23 +00007205 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01007206
Vladimir Marko41559982017-01-06 14:04:23 +00007207 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007208 Location out_loc = locations->Out();
7209 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007210
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007211 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7212 ? kWithoutReadBarrier
7213 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007214 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007215 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007216 case HLoadClass::LoadKind::kReferrersClass: {
7217 DCHECK(!cls->CanCallRuntime());
7218 DCHECK(!cls->MustGenerateClinitCheck());
7219 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7220 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007221 GenerateGcRootFieldLoad(cls,
7222 out_loc,
7223 current_method,
7224 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007225 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007226 break;
7227 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007228 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007229 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007230 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007231 CodeGeneratorARM::PcRelativePatchInfo* labels =
7232 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7233 __ BindTrackedLabel(&labels->movw_label);
7234 __ movw(out, /* placeholder */ 0u);
7235 __ BindTrackedLabel(&labels->movt_label);
7236 __ movt(out, /* placeholder */ 0u);
7237 __ BindTrackedLabel(&labels->add_pc_label);
7238 __ add(out, out, ShifterOperand(PC));
7239 break;
7240 }
7241 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007242 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007243 uint32_t address = dchecked_integral_cast<uint32_t>(
7244 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7245 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007246 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7247 break;
7248 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007249 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007250 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7251 ? locations->GetTemp(0).AsRegister<Register>()
7252 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007253 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007254 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007255 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00007256 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007257 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00007258 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007259 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00007260 __ add(temp, temp, ShifterOperand(PC));
7261 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007262 generate_null_check = true;
7263 break;
7264 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007265 case HLoadClass::LoadKind::kJitTableAddress: {
7266 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7267 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007268 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007269 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007270 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007271 break;
7272 }
Vladimir Marko41559982017-01-06 14:04:23 +00007273 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007274 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007275 LOG(FATAL) << "UNREACHABLE";
7276 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007277 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007278
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007279 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7280 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01007281 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007282 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7283 codegen_->AddSlowPath(slow_path);
7284 if (generate_null_check) {
7285 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7286 }
7287 if (cls->MustGenerateClinitCheck()) {
7288 GenerateClassInitializationCheck(slow_path, out);
7289 } else {
7290 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007291 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007292 }
7293}
7294
7295void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
7296 LocationSummary* locations =
7297 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
7298 locations->SetInAt(0, Location::RequiresRegister());
7299 if (check->HasUses()) {
7300 locations->SetOut(Location::SameAsFirstInput());
7301 }
7302}
7303
7304void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007305 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007306 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007307 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007308 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00007309 GenerateClassInitializationCheck(slow_path,
7310 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007311}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007312
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007313void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01007314 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007315 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
7316 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
7317 __ b(slow_path->GetEntryLabel(), LT);
7318 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7319 // properly. Therefore, we do a memory fence.
7320 __ dmb(ISH);
7321 __ Bind(slow_path->GetExitLabel());
7322}
7323
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007324HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
7325 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007326 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007327 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007328 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01007329 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007330 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007331 case HLoadString::LoadKind::kJitTableAddress:
7332 DCHECK(Runtime::Current()->UseJitCompilation());
7333 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007334 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007335 case HLoadString::LoadKind::kDexCacheViaMethod:
7336 break;
7337 }
7338 return desired_string_load_kind;
7339}
7340
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007341void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007342 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007343 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007344 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007345 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007346 locations->SetOut(Location::RegisterLocation(R0));
7347 } else {
7348 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007349 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7350 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007351 // Rely on the pResolveString and marking to save everything we need, including temps.
7352 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7353 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007354 locations->AddTemp(Location::RequiresRegister());
7355 RegisterSet caller_saves = RegisterSet::Empty();
7356 InvokeRuntimeCallingConvention calling_convention;
7357 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7358 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7359 // that the the kPrimNot result register is the same as the first argument register.
7360 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007361 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7362 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
7363 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007364 } else {
7365 // For non-Baker read barrier we have a temp-clobbering call.
7366 }
7367 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007368 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007369}
7370
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007371// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7372// move.
7373void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007374 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007375 Location out_loc = locations->Out();
7376 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007377 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00007378
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007379 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007380 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007381 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007382 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007383 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007384 __ BindTrackedLabel(&labels->movw_label);
7385 __ movw(out, /* placeholder */ 0u);
7386 __ BindTrackedLabel(&labels->movt_label);
7387 __ movt(out, /* placeholder */ 0u);
7388 __ BindTrackedLabel(&labels->add_pc_label);
7389 __ add(out, out, ShifterOperand(PC));
7390 return; // No dex cache slow path.
7391 }
7392 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007393 uint32_t address = dchecked_integral_cast<uint32_t>(
7394 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7395 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007396 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7397 return; // No dex cache slow path.
7398 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007399 case HLoadString::LoadKind::kBssEntry: {
7400 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007401 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7402 ? locations->GetTemp(0).AsRegister<Register>()
7403 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00007404 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007405 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007406 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007407 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007408 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007409 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007410 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007411 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007412 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007413 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
7414 codegen_->AddSlowPath(slow_path);
7415 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7416 __ Bind(slow_path->GetExitLabel());
7417 return;
7418 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007419 case HLoadString::LoadKind::kJitTableAddress: {
7420 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007421 load->GetStringIndex(),
7422 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007423 // /* GcRoot<mirror::String> */ out = *out
7424 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7425 return;
7426 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007427 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007428 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007429 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007430
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007431 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
7432 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
7433 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007434 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007435 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007436 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7437 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007438}
7439
David Brazdilcb1c0552015-08-04 16:22:25 +01007440static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007441 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01007442}
7443
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007444void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
7445 LocationSummary* locations =
7446 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7447 locations->SetOut(Location::RequiresRegister());
7448}
7449
7450void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007451 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01007452 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7453}
7454
7455void LocationsBuilderARM::VisitClearException(HClearException* clear) {
7456 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7457}
7458
7459void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007460 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01007461 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007462}
7463
7464void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
7465 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007466 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007467 InvokeRuntimeCallingConvention calling_convention;
7468 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7469}
7470
7471void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007472 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007473 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007474}
7475
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007476// Temp is used for read barrier.
7477static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7478 if (kEmitCompilerReadBarrier &&
7479 (kUseBakerReadBarrier ||
7480 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7481 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7482 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7483 return 1;
7484 }
7485 return 0;
7486}
7487
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007488// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007489// interface pointer, one for loading the current interface.
7490// The other checks have one temp for loading the object's class.
7491static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7492 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7493 return 3;
7494 }
7495 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00007496}
7497
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007498void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007499 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00007500 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007501 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00007502 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007503 case TypeCheckKind::kExactCheck:
7504 case TypeCheckKind::kAbstractClassCheck:
7505 case TypeCheckKind::kClassHierarchyCheck:
7506 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007507 call_kind =
7508 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01007509 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007510 break;
7511 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007512 case TypeCheckKind::kUnresolvedCheck:
7513 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007514 call_kind = LocationSummary::kCallOnSlowPath;
7515 break;
7516 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007517
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007518 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007519 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007520 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007521 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007522 locations->SetInAt(0, Location::RequiresRegister());
7523 locations->SetInAt(1, Location::RequiresRegister());
7524 // The "out" register is used as a temporary, so it overlaps with the inputs.
7525 // Note that TypeCheckSlowPathARM uses this register too.
7526 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007527 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007528 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7529 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7530 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007531}
7532
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007533void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007534 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007535 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007536 Location obj_loc = locations->InAt(0);
7537 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007538 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007539 Location out_loc = locations->Out();
7540 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007541 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7542 DCHECK_LE(num_temps, 1u);
7543 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007544 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007545 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7546 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7547 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007548 Label done;
7549 Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01007550 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007551
7552 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007553 // avoid null check if we know obj is not null.
7554 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007555 DCHECK_NE(out, obj);
7556 __ LoadImmediate(out, 0);
7557 __ CompareAndBranchIfZero(obj, final_label);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007558 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007559
Roland Levillainc9285912015-12-18 10:38:42 +00007560 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007561 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007562 // /* HeapReference<Class> */ out = obj->klass_
7563 GenerateReferenceLoadTwoRegisters(instruction,
7564 out_loc,
7565 obj_loc,
7566 class_offset,
7567 maybe_temp_loc,
7568 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007569 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007570 __ cmp(out, ShifterOperand(cls));
7571 // We speculatively set the result to false without changing the condition
7572 // flags, which allows us to avoid some branching later.
7573 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7574
7575 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7576 // we check that the output is in a low register, so that a 16-bit MOV
7577 // encoding can be used.
7578 if (ArmAssembler::IsLowRegister(out)) {
7579 __ it(EQ);
7580 __ mov(out, ShifterOperand(1), EQ);
7581 } else {
7582 __ b(final_label, NE);
7583 __ LoadImmediate(out, 1);
7584 }
7585
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007586 break;
7587 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007588
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007589 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007590 // /* HeapReference<Class> */ out = obj->klass_
7591 GenerateReferenceLoadTwoRegisters(instruction,
7592 out_loc,
7593 obj_loc,
7594 class_offset,
7595 maybe_temp_loc,
7596 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007597 // If the class is abstract, we eagerly fetch the super class of the
7598 // object to avoid doing a comparison we know will fail.
7599 Label loop;
7600 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007601 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007602 GenerateReferenceLoadOneRegister(instruction,
7603 out_loc,
7604 super_offset,
7605 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007606 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007607 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007608 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007609 __ cmp(out, ShifterOperand(cls));
7610 __ b(&loop, NE);
7611 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007612 break;
7613 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007614
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007615 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007616 // /* HeapReference<Class> */ out = obj->klass_
7617 GenerateReferenceLoadTwoRegisters(instruction,
7618 out_loc,
7619 obj_loc,
7620 class_offset,
7621 maybe_temp_loc,
7622 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007623 // Walk over the class hierarchy to find a match.
7624 Label loop, success;
7625 __ Bind(&loop);
7626 __ cmp(out, ShifterOperand(cls));
7627 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007628 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007629 GenerateReferenceLoadOneRegister(instruction,
7630 out_loc,
7631 super_offset,
7632 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007633 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007634 // This is essentially a null check, but it sets the condition flags to the
7635 // proper value for the code that follows the loop, i.e. not `EQ`.
7636 __ cmp(out, ShifterOperand(1));
7637 __ b(&loop, HS);
7638
7639 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7640 // we check that the output is in a low register, so that a 16-bit MOV
7641 // encoding can be used.
7642 if (ArmAssembler::IsLowRegister(out)) {
7643 // If `out` is null, we use it for the result, and the condition flags
7644 // have already been set to `NE`, so the IT block that comes afterwards
7645 // (and which handles the successful case) turns into a NOP (instead of
7646 // overwriting `out`).
7647 __ Bind(&success);
7648 // There is only one branch to the `success` label (which is bound to this
7649 // IT block), and it has the same condition, `EQ`, so in that case the MOV
7650 // is executed.
7651 __ it(EQ);
7652 __ mov(out, ShifterOperand(1), EQ);
7653 } else {
7654 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007655 __ b(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007656 __ Bind(&success);
7657 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007658 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007659
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007660 break;
7661 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007662
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007663 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007664 // /* HeapReference<Class> */ out = obj->klass_
7665 GenerateReferenceLoadTwoRegisters(instruction,
7666 out_loc,
7667 obj_loc,
7668 class_offset,
7669 maybe_temp_loc,
7670 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007671 // Do an exact check.
7672 Label exact_check;
7673 __ cmp(out, ShifterOperand(cls));
7674 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007675 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007676 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007677 GenerateReferenceLoadOneRegister(instruction,
7678 out_loc,
7679 component_offset,
7680 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007681 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007682 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007683 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007684 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7685 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007686 __ cmp(out, ShifterOperand(0));
7687 // We speculatively set the result to false without changing the condition
7688 // flags, which allows us to avoid some branching later.
7689 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7690
7691 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7692 // we check that the output is in a low register, so that a 16-bit MOV
7693 // encoding can be used.
7694 if (ArmAssembler::IsLowRegister(out)) {
7695 __ Bind(&exact_check);
7696 __ it(EQ);
7697 __ mov(out, ShifterOperand(1), EQ);
7698 } else {
7699 __ b(final_label, NE);
7700 __ Bind(&exact_check);
7701 __ LoadImmediate(out, 1);
7702 }
7703
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007704 break;
7705 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007706
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007707 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007708 // No read barrier since the slow path will retry upon failure.
7709 // /* HeapReference<Class> */ out = obj->klass_
7710 GenerateReferenceLoadTwoRegisters(instruction,
7711 out_loc,
7712 obj_loc,
7713 class_offset,
7714 maybe_temp_loc,
7715 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007716 __ cmp(out, ShifterOperand(cls));
7717 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00007718 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7719 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007720 codegen_->AddSlowPath(slow_path);
7721 __ b(slow_path->GetEntryLabel(), NE);
7722 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007723 break;
7724 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007725
Calin Juravle98893e12015-10-02 21:05:03 +01007726 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007727 case TypeCheckKind::kInterfaceCheck: {
7728 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007729 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00007730 // cases.
7731 //
7732 // We cannot directly call the InstanceofNonTrivial runtime
7733 // entry point without resorting to a type checking slow path
7734 // here (i.e. by calling InvokeRuntime directly), as it would
7735 // require to assign fixed registers for the inputs of this
7736 // HInstanceOf instruction (following the runtime calling
7737 // convention), which might be cluttered by the potential first
7738 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00007739 //
7740 // TODO: Introduce a new runtime entry point taking the object
7741 // to test (instead of its class) as argument, and let it deal
7742 // with the read barrier issues. This will let us refactor this
7743 // case of the `switch` code as it was previously (with a direct
7744 // call to the runtime not using a type checking slow path).
7745 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00007746 DCHECK(locations->OnlyCallsOnSlowPath());
7747 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7748 /* is_fatal */ false);
7749 codegen_->AddSlowPath(slow_path);
7750 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007751 break;
7752 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007753 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007754
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007755 if (done.IsLinked()) {
7756 __ Bind(&done);
7757 }
7758
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007759 if (slow_path != nullptr) {
7760 __ Bind(slow_path->GetExitLabel());
7761 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007762}
7763
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007764void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007765 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7766 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7767
Roland Levillain3b359c72015-11-17 19:35:12 +00007768 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7769 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007770 case TypeCheckKind::kExactCheck:
7771 case TypeCheckKind::kAbstractClassCheck:
7772 case TypeCheckKind::kClassHierarchyCheck:
7773 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007774 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7775 LocationSummary::kCallOnSlowPath :
7776 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007777 break;
7778 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007779 case TypeCheckKind::kUnresolvedCheck:
7780 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007781 call_kind = LocationSummary::kCallOnSlowPath;
7782 break;
7783 }
7784
Roland Levillain3b359c72015-11-17 19:35:12 +00007785 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7786 locations->SetInAt(0, Location::RequiresRegister());
7787 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007788 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007789}
7790
7791void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007792 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007793 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007794 Location obj_loc = locations->InAt(0);
7795 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007796 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007797 Location temp_loc = locations->GetTemp(0);
7798 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007799 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7800 DCHECK_LE(num_temps, 3u);
7801 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7802 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7803 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7804 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7805 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7806 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7807 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7808 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7809 const uint32_t object_array_data_offset =
7810 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007811
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007812 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7813 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7814 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007815 bool is_type_check_slow_path_fatal = false;
7816 if (!kEmitCompilerReadBarrier) {
7817 is_type_check_slow_path_fatal =
7818 (type_check_kind == TypeCheckKind::kExactCheck ||
7819 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7820 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7821 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7822 !instruction->CanThrowIntoCatchBlock();
7823 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01007824 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007825 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7826 is_type_check_slow_path_fatal);
7827 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007828
7829 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007830 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007831 // Avoid null check if we know obj is not null.
7832 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007833 __ CompareAndBranchIfZero(obj, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007834 }
7835
Roland Levillain3b359c72015-11-17 19:35:12 +00007836 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007837 case TypeCheckKind::kExactCheck:
7838 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007839 // /* HeapReference<Class> */ temp = obj->klass_
7840 GenerateReferenceLoadTwoRegisters(instruction,
7841 temp_loc,
7842 obj_loc,
7843 class_offset,
7844 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007845 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007846
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007847 __ cmp(temp, ShifterOperand(cls));
7848 // Jump to slow path for throwing the exception or doing a
7849 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00007850 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007851 break;
7852 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007853
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007854 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007855 // /* HeapReference<Class> */ temp = obj->klass_
7856 GenerateReferenceLoadTwoRegisters(instruction,
7857 temp_loc,
7858 obj_loc,
7859 class_offset,
7860 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007861 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007862
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007863 // If the class is abstract, we eagerly fetch the super class of the
7864 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007865 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007866 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007867 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007868 GenerateReferenceLoadOneRegister(instruction,
7869 temp_loc,
7870 super_offset,
7871 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007872 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007873
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007874 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7875 // exception.
7876 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00007877
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007878 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007879 __ cmp(temp, ShifterOperand(cls));
7880 __ b(&loop, NE);
7881 break;
7882 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007883
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007884 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007885 // /* HeapReference<Class> */ temp = obj->klass_
7886 GenerateReferenceLoadTwoRegisters(instruction,
7887 temp_loc,
7888 obj_loc,
7889 class_offset,
7890 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007891 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007892
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007893 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007894 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007895 __ Bind(&loop);
7896 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007897 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007898
Roland Levillain3b359c72015-11-17 19:35:12 +00007899 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007900 GenerateReferenceLoadOneRegister(instruction,
7901 temp_loc,
7902 super_offset,
7903 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007904 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007905
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007906 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7907 // exception.
7908 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7909 // Otherwise, jump to the beginning of the loop.
7910 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007911 break;
7912 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007913
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007914 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007915 // /* HeapReference<Class> */ temp = obj->klass_
7916 GenerateReferenceLoadTwoRegisters(instruction,
7917 temp_loc,
7918 obj_loc,
7919 class_offset,
7920 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007921 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007922
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007923 // Do an exact check.
7924 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007925 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007926
7927 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007928 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007929 GenerateReferenceLoadOneRegister(instruction,
7930 temp_loc,
7931 component_offset,
7932 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007933 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007934 // If the component type is null, jump to the slow path to throw the exception.
7935 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7936 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7937 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007938 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00007939 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007940 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007941 break;
7942 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007943
Calin Juravle98893e12015-10-02 21:05:03 +01007944 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007945 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00007946 // We cannot directly call the CheckCast runtime entry point
7947 // without resorting to a type checking slow path here (i.e. by
7948 // calling InvokeRuntime directly), as it would require to
7949 // assign fixed registers for the inputs of this HInstanceOf
7950 // instruction (following the runtime calling convention), which
7951 // might be cluttered by the potential first read barrier
7952 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007953
Roland Levillain3b359c72015-11-17 19:35:12 +00007954 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007955 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007956
7957 case TypeCheckKind::kInterfaceCheck: {
7958 // Avoid read barriers to improve performance of the fast path. We can not get false
7959 // positives by doing this.
7960 // /* HeapReference<Class> */ temp = obj->klass_
7961 GenerateReferenceLoadTwoRegisters(instruction,
7962 temp_loc,
7963 obj_loc,
7964 class_offset,
7965 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007966 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007967
7968 // /* HeapReference<Class> */ temp = temp->iftable_
7969 GenerateReferenceLoadTwoRegisters(instruction,
7970 temp_loc,
7971 temp_loc,
7972 iftable_offset,
7973 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007974 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007975 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007976 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08007977 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007978 Label start_loop;
7979 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007980 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
7981 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007982 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
7983 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007984 // Go to next interface.
7985 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
7986 __ sub(maybe_temp2_loc.AsRegister<Register>(),
7987 maybe_temp2_loc.AsRegister<Register>(),
7988 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007989 // Compare the classes and continue the loop if they do not match.
7990 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
7991 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007992 break;
7993 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007994 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007995
7996 if (done.IsLinked()) {
7997 __ Bind(&done);
7998 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007999
Roland Levillain3b359c72015-11-17 19:35:12 +00008000 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00008001}
8002
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008003void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
8004 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008005 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008006 InvokeRuntimeCallingConvention calling_convention;
8007 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8008}
8009
8010void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01008011 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8012 instruction,
8013 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008014 if (instruction->IsEnter()) {
8015 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8016 } else {
8017 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8018 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008019}
8020
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008021void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
8022void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
8023void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008024
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008025void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008026 LocationSummary* locations =
8027 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8028 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
8029 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008030 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008031 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008032 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00008033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008034}
8035
8036void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
8037 HandleBitwiseOperation(instruction);
8038}
8039
8040void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
8041 HandleBitwiseOperation(instruction);
8042}
8043
8044void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
8045 HandleBitwiseOperation(instruction);
8046}
8047
Artem Serov7fc63502016-02-09 17:15:29 +00008048
8049void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8050 LocationSummary* locations =
8051 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8052 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
8053 || instruction->GetResultType() == Primitive::kPrimLong);
8054
8055 locations->SetInAt(0, Location::RequiresRegister());
8056 locations->SetInAt(1, Location::RequiresRegister());
8057 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8058}
8059
8060void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8061 LocationSummary* locations = instruction->GetLocations();
8062 Location first = locations->InAt(0);
8063 Location second = locations->InAt(1);
8064 Location out = locations->Out();
8065
8066 if (instruction->GetResultType() == Primitive::kPrimInt) {
8067 Register first_reg = first.AsRegister<Register>();
8068 ShifterOperand second_reg(second.AsRegister<Register>());
8069 Register out_reg = out.AsRegister<Register>();
8070
8071 switch (instruction->GetOpKind()) {
8072 case HInstruction::kAnd:
8073 __ bic(out_reg, first_reg, second_reg);
8074 break;
8075 case HInstruction::kOr:
8076 __ orn(out_reg, first_reg, second_reg);
8077 break;
8078 // There is no EON on arm.
8079 case HInstruction::kXor:
8080 default:
8081 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8082 UNREACHABLE();
8083 }
8084 return;
8085
8086 } else {
8087 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8088 Register first_low = first.AsRegisterPairLow<Register>();
8089 Register first_high = first.AsRegisterPairHigh<Register>();
8090 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
8091 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
8092 Register out_low = out.AsRegisterPairLow<Register>();
8093 Register out_high = out.AsRegisterPairHigh<Register>();
8094
8095 switch (instruction->GetOpKind()) {
8096 case HInstruction::kAnd:
8097 __ bic(out_low, first_low, second_low);
8098 __ bic(out_high, first_high, second_high);
8099 break;
8100 case HInstruction::kOr:
8101 __ orn(out_low, first_low, second_low);
8102 __ orn(out_high, first_high, second_high);
8103 break;
8104 // There is no EON on arm.
8105 case HInstruction::kXor:
8106 default:
8107 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8108 UNREACHABLE();
8109 }
8110 }
8111}
8112
Anton Kirilov74234da2017-01-13 14:42:47 +00008113void LocationsBuilderARM::VisitDataProcWithShifterOp(
8114 HDataProcWithShifterOp* instruction) {
8115 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
8116 instruction->GetType() == Primitive::kPrimLong);
8117 LocationSummary* locations =
8118 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8119 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
8120 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8121
8122 locations->SetInAt(0, Location::RequiresRegister());
8123 locations->SetInAt(1, Location::RequiresRegister());
8124 locations->SetOut(Location::RequiresRegister(),
8125 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8126}
8127
8128void InstructionCodeGeneratorARM::VisitDataProcWithShifterOp(
8129 HDataProcWithShifterOp* instruction) {
8130 const LocationSummary* const locations = instruction->GetLocations();
8131 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8132 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8133 const Location left = locations->InAt(0);
8134 const Location right = locations->InAt(1);
8135 const Location out = locations->Out();
8136
8137 if (instruction->GetType() == Primitive::kPrimInt) {
8138 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
8139
8140 const Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
8141 ? right.AsRegisterPairLow<Register>()
8142 : right.AsRegister<Register>();
8143
8144 GenerateDataProcInstruction(kind,
8145 out.AsRegister<Register>(),
8146 left.AsRegister<Register>(),
8147 ShifterOperand(second,
8148 ShiftFromOpKind(op_kind),
8149 instruction->GetShiftAmount()),
8150 codegen_);
8151 } else {
8152 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
8153
8154 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8155 const Register second = right.AsRegister<Register>();
8156
8157 DCHECK_NE(out.AsRegisterPairLow<Register>(), second);
8158 GenerateDataProc(kind,
8159 out,
8160 left,
8161 ShifterOperand(second),
8162 ShifterOperand(second, ASR, 31),
8163 codegen_);
8164 } else {
8165 GenerateLongDataProc(instruction, codegen_);
8166 }
8167 }
8168}
8169
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008170void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
8171 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8172 if (value == 0xffffffffu) {
8173 if (out != first) {
8174 __ mov(out, ShifterOperand(first));
8175 }
8176 return;
8177 }
8178 if (value == 0u) {
8179 __ mov(out, ShifterOperand(0));
8180 return;
8181 }
8182 ShifterOperand so;
8183 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
8184 __ and_(out, first, so);
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008185 } else if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008186 __ bic(out, first, ShifterOperand(~value));
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008187 } else {
8188 DCHECK(IsPowerOfTwo(value + 1));
8189 __ ubfx(out, first, 0, WhichPowerOf2(value + 1));
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008190 }
8191}
8192
8193void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
8194 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8195 if (value == 0u) {
8196 if (out != first) {
8197 __ mov(out, ShifterOperand(first));
8198 }
8199 return;
8200 }
8201 if (value == 0xffffffffu) {
8202 __ mvn(out, ShifterOperand(0));
8203 return;
8204 }
8205 ShifterOperand so;
8206 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
8207 __ orr(out, first, so);
8208 } else {
8209 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
8210 __ orn(out, first, ShifterOperand(~value));
8211 }
8212}
8213
8214void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
8215 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8216 if (value == 0u) {
8217 if (out != first) {
8218 __ mov(out, ShifterOperand(first));
8219 }
8220 return;
8221 }
8222 __ eor(out, first, ShifterOperand(value));
8223}
8224
Vladimir Marko59751a72016-08-05 14:37:27 +01008225void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
8226 Location first,
8227 uint64_t value) {
8228 Register out_low = out.AsRegisterPairLow<Register>();
8229 Register out_high = out.AsRegisterPairHigh<Register>();
8230 Register first_low = first.AsRegisterPairLow<Register>();
8231 Register first_high = first.AsRegisterPairHigh<Register>();
8232 uint32_t value_low = Low32Bits(value);
8233 uint32_t value_high = High32Bits(value);
8234 if (value_low == 0u) {
8235 if (out_low != first_low) {
8236 __ mov(out_low, ShifterOperand(first_low));
8237 }
8238 __ AddConstant(out_high, first_high, value_high);
8239 return;
8240 }
8241 __ AddConstantSetFlags(out_low, first_low, value_low);
8242 ShifterOperand so;
8243 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
8244 __ adc(out_high, first_high, so);
8245 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
8246 __ sbc(out_high, first_high, so);
8247 } else {
8248 LOG(FATAL) << "Unexpected constant " << value_high;
8249 UNREACHABLE();
8250 }
8251}
8252
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008253void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
8254 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008255 Location first = locations->InAt(0);
8256 Location second = locations->InAt(1);
8257 Location out = locations->Out();
8258
8259 if (second.IsConstant()) {
8260 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8261 uint32_t value_low = Low32Bits(value);
8262 if (instruction->GetResultType() == Primitive::kPrimInt) {
8263 Register first_reg = first.AsRegister<Register>();
8264 Register out_reg = out.AsRegister<Register>();
8265 if (instruction->IsAnd()) {
8266 GenerateAndConst(out_reg, first_reg, value_low);
8267 } else if (instruction->IsOr()) {
8268 GenerateOrrConst(out_reg, first_reg, value_low);
8269 } else {
8270 DCHECK(instruction->IsXor());
8271 GenerateEorConst(out_reg, first_reg, value_low);
8272 }
8273 } else {
8274 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8275 uint32_t value_high = High32Bits(value);
8276 Register first_low = first.AsRegisterPairLow<Register>();
8277 Register first_high = first.AsRegisterPairHigh<Register>();
8278 Register out_low = out.AsRegisterPairLow<Register>();
8279 Register out_high = out.AsRegisterPairHigh<Register>();
8280 if (instruction->IsAnd()) {
8281 GenerateAndConst(out_low, first_low, value_low);
8282 GenerateAndConst(out_high, first_high, value_high);
8283 } else if (instruction->IsOr()) {
8284 GenerateOrrConst(out_low, first_low, value_low);
8285 GenerateOrrConst(out_high, first_high, value_high);
8286 } else {
8287 DCHECK(instruction->IsXor());
8288 GenerateEorConst(out_low, first_low, value_low);
8289 GenerateEorConst(out_high, first_high, value_high);
8290 }
8291 }
8292 return;
8293 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008294
8295 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008296 Register first_reg = first.AsRegister<Register>();
8297 ShifterOperand second_reg(second.AsRegister<Register>());
8298 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008299 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008300 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008301 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008302 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008303 } else {
8304 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008305 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008306 }
8307 } else {
8308 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008309 Register first_low = first.AsRegisterPairLow<Register>();
8310 Register first_high = first.AsRegisterPairHigh<Register>();
8311 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
8312 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
8313 Register out_low = out.AsRegisterPairLow<Register>();
8314 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008315 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008316 __ and_(out_low, first_low, second_low);
8317 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008318 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008319 __ orr(out_low, first_low, second_low);
8320 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008321 } else {
8322 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008323 __ eor(out_low, first_low, second_low);
8324 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008325 }
8326 }
8327}
8328
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008329void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
8330 HInstruction* instruction,
8331 Location out,
8332 uint32_t offset,
8333 Location maybe_temp,
8334 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008335 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008336 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08008337 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008338 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00008339 if (kUseBakerReadBarrier) {
8340 // Load with fast path based Baker's read barrier.
8341 // /* HeapReference<Object> */ out = *(out + offset)
8342 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008343 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00008344 } else {
8345 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008346 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00008347 // in the following move operation, as we will need it for the
8348 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008349 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00008350 // /* HeapReference<Object> */ out = *(out + offset)
8351 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008352 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00008353 }
8354 } else {
8355 // Plain load with no read barrier.
8356 // /* HeapReference<Object> */ out = *(out + offset)
8357 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8358 __ MaybeUnpoisonHeapReference(out_reg);
8359 }
8360}
8361
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008362void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
8363 HInstruction* instruction,
8364 Location out,
8365 Location obj,
8366 uint32_t offset,
8367 Location maybe_temp,
8368 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008369 Register out_reg = out.AsRegister<Register>();
8370 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008371 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08008372 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00008373 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008374 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00008375 // Load with fast path based Baker's read barrier.
8376 // /* HeapReference<Object> */ out = *(obj + offset)
8377 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008378 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00008379 } else {
8380 // Load with slow path based read barrier.
8381 // /* HeapReference<Object> */ out = *(obj + offset)
8382 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8383 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8384 }
8385 } else {
8386 // Plain load with no read barrier.
8387 // /* HeapReference<Object> */ out = *(obj + offset)
8388 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8389 __ MaybeUnpoisonHeapReference(out_reg);
8390 }
8391}
8392
8393void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
8394 Location root,
8395 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008396 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008397 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008398 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008399 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008400 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00008401 if (kUseBakerReadBarrier) {
8402 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008403 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008404 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8405 !Runtime::Current()->UseJitCompilation()) {
8406 // Note that we do not actually check the value of `GetIsGcMarking()`
8407 // to decide whether to mark the loaded GC root or not. Instead, we
8408 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8409 // barrier mark introspection entrypoint. If `temp` is null, it means
8410 // that `GetIsGcMarking()` is false, and vice versa.
8411 //
8412 // We use link-time generated thunks for the slow path. That thunk
8413 // checks the reference and jumps to the entrypoint if needed.
8414 //
8415 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8416 // lr = &return_address;
8417 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8418 // if (temp != nullptr) {
8419 // goto gc_root_thunk<root_reg>(lr)
8420 // }
8421 // return_address:
Roland Levillainc9285912015-12-18 10:38:42 +00008422
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008423 CheckLastTempIsBakerCcEntrypointRegister(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008424 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008425 uint32_t custom_data =
Vladimir Marko88abba22017-05-03 17:09:25 +01008426 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg, narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008427 Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008428
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008429 // entrypoint_reg =
8430 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8431 DCHECK_EQ(IP, 12);
8432 const int32_t entry_point_offset =
8433 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8434 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008435
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008436 Label return_address;
8437 __ AdrCode(LR, &return_address);
8438 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008439 // Currently the offset is always within range. If that changes,
8440 // we shall have to split the load the same way as for fields.
8441 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008442 DCHECK(!down_cast<Thumb2Assembler*>(GetAssembler())->IsForced32Bit());
8443 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()), !narrow);
8444 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008445 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8446 EmitPlaceholderBne(codegen_, bne_label);
8447 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008448 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8449 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8450 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008451 } else {
8452 // Note that we do not actually check the value of
8453 // `GetIsGcMarking()` to decide whether to mark the loaded GC
8454 // root or not. Instead, we load into `temp` the read barrier
8455 // mark entry point corresponding to register `root`. If `temp`
8456 // is null, it means that `GetIsGcMarking()` is false, and vice
8457 // versa.
8458 //
8459 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8460 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8461 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8462 // // Slow path.
8463 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
8464 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008465
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008466 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
8467 Location temp = Location::RegisterLocation(LR);
8468 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
8469 instruction, root, /* entrypoint */ temp);
8470 codegen_->AddSlowPath(slow_path);
8471
8472 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8473 const int32_t entry_point_offset =
8474 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
8475 // Loading the entrypoint does not require a load acquire since it is only changed when
8476 // threads are suspended or running a checkpoint.
8477 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
8478
8479 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8480 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8481 static_assert(
8482 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8483 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8484 "have different sizes.");
8485 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8486 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8487 "have different sizes.");
8488
8489 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8490 // checking GetIsGcMarking.
8491 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
8492 __ Bind(slow_path->GetExitLabel());
8493 }
Roland Levillainc9285912015-12-18 10:38:42 +00008494 } else {
8495 // GC root loaded through a slow path for read barriers other
8496 // than Baker's.
8497 // /* GcRoot<mirror::Object>* */ root = obj + offset
8498 __ AddConstant(root_reg, obj, offset);
8499 // /* mirror::Object* */ root = root->Read()
8500 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8501 }
8502 } else {
8503 // Plain GC root load with no read barrier.
8504 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8505 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8506 // Note that GC roots are not affected by heap poisoning, thus we
8507 // do not have to unpoison `root_reg` here.
8508 }
8509}
8510
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008511void CodeGeneratorARM::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8512 DCHECK(kEmitCompilerReadBarrier);
8513 DCHECK(kUseBakerReadBarrier);
8514 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8515 if (!Runtime::Current()->UseJitCompilation()) {
8516 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
8517 }
8518 }
8519}
8520
Roland Levillainc9285912015-12-18 10:38:42 +00008521void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8522 Location ref,
8523 Register obj,
8524 uint32_t offset,
8525 Location temp,
8526 bool needs_null_check) {
8527 DCHECK(kEmitCompilerReadBarrier);
8528 DCHECK(kUseBakerReadBarrier);
8529
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008530 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8531 !Runtime::Current()->UseJitCompilation()) {
8532 // Note that we do not actually check the value of `GetIsGcMarking()`
8533 // to decide whether to mark the loaded reference or not. Instead, we
8534 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8535 // barrier mark introspection entrypoint. If `temp` is null, it means
8536 // that `GetIsGcMarking()` is false, and vice versa.
8537 //
8538 // We use link-time generated thunks for the slow path. That thunk checks
8539 // the holder and jumps to the entrypoint if needed. If the holder is not
8540 // gray, it creates a fake dependency and returns to the LDR instruction.
8541 //
8542 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8543 // lr = &gray_return_address;
8544 // if (temp != nullptr) {
8545 // goto field_thunk<holder_reg, base_reg>(lr)
8546 // }
8547 // not_gray_return_address:
8548 // // Original reference load. If the offset is too large to fit
8549 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008550 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008551 // gray_return_address:
8552
8553 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko88abba22017-05-03 17:09:25 +01008554 Register ref_reg = ref.AsRegister<Register>();
8555 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008556 Register base = obj;
8557 if (offset >= kReferenceLoadMinFarOffset) {
8558 base = temp.AsRegister<Register>();
8559 DCHECK_NE(base, kBakerCcEntrypointRegister);
8560 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8561 __ AddConstant(base, obj, offset & ~(kReferenceLoadMinFarOffset - 1u));
8562 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008563 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8564 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8565 // increase the overall code size when taking the generated thunks into account.
8566 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008567 }
8568 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8569 uint32_t custom_data =
Vladimir Marko88abba22017-05-03 17:09:25 +01008570 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(base, obj, narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008571 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8572
8573 // entrypoint_reg =
8574 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8575 DCHECK_EQ(IP, 12);
8576 const int32_t entry_point_offset =
8577 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8578 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8579
8580 Label return_address;
8581 __ AdrCode(LR, &return_address);
8582 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008583 EmitPlaceholderBne(this, bne_label);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008584 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008585 DCHECK(!down_cast<Thumb2Assembler*>(GetAssembler())->IsForced32Bit());
8586 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()), !narrow);
8587 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008588 __ LoadFromOffset(kLoadWord, ref_reg, base, offset);
8589 if (needs_null_check) {
8590 MaybeRecordImplicitNullCheck(instruction);
8591 }
8592 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8593 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008594 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8595 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8596 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008597 return;
8598 }
8599
Roland Levillainc9285912015-12-18 10:38:42 +00008600 // /* HeapReference<Object> */ ref = *(obj + offset)
8601 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01008602 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00008603 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008604 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008605}
8606
8607void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8608 Location ref,
8609 Register obj,
8610 uint32_t data_offset,
8611 Location index,
8612 Location temp,
8613 bool needs_null_check) {
8614 DCHECK(kEmitCompilerReadBarrier);
8615 DCHECK(kUseBakerReadBarrier);
8616
Roland Levillainbfea3352016-06-23 13:48:47 +01008617 static_assert(
8618 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8619 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008620 ScaleFactor scale_factor = TIMES_4;
8621
8622 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8623 !Runtime::Current()->UseJitCompilation()) {
8624 // Note that we do not actually check the value of `GetIsGcMarking()`
8625 // to decide whether to mark the loaded reference or not. Instead, we
8626 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8627 // barrier mark introspection entrypoint. If `temp` is null, it means
8628 // that `GetIsGcMarking()` is false, and vice versa.
8629 //
8630 // We use link-time generated thunks for the slow path. That thunk checks
8631 // the holder and jumps to the entrypoint if needed. If the holder is not
8632 // gray, it creates a fake dependency and returns to the LDR instruction.
8633 //
8634 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8635 // lr = &gray_return_address;
8636 // if (temp != nullptr) {
8637 // goto field_thunk<holder_reg, base_reg>(lr)
8638 // }
8639 // not_gray_return_address:
8640 // // Original reference load. If the offset is too large to fit
8641 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008642 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008643 // gray_return_address:
8644
8645 DCHECK(index.IsValid());
8646 Register index_reg = index.AsRegister<Register>();
8647 Register ref_reg = ref.AsRegister<Register>();
8648 Register data_reg = temp.AsRegister<Register>();
8649 DCHECK_NE(data_reg, kBakerCcEntrypointRegister);
8650
8651 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8652 uint32_t custom_data =
8653 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg);
8654 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8655
8656 // entrypoint_reg =
8657 // Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
8658 DCHECK_EQ(IP, 12);
8659 const int32_t entry_point_offset =
8660 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8661 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8662 __ AddConstant(data_reg, obj, data_offset);
8663
8664 Label return_address;
8665 __ AdrCode(LR, &return_address);
8666 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008667 EmitPlaceholderBne(this, bne_label);
Vladimir Marko88abba22017-05-03 17:09:25 +01008668 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()));
8669 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008670 __ ldr(ref_reg, Address(data_reg, index_reg, LSL, scale_factor));
8671 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8672 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8673 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008674 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8675 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008676 return;
8677 }
8678
Roland Levillainc9285912015-12-18 10:38:42 +00008679 // /* HeapReference<Object> */ ref =
8680 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
8681 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008682 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008683}
8684
8685void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8686 Location ref,
8687 Register obj,
8688 uint32_t offset,
8689 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01008690 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00008691 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008692 bool needs_null_check) {
Roland Levillainc9285912015-12-18 10:38:42 +00008693 DCHECK(kEmitCompilerReadBarrier);
8694 DCHECK(kUseBakerReadBarrier);
8695
Roland Levillain54f869e2017-03-06 13:54:11 +00008696 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8697 // whether we need to enter the slow path to mark the reference.
8698 // Then, in the slow path, check the gray bit in the lock word of
8699 // the reference's holder (`obj`) to decide whether to mark `ref` or
8700 // not.
Roland Levillainc9285912015-12-18 10:38:42 +00008701 //
Roland Levillainba650a42017-03-06 13:52:32 +00008702 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainff487002017-03-07 16:50:01 +00008703 // instead, we load into `temp2` the read barrier mark entry point
8704 // corresponding to register `ref`. If `temp2` is null, it means
8705 // that `GetIsGcMarking()` is false, and vice versa.
8706 //
8707 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8708 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8709 // // Slow path.
8710 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8711 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8712 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8713 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8714 // if (is_gray) {
8715 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8716 // }
8717 // } else {
8718 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8719 // }
8720
8721 Register temp_reg = temp.AsRegister<Register>();
8722
8723 // Slow path marking the object `ref` when the GC is marking. The
8724 // entrypoint will already be loaded in `temp2`.
8725 Location temp2 = Location::RegisterLocation(LR);
8726 SlowPathCodeARM* slow_path =
8727 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM(
8728 instruction,
8729 ref,
8730 obj,
8731 offset,
8732 index,
8733 scale_factor,
8734 needs_null_check,
8735 temp_reg,
8736 /* entrypoint */ temp2);
8737 AddSlowPath(slow_path);
8738
8739 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8740 const int32_t entry_point_offset =
8741 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8742 // Loading the entrypoint does not require a load acquire since it is only changed when
8743 // threads are suspended or running a checkpoint.
8744 __ LoadFromOffset(kLoadWord, temp2.AsRegister<Register>(), TR, entry_point_offset);
8745 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8746 // checking GetIsGcMarking.
8747 __ CompareAndBranchIfNonZero(temp2.AsRegister<Register>(), slow_path->GetEntryLabel());
8748 // Fast path: the GC is not marking: just load the reference.
8749 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8750 __ Bind(slow_path->GetExitLabel());
8751}
8752
8753void CodeGeneratorARM::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8754 Location ref,
8755 Register obj,
8756 Location field_offset,
8757 Location temp,
8758 bool needs_null_check,
8759 Register temp2) {
8760 DCHECK(kEmitCompilerReadBarrier);
8761 DCHECK(kUseBakerReadBarrier);
8762
8763 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8764 // whether we need to enter the slow path to update the reference
8765 // field within `obj`. Then, in the slow path, check the gray bit
8766 // in the lock word of the reference's holder (`obj`) to decide
8767 // whether to mark `ref` and update the field or not.
8768 //
8769 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainba650a42017-03-06 13:52:32 +00008770 // instead, we load into `temp3` the read barrier mark entry point
8771 // corresponding to register `ref`. If `temp3` is null, it means
8772 // that `GetIsGcMarking()` is false, and vice versa.
8773 //
8774 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008775 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8776 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008777 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8778 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8779 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8780 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8781 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008782 // old_ref = ref;
Roland Levillain54f869e2017-03-06 13:54:11 +00008783 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008784 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008785 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008786 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008787
Roland Levillain35345a52017-02-27 14:32:08 +00008788 Register temp_reg = temp.AsRegister<Register>();
Roland Levillain1372c9f2017-01-13 11:47:39 +00008789
Roland Levillainff487002017-03-07 16:50:01 +00008790 // Slow path updating the object reference at address `obj +
8791 // field_offset` when the GC is marking. The entrypoint will already
8792 // be loaded in `temp3`.
Roland Levillainba650a42017-03-06 13:52:32 +00008793 Location temp3 = Location::RegisterLocation(LR);
Roland Levillainff487002017-03-07 16:50:01 +00008794 SlowPathCodeARM* slow_path =
8795 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(
8796 instruction,
8797 ref,
8798 obj,
8799 /* offset */ 0u,
8800 /* index */ field_offset,
8801 /* scale_factor */ ScaleFactor::TIMES_1,
8802 needs_null_check,
8803 temp_reg,
8804 temp2,
8805 /* entrypoint */ temp3);
Roland Levillainba650a42017-03-06 13:52:32 +00008806 AddSlowPath(slow_path);
Roland Levillain35345a52017-02-27 14:32:08 +00008807
Roland Levillainba650a42017-03-06 13:52:32 +00008808 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8809 const int32_t entry_point_offset =
8810 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8811 // Loading the entrypoint does not require a load acquire since it is only changed when
8812 // threads are suspended or running a checkpoint.
8813 __ LoadFromOffset(kLoadWord, temp3.AsRegister<Register>(), TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008814 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8815 // checking GetIsGcMarking.
8816 __ CompareAndBranchIfNonZero(temp3.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008817 // Fast path: the GC is not marking: nothing to do (the field is
8818 // up-to-date, and we don't need to load the reference).
Roland Levillainba650a42017-03-06 13:52:32 +00008819 __ Bind(slow_path->GetExitLabel());
8820}
Roland Levillain35345a52017-02-27 14:32:08 +00008821
Roland Levillainba650a42017-03-06 13:52:32 +00008822void CodeGeneratorARM::GenerateRawReferenceLoad(HInstruction* instruction,
8823 Location ref,
8824 Register obj,
8825 uint32_t offset,
8826 Location index,
8827 ScaleFactor scale_factor,
8828 bool needs_null_check) {
8829 Register ref_reg = ref.AsRegister<Register>();
8830
Roland Levillainc9285912015-12-18 10:38:42 +00008831 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008832 // Load types involving an "index": ArrayGet,
8833 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8834 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008835 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00008836 if (index.IsConstant()) {
8837 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01008838 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00008839 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
8840 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01008841 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008842 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8843 // intrinsics, which use a register pair as index ("long
8844 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01008845 Register index_reg = index.IsRegisterPair()
8846 ? index.AsRegisterPairLow<Register>()
8847 : index.AsRegister<Register>();
8848 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00008849 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
8850 }
8851 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008852 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillainc9285912015-12-18 10:38:42 +00008853 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
8854 }
8855
Roland Levillainba650a42017-03-06 13:52:32 +00008856 if (needs_null_check) {
8857 MaybeRecordImplicitNullCheck(instruction);
8858 }
8859
Roland Levillainc9285912015-12-18 10:38:42 +00008860 // Object* ref = ref_addr->AsMirrorPtr()
8861 __ MaybeUnpoisonHeapReference(ref_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00008862}
8863
8864void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
8865 Location out,
8866 Location ref,
8867 Location obj,
8868 uint32_t offset,
8869 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008870 DCHECK(kEmitCompilerReadBarrier);
8871
Roland Levillainc9285912015-12-18 10:38:42 +00008872 // Insert a slow path based read barrier *after* the reference load.
8873 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008874 // If heap poisoning is enabled, the unpoisoning of the loaded
8875 // reference will be carried out by the runtime within the slow
8876 // path.
8877 //
8878 // Note that `ref` currently does not get unpoisoned (when heap
8879 // poisoning is enabled), which is alright as the `ref` argument is
8880 // not used by the artReadBarrierSlow entry point.
8881 //
8882 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008883 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00008884 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
8885 AddSlowPath(slow_path);
8886
Roland Levillain3b359c72015-11-17 19:35:12 +00008887 __ b(slow_path->GetEntryLabel());
8888 __ Bind(slow_path->GetExitLabel());
8889}
8890
Roland Levillainc9285912015-12-18 10:38:42 +00008891void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8892 Location out,
8893 Location ref,
8894 Location obj,
8895 uint32_t offset,
8896 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008897 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00008898 // Baker's read barriers shall be handled by the fast path
8899 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
8900 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00008901 // If heap poisoning is enabled, unpoisoning will be taken care of
8902 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00008903 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00008904 } else if (kPoisonHeapReferences) {
8905 __ UnpoisonHeapReference(out.AsRegister<Register>());
8906 }
8907}
8908
Roland Levillainc9285912015-12-18 10:38:42 +00008909void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8910 Location out,
8911 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008912 DCHECK(kEmitCompilerReadBarrier);
8913
Roland Levillainc9285912015-12-18 10:38:42 +00008914 // Insert a slow path based read barrier *after* the GC root load.
8915 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008916 // Note that GC roots are not affected by heap poisoning, so we do
8917 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008918 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00008919 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
8920 AddSlowPath(slow_path);
8921
Roland Levillain3b359c72015-11-17 19:35:12 +00008922 __ b(slow_path->GetEntryLabel());
8923 __ Bind(slow_path->GetExitLabel());
8924}
8925
Vladimir Markodc151b22015-10-15 18:02:30 +01008926HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
8927 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008928 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008929 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01008930}
8931
Vladimir Markob4536b72015-11-24 13:45:23 +00008932Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
8933 Register temp) {
8934 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8935 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8936 if (!invoke->GetLocations()->Intrinsified()) {
8937 return location.AsRegister<Register>();
8938 }
8939 // For intrinsics we allow any location, so it may be on the stack.
8940 if (!location.IsRegister()) {
8941 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
8942 return temp;
8943 }
8944 // For register locations, check if the register was saved. If so, get it from the stack.
8945 // Note: There is a chance that the register was saved but not overwritten, so we could
8946 // save one load. However, since this is just an intrinsic slow path we prefer this
8947 // simple and more robust approach rather that trying to determine if that's the case.
8948 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08008949 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00008950 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
8951 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
8952 return temp;
8953 }
8954 return location.AsRegister<Register>();
8955}
8956
TatWai Chongd8c052a2016-11-02 16:12:48 +08008957Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
8958 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00008959 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
8960 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008961 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8962 uint32_t offset =
8963 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00008964 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008965 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00008966 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008967 }
Vladimir Marko58155012015-08-19 12:49:41 +00008968 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00008969 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008970 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008971 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
8972 DCHECK(GetCompilerOptions().IsBootImage());
8973 Register temp_reg = temp.AsRegister<Register>();
8974 PcRelativePatchInfo* labels = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
8975 __ BindTrackedLabel(&labels->movw_label);
8976 __ movw(temp_reg, /* placeholder */ 0u);
8977 __ BindTrackedLabel(&labels->movt_label);
8978 __ movt(temp_reg, /* placeholder */ 0u);
8979 __ BindTrackedLabel(&labels->add_pc_label);
8980 __ add(temp_reg, temp_reg, ShifterOperand(PC));
8981 break;
8982 }
Vladimir Marko58155012015-08-19 12:49:41 +00008983 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8984 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
8985 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00008986 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
8987 HArmDexCacheArraysBase* base =
8988 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
8989 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
8990 temp.AsRegister<Register>());
8991 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
8992 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
8993 break;
8994 }
Vladimir Marko58155012015-08-19 12:49:41 +00008995 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00008996 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008997 Register method_reg;
8998 Register reg = temp.AsRegister<Register>();
8999 if (current_method.IsRegister()) {
9000 method_reg = current_method.AsRegister<Register>();
9001 } else {
9002 DCHECK(invoke->GetLocations()->Intrinsified());
9003 DCHECK(!current_method.IsValid());
9004 method_reg = reg;
9005 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
9006 }
Roland Levillain3b359c72015-11-17 19:35:12 +00009007 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
9008 __ LoadFromOffset(kLoadWord,
9009 reg,
9010 method_reg,
9011 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01009012 // temp = temp[index_in_cache];
9013 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
9014 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00009015 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
9016 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01009017 }
Vladimir Marko58155012015-08-19 12:49:41 +00009018 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08009019 return callee_method;
9020}
9021
9022void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
9023 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00009024
9025 switch (invoke->GetCodePtrLocation()) {
9026 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
9027 __ bl(GetFrameEntryLabel());
9028 break;
Vladimir Marko58155012015-08-19 12:49:41 +00009029 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
9030 // LR = callee_method->entry_point_from_quick_compiled_code_
9031 __ LoadFromOffset(
9032 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07009033 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00009034 // LR()
9035 __ blx(LR);
9036 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08009037 }
9038
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08009039 DCHECK(!IsLeafMethod());
9040}
9041
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009042void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
9043 Register temp = temp_location.AsRegister<Register>();
9044 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9045 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00009046
9047 // Use the calling convention instead of the location of the receiver, as
9048 // intrinsics may have put the receiver in a different register. In the intrinsics
9049 // slow path, the arguments have been moved to the right place, so here we are
9050 // guaranteed that the receiver is the first register of the calling convention.
9051 InvokeDexCallingConvention calling_convention;
9052 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009053 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00009054 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00009055 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009056 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00009057 // Instead of simply (possibly) unpoisoning `temp` here, we should
9058 // emit a read barrier for the previous class reference load.
9059 // However this is not required in practice, as this is an
9060 // intermediate/temporary reference and because the current
9061 // concurrent copying collector keeps the from-space memory
9062 // intact/accessible until the end of the marking phase (the
9063 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009064 __ MaybeUnpoisonHeapReference(temp);
9065 // temp = temp->GetMethodAt(method_offset);
9066 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07009067 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009068 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
9069 // LR = temp->GetEntryPoint();
9070 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
9071 // LR();
9072 __ blx(LR);
9073}
9074
Vladimir Marko65979462017-05-19 17:25:12 +01009075CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeMethodPatch(
9076 MethodReference target_method) {
9077 return NewPcRelativePatch(*target_method.dex_file,
9078 target_method.dex_method_index,
9079 &pc_relative_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009080}
9081
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01009082CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08009083 const DexFile& dex_file, dex::TypeIndex type_index) {
9084 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01009085}
9086
Vladimir Marko1998cd02017-01-13 13:02:58 +00009087CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
9088 const DexFile& dex_file, dex::TypeIndex type_index) {
9089 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
9090}
9091
Vladimir Marko65979462017-05-19 17:25:12 +01009092CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
9093 const DexFile& dex_file, dex::StringIndex string_index) {
9094 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
9095}
9096
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009097CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
9098 const DexFile& dex_file, uint32_t element_offset) {
9099 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
9100}
9101
9102CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
9103 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
9104 patches->emplace_back(dex_file, offset_or_index);
9105 return &patches->back();
9106}
9107
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009108Label* CodeGeneratorARM::NewBakerReadBarrierPatch(uint32_t custom_data) {
9109 baker_read_barrier_patches_.emplace_back(custom_data);
9110 return &baker_read_barrier_patches_.back().label;
9111}
9112
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009113Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009114 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009115}
9116
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009117Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009118 dex::StringIndex string_index,
9119 Handle<mirror::String> handle) {
9120 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
9121 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009122 return jit_string_patches_.GetOrCreate(
9123 StringReference(&dex_file, string_index),
9124 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
9125}
9126
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009127Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
9128 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009129 Handle<mirror::Class> handle) {
9130 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
9131 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009132 return jit_class_patches_.GetOrCreate(
9133 TypeReference(&dex_file, type_index),
9134 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
9135}
9136
Vladimir Markoaad75c62016-10-03 08:46:48 +00009137template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
9138inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
9139 const ArenaDeque<PcRelativePatchInfo>& infos,
9140 ArenaVector<LinkerPatch>* linker_patches) {
9141 for (const PcRelativePatchInfo& info : infos) {
9142 const DexFile& dex_file = info.target_dex_file;
9143 size_t offset_or_index = info.offset_or_index;
9144 DCHECK(info.add_pc_label.IsBound());
9145 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
9146 // Add MOVW patch.
9147 DCHECK(info.movw_label.IsBound());
9148 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
9149 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
9150 // Add MOVT patch.
9151 DCHECK(info.movt_label.IsBound());
9152 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
9153 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
9154 }
9155}
9156
Vladimir Marko58155012015-08-19 12:49:41 +00009157void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
9158 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00009159 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00009160 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009161 /* MOVW+MOVT for each entry */ 2u * pc_relative_method_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00009162 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009163 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009164 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009165 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00009166 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00009167 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
9168 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009169 if (GetCompilerOptions().IsBootImage()) {
9170 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00009171 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00009172 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
9173 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00009174 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
9175 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009176 } else {
9177 DCHECK(pc_relative_method_patches_.empty());
9178 DCHECK(pc_relative_type_patches_.empty());
9179 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
9180 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009181 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009182 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
9183 linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009184 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
9185 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.Position(),
9186 info.custom_data));
9187 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009188 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009189}
9190
9191Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
9192 return map->GetOrCreate(
9193 value,
9194 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00009195}
9196
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03009197void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9198 LocationSummary* locations =
9199 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
9200 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9201 Location::RequiresRegister());
9202 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9203 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9204 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9205}
9206
9207void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9208 LocationSummary* locations = instr->GetLocations();
9209 Register res = locations->Out().AsRegister<Register>();
9210 Register accumulator =
9211 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
9212 Register mul_left =
9213 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
9214 Register mul_right =
9215 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
9216
9217 if (instr->GetOpKind() == HInstruction::kAdd) {
9218 __ mla(res, mul_left, mul_right, accumulator);
9219 } else {
9220 __ mls(res, mul_left, mul_right, accumulator);
9221 }
9222}
9223
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01009224void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00009225 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00009226 LOG(FATAL) << "Unreachable";
9227}
9228
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01009229void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00009230 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00009231 LOG(FATAL) << "Unreachable";
9232}
9233
Mark Mendellfe57faa2015-09-18 09:26:15 -04009234// Simple implementation of packed switch - generate cascaded compare/jumps.
9235void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9236 LocationSummary* locations =
9237 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9238 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009239 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009240 codegen_->GetAssembler()->IsThumb()) {
9241 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9242 if (switch_instr->GetStartValue() != 0) {
9243 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9244 }
9245 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04009246}
9247
9248void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9249 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009250 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04009251 LocationSummary* locations = switch_instr->GetLocations();
9252 Register value_reg = locations->InAt(0).AsRegister<Register>();
9253 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9254
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009255 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009256 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009257 Register temp_reg = IP;
9258 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9259 // the immediate, because IP is used as the destination register. For the other
9260 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9261 // and they can be encoded in the instruction without making use of IP register.
9262 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
9263
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009264 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009265 // Jump to successors[0] if value == lower_bound.
9266 __ b(codegen_->GetLabelOf(successors[0]), EQ);
9267 int32_t last_index = 0;
9268 for (; num_entries - last_index > 2; last_index += 2) {
9269 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
9270 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9271 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
9272 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9273 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
9274 }
9275 if (num_entries - last_index == 2) {
9276 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00009277 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009278 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009279 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04009280
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009281 // And the default for any other value.
9282 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9283 __ b(codegen_->GetLabelOf(default_block));
9284 }
9285 } else {
9286 // Create a table lookup.
9287 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
9288
9289 // Materialize a pointer to the switch table
9290 std::vector<Label*> labels(num_entries);
9291 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9292 for (uint32_t i = 0; i < num_entries; i++) {
9293 labels[i] = codegen_->GetLabelOf(successors[i]);
9294 }
9295 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
9296
9297 // Remove the bias.
9298 Register key_reg;
9299 if (lower_bound != 0) {
9300 key_reg = locations->GetTemp(1).AsRegister<Register>();
9301 __ AddConstant(key_reg, value_reg, -lower_bound);
9302 } else {
9303 key_reg = value_reg;
9304 }
9305
9306 // Check whether the value is in the table, jump to default block if not.
9307 __ CmpConstant(key_reg, num_entries - 1);
9308 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
9309
9310 // Load the displacement from the table.
9311 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
9312
9313 // Dispatch is a direct add to the PC (for Thumb2).
9314 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04009315 }
9316}
9317
Vladimir Markob4536b72015-11-24 13:45:23 +00009318void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
9319 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
9320 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00009321}
9322
9323void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
9324 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009325 CodeGeneratorARM::PcRelativePatchInfo* labels =
9326 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00009327 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009328 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00009329 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009330 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00009331 __ BindTrackedLabel(&labels->add_pc_label);
9332 __ add(base_reg, base_reg, ShifterOperand(PC));
9333}
9334
Andreas Gampe85b62f22015-09-09 13:15:38 -07009335void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
9336 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00009337 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07009338 return;
9339 }
9340
9341 DCHECK_NE(type, Primitive::kPrimVoid);
9342
9343 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
9344 if (return_loc.Equals(trg)) {
9345 return;
9346 }
9347
9348 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9349 // with the last branch.
9350 if (type == Primitive::kPrimLong) {
9351 HParallelMove parallel_move(GetGraph()->GetArena());
9352 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
9353 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
9354 GetMoveResolver()->EmitNativeCode(&parallel_move);
9355 } else if (type == Primitive::kPrimDouble) {
9356 HParallelMove parallel_move(GetGraph()->GetArena());
9357 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
9358 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
9359 GetMoveResolver()->EmitNativeCode(&parallel_move);
9360 } else {
9361 // Let the parallel move resolver take care of all of this.
9362 HParallelMove parallel_move(GetGraph()->GetArena());
9363 parallel_move.AddMove(return_loc, trg, type, nullptr);
9364 GetMoveResolver()->EmitNativeCode(&parallel_move);
9365 }
9366}
9367
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009368void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
9369 LocationSummary* locations =
9370 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9371 locations->SetInAt(0, Location::RequiresRegister());
9372 locations->SetOut(Location::RequiresRegister());
9373}
9374
9375void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
9376 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009377 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009378 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009379 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009380 __ LoadFromOffset(kLoadWord,
9381 locations->Out().AsRegister<Register>(),
9382 locations->InAt(0).AsRegister<Register>(),
9383 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009384 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009385 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009386 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009387 __ LoadFromOffset(kLoadWord,
9388 locations->Out().AsRegister<Register>(),
9389 locations->InAt(0).AsRegister<Register>(),
9390 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9391 __ LoadFromOffset(kLoadWord,
9392 locations->Out().AsRegister<Register>(),
9393 locations->Out().AsRegister<Register>(),
9394 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009395 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009396}
9397
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009398static void PatchJitRootUse(uint8_t* code,
9399 const uint8_t* roots_data,
9400 Literal* literal,
9401 uint64_t index_in_table) {
9402 DCHECK(literal->GetLabel()->IsBound());
9403 uint32_t literal_offset = literal->GetLabel()->Position();
9404 uintptr_t address =
9405 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9406 uint8_t* data = code + literal_offset;
9407 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9408}
9409
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009410void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9411 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009412 const StringReference& string_reference = entry.first;
9413 Literal* table_entry_literal = entry.second;
9414 const auto it = jit_string_roots_.find(string_reference);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009415 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009416 uint64_t index_in_table = it->second;
9417 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009418 }
9419 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009420 const TypeReference& type_reference = entry.first;
9421 Literal* table_entry_literal = entry.second;
9422 const auto it = jit_class_roots_.find(type_reference);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009423 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009424 uint64_t index_in_table = it->second;
9425 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009426 }
9427}
9428
Roland Levillain4d027112015-07-01 15:41:14 +01009429#undef __
9430#undef QUICK_ENTRY_POINT
9431
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00009432} // namespace arm
9433} // namespace art