blob: 3c6e277ff924d946d04b706354344c881d6d6c92 [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) {
93 DCHECK(down_cast<Thumb2Assembler*>(codegen->GetAssembler())->IsForced32Bit());
94 __ BindTrackedLabel(bne_label);
95 Label placeholder_label;
96 __ b(&placeholder_label, NE); // Placeholder, patched at link-time.
97 __ Bind(&placeholder_label);
98}
99
Artem Serovf4d6aee2016-07-11 10:41:45 +0100100static constexpr int kRegListThreshold = 4;
101
Artem Serovd300d8f2016-07-15 14:00:56 +0100102// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
103// for each live D registers they treat two corresponding S registers as live ones.
104//
105// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
106// from a list of contiguous S registers a list of contiguous D registers (processing first/last
107// S registers corner cases) and save/restore this new list treating them as D registers.
108// - decreasing code size
109// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
110// restored and then used in regular non SlowPath code as D register.
111//
112// For the following example (v means the S register is live):
113// D names: | D0 | D1 | D2 | D4 | ...
114// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
115// Live? | | v | v | v | v | v | v | | ...
116//
117// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
118// as D registers.
119static size_t SaveContiguousSRegisterList(size_t first,
120 size_t last,
121 CodeGenerator* codegen,
122 size_t stack_offset) {
123 DCHECK_LE(first, last);
124 if ((first == last) && (first == 0)) {
125 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first);
126 return stack_offset;
127 }
128 if (first % 2 == 1) {
129 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first++);
130 }
131
132 bool save_last = false;
133 if (last % 2 == 0) {
134 save_last = true;
135 --last;
136 }
137
138 if (first < last) {
139 DRegister d_reg = static_cast<DRegister>(first / 2);
140 DCHECK_EQ((last - first + 1) % 2, 0u);
141 size_t number_of_d_regs = (last - first + 1) / 2;
142
143 if (number_of_d_regs == 1) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100144 __ StoreDToOffset(d_reg, SP, stack_offset);
Artem Serovd300d8f2016-07-15 14:00:56 +0100145 } else if (number_of_d_regs > 1) {
146 __ add(IP, SP, ShifterOperand(stack_offset));
147 __ vstmiad(IP, d_reg, number_of_d_regs);
148 }
149 stack_offset += number_of_d_regs * kArmWordSize * 2;
150 }
151
152 if (save_last) {
153 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, last + 1);
154 }
155
156 return stack_offset;
157}
158
159static size_t RestoreContiguousSRegisterList(size_t first,
160 size_t last,
161 CodeGenerator* codegen,
162 size_t stack_offset) {
163 DCHECK_LE(first, last);
164 if ((first == last) && (first == 0)) {
165 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first);
166 return stack_offset;
167 }
168 if (first % 2 == 1) {
169 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first++);
170 }
171
172 bool restore_last = false;
173 if (last % 2 == 0) {
174 restore_last = true;
175 --last;
176 }
177
178 if (first < last) {
179 DRegister d_reg = static_cast<DRegister>(first / 2);
180 DCHECK_EQ((last - first + 1) % 2, 0u);
181 size_t number_of_d_regs = (last - first + 1) / 2;
182 if (number_of_d_regs == 1) {
183 __ LoadDFromOffset(d_reg, SP, stack_offset);
184 } else if (number_of_d_regs > 1) {
185 __ add(IP, SP, ShifterOperand(stack_offset));
186 __ vldmiad(IP, d_reg, number_of_d_regs);
187 }
188 stack_offset += number_of_d_regs * kArmWordSize * 2;
189 }
190
191 if (restore_last) {
192 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, last + 1);
193 }
194
195 return stack_offset;
196}
197
Artem Serovf4d6aee2016-07-11 10:41:45 +0100198void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
199 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
200 size_t orig_offset = stack_offset;
201
202 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
203 for (uint32_t i : LowToHighBits(core_spills)) {
204 // If the register holds an object, update the stack mask.
205 if (locations->RegisterContainsObject(i)) {
206 locations->SetStackBit(stack_offset / kVRegSize);
207 }
208 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
209 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
210 saved_core_stack_offsets_[i] = stack_offset;
211 stack_offset += kArmWordSize;
212 }
213
214 int reg_num = POPCOUNT(core_spills);
215 if (reg_num != 0) {
216 if (reg_num > kRegListThreshold) {
217 __ StoreList(RegList(core_spills), orig_offset);
218 } else {
219 stack_offset = orig_offset;
220 for (uint32_t i : LowToHighBits(core_spills)) {
221 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
222 }
223 }
224 }
225
Artem Serovd300d8f2016-07-15 14:00:56 +0100226 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
227 orig_offset = stack_offset;
Vladimir Marko804b03f2016-09-14 16:26:36 +0100228 for (uint32_t i : LowToHighBits(fp_spills)) {
Artem Serovf4d6aee2016-07-11 10:41:45 +0100229 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
230 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serovd300d8f2016-07-15 14:00:56 +0100231 stack_offset += kArmWordSize;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100232 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100233
234 stack_offset = orig_offset;
235 while (fp_spills != 0u) {
236 uint32_t begin = CTZ(fp_spills);
237 uint32_t tmp = fp_spills + (1u << begin);
238 fp_spills &= tmp; // Clear the contiguous range of 1s.
239 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
240 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
241 }
242 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100243}
244
245void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
246 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
247 size_t orig_offset = stack_offset;
248
249 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
250 for (uint32_t i : LowToHighBits(core_spills)) {
251 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
252 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
253 stack_offset += kArmWordSize;
254 }
255
256 int reg_num = POPCOUNT(core_spills);
257 if (reg_num != 0) {
258 if (reg_num > kRegListThreshold) {
259 __ LoadList(RegList(core_spills), orig_offset);
260 } else {
261 stack_offset = orig_offset;
262 for (uint32_t i : LowToHighBits(core_spills)) {
263 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
264 }
265 }
266 }
267
Artem Serovd300d8f2016-07-15 14:00:56 +0100268 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
269 while (fp_spills != 0u) {
270 uint32_t begin = CTZ(fp_spills);
271 uint32_t tmp = fp_spills + (1u << begin);
272 fp_spills &= tmp; // Clear the contiguous range of 1s.
273 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
274 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
Artem Serovf4d6aee2016-07-11 10:41:45 +0100275 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100276 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100277}
278
279class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100280 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100281 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100282
Alexandre Rames67555f72014-11-18 10:55:16 +0000283 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100284 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100285 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000286 if (instruction_->CanThrowIntoCatchBlock()) {
287 // Live registers will be restored in the catch block if caught.
288 SaveLiveRegisters(codegen, instruction_->GetLocations());
289 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100290 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
291 instruction_,
292 instruction_->GetDexPc(),
293 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000294 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100295 }
296
Alexandre Rames8158f282015-08-07 10:26:17 +0100297 bool IsFatal() const OVERRIDE { return true; }
298
Alexandre Rames9931f312015-06-19 14:47:01 +0100299 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
300
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100301 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100302 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
303};
304
Artem Serovf4d6aee2016-07-11 10:41:45 +0100305class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000306 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100307 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000308
Alexandre Rames67555f72014-11-18 10:55:16 +0000309 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000310 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
311 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100312 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000313 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000314 }
315
Alexandre Rames8158f282015-08-07 10:26:17 +0100316 bool IsFatal() const OVERRIDE { return true; }
317
Alexandre Rames9931f312015-06-19 14:47:01 +0100318 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
319
Calin Juravled0d48522014-11-04 16:40:20 +0000320 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000321 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
322};
323
Artem Serovf4d6aee2016-07-11 10:41:45 +0100324class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000325 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000326 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100327 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000328
Alexandre Rames67555f72014-11-18 10:55:16 +0000329 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100330 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000331 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100332 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000333 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100334 if (successor_ == nullptr) {
335 __ b(GetReturnLabel());
336 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100337 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100338 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000339 }
340
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100341 Label* GetReturnLabel() {
342 DCHECK(successor_ == nullptr);
343 return &return_label_;
344 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000345
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100346 HBasicBlock* GetSuccessor() const {
347 return successor_;
348 }
349
Alexandre Rames9931f312015-06-19 14:47:01 +0100350 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
351
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000352 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100353 // If not null, the block to branch to after the suspend check.
354 HBasicBlock* const successor_;
355
356 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000357 Label return_label_;
358
359 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
360};
361
Artem Serovf4d6aee2016-07-11 10:41:45 +0100362class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100363 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100364 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100365 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100366
Alexandre Rames67555f72014-11-18 10:55:16 +0000367 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100368 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100369 LocationSummary* locations = instruction_->GetLocations();
370
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100371 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000372 if (instruction_->CanThrowIntoCatchBlock()) {
373 // Live registers will be restored in the catch block if caught.
374 SaveLiveRegisters(codegen, instruction_->GetLocations());
375 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000376 // We're moving two locations to locations that could overlap, so we need a parallel
377 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100378 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000379 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100380 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000381 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100382 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100383 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100384 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
385 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100386 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
387 ? kQuickThrowStringBounds
388 : kQuickThrowArrayBounds;
389 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100390 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000391 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100392 }
393
Alexandre Rames8158f282015-08-07 10:26:17 +0100394 bool IsFatal() const OVERRIDE { return true; }
395
Alexandre Rames9931f312015-06-19 14:47:01 +0100396 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
397
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100398 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100399 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
400};
401
Artem Serovf4d6aee2016-07-11 10:41:45 +0100402class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100403 public:
Vladimir Markoea4c1262017-02-06 19:59:33 +0000404 LoadClassSlowPathARM(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000405 : SlowPathCodeARM(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000406 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
407 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100408
Alexandre Rames67555f72014-11-18 10:55:16 +0000409 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000410 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000411 Location out = locations->Out();
412 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000413
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100414 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
415 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000416 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100417
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100418 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000419 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
420 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
421 bool is_load_class_bss_entry =
422 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
423 Register entry_address = kNoRegister;
424 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
425 Register temp = locations->GetTemp(0).AsRegister<Register>();
426 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
427 // the kSaveEverything call.
428 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
429 entry_address = temp_is_r0 ? out.AsRegister<Register>() : temp;
430 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
431 if (temp_is_r0) {
432 __ mov(entry_address, ShifterOperand(temp));
433 }
434 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000435 dex::TypeIndex type_index = cls_->GetTypeIndex();
436 __ LoadImmediate(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100437 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
438 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000439 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000440 if (do_clinit_) {
441 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
442 } else {
443 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
444 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000445
Vladimir Markoea4c1262017-02-06 19:59:33 +0000446 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
447 if (is_load_class_bss_entry) {
448 if (call_saves_everything_except_r0) {
449 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
450 __ str(R0, Address(entry_address));
451 } else {
452 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
453 Register temp = IP;
454 CodeGeneratorARM::PcRelativePatchInfo* labels =
455 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
456 __ BindTrackedLabel(&labels->movw_label);
457 __ movw(temp, /* placeholder */ 0u);
458 __ BindTrackedLabel(&labels->movt_label);
459 __ movt(temp, /* placeholder */ 0u);
460 __ BindTrackedLabel(&labels->add_pc_label);
461 __ add(temp, temp, ShifterOperand(PC));
462 __ str(R0, Address(temp));
463 }
464 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000465 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000466 if (out.IsValid()) {
467 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000468 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
469 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000470 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100471 __ b(GetExitLabel());
472 }
473
Alexandre Rames9931f312015-06-19 14:47:01 +0100474 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
475
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100476 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000477 // The class this slow path will load.
478 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100479
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000480 // The dex PC of `at_`.
481 const uint32_t dex_pc_;
482
483 // Whether to initialize the class.
484 const bool do_clinit_;
485
486 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100487};
488
Vladimir Markoaad75c62016-10-03 08:46:48 +0000489class LoadStringSlowPathARM : public SlowPathCodeARM {
490 public:
491 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
492
493 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000494 DCHECK(instruction_->IsLoadString());
495 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000496 LocationSummary* locations = instruction_->GetLocations();
497 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100498 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000499 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100500 Register out = locations->Out().AsRegister<Register>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100501 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000502
503 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
504 __ Bind(GetEntryLabel());
505 SaveLiveRegisters(codegen, locations);
506
507 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100508 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000509 // the kSaveEverything call.
510 Register entry_address = kNoRegister;
511 if (call_saves_everything_except_r0) {
512 Register temp = locations->GetTemp(0).AsRegister<Register>();
513 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
514 entry_address = temp_is_r0 ? out : temp;
515 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
516 if (temp_is_r0) {
517 __ mov(entry_address, ShifterOperand(temp));
518 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100519 }
520
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000521 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000522 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
523 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100524
525 // Store the resolved String to the .bss entry.
526 if (call_saves_everything_except_r0) {
527 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
528 __ str(R0, Address(entry_address));
529 } else {
530 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000531 Register temp = IP;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100532 CodeGeneratorARM::PcRelativePatchInfo* labels =
533 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
534 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000535 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100536 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000537 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100538 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000539 __ add(temp, temp, ShifterOperand(PC));
540 __ str(R0, Address(temp));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100541 }
542
Vladimir Markoaad75c62016-10-03 08:46:48 +0000543 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000544 RestoreLiveRegisters(codegen, locations);
545
Vladimir Markoaad75c62016-10-03 08:46:48 +0000546 __ b(GetExitLabel());
547 }
548
549 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
550
551 private:
552 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
553};
554
Artem Serovf4d6aee2016-07-11 10:41:45 +0100555class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000556 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000557 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100558 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000559
Alexandre Rames67555f72014-11-18 10:55:16 +0000560 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000561 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000562 DCHECK(instruction_->IsCheckCast()
563 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000564
565 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
566 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000567
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000568 if (!is_fatal_) {
569 SaveLiveRegisters(codegen, locations);
570 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000571
572 // We're moving two locations to locations that could overlap, so we need a parallel
573 // move resolver.
574 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800575 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800576 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
577 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800578 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800579 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
580 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000581 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100582 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100583 instruction_,
584 instruction_->GetDexPc(),
585 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800586 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000587 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
588 } else {
589 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800590 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
591 instruction_,
592 instruction_->GetDexPc(),
593 this);
594 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000595 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000596
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000597 if (!is_fatal_) {
598 RestoreLiveRegisters(codegen, locations);
599 __ b(GetExitLabel());
600 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000601 }
602
Alexandre Rames9931f312015-06-19 14:47:01 +0100603 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
604
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000605 bool IsFatal() const OVERRIDE { return is_fatal_; }
606
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000607 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000608 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000609
610 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
611};
612
Artem Serovf4d6aee2016-07-11 10:41:45 +0100613class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700614 public:
Aart Bik42249c32016-01-07 15:33:50 -0800615 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100616 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700617
618 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800619 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700620 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100621 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000622 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700623 }
624
Alexandre Rames9931f312015-06-19 14:47:01 +0100625 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
626
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700627 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700628 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
629};
630
Artem Serovf4d6aee2016-07-11 10:41:45 +0100631class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100632 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100633 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100634
635 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
636 LocationSummary* locations = instruction_->GetLocations();
637 __ Bind(GetEntryLabel());
638 SaveLiveRegisters(codegen, locations);
639
640 InvokeRuntimeCallingConvention calling_convention;
641 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
642 parallel_move.AddMove(
643 locations->InAt(0),
644 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
645 Primitive::kPrimNot,
646 nullptr);
647 parallel_move.AddMove(
648 locations->InAt(1),
649 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
650 Primitive::kPrimInt,
651 nullptr);
652 parallel_move.AddMove(
653 locations->InAt(2),
654 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
655 Primitive::kPrimNot,
656 nullptr);
657 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
658
659 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100660 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000661 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100662 RestoreLiveRegisters(codegen, locations);
663 __ b(GetExitLabel());
664 }
665
666 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
667
668 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100669 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
670};
671
Roland Levillain54f869e2017-03-06 13:54:11 +0000672// Abstract base class for read barrier slow paths marking a reference
673// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000674//
Roland Levillain54f869e2017-03-06 13:54:11 +0000675// Argument `entrypoint` must be a register location holding the read
676// barrier marking runtime entry point to be invoked.
677class ReadBarrierMarkSlowPathBaseARM : public SlowPathCodeARM {
678 protected:
679 ReadBarrierMarkSlowPathBaseARM(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000680 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
681 DCHECK(kEmitCompilerReadBarrier);
682 }
683
Roland Levillain54f869e2017-03-06 13:54:11 +0000684 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000685
Roland Levillain54f869e2017-03-06 13:54:11 +0000686 // Generate assembly code calling the read barrier marking runtime
687 // entry point (ReadBarrierMarkRegX).
688 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000689 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain47b3ab22017-02-27 14:31:35 +0000690
Roland Levillain47b3ab22017-02-27 14:31:35 +0000691 // No need to save live registers; it's taken care of by the
692 // entrypoint. Also, there is no need to update the stack mask,
693 // as this runtime call will not trigger a garbage collection.
694 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
695 DCHECK_NE(ref_reg, SP);
696 DCHECK_NE(ref_reg, LR);
697 DCHECK_NE(ref_reg, PC);
698 // IP is used internally by the ReadBarrierMarkRegX entry point
699 // as a temporary, it cannot be the entry point's input/output.
700 DCHECK_NE(ref_reg, IP);
701 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
702 // "Compact" slow path, saving two moves.
703 //
704 // Instead of using the standard runtime calling convention (input
705 // and output in R0):
706 //
707 // R0 <- ref
708 // R0 <- ReadBarrierMark(R0)
709 // ref <- R0
710 //
711 // we just use rX (the register containing `ref`) as input and output
712 // of a dedicated entrypoint:
713 //
714 // rX <- ReadBarrierMarkRegX(rX)
715 //
716 if (entrypoint_.IsValid()) {
717 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
718 __ blx(entrypoint_.AsRegister<Register>());
719 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000720 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000721 int32_t entry_point_offset =
722 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
723 // This runtime call does not require a stack map.
724 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
725 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000726 }
727
728 // The location (register) of the marked object reference.
729 const Location ref_;
730
731 // The location of the entrypoint if it is already loaded.
732 const Location entrypoint_;
733
734 private:
735 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM);
736};
737
Dave Allison20dfc792014-06-16 20:44:29 -0700738// Slow path marking an object reference `ref` during a read
739// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000740// reference does not get updated by this slow path after marking.
Dave Allison20dfc792014-06-16 20:44:29 -0700741//
742// This means that after the execution of this slow path, `ref` will
743// always be up-to-date, but `obj.field` may not; i.e., after the
744// flip, `ref` will be a to-space reference, but `obj.field` will
745// probably still be a from-space reference (unless it gets updated by
746// another thread, or if another thread installed another object
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000747// reference (different from `ref`) in `obj.field`).
748//
749// If `entrypoint` is a valid location it is assumed to already be
750// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000751// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000752class ReadBarrierMarkSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000753 public:
754 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
755 Location ref,
756 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000757 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000758 DCHECK(kEmitCompilerReadBarrier);
759 }
760
761 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
762
763 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
764 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000765 DCHECK(locations->CanCall());
766 if (kIsDebugBuild) {
767 Register ref_reg = ref_.AsRegister<Register>();
768 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
769 }
770 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
771 << "Unexpected instruction in read barrier marking slow path: "
772 << instruction_->DebugName();
773
774 __ Bind(GetEntryLabel());
775 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000776 __ b(GetExitLabel());
777 }
778
779 private:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000780 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
781};
782
Roland Levillain54f869e2017-03-06 13:54:11 +0000783// Slow path loading `obj`'s lock word, loading a reference from
784// object `*(obj + offset + (index << scale_factor))` into `ref`, and
785// marking `ref` if `obj` is gray according to the lock word (Baker
786// read barrier). The field `obj.field` in the object `obj` holding
787// this reference does not get updated by this slow path after marking
788// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
789// below for that).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000790//
Roland Levillain54f869e2017-03-06 13:54:11 +0000791// This means that after the execution of this slow path, `ref` will
792// always be up-to-date, but `obj.field` may not; i.e., after the
793// flip, `ref` will be a to-space reference, but `obj.field` will
794// probably still be a from-space reference (unless it gets updated by
795// another thread, or if another thread installed another object
796// reference (different from `ref`) in `obj.field`).
797//
798// Argument `entrypoint` must be a register location holding the read
799// barrier marking runtime entry point to be invoked.
800class LoadReferenceWithBakerReadBarrierSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000801 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000802 LoadReferenceWithBakerReadBarrierSlowPathARM(HInstruction* instruction,
803 Location ref,
804 Register obj,
805 uint32_t offset,
806 Location index,
807 ScaleFactor scale_factor,
808 bool needs_null_check,
809 Register temp,
810 Location entrypoint)
811 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000812 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000813 offset_(offset),
814 index_(index),
815 scale_factor_(scale_factor),
816 needs_null_check_(needs_null_check),
817 temp_(temp) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000818 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000819 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000820 }
821
Roland Levillain54f869e2017-03-06 13:54:11 +0000822 const char* GetDescription() const OVERRIDE {
823 return "LoadReferenceWithBakerReadBarrierSlowPathARM";
824 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000825
826 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
827 LocationSummary* locations = instruction_->GetLocations();
828 Register ref_reg = ref_.AsRegister<Register>();
829 DCHECK(locations->CanCall());
830 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000831 DCHECK_NE(ref_reg, temp_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000832 DCHECK(instruction_->IsInstanceFieldGet() ||
833 instruction_->IsStaticFieldGet() ||
834 instruction_->IsArrayGet() ||
835 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000836 instruction_->IsInstanceOf() ||
837 instruction_->IsCheckCast() ||
838 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
839 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
840 << "Unexpected instruction in read barrier marking slow path: "
841 << instruction_->DebugName();
842 // The read barrier instrumentation of object ArrayGet
843 // instructions does not support the HIntermediateAddress
844 // instruction.
845 DCHECK(!(instruction_->IsArrayGet() &&
846 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
847
848 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000849
850 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
851 // inserted after the original load. However, in fast path based
852 // Baker's read barriers, we need to perform the load of
853 // mirror::Object::monitor_ *before* the original reference load.
854 // This load-load ordering is required by the read barrier.
855 // The fast path/slow path (for Baker's algorithm) should look like:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000856 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000857 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
858 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
859 // HeapReference<mirror::Object> ref = *src; // Original reference load.
860 // bool is_gray = (rb_state == ReadBarrier::GrayState());
861 // if (is_gray) {
862 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
863 // }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000864 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000865 // Note: the original implementation in ReadBarrier::Barrier is
866 // slightly more complex as it performs additional checks that we do
867 // not do here for performance reasons.
868
869 // /* int32_t */ monitor = obj->monitor_
870 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
871 __ LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
872 if (needs_null_check_) {
873 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000874 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000875 // /* LockWord */ lock_word = LockWord(monitor)
876 static_assert(sizeof(LockWord) == sizeof(int32_t),
877 "art::LockWord and int32_t have different sizes.");
878
879 // Introduce a dependency on the lock_word including the rb_state,
880 // which shall prevent load-load reordering without using
881 // a memory barrier (which would be more expensive).
882 // `obj` is unchanged by this operation, but its value now depends
883 // on `temp`.
884 __ add(obj_, obj_, ShifterOperand(temp_, LSR, 32));
885
886 // The actual reference load.
887 // A possible implicit null check has already been handled above.
888 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
889 arm_codegen->GenerateRawReferenceLoad(
890 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
891
892 // Mark the object `ref` when `obj` is gray.
893 //
894 // if (rb_state == ReadBarrier::GrayState())
895 // ref = ReadBarrier::Mark(ref);
896 //
897 // Given the numeric representation, it's enough to check the low bit of the
898 // rb_state. We do that by shifting the bit out of the lock word with LSRS
899 // which can be a 16-bit instruction unlike the TST immediate.
900 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
901 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
902 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
903 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
904 GenerateReadBarrierMarkRuntimeCall(codegen);
905
Roland Levillain47b3ab22017-02-27 14:31:35 +0000906 __ b(GetExitLabel());
907 }
908
909 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000910 // The register containing the object holding the marked object reference field.
911 Register obj_;
912 // The offset, index and scale factor to access the reference in `obj_`.
913 uint32_t offset_;
914 Location index_;
915 ScaleFactor scale_factor_;
916 // Is a null check required?
917 bool needs_null_check_;
918 // A temporary register used to hold the lock word of `obj_`.
919 Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000920
Roland Levillain54f869e2017-03-06 13:54:11 +0000921 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000922};
923
Roland Levillain54f869e2017-03-06 13:54:11 +0000924// Slow path loading `obj`'s lock word, loading a reference from
925// object `*(obj + offset + (index << scale_factor))` into `ref`, and
926// marking `ref` if `obj` is gray according to the lock word (Baker
927// read barrier). If needed, this slow path also atomically updates
928// the field `obj.field` in the object `obj` holding this reference
929// after marking (contrary to
930// LoadReferenceWithBakerReadBarrierSlowPathARM above, which never
931// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000932//
933// This means that after the execution of this slow path, both `ref`
934// and `obj.field` will be up-to-date; i.e., after the flip, both will
935// hold the same to-space reference (unless another thread installed
936// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000937//
Roland Levillain54f869e2017-03-06 13:54:11 +0000938// Argument `entrypoint` must be a register location holding the read
939// barrier marking runtime entry point to be invoked.
940class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
941 : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000942 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000943 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(HInstruction* instruction,
944 Location ref,
945 Register obj,
946 uint32_t offset,
947 Location index,
948 ScaleFactor scale_factor,
949 bool needs_null_check,
950 Register temp1,
951 Register temp2,
952 Location entrypoint)
953 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000954 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000955 offset_(offset),
956 index_(index),
957 scale_factor_(scale_factor),
958 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000959 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +0000960 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000961 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000962 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000963 }
964
Roland Levillain54f869e2017-03-06 13:54:11 +0000965 const char* GetDescription() const OVERRIDE {
966 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM";
967 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000968
969 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
970 LocationSummary* locations = instruction_->GetLocations();
971 Register ref_reg = ref_.AsRegister<Register>();
972 DCHECK(locations->CanCall());
973 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000974 DCHECK_NE(ref_reg, temp1_);
975
976 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000977 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
978 << "Unexpected instruction in read barrier marking and field updating slow path: "
979 << instruction_->DebugName();
980 DCHECK(instruction_->GetLocations()->Intrinsified());
981 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000982 DCHECK_EQ(offset_, 0u);
983 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
984 // The location of the offset of the marked reference field within `obj_`.
985 Location field_offset = index_;
986 DCHECK(field_offset.IsRegisterPair()) << field_offset;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000987
988 __ Bind(GetEntryLabel());
989
Roland Levillain54f869e2017-03-06 13:54:11 +0000990 // /* int32_t */ monitor = obj->monitor_
991 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
992 __ LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
993 if (needs_null_check_) {
994 codegen->MaybeRecordImplicitNullCheck(instruction_);
995 }
996 // /* LockWord */ lock_word = LockWord(monitor)
997 static_assert(sizeof(LockWord) == sizeof(int32_t),
998 "art::LockWord and int32_t have different sizes.");
999
1000 // Introduce a dependency on the lock_word including the rb_state,
1001 // which shall prevent load-load reordering without using
1002 // a memory barrier (which would be more expensive).
1003 // `obj` is unchanged by this operation, but its value now depends
1004 // on `temp1`.
1005 __ add(obj_, obj_, ShifterOperand(temp1_, LSR, 32));
1006
1007 // The actual reference load.
1008 // A possible implicit null check has already been handled above.
1009 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1010 arm_codegen->GenerateRawReferenceLoad(
1011 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1012
1013 // Mark the object `ref` when `obj` is gray.
1014 //
1015 // if (rb_state == ReadBarrier::GrayState())
1016 // ref = ReadBarrier::Mark(ref);
1017 //
1018 // Given the numeric representation, it's enough to check the low bit of the
1019 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1020 // which can be a 16-bit instruction unlike the TST immediate.
1021 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1022 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1023 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1024 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
1025
1026 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001027 // Note that we cannot use IP to save the old reference, as IP is
1028 // used internally by the ReadBarrierMarkRegX entry point, and we
1029 // need the old reference after the call to that entry point.
1030 DCHECK_NE(temp1_, IP);
1031 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001032
Roland Levillain54f869e2017-03-06 13:54:11 +00001033 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001034
1035 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001036 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001037 //
1038 // Note that this field could also hold a different object, if
1039 // another thread had concurrently changed it. In that case, the
1040 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1041 // (CAS) operation below would abort the CAS, leaving the field
1042 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001043 __ cmp(temp1_, ShifterOperand(ref_reg));
Roland Levillain54f869e2017-03-06 13:54:11 +00001044 __ b(GetExitLabel(), EQ);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001045
1046 // Update the the holder's field atomically. This may fail if
1047 // mutator updates before us, but it's OK. This is achieved
1048 // using a strong compare-and-set (CAS) operation with relaxed
1049 // memory synchronization ordering, where the expected value is
1050 // the old reference and the desired value is the new reference.
1051
1052 // Convenience aliases.
1053 Register base = obj_;
1054 // The UnsafeCASObject intrinsic uses a register pair as field
1055 // offset ("long offset"), of which only the low part contains
1056 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001057 Register offset = field_offset.AsRegisterPairLow<Register>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001058 Register expected = temp1_;
1059 Register value = ref_reg;
1060 Register tmp_ptr = IP; // Pointer to actual memory.
1061 Register tmp = temp2_; // Value in memory.
1062
1063 __ add(tmp_ptr, base, ShifterOperand(offset));
1064
1065 if (kPoisonHeapReferences) {
1066 __ PoisonHeapReference(expected);
1067 if (value == expected) {
1068 // Do not poison `value`, as it is the same register as
1069 // `expected`, which has just been poisoned.
1070 } else {
1071 __ PoisonHeapReference(value);
1072 }
1073 }
1074
1075 // do {
1076 // tmp = [r_ptr] - expected;
1077 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1078
Roland Levillain24a4d112016-10-26 13:10:46 +01001079 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001080 __ Bind(&loop_head);
1081
1082 __ ldrex(tmp, tmp_ptr);
1083
1084 __ subs(tmp, tmp, ShifterOperand(expected));
1085
Roland Levillain24a4d112016-10-26 13:10:46 +01001086 __ it(NE);
1087 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001088
Roland Levillain24a4d112016-10-26 13:10:46 +01001089 __ b(&exit_loop, NE);
1090
1091 __ strex(tmp, value, tmp_ptr);
1092 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001093 __ b(&loop_head, EQ);
1094
Roland Levillain24a4d112016-10-26 13:10:46 +01001095 __ Bind(&exit_loop);
1096
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001097 if (kPoisonHeapReferences) {
1098 __ UnpoisonHeapReference(expected);
1099 if (value == expected) {
1100 // Do not unpoison `value`, as it is the same register as
1101 // `expected`, which has just been unpoisoned.
1102 } else {
1103 __ UnpoisonHeapReference(value);
1104 }
1105 }
1106
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001107 __ b(GetExitLabel());
1108 }
1109
1110 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001111 // The register containing the object holding the marked object reference field.
1112 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001113 // The offset, index and scale factor to access the reference in `obj_`.
1114 uint32_t offset_;
1115 Location index_;
1116 ScaleFactor scale_factor_;
1117 // Is a null check required?
1118 bool needs_null_check_;
1119 // A temporary register used to hold the lock word of `obj_`; and
1120 // also to hold the original reference value, when the reference is
1121 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001122 const Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001123 // A temporary register used in the implementation of the CAS, to
1124 // update the object's reference field.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001125 const Register temp2_;
1126
Roland Levillain54f869e2017-03-06 13:54:11 +00001127 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001128};
1129
Roland Levillain3b359c72015-11-17 19:35:12 +00001130// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001131class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001132 public:
1133 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
1134 Location out,
1135 Location ref,
1136 Location obj,
1137 uint32_t offset,
1138 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001139 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +00001140 out_(out),
1141 ref_(ref),
1142 obj_(obj),
1143 offset_(offset),
1144 index_(index) {
1145 DCHECK(kEmitCompilerReadBarrier);
1146 // If `obj` is equal to `out` or `ref`, it means the initial object
1147 // has been overwritten by (or after) the heap object reference load
1148 // to be instrumented, e.g.:
1149 //
1150 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00001151 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001152 //
1153 // In that case, we have lost the information about the original
1154 // object, and the emitted read barrier cannot work properly.
1155 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1156 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1157 }
1158
1159 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1160 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1161 LocationSummary* locations = instruction_->GetLocations();
1162 Register reg_out = out_.AsRegister<Register>();
1163 DCHECK(locations->CanCall());
1164 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +01001165 DCHECK(instruction_->IsInstanceFieldGet() ||
1166 instruction_->IsStaticFieldGet() ||
1167 instruction_->IsArrayGet() ||
1168 instruction_->IsInstanceOf() ||
1169 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001170 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +00001171 << "Unexpected instruction in read barrier for heap reference slow path: "
1172 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001173 // The read barrier instrumentation of object ArrayGet
1174 // instructions does not support the HIntermediateAddress
1175 // instruction.
1176 DCHECK(!(instruction_->IsArrayGet() &&
1177 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +00001178
1179 __ Bind(GetEntryLabel());
1180 SaveLiveRegisters(codegen, locations);
1181
1182 // We may have to change the index's value, but as `index_` is a
1183 // constant member (like other "inputs" of this slow path),
1184 // introduce a copy of it, `index`.
1185 Location index = index_;
1186 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001187 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +00001188 if (instruction_->IsArrayGet()) {
1189 // Compute the actual memory offset and store it in `index`.
1190 Register index_reg = index_.AsRegister<Register>();
1191 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
1192 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
1193 // We are about to change the value of `index_reg` (see the
1194 // calls to art::arm::Thumb2Assembler::Lsl and
1195 // art::arm::Thumb2Assembler::AddConstant below), but it has
1196 // not been saved by the previous call to
1197 // art::SlowPathCode::SaveLiveRegisters, as it is a
1198 // callee-save register --
1199 // art::SlowPathCode::SaveLiveRegisters does not consider
1200 // callee-save registers, as it has been designed with the
1201 // assumption that callee-save registers are supposed to be
1202 // handled by the called function. So, as a callee-save
1203 // register, `index_reg` _would_ eventually be saved onto
1204 // the stack, but it would be too late: we would have
1205 // changed its value earlier. Therefore, we manually save
1206 // it here into another freely available register,
1207 // `free_reg`, chosen of course among the caller-save
1208 // registers (as a callee-save `free_reg` register would
1209 // exhibit the same problem).
1210 //
1211 // Note we could have requested a temporary register from
1212 // the register allocator instead; but we prefer not to, as
1213 // this is a slow path, and we know we can find a
1214 // caller-save register that is available.
1215 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1216 __ Mov(free_reg, index_reg);
1217 index_reg = free_reg;
1218 index = Location::RegisterLocation(index_reg);
1219 } else {
1220 // The initial register stored in `index_` has already been
1221 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1222 // (as it is not a callee-save register), so we can freely
1223 // use it.
1224 }
1225 // Shifting the index value contained in `index_reg` by the scale
1226 // factor (2) cannot overflow in practice, as the runtime is
1227 // unable to allocate object arrays with a size larger than
1228 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1229 __ Lsl(index_reg, index_reg, TIMES_4);
1230 static_assert(
1231 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1232 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1233 __ AddConstant(index_reg, index_reg, offset_);
1234 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001235 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1236 // intrinsics, `index_` is not shifted by a scale factor of 2
1237 // (as in the case of ArrayGet), as it is actually an offset
1238 // to an object field within an object.
1239 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001240 DCHECK(instruction_->GetLocations()->Intrinsified());
1241 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1242 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1243 << instruction_->AsInvoke()->GetIntrinsic();
1244 DCHECK_EQ(offset_, 0U);
1245 DCHECK(index_.IsRegisterPair());
1246 // UnsafeGet's offset location is a register pair, the low
1247 // part contains the correct offset.
1248 index = index_.ToLow();
1249 }
1250 }
1251
1252 // We're moving two or three locations to locations that could
1253 // overlap, so we need a parallel move resolver.
1254 InvokeRuntimeCallingConvention calling_convention;
1255 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1256 parallel_move.AddMove(ref_,
1257 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1258 Primitive::kPrimNot,
1259 nullptr);
1260 parallel_move.AddMove(obj_,
1261 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1262 Primitive::kPrimNot,
1263 nullptr);
1264 if (index.IsValid()) {
1265 parallel_move.AddMove(index,
1266 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1267 Primitive::kPrimInt,
1268 nullptr);
1269 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1270 } else {
1271 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1272 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1273 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001274 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001275 CheckEntrypointTypes<
1276 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1277 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1278
1279 RestoreLiveRegisters(codegen, locations);
1280 __ b(GetExitLabel());
1281 }
1282
1283 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1284
1285 private:
1286 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1287 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1288 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1289 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1290 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1291 return static_cast<Register>(i);
1292 }
1293 }
1294 // We shall never fail to find a free caller-save register, as
1295 // there are more than two core caller-save registers on ARM
1296 // (meaning it is possible to find one which is different from
1297 // `ref` and `obj`).
1298 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1299 LOG(FATAL) << "Could not find a free caller-save register";
1300 UNREACHABLE();
1301 }
1302
Roland Levillain3b359c72015-11-17 19:35:12 +00001303 const Location out_;
1304 const Location ref_;
1305 const Location obj_;
1306 const uint32_t offset_;
1307 // An additional location containing an index to an array.
1308 // Only used for HArrayGet and the UnsafeGetObject &
1309 // UnsafeGetObjectVolatile intrinsics.
1310 const Location index_;
1311
1312 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1313};
1314
1315// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001316class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001317 public:
1318 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001319 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001320 DCHECK(kEmitCompilerReadBarrier);
1321 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001322
1323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1324 LocationSummary* locations = instruction_->GetLocations();
1325 Register reg_out = out_.AsRegister<Register>();
1326 DCHECK(locations->CanCall());
1327 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001328 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1329 << "Unexpected instruction in read barrier for GC root slow path: "
1330 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001331
1332 __ Bind(GetEntryLabel());
1333 SaveLiveRegisters(codegen, locations);
1334
1335 InvokeRuntimeCallingConvention calling_convention;
1336 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1337 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001338 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001339 instruction_,
1340 instruction_->GetDexPc(),
1341 this);
1342 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1343 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1344
1345 RestoreLiveRegisters(codegen, locations);
1346 __ b(GetExitLabel());
1347 }
1348
1349 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1350
1351 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001352 const Location out_;
1353 const Location root_;
1354
1355 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1356};
1357
Aart Bike9f37602015-10-09 11:15:55 -07001358inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001359 switch (cond) {
1360 case kCondEQ: return EQ;
1361 case kCondNE: return NE;
1362 case kCondLT: return LT;
1363 case kCondLE: return LE;
1364 case kCondGT: return GT;
1365 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001366 case kCondB: return LO;
1367 case kCondBE: return LS;
1368 case kCondA: return HI;
1369 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001370 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001371 LOG(FATAL) << "Unreachable";
1372 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001373}
1374
Aart Bike9f37602015-10-09 11:15:55 -07001375// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001376inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001377 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001378 case kCondEQ: return EQ;
1379 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001380 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001381 case kCondLT: return LO;
1382 case kCondLE: return LS;
1383 case kCondGT: return HI;
1384 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001385 // Unsigned remain unchanged.
1386 case kCondB: return LO;
1387 case kCondBE: return LS;
1388 case kCondA: return HI;
1389 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001390 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001391 LOG(FATAL) << "Unreachable";
1392 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001393}
1394
Vladimir Markod6e069b2016-01-18 11:11:01 +00001395inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1396 // The ARM condition codes can express all the necessary branches, see the
1397 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1398 // There is no dex instruction or HIR that would need the missing conditions
1399 // "equal or unordered" or "not equal".
1400 switch (cond) {
1401 case kCondEQ: return EQ;
1402 case kCondNE: return NE /* unordered */;
1403 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1404 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1405 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1406 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1407 default:
1408 LOG(FATAL) << "UNREACHABLE";
1409 UNREACHABLE();
1410 }
1411}
1412
Anton Kirilov74234da2017-01-13 14:42:47 +00001413inline Shift ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1414 switch (op_kind) {
1415 case HDataProcWithShifterOp::kASR: return ASR;
1416 case HDataProcWithShifterOp::kLSL: return LSL;
1417 case HDataProcWithShifterOp::kLSR: return LSR;
1418 default:
1419 LOG(FATAL) << "Unexpected op kind " << op_kind;
1420 UNREACHABLE();
1421 }
1422}
1423
1424static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1425 Register out,
1426 Register first,
1427 const ShifterOperand& second,
1428 CodeGeneratorARM* codegen) {
1429 if (second.IsImmediate() && second.GetImmediate() == 0) {
1430 const ShifterOperand in = kind == HInstruction::kAnd
1431 ? ShifterOperand(0)
1432 : ShifterOperand(first);
1433
1434 __ mov(out, in);
1435 } else {
1436 switch (kind) {
1437 case HInstruction::kAdd:
1438 __ add(out, first, second);
1439 break;
1440 case HInstruction::kAnd:
1441 __ and_(out, first, second);
1442 break;
1443 case HInstruction::kOr:
1444 __ orr(out, first, second);
1445 break;
1446 case HInstruction::kSub:
1447 __ sub(out, first, second);
1448 break;
1449 case HInstruction::kXor:
1450 __ eor(out, first, second);
1451 break;
1452 default:
1453 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1454 UNREACHABLE();
1455 }
1456 }
1457}
1458
1459static void GenerateDataProc(HInstruction::InstructionKind kind,
1460 const Location& out,
1461 const Location& first,
1462 const ShifterOperand& second_lo,
1463 const ShifterOperand& second_hi,
1464 CodeGeneratorARM* codegen) {
1465 const Register first_hi = first.AsRegisterPairHigh<Register>();
1466 const Register first_lo = first.AsRegisterPairLow<Register>();
1467 const Register out_hi = out.AsRegisterPairHigh<Register>();
1468 const Register out_lo = out.AsRegisterPairLow<Register>();
1469
1470 if (kind == HInstruction::kAdd) {
1471 __ adds(out_lo, first_lo, second_lo);
1472 __ adc(out_hi, first_hi, second_hi);
1473 } else if (kind == HInstruction::kSub) {
1474 __ subs(out_lo, first_lo, second_lo);
1475 __ sbc(out_hi, first_hi, second_hi);
1476 } else {
1477 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1478 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1479 }
1480}
1481
1482static ShifterOperand GetShifterOperand(Register rm, Shift shift, uint32_t shift_imm) {
1483 return shift_imm == 0 ? ShifterOperand(rm) : ShifterOperand(rm, shift, shift_imm);
1484}
1485
1486static void GenerateLongDataProc(HDataProcWithShifterOp* instruction, CodeGeneratorARM* codegen) {
1487 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1488 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1489
1490 const LocationSummary* const locations = instruction->GetLocations();
1491 const uint32_t shift_value = instruction->GetShiftAmount();
1492 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1493 const Location first = locations->InAt(0);
1494 const Location second = locations->InAt(1);
1495 const Location out = locations->Out();
1496 const Register first_hi = first.AsRegisterPairHigh<Register>();
1497 const Register first_lo = first.AsRegisterPairLow<Register>();
1498 const Register out_hi = out.AsRegisterPairHigh<Register>();
1499 const Register out_lo = out.AsRegisterPairLow<Register>();
1500 const Register second_hi = second.AsRegisterPairHigh<Register>();
1501 const Register second_lo = second.AsRegisterPairLow<Register>();
1502 const Shift shift = ShiftFromOpKind(instruction->GetOpKind());
1503
1504 if (shift_value >= 32) {
1505 if (shift == LSL) {
1506 GenerateDataProcInstruction(kind,
1507 out_hi,
1508 first_hi,
1509 ShifterOperand(second_lo, LSL, shift_value - 32),
1510 codegen);
1511 GenerateDataProcInstruction(kind,
1512 out_lo,
1513 first_lo,
1514 ShifterOperand(0),
1515 codegen);
1516 } else if (shift == ASR) {
1517 GenerateDataProc(kind,
1518 out,
1519 first,
1520 GetShifterOperand(second_hi, ASR, shift_value - 32),
1521 ShifterOperand(second_hi, ASR, 31),
1522 codegen);
1523 } else {
1524 DCHECK_EQ(shift, LSR);
1525 GenerateDataProc(kind,
1526 out,
1527 first,
1528 GetShifterOperand(second_hi, LSR, shift_value - 32),
1529 ShifterOperand(0),
1530 codegen);
1531 }
1532 } else {
1533 DCHECK_GT(shift_value, 1U);
1534 DCHECK_LT(shift_value, 32U);
1535
1536 if (shift == LSL) {
1537 // We are not doing this for HInstruction::kAdd because the output will require
1538 // Location::kOutputOverlap; not applicable to other cases.
1539 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1540 GenerateDataProcInstruction(kind,
1541 out_hi,
1542 first_hi,
1543 ShifterOperand(second_hi, LSL, shift_value),
1544 codegen);
1545 GenerateDataProcInstruction(kind,
1546 out_hi,
1547 out_hi,
1548 ShifterOperand(second_lo, LSR, 32 - shift_value),
1549 codegen);
1550 GenerateDataProcInstruction(kind,
1551 out_lo,
1552 first_lo,
1553 ShifterOperand(second_lo, LSL, shift_value),
1554 codegen);
1555 } else {
1556 __ Lsl(IP, second_hi, shift_value);
1557 __ orr(IP, IP, ShifterOperand(second_lo, LSR, 32 - shift_value));
1558 GenerateDataProc(kind,
1559 out,
1560 first,
1561 ShifterOperand(second_lo, LSL, shift_value),
1562 ShifterOperand(IP),
1563 codegen);
1564 }
1565 } else {
1566 DCHECK(shift == ASR || shift == LSR);
1567
1568 // We are not doing this for HInstruction::kAdd because the output will require
1569 // Location::kOutputOverlap; not applicable to other cases.
1570 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1571 GenerateDataProcInstruction(kind,
1572 out_lo,
1573 first_lo,
1574 ShifterOperand(second_lo, LSR, shift_value),
1575 codegen);
1576 GenerateDataProcInstruction(kind,
1577 out_lo,
1578 out_lo,
1579 ShifterOperand(second_hi, LSL, 32 - shift_value),
1580 codegen);
1581 GenerateDataProcInstruction(kind,
1582 out_hi,
1583 first_hi,
1584 ShifterOperand(second_hi, shift, shift_value),
1585 codegen);
1586 } else {
1587 __ Lsr(IP, second_lo, shift_value);
1588 __ orr(IP, IP, ShifterOperand(second_hi, LSL, 32 - shift_value));
1589 GenerateDataProc(kind,
1590 out,
1591 first,
1592 ShifterOperand(IP),
1593 ShifterOperand(second_hi, shift, shift_value),
1594 codegen);
1595 }
1596 }
1597 }
1598}
1599
Donghui Bai426b49c2016-11-08 14:55:38 +08001600static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARM* codegen) {
1601 Primitive::Type type = instruction->InputAt(0)->GetType();
1602 Location lhs_loc = instruction->GetLocations()->InAt(0);
1603 Location rhs_loc = instruction->GetLocations()->InAt(1);
1604 if (rhs_loc.IsConstant()) {
1605 // 0.0 is the only immediate that can be encoded directly in
1606 // a VCMP instruction.
1607 //
1608 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1609 // specify that in a floating-point comparison, positive zero
1610 // and negative zero are considered equal, so we can use the
1611 // literal 0.0 for both cases here.
1612 //
1613 // Note however that some methods (Float.equal, Float.compare,
1614 // Float.compareTo, Double.equal, Double.compare,
1615 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1616 // StrictMath.min) consider 0.0 to be (strictly) greater than
1617 // -0.0. So if we ever translate calls to these methods into a
1618 // HCompare instruction, we must handle the -0.0 case with
1619 // care here.
1620 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1621 if (type == Primitive::kPrimFloat) {
1622 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1623 } else {
1624 DCHECK_EQ(type, Primitive::kPrimDouble);
1625 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1626 }
1627 } else {
1628 if (type == Primitive::kPrimFloat) {
1629 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1630 } else {
1631 DCHECK_EQ(type, Primitive::kPrimDouble);
1632 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1633 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1634 }
1635 }
1636}
1637
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001638static std::pair<Condition, Condition> GenerateLongTestConstant(HCondition* condition,
1639 bool invert,
1640 CodeGeneratorARM* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001641 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1642
1643 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001644 IfCondition cond = condition->GetCondition();
1645 IfCondition opposite = condition->GetOppositeCondition();
1646
1647 if (invert) {
1648 std::swap(cond, opposite);
1649 }
1650
1651 std::pair<Condition, Condition> ret;
Donghui Bai426b49c2016-11-08 14:55:38 +08001652 const Location left = locations->InAt(0);
1653 const Location right = locations->InAt(1);
1654
1655 DCHECK(right.IsConstant());
1656
1657 const Register left_high = left.AsRegisterPairHigh<Register>();
1658 const Register left_low = left.AsRegisterPairLow<Register>();
1659 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
1660
1661 switch (cond) {
1662 case kCondEQ:
1663 case kCondNE:
1664 case kCondB:
1665 case kCondBE:
1666 case kCondA:
1667 case kCondAE:
1668 __ CmpConstant(left_high, High32Bits(value));
1669 __ it(EQ);
1670 __ cmp(left_low, ShifterOperand(Low32Bits(value)), EQ);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001671 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001672 break;
1673 case kCondLE:
1674 case kCondGT:
1675 // Trivially true or false.
1676 if (value == std::numeric_limits<int64_t>::max()) {
1677 __ cmp(left_low, ShifterOperand(left_low));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001678 ret = cond == kCondLE ? std::make_pair(EQ, NE) : std::make_pair(NE, EQ);
Donghui Bai426b49c2016-11-08 14:55:38 +08001679 break;
1680 }
1681
1682 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001683 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001684 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001685 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001686 } else {
1687 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001688 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001689 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001690 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001691 }
1692
1693 value++;
1694 FALLTHROUGH_INTENDED;
1695 case kCondGE:
1696 case kCondLT:
1697 __ CmpConstant(left_low, Low32Bits(value));
1698 __ sbcs(IP, left_high, ShifterOperand(High32Bits(value)));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001699 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001700 break;
1701 default:
1702 LOG(FATAL) << "Unreachable";
1703 UNREACHABLE();
1704 }
1705
1706 return ret;
1707}
1708
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001709static std::pair<Condition, Condition> GenerateLongTest(HCondition* condition,
1710 bool invert,
1711 CodeGeneratorARM* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001712 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1713
1714 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001715 IfCondition cond = condition->GetCondition();
1716 IfCondition opposite = condition->GetOppositeCondition();
1717
1718 if (invert) {
1719 std::swap(cond, opposite);
1720 }
1721
1722 std::pair<Condition, Condition> ret;
Donghui Bai426b49c2016-11-08 14:55:38 +08001723 Location left = locations->InAt(0);
1724 Location right = locations->InAt(1);
1725
1726 DCHECK(right.IsRegisterPair());
1727
1728 switch (cond) {
1729 case kCondEQ:
1730 case kCondNE:
1731 case kCondB:
1732 case kCondBE:
1733 case kCondA:
1734 case kCondAE:
1735 __ cmp(left.AsRegisterPairHigh<Register>(),
1736 ShifterOperand(right.AsRegisterPairHigh<Register>()));
1737 __ it(EQ);
1738 __ cmp(left.AsRegisterPairLow<Register>(),
1739 ShifterOperand(right.AsRegisterPairLow<Register>()),
1740 EQ);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001741 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001742 break;
1743 case kCondLE:
1744 case kCondGT:
1745 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001746 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001747 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001748 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001749 } else {
1750 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001751 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001752 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001753 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001754 }
1755
1756 std::swap(left, right);
1757 FALLTHROUGH_INTENDED;
1758 case kCondGE:
1759 case kCondLT:
1760 __ cmp(left.AsRegisterPairLow<Register>(),
1761 ShifterOperand(right.AsRegisterPairLow<Register>()));
1762 __ sbcs(IP,
1763 left.AsRegisterPairHigh<Register>(),
1764 ShifterOperand(right.AsRegisterPairHigh<Register>()));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001765 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001766 break;
1767 default:
1768 LOG(FATAL) << "Unreachable";
1769 UNREACHABLE();
1770 }
1771
1772 return ret;
1773}
1774
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001775static std::pair<Condition, Condition> GenerateTest(HCondition* condition,
1776 bool invert,
1777 CodeGeneratorARM* codegen) {
1778 const LocationSummary* const locations = condition->GetLocations();
1779 const Primitive::Type type = condition->GetLeft()->GetType();
1780 IfCondition cond = condition->GetCondition();
1781 IfCondition opposite = condition->GetOppositeCondition();
1782 std::pair<Condition, Condition> ret;
1783 const Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08001784
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001785 if (invert) {
1786 std::swap(cond, opposite);
1787 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001788
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001789 if (type == Primitive::kPrimLong) {
1790 ret = locations->InAt(1).IsConstant()
1791 ? GenerateLongTestConstant(condition, invert, codegen)
1792 : GenerateLongTest(condition, invert, codegen);
1793 } else if (Primitive::IsFloatingPointType(type)) {
1794 GenerateVcmp(condition, codegen);
1795 __ vmstat();
1796 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1797 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001798 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001799 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08001800
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001801 const Register left = locations->InAt(0).AsRegister<Register>();
1802
1803 if (right.IsRegister()) {
1804 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001805 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001806 DCHECK(right.IsConstant());
1807 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001808 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001809
1810 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001811 }
1812
1813 return ret;
1814}
1815
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001816static bool CanGenerateTest(HCondition* condition, ArmAssembler* assembler) {
1817 if (condition->GetLeft()->GetType() == Primitive::kPrimLong) {
1818 const LocationSummary* const locations = condition->GetLocations();
1819 const IfCondition c = condition->GetCondition();
Donghui Bai426b49c2016-11-08 14:55:38 +08001820
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001821 if (locations->InAt(1).IsConstant()) {
1822 const int64_t value = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
1823 ShifterOperand so;
Donghui Bai426b49c2016-11-08 14:55:38 +08001824
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001825 if (c < kCondLT || c > kCondGE) {
1826 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1827 // we check that the least significant half of the first input to be compared
1828 // is in a low register (the other half is read outside an IT block), and
1829 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
1830 // encoding can be used.
1831 if (!ArmAssembler::IsLowRegister(locations->InAt(0).AsRegisterPairLow<Register>()) ||
1832 !IsUint<8>(Low32Bits(value))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001833 return false;
1834 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001835 } else if (c == kCondLE || c == kCondGT) {
1836 if (value < std::numeric_limits<int64_t>::max() &&
1837 !assembler->ShifterOperandCanHold(kNoRegister,
1838 kNoRegister,
1839 SBC,
1840 High32Bits(value + 1),
1841 kCcSet,
1842 &so)) {
1843 return false;
1844 }
1845 } else if (!assembler->ShifterOperandCanHold(kNoRegister,
1846 kNoRegister,
1847 SBC,
1848 High32Bits(value),
1849 kCcSet,
1850 &so)) {
1851 return false;
Donghui Bai426b49c2016-11-08 14:55:38 +08001852 }
1853 }
1854 }
1855
1856 return true;
1857}
1858
1859static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
1860 const Primitive::Type type = constant->GetType();
1861 bool ret = false;
1862
1863 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
1864
1865 if (type == Primitive::kPrimLong) {
1866 const uint64_t value = constant->AsLongConstant()->GetValueAsUint64();
1867
1868 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
1869 } else {
1870 ret = IsUint<8>(CodeGenerator::GetInt32ValueOf(constant));
1871 }
1872
1873 return ret;
1874}
1875
1876static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
1877 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
1878
1879 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
1880 return Location::ConstantLocation(constant->AsConstant());
1881 }
1882
1883 return Location::RequiresRegister();
1884}
1885
1886static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
1887 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1888 // we check that we are not dealing with floating-point output (there is no
1889 // 16-bit VMOV encoding).
1890 if (!out.IsRegister() && !out.IsRegisterPair()) {
1891 return false;
1892 }
1893
1894 // For constants, we also check that the output is in one or two low registers,
1895 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
1896 // MOV encoding can be used.
1897 if (src.IsConstant()) {
1898 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
1899 return false;
1900 }
1901
1902 if (out.IsRegister()) {
1903 if (!ArmAssembler::IsLowRegister(out.AsRegister<Register>())) {
1904 return false;
1905 }
1906 } else {
1907 DCHECK(out.IsRegisterPair());
1908
1909 if (!ArmAssembler::IsLowRegister(out.AsRegisterPairHigh<Register>())) {
1910 return false;
1911 }
1912 }
1913 }
1914
1915 return true;
1916}
1917
Anton Kirilov74234da2017-01-13 14:42:47 +00001918#undef __
1919// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1920#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
1921
Donghui Bai426b49c2016-11-08 14:55:38 +08001922Label* CodeGeneratorARM::GetFinalLabel(HInstruction* instruction, Label* final_label) {
1923 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00001924 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08001925
1926 const HBasicBlock* const block = instruction->GetBlock();
1927 const HLoopInformation* const info = block->GetLoopInformation();
1928 HInstruction* const next = instruction->GetNext();
1929
1930 // Avoid a branch to a branch.
1931 if (next->IsGoto() && (info == nullptr ||
1932 !info->IsBackEdge(*block) ||
1933 !info->HasSuspendCheck())) {
1934 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
1935 }
1936
1937 return final_label;
1938}
1939
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001940void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001941 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001942}
1943
1944void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01001945 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001946}
1947
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001948size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1949 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
1950 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001951}
1952
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001953size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1954 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
1955 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001956}
1957
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001958size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1959 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1960 return kArmWordSize;
1961}
1962
1963size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1964 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
1965 return kArmWordSize;
1966}
1967
Calin Juravle34166012014-12-19 17:22:29 +00001968CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00001969 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01001970 const CompilerOptions& compiler_options,
1971 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00001972 : CodeGenerator(graph,
1973 kNumberOfCoreRegisters,
1974 kNumberOfSRegisters,
1975 kNumberOfRegisterPairs,
1976 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1977 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00001978 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1979 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001980 compiler_options,
1981 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001982 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001983 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01001984 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01001985 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001986 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001987 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001988 uint32_literals_(std::less<uint32_t>(),
1989 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001990 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1991 boot_image_string_patches_(StringReferenceValueComparator(),
1992 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1993 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001994 boot_image_type_patches_(TypeReferenceValueComparator(),
1995 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1996 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001997 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01001998 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001999 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00002000 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2001 jit_class_patches_(TypeReferenceValueComparator(),
2002 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07002003 // Always save the LR register to mimic Quick.
2004 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01002005}
2006
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002007void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
2008 // Ensure that we fix up branches and literal loads and emit the literal pool.
2009 __ FinalizeCode();
2010
2011 // Adjust native pc offsets in stack maps.
2012 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08002013 uint32_t old_position =
2014 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002015 uint32_t new_position = __ GetAdjustedPosition(old_position);
2016 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
2017 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01002018 // Adjust pc offsets for the disassembly information.
2019 if (disasm_info_ != nullptr) {
2020 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
2021 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
2022 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
2023 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
2024 it.second.start = __ GetAdjustedPosition(it.second.start);
2025 it.second.end = __ GetAdjustedPosition(it.second.end);
2026 }
2027 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
2028 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
2029 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
2030 }
2031 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002032
2033 CodeGenerator::Finalize(allocator);
2034}
2035
David Brazdil58282f42016-01-14 12:45:10 +00002036void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002037 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002038 blocked_core_registers_[SP] = true;
2039 blocked_core_registers_[LR] = true;
2040 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002041
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002042 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002043 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002044
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002045 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002046 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002047
David Brazdil58282f42016-01-14 12:45:10 +00002048 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01002049 // Stubs do not save callee-save floating point registers. If the graph
2050 // is debuggable, we need to deal with these registers differently. For
2051 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002052 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
2053 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
2054 }
2055 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002056}
2057
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002058InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002059 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002060 assembler_(codegen->GetAssembler()),
2061 codegen_(codegen) {}
2062
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002063void CodeGeneratorARM::ComputeSpillMask() {
2064 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2065 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00002066 // There is no easy instruction to restore just the PC on thumb2. We spill and
2067 // restore another arbitrary register.
2068 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002069 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2070 // We use vpush and vpop for saving and restoring floating point registers, which take
2071 // a SRegister and the number of registers to save/restore after that SRegister. We
2072 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2073 // but in the range.
2074 if (fpu_spill_mask_ != 0) {
2075 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2076 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2077 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2078 fpu_spill_mask_ |= (1 << i);
2079 }
2080 }
2081}
2082
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002083static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002084 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002085}
2086
2087static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002088 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002089}
2090
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002091void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00002092 bool skip_overflow_check =
2093 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002094 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002095 __ Bind(&frame_entry_label_);
2096
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002097 if (HasEmptyFrame()) {
2098 return;
2099 }
2100
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002101 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002102 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
2103 __ LoadFromOffset(kLoadWord, IP, IP, 0);
2104 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002105 }
2106
Andreas Gampe501fd632015-09-10 16:11:06 -07002107 __ PushList(core_spill_mask_);
2108 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2109 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002110 if (fpu_spill_mask_ != 0) {
2111 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2112 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002113 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01002114 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002115 }
Mingyao Yang063fc772016-08-02 11:02:54 -07002116
2117 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2118 // Initialize should_deoptimize flag to 0.
2119 __ mov(IP, ShifterOperand(0));
2120 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
2121 }
2122
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002123 int adjust = GetFrameSize() - FrameEntrySpillSize();
2124 __ AddConstant(SP, -adjust);
2125 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002126
2127 // Save the current method if we need it. Note that we do not
2128 // do this in HCurrentMethod, as the instruction might have been removed
2129 // in the SSA graph.
2130 if (RequiresCurrentMethod()) {
2131 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
2132 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002133}
2134
2135void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002136 if (HasEmptyFrame()) {
2137 __ bx(LR);
2138 return;
2139 }
David Srbeckyc34dc932015-04-12 09:27:43 +01002140 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002141 int adjust = GetFrameSize() - FrameEntrySpillSize();
2142 __ AddConstant(SP, adjust);
2143 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002144 if (fpu_spill_mask_ != 0) {
2145 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2146 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07002147 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002148 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002149 }
Andreas Gampe501fd632015-09-10 16:11:06 -07002150 // Pop LR into PC to return.
2151 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
2152 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
2153 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01002154 __ cfi().RestoreState();
2155 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002156}
2157
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002158void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07002159 Label* label = GetLabelOf(block);
2160 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002161}
2162
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002163Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002164 switch (type) {
2165 case Primitive::kPrimBoolean:
2166 case Primitive::kPrimByte:
2167 case Primitive::kPrimChar:
2168 case Primitive::kPrimShort:
2169 case Primitive::kPrimInt:
2170 case Primitive::kPrimNot: {
2171 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002172 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002173 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002174 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002175 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002176 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002177 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002178 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002179
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002180 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002181 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002182 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002183 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002184 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002185 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002186 if (calling_convention.GetRegisterAt(index) == R1) {
2187 // Skip R1, and use R2_R3 instead.
2188 gp_index_++;
2189 index++;
2190 }
2191 }
2192 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2193 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002194 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01002195
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002196 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002197 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002198 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002199 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2200 }
2201 }
2202
2203 case Primitive::kPrimFloat: {
2204 uint32_t stack_index = stack_index_++;
2205 if (float_index_ % 2 == 0) {
2206 float_index_ = std::max(double_index_, float_index_);
2207 }
2208 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2209 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
2210 } else {
2211 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2212 }
2213 }
2214
2215 case Primitive::kPrimDouble: {
2216 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2217 uint32_t stack_index = stack_index_;
2218 stack_index_ += 2;
2219 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2220 uint32_t index = double_index_;
2221 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002222 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002223 calling_convention.GetFpuRegisterAt(index),
2224 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002225 DCHECK(ExpectedPairLayout(result));
2226 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002227 } else {
2228 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002229 }
2230 }
2231
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002232 case Primitive::kPrimVoid:
2233 LOG(FATAL) << "Unexpected parameter type " << type;
2234 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002235 }
Roland Levillain3b359c72015-11-17 19:35:12 +00002236 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002237}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002238
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002239Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002240 switch (type) {
2241 case Primitive::kPrimBoolean:
2242 case Primitive::kPrimByte:
2243 case Primitive::kPrimChar:
2244 case Primitive::kPrimShort:
2245 case Primitive::kPrimInt:
2246 case Primitive::kPrimNot: {
2247 return Location::RegisterLocation(R0);
2248 }
2249
2250 case Primitive::kPrimFloat: {
2251 return Location::FpuRegisterLocation(S0);
2252 }
2253
2254 case Primitive::kPrimLong: {
2255 return Location::RegisterPairLocation(R0, R1);
2256 }
2257
2258 case Primitive::kPrimDouble: {
2259 return Location::FpuRegisterPairLocation(S0, S1);
2260 }
2261
2262 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00002263 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002264 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002265
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002266 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002267}
2268
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002269Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
2270 return Location::RegisterLocation(kMethodRegisterArgument);
2271}
2272
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002273void CodeGeneratorARM::Move32(Location destination, Location source) {
2274 if (source.Equals(destination)) {
2275 return;
2276 }
2277 if (destination.IsRegister()) {
2278 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002279 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002280 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002281 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002282 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002283 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002284 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002285 } else if (destination.IsFpuRegister()) {
2286 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002287 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002288 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002289 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002290 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002291 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002292 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002293 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002294 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002295 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002296 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002297 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002298 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002299 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002300 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002301 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
2302 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002303 }
2304 }
2305}
2306
2307void CodeGeneratorARM::Move64(Location destination, Location source) {
2308 if (source.Equals(destination)) {
2309 return;
2310 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002311 if (destination.IsRegisterPair()) {
2312 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002313 EmitParallelMoves(
2314 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
2315 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002316 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002317 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002318 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
2319 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002320 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002321 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002322 } else if (source.IsFpuRegisterPair()) {
2323 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
2324 destination.AsRegisterPairHigh<Register>(),
2325 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002326 } else {
2327 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002328 DCHECK(ExpectedPairLayout(destination));
2329 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
2330 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002331 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002332 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002333 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002334 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2335 SP,
2336 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01002337 } else if (source.IsRegisterPair()) {
2338 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2339 source.AsRegisterPairLow<Register>(),
2340 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002341 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002342 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002343 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002344 } else {
2345 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002346 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002347 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002348 if (source.AsRegisterPairLow<Register>() == R1) {
2349 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002350 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
2351 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002352 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002353 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002354 SP, destination.GetStackIndex());
2355 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002356 } else if (source.IsFpuRegisterPair()) {
2357 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
2358 SP,
2359 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002360 } else {
2361 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002362 EmitParallelMoves(
2363 Location::StackSlot(source.GetStackIndex()),
2364 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002365 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002366 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002367 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
2368 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002369 }
2370 }
2371}
2372
Calin Juravle175dc732015-08-25 15:42:32 +01002373void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
2374 DCHECK(location.IsRegister());
2375 __ LoadImmediate(location.AsRegister<Register>(), value);
2376}
2377
Calin Juravlee460d1d2015-09-29 04:52:17 +01002378void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002379 HParallelMove move(GetGraph()->GetArena());
2380 move.AddMove(src, dst, dst_type, nullptr);
2381 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002382}
2383
2384void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
2385 if (location.IsRegister()) {
2386 locations->AddTemp(location);
2387 } else if (location.IsRegisterPair()) {
2388 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
2389 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
2390 } else {
2391 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2392 }
2393}
2394
Calin Juravle175dc732015-08-25 15:42:32 +01002395void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
2396 HInstruction* instruction,
2397 uint32_t dex_pc,
2398 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002399 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002400 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002401 if (EntrypointRequiresStackMap(entrypoint)) {
2402 RecordPcInfo(instruction, dex_pc, slow_path);
2403 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002404}
2405
Roland Levillaindec8f632016-07-22 17:10:06 +01002406void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2407 HInstruction* instruction,
2408 SlowPathCode* slow_path) {
2409 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002410 GenerateInvokeRuntime(entry_point_offset);
2411}
2412
2413void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01002414 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2415 __ blx(LR);
2416}
2417
David Brazdilfc6a86a2015-06-26 10:33:45 +00002418void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002419 DCHECK(!successor->IsExitBlock());
2420
2421 HBasicBlock* block = got->GetBlock();
2422 HInstruction* previous = got->GetPrevious();
2423
2424 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00002425 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002426 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2427 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2428 return;
2429 }
2430
2431 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2432 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2433 }
2434 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002435 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002436 }
2437}
2438
David Brazdilfc6a86a2015-06-26 10:33:45 +00002439void LocationsBuilderARM::VisitGoto(HGoto* got) {
2440 got->SetLocations(nullptr);
2441}
2442
2443void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
2444 HandleGoto(got, got->GetSuccessor());
2445}
2446
2447void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2448 try_boundary->SetLocations(nullptr);
2449}
2450
2451void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2452 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2453 if (!successor->IsExitBlock()) {
2454 HandleGoto(try_boundary, successor);
2455 }
2456}
2457
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002458void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002459 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002460}
2461
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002462void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002463}
2464
Roland Levillain4fa13f62015-07-06 18:11:54 +01002465void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond,
2466 Label* true_label,
2467 Label* false_label) {
2468 LocationSummary* locations = cond->GetLocations();
2469 Location left = locations->InAt(0);
2470 Location right = locations->InAt(1);
2471 IfCondition if_cond = cond->GetCondition();
2472
2473 Register left_high = left.AsRegisterPairHigh<Register>();
2474 Register left_low = left.AsRegisterPairLow<Register>();
2475 IfCondition true_high_cond = if_cond;
2476 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07002477 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
Roland Levillain4fa13f62015-07-06 18:11:54 +01002478
2479 // Set the conditions for the test, remembering that == needs to be
2480 // decided using the low words.
2481 switch (if_cond) {
2482 case kCondEQ:
2483 case kCondNE:
2484 // Nothing to do.
2485 break;
2486 case kCondLT:
2487 false_high_cond = kCondGT;
2488 break;
2489 case kCondLE:
2490 true_high_cond = kCondLT;
2491 break;
2492 case kCondGT:
2493 false_high_cond = kCondLT;
2494 break;
2495 case kCondGE:
2496 true_high_cond = kCondGT;
2497 break;
Aart Bike9f37602015-10-09 11:15:55 -07002498 case kCondB:
2499 false_high_cond = kCondA;
2500 break;
2501 case kCondBE:
2502 true_high_cond = kCondB;
2503 break;
2504 case kCondA:
2505 false_high_cond = kCondB;
2506 break;
2507 case kCondAE:
2508 true_high_cond = kCondA;
2509 break;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002510 }
2511 if (right.IsConstant()) {
2512 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
2513 int32_t val_low = Low32Bits(value);
2514 int32_t val_high = High32Bits(value);
2515
Vladimir Markoac6ac102015-12-17 12:14:00 +00002516 __ CmpConstant(left_high, val_high);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002517 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002518 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002519 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002520 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002521 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002522 __ b(true_label, ARMCondition(true_high_cond));
2523 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002524 }
2525 // Must be equal high, so compare the lows.
Vladimir Markoac6ac102015-12-17 12:14:00 +00002526 __ CmpConstant(left_low, val_low);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002527 } else {
2528 Register right_high = right.AsRegisterPairHigh<Register>();
2529 Register right_low = right.AsRegisterPairLow<Register>();
2530
2531 __ cmp(left_high, ShifterOperand(right_high));
2532 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07002533 __ b(true_label, ARMCondition(true_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002534 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07002535 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002536 } else {
Aart Bike9f37602015-10-09 11:15:55 -07002537 __ b(true_label, ARMCondition(true_high_cond));
2538 __ b(false_label, ARMCondition(false_high_cond));
Roland Levillain4fa13f62015-07-06 18:11:54 +01002539 }
2540 // Must be equal high, so compare the lows.
2541 __ cmp(left_low, ShifterOperand(right_low));
2542 }
2543 // The last comparison might be unsigned.
Aart Bike9f37602015-10-09 11:15:55 -07002544 // TODO: optimize cases where this is always true/false
Roland Levillain4fa13f62015-07-06 18:11:54 +01002545 __ b(true_label, final_condition);
2546}
2547
David Brazdil0debae72015-11-12 18:37:00 +00002548void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
2549 Label* true_target_in,
2550 Label* false_target_in) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002551 if (CanGenerateTest(condition, codegen_->GetAssembler())) {
2552 Label* non_fallthrough_target;
2553 bool invert;
2554
2555 if (true_target_in == nullptr) {
2556 DCHECK(false_target_in != nullptr);
2557 non_fallthrough_target = false_target_in;
2558 invert = true;
2559 } else {
2560 non_fallthrough_target = true_target_in;
2561 invert = false;
2562 }
2563
2564 const auto cond = GenerateTest(condition, invert, codegen_);
2565
2566 __ b(non_fallthrough_target, cond.first);
2567
2568 if (false_target_in != nullptr && false_target_in != non_fallthrough_target) {
2569 __ b(false_target_in);
2570 }
2571
2572 return;
2573 }
2574
David Brazdil0debae72015-11-12 18:37:00 +00002575 // Generated branching requires both targets to be explicit. If either of the
2576 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2577 Label fallthrough_target;
2578 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
2579 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
2580
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002581 DCHECK_EQ(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
2582 GenerateLongComparesAndJumps(condition, true_target, false_target);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002583
David Brazdil0debae72015-11-12 18:37:00 +00002584 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002585 __ b(false_target);
2586 }
David Brazdil0debae72015-11-12 18:37:00 +00002587
2588 if (fallthrough_target.IsLinked()) {
2589 __ Bind(&fallthrough_target);
2590 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002591}
2592
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002593void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002594 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002595 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002596 Label* false_target) {
2597 HInstruction* cond = instruction->InputAt(condition_input_index);
2598
2599 if (true_target == nullptr && false_target == nullptr) {
2600 // Nothing to do. The code always falls through.
2601 return;
2602 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002603 // Constant condition, statically compared against "true" (integer value 1).
2604 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002605 if (true_target != nullptr) {
2606 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002607 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002608 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002609 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002610 if (false_target != nullptr) {
2611 __ b(false_target);
2612 }
2613 }
2614 return;
2615 }
2616
2617 // The following code generates these patterns:
2618 // (1) true_target == nullptr && false_target != nullptr
2619 // - opposite condition true => branch to false_target
2620 // (2) true_target != nullptr && false_target == nullptr
2621 // - condition true => branch to true_target
2622 // (3) true_target != nullptr && false_target != nullptr
2623 // - condition true => branch to true_target
2624 // - branch to false_target
2625 if (IsBooleanValueOrMaterializedCondition(cond)) {
2626 // Condition has been materialized, compare the output to 0.
2627 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2628 DCHECK(cond_val.IsRegister());
2629 if (true_target == nullptr) {
2630 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
2631 } else {
2632 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002633 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002634 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002635 // Condition has not been materialized. Use its inputs as the comparison and
2636 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002637 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002638
2639 // If this is a long or FP comparison that has been folded into
2640 // the HCondition, generate the comparison directly.
2641 Primitive::Type type = condition->InputAt(0)->GetType();
2642 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2643 GenerateCompareTestAndBranch(condition, true_target, false_target);
2644 return;
2645 }
2646
Donghui Bai426b49c2016-11-08 14:55:38 +08002647 Label* non_fallthrough_target;
2648 Condition arm_cond;
David Brazdil0debae72015-11-12 18:37:00 +00002649 LocationSummary* locations = cond->GetLocations();
2650 DCHECK(locations->InAt(0).IsRegister());
2651 Register left = locations->InAt(0).AsRegister<Register>();
2652 Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08002653
David Brazdil0debae72015-11-12 18:37:00 +00002654 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002655 arm_cond = ARMCondition(condition->GetOppositeCondition());
2656 non_fallthrough_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002657 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002658 arm_cond = ARMCondition(condition->GetCondition());
2659 non_fallthrough_target = true_target;
2660 }
2661
2662 if (right.IsConstant() && (arm_cond == NE || arm_cond == EQ) &&
2663 CodeGenerator::GetInt32ValueOf(right.GetConstant()) == 0) {
2664 if (arm_cond == EQ) {
2665 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2666 } else {
2667 DCHECK_EQ(arm_cond, NE);
2668 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2669 }
2670 } else {
2671 if (right.IsRegister()) {
2672 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
2673 } else {
2674 DCHECK(right.IsConstant());
2675 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2676 }
2677
2678 __ b(non_fallthrough_target, arm_cond);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002679 }
Dave Allison20dfc792014-06-16 20:44:29 -07002680 }
David Brazdil0debae72015-11-12 18:37:00 +00002681
2682 // If neither branch falls through (case 3), the conditional branch to `true_target`
2683 // was already emitted (case 2) and we need to emit a jump to `false_target`.
2684 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002685 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002686 }
2687}
2688
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002689void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002690 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
2691 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002692 locations->SetInAt(0, Location::RequiresRegister());
2693 }
2694}
2695
2696void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00002697 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
2698 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
2699 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
2700 nullptr : codegen_->GetLabelOf(true_successor);
2701 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
2702 nullptr : codegen_->GetLabelOf(false_successor);
2703 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002704}
2705
2706void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
2707 LocationSummary* locations = new (GetGraph()->GetArena())
2708 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01002709 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00002710 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002711 locations->SetInAt(0, Location::RequiresRegister());
2712 }
2713}
2714
2715void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01002716 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00002717 GenerateTestAndBranch(deoptimize,
2718 /* condition_input_index */ 0,
2719 slow_path->GetEntryLabel(),
2720 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002721}
Dave Allison20dfc792014-06-16 20:44:29 -07002722
Mingyao Yang063fc772016-08-02 11:02:54 -07002723void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2724 LocationSummary* locations = new (GetGraph()->GetArena())
2725 LocationSummary(flag, LocationSummary::kNoCall);
2726 locations->SetOut(Location::RequiresRegister());
2727}
2728
2729void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
2730 __ LoadFromOffset(kLoadWord,
2731 flag->GetLocations()->Out().AsRegister<Register>(),
2732 SP,
2733 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
2734}
2735
David Brazdil74eb1b22015-12-14 11:44:01 +00002736void LocationsBuilderARM::VisitSelect(HSelect* select) {
2737 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08002738 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
2739
2740 if (is_floating_point) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002741 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002742 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002743 } else {
2744 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08002745 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00002746 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002747
David Brazdil74eb1b22015-12-14 11:44:01 +00002748 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002749 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
2750 // The code generator handles overlap with the values, but not with the condition.
2751 locations->SetOut(Location::SameAsFirstInput());
2752 } else if (is_floating_point) {
2753 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2754 } else {
2755 if (!locations->InAt(1).IsConstant()) {
2756 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
2757 }
2758
2759 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00002760 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002761}
2762
2763void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002764 HInstruction* const condition = select->GetCondition();
2765 const LocationSummary* const locations = select->GetLocations();
2766 const Primitive::Type type = select->GetType();
2767 const Location first = locations->InAt(0);
2768 const Location out = locations->Out();
2769 const Location second = locations->InAt(1);
2770 Location src;
2771
2772 if (condition->IsIntConstant()) {
2773 if (condition->AsIntConstant()->IsFalse()) {
2774 src = first;
2775 } else {
2776 src = second;
2777 }
2778
2779 codegen_->MoveLocation(out, src, type);
2780 return;
2781 }
2782
2783 if (!Primitive::IsFloatingPointType(type) &&
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002784 (IsBooleanValueOrMaterializedCondition(condition) ||
2785 CanGenerateTest(condition->AsCondition(), codegen_->GetAssembler()))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002786 bool invert = false;
2787
2788 if (out.Equals(second)) {
2789 src = first;
2790 invert = true;
2791 } else if (out.Equals(first)) {
2792 src = second;
2793 } else if (second.IsConstant()) {
2794 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
2795 src = second;
2796 } else if (first.IsConstant()) {
2797 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
2798 src = first;
2799 invert = true;
2800 } else {
2801 src = second;
2802 }
2803
2804 if (CanGenerateConditionalMove(out, src)) {
2805 if (!out.Equals(first) && !out.Equals(second)) {
2806 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
2807 }
2808
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002809 std::pair<Condition, Condition> cond;
2810
2811 if (IsBooleanValueOrMaterializedCondition(condition)) {
2812 __ CmpConstant(locations->InAt(2).AsRegister<Register>(), 0);
2813 cond = invert ? std::make_pair(EQ, NE) : std::make_pair(NE, EQ);
2814 } else {
2815 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
2816 }
Donghui Bai426b49c2016-11-08 14:55:38 +08002817
2818 if (out.IsRegister()) {
2819 ShifterOperand operand;
2820
2821 if (src.IsConstant()) {
2822 operand = ShifterOperand(CodeGenerator::GetInt32ValueOf(src.GetConstant()));
2823 } else {
2824 DCHECK(src.IsRegister());
2825 operand = ShifterOperand(src.AsRegister<Register>());
2826 }
2827
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002828 __ it(cond.first);
2829 __ mov(out.AsRegister<Register>(), operand, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08002830 } else {
2831 DCHECK(out.IsRegisterPair());
2832
2833 ShifterOperand operand_high;
2834 ShifterOperand operand_low;
2835
2836 if (src.IsConstant()) {
2837 const int64_t value = src.GetConstant()->AsLongConstant()->GetValue();
2838
2839 operand_high = ShifterOperand(High32Bits(value));
2840 operand_low = ShifterOperand(Low32Bits(value));
2841 } else {
2842 DCHECK(src.IsRegisterPair());
2843 operand_high = ShifterOperand(src.AsRegisterPairHigh<Register>());
2844 operand_low = ShifterOperand(src.AsRegisterPairLow<Register>());
2845 }
2846
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002847 __ it(cond.first);
2848 __ mov(out.AsRegisterPairLow<Register>(), operand_low, cond.first);
2849 __ it(cond.first);
2850 __ mov(out.AsRegisterPairHigh<Register>(), operand_high, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08002851 }
2852
2853 return;
2854 }
2855 }
2856
2857 Label* false_target = nullptr;
2858 Label* true_target = nullptr;
2859 Label select_end;
2860 Label* target = codegen_->GetFinalLabel(select, &select_end);
2861
2862 if (out.Equals(second)) {
2863 true_target = target;
2864 src = first;
2865 } else {
2866 false_target = target;
2867 src = second;
2868
2869 if (!out.Equals(first)) {
2870 codegen_->MoveLocation(out, first, type);
2871 }
2872 }
2873
2874 GenerateTestAndBranch(select, 2, true_target, false_target);
2875 codegen_->MoveLocation(out, src, type);
2876
2877 if (select_end.IsLinked()) {
2878 __ Bind(&select_end);
2879 }
David Brazdil74eb1b22015-12-14 11:44:01 +00002880}
2881
David Srbecky0cf44932015-12-09 14:09:59 +00002882void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
2883 new (GetGraph()->GetArena()) LocationSummary(info);
2884}
2885
David Srbeckyd28f4a02016-03-14 17:14:24 +00002886void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
2887 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00002888}
2889
2890void CodeGeneratorARM::GenerateNop() {
2891 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00002892}
2893
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002894void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002895 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01002896 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002897 // Handle the long/FP comparisons made in instruction simplification.
2898 switch (cond->InputAt(0)->GetType()) {
2899 case Primitive::kPrimLong:
2900 locations->SetInAt(0, Location::RequiresRegister());
2901 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002902 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002903 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002904 }
2905 break;
2906
2907 case Primitive::kPrimFloat:
2908 case Primitive::kPrimDouble:
2909 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01002910 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002911 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002912 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2913 }
2914 break;
2915
2916 default:
2917 locations->SetInAt(0, Location::RequiresRegister());
2918 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00002919 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002920 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2921 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01002922 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002923}
2924
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002925void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00002926 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002927 return;
Dave Allison20dfc792014-06-16 20:44:29 -07002928 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002929
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002930 const Register out = cond->GetLocations()->Out().AsRegister<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01002931
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002932 if (ArmAssembler::IsLowRegister(out) && CanGenerateTest(cond, codegen_->GetAssembler())) {
2933 const auto condition = GenerateTest(cond, false, codegen_);
2934
2935 __ it(condition.first);
2936 __ mov(out, ShifterOperand(1), condition.first);
2937 __ it(condition.second);
2938 __ mov(out, ShifterOperand(0), condition.second);
2939 return;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002940 }
2941
2942 // Convert the jumps into the result.
2943 Label done_label;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002944 Label* const final_label = codegen_->GetFinalLabel(cond, &done_label);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002945
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002946 if (cond->InputAt(0)->GetType() == Primitive::kPrimLong) {
2947 Label true_label, false_label;
Roland Levillain4fa13f62015-07-06 18:11:54 +01002948
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002949 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
2950
2951 // False case: result = 0.
2952 __ Bind(&false_label);
2953 __ LoadImmediate(out, 0);
2954 __ b(final_label);
2955
2956 // True case: result = 1.
2957 __ Bind(&true_label);
2958 __ LoadImmediate(out, 1);
2959 } else {
2960 DCHECK(CanGenerateTest(cond, codegen_->GetAssembler()));
2961
2962 const auto condition = GenerateTest(cond, false, codegen_);
2963
2964 __ mov(out, ShifterOperand(0), AL, kCcKeep);
2965 __ b(final_label, condition.second);
2966 __ LoadImmediate(out, 1);
2967 }
Anton Kirilov6f644202017-02-27 18:29:45 +00002968
2969 if (done_label.IsLinked()) {
2970 __ Bind(&done_label);
2971 }
Dave Allison20dfc792014-06-16 20:44:29 -07002972}
2973
2974void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002975 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002976}
2977
2978void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002979 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002980}
2981
2982void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002983 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002984}
2985
2986void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002987 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002988}
2989
2990void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002991 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002992}
2993
2994void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002995 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07002996}
2997
2998void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00002999 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003000}
3001
3002void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003003 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003004}
3005
3006void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003007 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003008}
3009
3010void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003011 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003012}
3013
3014void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003015 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003016}
3017
3018void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003019 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003020}
3021
Aart Bike9f37602015-10-09 11:15:55 -07003022void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003023 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003024}
3025
3026void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003027 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003028}
3029
3030void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003031 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003032}
3033
3034void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003035 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003036}
3037
3038void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003039 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003040}
3041
3042void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003043 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003044}
3045
3046void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003047 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003048}
3049
3050void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003051 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003052}
3053
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003054void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003055 LocationSummary* locations =
3056 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003057 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003058}
3059
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003060void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01003061 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003062}
3063
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003064void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
3065 LocationSummary* locations =
3066 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3067 locations->SetOut(Location::ConstantLocation(constant));
3068}
3069
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003070void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003071 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003072}
3073
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003074void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003075 LocationSummary* locations =
3076 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003077 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003078}
3079
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003080void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003081 // Will be generated at use site.
3082}
3083
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003084void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
3085 LocationSummary* locations =
3086 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3087 locations->SetOut(Location::ConstantLocation(constant));
3088}
3089
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003090void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003091 // Will be generated at use site.
3092}
3093
3094void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
3095 LocationSummary* locations =
3096 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3097 locations->SetOut(Location::ConstantLocation(constant));
3098}
3099
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003100void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003101 // Will be generated at use site.
3102}
3103
Igor Murashkind01745e2017-04-05 16:40:31 -07003104void LocationsBuilderARM::VisitConstructorFence(HConstructorFence* constructor_fence) {
3105 constructor_fence->SetLocations(nullptr);
3106}
3107
3108void InstructionCodeGeneratorARM::VisitConstructorFence(
3109 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3110 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3111}
3112
Calin Juravle27df7582015-04-17 19:12:31 +01003113void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3114 memory_barrier->SetLocations(nullptr);
3115}
3116
3117void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00003118 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01003119}
3120
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003121void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003122 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003123}
3124
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003125void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003126 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003127}
3128
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003129void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003130 LocationSummary* locations =
3131 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003132 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003133}
3134
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003135void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003136 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003137}
3138
Calin Juravle175dc732015-08-25 15:42:32 +01003139void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3140 // The trampoline uses the same calling convention as dex calling conventions,
3141 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3142 // the method_idx.
3143 HandleInvoke(invoke);
3144}
3145
3146void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3147 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3148}
3149
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003150void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003151 // Explicit clinit checks triggered by static invokes must have been pruned by
3152 // art::PrepareForRegisterAllocation.
3153 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003154
Vladimir Marko68c981f2016-08-26 13:13:33 +01003155 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003156 if (intrinsic.TryDispatch(invoke)) {
Vladimir Markob4536b72015-11-24 13:45:23 +00003157 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
3158 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
3159 }
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003160 return;
3161 }
3162
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003163 HandleInvoke(invoke);
Vladimir Markob4536b72015-11-24 13:45:23 +00003164
3165 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
3166 if (invoke->HasPcRelativeDexCache()) {
3167 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
3168 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003169}
3170
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003171static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
3172 if (invoke->GetLocations()->Intrinsified()) {
3173 IntrinsicCodeGeneratorARM intrinsic(codegen);
3174 intrinsic.Dispatch(invoke);
3175 return true;
3176 }
3177 return false;
3178}
3179
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003180void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003181 // Explicit clinit checks triggered by static invokes must have been pruned by
3182 // art::PrepareForRegisterAllocation.
3183 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003184
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003185 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3186 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003187 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003188
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003189 LocationSummary* locations = invoke->GetLocations();
3190 codegen_->GenerateStaticOrDirectCall(
3191 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00003192 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003193}
3194
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003195void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003196 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003197 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003198}
3199
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003200void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01003201 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003202 if (intrinsic.TryDispatch(invoke)) {
3203 return;
3204 }
3205
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003206 HandleInvoke(invoke);
3207}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003208
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003209void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003210 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3211 return;
3212 }
3213
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003214 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003215 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003216 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003217}
3218
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003219void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3220 HandleInvoke(invoke);
3221 // Add the hidden argument.
3222 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
3223}
3224
3225void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3226 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00003227 LocationSummary* locations = invoke->GetLocations();
3228 Register temp = locations->GetTemp(0).AsRegister<Register>();
3229 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003230 Location receiver = locations->InAt(0);
3231 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3232
Roland Levillain3b359c72015-11-17 19:35:12 +00003233 // Set the hidden argument. This is safe to do this here, as R12
3234 // won't be modified thereafter, before the `blx` (call) instruction.
3235 DCHECK_EQ(R12, hidden_reg);
3236 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003237
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003238 if (receiver.IsStackSlot()) {
3239 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00003240 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003241 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3242 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00003243 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00003244 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003245 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003246 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00003247 // Instead of simply (possibly) unpoisoning `temp` here, we should
3248 // emit a read barrier for the previous class reference load.
3249 // However this is not required in practice, as this is an
3250 // intermediate/temporary reference and because the current
3251 // concurrent copying collector keeps the from-space memory
3252 // intact/accessible until the end of the marking phase (the
3253 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003254 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003255 __ LoadFromOffset(kLoadWord, temp, temp,
3256 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3257 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003258 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003259 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003260 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00003261 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07003262 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003263 // LR = temp->GetEntryPoint();
3264 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
3265 // LR();
3266 __ blx(LR);
3267 DCHECK(!codegen_->IsLeafMethod());
3268 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3269}
3270
Orion Hodsonac141392017-01-13 11:53:47 +00003271void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3272 HandleInvoke(invoke);
3273}
3274
3275void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3276 codegen_->GenerateInvokePolymorphicCall(invoke);
3277}
3278
Roland Levillain88cb1752014-10-20 16:36:47 +01003279void LocationsBuilderARM::VisitNeg(HNeg* neg) {
3280 LocationSummary* locations =
3281 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3282 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003283 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01003284 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3286 break;
3287 }
3288 case Primitive::kPrimLong: {
3289 locations->SetInAt(0, Location::RequiresRegister());
3290 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003291 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003292 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003293
Roland Levillain88cb1752014-10-20 16:36:47 +01003294 case Primitive::kPrimFloat:
3295 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003296 locations->SetInAt(0, Location::RequiresFpuRegister());
3297 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003298 break;
3299
3300 default:
3301 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3302 }
3303}
3304
3305void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
3306 LocationSummary* locations = neg->GetLocations();
3307 Location out = locations->Out();
3308 Location in = locations->InAt(0);
3309 switch (neg->GetResultType()) {
3310 case Primitive::kPrimInt:
3311 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003312 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01003313 break;
3314
3315 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003316 DCHECK(in.IsRegisterPair());
3317 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3318 __ rsbs(out.AsRegisterPairLow<Register>(),
3319 in.AsRegisterPairLow<Register>(),
3320 ShifterOperand(0));
3321 // We cannot emit an RSC (Reverse Subtract with Carry)
3322 // instruction here, as it does not exist in the Thumb-2
3323 // instruction set. We use the following approach
3324 // using SBC and SUB instead.
3325 //
3326 // out.hi = -C
3327 __ sbc(out.AsRegisterPairHigh<Register>(),
3328 out.AsRegisterPairHigh<Register>(),
3329 ShifterOperand(out.AsRegisterPairHigh<Register>()));
3330 // out.hi = out.hi - in.hi
3331 __ sub(out.AsRegisterPairHigh<Register>(),
3332 out.AsRegisterPairHigh<Register>(),
3333 ShifterOperand(in.AsRegisterPairHigh<Register>()));
3334 break;
3335
Roland Levillain88cb1752014-10-20 16:36:47 +01003336 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003337 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003338 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00003339 break;
3340
Roland Levillain88cb1752014-10-20 16:36:47 +01003341 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003342 DCHECK(in.IsFpuRegisterPair());
3343 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3344 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01003345 break;
3346
3347 default:
3348 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3349 }
3350}
3351
Roland Levillaindff1f282014-11-05 14:15:05 +00003352void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003353 Primitive::Type result_type = conversion->GetResultType();
3354 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003355 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00003356
Roland Levillain5b3ee562015-04-14 16:02:41 +01003357 // The float-to-long, double-to-long and long-to-float type conversions
3358 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003359 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01003360 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3361 && result_type == Primitive::kPrimLong)
3362 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003363 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00003364 : LocationSummary::kNoCall;
3365 LocationSummary* locations =
3366 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3367
David Brazdilb2bd1c52015-03-25 11:17:37 +00003368 // The Java language does not allow treating boolean as an integral type but
3369 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00003370
Roland Levillaindff1f282014-11-05 14:15:05 +00003371 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003372 case Primitive::kPrimByte:
3373 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003374 case Primitive::kPrimLong:
3375 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003376 case Primitive::kPrimBoolean:
3377 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003378 case Primitive::kPrimShort:
3379 case Primitive::kPrimInt:
3380 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003381 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003382 locations->SetInAt(0, Location::RequiresRegister());
3383 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3384 break;
3385
3386 default:
3387 LOG(FATAL) << "Unexpected type conversion from " << input_type
3388 << " to " << result_type;
3389 }
3390 break;
3391
Roland Levillain01a8d712014-11-14 16:27:39 +00003392 case Primitive::kPrimShort:
3393 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003394 case Primitive::kPrimLong:
3395 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003396 case Primitive::kPrimBoolean:
3397 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003398 case Primitive::kPrimByte:
3399 case Primitive::kPrimInt:
3400 case Primitive::kPrimChar:
3401 // Processing a Dex `int-to-short' instruction.
3402 locations->SetInAt(0, Location::RequiresRegister());
3403 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3404 break;
3405
3406 default:
3407 LOG(FATAL) << "Unexpected type conversion from " << input_type
3408 << " to " << result_type;
3409 }
3410 break;
3411
Roland Levillain946e1432014-11-11 17:35:19 +00003412 case Primitive::kPrimInt:
3413 switch (input_type) {
3414 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003415 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003416 locations->SetInAt(0, Location::Any());
3417 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3418 break;
3419
3420 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00003421 // Processing a Dex `float-to-int' instruction.
3422 locations->SetInAt(0, Location::RequiresFpuRegister());
3423 locations->SetOut(Location::RequiresRegister());
3424 locations->AddTemp(Location::RequiresFpuRegister());
3425 break;
3426
Roland Levillain946e1432014-11-11 17:35:19 +00003427 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003428 // Processing a Dex `double-to-int' instruction.
3429 locations->SetInAt(0, Location::RequiresFpuRegister());
3430 locations->SetOut(Location::RequiresRegister());
3431 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00003432 break;
3433
3434 default:
3435 LOG(FATAL) << "Unexpected type conversion from " << input_type
3436 << " to " << result_type;
3437 }
3438 break;
3439
Roland Levillaindff1f282014-11-05 14:15:05 +00003440 case Primitive::kPrimLong:
3441 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003442 case Primitive::kPrimBoolean:
3443 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003444 case Primitive::kPrimByte:
3445 case Primitive::kPrimShort:
3446 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003447 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003448 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003449 locations->SetInAt(0, Location::RequiresRegister());
3450 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3451 break;
3452
Roland Levillain624279f2014-12-04 11:54:28 +00003453 case Primitive::kPrimFloat: {
3454 // Processing a Dex `float-to-long' instruction.
3455 InvokeRuntimeCallingConvention calling_convention;
3456 locations->SetInAt(0, Location::FpuRegisterLocation(
3457 calling_convention.GetFpuRegisterAt(0)));
3458 locations->SetOut(Location::RegisterPairLocation(R0, R1));
3459 break;
3460 }
3461
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003462 case Primitive::kPrimDouble: {
3463 // Processing a Dex `double-to-long' instruction.
3464 InvokeRuntimeCallingConvention calling_convention;
3465 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3466 calling_convention.GetFpuRegisterAt(0),
3467 calling_convention.GetFpuRegisterAt(1)));
3468 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00003469 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003470 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003471
3472 default:
3473 LOG(FATAL) << "Unexpected type conversion from " << input_type
3474 << " to " << result_type;
3475 }
3476 break;
3477
Roland Levillain981e4542014-11-14 11:47:14 +00003478 case Primitive::kPrimChar:
3479 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003480 case Primitive::kPrimLong:
3481 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003482 case Primitive::kPrimBoolean:
3483 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003484 case Primitive::kPrimByte:
3485 case Primitive::kPrimShort:
3486 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003487 // Processing a Dex `int-to-char' instruction.
3488 locations->SetInAt(0, Location::RequiresRegister());
3489 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3490 break;
3491
3492 default:
3493 LOG(FATAL) << "Unexpected type conversion from " << input_type
3494 << " to " << result_type;
3495 }
3496 break;
3497
Roland Levillaindff1f282014-11-05 14:15:05 +00003498 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003499 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003500 case Primitive::kPrimBoolean:
3501 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003502 case Primitive::kPrimByte:
3503 case Primitive::kPrimShort:
3504 case Primitive::kPrimInt:
3505 case Primitive::kPrimChar:
3506 // Processing a Dex `int-to-float' instruction.
3507 locations->SetInAt(0, Location::RequiresRegister());
3508 locations->SetOut(Location::RequiresFpuRegister());
3509 break;
3510
Roland Levillain5b3ee562015-04-14 16:02:41 +01003511 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00003512 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01003513 InvokeRuntimeCallingConvention calling_convention;
3514 locations->SetInAt(0, Location::RegisterPairLocation(
3515 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3516 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00003517 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01003518 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003519
Roland Levillaincff13742014-11-17 14:32:17 +00003520 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003521 // Processing a Dex `double-to-float' instruction.
3522 locations->SetInAt(0, Location::RequiresFpuRegister());
3523 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003524 break;
3525
3526 default:
3527 LOG(FATAL) << "Unexpected type conversion from " << input_type
3528 << " to " << result_type;
3529 };
3530 break;
3531
Roland Levillaindff1f282014-11-05 14:15:05 +00003532 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003533 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003534 case Primitive::kPrimBoolean:
3535 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003536 case Primitive::kPrimByte:
3537 case Primitive::kPrimShort:
3538 case Primitive::kPrimInt:
3539 case Primitive::kPrimChar:
3540 // Processing a Dex `int-to-double' instruction.
3541 locations->SetInAt(0, Location::RequiresRegister());
3542 locations->SetOut(Location::RequiresFpuRegister());
3543 break;
3544
3545 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003546 // Processing a Dex `long-to-double' instruction.
3547 locations->SetInAt(0, Location::RequiresRegister());
3548 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01003549 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00003550 locations->AddTemp(Location::RequiresFpuRegister());
3551 break;
3552
Roland Levillaincff13742014-11-17 14:32:17 +00003553 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003554 // Processing a Dex `float-to-double' instruction.
3555 locations->SetInAt(0, Location::RequiresFpuRegister());
3556 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003557 break;
3558
3559 default:
3560 LOG(FATAL) << "Unexpected type conversion from " << input_type
3561 << " to " << result_type;
3562 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003563 break;
3564
3565 default:
3566 LOG(FATAL) << "Unexpected type conversion from " << input_type
3567 << " to " << result_type;
3568 }
3569}
3570
3571void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
3572 LocationSummary* locations = conversion->GetLocations();
3573 Location out = locations->Out();
3574 Location in = locations->InAt(0);
3575 Primitive::Type result_type = conversion->GetResultType();
3576 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003577 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00003578 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003579 case Primitive::kPrimByte:
3580 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003581 case Primitive::kPrimLong:
3582 // Type conversion from long to byte is a result of code transformations.
3583 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
3584 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003585 case Primitive::kPrimBoolean:
3586 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003587 case Primitive::kPrimShort:
3588 case Primitive::kPrimInt:
3589 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003590 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003591 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00003592 break;
3593
3594 default:
3595 LOG(FATAL) << "Unexpected type conversion from " << input_type
3596 << " to " << result_type;
3597 }
3598 break;
3599
Roland Levillain01a8d712014-11-14 16:27:39 +00003600 case Primitive::kPrimShort:
3601 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003602 case Primitive::kPrimLong:
3603 // Type conversion from long to short is a result of code transformations.
3604 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3605 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003606 case Primitive::kPrimBoolean:
3607 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003608 case Primitive::kPrimByte:
3609 case Primitive::kPrimInt:
3610 case Primitive::kPrimChar:
3611 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003612 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00003613 break;
3614
3615 default:
3616 LOG(FATAL) << "Unexpected type conversion from " << input_type
3617 << " to " << result_type;
3618 }
3619 break;
3620
Roland Levillain946e1432014-11-11 17:35:19 +00003621 case Primitive::kPrimInt:
3622 switch (input_type) {
3623 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003624 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003625 DCHECK(out.IsRegister());
3626 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003627 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00003628 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003629 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00003630 } else {
3631 DCHECK(in.IsConstant());
3632 DCHECK(in.GetConstant()->IsLongConstant());
3633 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003634 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00003635 }
3636 break;
3637
Roland Levillain3f8f9362014-12-02 17:45:01 +00003638 case Primitive::kPrimFloat: {
3639 // Processing a Dex `float-to-int' instruction.
3640 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003641 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00003642 __ vmovrs(out.AsRegister<Register>(), temp);
3643 break;
3644 }
3645
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003646 case Primitive::kPrimDouble: {
3647 // Processing a Dex `double-to-int' instruction.
3648 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01003649 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003650 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00003651 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003652 }
Roland Levillain946e1432014-11-11 17:35:19 +00003653
3654 default:
3655 LOG(FATAL) << "Unexpected type conversion from " << input_type
3656 << " to " << result_type;
3657 }
3658 break;
3659
Roland Levillaindff1f282014-11-05 14:15:05 +00003660 case Primitive::kPrimLong:
3661 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003662 case Primitive::kPrimBoolean:
3663 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003664 case Primitive::kPrimByte:
3665 case Primitive::kPrimShort:
3666 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003667 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003668 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003669 DCHECK(out.IsRegisterPair());
3670 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003671 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00003672 // Sign extension.
3673 __ Asr(out.AsRegisterPairHigh<Register>(),
3674 out.AsRegisterPairLow<Register>(),
3675 31);
3676 break;
3677
3678 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00003679 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003680 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003681 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00003682 break;
3683
Roland Levillaindff1f282014-11-05 14:15:05 +00003684 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003685 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003686 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003687 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00003688 break;
3689
3690 default:
3691 LOG(FATAL) << "Unexpected type conversion from " << input_type
3692 << " to " << result_type;
3693 }
3694 break;
3695
Roland Levillain981e4542014-11-14 11:47:14 +00003696 case Primitive::kPrimChar:
3697 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003698 case Primitive::kPrimLong:
3699 // Type conversion from long to char is a result of code transformations.
3700 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3701 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003702 case Primitive::kPrimBoolean:
3703 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003704 case Primitive::kPrimByte:
3705 case Primitive::kPrimShort:
3706 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003707 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003708 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00003709 break;
3710
3711 default:
3712 LOG(FATAL) << "Unexpected type conversion from " << input_type
3713 << " to " << result_type;
3714 }
3715 break;
3716
Roland Levillaindff1f282014-11-05 14:15:05 +00003717 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003718 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003719 case Primitive::kPrimBoolean:
3720 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003721 case Primitive::kPrimByte:
3722 case Primitive::kPrimShort:
3723 case Primitive::kPrimInt:
3724 case Primitive::kPrimChar: {
3725 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003726 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
3727 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003728 break;
3729 }
3730
Roland Levillain5b3ee562015-04-14 16:02:41 +01003731 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00003732 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01003733 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003734 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00003735 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00003736
Roland Levillaincff13742014-11-17 14:32:17 +00003737 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003738 // Processing a Dex `double-to-float' instruction.
3739 __ vcvtsd(out.AsFpuRegister<SRegister>(),
3740 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00003741 break;
3742
3743 default:
3744 LOG(FATAL) << "Unexpected type conversion from " << input_type
3745 << " to " << result_type;
3746 };
3747 break;
3748
Roland Levillaindff1f282014-11-05 14:15:05 +00003749 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003750 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003751 case Primitive::kPrimBoolean:
3752 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003753 case Primitive::kPrimByte:
3754 case Primitive::kPrimShort:
3755 case Primitive::kPrimInt:
3756 case Primitive::kPrimChar: {
3757 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003758 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00003759 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3760 out.AsFpuRegisterPairLow<SRegister>());
3761 break;
3762 }
3763
Roland Levillain647b9ed2014-11-27 12:06:00 +00003764 case Primitive::kPrimLong: {
3765 // Processing a Dex `long-to-double' instruction.
3766 Register low = in.AsRegisterPairLow<Register>();
3767 Register high = in.AsRegisterPairHigh<Register>();
3768 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
3769 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003770 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00003771 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01003772 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
3773 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003774
Roland Levillain682393c2015-04-14 15:57:52 +01003775 // temp_d = int-to-double(high)
3776 __ vmovsr(temp_s, high);
3777 __ vcvtdi(temp_d, temp_s);
3778 // constant_d = k2Pow32EncodingForDouble
3779 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
3780 // out_d = unsigned-to-double(low)
3781 __ vmovsr(out_s, low);
3782 __ vcvtdu(out_d, out_s);
3783 // out_d += temp_d * constant_d
3784 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00003785 break;
3786 }
3787
Roland Levillaincff13742014-11-17 14:32:17 +00003788 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003789 // Processing a Dex `float-to-double' instruction.
3790 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3791 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00003792 break;
3793
3794 default:
3795 LOG(FATAL) << "Unexpected type conversion from " << input_type
3796 << " to " << result_type;
3797 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003798 break;
3799
3800 default:
3801 LOG(FATAL) << "Unexpected type conversion from " << input_type
3802 << " to " << result_type;
3803 }
3804}
3805
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003806void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003807 LocationSummary* locations =
3808 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003809 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003810 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003811 locations->SetInAt(0, Location::RequiresRegister());
3812 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003813 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3814 break;
3815 }
3816
3817 case Primitive::kPrimLong: {
3818 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003819 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003820 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003821 break;
3822 }
3823
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003824 case Primitive::kPrimFloat:
3825 case Primitive::kPrimDouble: {
3826 locations->SetInAt(0, Location::RequiresFpuRegister());
3827 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003828 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003829 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003830 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003831
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003832 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003833 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003834 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003835}
3836
3837void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
3838 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003839 Location out = locations->Out();
3840 Location first = locations->InAt(0);
3841 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003842 switch (add->GetResultType()) {
3843 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003844 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003845 __ add(out.AsRegister<Register>(),
3846 first.AsRegister<Register>(),
3847 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003848 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003849 __ AddConstant(out.AsRegister<Register>(),
3850 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003851 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003852 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003853 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003854
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003855 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003856 if (second.IsConstant()) {
3857 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3858 GenerateAddLongConst(out, first, value);
3859 } else {
3860 DCHECK(second.IsRegisterPair());
3861 __ adds(out.AsRegisterPairLow<Register>(),
3862 first.AsRegisterPairLow<Register>(),
3863 ShifterOperand(second.AsRegisterPairLow<Register>()));
3864 __ adc(out.AsRegisterPairHigh<Register>(),
3865 first.AsRegisterPairHigh<Register>(),
3866 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3867 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003868 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003869 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003870
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003871 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00003872 __ vadds(out.AsFpuRegister<SRegister>(),
3873 first.AsFpuRegister<SRegister>(),
3874 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003875 break;
3876
3877 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003878 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3879 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3880 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003881 break;
3882
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003883 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003884 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003885 }
3886}
3887
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003888void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003889 LocationSummary* locations =
3890 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003891 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003892 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003893 locations->SetInAt(0, Location::RequiresRegister());
3894 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00003895 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3896 break;
3897 }
3898
3899 case Primitive::kPrimLong: {
3900 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01003901 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003902 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003903 break;
3904 }
Calin Juravle11351682014-10-23 15:38:15 +01003905 case Primitive::kPrimFloat:
3906 case Primitive::kPrimDouble: {
3907 locations->SetInAt(0, Location::RequiresFpuRegister());
3908 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003909 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003910 break;
Calin Juravle11351682014-10-23 15:38:15 +01003911 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003912 default:
Calin Juravle11351682014-10-23 15:38:15 +01003913 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003914 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003915}
3916
3917void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
3918 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01003919 Location out = locations->Out();
3920 Location first = locations->InAt(0);
3921 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003922 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003923 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01003924 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003925 __ sub(out.AsRegister<Register>(),
3926 first.AsRegister<Register>(),
3927 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003928 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003929 __ AddConstant(out.AsRegister<Register>(),
3930 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01003931 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003932 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003933 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003934 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003935
Calin Juravle11351682014-10-23 15:38:15 +01003936 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01003937 if (second.IsConstant()) {
3938 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
3939 GenerateAddLongConst(out, first, -value);
3940 } else {
3941 DCHECK(second.IsRegisterPair());
3942 __ subs(out.AsRegisterPairLow<Register>(),
3943 first.AsRegisterPairLow<Register>(),
3944 ShifterOperand(second.AsRegisterPairLow<Register>()));
3945 __ sbc(out.AsRegisterPairHigh<Register>(),
3946 first.AsRegisterPairHigh<Register>(),
3947 ShifterOperand(second.AsRegisterPairHigh<Register>()));
3948 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003949 break;
Calin Juravle11351682014-10-23 15:38:15 +01003950 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003951
Calin Juravle11351682014-10-23 15:38:15 +01003952 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00003953 __ vsubs(out.AsFpuRegister<SRegister>(),
3954 first.AsFpuRegister<SRegister>(),
3955 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003956 break;
Calin Juravle11351682014-10-23 15:38:15 +01003957 }
3958
3959 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003960 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3961 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
3962 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01003963 break;
3964 }
3965
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003966
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003967 default:
Calin Juravle11351682014-10-23 15:38:15 +01003968 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003969 }
3970}
3971
Calin Juravle34bacdf2014-10-07 20:23:36 +01003972void LocationsBuilderARM::VisitMul(HMul* mul) {
3973 LocationSummary* locations =
3974 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3975 switch (mul->GetResultType()) {
3976 case Primitive::kPrimInt:
3977 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003978 locations->SetInAt(0, Location::RequiresRegister());
3979 locations->SetInAt(1, Location::RequiresRegister());
3980 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003981 break;
3982 }
3983
Calin Juravleb5bfa962014-10-21 18:02:24 +01003984 case Primitive::kPrimFloat:
3985 case Primitive::kPrimDouble: {
3986 locations->SetInAt(0, Location::RequiresFpuRegister());
3987 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003988 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01003989 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003990 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003991
3992 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003993 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003994 }
3995}
3996
3997void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
3998 LocationSummary* locations = mul->GetLocations();
3999 Location out = locations->Out();
4000 Location first = locations->InAt(0);
4001 Location second = locations->InAt(1);
4002 switch (mul->GetResultType()) {
4003 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00004004 __ mul(out.AsRegister<Register>(),
4005 first.AsRegister<Register>(),
4006 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004007 break;
4008 }
4009 case Primitive::kPrimLong: {
4010 Register out_hi = out.AsRegisterPairHigh<Register>();
4011 Register out_lo = out.AsRegisterPairLow<Register>();
4012 Register in1_hi = first.AsRegisterPairHigh<Register>();
4013 Register in1_lo = first.AsRegisterPairLow<Register>();
4014 Register in2_hi = second.AsRegisterPairHigh<Register>();
4015 Register in2_lo = second.AsRegisterPairLow<Register>();
4016
4017 // Extra checks to protect caused by the existence of R1_R2.
4018 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4019 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
4020 DCHECK_NE(out_hi, in1_lo);
4021 DCHECK_NE(out_hi, in2_lo);
4022
4023 // input: in1 - 64 bits, in2 - 64 bits
4024 // output: out
4025 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4026 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4027 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4028
4029 // IP <- in1.lo * in2.hi
4030 __ mul(IP, in1_lo, in2_hi);
4031 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4032 __ mla(out_hi, in1_hi, in2_lo, IP);
4033 // out.lo <- (in1.lo * in2.lo)[31:0];
4034 __ umull(out_lo, IP, in1_lo, in2_lo);
4035 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
4036 __ add(out_hi, out_hi, ShifterOperand(IP));
4037 break;
4038 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01004039
4040 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004041 __ vmuls(out.AsFpuRegister<SRegister>(),
4042 first.AsFpuRegister<SRegister>(),
4043 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004044 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004045 }
4046
4047 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004048 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4049 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4050 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01004051 break;
4052 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004053
4054 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004055 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004056 }
4057}
4058
Zheng Xuc6667102015-05-15 16:08:45 +08004059void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4060 DCHECK(instruction->IsDiv() || instruction->IsRem());
4061 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4062
4063 LocationSummary* locations = instruction->GetLocations();
4064 Location second = locations->InAt(1);
4065 DCHECK(second.IsConstant());
4066
4067 Register out = locations->Out().AsRegister<Register>();
4068 Register dividend = locations->InAt(0).AsRegister<Register>();
4069 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4070 DCHECK(imm == 1 || imm == -1);
4071
4072 if (instruction->IsRem()) {
4073 __ LoadImmediate(out, 0);
4074 } else {
4075 if (imm == 1) {
4076 __ Mov(out, dividend);
4077 } else {
4078 __ rsb(out, dividend, ShifterOperand(0));
4079 }
4080 }
4081}
4082
4083void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4084 DCHECK(instruction->IsDiv() || instruction->IsRem());
4085 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4086
4087 LocationSummary* locations = instruction->GetLocations();
4088 Location second = locations->InAt(1);
4089 DCHECK(second.IsConstant());
4090
4091 Register out = locations->Out().AsRegister<Register>();
4092 Register dividend = locations->InAt(0).AsRegister<Register>();
4093 Register temp = locations->GetTemp(0).AsRegister<Register>();
4094 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004095 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08004096 int ctz_imm = CTZ(abs_imm);
4097
4098 if (ctz_imm == 1) {
4099 __ Lsr(temp, dividend, 32 - ctz_imm);
4100 } else {
4101 __ Asr(temp, dividend, 31);
4102 __ Lsr(temp, temp, 32 - ctz_imm);
4103 }
4104 __ add(out, temp, ShifterOperand(dividend));
4105
4106 if (instruction->IsDiv()) {
4107 __ Asr(out, out, ctz_imm);
4108 if (imm < 0) {
4109 __ rsb(out, out, ShifterOperand(0));
4110 }
4111 } else {
4112 __ ubfx(out, out, 0, ctz_imm);
4113 __ sub(out, out, ShifterOperand(temp));
4114 }
4115}
4116
4117void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4118 DCHECK(instruction->IsDiv() || instruction->IsRem());
4119 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4120
4121 LocationSummary* locations = instruction->GetLocations();
4122 Location second = locations->InAt(1);
4123 DCHECK(second.IsConstant());
4124
4125 Register out = locations->Out().AsRegister<Register>();
4126 Register dividend = locations->InAt(0).AsRegister<Register>();
4127 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4128 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4129 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4130
4131 int64_t magic;
4132 int shift;
4133 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4134
4135 __ LoadImmediate(temp1, magic);
4136 __ smull(temp2, temp1, dividend, temp1);
4137
4138 if (imm > 0 && magic < 0) {
4139 __ add(temp1, temp1, ShifterOperand(dividend));
4140 } else if (imm < 0 && magic > 0) {
4141 __ sub(temp1, temp1, ShifterOperand(dividend));
4142 }
4143
4144 if (shift != 0) {
4145 __ Asr(temp1, temp1, shift);
4146 }
4147
4148 if (instruction->IsDiv()) {
4149 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
4150 } else {
4151 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
4152 // TODO: Strength reduction for mls.
4153 __ LoadImmediate(temp2, imm);
4154 __ mls(out, temp1, temp2, dividend);
4155 }
4156}
4157
4158void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
4159 DCHECK(instruction->IsDiv() || instruction->IsRem());
4160 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4161
4162 LocationSummary* locations = instruction->GetLocations();
4163 Location second = locations->InAt(1);
4164 DCHECK(second.IsConstant());
4165
4166 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4167 if (imm == 0) {
4168 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4169 } else if (imm == 1 || imm == -1) {
4170 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004171 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004172 DivRemByPowerOfTwo(instruction);
4173 } else {
4174 DCHECK(imm <= -2 || imm >= 2);
4175 GenerateDivRemWithAnyConstant(instruction);
4176 }
4177}
4178
Calin Juravle7c4954d2014-10-28 16:57:40 +00004179void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004180 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4181 if (div->GetResultType() == Primitive::kPrimLong) {
4182 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004183 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004184 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4185 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004186 } else if (div->GetResultType() == Primitive::kPrimInt &&
4187 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4188 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004189 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004190 }
4191
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004192 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4193
Calin Juravle7c4954d2014-10-28 16:57:40 +00004194 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004195 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004196 if (div->InputAt(1)->IsConstant()) {
4197 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004198 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004199 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004200 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
4201 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004202 // No temp register required.
4203 } else {
4204 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004205 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004206 locations->AddTemp(Location::RequiresRegister());
4207 }
4208 }
4209 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004210 locations->SetInAt(0, Location::RequiresRegister());
4211 locations->SetInAt(1, Location::RequiresRegister());
4212 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4213 } else {
4214 InvokeRuntimeCallingConvention calling_convention;
4215 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4216 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004217 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004218 // we only need the former.
4219 locations->SetOut(Location::RegisterLocation(R0));
4220 }
Calin Juravled0d48522014-11-04 16:40:20 +00004221 break;
4222 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004223 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004224 InvokeRuntimeCallingConvention calling_convention;
4225 locations->SetInAt(0, Location::RegisterPairLocation(
4226 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4227 locations->SetInAt(1, Location::RegisterPairLocation(
4228 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004229 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004230 break;
4231 }
4232 case Primitive::kPrimFloat:
4233 case Primitive::kPrimDouble: {
4234 locations->SetInAt(0, Location::RequiresFpuRegister());
4235 locations->SetInAt(1, Location::RequiresFpuRegister());
4236 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4237 break;
4238 }
4239
4240 default:
4241 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4242 }
4243}
4244
4245void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
4246 LocationSummary* locations = div->GetLocations();
4247 Location out = locations->Out();
4248 Location first = locations->InAt(0);
4249 Location second = locations->InAt(1);
4250
4251 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004252 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004253 if (second.IsConstant()) {
4254 GenerateDivRemConstantIntegral(div);
4255 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004256 __ sdiv(out.AsRegister<Register>(),
4257 first.AsRegister<Register>(),
4258 second.AsRegister<Register>());
4259 } else {
4260 InvokeRuntimeCallingConvention calling_convention;
4261 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4262 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4263 DCHECK_EQ(R0, out.AsRegister<Register>());
4264
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004265 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004266 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004267 }
Calin Juravled0d48522014-11-04 16:40:20 +00004268 break;
4269 }
4270
Calin Juravle7c4954d2014-10-28 16:57:40 +00004271 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004272 InvokeRuntimeCallingConvention calling_convention;
4273 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4274 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4275 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4276 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4277 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004278 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004279
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004280 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004281 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00004282 break;
4283 }
4284
4285 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004286 __ vdivs(out.AsFpuRegister<SRegister>(),
4287 first.AsFpuRegister<SRegister>(),
4288 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004289 break;
4290 }
4291
4292 case Primitive::kPrimDouble: {
4293 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4294 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4295 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
4296 break;
4297 }
4298
4299 default:
4300 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4301 }
4302}
4303
Calin Juravlebacfec32014-11-14 15:54:36 +00004304void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004305 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004306
4307 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004308 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004309 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4310 // sdiv will be replaced by other instruction sequence.
4311 call_kind = LocationSummary::kNoCall;
4312 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4313 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004314 // Have hardware divide instruction for int, do it with three instructions.
4315 call_kind = LocationSummary::kNoCall;
4316 }
4317
Calin Juravlebacfec32014-11-14 15:54:36 +00004318 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4319
Calin Juravled2ec87d2014-12-08 14:24:46 +00004320 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004321 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004322 if (rem->InputAt(1)->IsConstant()) {
4323 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004324 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004325 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004326 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
4327 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004328 // No temp register required.
4329 } else {
4330 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004331 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004332 locations->AddTemp(Location::RequiresRegister());
4333 }
4334 }
4335 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004336 locations->SetInAt(0, Location::RequiresRegister());
4337 locations->SetInAt(1, Location::RequiresRegister());
4338 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4339 locations->AddTemp(Location::RequiresRegister());
4340 } else {
4341 InvokeRuntimeCallingConvention calling_convention;
4342 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4343 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004344 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004345 // we only need the latter.
4346 locations->SetOut(Location::RegisterLocation(R1));
4347 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004348 break;
4349 }
4350 case Primitive::kPrimLong: {
4351 InvokeRuntimeCallingConvention calling_convention;
4352 locations->SetInAt(0, Location::RegisterPairLocation(
4353 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4354 locations->SetInAt(1, Location::RegisterPairLocation(
4355 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4356 // The runtime helper puts the output in R2,R3.
4357 locations->SetOut(Location::RegisterPairLocation(R2, R3));
4358 break;
4359 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00004360 case Primitive::kPrimFloat: {
4361 InvokeRuntimeCallingConvention calling_convention;
4362 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4363 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4364 locations->SetOut(Location::FpuRegisterLocation(S0));
4365 break;
4366 }
4367
Calin Juravlebacfec32014-11-14 15:54:36 +00004368 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004369 InvokeRuntimeCallingConvention calling_convention;
4370 locations->SetInAt(0, Location::FpuRegisterPairLocation(
4371 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4372 locations->SetInAt(1, Location::FpuRegisterPairLocation(
4373 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4374 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00004375 break;
4376 }
4377
4378 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004379 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004380 }
4381}
4382
4383void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
4384 LocationSummary* locations = rem->GetLocations();
4385 Location out = locations->Out();
4386 Location first = locations->InAt(0);
4387 Location second = locations->InAt(1);
4388
Calin Juravled2ec87d2014-12-08 14:24:46 +00004389 Primitive::Type type = rem->GetResultType();
4390 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004391 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004392 if (second.IsConstant()) {
4393 GenerateDivRemConstantIntegral(rem);
4394 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004395 Register reg1 = first.AsRegister<Register>();
4396 Register reg2 = second.AsRegister<Register>();
4397 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004398
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004399 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004400 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004401 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004402 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004403 } else {
4404 InvokeRuntimeCallingConvention calling_convention;
4405 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4406 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4407 DCHECK_EQ(R1, out.AsRegister<Register>());
4408
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004409 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004410 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004411 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004412 break;
4413 }
4414
4415 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004416 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004417 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004418 break;
4419 }
4420
Calin Juravled2ec87d2014-12-08 14:24:46 +00004421 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004422 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004423 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00004424 break;
4425 }
4426
Calin Juravlebacfec32014-11-14 15:54:36 +00004427 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004428 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004429 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004430 break;
4431 }
4432
4433 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004434 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004435 }
4436}
4437
Calin Juravled0d48522014-11-04 16:40:20 +00004438void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004439 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004440 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00004441}
4442
4443void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004444 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004445 codegen_->AddSlowPath(slow_path);
4446
4447 LocationSummary* locations = instruction->GetLocations();
4448 Location value = locations->InAt(0);
4449
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004450 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00004451 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004452 case Primitive::kPrimByte:
4453 case Primitive::kPrimChar:
4454 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004455 case Primitive::kPrimInt: {
4456 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004457 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004458 } else {
4459 DCHECK(value.IsConstant()) << value;
4460 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4461 __ b(slow_path->GetEntryLabel());
4462 }
4463 }
4464 break;
4465 }
4466 case Primitive::kPrimLong: {
4467 if (value.IsRegisterPair()) {
4468 __ orrs(IP,
4469 value.AsRegisterPairLow<Register>(),
4470 ShifterOperand(value.AsRegisterPairHigh<Register>()));
4471 __ b(slow_path->GetEntryLabel(), EQ);
4472 } else {
4473 DCHECK(value.IsConstant()) << value;
4474 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4475 __ b(slow_path->GetEntryLabel());
4476 }
4477 }
4478 break;
4479 default:
4480 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4481 }
4482 }
Calin Juravled0d48522014-11-04 16:40:20 +00004483}
4484
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004485void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
4486 Register in = locations->InAt(0).AsRegister<Register>();
4487 Location rhs = locations->InAt(1);
4488 Register out = locations->Out().AsRegister<Register>();
4489
4490 if (rhs.IsConstant()) {
4491 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4492 // so map all rotations to a +ve. equivalent in that range.
4493 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4494 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4495 if (rot) {
4496 // Rotate, mapping left rotations to right equivalents if necessary.
4497 // (e.g. left by 2 bits == right by 30.)
4498 __ Ror(out, in, rot);
4499 } else if (out != in) {
4500 __ Mov(out, in);
4501 }
4502 } else {
4503 __ Ror(out, in, rhs.AsRegister<Register>());
4504 }
4505}
4506
4507// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4508// rotates by swapping input regs (effectively rotating by the first 32-bits of
4509// a larger rotation) or flipping direction (thus treating larger right/left
4510// rotations as sub-word sized rotations in the other direction) as appropriate.
Anton Kirilov6f644202017-02-27 18:29:45 +00004511void InstructionCodeGeneratorARM::HandleLongRotate(HRor* ror) {
4512 LocationSummary* locations = ror->GetLocations();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004513 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
4514 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
4515 Location rhs = locations->InAt(1);
4516 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
4517 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
4518
4519 if (rhs.IsConstant()) {
4520 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4521 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00004522 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004523 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4524 // logic below to a simple pair of binary orr.
4525 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4526 if (rot >= kArmBitsPerWord) {
4527 rot -= kArmBitsPerWord;
4528 std::swap(in_reg_hi, in_reg_lo);
4529 }
4530 // Rotate, or mov to out for zero or word size rotations.
4531 if (rot != 0u) {
4532 __ Lsr(out_reg_hi, in_reg_hi, rot);
4533 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
4534 __ Lsr(out_reg_lo, in_reg_lo, rot);
4535 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
4536 } else {
4537 __ Mov(out_reg_lo, in_reg_lo);
4538 __ Mov(out_reg_hi, in_reg_hi);
4539 }
4540 } else {
4541 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
4542 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
4543 Label end;
4544 Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004545 Label* final_label = codegen_->GetFinalLabel(ror, &end);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004546
4547 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
4548 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
4549 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
4550 __ b(&shift_by_32_plus_shift_right, CC);
4551
4552 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4553 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4554 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4555 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4556 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4557 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4558 __ Lsr(shift_left, in_reg_hi, shift_right);
4559 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
Anton Kirilov6f644202017-02-27 18:29:45 +00004560 __ b(final_label);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004561
4562 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4563 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4564 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4565 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4566 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4567 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4568 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4569 __ Lsl(shift_right, in_reg_hi, shift_left);
4570 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
4571
Anton Kirilov6f644202017-02-27 18:29:45 +00004572 if (end.IsLinked()) {
4573 __ Bind(&end);
4574 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004575 }
4576}
Roland Levillain22c49222016-03-18 14:04:28 +00004577
4578void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004579 LocationSummary* locations =
4580 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4581 switch (ror->GetResultType()) {
4582 case Primitive::kPrimInt: {
4583 locations->SetInAt(0, Location::RequiresRegister());
4584 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4585 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4586 break;
4587 }
4588 case Primitive::kPrimLong: {
4589 locations->SetInAt(0, Location::RequiresRegister());
4590 if (ror->InputAt(1)->IsConstant()) {
4591 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4592 } else {
4593 locations->SetInAt(1, Location::RequiresRegister());
4594 locations->AddTemp(Location::RequiresRegister());
4595 locations->AddTemp(Location::RequiresRegister());
4596 }
4597 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4598 break;
4599 }
4600 default:
4601 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4602 }
4603}
4604
Roland Levillain22c49222016-03-18 14:04:28 +00004605void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004606 LocationSummary* locations = ror->GetLocations();
4607 Primitive::Type type = ror->GetResultType();
4608 switch (type) {
4609 case Primitive::kPrimInt: {
4610 HandleIntegerRotate(locations);
4611 break;
4612 }
4613 case Primitive::kPrimLong: {
Anton Kirilov6f644202017-02-27 18:29:45 +00004614 HandleLongRotate(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004615 break;
4616 }
4617 default:
4618 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00004619 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004620 }
4621}
4622
Calin Juravle9aec02f2014-11-18 23:06:35 +00004623void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
4624 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4625
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004626 LocationSummary* locations =
4627 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004628
4629 switch (op->GetResultType()) {
4630 case Primitive::kPrimInt: {
4631 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004632 if (op->InputAt(1)->IsConstant()) {
4633 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4634 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4635 } else {
4636 locations->SetInAt(1, Location::RequiresRegister());
4637 // Make the output overlap, as it will be used to hold the masked
4638 // second input.
4639 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4640 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004641 break;
4642 }
4643 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004644 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004645 if (op->InputAt(1)->IsConstant()) {
4646 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
4647 // For simplicity, use kOutputOverlap even though we only require that low registers
4648 // don't clash with high registers which the register allocator currently guarantees.
4649 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4650 } else {
4651 locations->SetInAt(1, Location::RequiresRegister());
4652 locations->AddTemp(Location::RequiresRegister());
4653 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4654 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004655 break;
4656 }
4657 default:
4658 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
4659 }
4660}
4661
4662void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
4663 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
4664
4665 LocationSummary* locations = op->GetLocations();
4666 Location out = locations->Out();
4667 Location first = locations->InAt(0);
4668 Location second = locations->InAt(1);
4669
4670 Primitive::Type type = op->GetResultType();
4671 switch (type) {
4672 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004673 Register out_reg = out.AsRegister<Register>();
4674 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004675 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004676 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00004677 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00004678 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00004679 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004680 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004681 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004682 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004683 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01004684 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00004685 }
4686 } else {
4687 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004688 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00004689 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00004690 __ Mov(out_reg, first_reg);
4691 } else if (op->IsShl()) {
4692 __ Lsl(out_reg, first_reg, shift_value);
4693 } else if (op->IsShr()) {
4694 __ Asr(out_reg, first_reg, shift_value);
4695 } else {
4696 __ Lsr(out_reg, first_reg, shift_value);
4697 }
4698 }
4699 break;
4700 }
4701 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004702 Register o_h = out.AsRegisterPairHigh<Register>();
4703 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004704
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004705 Register high = first.AsRegisterPairHigh<Register>();
4706 Register low = first.AsRegisterPairLow<Register>();
4707
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004708 if (second.IsRegister()) {
4709 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00004710
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004711 Register second_reg = second.AsRegister<Register>();
4712
4713 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004714 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004715 // Shift the high part
4716 __ Lsl(o_h, high, o_l);
4717 // Shift the low part and `or` what overflew on the high part
4718 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
4719 __ Lsr(temp, low, temp);
4720 __ orr(o_h, o_h, ShifterOperand(temp));
4721 // If the shift is > 32 bits, override the high part
4722 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
4723 __ it(PL);
4724 __ Lsl(o_h, low, temp, PL);
4725 // Shift the low part
4726 __ Lsl(o_l, low, o_l);
4727 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004728 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004729 // Shift the low part
4730 __ Lsr(o_l, low, o_h);
4731 // Shift the high part and `or` what underflew on the low part
4732 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4733 __ Lsl(temp, high, temp);
4734 __ orr(o_l, o_l, ShifterOperand(temp));
4735 // If the shift is > 32 bits, override the low part
4736 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4737 __ it(PL);
4738 __ Asr(o_l, high, temp, PL);
4739 // Shift the high part
4740 __ Asr(o_h, high, o_h);
4741 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004742 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004743 // same as Shr except we use `Lsr`s and not `Asr`s
4744 __ Lsr(o_l, low, o_h);
4745 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
4746 __ Lsl(temp, high, temp);
4747 __ orr(o_l, o_l, ShifterOperand(temp));
4748 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
4749 __ it(PL);
4750 __ Lsr(o_l, high, temp, PL);
4751 __ Lsr(o_h, high, o_h);
4752 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004753 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004754 // Register allocator doesn't create partial overlap.
4755 DCHECK_NE(o_l, high);
4756 DCHECK_NE(o_h, low);
4757 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00004758 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004759 if (shift_value > 32) {
4760 if (op->IsShl()) {
4761 __ Lsl(o_h, low, shift_value - 32);
4762 __ LoadImmediate(o_l, 0);
4763 } else if (op->IsShr()) {
4764 __ Asr(o_l, high, shift_value - 32);
4765 __ Asr(o_h, high, 31);
4766 } else {
4767 __ Lsr(o_l, high, shift_value - 32);
4768 __ LoadImmediate(o_h, 0);
4769 }
4770 } else if (shift_value == 32) {
4771 if (op->IsShl()) {
4772 __ mov(o_h, ShifterOperand(low));
4773 __ LoadImmediate(o_l, 0);
4774 } else if (op->IsShr()) {
4775 __ mov(o_l, ShifterOperand(high));
4776 __ Asr(o_h, high, 31);
4777 } else {
4778 __ mov(o_l, ShifterOperand(high));
4779 __ LoadImmediate(o_h, 0);
4780 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00004781 } else if (shift_value == 1) {
4782 if (op->IsShl()) {
4783 __ Lsls(o_l, low, 1);
4784 __ adc(o_h, high, ShifterOperand(high));
4785 } else if (op->IsShr()) {
4786 __ Asrs(o_h, high, 1);
4787 __ Rrx(o_l, low);
4788 } else {
4789 __ Lsrs(o_h, high, 1);
4790 __ Rrx(o_l, low);
4791 }
4792 } else {
4793 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004794 if (op->IsShl()) {
4795 __ Lsl(o_h, high, shift_value);
4796 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
4797 __ Lsl(o_l, low, shift_value);
4798 } else if (op->IsShr()) {
4799 __ Lsr(o_l, low, shift_value);
4800 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4801 __ Asr(o_h, high, shift_value);
4802 } else {
4803 __ Lsr(o_l, low, shift_value);
4804 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
4805 __ Lsr(o_h, high, shift_value);
4806 }
4807 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004808 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00004809 break;
4810 }
4811 default:
4812 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00004813 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00004814 }
4815}
4816
4817void LocationsBuilderARM::VisitShl(HShl* shl) {
4818 HandleShift(shl);
4819}
4820
4821void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
4822 HandleShift(shl);
4823}
4824
4825void LocationsBuilderARM::VisitShr(HShr* shr) {
4826 HandleShift(shr);
4827}
4828
4829void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
4830 HandleShift(shr);
4831}
4832
4833void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
4834 HandleShift(ushr);
4835}
4836
4837void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
4838 HandleShift(ushr);
4839}
4840
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004841void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004842 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004843 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00004844 if (instruction->IsStringAlloc()) {
4845 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4846 } else {
4847 InvokeRuntimeCallingConvention calling_convention;
4848 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00004849 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004850 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004851}
4852
4853void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004854 // Note: if heap poisoning is enabled, the entry point takes cares
4855 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004856 if (instruction->IsStringAlloc()) {
4857 // String is allocated through StringFactory. Call NewEmptyString entry point.
4858 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004859 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004860 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
4861 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
4862 __ blx(LR);
4863 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4864 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004865 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00004866 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00004867 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004868}
4869
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004870void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
4871 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004872 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004873 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004874 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004875 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4876 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004877}
4878
4879void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004880 // Note: if heap poisoning is enabled, the entry point takes cares
4881 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004882 QuickEntrypointEnum entrypoint =
4883 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
4884 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00004885 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00004886 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004887}
4888
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004889void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004890 LocationSummary* locations =
4891 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004892 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4893 if (location.IsStackSlot()) {
4894 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4895 } else if (location.IsDoubleStackSlot()) {
4896 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004897 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004898 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004899}
4900
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004901void InstructionCodeGeneratorARM::VisitParameterValue(
4902 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004903 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004904}
4905
4906void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
4907 LocationSummary* locations =
4908 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4909 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4910}
4911
4912void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
4913 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004914}
4915
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004916void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004917 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004918 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004919 locations->SetInAt(0, Location::RequiresRegister());
4920 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004921}
4922
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004923void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
4924 LocationSummary* locations = not_->GetLocations();
4925 Location out = locations->Out();
4926 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004927 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004928 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004929 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004930 break;
4931
4932 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004933 __ mvn(out.AsRegisterPairLow<Register>(),
4934 ShifterOperand(in.AsRegisterPairLow<Register>()));
4935 __ mvn(out.AsRegisterPairHigh<Register>(),
4936 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004937 break;
4938
4939 default:
4940 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4941 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004942}
4943
David Brazdil66d126e2015-04-03 16:02:44 +01004944void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
4945 LocationSummary* locations =
4946 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4947 locations->SetInAt(0, Location::RequiresRegister());
4948 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4949}
4950
4951void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004952 LocationSummary* locations = bool_not->GetLocations();
4953 Location out = locations->Out();
4954 Location in = locations->InAt(0);
4955 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
4956}
4957
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004958void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004959 LocationSummary* locations =
4960 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004961 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004962 case Primitive::kPrimBoolean:
4963 case Primitive::kPrimByte:
4964 case Primitive::kPrimShort:
4965 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004966 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004967 case Primitive::kPrimLong: {
4968 locations->SetInAt(0, Location::RequiresRegister());
4969 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004970 // Output overlaps because it is written before doing the low comparison.
4971 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00004972 break;
4973 }
4974 case Primitive::kPrimFloat:
4975 case Primitive::kPrimDouble: {
4976 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01004977 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00004978 locations->SetOut(Location::RequiresRegister());
4979 break;
4980 }
4981 default:
4982 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4983 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004984}
4985
4986void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004987 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004988 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004989 Location left = locations->InAt(0);
4990 Location right = locations->InAt(1);
4991
Vladimir Markocf93a5c2015-06-16 11:33:24 +00004992 Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00004993 Label* final_label = codegen_->GetFinalLabel(compare, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00004994 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00004995 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00004996 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004997 case Primitive::kPrimBoolean:
4998 case Primitive::kPrimByte:
4999 case Primitive::kPrimShort:
5000 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08005001 case Primitive::kPrimInt: {
5002 __ LoadImmediate(out, 0);
5003 __ cmp(left.AsRegister<Register>(),
5004 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
5005 less_cond = LT;
5006 break;
5007 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005008 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005009 __ cmp(left.AsRegisterPairHigh<Register>(),
5010 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005011 __ b(&less, LT);
5012 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01005013 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00005014 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005015 __ cmp(left.AsRegisterPairLow<Register>(),
5016 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005017 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00005018 break;
5019 }
5020 case Primitive::kPrimFloat:
5021 case Primitive::kPrimDouble: {
5022 __ LoadImmediate(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005023 GenerateVcmp(compare, codegen_);
Calin Juravleddb7df22014-11-25 20:56:51 +00005024 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005025 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005026 break;
5027 }
5028 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005029 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00005030 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005031 }
Aart Bika19616e2016-02-01 18:57:58 -08005032
Anton Kirilov6f644202017-02-27 18:29:45 +00005033 __ b(final_label, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00005034 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00005035
5036 __ Bind(&greater);
5037 __ LoadImmediate(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005038 __ b(final_label);
Calin Juravleddb7df22014-11-25 20:56:51 +00005039
5040 __ Bind(&less);
5041 __ LoadImmediate(out, -1);
5042
Anton Kirilov6f644202017-02-27 18:29:45 +00005043 if (done.IsLinked()) {
5044 __ Bind(&done);
5045 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005046}
5047
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005048void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005049 LocationSummary* locations =
5050 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005051 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005052 locations->SetInAt(i, Location::Any());
5053 }
5054 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005055}
5056
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005057void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005058 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005059}
5060
Roland Levillainc9285912015-12-18 10:38:42 +00005061void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
5062 // TODO (ported from quick): revisit ARM barrier kinds.
5063 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00005064 switch (kind) {
5065 case MemBarrierKind::kAnyStore:
5066 case MemBarrierKind::kLoadAny:
5067 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005068 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00005069 break;
5070 }
5071 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005072 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00005073 break;
5074 }
5075 default:
5076 LOG(FATAL) << "Unexpected memory barrier " << kind;
5077 }
Kenny Root1d8199d2015-06-02 11:01:10 -07005078 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00005079}
5080
5081void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
5082 uint32_t offset,
5083 Register out_lo,
5084 Register out_hi) {
5085 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005086 // Ensure `out_lo` is different from `addr`, so that loading
5087 // `offset` into `out_lo` does not clutter `addr`.
5088 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00005089 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005090 __ add(IP, addr, ShifterOperand(out_lo));
5091 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005092 }
5093 __ ldrexd(out_lo, out_hi, addr);
5094}
5095
5096void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
5097 uint32_t offset,
5098 Register value_lo,
5099 Register value_hi,
5100 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00005101 Register temp2,
5102 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005103 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00005104 if (offset != 0) {
5105 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005106 __ add(IP, addr, ShifterOperand(temp1));
5107 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005108 }
5109 __ Bind(&fail);
5110 // We need a load followed by store. (The address used in a STREX instruction must
5111 // be the same as the address in the most recently executed LDREX instruction.)
5112 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00005113 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005114 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005115 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00005116}
5117
5118void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5119 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5120
Nicolas Geoffray39468442014-09-02 15:17:15 +01005121 LocationSummary* locations =
5122 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005123 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005124
Calin Juravle52c48962014-12-16 17:02:57 +00005125 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005126 if (Primitive::IsFloatingPointType(field_type)) {
5127 locations->SetInAt(1, Location::RequiresFpuRegister());
5128 } else {
5129 locations->SetInAt(1, Location::RequiresRegister());
5130 }
5131
Calin Juravle52c48962014-12-16 17:02:57 +00005132 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00005133 bool generate_volatile = field_info.IsVolatile()
5134 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005135 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01005136 bool needs_write_barrier =
5137 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005138 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00005139 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01005140 if (needs_write_barrier) {
5141 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005142 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005143 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005144 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005145 // - registers need to be consecutive
5146 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005147 // We don't test for ARM yet, and the assertion makes sure that we
5148 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005149 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5150
5151 locations->AddTemp(Location::RequiresRegister());
5152 locations->AddTemp(Location::RequiresRegister());
5153 if (field_type == Primitive::kPrimDouble) {
5154 // For doubles we need two more registers to copy the value.
5155 locations->AddTemp(Location::RegisterLocation(R2));
5156 locations->AddTemp(Location::RegisterLocation(R3));
5157 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005158 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005159}
5160
Calin Juravle52c48962014-12-16 17:02:57 +00005161void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005162 const FieldInfo& field_info,
5163 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005164 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5165
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005166 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00005167 Register base = locations->InAt(0).AsRegister<Register>();
5168 Location value = locations->InAt(1);
5169
5170 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005171 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005172 Primitive::Type field_type = field_info.GetFieldType();
5173 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005174 bool needs_write_barrier =
5175 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005176
5177 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005178 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005179 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005180
5181 switch (field_type) {
5182 case Primitive::kPrimBoolean:
5183 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00005184 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005185 break;
5186 }
5187
5188 case Primitive::kPrimShort:
5189 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00005190 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005191 break;
5192 }
5193
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005194 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005195 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01005196 if (kPoisonHeapReferences && needs_write_barrier) {
5197 // Note that in the case where `value` is a null reference,
5198 // we do not enter this block, as a null reference does not
5199 // need poisoning.
5200 DCHECK_EQ(field_type, Primitive::kPrimNot);
5201 Register temp = locations->GetTemp(0).AsRegister<Register>();
5202 __ Mov(temp, value.AsRegister<Register>());
5203 __ PoisonHeapReference(temp);
5204 __ StoreToOffset(kStoreWord, temp, base, offset);
5205 } else {
5206 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
5207 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005208 break;
5209 }
5210
5211 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00005212 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005213 GenerateWideAtomicStore(base, offset,
5214 value.AsRegisterPairLow<Register>(),
5215 value.AsRegisterPairHigh<Register>(),
5216 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005217 locations->GetTemp(1).AsRegister<Register>(),
5218 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005219 } else {
5220 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005221 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005222 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005223 break;
5224 }
5225
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005226 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00005227 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005228 break;
5229 }
5230
5231 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005232 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005233 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005234 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
5235 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
5236
5237 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
5238
5239 GenerateWideAtomicStore(base, offset,
5240 value_reg_lo,
5241 value_reg_hi,
5242 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005243 locations->GetTemp(3).AsRegister<Register>(),
5244 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005245 } else {
5246 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005247 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005248 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005249 break;
5250 }
5251
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005252 case Primitive::kPrimVoid:
5253 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005254 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005255 }
Calin Juravle52c48962014-12-16 17:02:57 +00005256
Calin Juravle77520bc2015-01-12 18:45:46 +00005257 // Longs and doubles are handled in the switch.
5258 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
5259 codegen_->MaybeRecordImplicitNullCheck(instruction);
5260 }
5261
5262 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5263 Register temp = locations->GetTemp(0).AsRegister<Register>();
5264 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005265 codegen_->MarkGCCard(
5266 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005267 }
5268
Calin Juravle52c48962014-12-16 17:02:57 +00005269 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005270 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005271 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005272}
5273
Calin Juravle52c48962014-12-16 17:02:57 +00005274void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5275 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00005276
5277 bool object_field_get_with_read_barrier =
5278 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005279 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005280 new (GetGraph()->GetArena()) LocationSummary(instruction,
5281 object_field_get_with_read_barrier ?
5282 LocationSummary::kCallOnSlowPath :
5283 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005284 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005285 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005286 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005287 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00005288
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005289 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00005290 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005291 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00005292 // The output overlaps in case of volatile long: we don't want the
5293 // code generated by GenerateWideAtomicLoad to overwrite the
5294 // object's location. Likewise, in the case of an object field get
5295 // with read barriers enabled, we do not want the load to overwrite
5296 // the object's location, as we need it to emit the read barrier.
5297 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5298 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01005299
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005300 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5301 locations->SetOut(Location::RequiresFpuRegister());
5302 } else {
5303 locations->SetOut(Location::RequiresRegister(),
5304 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5305 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005306 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00005307 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005308 // - registers need to be consecutive
5309 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005310 // We don't test for ARM yet, and the assertion makes sure that we
5311 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005312 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5313 locations->AddTemp(Location::RequiresRegister());
5314 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00005315 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5316 // We need a temporary register for the read barrier marking slow
5317 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005318 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5319 !Runtime::Current()->UseJitCompilation()) {
5320 // If link-time thunks for the Baker read barrier are enabled, for AOT
5321 // loads we need a temporary only if the offset is too big.
5322 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5323 locations->AddTemp(Location::RequiresRegister());
5324 }
5325 // And we always need the reserved entrypoint register.
5326 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5327 } else {
5328 locations->AddTemp(Location::RequiresRegister());
5329 }
Calin Juravle52c48962014-12-16 17:02:57 +00005330 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005331}
5332
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005333Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5334 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
5335 << input->GetType();
5336 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5337 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5338 return Location::ConstantLocation(input->AsConstant());
5339 } else {
5340 return Location::RequiresFpuRegister();
5341 }
5342}
5343
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005344Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
5345 Opcode opcode) {
5346 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5347 if (constant->IsConstant() &&
5348 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5349 return Location::ConstantLocation(constant->AsConstant());
5350 }
5351 return Location::RequiresRegister();
5352}
5353
5354bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
5355 Opcode opcode) {
5356 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5357 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01005358 Opcode high_opcode = opcode;
5359 SetCc low_set_cc = kCcDontCare;
5360 switch (opcode) {
5361 case SUB:
5362 // Flip the operation to an ADD.
5363 value = -value;
5364 opcode = ADD;
5365 FALLTHROUGH_INTENDED;
5366 case ADD:
5367 if (Low32Bits(value) == 0u) {
5368 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5369 }
5370 high_opcode = ADC;
5371 low_set_cc = kCcSet;
5372 break;
5373 default:
5374 break;
5375 }
5376 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5377 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005378 } else {
5379 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5380 }
5381}
5382
Vladimir Marko59751a72016-08-05 14:37:27 +01005383bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
5384 Opcode opcode,
5385 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005386 ShifterOperand so;
5387 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01005388 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005389 return true;
5390 }
5391 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005392 uint32_t neg_value = 0;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005393 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005394 case AND: neg_opcode = BIC; neg_value = ~value; break;
5395 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5396 case ADD: neg_opcode = SUB; neg_value = -value; break;
5397 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5398 case SUB: neg_opcode = ADD; neg_value = -value; break;
5399 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5400 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005401 default:
5402 return false;
5403 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005404
5405 if (assembler->ShifterOperandCanHold(kNoRegister,
5406 kNoRegister,
5407 neg_opcode,
5408 neg_value,
5409 set_cc,
5410 &so)) {
5411 return true;
5412 }
5413
5414 return opcode == AND && IsPowerOfTwo(value + 1);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005415}
5416
Calin Juravle52c48962014-12-16 17:02:57 +00005417void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
5418 const FieldInfo& field_info) {
5419 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005420
Calin Juravle52c48962014-12-16 17:02:57 +00005421 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005422 Location base_loc = locations->InAt(0);
5423 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005424 Location out = locations->Out();
5425 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005426 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005427 Primitive::Type field_type = field_info.GetFieldType();
5428 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5429
5430 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00005431 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00005432 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005433 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005434
Roland Levillainc9285912015-12-18 10:38:42 +00005435 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00005436 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005437 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005438
Roland Levillainc9285912015-12-18 10:38:42 +00005439 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00005440 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005441 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005442
Roland Levillainc9285912015-12-18 10:38:42 +00005443 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00005444 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005445 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005446
5447 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00005448 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005449 break;
Roland Levillainc9285912015-12-18 10:38:42 +00005450
5451 case Primitive::kPrimNot: {
5452 // /* HeapReference<Object> */ out = *(base + offset)
5453 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5454 Location temp_loc = locations->GetTemp(0);
5455 // Note that a potential implicit null check is handled in this
5456 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
5457 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5458 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5459 if (is_volatile) {
5460 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5461 }
5462 } else {
5463 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
5464 codegen_->MaybeRecordImplicitNullCheck(instruction);
5465 if (is_volatile) {
5466 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5467 }
5468 // If read barriers are enabled, emit read barriers other than
5469 // Baker's using a slow path (and also unpoison the loaded
5470 // reference, if heap poisoning is enabled).
5471 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5472 }
5473 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005474 }
5475
Roland Levillainc9285912015-12-18 10:38:42 +00005476 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00005477 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005478 GenerateWideAtomicLoad(base, offset,
5479 out.AsRegisterPairLow<Register>(),
5480 out.AsRegisterPairHigh<Register>());
5481 } else {
5482 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
5483 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005484 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005485
Roland Levillainc9285912015-12-18 10:38:42 +00005486 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00005487 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005488 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005489
5490 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005491 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005492 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005493 Register lo = locations->GetTemp(0).AsRegister<Register>();
5494 Register hi = locations->GetTemp(1).AsRegister<Register>();
5495 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00005496 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005497 __ vmovdrr(out_reg, lo, hi);
5498 } else {
5499 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005500 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005501 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005502 break;
5503 }
5504
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005505 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005506 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005507 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005508 }
Calin Juravle52c48962014-12-16 17:02:57 +00005509
Roland Levillainc9285912015-12-18 10:38:42 +00005510 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5511 // Potential implicit null checks, in the case of reference or
5512 // double fields, are handled in the previous switch statement.
5513 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005514 codegen_->MaybeRecordImplicitNullCheck(instruction);
5515 }
5516
Calin Juravle52c48962014-12-16 17:02:57 +00005517 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005518 if (field_type == Primitive::kPrimNot) {
5519 // Memory barriers, in the case of references, are also handled
5520 // in the previous switch statement.
5521 } else {
5522 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5523 }
Roland Levillain4d027112015-07-01 15:41:14 +01005524 }
Calin Juravle52c48962014-12-16 17:02:57 +00005525}
5526
5527void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5528 HandleFieldSet(instruction, instruction->GetFieldInfo());
5529}
5530
5531void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005532 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005533}
5534
5535void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5536 HandleFieldGet(instruction, instruction->GetFieldInfo());
5537}
5538
5539void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5540 HandleFieldGet(instruction, instruction->GetFieldInfo());
5541}
5542
5543void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5544 HandleFieldGet(instruction, instruction->GetFieldInfo());
5545}
5546
5547void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5548 HandleFieldGet(instruction, instruction->GetFieldInfo());
5549}
5550
5551void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5552 HandleFieldSet(instruction, instruction->GetFieldInfo());
5553}
5554
5555void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005556 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005557}
5558
Calin Juravlee460d1d2015-09-29 04:52:17 +01005559void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
5560 HUnresolvedInstanceFieldGet* instruction) {
5561 FieldAccessCallingConventionARM calling_convention;
5562 codegen_->CreateUnresolvedFieldLocationSummary(
5563 instruction, instruction->GetFieldType(), calling_convention);
5564}
5565
5566void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
5567 HUnresolvedInstanceFieldGet* instruction) {
5568 FieldAccessCallingConventionARM calling_convention;
5569 codegen_->GenerateUnresolvedFieldAccess(instruction,
5570 instruction->GetFieldType(),
5571 instruction->GetFieldIndex(),
5572 instruction->GetDexPc(),
5573 calling_convention);
5574}
5575
5576void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
5577 HUnresolvedInstanceFieldSet* instruction) {
5578 FieldAccessCallingConventionARM calling_convention;
5579 codegen_->CreateUnresolvedFieldLocationSummary(
5580 instruction, instruction->GetFieldType(), calling_convention);
5581}
5582
5583void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
5584 HUnresolvedInstanceFieldSet* instruction) {
5585 FieldAccessCallingConventionARM calling_convention;
5586 codegen_->GenerateUnresolvedFieldAccess(instruction,
5587 instruction->GetFieldType(),
5588 instruction->GetFieldIndex(),
5589 instruction->GetDexPc(),
5590 calling_convention);
5591}
5592
5593void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
5594 HUnresolvedStaticFieldGet* instruction) {
5595 FieldAccessCallingConventionARM calling_convention;
5596 codegen_->CreateUnresolvedFieldLocationSummary(
5597 instruction, instruction->GetFieldType(), calling_convention);
5598}
5599
5600void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
5601 HUnresolvedStaticFieldGet* instruction) {
5602 FieldAccessCallingConventionARM calling_convention;
5603 codegen_->GenerateUnresolvedFieldAccess(instruction,
5604 instruction->GetFieldType(),
5605 instruction->GetFieldIndex(),
5606 instruction->GetDexPc(),
5607 calling_convention);
5608}
5609
5610void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
5611 HUnresolvedStaticFieldSet* instruction) {
5612 FieldAccessCallingConventionARM calling_convention;
5613 codegen_->CreateUnresolvedFieldLocationSummary(
5614 instruction, instruction->GetFieldType(), calling_convention);
5615}
5616
5617void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
5618 HUnresolvedStaticFieldSet* instruction) {
5619 FieldAccessCallingConventionARM calling_convention;
5620 codegen_->GenerateUnresolvedFieldAccess(instruction,
5621 instruction->GetFieldType(),
5622 instruction->GetFieldIndex(),
5623 instruction->GetDexPc(),
5624 calling_convention);
5625}
5626
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005627void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005628 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5629 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005630}
5631
Calin Juravle2ae48182016-03-16 14:05:09 +00005632void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
5633 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005634 return;
5635 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005636 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005637
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005638 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00005639 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005640}
5641
Calin Juravle2ae48182016-03-16 14:05:09 +00005642void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01005643 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005644 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005645
5646 LocationSummary* locations = instruction->GetLocations();
5647 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005648
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005649 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005650}
5651
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005652void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005653 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005654}
5655
Artem Serov6c916792016-07-11 14:02:34 +01005656static LoadOperandType GetLoadOperandType(Primitive::Type type) {
5657 switch (type) {
5658 case Primitive::kPrimNot:
5659 return kLoadWord;
5660 case Primitive::kPrimBoolean:
5661 return kLoadUnsignedByte;
5662 case Primitive::kPrimByte:
5663 return kLoadSignedByte;
5664 case Primitive::kPrimChar:
5665 return kLoadUnsignedHalfword;
5666 case Primitive::kPrimShort:
5667 return kLoadSignedHalfword;
5668 case Primitive::kPrimInt:
5669 return kLoadWord;
5670 case Primitive::kPrimLong:
5671 return kLoadWordPair;
5672 case Primitive::kPrimFloat:
5673 return kLoadSWord;
5674 case Primitive::kPrimDouble:
5675 return kLoadDWord;
5676 default:
5677 LOG(FATAL) << "Unreachable type " << type;
5678 UNREACHABLE();
5679 }
5680}
5681
5682static StoreOperandType GetStoreOperandType(Primitive::Type type) {
5683 switch (type) {
5684 case Primitive::kPrimNot:
5685 return kStoreWord;
5686 case Primitive::kPrimBoolean:
5687 case Primitive::kPrimByte:
5688 return kStoreByte;
5689 case Primitive::kPrimChar:
5690 case Primitive::kPrimShort:
5691 return kStoreHalfword;
5692 case Primitive::kPrimInt:
5693 return kStoreWord;
5694 case Primitive::kPrimLong:
5695 return kStoreWordPair;
5696 case Primitive::kPrimFloat:
5697 return kStoreSWord;
5698 case Primitive::kPrimDouble:
5699 return kStoreDWord;
5700 default:
5701 LOG(FATAL) << "Unreachable type " << type;
5702 UNREACHABLE();
5703 }
5704}
5705
5706void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
5707 Location out_loc,
5708 Register base,
5709 Register reg_offset,
5710 Condition cond) {
5711 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5712 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5713
5714 switch (type) {
5715 case Primitive::kPrimByte:
5716 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
5717 break;
5718 case Primitive::kPrimBoolean:
5719 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
5720 break;
5721 case Primitive::kPrimShort:
5722 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
5723 break;
5724 case Primitive::kPrimChar:
5725 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
5726 break;
5727 case Primitive::kPrimNot:
5728 case Primitive::kPrimInt:
5729 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
5730 break;
5731 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
5732 case Primitive::kPrimLong:
5733 case Primitive::kPrimFloat:
5734 case Primitive::kPrimDouble:
5735 default:
5736 LOG(FATAL) << "Unreachable type " << type;
5737 UNREACHABLE();
5738 }
5739}
5740
5741void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
5742 Location loc,
5743 Register base,
5744 Register reg_offset,
5745 Condition cond) {
5746 uint32_t shift_count = Primitive::ComponentSizeShift(type);
5747 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
5748
5749 switch (type) {
5750 case Primitive::kPrimByte:
5751 case Primitive::kPrimBoolean:
5752 __ strb(loc.AsRegister<Register>(), mem_address, cond);
5753 break;
5754 case Primitive::kPrimShort:
5755 case Primitive::kPrimChar:
5756 __ strh(loc.AsRegister<Register>(), mem_address, cond);
5757 break;
5758 case Primitive::kPrimNot:
5759 case Primitive::kPrimInt:
5760 __ str(loc.AsRegister<Register>(), mem_address, cond);
5761 break;
5762 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
5763 case Primitive::kPrimLong:
5764 case Primitive::kPrimFloat:
5765 case Primitive::kPrimDouble:
5766 default:
5767 LOG(FATAL) << "Unreachable type " << type;
5768 UNREACHABLE();
5769 }
5770}
5771
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005772void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005773 bool object_array_get_with_read_barrier =
5774 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005775 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005776 new (GetGraph()->GetArena()) LocationSummary(instruction,
5777 object_array_get_with_read_barrier ?
5778 LocationSummary::kCallOnSlowPath :
5779 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005780 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005781 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005782 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005783 locations->SetInAt(0, Location::RequiresRegister());
5784 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005785 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5786 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5787 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00005788 // The output overlaps in the case of an object array get with
5789 // read barriers enabled: we do not want the move to overwrite the
5790 // array's location, as we need it to emit the read barrier.
5791 locations->SetOut(
5792 Location::RequiresRegister(),
5793 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005794 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005795 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5796 // We need a temporary register for the read barrier marking slow
5797 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
5798 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5799 !Runtime::Current()->UseJitCompilation() &&
5800 instruction->GetIndex()->IsConstant()) {
5801 // Array loads with constant index are treated as field loads.
5802 // If link-time thunks for the Baker read barrier are enabled, for AOT
5803 // constant index loads we need a temporary only if the offset is too big.
5804 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
5805 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
5806 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
5807 if (offset >= kReferenceLoadMinFarOffset) {
5808 locations->AddTemp(Location::RequiresRegister());
5809 }
5810 // And we always need the reserved entrypoint register.
5811 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5812 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
5813 !Runtime::Current()->UseJitCompilation() &&
5814 !instruction->GetIndex()->IsConstant()) {
5815 // We need a non-scratch temporary for the array data pointer.
5816 locations->AddTemp(Location::RequiresRegister());
5817 // And we always need the reserved entrypoint register.
5818 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5819 } else {
5820 locations->AddTemp(Location::RequiresRegister());
5821 }
5822 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5823 // Also need a temporary for String compression feature.
Roland Levillainc9285912015-12-18 10:38:42 +00005824 locations->AddTemp(Location::RequiresRegister());
5825 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005826}
5827
5828void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
5829 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005830 Location obj_loc = locations->InAt(0);
5831 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005832 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00005833 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005834 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00005835 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07005836 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
5837 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01005838 HInstruction* array_instr = instruction->GetArray();
5839 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01005840
Roland Levillain4d027112015-07-01 15:41:14 +01005841 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01005842 case Primitive::kPrimBoolean:
5843 case Primitive::kPrimByte:
5844 case Primitive::kPrimShort:
5845 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00005846 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005847 Register length;
5848 if (maybe_compressed_char_at) {
5849 length = locations->GetTemp(0).AsRegister<Register>();
5850 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5851 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
5852 codegen_->MaybeRecordImplicitNullCheck(instruction);
5853 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005854 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01005855 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07005856 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07005857 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005858 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005859 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5860 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5861 "Expecting 0=compressed, 1=uncompressed");
5862 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005863 __ LoadFromOffset(kLoadUnsignedByte,
5864 out_loc.AsRegister<Register>(),
5865 obj,
5866 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00005867 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07005868 __ Bind(&uncompressed_load);
5869 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
5870 out_loc.AsRegister<Register>(),
5871 obj,
5872 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005873 if (done.IsLinked()) {
5874 __ Bind(&done);
5875 }
jessicahandojo05765752016-09-09 19:01:32 -07005876 } else {
5877 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01005878
jessicahandojo05765752016-09-09 19:01:32 -07005879 LoadOperandType load_type = GetLoadOperandType(type);
5880 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
5881 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005882 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005883 Register temp = IP;
5884
5885 if (has_intermediate_address) {
5886 // We do not need to compute the intermediate address from the array: the
5887 // input instruction has done it already. See the comment in
5888 // `TryExtractArrayAccessAddress()`.
5889 if (kIsDebugBuild) {
5890 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5891 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5892 }
5893 temp = obj;
5894 } else {
5895 __ add(temp, obj, ShifterOperand(data_offset));
5896 }
jessicahandojo05765752016-09-09 19:01:32 -07005897 if (maybe_compressed_char_at) {
5898 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005899 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005900 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
5901 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5902 "Expecting 0=compressed, 1=uncompressed");
5903 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07005904 __ ldrb(out_loc.AsRegister<Register>(),
5905 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00005906 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07005907 __ Bind(&uncompressed_load);
5908 __ ldrh(out_loc.AsRegister<Register>(),
5909 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00005910 if (done.IsLinked()) {
5911 __ Bind(&done);
5912 }
jessicahandojo05765752016-09-09 19:01:32 -07005913 } else {
5914 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
5915 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005916 }
5917 break;
5918 }
5919
Roland Levillainc9285912015-12-18 10:38:42 +00005920 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00005921 // The read barrier instrumentation of object ArrayGet
5922 // instructions does not support the HIntermediateAddress
5923 // instruction.
5924 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
5925
Roland Levillainc9285912015-12-18 10:38:42 +00005926 static_assert(
5927 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5928 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00005929 // /* HeapReference<Object> */ out =
5930 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5931 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5932 Location temp = locations->GetTemp(0);
5933 // Note that a potential implicit null check is handled in this
5934 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005935 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
5936 if (index.IsConstant()) {
5937 // Array load with a constant index can be treated as a field load.
5938 data_offset += helpers::Int32ConstantFrom(index) << Primitive::ComponentSizeShift(type);
5939 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
5940 out_loc,
5941 obj,
5942 data_offset,
5943 locations->GetTemp(0),
5944 /* needs_null_check */ false);
5945 } else {
5946 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5947 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
5948 }
Roland Levillainc9285912015-12-18 10:38:42 +00005949 } else {
5950 Register out = out_loc.AsRegister<Register>();
5951 if (index.IsConstant()) {
5952 size_t offset =
5953 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5954 __ LoadFromOffset(kLoadWord, out, obj, offset);
5955 codegen_->MaybeRecordImplicitNullCheck(instruction);
5956 // If read barriers are enabled, emit read barriers other than
5957 // Baker's using a slow path (and also unpoison the loaded
5958 // reference, if heap poisoning is enabled).
5959 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5960 } else {
Artem Serov328429f2016-07-06 16:23:04 +01005961 Register temp = IP;
5962
5963 if (has_intermediate_address) {
5964 // We do not need to compute the intermediate address from the array: the
5965 // input instruction has done it already. See the comment in
5966 // `TryExtractArrayAccessAddress()`.
5967 if (kIsDebugBuild) {
5968 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
5969 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
5970 }
5971 temp = obj;
5972 } else {
5973 __ add(temp, obj, ShifterOperand(data_offset));
5974 }
5975 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01005976
Roland Levillainc9285912015-12-18 10:38:42 +00005977 codegen_->MaybeRecordImplicitNullCheck(instruction);
5978 // If read barriers are enabled, emit read barriers other than
5979 // Baker's using a slow path (and also unpoison the loaded
5980 // reference, if heap poisoning is enabled).
5981 codegen_->MaybeGenerateReadBarrierSlow(
5982 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5983 }
5984 }
5985 break;
5986 }
5987
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005988 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005989 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005990 size_t offset =
5991 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00005992 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005993 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005994 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00005995 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005996 }
5997 break;
5998 }
5999
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006000 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00006001 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006002 if (index.IsConstant()) {
6003 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006004 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006005 } else {
6006 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00006007 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006008 }
6009 break;
6010 }
6011
6012 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00006013 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006014 if (index.IsConstant()) {
6015 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006016 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006017 } else {
6018 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00006019 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006020 }
6021 break;
6022 }
6023
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006024 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01006025 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006026 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006027 }
Roland Levillain4d027112015-07-01 15:41:14 +01006028
6029 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00006030 // Potential implicit null checks, in the case of reference
6031 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07006032 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00006033 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01006034 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006035}
6036
6037void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006038 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006039
6040 bool needs_write_barrier =
6041 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00006042 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006043
Nicolas Geoffray39468442014-09-02 15:17:15 +01006044 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006045 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01006046 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00006047 LocationSummary::kCallOnSlowPath :
6048 LocationSummary::kNoCall);
6049
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006050 locations->SetInAt(0, Location::RequiresRegister());
6051 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6052 if (Primitive::IsFloatingPointType(value_type)) {
6053 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006054 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006055 locations->SetInAt(2, Location::RequiresRegister());
6056 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006057 if (needs_write_barrier) {
6058 // Temporary registers for the write barrier.
6059 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006060 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006061 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006062}
6063
6064void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
6065 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006066 Location array_loc = locations->InAt(0);
6067 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006068 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006069 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00006070 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006071 bool needs_write_barrier =
6072 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01006073 uint32_t data_offset =
6074 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6075 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01006076 HInstruction* array_instr = instruction->GetArray();
6077 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006078
6079 switch (value_type) {
6080 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01006081 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006082 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01006083 case Primitive::kPrimChar:
6084 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006085 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01006086 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
6087 uint32_t full_offset =
6088 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6089 StoreOperandType store_type = GetStoreOperandType(value_type);
6090 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006091 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006092 Register temp = IP;
6093
6094 if (has_intermediate_address) {
6095 // We do not need to compute the intermediate address from the array: the
6096 // input instruction has done it already. See the comment in
6097 // `TryExtractArrayAccessAddress()`.
6098 if (kIsDebugBuild) {
6099 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6100 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
6101 }
6102 temp = array;
6103 } else {
6104 __ add(temp, array, ShifterOperand(data_offset));
6105 }
Artem Serov6c916792016-07-11 14:02:34 +01006106 codegen_->StoreToShiftedRegOffset(value_type,
6107 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01006108 temp,
Artem Serov6c916792016-07-11 14:02:34 +01006109 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006110 }
6111 break;
6112 }
6113
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006114 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00006115 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01006116 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6117 // See the comment in instruction_simplifier_shared.cc.
6118 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006119
6120 if (instruction->InputAt(2)->IsNullConstant()) {
6121 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006122 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006123 size_t offset =
6124 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01006125 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006126 } else {
6127 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006128 __ add(IP, array, ShifterOperand(data_offset));
6129 codegen_->StoreToShiftedRegOffset(value_type,
6130 value_loc,
6131 IP,
6132 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006133 }
Roland Levillain1407ee72016-01-08 15:56:19 +00006134 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00006135 DCHECK(!needs_write_barrier);
6136 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006137 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006138 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006139
6140 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01006141 Location temp1_loc = locations->GetTemp(0);
6142 Register temp1 = temp1_loc.AsRegister<Register>();
6143 Location temp2_loc = locations->GetTemp(1);
6144 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006145 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6146 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6147 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6148 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006149 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006150 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006151
Roland Levillain3b359c72015-11-17 19:35:12 +00006152 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006153 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
6154 codegen_->AddSlowPath(slow_path);
6155 if (instruction->GetValueCanBeNull()) {
6156 Label non_zero;
6157 __ CompareAndBranchIfNonZero(value, &non_zero);
6158 if (index.IsConstant()) {
6159 size_t offset =
6160 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6161 __ StoreToOffset(kStoreWord, value, array, offset);
6162 } else {
6163 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006164 __ add(IP, array, ShifterOperand(data_offset));
6165 codegen_->StoreToShiftedRegOffset(value_type,
6166 value_loc,
6167 IP,
6168 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006169 }
6170 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006171 __ b(final_label);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006172 __ Bind(&non_zero);
6173 }
6174
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006175 // Note that when read barriers are enabled, the type checks
6176 // are performed without read barriers. This is fine, even in
6177 // the case where a class object is in the from-space after
6178 // the flip, as a comparison involving such a type would not
6179 // produce a false positive; it may of course produce a false
6180 // negative, in which case we would take the ArraySet slow
6181 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006182
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006183 // /* HeapReference<Class> */ temp1 = array->klass_
6184 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
6185 codegen_->MaybeRecordImplicitNullCheck(instruction);
6186 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01006187
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006188 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6189 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6190 // /* HeapReference<Class> */ temp2 = value->klass_
6191 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
6192 // If heap poisoning is enabled, no need to unpoison `temp1`
6193 // nor `temp2`, as we are comparing two poisoned references.
6194 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01006195
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006196 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6197 Label do_put;
6198 __ b(&do_put, EQ);
6199 // If heap poisoning is enabled, the `temp1` reference has
6200 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00006201 __ MaybeUnpoisonHeapReference(temp1);
6202
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006203 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6204 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6205 // If heap poisoning is enabled, no need to unpoison
6206 // `temp1`, as we are comparing against null below.
6207 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
6208 __ Bind(&do_put);
6209 } else {
6210 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006211 }
6212 }
6213
Artem Serov6c916792016-07-11 14:02:34 +01006214 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006215 if (kPoisonHeapReferences) {
6216 // Note that in the case where `value` is a null reference,
6217 // we do not enter this block, as a null reference does not
6218 // need poisoning.
6219 DCHECK_EQ(value_type, Primitive::kPrimNot);
6220 __ Mov(temp1, value);
6221 __ PoisonHeapReference(temp1);
6222 source = temp1;
6223 }
6224
6225 if (index.IsConstant()) {
6226 size_t offset =
6227 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6228 __ StoreToOffset(kStoreWord, source, array, offset);
6229 } else {
6230 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006231
6232 __ add(IP, array, ShifterOperand(data_offset));
6233 codegen_->StoreToShiftedRegOffset(value_type,
6234 Location::RegisterLocation(source),
6235 IP,
6236 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006237 }
6238
Roland Levillain3b359c72015-11-17 19:35:12 +00006239 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006240 codegen_->MaybeRecordImplicitNullCheck(instruction);
6241 }
6242
6243 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6244
6245 if (done.IsLinked()) {
6246 __ Bind(&done);
6247 }
6248
6249 if (slow_path != nullptr) {
6250 __ Bind(slow_path->GetExitLabel());
6251 }
6252
6253 break;
6254 }
6255
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006256 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006257 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006258 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006259 size_t offset =
6260 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006261 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006262 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006263 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006264 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006265 }
6266 break;
6267 }
6268
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006269 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006270 Location value = locations->InAt(2);
6271 DCHECK(value.IsFpuRegister());
6272 if (index.IsConstant()) {
6273 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006274 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006275 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006276 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006277 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
6278 }
6279 break;
6280 }
6281
6282 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006283 Location value = locations->InAt(2);
6284 DCHECK(value.IsFpuRegisterPair());
6285 if (index.IsConstant()) {
6286 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006287 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006288 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006289 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006290 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
6291 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006292
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006293 break;
6294 }
6295
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006296 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006297 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006298 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006299 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006300
Roland Levillain80e67092016-01-08 16:04:55 +00006301 // Objects are handled in the switch.
6302 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006303 codegen_->MaybeRecordImplicitNullCheck(instruction);
6304 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006305}
6306
6307void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006308 LocationSummary* locations =
6309 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006310 locations->SetInAt(0, Location::RequiresRegister());
6311 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006312}
6313
6314void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
6315 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006316 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006317 Register obj = locations->InAt(0).AsRegister<Register>();
6318 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006319 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00006320 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07006321 // Mask out compression flag from String's array length.
6322 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006323 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07006324 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006325}
6326
Artem Serov328429f2016-07-06 16:23:04 +01006327void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01006328 LocationSummary* locations =
6329 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6330
6331 locations->SetInAt(0, Location::RequiresRegister());
6332 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6333 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6334}
6335
6336void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6337 LocationSummary* locations = instruction->GetLocations();
6338 Location out = locations->Out();
6339 Location first = locations->InAt(0);
6340 Location second = locations->InAt(1);
6341
Artem Serov328429f2016-07-06 16:23:04 +01006342 if (second.IsRegister()) {
6343 __ add(out.AsRegister<Register>(),
6344 first.AsRegister<Register>(),
6345 ShifterOperand(second.AsRegister<Register>()));
6346 } else {
6347 __ AddConstant(out.AsRegister<Register>(),
6348 first.AsRegister<Register>(),
6349 second.GetConstant()->AsIntConstant()->GetValue());
6350 }
6351}
6352
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006353void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006354 RegisterSet caller_saves = RegisterSet::Empty();
6355 InvokeRuntimeCallingConvention calling_convention;
6356 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6357 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6358 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006359
6360 HInstruction* index = instruction->InputAt(0);
6361 HInstruction* length = instruction->InputAt(1);
6362 // If both index and length are constants we can statically check the bounds. But if at least one
6363 // of them is not encodable ArmEncodableConstantOrRegister will create
6364 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6365 // locations.
6366 bool both_const = index->IsConstant() && length->IsConstant();
6367 locations->SetInAt(0, both_const
6368 ? Location::ConstantLocation(index->AsConstant())
6369 : ArmEncodableConstantOrRegister(index, CMP));
6370 locations->SetInAt(1, both_const
6371 ? Location::ConstantLocation(length->AsConstant())
6372 : ArmEncodableConstantOrRegister(length, CMP));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006373}
6374
6375void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
6376 LocationSummary* locations = instruction->GetLocations();
Artem Serov2dd053d2017-03-08 14:54:06 +00006377 Location index_loc = locations->InAt(0);
6378 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006379
Artem Serov2dd053d2017-03-08 14:54:06 +00006380 if (length_loc.IsConstant()) {
6381 int32_t length = helpers::Int32ConstantFrom(length_loc);
6382 if (index_loc.IsConstant()) {
6383 // BCE will remove the bounds check if we are guaranteed to pass.
6384 int32_t index = helpers::Int32ConstantFrom(index_loc);
6385 if (index < 0 || index >= length) {
6386 SlowPathCodeARM* slow_path =
6387 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6388 codegen_->AddSlowPath(slow_path);
6389 __ b(slow_path->GetEntryLabel());
6390 } else {
6391 // Some optimization after BCE may have generated this, and we should not
6392 // generate a bounds check if it is a valid range.
6393 }
6394 return;
6395 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006396
Artem Serov2dd053d2017-03-08 14:54:06 +00006397 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6398 __ cmp(index_loc.AsRegister<Register>(), ShifterOperand(length));
6399 codegen_->AddSlowPath(slow_path);
6400 __ b(slow_path->GetEntryLabel(), HS);
6401 } else {
6402 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6403 if (index_loc.IsConstant()) {
6404 int32_t index = helpers::Int32ConstantFrom(index_loc);
6405 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index));
6406 } else {
6407 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index_loc.AsRegister<Register>()));
6408 }
6409 codegen_->AddSlowPath(slow_path);
6410 __ b(slow_path->GetEntryLabel(), LS);
6411 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006412}
6413
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006414void CodeGeneratorARM::MarkGCCard(Register temp,
6415 Register card,
6416 Register object,
6417 Register value,
6418 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006419 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006420 if (can_be_null) {
6421 __ CompareAndBranchIfZero(value, &is_null);
6422 }
Andreas Gampe542451c2016-07-26 09:02:02 -07006423 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006424 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
6425 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006426 if (can_be_null) {
6427 __ Bind(&is_null);
6428 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006429}
6430
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006431void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006432 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006433}
6434
6435void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006436 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6437}
6438
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006439void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006440 LocationSummary* locations =
6441 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006442 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006443}
6444
6445void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006446 HBasicBlock* block = instruction->GetBlock();
6447 if (block->GetLoopInformation() != nullptr) {
6448 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6449 // The back edge will generate the suspend check.
6450 return;
6451 }
6452 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6453 // The goto will generate the suspend check.
6454 return;
6455 }
6456 GenerateSuspendCheck(instruction, nullptr);
6457}
6458
6459void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
6460 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006461 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006462 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
6463 if (slow_path == nullptr) {
6464 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
6465 instruction->SetSlowPath(slow_path);
6466 codegen_->AddSlowPath(slow_path);
6467 if (successor != nullptr) {
6468 DCHECK(successor->IsLoopHeader());
6469 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6470 }
6471 } else {
6472 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6473 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006474
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00006475 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006476 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006477 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006478 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006479 __ Bind(slow_path->GetReturnLabel());
6480 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006481 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006482 __ b(slow_path->GetEntryLabel());
6483 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006484}
6485
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006486ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
6487 return codegen_->GetAssembler();
6488}
6489
6490void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006491 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006492 Location source = move->GetSource();
6493 Location destination = move->GetDestination();
6494
6495 if (source.IsRegister()) {
6496 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006497 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006498 } else if (destination.IsFpuRegister()) {
6499 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006500 } else {
6501 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006502 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006503 SP, destination.GetStackIndex());
6504 }
6505 } else if (source.IsStackSlot()) {
6506 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006507 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006508 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006509 } else if (destination.IsFpuRegister()) {
6510 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006511 } else {
6512 DCHECK(destination.IsStackSlot());
6513 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
6514 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6515 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006516 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006517 if (destination.IsRegister()) {
6518 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
6519 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006520 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006521 } else {
6522 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006523 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
6524 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006525 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006526 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006527 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
6528 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006529 } else if (destination.IsRegisterPair()) {
6530 DCHECK(ExpectedPairLayout(destination));
6531 __ LoadFromOffset(
6532 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
6533 } else {
6534 DCHECK(destination.IsFpuRegisterPair()) << destination;
6535 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6536 SP,
6537 source.GetStackIndex());
6538 }
6539 } else if (source.IsRegisterPair()) {
6540 if (destination.IsRegisterPair()) {
6541 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6542 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006543 } else if (destination.IsFpuRegisterPair()) {
6544 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6545 source.AsRegisterPairLow<Register>(),
6546 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006547 } else {
6548 DCHECK(destination.IsDoubleStackSlot()) << destination;
6549 DCHECK(ExpectedPairLayout(source));
6550 __ StoreToOffset(
6551 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
6552 }
6553 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006554 if (destination.IsRegisterPair()) {
6555 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6556 destination.AsRegisterPairHigh<Register>(),
6557 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6558 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006559 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6560 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6561 } else {
6562 DCHECK(destination.IsDoubleStackSlot()) << destination;
6563 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
6564 SP,
6565 destination.GetStackIndex());
6566 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006567 } else {
6568 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006569 HConstant* constant = source.GetConstant();
6570 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6571 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006572 if (destination.IsRegister()) {
6573 __ LoadImmediate(destination.AsRegister<Register>(), value);
6574 } else {
6575 DCHECK(destination.IsStackSlot());
6576 __ LoadImmediate(IP, value);
6577 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6578 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006579 } else if (constant->IsLongConstant()) {
6580 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006581 if (destination.IsRegisterPair()) {
6582 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
6583 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006584 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006585 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006586 __ LoadImmediate(IP, Low32Bits(value));
6587 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6588 __ LoadImmediate(IP, High32Bits(value));
6589 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6590 }
6591 } else if (constant->IsDoubleConstant()) {
6592 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006593 if (destination.IsFpuRegisterPair()) {
6594 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006595 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006596 DCHECK(destination.IsDoubleStackSlot()) << destination;
6597 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006598 __ LoadImmediate(IP, Low32Bits(int_value));
6599 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6600 __ LoadImmediate(IP, High32Bits(int_value));
6601 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6602 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006603 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006604 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006605 float value = constant->AsFloatConstant()->GetValue();
6606 if (destination.IsFpuRegister()) {
6607 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
6608 } else {
6609 DCHECK(destination.IsStackSlot());
6610 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
6611 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6612 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006613 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006614 }
6615}
6616
6617void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
6618 __ Mov(IP, reg);
6619 __ LoadFromOffset(kLoadWord, reg, SP, mem);
6620 __ StoreToOffset(kStoreWord, IP, SP, mem);
6621}
6622
6623void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
6624 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
6625 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
6626 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
6627 SP, mem1 + stack_offset);
6628 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
6629 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
6630 SP, mem2 + stack_offset);
6631 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
6632}
6633
6634void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006635 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006636 Location source = move->GetSource();
6637 Location destination = move->GetDestination();
6638
6639 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006640 DCHECK_NE(source.AsRegister<Register>(), IP);
6641 DCHECK_NE(destination.AsRegister<Register>(), IP);
6642 __ Mov(IP, source.AsRegister<Register>());
6643 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
6644 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006645 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006646 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006647 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006648 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006649 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
6650 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006651 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006652 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006653 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006654 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006655 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006656 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006657 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006658 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006659 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6660 destination.AsRegisterPairHigh<Register>(),
6661 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006662 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006663 Register low_reg = source.IsRegisterPair()
6664 ? source.AsRegisterPairLow<Register>()
6665 : destination.AsRegisterPairLow<Register>();
6666 int mem = source.IsRegisterPair()
6667 ? destination.GetStackIndex()
6668 : source.GetStackIndex();
6669 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006670 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006671 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006672 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006673 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006674 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
6675 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006676 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006677 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006678 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006679 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
6680 DRegister reg = source.IsFpuRegisterPair()
6681 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
6682 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
6683 int mem = source.IsFpuRegisterPair()
6684 ? destination.GetStackIndex()
6685 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006686 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006687 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006688 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006689 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
6690 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
6691 : destination.AsFpuRegister<SRegister>();
6692 int mem = source.IsFpuRegister()
6693 ? destination.GetStackIndex()
6694 : source.GetStackIndex();
6695
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006696 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006697 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00006698 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006699 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006700 Exchange(source.GetStackIndex(), destination.GetStackIndex());
6701 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006702 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00006703 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006704 }
6705}
6706
6707void ParallelMoveResolverARM::SpillScratch(int reg) {
6708 __ Push(static_cast<Register>(reg));
6709}
6710
6711void ParallelMoveResolverARM::RestoreScratch(int reg) {
6712 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006713}
6714
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006715HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
6716 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006717 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006718 case HLoadClass::LoadKind::kInvalid:
6719 LOG(FATAL) << "UNREACHABLE";
6720 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006721 case HLoadClass::LoadKind::kReferrersClass:
6722 break;
6723 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6724 DCHECK(!GetCompilerOptions().GetCompilePic());
6725 break;
6726 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6727 DCHECK(GetCompilerOptions().GetCompilePic());
6728 break;
6729 case HLoadClass::LoadKind::kBootImageAddress:
6730 break;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006731 case HLoadClass::LoadKind::kBssEntry:
6732 DCHECK(!Runtime::Current()->UseJitCompilation());
6733 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006734 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006735 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006736 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006737 case HLoadClass::LoadKind::kDexCacheViaMethod:
6738 break;
6739 }
6740 return desired_class_load_kind;
6741}
6742
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006743void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00006744 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6745 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006746 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00006747 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006748 cls,
6749 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00006750 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00006751 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006752 return;
6753 }
Vladimir Marko41559982017-01-06 14:04:23 +00006754 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006755
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006756 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6757 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006758 ? LocationSummary::kCallOnSlowPath
6759 : LocationSummary::kNoCall;
6760 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006761 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006762 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006763 }
6764
Vladimir Marko41559982017-01-06 14:04:23 +00006765 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006766 locations->SetInAt(0, Location::RequiresRegister());
6767 }
6768 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00006769 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
6770 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6771 // Rely on the type resolution or initialization and marking to save everything we need.
6772 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6773 // to the custom calling convention) or by marking, so we request a different temp.
6774 locations->AddTemp(Location::RequiresRegister());
6775 RegisterSet caller_saves = RegisterSet::Empty();
6776 InvokeRuntimeCallingConvention calling_convention;
6777 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6778 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6779 // that the the kPrimNot result register is the same as the first argument register.
6780 locations->SetCustomSlowPathCallerSaves(caller_saves);
6781 } else {
6782 // For non-Baker read barrier we have a temp-clobbering call.
6783 }
6784 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006785 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
6786 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
6787 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
6788 !Runtime::Current()->UseJitCompilation())) {
6789 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6790 }
6791 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006792}
6793
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006794// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6795// move.
6796void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00006797 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6798 if (load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) {
6799 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01006800 return;
6801 }
Vladimir Marko41559982017-01-06 14:04:23 +00006802 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01006803
Vladimir Marko41559982017-01-06 14:04:23 +00006804 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006805 Location out_loc = locations->Out();
6806 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00006807
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006808 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6809 ? kWithoutReadBarrier
6810 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006811 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00006812 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006813 case HLoadClass::LoadKind::kReferrersClass: {
6814 DCHECK(!cls->CanCallRuntime());
6815 DCHECK(!cls->MustGenerateClinitCheck());
6816 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6817 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006818 GenerateGcRootFieldLoad(cls,
6819 out_loc,
6820 current_method,
6821 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006822 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006823 break;
6824 }
6825 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006826 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006827 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006828 __ LoadLiteral(out, codegen_->DeduplicateBootImageTypeLiteral(cls->GetDexFile(),
6829 cls->GetTypeIndex()));
6830 break;
6831 }
6832 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006833 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006834 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006835 CodeGeneratorARM::PcRelativePatchInfo* labels =
6836 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
6837 __ BindTrackedLabel(&labels->movw_label);
6838 __ movw(out, /* placeholder */ 0u);
6839 __ BindTrackedLabel(&labels->movt_label);
6840 __ movt(out, /* placeholder */ 0u);
6841 __ BindTrackedLabel(&labels->add_pc_label);
6842 __ add(out, out, ShifterOperand(PC));
6843 break;
6844 }
6845 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006846 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006847 uint32_t address = dchecked_integral_cast<uint32_t>(
6848 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
6849 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006850 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
6851 break;
6852 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006853 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006854 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
6855 ? locations->GetTemp(0).AsRegister<Register>()
6856 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006857 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00006858 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006859 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006860 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006861 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006862 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006863 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00006864 __ add(temp, temp, ShifterOperand(PC));
6865 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006866 generate_null_check = true;
6867 break;
6868 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006869 case HLoadClass::LoadKind::kJitTableAddress: {
6870 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
6871 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00006872 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006873 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00006874 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006875 break;
6876 }
Vladimir Marko41559982017-01-06 14:04:23 +00006877 case HLoadClass::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00006878 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00006879 LOG(FATAL) << "UNREACHABLE";
6880 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006881 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006882
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006883 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6884 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01006885 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006886 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6887 codegen_->AddSlowPath(slow_path);
6888 if (generate_null_check) {
6889 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
6890 }
6891 if (cls->MustGenerateClinitCheck()) {
6892 GenerateClassInitializationCheck(slow_path, out);
6893 } else {
6894 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006895 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006896 }
6897}
6898
6899void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
6900 LocationSummary* locations =
6901 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6902 locations->SetInAt(0, Location::RequiresRegister());
6903 if (check->HasUses()) {
6904 locations->SetOut(Location::SameAsFirstInput());
6905 }
6906}
6907
6908void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006909 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01006910 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006911 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006912 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006913 GenerateClassInitializationCheck(slow_path,
6914 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006915}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006916
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006917void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01006918 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006919 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
6920 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
6921 __ b(slow_path->GetEntryLabel(), LT);
6922 // Even if the initialized flag is set, we may be in a situation where caches are not synced
6923 // properly. Therefore, we do a memory fence.
6924 __ dmb(ISH);
6925 __ Bind(slow_path->GetExitLabel());
6926}
6927
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006928HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
6929 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006930 switch (desired_string_load_kind) {
6931 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6932 DCHECK(!GetCompilerOptions().GetCompilePic());
6933 break;
6934 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6935 DCHECK(GetCompilerOptions().GetCompilePic());
6936 break;
6937 case HLoadString::LoadKind::kBootImageAddress:
6938 break;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006939 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006940 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006941 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006942 case HLoadString::LoadKind::kJitTableAddress:
6943 DCHECK(Runtime::Current()->UseJitCompilation());
6944 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006945 case HLoadString::LoadKind::kDexCacheViaMethod:
6946 break;
6947 }
6948 return desired_string_load_kind;
6949}
6950
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006951void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006952 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006953 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006954 HLoadString::LoadKind load_kind = load->GetLoadKind();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006955 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006956 locations->SetOut(Location::RegisterLocation(R0));
6957 } else {
6958 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006959 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6960 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00006961 // Rely on the pResolveString and marking to save everything we need, including temps.
6962 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
6963 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006964 locations->AddTemp(Location::RequiresRegister());
6965 RegisterSet caller_saves = RegisterSet::Empty();
6966 InvokeRuntimeCallingConvention calling_convention;
6967 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6968 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
6969 // that the the kPrimNot result register is the same as the first argument register.
6970 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006971 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
6972 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6973 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006974 } else {
6975 // For non-Baker read barrier we have a temp-clobbering call.
6976 }
6977 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006978 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006979}
6980
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00006981// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
6982// move.
6983void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006984 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006985 Location out_loc = locations->Out();
6986 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006987 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00006988
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07006989 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006990 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006991 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006992 __ LoadLiteral(out, codegen_->DeduplicateBootImageStringLiteral(load->GetDexFile(),
6993 load->GetStringIndex()));
6994 return; // No dex cache slow path.
6995 }
6996 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00006997 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006998 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00006999 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007000 __ BindTrackedLabel(&labels->movw_label);
7001 __ movw(out, /* placeholder */ 0u);
7002 __ BindTrackedLabel(&labels->movt_label);
7003 __ movt(out, /* placeholder */ 0u);
7004 __ BindTrackedLabel(&labels->add_pc_label);
7005 __ add(out, out, ShifterOperand(PC));
7006 return; // No dex cache slow path.
7007 }
7008 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007009 uint32_t address = dchecked_integral_cast<uint32_t>(
7010 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7011 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007012 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7013 return; // No dex cache slow path.
7014 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007015 case HLoadString::LoadKind::kBssEntry: {
7016 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007017 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7018 ? locations->GetTemp(0).AsRegister<Register>()
7019 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00007020 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007021 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007022 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007023 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007024 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007025 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007026 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007027 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007028 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007029 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
7030 codegen_->AddSlowPath(slow_path);
7031 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7032 __ Bind(slow_path->GetExitLabel());
7033 return;
7034 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007035 case HLoadString::LoadKind::kJitTableAddress: {
7036 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007037 load->GetStringIndex(),
7038 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007039 // /* GcRoot<mirror::String> */ out = *out
7040 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7041 return;
7042 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007043 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007044 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007045 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007046
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007047 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
7048 DCHECK(load_kind == HLoadString::LoadKind::kDexCacheViaMethod);
7049 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007050 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007051 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007052 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7053 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007054}
7055
David Brazdilcb1c0552015-08-04 16:22:25 +01007056static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007057 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01007058}
7059
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007060void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
7061 LocationSummary* locations =
7062 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7063 locations->SetOut(Location::RequiresRegister());
7064}
7065
7066void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007067 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01007068 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7069}
7070
7071void LocationsBuilderARM::VisitClearException(HClearException* clear) {
7072 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7073}
7074
7075void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007076 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01007077 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007078}
7079
7080void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
7081 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007082 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007083 InvokeRuntimeCallingConvention calling_convention;
7084 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7085}
7086
7087void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007088 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007089 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007090}
7091
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007092// Temp is used for read barrier.
7093static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7094 if (kEmitCompilerReadBarrier &&
7095 (kUseBakerReadBarrier ||
7096 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7097 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7098 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7099 return 1;
7100 }
7101 return 0;
7102}
7103
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007104// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007105// interface pointer, one for loading the current interface.
7106// The other checks have one temp for loading the object's class.
7107static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7108 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7109 return 3;
7110 }
7111 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00007112}
7113
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007114void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007115 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00007116 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007117 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00007118 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007119 case TypeCheckKind::kExactCheck:
7120 case TypeCheckKind::kAbstractClassCheck:
7121 case TypeCheckKind::kClassHierarchyCheck:
7122 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007123 call_kind =
7124 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01007125 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007126 break;
7127 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007128 case TypeCheckKind::kUnresolvedCheck:
7129 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007130 call_kind = LocationSummary::kCallOnSlowPath;
7131 break;
7132 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007133
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007134 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007135 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007136 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007137 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007138 locations->SetInAt(0, Location::RequiresRegister());
7139 locations->SetInAt(1, Location::RequiresRegister());
7140 // The "out" register is used as a temporary, so it overlaps with the inputs.
7141 // Note that TypeCheckSlowPathARM uses this register too.
7142 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007143 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007144 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7145 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7146 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007147}
7148
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007149void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007150 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007151 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007152 Location obj_loc = locations->InAt(0);
7153 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007154 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007155 Location out_loc = locations->Out();
7156 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007157 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7158 DCHECK_LE(num_temps, 1u);
7159 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007160 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007161 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7162 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7163 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007164 Label done;
7165 Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01007166 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007167
7168 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007169 // avoid null check if we know obj is not null.
7170 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007171 DCHECK_NE(out, obj);
7172 __ LoadImmediate(out, 0);
7173 __ CompareAndBranchIfZero(obj, final_label);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007174 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007175
Roland Levillainc9285912015-12-18 10:38:42 +00007176 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007177 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007178 // /* HeapReference<Class> */ out = obj->klass_
7179 GenerateReferenceLoadTwoRegisters(instruction,
7180 out_loc,
7181 obj_loc,
7182 class_offset,
7183 maybe_temp_loc,
7184 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007185 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007186 __ cmp(out, ShifterOperand(cls));
7187 // We speculatively set the result to false without changing the condition
7188 // flags, which allows us to avoid some branching later.
7189 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7190
7191 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7192 // we check that the output is in a low register, so that a 16-bit MOV
7193 // encoding can be used.
7194 if (ArmAssembler::IsLowRegister(out)) {
7195 __ it(EQ);
7196 __ mov(out, ShifterOperand(1), EQ);
7197 } else {
7198 __ b(final_label, NE);
7199 __ LoadImmediate(out, 1);
7200 }
7201
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007202 break;
7203 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007204
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007205 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007206 // /* HeapReference<Class> */ out = obj->klass_
7207 GenerateReferenceLoadTwoRegisters(instruction,
7208 out_loc,
7209 obj_loc,
7210 class_offset,
7211 maybe_temp_loc,
7212 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007213 // If the class is abstract, we eagerly fetch the super class of the
7214 // object to avoid doing a comparison we know will fail.
7215 Label loop;
7216 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007217 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007218 GenerateReferenceLoadOneRegister(instruction,
7219 out_loc,
7220 super_offset,
7221 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007222 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007223 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007224 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007225 __ cmp(out, ShifterOperand(cls));
7226 __ b(&loop, NE);
7227 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007228 break;
7229 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007230
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007231 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007232 // /* HeapReference<Class> */ out = obj->klass_
7233 GenerateReferenceLoadTwoRegisters(instruction,
7234 out_loc,
7235 obj_loc,
7236 class_offset,
7237 maybe_temp_loc,
7238 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007239 // Walk over the class hierarchy to find a match.
7240 Label loop, success;
7241 __ Bind(&loop);
7242 __ cmp(out, ShifterOperand(cls));
7243 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007244 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007245 GenerateReferenceLoadOneRegister(instruction,
7246 out_loc,
7247 super_offset,
7248 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007249 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007250 // This is essentially a null check, but it sets the condition flags to the
7251 // proper value for the code that follows the loop, i.e. not `EQ`.
7252 __ cmp(out, ShifterOperand(1));
7253 __ b(&loop, HS);
7254
7255 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7256 // we check that the output is in a low register, so that a 16-bit MOV
7257 // encoding can be used.
7258 if (ArmAssembler::IsLowRegister(out)) {
7259 // If `out` is null, we use it for the result, and the condition flags
7260 // have already been set to `NE`, so the IT block that comes afterwards
7261 // (and which handles the successful case) turns into a NOP (instead of
7262 // overwriting `out`).
7263 __ Bind(&success);
7264 // There is only one branch to the `success` label (which is bound to this
7265 // IT block), and it has the same condition, `EQ`, so in that case the MOV
7266 // is executed.
7267 __ it(EQ);
7268 __ mov(out, ShifterOperand(1), EQ);
7269 } else {
7270 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007271 __ b(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007272 __ Bind(&success);
7273 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007274 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007275
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007276 break;
7277 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007278
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007279 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007280 // /* HeapReference<Class> */ out = obj->klass_
7281 GenerateReferenceLoadTwoRegisters(instruction,
7282 out_loc,
7283 obj_loc,
7284 class_offset,
7285 maybe_temp_loc,
7286 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007287 // Do an exact check.
7288 Label exact_check;
7289 __ cmp(out, ShifterOperand(cls));
7290 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007291 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007292 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007293 GenerateReferenceLoadOneRegister(instruction,
7294 out_loc,
7295 component_offset,
7296 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007297 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007298 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007299 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007300 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7301 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007302 __ cmp(out, ShifterOperand(0));
7303 // We speculatively set the result to false without changing the condition
7304 // flags, which allows us to avoid some branching later.
7305 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7306
7307 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7308 // we check that the output is in a low register, so that a 16-bit MOV
7309 // encoding can be used.
7310 if (ArmAssembler::IsLowRegister(out)) {
7311 __ Bind(&exact_check);
7312 __ it(EQ);
7313 __ mov(out, ShifterOperand(1), EQ);
7314 } else {
7315 __ b(final_label, NE);
7316 __ Bind(&exact_check);
7317 __ LoadImmediate(out, 1);
7318 }
7319
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007320 break;
7321 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007322
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007323 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007324 // No read barrier since the slow path will retry upon failure.
7325 // /* HeapReference<Class> */ out = obj->klass_
7326 GenerateReferenceLoadTwoRegisters(instruction,
7327 out_loc,
7328 obj_loc,
7329 class_offset,
7330 maybe_temp_loc,
7331 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007332 __ cmp(out, ShifterOperand(cls));
7333 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00007334 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7335 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007336 codegen_->AddSlowPath(slow_path);
7337 __ b(slow_path->GetEntryLabel(), NE);
7338 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007339 break;
7340 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007341
Calin Juravle98893e12015-10-02 21:05:03 +01007342 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007343 case TypeCheckKind::kInterfaceCheck: {
7344 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007345 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00007346 // cases.
7347 //
7348 // We cannot directly call the InstanceofNonTrivial runtime
7349 // entry point without resorting to a type checking slow path
7350 // here (i.e. by calling InvokeRuntime directly), as it would
7351 // require to assign fixed registers for the inputs of this
7352 // HInstanceOf instruction (following the runtime calling
7353 // convention), which might be cluttered by the potential first
7354 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00007355 //
7356 // TODO: Introduce a new runtime entry point taking the object
7357 // to test (instead of its class) as argument, and let it deal
7358 // with the read barrier issues. This will let us refactor this
7359 // case of the `switch` code as it was previously (with a direct
7360 // call to the runtime not using a type checking slow path).
7361 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00007362 DCHECK(locations->OnlyCallsOnSlowPath());
7363 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7364 /* is_fatal */ false);
7365 codegen_->AddSlowPath(slow_path);
7366 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007367 break;
7368 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007369 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007370
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007371 if (done.IsLinked()) {
7372 __ Bind(&done);
7373 }
7374
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007375 if (slow_path != nullptr) {
7376 __ Bind(slow_path->GetExitLabel());
7377 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007378}
7379
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007380void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007381 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7382 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7383
Roland Levillain3b359c72015-11-17 19:35:12 +00007384 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7385 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007386 case TypeCheckKind::kExactCheck:
7387 case TypeCheckKind::kAbstractClassCheck:
7388 case TypeCheckKind::kClassHierarchyCheck:
7389 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007390 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7391 LocationSummary::kCallOnSlowPath :
7392 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007393 break;
7394 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007395 case TypeCheckKind::kUnresolvedCheck:
7396 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007397 call_kind = LocationSummary::kCallOnSlowPath;
7398 break;
7399 }
7400
Roland Levillain3b359c72015-11-17 19:35:12 +00007401 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7402 locations->SetInAt(0, Location::RequiresRegister());
7403 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007404 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007405}
7406
7407void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007408 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007409 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007410 Location obj_loc = locations->InAt(0);
7411 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007412 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007413 Location temp_loc = locations->GetTemp(0);
7414 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007415 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7416 DCHECK_LE(num_temps, 3u);
7417 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7418 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7419 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7420 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7421 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7422 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7423 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7424 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7425 const uint32_t object_array_data_offset =
7426 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007427
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007428 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7429 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7430 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007431 bool is_type_check_slow_path_fatal = false;
7432 if (!kEmitCompilerReadBarrier) {
7433 is_type_check_slow_path_fatal =
7434 (type_check_kind == TypeCheckKind::kExactCheck ||
7435 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7436 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7437 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7438 !instruction->CanThrowIntoCatchBlock();
7439 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01007440 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007441 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7442 is_type_check_slow_path_fatal);
7443 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007444
7445 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007446 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007447 // Avoid null check if we know obj is not null.
7448 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007449 __ CompareAndBranchIfZero(obj, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007450 }
7451
Roland Levillain3b359c72015-11-17 19:35:12 +00007452 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007453 case TypeCheckKind::kExactCheck:
7454 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007455 // /* HeapReference<Class> */ temp = obj->klass_
7456 GenerateReferenceLoadTwoRegisters(instruction,
7457 temp_loc,
7458 obj_loc,
7459 class_offset,
7460 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007461 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007462
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007463 __ cmp(temp, ShifterOperand(cls));
7464 // Jump to slow path for throwing the exception or doing a
7465 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00007466 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007467 break;
7468 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007469
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007470 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007471 // /* HeapReference<Class> */ temp = obj->klass_
7472 GenerateReferenceLoadTwoRegisters(instruction,
7473 temp_loc,
7474 obj_loc,
7475 class_offset,
7476 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007477 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007478
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007479 // If the class is abstract, we eagerly fetch the super class of the
7480 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007481 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007482 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007483 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007484 GenerateReferenceLoadOneRegister(instruction,
7485 temp_loc,
7486 super_offset,
7487 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007488 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007489
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007490 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7491 // exception.
7492 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00007493
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007494 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007495 __ cmp(temp, ShifterOperand(cls));
7496 __ b(&loop, NE);
7497 break;
7498 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007499
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007500 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007501 // /* HeapReference<Class> */ temp = obj->klass_
7502 GenerateReferenceLoadTwoRegisters(instruction,
7503 temp_loc,
7504 obj_loc,
7505 class_offset,
7506 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007507 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007508
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007509 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007510 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007511 __ Bind(&loop);
7512 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007513 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007514
Roland Levillain3b359c72015-11-17 19:35:12 +00007515 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007516 GenerateReferenceLoadOneRegister(instruction,
7517 temp_loc,
7518 super_offset,
7519 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007520 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007521
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007522 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7523 // exception.
7524 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7525 // Otherwise, jump to the beginning of the loop.
7526 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007527 break;
7528 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007529
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007530 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007531 // /* HeapReference<Class> */ temp = obj->klass_
7532 GenerateReferenceLoadTwoRegisters(instruction,
7533 temp_loc,
7534 obj_loc,
7535 class_offset,
7536 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007537 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007538
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007539 // Do an exact check.
7540 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007541 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007542
7543 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007544 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007545 GenerateReferenceLoadOneRegister(instruction,
7546 temp_loc,
7547 component_offset,
7548 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007549 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007550 // If the component type is null, jump to the slow path to throw the exception.
7551 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7552 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7553 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007554 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00007555 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007556 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007557 break;
7558 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007559
Calin Juravle98893e12015-10-02 21:05:03 +01007560 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007561 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00007562 // We cannot directly call the CheckCast runtime entry point
7563 // without resorting to a type checking slow path here (i.e. by
7564 // calling InvokeRuntime directly), as it would require to
7565 // assign fixed registers for the inputs of this HInstanceOf
7566 // instruction (following the runtime calling convention), which
7567 // might be cluttered by the potential first read barrier
7568 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007569
Roland Levillain3b359c72015-11-17 19:35:12 +00007570 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007571 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007572
7573 case TypeCheckKind::kInterfaceCheck: {
7574 // Avoid read barriers to improve performance of the fast path. We can not get false
7575 // positives by doing this.
7576 // /* HeapReference<Class> */ temp = obj->klass_
7577 GenerateReferenceLoadTwoRegisters(instruction,
7578 temp_loc,
7579 obj_loc,
7580 class_offset,
7581 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007582 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007583
7584 // /* HeapReference<Class> */ temp = temp->iftable_
7585 GenerateReferenceLoadTwoRegisters(instruction,
7586 temp_loc,
7587 temp_loc,
7588 iftable_offset,
7589 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007590 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007591 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007592 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08007593 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007594 Label start_loop;
7595 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007596 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
7597 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007598 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
7599 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007600 // Go to next interface.
7601 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
7602 __ sub(maybe_temp2_loc.AsRegister<Register>(),
7603 maybe_temp2_loc.AsRegister<Register>(),
7604 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007605 // Compare the classes and continue the loop if they do not match.
7606 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
7607 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007608 break;
7609 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007610 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007611
7612 if (done.IsLinked()) {
7613 __ Bind(&done);
7614 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007615
Roland Levillain3b359c72015-11-17 19:35:12 +00007616 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007617}
7618
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007619void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
7620 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007621 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007622 InvokeRuntimeCallingConvention calling_convention;
7623 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7624}
7625
7626void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007627 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
7628 instruction,
7629 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007630 if (instruction->IsEnter()) {
7631 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7632 } else {
7633 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7634 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007635}
7636
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007637void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
7638void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
7639void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007640
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007641void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007642 LocationSummary* locations =
7643 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7644 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7645 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007646 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007647 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007648 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00007649 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007650}
7651
7652void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
7653 HandleBitwiseOperation(instruction);
7654}
7655
7656void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
7657 HandleBitwiseOperation(instruction);
7658}
7659
7660void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
7661 HandleBitwiseOperation(instruction);
7662}
7663
Artem Serov7fc63502016-02-09 17:15:29 +00007664
7665void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7666 LocationSummary* locations =
7667 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7668 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
7669 || instruction->GetResultType() == Primitive::kPrimLong);
7670
7671 locations->SetInAt(0, Location::RequiresRegister());
7672 locations->SetInAt(1, Location::RequiresRegister());
7673 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7674}
7675
7676void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
7677 LocationSummary* locations = instruction->GetLocations();
7678 Location first = locations->InAt(0);
7679 Location second = locations->InAt(1);
7680 Location out = locations->Out();
7681
7682 if (instruction->GetResultType() == Primitive::kPrimInt) {
7683 Register first_reg = first.AsRegister<Register>();
7684 ShifterOperand second_reg(second.AsRegister<Register>());
7685 Register out_reg = out.AsRegister<Register>();
7686
7687 switch (instruction->GetOpKind()) {
7688 case HInstruction::kAnd:
7689 __ bic(out_reg, first_reg, second_reg);
7690 break;
7691 case HInstruction::kOr:
7692 __ orn(out_reg, first_reg, second_reg);
7693 break;
7694 // There is no EON on arm.
7695 case HInstruction::kXor:
7696 default:
7697 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7698 UNREACHABLE();
7699 }
7700 return;
7701
7702 } else {
7703 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7704 Register first_low = first.AsRegisterPairLow<Register>();
7705 Register first_high = first.AsRegisterPairHigh<Register>();
7706 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7707 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7708 Register out_low = out.AsRegisterPairLow<Register>();
7709 Register out_high = out.AsRegisterPairHigh<Register>();
7710
7711 switch (instruction->GetOpKind()) {
7712 case HInstruction::kAnd:
7713 __ bic(out_low, first_low, second_low);
7714 __ bic(out_high, first_high, second_high);
7715 break;
7716 case HInstruction::kOr:
7717 __ orn(out_low, first_low, second_low);
7718 __ orn(out_high, first_high, second_high);
7719 break;
7720 // There is no EON on arm.
7721 case HInstruction::kXor:
7722 default:
7723 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
7724 UNREACHABLE();
7725 }
7726 }
7727}
7728
Anton Kirilov74234da2017-01-13 14:42:47 +00007729void LocationsBuilderARM::VisitDataProcWithShifterOp(
7730 HDataProcWithShifterOp* instruction) {
7731 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
7732 instruction->GetType() == Primitive::kPrimLong);
7733 LocationSummary* locations =
7734 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7735 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
7736 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
7737
7738 locations->SetInAt(0, Location::RequiresRegister());
7739 locations->SetInAt(1, Location::RequiresRegister());
7740 locations->SetOut(Location::RequiresRegister(),
7741 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
7742}
7743
7744void InstructionCodeGeneratorARM::VisitDataProcWithShifterOp(
7745 HDataProcWithShifterOp* instruction) {
7746 const LocationSummary* const locations = instruction->GetLocations();
7747 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
7748 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
7749 const Location left = locations->InAt(0);
7750 const Location right = locations->InAt(1);
7751 const Location out = locations->Out();
7752
7753 if (instruction->GetType() == Primitive::kPrimInt) {
7754 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
7755
7756 const Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
7757 ? right.AsRegisterPairLow<Register>()
7758 : right.AsRegister<Register>();
7759
7760 GenerateDataProcInstruction(kind,
7761 out.AsRegister<Register>(),
7762 left.AsRegister<Register>(),
7763 ShifterOperand(second,
7764 ShiftFromOpKind(op_kind),
7765 instruction->GetShiftAmount()),
7766 codegen_);
7767 } else {
7768 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
7769
7770 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
7771 const Register second = right.AsRegister<Register>();
7772
7773 DCHECK_NE(out.AsRegisterPairLow<Register>(), second);
7774 GenerateDataProc(kind,
7775 out,
7776 left,
7777 ShifterOperand(second),
7778 ShifterOperand(second, ASR, 31),
7779 codegen_);
7780 } else {
7781 GenerateLongDataProc(instruction, codegen_);
7782 }
7783 }
7784}
7785
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007786void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
7787 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
7788 if (value == 0xffffffffu) {
7789 if (out != first) {
7790 __ mov(out, ShifterOperand(first));
7791 }
7792 return;
7793 }
7794 if (value == 0u) {
7795 __ mov(out, ShifterOperand(0));
7796 return;
7797 }
7798 ShifterOperand so;
7799 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
7800 __ and_(out, first, so);
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007801 } else if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007802 __ bic(out, first, ShifterOperand(~value));
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00007803 } else {
7804 DCHECK(IsPowerOfTwo(value + 1));
7805 __ ubfx(out, first, 0, WhichPowerOf2(value + 1));
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007806 }
7807}
7808
7809void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
7810 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
7811 if (value == 0u) {
7812 if (out != first) {
7813 __ mov(out, ShifterOperand(first));
7814 }
7815 return;
7816 }
7817 if (value == 0xffffffffu) {
7818 __ mvn(out, ShifterOperand(0));
7819 return;
7820 }
7821 ShifterOperand so;
7822 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
7823 __ orr(out, first, so);
7824 } else {
7825 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
7826 __ orn(out, first, ShifterOperand(~value));
7827 }
7828}
7829
7830void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
7831 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
7832 if (value == 0u) {
7833 if (out != first) {
7834 __ mov(out, ShifterOperand(first));
7835 }
7836 return;
7837 }
7838 __ eor(out, first, ShifterOperand(value));
7839}
7840
Vladimir Marko59751a72016-08-05 14:37:27 +01007841void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
7842 Location first,
7843 uint64_t value) {
7844 Register out_low = out.AsRegisterPairLow<Register>();
7845 Register out_high = out.AsRegisterPairHigh<Register>();
7846 Register first_low = first.AsRegisterPairLow<Register>();
7847 Register first_high = first.AsRegisterPairHigh<Register>();
7848 uint32_t value_low = Low32Bits(value);
7849 uint32_t value_high = High32Bits(value);
7850 if (value_low == 0u) {
7851 if (out_low != first_low) {
7852 __ mov(out_low, ShifterOperand(first_low));
7853 }
7854 __ AddConstant(out_high, first_high, value_high);
7855 return;
7856 }
7857 __ AddConstantSetFlags(out_low, first_low, value_low);
7858 ShifterOperand so;
7859 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
7860 __ adc(out_high, first_high, so);
7861 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
7862 __ sbc(out_high, first_high, so);
7863 } else {
7864 LOG(FATAL) << "Unexpected constant " << value_high;
7865 UNREACHABLE();
7866 }
7867}
7868
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007869void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
7870 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007871 Location first = locations->InAt(0);
7872 Location second = locations->InAt(1);
7873 Location out = locations->Out();
7874
7875 if (second.IsConstant()) {
7876 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
7877 uint32_t value_low = Low32Bits(value);
7878 if (instruction->GetResultType() == Primitive::kPrimInt) {
7879 Register first_reg = first.AsRegister<Register>();
7880 Register out_reg = out.AsRegister<Register>();
7881 if (instruction->IsAnd()) {
7882 GenerateAndConst(out_reg, first_reg, value_low);
7883 } else if (instruction->IsOr()) {
7884 GenerateOrrConst(out_reg, first_reg, value_low);
7885 } else {
7886 DCHECK(instruction->IsXor());
7887 GenerateEorConst(out_reg, first_reg, value_low);
7888 }
7889 } else {
7890 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7891 uint32_t value_high = High32Bits(value);
7892 Register first_low = first.AsRegisterPairLow<Register>();
7893 Register first_high = first.AsRegisterPairHigh<Register>();
7894 Register out_low = out.AsRegisterPairLow<Register>();
7895 Register out_high = out.AsRegisterPairHigh<Register>();
7896 if (instruction->IsAnd()) {
7897 GenerateAndConst(out_low, first_low, value_low);
7898 GenerateAndConst(out_high, first_high, value_high);
7899 } else if (instruction->IsOr()) {
7900 GenerateOrrConst(out_low, first_low, value_low);
7901 GenerateOrrConst(out_high, first_high, value_high);
7902 } else {
7903 DCHECK(instruction->IsXor());
7904 GenerateEorConst(out_low, first_low, value_low);
7905 GenerateEorConst(out_high, first_high, value_high);
7906 }
7907 }
7908 return;
7909 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007910
7911 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007912 Register first_reg = first.AsRegister<Register>();
7913 ShifterOperand second_reg(second.AsRegister<Register>());
7914 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007915 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007916 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007917 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007918 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007919 } else {
7920 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007921 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007922 }
7923 } else {
7924 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007925 Register first_low = first.AsRegisterPairLow<Register>();
7926 Register first_high = first.AsRegisterPairHigh<Register>();
7927 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
7928 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
7929 Register out_low = out.AsRegisterPairLow<Register>();
7930 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007931 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007932 __ and_(out_low, first_low, second_low);
7933 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007934 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007935 __ orr(out_low, first_low, second_low);
7936 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007937 } else {
7938 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01007939 __ eor(out_low, first_low, second_low);
7940 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007941 }
7942 }
7943}
7944
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007945void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
7946 HInstruction* instruction,
7947 Location out,
7948 uint32_t offset,
7949 Location maybe_temp,
7950 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007951 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007952 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007953 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007954 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007955 if (kUseBakerReadBarrier) {
7956 // Load with fast path based Baker's read barrier.
7957 // /* HeapReference<Object> */ out = *(out + offset)
7958 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007959 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007960 } else {
7961 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007962 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00007963 // in the following move operation, as we will need it for the
7964 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007965 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00007966 // /* HeapReference<Object> */ out = *(out + offset)
7967 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007968 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00007969 }
7970 } else {
7971 // Plain load with no read barrier.
7972 // /* HeapReference<Object> */ out = *(out + offset)
7973 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
7974 __ MaybeUnpoisonHeapReference(out_reg);
7975 }
7976}
7977
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007978void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
7979 HInstruction* instruction,
7980 Location out,
7981 Location obj,
7982 uint32_t offset,
7983 Location maybe_temp,
7984 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00007985 Register out_reg = out.AsRegister<Register>();
7986 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007987 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007988 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00007989 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007990 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00007991 // Load with fast path based Baker's read barrier.
7992 // /* HeapReference<Object> */ out = *(obj + offset)
7993 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007994 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00007995 } else {
7996 // Load with slow path based read barrier.
7997 // /* HeapReference<Object> */ out = *(obj + offset)
7998 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
7999 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8000 }
8001 } else {
8002 // Plain load with no read barrier.
8003 // /* HeapReference<Object> */ out = *(obj + offset)
8004 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8005 __ MaybeUnpoisonHeapReference(out_reg);
8006 }
8007}
8008
8009void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
8010 Location root,
8011 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008012 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008013 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008014 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008015 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008016 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00008017 if (kUseBakerReadBarrier) {
8018 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008019 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008020 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8021 !Runtime::Current()->UseJitCompilation()) {
8022 // Note that we do not actually check the value of `GetIsGcMarking()`
8023 // to decide whether to mark the loaded GC root or not. Instead, we
8024 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8025 // barrier mark introspection entrypoint. If `temp` is null, it means
8026 // that `GetIsGcMarking()` is false, and vice versa.
8027 //
8028 // We use link-time generated thunks for the slow path. That thunk
8029 // checks the reference and jumps to the entrypoint if needed.
8030 //
8031 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8032 // lr = &return_address;
8033 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8034 // if (temp != nullptr) {
8035 // goto gc_root_thunk<root_reg>(lr)
8036 // }
8037 // return_address:
Roland Levillainc9285912015-12-18 10:38:42 +00008038
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008039 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8040 uint32_t custom_data =
8041 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg);
8042 Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008043
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008044 // entrypoint_reg =
8045 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8046 DCHECK_EQ(IP, 12);
8047 const int32_t entry_point_offset =
8048 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8049 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008050
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008051 Label return_address;
8052 __ AdrCode(LR, &return_address);
8053 __ CmpConstant(kBakerCcEntrypointRegister, 0);
8054 static_assert(
8055 BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_OFFSET == -8,
8056 "GC root LDR must be 2 32-bit instructions (8B) before the return address label.");
8057 // Currently the offset is always within range. If that changes,
8058 // we shall have to split the load the same way as for fields.
8059 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
8060 ScopedForce32Bit force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()));
8061 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8062 EmitPlaceholderBne(codegen_, bne_label);
8063 __ Bind(&return_address);
8064 } else {
8065 // Note that we do not actually check the value of
8066 // `GetIsGcMarking()` to decide whether to mark the loaded GC
8067 // root or not. Instead, we load into `temp` the read barrier
8068 // mark entry point corresponding to register `root`. If `temp`
8069 // is null, it means that `GetIsGcMarking()` is false, and vice
8070 // versa.
8071 //
8072 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8073 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8074 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8075 // // Slow path.
8076 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
8077 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008078
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008079 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
8080 Location temp = Location::RegisterLocation(LR);
8081 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
8082 instruction, root, /* entrypoint */ temp);
8083 codegen_->AddSlowPath(slow_path);
8084
8085 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8086 const int32_t entry_point_offset =
8087 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
8088 // Loading the entrypoint does not require a load acquire since it is only changed when
8089 // threads are suspended or running a checkpoint.
8090 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
8091
8092 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8093 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8094 static_assert(
8095 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8096 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8097 "have different sizes.");
8098 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8099 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8100 "have different sizes.");
8101
8102 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8103 // checking GetIsGcMarking.
8104 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
8105 __ Bind(slow_path->GetExitLabel());
8106 }
Roland Levillainc9285912015-12-18 10:38:42 +00008107 } else {
8108 // GC root loaded through a slow path for read barriers other
8109 // than Baker's.
8110 // /* GcRoot<mirror::Object>* */ root = obj + offset
8111 __ AddConstant(root_reg, obj, offset);
8112 // /* mirror::Object* */ root = root->Read()
8113 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8114 }
8115 } else {
8116 // Plain GC root load with no read barrier.
8117 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8118 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8119 // Note that GC roots are not affected by heap poisoning, thus we
8120 // do not have to unpoison `root_reg` here.
8121 }
8122}
8123
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008124void CodeGeneratorARM::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8125 DCHECK(kEmitCompilerReadBarrier);
8126 DCHECK(kUseBakerReadBarrier);
8127 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8128 if (!Runtime::Current()->UseJitCompilation()) {
8129 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
8130 }
8131 }
8132}
8133
Roland Levillainc9285912015-12-18 10:38:42 +00008134void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8135 Location ref,
8136 Register obj,
8137 uint32_t offset,
8138 Location temp,
8139 bool needs_null_check) {
8140 DCHECK(kEmitCompilerReadBarrier);
8141 DCHECK(kUseBakerReadBarrier);
8142
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008143 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8144 !Runtime::Current()->UseJitCompilation()) {
8145 // Note that we do not actually check the value of `GetIsGcMarking()`
8146 // to decide whether to mark the loaded reference or not. Instead, we
8147 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8148 // barrier mark introspection entrypoint. If `temp` is null, it means
8149 // that `GetIsGcMarking()` is false, and vice versa.
8150 //
8151 // We use link-time generated thunks for the slow path. That thunk checks
8152 // the holder and jumps to the entrypoint if needed. If the holder is not
8153 // gray, it creates a fake dependency and returns to the LDR instruction.
8154 //
8155 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8156 // lr = &gray_return_address;
8157 // if (temp != nullptr) {
8158 // goto field_thunk<holder_reg, base_reg>(lr)
8159 // }
8160 // not_gray_return_address:
8161 // // Original reference load. If the offset is too large to fit
8162 // // into LDR, we use an adjusted base register here.
8163 // GcRoot<mirror::Object> reference = *(obj+offset);
8164 // gray_return_address:
8165
8166 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
8167 Register base = obj;
8168 if (offset >= kReferenceLoadMinFarOffset) {
8169 base = temp.AsRegister<Register>();
8170 DCHECK_NE(base, kBakerCcEntrypointRegister);
8171 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8172 __ AddConstant(base, obj, offset & ~(kReferenceLoadMinFarOffset - 1u));
8173 offset &= (kReferenceLoadMinFarOffset - 1u);
8174 }
8175 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8176 uint32_t custom_data =
8177 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(base, obj);
8178 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8179
8180 // entrypoint_reg =
8181 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8182 DCHECK_EQ(IP, 12);
8183 const int32_t entry_point_offset =
8184 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8185 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8186
8187 Label return_address;
8188 __ AdrCode(LR, &return_address);
8189 __ CmpConstant(kBakerCcEntrypointRegister, 0);
8190 ScopedForce32Bit force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()));
8191 EmitPlaceholderBne(this, bne_label);
8192 static_assert(BAKER_MARK_INTROSPECTION_FIELD_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
8193 "Field LDR must be 1 32-bit instruction (4B) before the return address label; "
8194 " 2 32-bit instructions (8B) for heap poisoning.");
8195 Register ref_reg = ref.AsRegister<Register>();
8196 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
8197 __ LoadFromOffset(kLoadWord, ref_reg, base, offset);
8198 if (needs_null_check) {
8199 MaybeRecordImplicitNullCheck(instruction);
8200 }
8201 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8202 __ Bind(&return_address);
8203 return;
8204 }
8205
Roland Levillainc9285912015-12-18 10:38:42 +00008206 // /* HeapReference<Object> */ ref = *(obj + offset)
8207 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01008208 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00008209 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008210 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008211}
8212
8213void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8214 Location ref,
8215 Register obj,
8216 uint32_t data_offset,
8217 Location index,
8218 Location temp,
8219 bool needs_null_check) {
8220 DCHECK(kEmitCompilerReadBarrier);
8221 DCHECK(kUseBakerReadBarrier);
8222
Roland Levillainbfea3352016-06-23 13:48:47 +01008223 static_assert(
8224 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8225 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008226 ScaleFactor scale_factor = TIMES_4;
8227
8228 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8229 !Runtime::Current()->UseJitCompilation()) {
8230 // Note that we do not actually check the value of `GetIsGcMarking()`
8231 // to decide whether to mark the loaded reference or not. Instead, we
8232 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8233 // barrier mark introspection entrypoint. If `temp` is null, it means
8234 // that `GetIsGcMarking()` is false, and vice versa.
8235 //
8236 // We use link-time generated thunks for the slow path. That thunk checks
8237 // the holder and jumps to the entrypoint if needed. If the holder is not
8238 // gray, it creates a fake dependency and returns to the LDR instruction.
8239 //
8240 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8241 // lr = &gray_return_address;
8242 // if (temp != nullptr) {
8243 // goto field_thunk<holder_reg, base_reg>(lr)
8244 // }
8245 // not_gray_return_address:
8246 // // Original reference load. If the offset is too large to fit
8247 // // into LDR, we use an adjusted base register here.
8248 // GcRoot<mirror::Object> reference = data[index];
8249 // gray_return_address:
8250
8251 DCHECK(index.IsValid());
8252 Register index_reg = index.AsRegister<Register>();
8253 Register ref_reg = ref.AsRegister<Register>();
8254 Register data_reg = temp.AsRegister<Register>();
8255 DCHECK_NE(data_reg, kBakerCcEntrypointRegister);
8256
8257 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8258 uint32_t custom_data =
8259 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg);
8260 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8261
8262 // entrypoint_reg =
8263 // Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
8264 DCHECK_EQ(IP, 12);
8265 const int32_t entry_point_offset =
8266 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8267 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8268 __ AddConstant(data_reg, obj, data_offset);
8269
8270 Label return_address;
8271 __ AdrCode(LR, &return_address);
8272 __ CmpConstant(kBakerCcEntrypointRegister, 0);
8273 ScopedForce32Bit force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()));
8274 EmitPlaceholderBne(this, bne_label);
8275 static_assert(BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET == (kPoisonHeapReferences ? -8 : -4),
8276 "Array LDR must be 1 32-bit instruction (4B) before the return address label; "
8277 " 2 32-bit instructions (8B) for heap poisoning.");
8278 __ ldr(ref_reg, Address(data_reg, index_reg, LSL, scale_factor));
8279 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8280 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8281 __ Bind(&return_address);
8282 return;
8283 }
8284
Roland Levillainc9285912015-12-18 10:38:42 +00008285 // /* HeapReference<Object> */ ref =
8286 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
8287 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008288 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008289}
8290
8291void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8292 Location ref,
8293 Register obj,
8294 uint32_t offset,
8295 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01008296 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00008297 Location temp,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008298 bool needs_null_check,
8299 bool always_update_field,
8300 Register* temp2) {
Roland Levillainc9285912015-12-18 10:38:42 +00008301 DCHECK(kEmitCompilerReadBarrier);
8302 DCHECK(kUseBakerReadBarrier);
8303
Roland Levillain54f869e2017-03-06 13:54:11 +00008304 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8305 // whether we need to enter the slow path to mark the reference.
8306 // Then, in the slow path, check the gray bit in the lock word of
8307 // the reference's holder (`obj`) to decide whether to mark `ref` or
8308 // not.
Roland Levillainc9285912015-12-18 10:38:42 +00008309 //
Roland Levillainba650a42017-03-06 13:52:32 +00008310 // Note that we do not actually check the value of `GetIsGcMarking()`;
8311 // instead, we load into `temp3` the read barrier mark entry point
8312 // corresponding to register `ref`. If `temp3` is null, it means
8313 // that `GetIsGcMarking()` is false, and vice versa.
8314 //
8315 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008316 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8317 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008318 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8319 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8320 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8321 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8322 // if (is_gray) {
8323 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8324 // }
8325 // } else {
8326 // HeapReference<mirror::Object> ref = *src; // Original reference load.
Roland Levillainc9285912015-12-18 10:38:42 +00008327 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008328
Roland Levillain35345a52017-02-27 14:32:08 +00008329 Register temp_reg = temp.AsRegister<Register>();
Roland Levillain1372c9f2017-01-13 11:47:39 +00008330
Roland Levillainba650a42017-03-06 13:52:32 +00008331 // Slow path marking the object `ref` when the GC is marking. The
8332 // entrypoint will already be loaded in `temp3`.
8333 Location temp3 = Location::RegisterLocation(LR);
8334 SlowPathCodeARM* slow_path;
8335 if (always_update_field) {
8336 DCHECK(temp2 != nullptr);
Roland Levillain54f869e2017-03-06 13:54:11 +00008337 // LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM only
8338 // supports address of the form `obj + field_offset`, where `obj`
8339 // is a register and `field_offset` is a register pair (of which
8340 // only the lower half is used). Thus `offset` and `scale_factor`
8341 // above are expected to be null in this code path.
Roland Levillainba650a42017-03-06 13:52:32 +00008342 DCHECK_EQ(offset, 0u);
8343 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Roland Levillain54f869e2017-03-06 13:54:11 +00008344 Location field_offset = index;
8345 slow_path =
8346 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(
8347 instruction,
8348 ref,
8349 obj,
8350 offset,
8351 /* index */ field_offset,
8352 scale_factor,
8353 needs_null_check,
8354 temp_reg,
8355 *temp2,
8356 /* entrypoint */ temp3);
Roland Levillainba650a42017-03-06 13:52:32 +00008357 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +00008358 slow_path = new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM(
8359 instruction,
8360 ref,
8361 obj,
8362 offset,
8363 index,
8364 scale_factor,
8365 needs_null_check,
8366 temp_reg,
8367 /* entrypoint */ temp3);
Roland Levillain35345a52017-02-27 14:32:08 +00008368 }
Roland Levillainba650a42017-03-06 13:52:32 +00008369 AddSlowPath(slow_path);
Roland Levillain35345a52017-02-27 14:32:08 +00008370
Roland Levillainba650a42017-03-06 13:52:32 +00008371 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8372 const int32_t entry_point_offset =
8373 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8374 // Loading the entrypoint does not require a load acquire since it is only changed when
8375 // threads are suspended or running a checkpoint.
8376 __ LoadFromOffset(kLoadWord, temp3.AsRegister<Register>(), TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008377 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8378 // checking GetIsGcMarking.
8379 __ CompareAndBranchIfNonZero(temp3.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +00008380 // Fast path: just load the reference.
8381 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
Roland Levillainba650a42017-03-06 13:52:32 +00008382 __ Bind(slow_path->GetExitLabel());
8383}
Roland Levillain35345a52017-02-27 14:32:08 +00008384
Roland Levillainba650a42017-03-06 13:52:32 +00008385void CodeGeneratorARM::GenerateRawReferenceLoad(HInstruction* instruction,
8386 Location ref,
8387 Register obj,
8388 uint32_t offset,
8389 Location index,
8390 ScaleFactor scale_factor,
8391 bool needs_null_check) {
8392 Register ref_reg = ref.AsRegister<Register>();
8393
Roland Levillainc9285912015-12-18 10:38:42 +00008394 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008395 // Load types involving an "index": ArrayGet,
8396 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8397 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008398 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00008399 if (index.IsConstant()) {
8400 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01008401 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00008402 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
8403 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01008404 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008405 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8406 // intrinsics, which use a register pair as index ("long
8407 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01008408 Register index_reg = index.IsRegisterPair()
8409 ? index.AsRegisterPairLow<Register>()
8410 : index.AsRegister<Register>();
8411 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00008412 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
8413 }
8414 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008415 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillainc9285912015-12-18 10:38:42 +00008416 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
8417 }
8418
Roland Levillainba650a42017-03-06 13:52:32 +00008419 if (needs_null_check) {
8420 MaybeRecordImplicitNullCheck(instruction);
8421 }
8422
Roland Levillainc9285912015-12-18 10:38:42 +00008423 // Object* ref = ref_addr->AsMirrorPtr()
8424 __ MaybeUnpoisonHeapReference(ref_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00008425}
8426
8427void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
8428 Location out,
8429 Location ref,
8430 Location obj,
8431 uint32_t offset,
8432 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008433 DCHECK(kEmitCompilerReadBarrier);
8434
Roland Levillainc9285912015-12-18 10:38:42 +00008435 // Insert a slow path based read barrier *after* the reference load.
8436 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008437 // If heap poisoning is enabled, the unpoisoning of the loaded
8438 // reference will be carried out by the runtime within the slow
8439 // path.
8440 //
8441 // Note that `ref` currently does not get unpoisoned (when heap
8442 // poisoning is enabled), which is alright as the `ref` argument is
8443 // not used by the artReadBarrierSlow entry point.
8444 //
8445 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008446 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00008447 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
8448 AddSlowPath(slow_path);
8449
Roland Levillain3b359c72015-11-17 19:35:12 +00008450 __ b(slow_path->GetEntryLabel());
8451 __ Bind(slow_path->GetExitLabel());
8452}
8453
Roland Levillainc9285912015-12-18 10:38:42 +00008454void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8455 Location out,
8456 Location ref,
8457 Location obj,
8458 uint32_t offset,
8459 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008460 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00008461 // Baker's read barriers shall be handled by the fast path
8462 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
8463 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00008464 // If heap poisoning is enabled, unpoisoning will be taken care of
8465 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00008466 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00008467 } else if (kPoisonHeapReferences) {
8468 __ UnpoisonHeapReference(out.AsRegister<Register>());
8469 }
8470}
8471
Roland Levillainc9285912015-12-18 10:38:42 +00008472void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8473 Location out,
8474 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008475 DCHECK(kEmitCompilerReadBarrier);
8476
Roland Levillainc9285912015-12-18 10:38:42 +00008477 // Insert a slow path based read barrier *after* the GC root load.
8478 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008479 // Note that GC roots are not affected by heap poisoning, so we do
8480 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008481 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00008482 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
8483 AddSlowPath(slow_path);
8484
Roland Levillain3b359c72015-11-17 19:35:12 +00008485 __ b(slow_path->GetEntryLabel());
8486 __ Bind(slow_path->GetExitLabel());
8487}
8488
Vladimir Markodc151b22015-10-15 18:02:30 +01008489HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
8490 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008491 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008492 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01008493}
8494
Vladimir Markob4536b72015-11-24 13:45:23 +00008495Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
8496 Register temp) {
8497 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8498 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8499 if (!invoke->GetLocations()->Intrinsified()) {
8500 return location.AsRegister<Register>();
8501 }
8502 // For intrinsics we allow any location, so it may be on the stack.
8503 if (!location.IsRegister()) {
8504 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
8505 return temp;
8506 }
8507 // For register locations, check if the register was saved. If so, get it from the stack.
8508 // Note: There is a chance that the register was saved but not overwritten, so we could
8509 // save one load. However, since this is just an intrinsic slow path we prefer this
8510 // simple and more robust approach rather that trying to determine if that's the case.
8511 SlowPathCode* slow_path = GetCurrentSlowPath();
TatWai Chongd8c052a2016-11-02 16:12:48 +08008512 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00008513 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
8514 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
8515 return temp;
8516 }
8517 return location.AsRegister<Register>();
8518}
8519
TatWai Chongd8c052a2016-11-02 16:12:48 +08008520Location CodeGeneratorARM::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
8521 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00008522 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
8523 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008524 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8525 uint32_t offset =
8526 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00008527 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008528 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00008529 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008530 }
Vladimir Marko58155012015-08-19 12:49:41 +00008531 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00008532 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008533 break;
8534 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8535 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
8536 break;
Vladimir Markob4536b72015-11-24 13:45:23 +00008537 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
8538 HArmDexCacheArraysBase* base =
8539 invoke->InputAt(invoke->GetSpecialInputIndex())->AsArmDexCacheArraysBase();
8540 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
8541 temp.AsRegister<Register>());
8542 int32_t offset = invoke->GetDexCacheArrayOffset() - base->GetElementOffset();
8543 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), base_reg, offset);
8544 break;
8545 }
Vladimir Marko58155012015-08-19 12:49:41 +00008546 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00008547 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008548 Register method_reg;
8549 Register reg = temp.AsRegister<Register>();
8550 if (current_method.IsRegister()) {
8551 method_reg = current_method.AsRegister<Register>();
8552 } else {
8553 DCHECK(invoke->GetLocations()->Intrinsified());
8554 DCHECK(!current_method.IsValid());
8555 method_reg = reg;
8556 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset);
8557 }
Roland Levillain3b359c72015-11-17 19:35:12 +00008558 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
8559 __ LoadFromOffset(kLoadWord,
8560 reg,
8561 method_reg,
8562 ArtMethod::DexCacheResolvedMethodsOffset(kArmPointerSize).Int32Value());
Vladimir Marko40ecb122016-04-06 17:33:41 +01008563 // temp = temp[index_in_cache];
8564 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
8565 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00008566 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache));
8567 break;
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01008568 }
Vladimir Marko58155012015-08-19 12:49:41 +00008569 }
TatWai Chongd8c052a2016-11-02 16:12:48 +08008570 return callee_method;
8571}
8572
8573void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
8574 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00008575
8576 switch (invoke->GetCodePtrLocation()) {
8577 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
8578 __ bl(GetFrameEntryLabel());
8579 break;
Vladimir Marko58155012015-08-19 12:49:41 +00008580 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8581 // LR = callee_method->entry_point_from_quick_compiled_code_
8582 __ LoadFromOffset(
8583 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07008584 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00008585 // LR()
8586 __ blx(LR);
8587 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08008588 }
8589
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08008590 DCHECK(!IsLeafMethod());
8591}
8592
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008593void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) {
8594 Register temp = temp_location.AsRegister<Register>();
8595 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8596 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00008597
8598 // Use the calling convention instead of the location of the receiver, as
8599 // intrinsics may have put the receiver in a different register. In the intrinsics
8600 // slow path, the arguments have been moved to the right place, so here we are
8601 // guaranteed that the receiver is the first register of the calling convention.
8602 InvokeDexCallingConvention calling_convention;
8603 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008604 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00008605 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00008606 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008607 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00008608 // Instead of simply (possibly) unpoisoning `temp` here, we should
8609 // emit a read barrier for the previous class reference load.
8610 // However this is not required in practice, as this is an
8611 // intermediate/temporary reference and because the current
8612 // concurrent copying collector keeps the from-space memory
8613 // intact/accessible until the end of the marking phase (the
8614 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008615 __ MaybeUnpoisonHeapReference(temp);
8616 // temp = temp->GetMethodAt(method_offset);
8617 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07008618 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00008619 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
8620 // LR = temp->GetEntryPoint();
8621 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
8622 // LR();
8623 __ blx(LR);
8624}
8625
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008626CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008627 const DexFile& dex_file, dex::StringIndex string_index) {
8628 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008629}
8630
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008631CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08008632 const DexFile& dex_file, dex::TypeIndex type_index) {
8633 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008634}
8635
Vladimir Marko1998cd02017-01-13 13:02:58 +00008636CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
8637 const DexFile& dex_file, dex::TypeIndex type_index) {
8638 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
8639}
8640
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008641CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeDexCacheArrayPatch(
8642 const DexFile& dex_file, uint32_t element_offset) {
8643 return NewPcRelativePatch(dex_file, element_offset, &pc_relative_dex_cache_patches_);
8644}
8645
8646CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
8647 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
8648 patches->emplace_back(dex_file, offset_or_index);
8649 return &patches->back();
8650}
8651
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008652Label* CodeGeneratorARM::NewBakerReadBarrierPatch(uint32_t custom_data) {
8653 baker_read_barrier_patches_.emplace_back(custom_data);
8654 return &baker_read_barrier_patches_.back().label;
8655}
8656
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008657Literal* CodeGeneratorARM::DeduplicateBootImageStringLiteral(const DexFile& dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008658 dex::StringIndex string_index) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008659 return boot_image_string_patches_.GetOrCreate(
8660 StringReference(&dex_file, string_index),
8661 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8662}
8663
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008664Literal* CodeGeneratorARM::DeduplicateBootImageTypeLiteral(const DexFile& dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08008665 dex::TypeIndex type_index) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008666 return boot_image_type_patches_.GetOrCreate(
8667 TypeReference(&dex_file, type_index),
8668 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8669}
8670
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008671Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00008672 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008673}
8674
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008675Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008676 dex::StringIndex string_index,
8677 Handle<mirror::String> handle) {
8678 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
8679 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008680 return jit_string_patches_.GetOrCreate(
8681 StringReference(&dex_file, string_index),
8682 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8683}
8684
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008685Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
8686 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008687 Handle<mirror::Class> handle) {
8688 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
8689 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008690 return jit_class_patches_.GetOrCreate(
8691 TypeReference(&dex_file, type_index),
8692 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
8693}
8694
Vladimir Markoaad75c62016-10-03 08:46:48 +00008695template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
8696inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
8697 const ArenaDeque<PcRelativePatchInfo>& infos,
8698 ArenaVector<LinkerPatch>* linker_patches) {
8699 for (const PcRelativePatchInfo& info : infos) {
8700 const DexFile& dex_file = info.target_dex_file;
8701 size_t offset_or_index = info.offset_or_index;
8702 DCHECK(info.add_pc_label.IsBound());
8703 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
8704 // Add MOVW patch.
8705 DCHECK(info.movw_label.IsBound());
8706 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
8707 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
8708 // Add MOVT patch.
8709 DCHECK(info.movt_label.IsBound());
8710 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
8711 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
8712 }
8713}
8714
Vladimir Marko58155012015-08-19 12:49:41 +00008715void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
8716 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00008717 size_t size =
Vladimir Markoaad75c62016-10-03 08:46:48 +00008718 /* MOVW+MOVT for each entry */ 2u * pc_relative_dex_cache_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008719 boot_image_string_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008720 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008721 boot_image_type_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00008722 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008723 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
8724 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00008725 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008726 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
8727 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008728 for (const auto& entry : boot_image_string_patches_) {
8729 const StringReference& target_string = entry.first;
8730 Literal* literal = entry.second;
8731 DCHECK(literal->GetLabel()->IsBound());
8732 uint32_t literal_offset = literal->GetLabel()->Position();
8733 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
8734 target_string.dex_file,
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008735 target_string.string_index.index_));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008736 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00008737 if (!GetCompilerOptions().IsBootImage()) {
Vladimir Marko1998cd02017-01-13 13:02:58 +00008738 DCHECK(pc_relative_type_patches_.empty());
Vladimir Markoaad75c62016-10-03 08:46:48 +00008739 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
8740 linker_patches);
8741 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008742 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
8743 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008744 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
8745 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008746 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008747 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
8748 linker_patches);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008749 for (const auto& entry : boot_image_type_patches_) {
8750 const TypeReference& target_type = entry.first;
8751 Literal* literal = entry.second;
8752 DCHECK(literal->GetLabel()->IsBound());
8753 uint32_t literal_offset = literal->GetLabel()->Position();
8754 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
8755 target_type.dex_file,
Andreas Gampea5b09a62016-11-17 15:21:22 -08008756 target_type.type_index.index_));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01008757 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008758 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
8759 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.Position(),
8760 info.custom_data));
8761 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00008762 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008763}
8764
8765Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
8766 return map->GetOrCreate(
8767 value,
8768 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00008769}
8770
8771Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method,
8772 MethodToLiteralMap* map) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008773 return map->GetOrCreate(
8774 target_method,
8775 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
Vladimir Marko58155012015-08-19 12:49:41 +00008776}
8777
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03008778void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8779 LocationSummary* locations =
8780 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
8781 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
8782 Location::RequiresRegister());
8783 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
8784 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
8785 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8786}
8787
8788void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
8789 LocationSummary* locations = instr->GetLocations();
8790 Register res = locations->Out().AsRegister<Register>();
8791 Register accumulator =
8792 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
8793 Register mul_left =
8794 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
8795 Register mul_right =
8796 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
8797
8798 if (instr->GetOpKind() == HInstruction::kAdd) {
8799 __ mla(res, mul_left, mul_right, accumulator);
8800 } else {
8801 __ mls(res, mul_left, mul_right, accumulator);
8802 }
8803}
8804
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008805void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008806 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008807 LOG(FATAL) << "Unreachable";
8808}
8809
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01008810void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00008811 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00008812 LOG(FATAL) << "Unreachable";
8813}
8814
Mark Mendellfe57faa2015-09-18 09:26:15 -04008815// Simple implementation of packed switch - generate cascaded compare/jumps.
8816void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8817 LocationSummary* locations =
8818 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8819 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008820 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008821 codegen_->GetAssembler()->IsThumb()) {
8822 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
8823 if (switch_instr->GetStartValue() != 0) {
8824 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
8825 }
8826 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008827}
8828
8829void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8830 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008831 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04008832 LocationSummary* locations = switch_instr->GetLocations();
8833 Register value_reg = locations->InAt(0).AsRegister<Register>();
8834 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8835
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008836 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008837 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008838 Register temp_reg = IP;
8839 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
8840 // the immediate, because IP is used as the destination register. For the other
8841 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
8842 // and they can be encoded in the instruction without making use of IP register.
8843 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
8844
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008845 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008846 // Jump to successors[0] if value == lower_bound.
8847 __ b(codegen_->GetLabelOf(successors[0]), EQ);
8848 int32_t last_index = 0;
8849 for (; num_entries - last_index > 2; last_index += 2) {
8850 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
8851 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8852 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
8853 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8854 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
8855 }
8856 if (num_entries - last_index == 2) {
8857 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00008858 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008859 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008860 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04008861
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07008862 // And the default for any other value.
8863 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
8864 __ b(codegen_->GetLabelOf(default_block));
8865 }
8866 } else {
8867 // Create a table lookup.
8868 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
8869
8870 // Materialize a pointer to the switch table
8871 std::vector<Label*> labels(num_entries);
8872 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
8873 for (uint32_t i = 0; i < num_entries; i++) {
8874 labels[i] = codegen_->GetLabelOf(successors[i]);
8875 }
8876 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
8877
8878 // Remove the bias.
8879 Register key_reg;
8880 if (lower_bound != 0) {
8881 key_reg = locations->GetTemp(1).AsRegister<Register>();
8882 __ AddConstant(key_reg, value_reg, -lower_bound);
8883 } else {
8884 key_reg = value_reg;
8885 }
8886
8887 // Check whether the value is in the table, jump to default block if not.
8888 __ CmpConstant(key_reg, num_entries - 1);
8889 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
8890
8891 // Load the displacement from the table.
8892 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
8893
8894 // Dispatch is a direct add to the PC (for Thumb2).
8895 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04008896 }
8897}
8898
Vladimir Markob4536b72015-11-24 13:45:23 +00008899void LocationsBuilderARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8900 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(base);
8901 locations->SetOut(Location::RequiresRegister());
Vladimir Markob4536b72015-11-24 13:45:23 +00008902}
8903
8904void InstructionCodeGeneratorARM::VisitArmDexCacheArraysBase(HArmDexCacheArraysBase* base) {
8905 Register base_reg = base->GetLocations()->Out().AsRegister<Register>();
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008906 CodeGeneratorARM::PcRelativePatchInfo* labels =
8907 codegen_->NewPcRelativeDexCacheArrayPatch(base->GetDexFile(), base->GetElementOffset());
Vladimir Markob4536b72015-11-24 13:45:23 +00008908 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008909 __ movw(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008910 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00008911 __ movt(base_reg, /* placeholder */ 0u);
Vladimir Markob4536b72015-11-24 13:45:23 +00008912 __ BindTrackedLabel(&labels->add_pc_label);
8913 __ add(base_reg, base_reg, ShifterOperand(PC));
8914}
8915
Andreas Gampe85b62f22015-09-09 13:15:38 -07008916void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
8917 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00008918 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07008919 return;
8920 }
8921
8922 DCHECK_NE(type, Primitive::kPrimVoid);
8923
8924 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
8925 if (return_loc.Equals(trg)) {
8926 return;
8927 }
8928
8929 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
8930 // with the last branch.
8931 if (type == Primitive::kPrimLong) {
8932 HParallelMove parallel_move(GetGraph()->GetArena());
8933 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
8934 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
8935 GetMoveResolver()->EmitNativeCode(&parallel_move);
8936 } else if (type == Primitive::kPrimDouble) {
8937 HParallelMove parallel_move(GetGraph()->GetArena());
8938 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
8939 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
8940 GetMoveResolver()->EmitNativeCode(&parallel_move);
8941 } else {
8942 // Let the parallel move resolver take care of all of this.
8943 HParallelMove parallel_move(GetGraph()->GetArena());
8944 parallel_move.AddMove(return_loc, trg, type, nullptr);
8945 GetMoveResolver()->EmitNativeCode(&parallel_move);
8946 }
8947}
8948
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008949void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
8950 LocationSummary* locations =
8951 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8952 locations->SetInAt(0, Location::RequiresRegister());
8953 locations->SetOut(Location::RequiresRegister());
8954}
8955
8956void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
8957 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008958 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008959 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008960 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008961 __ LoadFromOffset(kLoadWord,
8962 locations->Out().AsRegister<Register>(),
8963 locations->InAt(0).AsRegister<Register>(),
8964 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008965 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008966 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008967 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008968 __ LoadFromOffset(kLoadWord,
8969 locations->Out().AsRegister<Register>(),
8970 locations->InAt(0).AsRegister<Register>(),
8971 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
8972 __ LoadFromOffset(kLoadWord,
8973 locations->Out().AsRegister<Register>(),
8974 locations->Out().AsRegister<Register>(),
8975 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008976 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008977}
8978
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008979static void PatchJitRootUse(uint8_t* code,
8980 const uint8_t* roots_data,
8981 Literal* literal,
8982 uint64_t index_in_table) {
8983 DCHECK(literal->GetLabel()->IsBound());
8984 uint32_t literal_offset = literal->GetLabel()->Position();
8985 uintptr_t address =
8986 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
8987 uint8_t* data = code + literal_offset;
8988 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
8989}
8990
Nicolas Geoffray132d8362016-11-16 09:19:42 +00008991void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
8992 for (const auto& entry : jit_string_patches_) {
8993 const auto& it = jit_string_roots_.find(entry.first);
8994 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008995 PatchJitRootUse(code, roots_data, entry.second, it->second);
8996 }
8997 for (const auto& entry : jit_class_patches_) {
8998 const auto& it = jit_class_roots_.find(entry.first);
8999 DCHECK(it != jit_class_roots_.end());
9000 PatchJitRootUse(code, roots_data, entry.second, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009001 }
9002}
9003
Roland Levillain4d027112015-07-01 15:41:14 +01009004#undef __
9005#undef QUICK_ENTRY_POINT
9006
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00009007} // namespace arm
9008} // namespace art