blob: 804bc0f1ff409440eb67a018c8c477d3378f4acc [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 Geoffray76b1e172015-05-27 17:18:33 +010050static constexpr Register kMethodRegisterArgument = R0;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010051
David Brazdil58282f42016-01-14 12:45:10 +000052static constexpr Register kCoreAlwaysSpillRegister = R5;
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000053static constexpr Register kCoreCalleeSaves[] =
Andreas Gampe501fd632015-09-10 16:11:06 -070054 { R5, R6, R7, R8, R10, R11, LR };
Nicolas Geoffray4dee6362015-01-23 18:23:14 +000055static constexpr SRegister kFpuCalleeSaves[] =
56 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010057
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +000058// D31 cannot be split into two S registers, and the register allocator only works on
59// S registers. Therefore there is no need to block it.
60static constexpr DRegister DTMP = D31;
61
Vladimir Markof3e0ee22015-12-17 15:23:13 +000062static constexpr uint32_t kPackedSwitchCompareJumpThreshold = 7;
Andreas Gampe7cffc3b2015-10-19 21:31:53 -070063
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010064// Reference load (except object array loads) is using LDR Rt, [Rn, #offset] which can handle
65// offset < 4KiB. For offsets >= 4KiB, the load shall be emitted as two or more instructions.
66// For the Baker read barrier implementation using link-generated thunks we need to split
67// the offset explicitly.
68constexpr uint32_t kReferenceLoadMinFarOffset = 4 * KB;
69
70// Flags controlling the use of link-time generated thunks for Baker read barriers.
71constexpr bool kBakerReadBarrierLinkTimeThunksEnableForFields = true;
72constexpr bool kBakerReadBarrierLinkTimeThunksEnableForArrays = true;
73constexpr bool kBakerReadBarrierLinkTimeThunksEnableForGcRoots = true;
74
75// The reserved entrypoint register for link-time generated thunks.
76const Register kBakerCcEntrypointRegister = R4;
77
Roland Levillain7cbd27f2016-08-11 23:53:33 +010078// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
79#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070080#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmPointerSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010081
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010082static inline void CheckLastTempIsBakerCcEntrypointRegister(HInstruction* instruction) {
83 DCHECK_EQ(static_cast<uint32_t>(kBakerCcEntrypointRegister),
84 linker::Thumb2RelativePatcher::kBakerCcEntrypointRegister);
85 DCHECK_NE(instruction->GetLocations()->GetTempCount(), 0u);
86 DCHECK_EQ(kBakerCcEntrypointRegister,
87 instruction->GetLocations()->GetTemp(
88 instruction->GetLocations()->GetTempCount() - 1u).AsRegister<Register>());
89}
90
91static inline void EmitPlaceholderBne(CodeGeneratorARM* codegen, Label* bne_label) {
Vladimir Marko88abba22017-05-03 17:09:25 +010092 ScopedForce32Bit force_32bit(down_cast<Thumb2Assembler*>(codegen->GetAssembler()));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +010093 __ BindTrackedLabel(bne_label);
94 Label placeholder_label;
95 __ b(&placeholder_label, NE); // Placeholder, patched at link-time.
96 __ Bind(&placeholder_label);
97}
98
Vladimir Marko88abba22017-05-03 17:09:25 +010099static inline bool CanEmitNarrowLdr(Register rt, Register rn, uint32_t offset) {
100 return ArmAssembler::IsLowRegister(rt) && ArmAssembler::IsLowRegister(rn) && offset < 32u;
101}
102
Artem Serovf4d6aee2016-07-11 10:41:45 +0100103static constexpr int kRegListThreshold = 4;
104
Artem Serovd300d8f2016-07-15 14:00:56 +0100105// SaveLiveRegisters and RestoreLiveRegisters from SlowPathCodeARM operate on sets of S registers,
106// for each live D registers they treat two corresponding S registers as live ones.
107//
108// Two following functions (SaveContiguousSRegisterList, RestoreContiguousSRegisterList) build
109// from a list of contiguous S registers a list of contiguous D registers (processing first/last
110// S registers corner cases) and save/restore this new list treating them as D registers.
111// - decreasing code size
112// - avoiding hazards on Cortex-A57, when a pair of S registers for an actual live D register is
113// restored and then used in regular non SlowPath code as D register.
114//
115// For the following example (v means the S register is live):
116// D names: | D0 | D1 | D2 | D4 | ...
117// S names: | S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7 | ...
118// Live? | | v | v | v | v | v | v | | ...
119//
120// S1 and S6 will be saved/restored independently; D registers list (D1, D2) will be processed
121// as D registers.
122static size_t SaveContiguousSRegisterList(size_t first,
123 size_t last,
124 CodeGenerator* codegen,
125 size_t stack_offset) {
126 DCHECK_LE(first, last);
127 if ((first == last) && (first == 0)) {
128 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first);
129 return stack_offset;
130 }
131 if (first % 2 == 1) {
132 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, first++);
133 }
134
135 bool save_last = false;
136 if (last % 2 == 0) {
137 save_last = true;
138 --last;
139 }
140
141 if (first < last) {
142 DRegister d_reg = static_cast<DRegister>(first / 2);
143 DCHECK_EQ((last - first + 1) % 2, 0u);
144 size_t number_of_d_regs = (last - first + 1) / 2;
145
146 if (number_of_d_regs == 1) {
Scott Wakelinga7812ae2016-10-17 10:03:36 +0100147 __ StoreDToOffset(d_reg, SP, stack_offset);
Artem Serovd300d8f2016-07-15 14:00:56 +0100148 } else if (number_of_d_regs > 1) {
149 __ add(IP, SP, ShifterOperand(stack_offset));
150 __ vstmiad(IP, d_reg, number_of_d_regs);
151 }
152 stack_offset += number_of_d_regs * kArmWordSize * 2;
153 }
154
155 if (save_last) {
156 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, last + 1);
157 }
158
159 return stack_offset;
160}
161
162static size_t RestoreContiguousSRegisterList(size_t first,
163 size_t last,
164 CodeGenerator* codegen,
165 size_t stack_offset) {
166 DCHECK_LE(first, last);
167 if ((first == last) && (first == 0)) {
168 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first);
169 return stack_offset;
170 }
171 if (first % 2 == 1) {
172 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, first++);
173 }
174
175 bool restore_last = false;
176 if (last % 2 == 0) {
177 restore_last = true;
178 --last;
179 }
180
181 if (first < last) {
182 DRegister d_reg = static_cast<DRegister>(first / 2);
183 DCHECK_EQ((last - first + 1) % 2, 0u);
184 size_t number_of_d_regs = (last - first + 1) / 2;
185 if (number_of_d_regs == 1) {
186 __ LoadDFromOffset(d_reg, SP, stack_offset);
187 } else if (number_of_d_regs > 1) {
188 __ add(IP, SP, ShifterOperand(stack_offset));
189 __ vldmiad(IP, d_reg, number_of_d_regs);
190 }
191 stack_offset += number_of_d_regs * kArmWordSize * 2;
192 }
193
194 if (restore_last) {
195 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, last + 1);
196 }
197
198 return stack_offset;
199}
200
Artem Serovf4d6aee2016-07-11 10:41:45 +0100201void SlowPathCodeARM::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
202 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
203 size_t orig_offset = stack_offset;
204
205 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
206 for (uint32_t i : LowToHighBits(core_spills)) {
207 // If the register holds an object, update the stack mask.
208 if (locations->RegisterContainsObject(i)) {
209 locations->SetStackBit(stack_offset / kVRegSize);
210 }
211 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
212 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
213 saved_core_stack_offsets_[i] = stack_offset;
214 stack_offset += kArmWordSize;
215 }
216
217 int reg_num = POPCOUNT(core_spills);
218 if (reg_num != 0) {
219 if (reg_num > kRegListThreshold) {
220 __ StoreList(RegList(core_spills), orig_offset);
221 } else {
222 stack_offset = orig_offset;
223 for (uint32_t i : LowToHighBits(core_spills)) {
224 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
225 }
226 }
227 }
228
Artem Serovd300d8f2016-07-15 14:00:56 +0100229 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
230 orig_offset = stack_offset;
Vladimir Marko804b03f2016-09-14 16:26:36 +0100231 for (uint32_t i : LowToHighBits(fp_spills)) {
Artem Serovf4d6aee2016-07-11 10:41:45 +0100232 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
233 saved_fpu_stack_offsets_[i] = stack_offset;
Artem Serovd300d8f2016-07-15 14:00:56 +0100234 stack_offset += kArmWordSize;
Artem Serovf4d6aee2016-07-11 10:41:45 +0100235 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100236
237 stack_offset = orig_offset;
238 while (fp_spills != 0u) {
239 uint32_t begin = CTZ(fp_spills);
240 uint32_t tmp = fp_spills + (1u << begin);
241 fp_spills &= tmp; // Clear the contiguous range of 1s.
242 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
243 stack_offset = SaveContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
244 }
245 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100246}
247
248void SlowPathCodeARM::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
249 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
250 size_t orig_offset = stack_offset;
251
252 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true);
253 for (uint32_t i : LowToHighBits(core_spills)) {
254 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
255 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
256 stack_offset += kArmWordSize;
257 }
258
259 int reg_num = POPCOUNT(core_spills);
260 if (reg_num != 0) {
261 if (reg_num > kRegListThreshold) {
262 __ LoadList(RegList(core_spills), orig_offset);
263 } else {
264 stack_offset = orig_offset;
265 for (uint32_t i : LowToHighBits(core_spills)) {
266 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
267 }
268 }
269 }
270
Artem Serovd300d8f2016-07-15 14:00:56 +0100271 uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false);
272 while (fp_spills != 0u) {
273 uint32_t begin = CTZ(fp_spills);
274 uint32_t tmp = fp_spills + (1u << begin);
275 fp_spills &= tmp; // Clear the contiguous range of 1s.
276 uint32_t end = (tmp == 0u) ? 32u : CTZ(tmp); // CTZ(0) is undefined.
277 stack_offset = RestoreContiguousSRegisterList(begin, end - 1, codegen, stack_offset);
Artem Serovf4d6aee2016-07-11 10:41:45 +0100278 }
Artem Serovd300d8f2016-07-15 14:00:56 +0100279 DCHECK_LE(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Artem Serovf4d6aee2016-07-11 10:41:45 +0100280}
281
282class NullCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100283 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100284 explicit NullCheckSlowPathARM(HNullCheck* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100285
Alexandre Rames67555f72014-11-18 10:55:16 +0000286 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100287 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100288 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000289 if (instruction_->CanThrowIntoCatchBlock()) {
290 // Live registers will be restored in the catch block if caught.
291 SaveLiveRegisters(codegen, instruction_->GetLocations());
292 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100293 arm_codegen->InvokeRuntime(kQuickThrowNullPointer,
294 instruction_,
295 instruction_->GetDexPc(),
296 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000297 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100298 }
299
Alexandre Rames8158f282015-08-07 10:26:17 +0100300 bool IsFatal() const OVERRIDE { return true; }
301
Alexandre Rames9931f312015-06-19 14:47:01 +0100302 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; }
303
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100304 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100305 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM);
306};
307
Artem Serovf4d6aee2016-07-11 10:41:45 +0100308class DivZeroCheckSlowPathARM : public SlowPathCodeARM {
Calin Juravled0d48522014-11-04 16:40:20 +0000309 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100310 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : SlowPathCodeARM(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000311
Alexandre Rames67555f72014-11-18 10:55:16 +0000312 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000313 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
314 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100315 arm_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000316 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +0000317 }
318
Alexandre Rames8158f282015-08-07 10:26:17 +0100319 bool IsFatal() const OVERRIDE { return true; }
320
Alexandre Rames9931f312015-06-19 14:47:01 +0100321 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; }
322
Calin Juravled0d48522014-11-04 16:40:20 +0000323 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000324 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM);
325};
326
Artem Serovf4d6aee2016-07-11 10:41:45 +0100327class SuspendCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000328 public:
Alexandre Rames67555f72014-11-18 10:55:16 +0000329 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100330 : SlowPathCodeARM(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000331
Alexandre Rames67555f72014-11-18 10:55:16 +0000332 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100333 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000334 __ Bind(GetEntryLabel());
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100335 arm_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000336 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100337 if (successor_ == nullptr) {
338 __ b(GetReturnLabel());
339 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100340 __ b(arm_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100341 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000342 }
343
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100344 Label* GetReturnLabel() {
345 DCHECK(successor_ == nullptr);
346 return &return_label_;
347 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000348
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100349 HBasicBlock* GetSuccessor() const {
350 return successor_;
351 }
352
Alexandre Rames9931f312015-06-19 14:47:01 +0100353 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; }
354
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000355 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100356 // If not null, the block to branch to after the suspend check.
357 HBasicBlock* const successor_;
358
359 // If `successor_` is null, the label to branch to after the suspend check.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000360 Label return_label_;
361
362 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM);
363};
364
Artem Serovf4d6aee2016-07-11 10:41:45 +0100365class BoundsCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100366 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100367 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100368 : SlowPathCodeARM(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100369
Alexandre Rames67555f72014-11-18 10:55:16 +0000370 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100371 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100372 LocationSummary* locations = instruction_->GetLocations();
373
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100374 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +0000375 if (instruction_->CanThrowIntoCatchBlock()) {
376 // Live registers will be restored in the catch block if caught.
377 SaveLiveRegisters(codegen, instruction_->GetLocations());
378 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000379 // We're moving two locations to locations that could overlap, so we need a parallel
380 // move resolver.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100381 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000382 codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100383 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000384 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100385 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100386 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100387 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
388 Primitive::kPrimInt);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100389 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
390 ? kQuickThrowStringBounds
391 : kQuickThrowArrayBounds;
392 arm_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100393 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000394 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100395 }
396
Alexandre Rames8158f282015-08-07 10:26:17 +0100397 bool IsFatal() const OVERRIDE { return true; }
398
Alexandre Rames9931f312015-06-19 14:47:01 +0100399 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; }
400
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100401 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100402 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM);
403};
404
Artem Serovf4d6aee2016-07-11 10:41:45 +0100405class LoadClassSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100406 public:
Vladimir Markoea4c1262017-02-06 19:59:33 +0000407 LoadClassSlowPathARM(HLoadClass* cls, HInstruction* at, uint32_t dex_pc, bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000408 : SlowPathCodeARM(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000409 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
410 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100411
Alexandre Rames67555f72014-11-18 10:55:16 +0000412 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000413 LocationSummary* locations = instruction_->GetLocations();
Vladimir Markoea4c1262017-02-06 19:59:33 +0000414 Location out = locations->Out();
415 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000416
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100417 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
418 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000419 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100420
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100421 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoea4c1262017-02-06 19:59:33 +0000422 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
423 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
424 bool is_load_class_bss_entry =
425 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
426 Register entry_address = kNoRegister;
427 if (is_load_class_bss_entry && call_saves_everything_except_r0) {
428 Register temp = locations->GetTemp(0).AsRegister<Register>();
429 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
430 // the kSaveEverything call.
431 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
432 entry_address = temp_is_r0 ? out.AsRegister<Register>() : temp;
433 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
434 if (temp_is_r0) {
435 __ mov(entry_address, ShifterOperand(temp));
436 }
437 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000438 dex::TypeIndex type_index = cls_->GetTypeIndex();
439 __ LoadImmediate(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100440 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
441 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000442 arm_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000443 if (do_clinit_) {
444 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
445 } else {
446 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
447 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000448
Vladimir Markoea4c1262017-02-06 19:59:33 +0000449 // For HLoadClass/kBssEntry, store the resolved Class to the BSS entry.
450 if (is_load_class_bss_entry) {
451 if (call_saves_everything_except_r0) {
452 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
453 __ str(R0, Address(entry_address));
454 } else {
455 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
456 Register temp = IP;
457 CodeGeneratorARM::PcRelativePatchInfo* labels =
458 arm_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
459 __ BindTrackedLabel(&labels->movw_label);
460 __ movw(temp, /* placeholder */ 0u);
461 __ BindTrackedLabel(&labels->movt_label);
462 __ movt(temp, /* placeholder */ 0u);
463 __ BindTrackedLabel(&labels->add_pc_label);
464 __ add(temp, temp, ShifterOperand(PC));
465 __ str(R0, Address(temp));
466 }
467 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000468 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000469 if (out.IsValid()) {
470 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000471 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
472 }
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000473 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100474 __ b(GetExitLabel());
475 }
476
Alexandre Rames9931f312015-06-19 14:47:01 +0100477 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; }
478
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100479 private:
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000480 // The class this slow path will load.
481 HLoadClass* const cls_;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100482
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000483 // The dex PC of `at_`.
484 const uint32_t dex_pc_;
485
486 // Whether to initialize the class.
487 const bool do_clinit_;
488
489 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100490};
491
Vladimir Markoaad75c62016-10-03 08:46:48 +0000492class LoadStringSlowPathARM : public SlowPathCodeARM {
493 public:
494 explicit LoadStringSlowPathARM(HLoadString* instruction) : SlowPathCodeARM(instruction) {}
495
496 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Markoea4c1262017-02-06 19:59:33 +0000497 DCHECK(instruction_->IsLoadString());
498 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000499 LocationSummary* locations = instruction_->GetLocations();
500 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100501 HLoadString* load = instruction_->AsLoadString();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000502 const dex::StringIndex string_index = load->GetStringIndex();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100503 Register out = locations->Out().AsRegister<Register>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100504 constexpr bool call_saves_everything_except_r0 = (!kUseReadBarrier || kUseBakerReadBarrier);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000505
506 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
507 __ Bind(GetEntryLabel());
508 SaveLiveRegisters(codegen, locations);
509
510 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100511 // In the unlucky case that the `temp` is R0, we preserve the address in `out` across
Vladimir Markoea4c1262017-02-06 19:59:33 +0000512 // the kSaveEverything call.
513 Register entry_address = kNoRegister;
514 if (call_saves_everything_except_r0) {
515 Register temp = locations->GetTemp(0).AsRegister<Register>();
516 bool temp_is_r0 = (temp == calling_convention.GetRegisterAt(0));
517 entry_address = temp_is_r0 ? out : temp;
518 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
519 if (temp_is_r0) {
520 __ mov(entry_address, ShifterOperand(temp));
521 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100522 }
523
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000524 __ LoadImmediate(calling_convention.GetRegisterAt(0), string_index.index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000525 arm_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
526 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100527
528 // Store the resolved String to the .bss entry.
529 if (call_saves_everything_except_r0) {
530 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
531 __ str(R0, Address(entry_address));
532 } else {
533 // For non-Baker read barrier, we need to re-calculate the address of the string entry.
Vladimir Markoea4c1262017-02-06 19:59:33 +0000534 Register temp = IP;
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100535 CodeGeneratorARM::PcRelativePatchInfo* labels =
536 arm_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
537 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000538 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100539 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000540 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100541 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +0000542 __ add(temp, temp, ShifterOperand(PC));
543 __ str(R0, Address(temp));
Vladimir Marko94ce9c22016-09-30 14:50:51 +0100544 }
545
Vladimir Markoaad75c62016-10-03 08:46:48 +0000546 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
Vladimir Markoaad75c62016-10-03 08:46:48 +0000547 RestoreLiveRegisters(codegen, locations);
548
Vladimir Markoaad75c62016-10-03 08:46:48 +0000549 __ b(GetExitLabel());
550 }
551
552 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; }
553
554 private:
555 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM);
556};
557
Artem Serovf4d6aee2016-07-11 10:41:45 +0100558class TypeCheckSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000559 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000560 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100561 : SlowPathCodeARM(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000562
Alexandre Rames67555f72014-11-18 10:55:16 +0000563 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000564 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000565 DCHECK(instruction_->IsCheckCast()
566 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000567
568 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
569 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000570
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000571 if (!is_fatal_) {
572 SaveLiveRegisters(codegen, locations);
573 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000574
575 // We're moving two locations to locations that could overlap, so we need a parallel
576 // move resolver.
577 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800578 codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800579 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
580 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800581 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800582 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
583 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000584 if (instruction_->IsInstanceOf()) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100585 arm_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100586 instruction_,
587 instruction_->GetDexPc(),
588 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800589 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000590 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0));
591 } else {
592 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800593 arm_codegen->InvokeRuntime(kQuickCheckInstanceOf,
594 instruction_,
595 instruction_->GetDexPc(),
596 this);
597 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000598 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000599
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000600 if (!is_fatal_) {
601 RestoreLiveRegisters(codegen, locations);
602 __ b(GetExitLabel());
603 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000604 }
605
Alexandre Rames9931f312015-06-19 14:47:01 +0100606 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; }
607
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000608 bool IsFatal() const OVERRIDE { return is_fatal_; }
609
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000610 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000611 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000612
613 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM);
614};
615
Artem Serovf4d6aee2016-07-11 10:41:45 +0100616class DeoptimizationSlowPathARM : public SlowPathCodeARM {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700617 public:
Aart Bik42249c32016-01-07 15:33:50 -0800618 explicit DeoptimizationSlowPathARM(HDeoptimize* instruction)
Artem Serovf4d6aee2016-07-11 10:41:45 +0100619 : SlowPathCodeARM(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700620
621 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800622 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700623 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100624 LocationSummary* locations = instruction_->GetLocations();
625 SaveLiveRegisters(codegen, locations);
626 InvokeRuntimeCallingConvention calling_convention;
627 __ LoadImmediate(calling_convention.GetRegisterAt(0),
628 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100629 arm_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100630 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700631 }
632
Alexandre Rames9931f312015-06-19 14:47:01 +0100633 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; }
634
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700635 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700636 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM);
637};
638
Artem Serovf4d6aee2016-07-11 10:41:45 +0100639class ArraySetSlowPathARM : public SlowPathCodeARM {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100640 public:
Artem Serovf4d6aee2016-07-11 10:41:45 +0100641 explicit ArraySetSlowPathARM(HInstruction* instruction) : SlowPathCodeARM(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100642
643 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
644 LocationSummary* locations = instruction_->GetLocations();
645 __ Bind(GetEntryLabel());
646 SaveLiveRegisters(codegen, locations);
647
648 InvokeRuntimeCallingConvention calling_convention;
649 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
650 parallel_move.AddMove(
651 locations->InAt(0),
652 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
653 Primitive::kPrimNot,
654 nullptr);
655 parallel_move.AddMove(
656 locations->InAt(1),
657 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
658 Primitive::kPrimInt,
659 nullptr);
660 parallel_move.AddMove(
661 locations->InAt(2),
662 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
663 Primitive::kPrimNot,
664 nullptr);
665 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
666
667 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +0100668 arm_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000669 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100670 RestoreLiveRegisters(codegen, locations);
671 __ b(GetExitLabel());
672 }
673
674 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; }
675
676 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100677 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM);
678};
679
Roland Levillain54f869e2017-03-06 13:54:11 +0000680// Abstract base class for read barrier slow paths marking a reference
681// `ref`.
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000682//
Roland Levillain54f869e2017-03-06 13:54:11 +0000683// Argument `entrypoint` must be a register location holding the read
684// barrier marking runtime entry point to be invoked.
685class ReadBarrierMarkSlowPathBaseARM : public SlowPathCodeARM {
686 protected:
687 ReadBarrierMarkSlowPathBaseARM(HInstruction* instruction, Location ref, Location entrypoint)
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000688 : SlowPathCodeARM(instruction), ref_(ref), entrypoint_(entrypoint) {
689 DCHECK(kEmitCompilerReadBarrier);
690 }
691
Roland Levillain54f869e2017-03-06 13:54:11 +0000692 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathBaseARM"; }
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000693
Roland Levillain54f869e2017-03-06 13:54:11 +0000694 // Generate assembly code calling the read barrier marking runtime
695 // entry point (ReadBarrierMarkRegX).
696 void GenerateReadBarrierMarkRuntimeCall(CodeGenerator* codegen) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000697 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain47b3ab22017-02-27 14:31:35 +0000698
Roland Levillain47b3ab22017-02-27 14:31:35 +0000699 // No need to save live registers; it's taken care of by the
700 // entrypoint. Also, there is no need to update the stack mask,
701 // as this runtime call will not trigger a garbage collection.
702 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
703 DCHECK_NE(ref_reg, SP);
704 DCHECK_NE(ref_reg, LR);
705 DCHECK_NE(ref_reg, PC);
706 // IP is used internally by the ReadBarrierMarkRegX entry point
707 // as a temporary, it cannot be the entry point's input/output.
708 DCHECK_NE(ref_reg, IP);
709 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCoreRegisters) << ref_reg;
710 // "Compact" slow path, saving two moves.
711 //
712 // Instead of using the standard runtime calling convention (input
713 // and output in R0):
714 //
715 // R0 <- ref
716 // R0 <- ReadBarrierMark(R0)
717 // ref <- R0
718 //
719 // we just use rX (the register containing `ref`) as input and output
720 // of a dedicated entrypoint:
721 //
722 // rX <- ReadBarrierMarkRegX(rX)
723 //
724 if (entrypoint_.IsValid()) {
725 arm_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
726 __ blx(entrypoint_.AsRegister<Register>());
727 } else {
Roland Levillain54f869e2017-03-06 13:54:11 +0000728 // Entrypoint is not already loaded, load from the thread.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000729 int32_t entry_point_offset =
730 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref_reg);
731 // This runtime call does not require a stack map.
732 arm_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
733 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000734 }
735
736 // The location (register) of the marked object reference.
737 const Location ref_;
738
739 // The location of the entrypoint if it is already loaded.
740 const Location entrypoint_;
741
742 private:
743 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathBaseARM);
744};
745
Dave Allison20dfc792014-06-16 20:44:29 -0700746// Slow path marking an object reference `ref` during a read
747// barrier. The field `obj.field` in the object `obj` holding this
Roland Levillain54f869e2017-03-06 13:54:11 +0000748// reference does not get updated by this slow path after marking.
Dave Allison20dfc792014-06-16 20:44:29 -0700749//
750// This means that after the execution of this slow path, `ref` will
751// always be up-to-date, but `obj.field` may not; i.e., after the
752// flip, `ref` will be a to-space reference, but `obj.field` will
753// probably still be a from-space reference (unless it gets updated by
754// another thread, or if another thread installed another object
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000755// reference (different from `ref`) in `obj.field`).
756//
757// If `entrypoint` is a valid location it is assumed to already be
758// holding the entrypoint. The case where the entrypoint is passed in
Roland Levillainba650a42017-03-06 13:52:32 +0000759// is when the decision to mark is based on whether the GC is marking.
Roland Levillain54f869e2017-03-06 13:54:11 +0000760class ReadBarrierMarkSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000761 public:
762 ReadBarrierMarkSlowPathARM(HInstruction* instruction,
763 Location ref,
764 Location entrypoint = Location::NoLocation())
Roland Levillain54f869e2017-03-06 13:54:11 +0000765 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint) {
Roland Levillain27b1f9c2017-01-17 16:56:34 +0000766 DCHECK(kEmitCompilerReadBarrier);
767 }
768
769 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathARM"; }
770
771 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
772 LocationSummary* locations = instruction_->GetLocations();
Roland Levillain54f869e2017-03-06 13:54:11 +0000773 DCHECK(locations->CanCall());
774 if (kIsDebugBuild) {
775 Register ref_reg = ref_.AsRegister<Register>();
776 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
777 }
778 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
779 << "Unexpected instruction in read barrier marking slow path: "
780 << instruction_->DebugName();
781
782 __ Bind(GetEntryLabel());
783 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000784 __ b(GetExitLabel());
785 }
786
787 private:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000788 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathARM);
789};
790
Roland Levillain54f869e2017-03-06 13:54:11 +0000791// Slow path loading `obj`'s lock word, loading a reference from
792// object `*(obj + offset + (index << scale_factor))` into `ref`, and
793// marking `ref` if `obj` is gray according to the lock word (Baker
794// read barrier). The field `obj.field` in the object `obj` holding
795// this reference does not get updated by this slow path after marking
796// (see LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
797// below for that).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000798//
Roland Levillain54f869e2017-03-06 13:54:11 +0000799// This means that after the execution of this slow path, `ref` will
800// always be up-to-date, but `obj.field` may not; i.e., after the
801// flip, `ref` will be a to-space reference, but `obj.field` will
802// probably still be a from-space reference (unless it gets updated by
803// another thread, or if another thread installed another object
804// reference (different from `ref`) in `obj.field`).
805//
806// Argument `entrypoint` must be a register location holding the read
807// barrier marking runtime entry point to be invoked.
808class LoadReferenceWithBakerReadBarrierSlowPathARM : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000809 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000810 LoadReferenceWithBakerReadBarrierSlowPathARM(HInstruction* instruction,
811 Location ref,
812 Register obj,
813 uint32_t offset,
814 Location index,
815 ScaleFactor scale_factor,
816 bool needs_null_check,
817 Register temp,
818 Location entrypoint)
819 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000820 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000821 offset_(offset),
822 index_(index),
823 scale_factor_(scale_factor),
824 needs_null_check_(needs_null_check),
825 temp_(temp) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000826 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000827 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000828 }
829
Roland Levillain54f869e2017-03-06 13:54:11 +0000830 const char* GetDescription() const OVERRIDE {
831 return "LoadReferenceWithBakerReadBarrierSlowPathARM";
832 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000833
834 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
835 LocationSummary* locations = instruction_->GetLocations();
836 Register ref_reg = ref_.AsRegister<Register>();
837 DCHECK(locations->CanCall());
838 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000839 DCHECK_NE(ref_reg, temp_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000840 DCHECK(instruction_->IsInstanceFieldGet() ||
841 instruction_->IsStaticFieldGet() ||
842 instruction_->IsArrayGet() ||
843 instruction_->IsArraySet() ||
Roland Levillain47b3ab22017-02-27 14:31:35 +0000844 instruction_->IsInstanceOf() ||
845 instruction_->IsCheckCast() ||
846 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
847 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
848 << "Unexpected instruction in read barrier marking slow path: "
849 << instruction_->DebugName();
850 // The read barrier instrumentation of object ArrayGet
851 // instructions does not support the HIntermediateAddress
852 // instruction.
853 DCHECK(!(instruction_->IsArrayGet() &&
854 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
855
856 __ Bind(GetEntryLabel());
Roland Levillain54f869e2017-03-06 13:54:11 +0000857
858 // When using MaybeGenerateReadBarrierSlow, the read barrier call is
859 // inserted after the original load. However, in fast path based
860 // Baker's read barriers, we need to perform the load of
861 // mirror::Object::monitor_ *before* the original reference load.
862 // This load-load ordering is required by the read barrier.
Roland Levillainff487002017-03-07 16:50:01 +0000863 // The slow path (for Baker's algorithm) should look like:
Roland Levillain47b3ab22017-02-27 14:31:35 +0000864 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000865 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
866 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
867 // HeapReference<mirror::Object> ref = *src; // Original reference load.
868 // bool is_gray = (rb_state == ReadBarrier::GrayState());
869 // if (is_gray) {
870 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
871 // }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000872 //
Roland Levillain54f869e2017-03-06 13:54:11 +0000873 // Note: the original implementation in ReadBarrier::Barrier is
874 // slightly more complex as it performs additional checks that we do
875 // not do here for performance reasons.
876
877 // /* int32_t */ monitor = obj->monitor_
878 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
879 __ LoadFromOffset(kLoadWord, temp_, obj_, monitor_offset);
880 if (needs_null_check_) {
881 codegen->MaybeRecordImplicitNullCheck(instruction_);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000882 }
Roland Levillain54f869e2017-03-06 13:54:11 +0000883 // /* LockWord */ lock_word = LockWord(monitor)
884 static_assert(sizeof(LockWord) == sizeof(int32_t),
885 "art::LockWord and int32_t have different sizes.");
886
887 // Introduce a dependency on the lock_word including the rb_state,
888 // which shall prevent load-load reordering without using
889 // a memory barrier (which would be more expensive).
890 // `obj` is unchanged by this operation, but its value now depends
891 // on `temp`.
892 __ add(obj_, obj_, ShifterOperand(temp_, LSR, 32));
893
894 // The actual reference load.
895 // A possible implicit null check has already been handled above.
896 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
897 arm_codegen->GenerateRawReferenceLoad(
898 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
899
900 // Mark the object `ref` when `obj` is gray.
901 //
902 // if (rb_state == ReadBarrier::GrayState())
903 // ref = ReadBarrier::Mark(ref);
904 //
905 // Given the numeric representation, it's enough to check the low bit of the
906 // rb_state. We do that by shifting the bit out of the lock word with LSRS
907 // which can be a 16-bit instruction unlike the TST immediate.
908 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
909 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
910 __ Lsrs(temp_, temp_, LockWord::kReadBarrierStateShift + 1);
911 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
912 GenerateReadBarrierMarkRuntimeCall(codegen);
913
Roland Levillain47b3ab22017-02-27 14:31:35 +0000914 __ b(GetExitLabel());
915 }
916
917 private:
Roland Levillain54f869e2017-03-06 13:54:11 +0000918 // The register containing the object holding the marked object reference field.
919 Register obj_;
920 // The offset, index and scale factor to access the reference in `obj_`.
921 uint32_t offset_;
922 Location index_;
923 ScaleFactor scale_factor_;
924 // Is a null check required?
925 bool needs_null_check_;
926 // A temporary register used to hold the lock word of `obj_`.
927 Register temp_;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000928
Roland Levillain54f869e2017-03-06 13:54:11 +0000929 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierSlowPathARM);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000930};
931
Roland Levillain54f869e2017-03-06 13:54:11 +0000932// Slow path loading `obj`'s lock word, loading a reference from
933// object `*(obj + offset + (index << scale_factor))` into `ref`, and
934// marking `ref` if `obj` is gray according to the lock word (Baker
935// read barrier). If needed, this slow path also atomically updates
936// the field `obj.field` in the object `obj` holding this reference
937// after marking (contrary to
938// LoadReferenceWithBakerReadBarrierSlowPathARM above, which never
939// tries to update `obj.field`).
Roland Levillain47b3ab22017-02-27 14:31:35 +0000940//
941// This means that after the execution of this slow path, both `ref`
942// and `obj.field` will be up-to-date; i.e., after the flip, both will
943// hold the same to-space reference (unless another thread installed
944// another object reference (different from `ref`) in `obj.field`).
Roland Levillainba650a42017-03-06 13:52:32 +0000945//
Roland Levillain54f869e2017-03-06 13:54:11 +0000946// Argument `entrypoint` must be a register location holding the read
947// barrier marking runtime entry point to be invoked.
948class LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM
949 : public ReadBarrierMarkSlowPathBaseARM {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000950 public:
Roland Levillain54f869e2017-03-06 13:54:11 +0000951 LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(HInstruction* instruction,
952 Location ref,
953 Register obj,
954 uint32_t offset,
955 Location index,
956 ScaleFactor scale_factor,
957 bool needs_null_check,
958 Register temp1,
959 Register temp2,
960 Location entrypoint)
961 : ReadBarrierMarkSlowPathBaseARM(instruction, ref, entrypoint),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000962 obj_(obj),
Roland Levillain54f869e2017-03-06 13:54:11 +0000963 offset_(offset),
964 index_(index),
965 scale_factor_(scale_factor),
966 needs_null_check_(needs_null_check),
Roland Levillain47b3ab22017-02-27 14:31:35 +0000967 temp1_(temp1),
Roland Levillain54f869e2017-03-06 13:54:11 +0000968 temp2_(temp2) {
Roland Levillain47b3ab22017-02-27 14:31:35 +0000969 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain54f869e2017-03-06 13:54:11 +0000970 DCHECK(kUseBakerReadBarrier);
Roland Levillain47b3ab22017-02-27 14:31:35 +0000971 }
972
Roland Levillain54f869e2017-03-06 13:54:11 +0000973 const char* GetDescription() const OVERRIDE {
974 return "LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM";
975 }
Roland Levillain47b3ab22017-02-27 14:31:35 +0000976
977 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
978 LocationSummary* locations = instruction_->GetLocations();
979 Register ref_reg = ref_.AsRegister<Register>();
980 DCHECK(locations->CanCall());
981 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain54f869e2017-03-06 13:54:11 +0000982 DCHECK_NE(ref_reg, temp1_);
983
984 // This slow path is only used by the UnsafeCASObject intrinsic at the moment.
Roland Levillain47b3ab22017-02-27 14:31:35 +0000985 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
986 << "Unexpected instruction in read barrier marking and field updating slow path: "
987 << instruction_->DebugName();
988 DCHECK(instruction_->GetLocations()->Intrinsified());
989 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
Roland Levillain54f869e2017-03-06 13:54:11 +0000990 DCHECK_EQ(offset_, 0u);
991 DCHECK_EQ(scale_factor_, ScaleFactor::TIMES_1);
992 // The location of the offset of the marked reference field within `obj_`.
993 Location field_offset = index_;
994 DCHECK(field_offset.IsRegisterPair()) << field_offset;
Roland Levillain47b3ab22017-02-27 14:31:35 +0000995
996 __ Bind(GetEntryLabel());
997
Roland Levillainff487002017-03-07 16:50:01 +0000998 // The implementation is similar to LoadReferenceWithBakerReadBarrierSlowPathARM's:
999 //
1000 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
1001 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
1002 // HeapReference<mirror::Object> ref = *src; // Original reference load.
1003 // bool is_gray = (rb_state == ReadBarrier::GrayState());
1004 // if (is_gray) {
1005 // old_ref = ref;
1006 // ref = entrypoint(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
1007 // compareAndSwapObject(obj, field_offset, old_ref, ref);
1008 // }
1009
Roland Levillain54f869e2017-03-06 13:54:11 +00001010 // /* int32_t */ monitor = obj->monitor_
1011 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
1012 __ LoadFromOffset(kLoadWord, temp1_, obj_, monitor_offset);
1013 if (needs_null_check_) {
1014 codegen->MaybeRecordImplicitNullCheck(instruction_);
1015 }
1016 // /* LockWord */ lock_word = LockWord(monitor)
1017 static_assert(sizeof(LockWord) == sizeof(int32_t),
1018 "art::LockWord and int32_t have different sizes.");
1019
1020 // Introduce a dependency on the lock_word including the rb_state,
1021 // which shall prevent load-load reordering without using
1022 // a memory barrier (which would be more expensive).
1023 // `obj` is unchanged by this operation, but its value now depends
1024 // on `temp1`.
1025 __ add(obj_, obj_, ShifterOperand(temp1_, LSR, 32));
1026
1027 // The actual reference load.
1028 // A possible implicit null check has already been handled above.
1029 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1030 arm_codegen->GenerateRawReferenceLoad(
1031 instruction_, ref_, obj_, offset_, index_, scale_factor_, /* needs_null_check */ false);
1032
1033 // Mark the object `ref` when `obj` is gray.
1034 //
1035 // if (rb_state == ReadBarrier::GrayState())
1036 // ref = ReadBarrier::Mark(ref);
1037 //
1038 // Given the numeric representation, it's enough to check the low bit of the
1039 // rb_state. We do that by shifting the bit out of the lock word with LSRS
1040 // which can be a 16-bit instruction unlike the TST immediate.
1041 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
1042 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
1043 __ Lsrs(temp1_, temp1_, LockWord::kReadBarrierStateShift + 1);
1044 __ b(GetExitLabel(), CC); // Carry flag is the last bit shifted out by LSRS.
1045
1046 // Save the old value of the reference before marking it.
Roland Levillain47b3ab22017-02-27 14:31:35 +00001047 // Note that we cannot use IP to save the old reference, as IP is
1048 // used internally by the ReadBarrierMarkRegX entry point, and we
1049 // need the old reference after the call to that entry point.
1050 DCHECK_NE(temp1_, IP);
1051 __ Mov(temp1_, ref_reg);
Roland Levillain27b1f9c2017-01-17 16:56:34 +00001052
Roland Levillain54f869e2017-03-06 13:54:11 +00001053 GenerateReadBarrierMarkRuntimeCall(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001054
1055 // If the new reference is different from the old reference,
Roland Levillain54f869e2017-03-06 13:54:11 +00001056 // update the field in the holder (`*(obj_ + field_offset)`).
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001057 //
1058 // Note that this field could also hold a different object, if
1059 // another thread had concurrently changed it. In that case, the
1060 // LDREX/SUBS/ITNE sequence of instructions in the compare-and-set
1061 // (CAS) operation below would abort the CAS, leaving the field
1062 // as-is.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001063 __ cmp(temp1_, ShifterOperand(ref_reg));
Roland Levillain54f869e2017-03-06 13:54:11 +00001064 __ b(GetExitLabel(), EQ);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001065
1066 // Update the the holder's field atomically. This may fail if
1067 // mutator updates before us, but it's OK. This is achieved
1068 // using a strong compare-and-set (CAS) operation with relaxed
1069 // memory synchronization ordering, where the expected value is
1070 // the old reference and the desired value is the new reference.
1071
1072 // Convenience aliases.
1073 Register base = obj_;
1074 // The UnsafeCASObject intrinsic uses a register pair as field
1075 // offset ("long offset"), of which only the low part contains
1076 // data.
Roland Levillain54f869e2017-03-06 13:54:11 +00001077 Register offset = field_offset.AsRegisterPairLow<Register>();
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001078 Register expected = temp1_;
1079 Register value = ref_reg;
1080 Register tmp_ptr = IP; // Pointer to actual memory.
1081 Register tmp = temp2_; // Value in memory.
1082
1083 __ add(tmp_ptr, base, ShifterOperand(offset));
1084
1085 if (kPoisonHeapReferences) {
1086 __ PoisonHeapReference(expected);
1087 if (value == expected) {
1088 // Do not poison `value`, as it is the same register as
1089 // `expected`, which has just been poisoned.
1090 } else {
1091 __ PoisonHeapReference(value);
1092 }
1093 }
1094
1095 // do {
1096 // tmp = [r_ptr] - expected;
1097 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
1098
Roland Levillain24a4d112016-10-26 13:10:46 +01001099 Label loop_head, exit_loop;
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001100 __ Bind(&loop_head);
1101
1102 __ ldrex(tmp, tmp_ptr);
1103
1104 __ subs(tmp, tmp, ShifterOperand(expected));
1105
Roland Levillain24a4d112016-10-26 13:10:46 +01001106 __ it(NE);
1107 __ clrex(NE);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001108
Roland Levillain24a4d112016-10-26 13:10:46 +01001109 __ b(&exit_loop, NE);
1110
1111 __ strex(tmp, value, tmp_ptr);
1112 __ cmp(tmp, ShifterOperand(1));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001113 __ b(&loop_head, EQ);
1114
Roland Levillain24a4d112016-10-26 13:10:46 +01001115 __ Bind(&exit_loop);
1116
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001117 if (kPoisonHeapReferences) {
1118 __ UnpoisonHeapReference(expected);
1119 if (value == expected) {
1120 // Do not unpoison `value`, as it is the same register as
1121 // `expected`, which has just been unpoisoned.
1122 } else {
1123 __ UnpoisonHeapReference(value);
1124 }
1125 }
1126
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001127 __ b(GetExitLabel());
1128 }
1129
1130 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001131 // The register containing the object holding the marked object reference field.
1132 const Register obj_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001133 // The offset, index and scale factor to access the reference in `obj_`.
1134 uint32_t offset_;
1135 Location index_;
1136 ScaleFactor scale_factor_;
1137 // Is a null check required?
1138 bool needs_null_check_;
1139 // A temporary register used to hold the lock word of `obj_`; and
1140 // also to hold the original reference value, when the reference is
1141 // marked.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001142 const Register temp1_;
Roland Levillain54f869e2017-03-06 13:54:11 +00001143 // A temporary register used in the implementation of the CAS, to
1144 // update the object's reference field.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001145 const Register temp2_;
1146
Roland Levillain54f869e2017-03-06 13:54:11 +00001147 DISALLOW_COPY_AND_ASSIGN(LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM);
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001148};
1149
Roland Levillain3b359c72015-11-17 19:35:12 +00001150// Slow path generating a read barrier for a heap reference.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001151class ReadBarrierForHeapReferenceSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001152 public:
1153 ReadBarrierForHeapReferenceSlowPathARM(HInstruction* instruction,
1154 Location out,
1155 Location ref,
1156 Location obj,
1157 uint32_t offset,
1158 Location index)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001159 : SlowPathCodeARM(instruction),
Roland Levillain3b359c72015-11-17 19:35:12 +00001160 out_(out),
1161 ref_(ref),
1162 obj_(obj),
1163 offset_(offset),
1164 index_(index) {
1165 DCHECK(kEmitCompilerReadBarrier);
1166 // If `obj` is equal to `out` or `ref`, it means the initial object
1167 // has been overwritten by (or after) the heap object reference load
1168 // to be instrumented, e.g.:
1169 //
1170 // __ LoadFromOffset(kLoadWord, out, out, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00001171 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00001172 //
1173 // In that case, we have lost the information about the original
1174 // object, and the emitted read barrier cannot work properly.
1175 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
1176 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
1177 }
1178
1179 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1180 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1181 LocationSummary* locations = instruction_->GetLocations();
1182 Register reg_out = out_.AsRegister<Register>();
1183 DCHECK(locations->CanCall());
1184 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +01001185 DCHECK(instruction_->IsInstanceFieldGet() ||
1186 instruction_->IsStaticFieldGet() ||
1187 instruction_->IsArrayGet() ||
1188 instruction_->IsInstanceOf() ||
1189 instruction_->IsCheckCast() ||
Andreas Gamped9911ee2017-03-27 13:27:24 -07001190 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
Roland Levillainc9285912015-12-18 10:38:42 +00001191 << "Unexpected instruction in read barrier for heap reference slow path: "
1192 << instruction_->DebugName();
Roland Levillain19c54192016-11-04 13:44:09 +00001193 // The read barrier instrumentation of object ArrayGet
1194 // instructions does not support the HIntermediateAddress
1195 // instruction.
1196 DCHECK(!(instruction_->IsArrayGet() &&
1197 instruction_->AsArrayGet()->GetArray()->IsIntermediateAddress()));
Roland Levillain3b359c72015-11-17 19:35:12 +00001198
1199 __ Bind(GetEntryLabel());
1200 SaveLiveRegisters(codegen, locations);
1201
1202 // We may have to change the index's value, but as `index_` is a
1203 // constant member (like other "inputs" of this slow path),
1204 // introduce a copy of it, `index`.
1205 Location index = index_;
1206 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +01001207 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain3b359c72015-11-17 19:35:12 +00001208 if (instruction_->IsArrayGet()) {
1209 // Compute the actual memory offset and store it in `index`.
1210 Register index_reg = index_.AsRegister<Register>();
1211 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
1212 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
1213 // We are about to change the value of `index_reg` (see the
1214 // calls to art::arm::Thumb2Assembler::Lsl and
1215 // art::arm::Thumb2Assembler::AddConstant below), but it has
1216 // not been saved by the previous call to
1217 // art::SlowPathCode::SaveLiveRegisters, as it is a
1218 // callee-save register --
1219 // art::SlowPathCode::SaveLiveRegisters does not consider
1220 // callee-save registers, as it has been designed with the
1221 // assumption that callee-save registers are supposed to be
1222 // handled by the called function. So, as a callee-save
1223 // register, `index_reg` _would_ eventually be saved onto
1224 // the stack, but it would be too late: we would have
1225 // changed its value earlier. Therefore, we manually save
1226 // it here into another freely available register,
1227 // `free_reg`, chosen of course among the caller-save
1228 // registers (as a callee-save `free_reg` register would
1229 // exhibit the same problem).
1230 //
1231 // Note we could have requested a temporary register from
1232 // the register allocator instead; but we prefer not to, as
1233 // this is a slow path, and we know we can find a
1234 // caller-save register that is available.
1235 Register free_reg = FindAvailableCallerSaveRegister(codegen);
1236 __ Mov(free_reg, index_reg);
1237 index_reg = free_reg;
1238 index = Location::RegisterLocation(index_reg);
1239 } else {
1240 // The initial register stored in `index_` has already been
1241 // saved in the call to art::SlowPathCode::SaveLiveRegisters
1242 // (as it is not a callee-save register), so we can freely
1243 // use it.
1244 }
1245 // Shifting the index value contained in `index_reg` by the scale
1246 // factor (2) cannot overflow in practice, as the runtime is
1247 // unable to allocate object arrays with a size larger than
1248 // 2^26 - 1 (that is, 2^28 - 4 bytes).
1249 __ Lsl(index_reg, index_reg, TIMES_4);
1250 static_assert(
1251 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
1252 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
1253 __ AddConstant(index_reg, index_reg, offset_);
1254 } else {
Roland Levillain3d312422016-06-23 13:53:42 +01001255 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
1256 // intrinsics, `index_` is not shifted by a scale factor of 2
1257 // (as in the case of ArrayGet), as it is actually an offset
1258 // to an object field within an object.
1259 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001260 DCHECK(instruction_->GetLocations()->Intrinsified());
1261 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
1262 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
1263 << instruction_->AsInvoke()->GetIntrinsic();
1264 DCHECK_EQ(offset_, 0U);
1265 DCHECK(index_.IsRegisterPair());
1266 // UnsafeGet's offset location is a register pair, the low
1267 // part contains the correct offset.
1268 index = index_.ToLow();
1269 }
1270 }
1271
1272 // We're moving two or three locations to locations that could
1273 // overlap, so we need a parallel move resolver.
1274 InvokeRuntimeCallingConvention calling_convention;
1275 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
1276 parallel_move.AddMove(ref_,
1277 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1278 Primitive::kPrimNot,
1279 nullptr);
1280 parallel_move.AddMove(obj_,
1281 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
1282 Primitive::kPrimNot,
1283 nullptr);
1284 if (index.IsValid()) {
1285 parallel_move.AddMove(index,
1286 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
1287 Primitive::kPrimInt,
1288 nullptr);
1289 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1290 } else {
1291 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
1292 __ LoadImmediate(calling_convention.GetRegisterAt(2), offset_);
1293 }
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001294 arm_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain3b359c72015-11-17 19:35:12 +00001295 CheckEntrypointTypes<
1296 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
1297 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1298
1299 RestoreLiveRegisters(codegen, locations);
1300 __ b(GetExitLabel());
1301 }
1302
1303 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathARM"; }
1304
1305 private:
1306 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1307 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1308 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1309 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1310 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
1311 return static_cast<Register>(i);
1312 }
1313 }
1314 // We shall never fail to find a free caller-save register, as
1315 // there are more than two core caller-save registers on ARM
1316 // (meaning it is possible to find one which is different from
1317 // `ref` and `obj`).
1318 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1319 LOG(FATAL) << "Could not find a free caller-save register";
1320 UNREACHABLE();
1321 }
1322
Roland Levillain3b359c72015-11-17 19:35:12 +00001323 const Location out_;
1324 const Location ref_;
1325 const Location obj_;
1326 const uint32_t offset_;
1327 // An additional location containing an index to an array.
1328 // Only used for HArrayGet and the UnsafeGetObject &
1329 // UnsafeGetObjectVolatile intrinsics.
1330 const Location index_;
1331
1332 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathARM);
1333};
1334
1335// Slow path generating a read barrier for a GC root.
Artem Serovf4d6aee2016-07-11 10:41:45 +01001336class ReadBarrierForRootSlowPathARM : public SlowPathCodeARM {
Roland Levillain3b359c72015-11-17 19:35:12 +00001337 public:
1338 ReadBarrierForRootSlowPathARM(HInstruction* instruction, Location out, Location root)
Artem Serovf4d6aee2016-07-11 10:41:45 +01001339 : SlowPathCodeARM(instruction), out_(out), root_(root) {
Roland Levillainc9285912015-12-18 10:38:42 +00001340 DCHECK(kEmitCompilerReadBarrier);
1341 }
Roland Levillain3b359c72015-11-17 19:35:12 +00001342
1343 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1344 LocationSummary* locations = instruction_->GetLocations();
1345 Register reg_out = out_.AsRegister<Register>();
1346 DCHECK(locations->CanCall());
1347 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillainc9285912015-12-18 10:38:42 +00001348 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1349 << "Unexpected instruction in read barrier for GC root slow path: "
1350 << instruction_->DebugName();
Roland Levillain3b359c72015-11-17 19:35:12 +00001351
1352 __ Bind(GetEntryLabel());
1353 SaveLiveRegisters(codegen, locations);
1354
1355 InvokeRuntimeCallingConvention calling_convention;
1356 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen);
1357 arm_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01001358 arm_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain3b359c72015-11-17 19:35:12 +00001359 instruction_,
1360 instruction_->GetDexPc(),
1361 this);
1362 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1363 arm_codegen->Move32(out_, Location::RegisterLocation(R0));
1364
1365 RestoreLiveRegisters(codegen, locations);
1366 __ b(GetExitLabel());
1367 }
1368
1369 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathARM"; }
1370
1371 private:
Roland Levillain3b359c72015-11-17 19:35:12 +00001372 const Location out_;
1373 const Location root_;
1374
1375 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathARM);
1376};
1377
Aart Bike9f37602015-10-09 11:15:55 -07001378inline Condition ARMCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001379 switch (cond) {
1380 case kCondEQ: return EQ;
1381 case kCondNE: return NE;
1382 case kCondLT: return LT;
1383 case kCondLE: return LE;
1384 case kCondGT: return GT;
1385 case kCondGE: return GE;
Aart Bike9f37602015-10-09 11:15:55 -07001386 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
Aart Bike9f37602015-10-09 11:15:55 -07001395// Maps signed condition to unsigned condition.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001396inline Condition ARMUnsignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -07001397 switch (cond) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001398 case kCondEQ: return EQ;
1399 case kCondNE: return NE;
Aart Bike9f37602015-10-09 11:15:55 -07001400 // Signed to unsigned.
Roland Levillain4fa13f62015-07-06 18:11:54 +01001401 case kCondLT: return LO;
1402 case kCondLE: return LS;
1403 case kCondGT: return HI;
1404 case kCondGE: return HS;
Aart Bike9f37602015-10-09 11:15:55 -07001405 // Unsigned remain unchanged.
1406 case kCondB: return LO;
1407 case kCondBE: return LS;
1408 case kCondA: return HI;
1409 case kCondAE: return HS;
Dave Allison20dfc792014-06-16 20:44:29 -07001410 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001411 LOG(FATAL) << "Unreachable";
1412 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -07001413}
1414
Vladimir Markod6e069b2016-01-18 11:11:01 +00001415inline Condition ARMFPCondition(IfCondition cond, bool gt_bias) {
1416 // The ARM condition codes can express all the necessary branches, see the
1417 // "Meaning (floating-point)" column in the table A8-1 of the ARMv7 reference manual.
1418 // There is no dex instruction or HIR that would need the missing conditions
1419 // "equal or unordered" or "not equal".
1420 switch (cond) {
1421 case kCondEQ: return EQ;
1422 case kCondNE: return NE /* unordered */;
1423 case kCondLT: return gt_bias ? CC : LT /* unordered */;
1424 case kCondLE: return gt_bias ? LS : LE /* unordered */;
1425 case kCondGT: return gt_bias ? HI /* unordered */ : GT;
1426 case kCondGE: return gt_bias ? CS /* unordered */ : GE;
1427 default:
1428 LOG(FATAL) << "UNREACHABLE";
1429 UNREACHABLE();
1430 }
1431}
1432
Anton Kirilov74234da2017-01-13 14:42:47 +00001433inline Shift ShiftFromOpKind(HDataProcWithShifterOp::OpKind op_kind) {
1434 switch (op_kind) {
1435 case HDataProcWithShifterOp::kASR: return ASR;
1436 case HDataProcWithShifterOp::kLSL: return LSL;
1437 case HDataProcWithShifterOp::kLSR: return LSR;
1438 default:
1439 LOG(FATAL) << "Unexpected op kind " << op_kind;
1440 UNREACHABLE();
1441 }
1442}
1443
1444static void GenerateDataProcInstruction(HInstruction::InstructionKind kind,
1445 Register out,
1446 Register first,
1447 const ShifterOperand& second,
1448 CodeGeneratorARM* codegen) {
1449 if (second.IsImmediate() && second.GetImmediate() == 0) {
1450 const ShifterOperand in = kind == HInstruction::kAnd
1451 ? ShifterOperand(0)
1452 : ShifterOperand(first);
1453
1454 __ mov(out, in);
1455 } else {
1456 switch (kind) {
1457 case HInstruction::kAdd:
1458 __ add(out, first, second);
1459 break;
1460 case HInstruction::kAnd:
1461 __ and_(out, first, second);
1462 break;
1463 case HInstruction::kOr:
1464 __ orr(out, first, second);
1465 break;
1466 case HInstruction::kSub:
1467 __ sub(out, first, second);
1468 break;
1469 case HInstruction::kXor:
1470 __ eor(out, first, second);
1471 break;
1472 default:
1473 LOG(FATAL) << "Unexpected instruction kind: " << kind;
1474 UNREACHABLE();
1475 }
1476 }
1477}
1478
1479static void GenerateDataProc(HInstruction::InstructionKind kind,
1480 const Location& out,
1481 const Location& first,
1482 const ShifterOperand& second_lo,
1483 const ShifterOperand& second_hi,
1484 CodeGeneratorARM* codegen) {
1485 const Register first_hi = first.AsRegisterPairHigh<Register>();
1486 const Register first_lo = first.AsRegisterPairLow<Register>();
1487 const Register out_hi = out.AsRegisterPairHigh<Register>();
1488 const Register out_lo = out.AsRegisterPairLow<Register>();
1489
1490 if (kind == HInstruction::kAdd) {
1491 __ adds(out_lo, first_lo, second_lo);
1492 __ adc(out_hi, first_hi, second_hi);
1493 } else if (kind == HInstruction::kSub) {
1494 __ subs(out_lo, first_lo, second_lo);
1495 __ sbc(out_hi, first_hi, second_hi);
1496 } else {
1497 GenerateDataProcInstruction(kind, out_lo, first_lo, second_lo, codegen);
1498 GenerateDataProcInstruction(kind, out_hi, first_hi, second_hi, codegen);
1499 }
1500}
1501
1502static ShifterOperand GetShifterOperand(Register rm, Shift shift, uint32_t shift_imm) {
1503 return shift_imm == 0 ? ShifterOperand(rm) : ShifterOperand(rm, shift, shift_imm);
1504}
1505
1506static void GenerateLongDataProc(HDataProcWithShifterOp* instruction, CodeGeneratorARM* codegen) {
1507 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
1508 DCHECK(HDataProcWithShifterOp::IsShiftOp(instruction->GetOpKind()));
1509
1510 const LocationSummary* const locations = instruction->GetLocations();
1511 const uint32_t shift_value = instruction->GetShiftAmount();
1512 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
1513 const Location first = locations->InAt(0);
1514 const Location second = locations->InAt(1);
1515 const Location out = locations->Out();
1516 const Register first_hi = first.AsRegisterPairHigh<Register>();
1517 const Register first_lo = first.AsRegisterPairLow<Register>();
1518 const Register out_hi = out.AsRegisterPairHigh<Register>();
1519 const Register out_lo = out.AsRegisterPairLow<Register>();
1520 const Register second_hi = second.AsRegisterPairHigh<Register>();
1521 const Register second_lo = second.AsRegisterPairLow<Register>();
1522 const Shift shift = ShiftFromOpKind(instruction->GetOpKind());
1523
1524 if (shift_value >= 32) {
1525 if (shift == LSL) {
1526 GenerateDataProcInstruction(kind,
1527 out_hi,
1528 first_hi,
1529 ShifterOperand(second_lo, LSL, shift_value - 32),
1530 codegen);
1531 GenerateDataProcInstruction(kind,
1532 out_lo,
1533 first_lo,
1534 ShifterOperand(0),
1535 codegen);
1536 } else if (shift == ASR) {
1537 GenerateDataProc(kind,
1538 out,
1539 first,
1540 GetShifterOperand(second_hi, ASR, shift_value - 32),
1541 ShifterOperand(second_hi, ASR, 31),
1542 codegen);
1543 } else {
1544 DCHECK_EQ(shift, LSR);
1545 GenerateDataProc(kind,
1546 out,
1547 first,
1548 GetShifterOperand(second_hi, LSR, shift_value - 32),
1549 ShifterOperand(0),
1550 codegen);
1551 }
1552 } else {
1553 DCHECK_GT(shift_value, 1U);
1554 DCHECK_LT(shift_value, 32U);
1555
1556 if (shift == LSL) {
1557 // We are not doing this for HInstruction::kAdd because the output will require
1558 // Location::kOutputOverlap; not applicable to other cases.
1559 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1560 GenerateDataProcInstruction(kind,
1561 out_hi,
1562 first_hi,
1563 ShifterOperand(second_hi, LSL, shift_value),
1564 codegen);
1565 GenerateDataProcInstruction(kind,
1566 out_hi,
1567 out_hi,
1568 ShifterOperand(second_lo, LSR, 32 - shift_value),
1569 codegen);
1570 GenerateDataProcInstruction(kind,
1571 out_lo,
1572 first_lo,
1573 ShifterOperand(second_lo, LSL, shift_value),
1574 codegen);
1575 } else {
1576 __ Lsl(IP, second_hi, shift_value);
1577 __ orr(IP, IP, ShifterOperand(second_lo, LSR, 32 - shift_value));
1578 GenerateDataProc(kind,
1579 out,
1580 first,
1581 ShifterOperand(second_lo, LSL, shift_value),
1582 ShifterOperand(IP),
1583 codegen);
1584 }
1585 } else {
1586 DCHECK(shift == ASR || shift == LSR);
1587
1588 // We are not doing this for HInstruction::kAdd because the output will require
1589 // Location::kOutputOverlap; not applicable to other cases.
1590 if (kind == HInstruction::kOr || kind == HInstruction::kXor) {
1591 GenerateDataProcInstruction(kind,
1592 out_lo,
1593 first_lo,
1594 ShifterOperand(second_lo, LSR, shift_value),
1595 codegen);
1596 GenerateDataProcInstruction(kind,
1597 out_lo,
1598 out_lo,
1599 ShifterOperand(second_hi, LSL, 32 - shift_value),
1600 codegen);
1601 GenerateDataProcInstruction(kind,
1602 out_hi,
1603 first_hi,
1604 ShifterOperand(second_hi, shift, shift_value),
1605 codegen);
1606 } else {
1607 __ Lsr(IP, second_lo, shift_value);
1608 __ orr(IP, IP, ShifterOperand(second_hi, LSL, 32 - shift_value));
1609 GenerateDataProc(kind,
1610 out,
1611 first,
1612 ShifterOperand(IP),
1613 ShifterOperand(second_hi, shift, shift_value),
1614 codegen);
1615 }
1616 }
1617 }
1618}
1619
Donghui Bai426b49c2016-11-08 14:55:38 +08001620static void GenerateVcmp(HInstruction* instruction, CodeGeneratorARM* codegen) {
1621 Primitive::Type type = instruction->InputAt(0)->GetType();
1622 Location lhs_loc = instruction->GetLocations()->InAt(0);
1623 Location rhs_loc = instruction->GetLocations()->InAt(1);
1624 if (rhs_loc.IsConstant()) {
1625 // 0.0 is the only immediate that can be encoded directly in
1626 // a VCMP instruction.
1627 //
1628 // Both the JLS (section 15.20.1) and the JVMS (section 6.5)
1629 // specify that in a floating-point comparison, positive zero
1630 // and negative zero are considered equal, so we can use the
1631 // literal 0.0 for both cases here.
1632 //
1633 // Note however that some methods (Float.equal, Float.compare,
1634 // Float.compareTo, Double.equal, Double.compare,
1635 // Double.compareTo, Math.max, Math.min, StrictMath.max,
1636 // StrictMath.min) consider 0.0 to be (strictly) greater than
1637 // -0.0. So if we ever translate calls to these methods into a
1638 // HCompare instruction, we must handle the -0.0 case with
1639 // care here.
1640 DCHECK(rhs_loc.GetConstant()->IsArithmeticZero());
1641 if (type == Primitive::kPrimFloat) {
1642 __ vcmpsz(lhs_loc.AsFpuRegister<SRegister>());
1643 } else {
1644 DCHECK_EQ(type, Primitive::kPrimDouble);
1645 __ vcmpdz(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()));
1646 }
1647 } else {
1648 if (type == Primitive::kPrimFloat) {
1649 __ vcmps(lhs_loc.AsFpuRegister<SRegister>(), rhs_loc.AsFpuRegister<SRegister>());
1650 } else {
1651 DCHECK_EQ(type, Primitive::kPrimDouble);
1652 __ vcmpd(FromLowSToD(lhs_loc.AsFpuRegisterPairLow<SRegister>()),
1653 FromLowSToD(rhs_loc.AsFpuRegisterPairLow<SRegister>()));
1654 }
1655 }
1656}
1657
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001658static int64_t AdjustConstantForCondition(int64_t value,
1659 IfCondition* condition,
1660 IfCondition* opposite) {
1661 if (value == 1) {
1662 if (*condition == kCondB) {
1663 value = 0;
1664 *condition = kCondEQ;
1665 *opposite = kCondNE;
1666 } else if (*condition == kCondAE) {
1667 value = 0;
1668 *condition = kCondNE;
1669 *opposite = kCondEQ;
1670 }
1671 } else if (value == -1) {
1672 if (*condition == kCondGT) {
1673 value = 0;
1674 *condition = kCondGE;
1675 *opposite = kCondLT;
1676 } else if (*condition == kCondLE) {
1677 value = 0;
1678 *condition = kCondLT;
1679 *opposite = kCondGE;
1680 }
1681 }
1682
1683 return value;
1684}
1685
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001686static std::pair<Condition, Condition> GenerateLongTestConstant(HCondition* condition,
1687 bool invert,
1688 CodeGeneratorARM* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001689 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1690
1691 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001692 IfCondition cond = condition->GetCondition();
1693 IfCondition opposite = condition->GetOppositeCondition();
1694
1695 if (invert) {
1696 std::swap(cond, opposite);
1697 }
1698
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001699 std::pair<Condition, Condition> ret(EQ, NE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001700 const Location left = locations->InAt(0);
1701 const Location right = locations->InAt(1);
1702
1703 DCHECK(right.IsConstant());
1704
1705 const Register left_high = left.AsRegisterPairHigh<Register>();
1706 const Register left_low = left.AsRegisterPairLow<Register>();
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001707 int64_t value = AdjustConstantForCondition(right.GetConstant()->AsLongConstant()->GetValue(),
1708 &cond,
1709 &opposite);
1710
1711 // Comparisons against 0 are common enough to deserve special attention.
1712 if (value == 0) {
1713 switch (cond) {
1714 case kCondNE:
1715 // x > 0 iff x != 0 when the comparison is unsigned.
1716 case kCondA:
1717 ret = std::make_pair(NE, EQ);
1718 FALLTHROUGH_INTENDED;
1719 case kCondEQ:
1720 // x <= 0 iff x == 0 when the comparison is unsigned.
1721 case kCondBE:
1722 __ orrs(IP, left_low, ShifterOperand(left_high));
1723 return ret;
1724 case kCondLT:
1725 case kCondGE:
1726 __ cmp(left_high, ShifterOperand(0));
1727 return std::make_pair(ARMCondition(cond), ARMCondition(opposite));
1728 // Trivially true or false.
1729 case kCondB:
1730 ret = std::make_pair(NE, EQ);
1731 FALLTHROUGH_INTENDED;
1732 case kCondAE:
1733 __ cmp(left_low, ShifterOperand(left_low));
1734 return ret;
1735 default:
1736 break;
1737 }
1738 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001739
1740 switch (cond) {
1741 case kCondEQ:
1742 case kCondNE:
1743 case kCondB:
1744 case kCondBE:
1745 case kCondA:
1746 case kCondAE:
1747 __ CmpConstant(left_high, High32Bits(value));
1748 __ it(EQ);
1749 __ cmp(left_low, ShifterOperand(Low32Bits(value)), EQ);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001750 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001751 break;
1752 case kCondLE:
1753 case kCondGT:
1754 // Trivially true or false.
1755 if (value == std::numeric_limits<int64_t>::max()) {
1756 __ cmp(left_low, ShifterOperand(left_low));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001757 ret = cond == kCondLE ? std::make_pair(EQ, NE) : std::make_pair(NE, EQ);
Donghui Bai426b49c2016-11-08 14:55:38 +08001758 break;
1759 }
1760
1761 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001762 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001763 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001764 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001765 } else {
1766 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001767 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001768 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001769 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001770 }
1771
1772 value++;
1773 FALLTHROUGH_INTENDED;
1774 case kCondGE:
1775 case kCondLT:
1776 __ CmpConstant(left_low, Low32Bits(value));
1777 __ sbcs(IP, left_high, ShifterOperand(High32Bits(value)));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001778 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001779 break;
1780 default:
1781 LOG(FATAL) << "Unreachable";
1782 UNREACHABLE();
1783 }
1784
1785 return ret;
1786}
1787
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001788static std::pair<Condition, Condition> GenerateLongTest(HCondition* condition,
1789 bool invert,
1790 CodeGeneratorARM* codegen) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001791 DCHECK_EQ(condition->GetLeft()->GetType(), Primitive::kPrimLong);
1792
1793 const LocationSummary* const locations = condition->GetLocations();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001794 IfCondition cond = condition->GetCondition();
1795 IfCondition opposite = condition->GetOppositeCondition();
1796
1797 if (invert) {
1798 std::swap(cond, opposite);
1799 }
1800
1801 std::pair<Condition, Condition> ret;
Donghui Bai426b49c2016-11-08 14:55:38 +08001802 Location left = locations->InAt(0);
1803 Location right = locations->InAt(1);
1804
1805 DCHECK(right.IsRegisterPair());
1806
1807 switch (cond) {
1808 case kCondEQ:
1809 case kCondNE:
1810 case kCondB:
1811 case kCondBE:
1812 case kCondA:
1813 case kCondAE:
1814 __ cmp(left.AsRegisterPairHigh<Register>(),
1815 ShifterOperand(right.AsRegisterPairHigh<Register>()));
1816 __ it(EQ);
1817 __ cmp(left.AsRegisterPairLow<Register>(),
1818 ShifterOperand(right.AsRegisterPairLow<Register>()),
1819 EQ);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001820 ret = std::make_pair(ARMUnsignedCondition(cond), ARMUnsignedCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001821 break;
1822 case kCondLE:
1823 case kCondGT:
1824 if (cond == kCondLE) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001825 DCHECK_EQ(opposite, kCondGT);
Donghui Bai426b49c2016-11-08 14:55:38 +08001826 cond = kCondGE;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001827 opposite = kCondLT;
Donghui Bai426b49c2016-11-08 14:55:38 +08001828 } else {
1829 DCHECK_EQ(cond, kCondGT);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001830 DCHECK_EQ(opposite, kCondLE);
Donghui Bai426b49c2016-11-08 14:55:38 +08001831 cond = kCondLT;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001832 opposite = kCondGE;
Donghui Bai426b49c2016-11-08 14:55:38 +08001833 }
1834
1835 std::swap(left, right);
1836 FALLTHROUGH_INTENDED;
1837 case kCondGE:
1838 case kCondLT:
1839 __ cmp(left.AsRegisterPairLow<Register>(),
1840 ShifterOperand(right.AsRegisterPairLow<Register>()));
1841 __ sbcs(IP,
1842 left.AsRegisterPairHigh<Register>(),
1843 ShifterOperand(right.AsRegisterPairHigh<Register>()));
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001844 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001845 break;
1846 default:
1847 LOG(FATAL) << "Unreachable";
1848 UNREACHABLE();
1849 }
1850
1851 return ret;
1852}
1853
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001854static std::pair<Condition, Condition> GenerateTest(HCondition* condition,
1855 bool invert,
1856 CodeGeneratorARM* codegen) {
1857 const LocationSummary* const locations = condition->GetLocations();
1858 const Primitive::Type type = condition->GetLeft()->GetType();
1859 IfCondition cond = condition->GetCondition();
1860 IfCondition opposite = condition->GetOppositeCondition();
1861 std::pair<Condition, Condition> ret;
1862 const Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08001863
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001864 if (invert) {
1865 std::swap(cond, opposite);
1866 }
Donghui Bai426b49c2016-11-08 14:55:38 +08001867
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001868 if (type == Primitive::kPrimLong) {
1869 ret = locations->InAt(1).IsConstant()
1870 ? GenerateLongTestConstant(condition, invert, codegen)
1871 : GenerateLongTest(condition, invert, codegen);
1872 } else if (Primitive::IsFloatingPointType(type)) {
1873 GenerateVcmp(condition, codegen);
1874 __ vmstat();
1875 ret = std::make_pair(ARMFPCondition(cond, condition->IsGtBias()),
1876 ARMFPCondition(opposite, condition->IsGtBias()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001877 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001878 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
Donghui Bai426b49c2016-11-08 14:55:38 +08001879
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001880 const Register left = locations->InAt(0).AsRegister<Register>();
1881
1882 if (right.IsRegister()) {
1883 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001884 } else {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001885 DCHECK(right.IsConstant());
1886 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
Donghui Bai426b49c2016-11-08 14:55:38 +08001887 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001888
1889 ret = std::make_pair(ARMCondition(cond), ARMCondition(opposite));
Donghui Bai426b49c2016-11-08 14:55:38 +08001890 }
1891
1892 return ret;
1893}
1894
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001895static bool CanGenerateTest(HCondition* condition, ArmAssembler* assembler) {
1896 if (condition->GetLeft()->GetType() == Primitive::kPrimLong) {
1897 const LocationSummary* const locations = condition->GetLocations();
Donghui Bai426b49c2016-11-08 14:55:38 +08001898
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001899 if (locations->InAt(1).IsConstant()) {
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001900 IfCondition c = condition->GetCondition();
1901 IfCondition opposite = condition->GetOppositeCondition();
1902 const int64_t value = AdjustConstantForCondition(
1903 Int64FromConstant(locations->InAt(1).GetConstant()),
1904 &c,
1905 &opposite);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001906 ShifterOperand so;
Donghui Bai426b49c2016-11-08 14:55:38 +08001907
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001908 if (c < kCondLT || c > kCondGE) {
1909 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
1910 // we check that the least significant half of the first input to be compared
1911 // is in a low register (the other half is read outside an IT block), and
1912 // the constant fits in an 8-bit unsigned integer, so that a 16-bit CMP
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001913 // encoding can be used; 0 is always handled, no matter what registers are
1914 // used by the first input.
1915 if (value != 0 &&
1916 (!ArmAssembler::IsLowRegister(locations->InAt(0).AsRegisterPairLow<Register>()) ||
1917 !IsUint<8>(Low32Bits(value)))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08001918 return false;
1919 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00001920 } else if (c == kCondLE || c == kCondGT) {
1921 if (value < std::numeric_limits<int64_t>::max() &&
1922 !assembler->ShifterOperandCanHold(kNoRegister,
1923 kNoRegister,
1924 SBC,
1925 High32Bits(value + 1),
1926 kCcSet,
1927 &so)) {
1928 return false;
1929 }
1930 } else if (!assembler->ShifterOperandCanHold(kNoRegister,
1931 kNoRegister,
1932 SBC,
1933 High32Bits(value),
1934 kCcSet,
1935 &so)) {
1936 return false;
Donghui Bai426b49c2016-11-08 14:55:38 +08001937 }
1938 }
1939 }
1940
1941 return true;
1942}
1943
Anton Kirilov5601d4e2017-05-11 19:33:50 +01001944static void GenerateConditionGeneric(HCondition* cond, CodeGeneratorARM* codegen) {
1945 DCHECK(CanGenerateTest(cond, codegen->GetAssembler()));
1946
1947 const Register out = cond->GetLocations()->Out().AsRegister<Register>();
1948 const auto condition = GenerateTest(cond, false, codegen);
1949
1950 __ mov(out, ShifterOperand(0), AL, kCcKeep);
1951
1952 if (ArmAssembler::IsLowRegister(out)) {
1953 __ it(condition.first);
1954 __ mov(out, ShifterOperand(1), condition.first);
1955 } else {
1956 Label done_label;
1957 Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
1958
1959 __ b(final_label, condition.second);
1960 __ LoadImmediate(out, 1);
1961
1962 if (done_label.IsLinked()) {
1963 __ Bind(&done_label);
1964 }
1965 }
1966}
1967
1968static void GenerateEqualLong(HCondition* cond, CodeGeneratorARM* codegen) {
1969 DCHECK_EQ(cond->GetLeft()->GetType(), Primitive::kPrimLong);
1970
1971 const LocationSummary* const locations = cond->GetLocations();
1972 IfCondition condition = cond->GetCondition();
1973 const Register out = locations->Out().AsRegister<Register>();
1974 const Location left = locations->InAt(0);
1975 const Location right = locations->InAt(1);
1976 Register left_high = left.AsRegisterPairHigh<Register>();
1977 Register left_low = left.AsRegisterPairLow<Register>();
1978
1979 if (right.IsConstant()) {
1980 IfCondition opposite = cond->GetOppositeCondition();
1981 const int64_t value = AdjustConstantForCondition(Int64FromConstant(right.GetConstant()),
1982 &condition,
1983 &opposite);
1984 int32_t value_high = -High32Bits(value);
1985 int32_t value_low = -Low32Bits(value);
1986
1987 // The output uses Location::kNoOutputOverlap.
1988 if (out == left_high) {
1989 std::swap(left_low, left_high);
1990 std::swap(value_low, value_high);
1991 }
1992
1993 __ AddConstant(out, left_low, value_low);
1994 __ AddConstant(IP, left_high, value_high);
1995 } else {
1996 DCHECK(right.IsRegisterPair());
1997 __ sub(IP, left_high, ShifterOperand(right.AsRegisterPairHigh<Register>()));
1998 __ sub(out, left_low, ShifterOperand(right.AsRegisterPairLow<Register>()));
1999 }
2000
2001 // Need to check after calling AdjustConstantForCondition().
2002 DCHECK(condition == kCondEQ || condition == kCondNE) << condition;
2003
2004 if (condition == kCondNE && ArmAssembler::IsLowRegister(out)) {
2005 __ orrs(out, out, ShifterOperand(IP));
2006 __ it(NE);
2007 __ mov(out, ShifterOperand(1), NE);
2008 } else {
2009 __ orr(out, out, ShifterOperand(IP));
2010 codegen->GenerateConditionWithZero(condition, out, out, IP);
2011 }
2012}
2013
2014static void GenerateLongComparesAndJumps(HCondition* cond,
2015 Label* true_label,
2016 Label* false_label,
2017 CodeGeneratorARM* codegen) {
2018 LocationSummary* locations = cond->GetLocations();
2019 Location left = locations->InAt(0);
2020 Location right = locations->InAt(1);
2021 IfCondition if_cond = cond->GetCondition();
2022
2023 Register left_high = left.AsRegisterPairHigh<Register>();
2024 Register left_low = left.AsRegisterPairLow<Register>();
2025 IfCondition true_high_cond = if_cond;
2026 IfCondition false_high_cond = cond->GetOppositeCondition();
2027 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part
2028
2029 // Set the conditions for the test, remembering that == needs to be
2030 // decided using the low words.
2031 switch (if_cond) {
2032 case kCondEQ:
2033 case kCondNE:
2034 // Nothing to do.
2035 break;
2036 case kCondLT:
2037 false_high_cond = kCondGT;
2038 break;
2039 case kCondLE:
2040 true_high_cond = kCondLT;
2041 break;
2042 case kCondGT:
2043 false_high_cond = kCondLT;
2044 break;
2045 case kCondGE:
2046 true_high_cond = kCondGT;
2047 break;
2048 case kCondB:
2049 false_high_cond = kCondA;
2050 break;
2051 case kCondBE:
2052 true_high_cond = kCondB;
2053 break;
2054 case kCondA:
2055 false_high_cond = kCondB;
2056 break;
2057 case kCondAE:
2058 true_high_cond = kCondA;
2059 break;
2060 }
2061 if (right.IsConstant()) {
2062 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
2063 int32_t val_low = Low32Bits(value);
2064 int32_t val_high = High32Bits(value);
2065
2066 __ CmpConstant(left_high, val_high);
2067 if (if_cond == kCondNE) {
2068 __ b(true_label, ARMCondition(true_high_cond));
2069 } else if (if_cond == kCondEQ) {
2070 __ b(false_label, ARMCondition(false_high_cond));
2071 } else {
2072 __ b(true_label, ARMCondition(true_high_cond));
2073 __ b(false_label, ARMCondition(false_high_cond));
2074 }
2075 // Must be equal high, so compare the lows.
2076 __ CmpConstant(left_low, val_low);
2077 } else {
2078 Register right_high = right.AsRegisterPairHigh<Register>();
2079 Register right_low = right.AsRegisterPairLow<Register>();
2080
2081 __ cmp(left_high, ShifterOperand(right_high));
2082 if (if_cond == kCondNE) {
2083 __ b(true_label, ARMCondition(true_high_cond));
2084 } else if (if_cond == kCondEQ) {
2085 __ b(false_label, ARMCondition(false_high_cond));
2086 } else {
2087 __ b(true_label, ARMCondition(true_high_cond));
2088 __ b(false_label, ARMCondition(false_high_cond));
2089 }
2090 // Must be equal high, so compare the lows.
2091 __ cmp(left_low, ShifterOperand(right_low));
2092 }
2093 // The last comparison might be unsigned.
2094 // TODO: optimize cases where this is always true/false
2095 __ b(true_label, final_condition);
2096}
2097
2098static void GenerateConditionLong(HCondition* cond, CodeGeneratorARM* codegen) {
2099 DCHECK_EQ(cond->GetLeft()->GetType(), Primitive::kPrimLong);
2100
2101 const LocationSummary* const locations = cond->GetLocations();
2102 IfCondition condition = cond->GetCondition();
2103 const Register out = locations->Out().AsRegister<Register>();
2104 const Location left = locations->InAt(0);
2105 const Location right = locations->InAt(1);
2106
2107 if (right.IsConstant()) {
2108 IfCondition opposite = cond->GetOppositeCondition();
2109
2110 // Comparisons against 0 are common enough to deserve special attention.
2111 if (AdjustConstantForCondition(Int64FromConstant(right.GetConstant()),
2112 &condition,
2113 &opposite) == 0) {
2114 switch (condition) {
2115 case kCondNE:
2116 case kCondA:
2117 if (ArmAssembler::IsLowRegister(out)) {
2118 // We only care if both input registers are 0 or not.
2119 __ orrs(out,
2120 left.AsRegisterPairLow<Register>(),
2121 ShifterOperand(left.AsRegisterPairHigh<Register>()));
2122 __ it(NE);
2123 __ mov(out, ShifterOperand(1), NE);
2124 return;
2125 }
2126
2127 FALLTHROUGH_INTENDED;
2128 case kCondEQ:
2129 case kCondBE:
2130 // We only care if both input registers are 0 or not.
2131 __ orr(out,
2132 left.AsRegisterPairLow<Register>(),
2133 ShifterOperand(left.AsRegisterPairHigh<Register>()));
2134 codegen->GenerateConditionWithZero(condition, out, out);
2135 return;
2136 case kCondLT:
2137 case kCondGE:
2138 // We only care about the sign bit.
2139 FALLTHROUGH_INTENDED;
2140 case kCondAE:
2141 case kCondB:
2142 codegen->GenerateConditionWithZero(condition, out, left.AsRegisterPairHigh<Register>());
2143 return;
2144 case kCondLE:
2145 case kCondGT:
2146 default:
2147 break;
2148 }
2149 }
2150 }
2151
2152 if ((condition == kCondEQ || condition == kCondNE) &&
2153 // If `out` is a low register, then the GenerateConditionGeneric()
2154 // function generates a shorter code sequence that is still branchless.
2155 (!ArmAssembler::IsLowRegister(out) || !CanGenerateTest(cond, codegen->GetAssembler()))) {
2156 GenerateEqualLong(cond, codegen);
2157 return;
2158 }
2159
2160 if (CanGenerateTest(cond, codegen->GetAssembler())) {
2161 GenerateConditionGeneric(cond, codegen);
2162 return;
2163 }
2164
2165 // Convert the jumps into the result.
2166 Label done_label;
2167 Label* const final_label = codegen->GetFinalLabel(cond, &done_label);
2168 Label true_label, false_label;
2169
2170 GenerateLongComparesAndJumps(cond, &true_label, &false_label, codegen);
2171
2172 // False case: result = 0.
2173 __ Bind(&false_label);
2174 __ mov(out, ShifterOperand(0));
2175 __ b(final_label);
2176
2177 // True case: result = 1.
2178 __ Bind(&true_label);
2179 __ mov(out, ShifterOperand(1));
2180
2181 if (done_label.IsLinked()) {
2182 __ Bind(&done_label);
2183 }
2184}
2185
2186static void GenerateConditionIntegralOrNonPrimitive(HCondition* cond, CodeGeneratorARM* codegen) {
2187 const Primitive::Type type = cond->GetLeft()->GetType();
2188
2189 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
2190
2191 if (type == Primitive::kPrimLong) {
2192 GenerateConditionLong(cond, codegen);
2193 return;
2194 }
2195
2196 const LocationSummary* const locations = cond->GetLocations();
2197 IfCondition condition = cond->GetCondition();
2198 Register in = locations->InAt(0).AsRegister<Register>();
2199 const Register out = locations->Out().AsRegister<Register>();
2200 const Location right = cond->GetLocations()->InAt(1);
2201 int64_t value;
2202
2203 if (right.IsConstant()) {
2204 IfCondition opposite = cond->GetOppositeCondition();
2205
2206 value = AdjustConstantForCondition(Int64FromConstant(right.GetConstant()),
2207 &condition,
2208 &opposite);
2209
2210 // Comparisons against 0 are common enough to deserve special attention.
2211 if (value == 0) {
2212 switch (condition) {
2213 case kCondNE:
2214 case kCondA:
2215 if (ArmAssembler::IsLowRegister(out) && out == in) {
2216 __ cmp(out, ShifterOperand(0));
2217 __ it(NE);
2218 __ mov(out, ShifterOperand(1), NE);
2219 return;
2220 }
2221
2222 FALLTHROUGH_INTENDED;
2223 case kCondEQ:
2224 case kCondBE:
2225 case kCondLT:
2226 case kCondGE:
2227 case kCondAE:
2228 case kCondB:
2229 codegen->GenerateConditionWithZero(condition, out, in);
2230 return;
2231 case kCondLE:
2232 case kCondGT:
2233 default:
2234 break;
2235 }
2236 }
2237 }
2238
2239 if (condition == kCondEQ || condition == kCondNE) {
2240 ShifterOperand operand;
2241
2242 if (right.IsConstant()) {
2243 operand = ShifterOperand(value);
2244 } else if (out == right.AsRegister<Register>()) {
2245 // Avoid 32-bit instructions if possible.
2246 operand = ShifterOperand(in);
2247 in = right.AsRegister<Register>();
2248 } else {
2249 operand = ShifterOperand(right.AsRegister<Register>());
2250 }
2251
2252 if (condition == kCondNE && ArmAssembler::IsLowRegister(out)) {
2253 __ subs(out, in, operand);
2254 __ it(NE);
2255 __ mov(out, ShifterOperand(1), NE);
2256 } else {
2257 __ sub(out, in, operand);
2258 codegen->GenerateConditionWithZero(condition, out, out);
2259 }
2260
2261 return;
2262 }
2263
2264 GenerateConditionGeneric(cond, codegen);
2265}
2266
Donghui Bai426b49c2016-11-08 14:55:38 +08002267static bool CanEncodeConstantAs8BitImmediate(HConstant* constant) {
2268 const Primitive::Type type = constant->GetType();
2269 bool ret = false;
2270
2271 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
2272
2273 if (type == Primitive::kPrimLong) {
2274 const uint64_t value = constant->AsLongConstant()->GetValueAsUint64();
2275
2276 ret = IsUint<8>(Low32Bits(value)) && IsUint<8>(High32Bits(value));
2277 } else {
2278 ret = IsUint<8>(CodeGenerator::GetInt32ValueOf(constant));
2279 }
2280
2281 return ret;
2282}
2283
2284static Location Arm8BitEncodableConstantOrRegister(HInstruction* constant) {
2285 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
2286
2287 if (constant->IsConstant() && CanEncodeConstantAs8BitImmediate(constant->AsConstant())) {
2288 return Location::ConstantLocation(constant->AsConstant());
2289 }
2290
2291 return Location::RequiresRegister();
2292}
2293
2294static bool CanGenerateConditionalMove(const Location& out, const Location& src) {
2295 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
2296 // we check that we are not dealing with floating-point output (there is no
2297 // 16-bit VMOV encoding).
2298 if (!out.IsRegister() && !out.IsRegisterPair()) {
2299 return false;
2300 }
2301
2302 // For constants, we also check that the output is in one or two low registers,
2303 // and that the constants fit in an 8-bit unsigned integer, so that a 16-bit
2304 // MOV encoding can be used.
2305 if (src.IsConstant()) {
2306 if (!CanEncodeConstantAs8BitImmediate(src.GetConstant())) {
2307 return false;
2308 }
2309
2310 if (out.IsRegister()) {
2311 if (!ArmAssembler::IsLowRegister(out.AsRegister<Register>())) {
2312 return false;
2313 }
2314 } else {
2315 DCHECK(out.IsRegisterPair());
2316
2317 if (!ArmAssembler::IsLowRegister(out.AsRegisterPairHigh<Register>())) {
2318 return false;
2319 }
2320 }
2321 }
2322
2323 return true;
2324}
2325
Anton Kirilov74234da2017-01-13 14:42:47 +00002326#undef __
2327// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
2328#define __ down_cast<ArmAssembler*>(GetAssembler())-> // NOLINT
2329
Donghui Bai426b49c2016-11-08 14:55:38 +08002330Label* CodeGeneratorARM::GetFinalLabel(HInstruction* instruction, Label* final_label) {
2331 DCHECK(!instruction->IsControlFlow() && !instruction->IsSuspendCheck());
Anton Kirilov6f644202017-02-27 18:29:45 +00002332 DCHECK(!instruction->IsInvoke() || !instruction->GetLocations()->CanCall());
Donghui Bai426b49c2016-11-08 14:55:38 +08002333
2334 const HBasicBlock* const block = instruction->GetBlock();
2335 const HLoopInformation* const info = block->GetLoopInformation();
2336 HInstruction* const next = instruction->GetNext();
2337
2338 // Avoid a branch to a branch.
2339 if (next->IsGoto() && (info == nullptr ||
2340 !info->IsBackEdge(*block) ||
2341 !info->HasSuspendCheck())) {
2342 final_label = GetLabelOf(next->AsGoto()->GetSuccessor());
2343 }
2344
2345 return final_label;
2346}
2347
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002348void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01002349 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002350}
2351
2352void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +01002353 stream << SRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01002354}
2355
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002356size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
2357 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index);
2358 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01002359}
2360
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002361size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
2362 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index);
2363 return kArmWordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01002364}
2365
Nicolas Geoffray840e5462015-01-07 16:01:24 +00002366size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
2367 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index);
2368 return kArmWordSize;
2369}
2370
2371size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
2372 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index);
2373 return kArmWordSize;
2374}
2375
Calin Juravle34166012014-12-19 17:22:29 +00002376CodeGeneratorARM::CodeGeneratorARM(HGraph* graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +00002377 const ArmInstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +01002378 const CompilerOptions& compiler_options,
2379 OptimizingCompilerStats* stats)
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002380 : CodeGenerator(graph,
2381 kNumberOfCoreRegisters,
2382 kNumberOfSRegisters,
2383 kNumberOfRegisterPairs,
2384 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
2385 arraysize(kCoreCalleeSaves)),
Nicolas Geoffray75d5b9b2015-10-05 07:40:35 +00002386 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
2387 arraysize(kFpuCalleeSaves)),
Serban Constantinescuecc43662015-08-13 13:33:12 +01002388 compiler_options,
2389 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01002390 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002391 location_builder_(graph, this),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002392 instruction_visitor_(graph, this),
Nicolas Geoffray8d486732014-07-16 16:23:40 +01002393 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01002394 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00002395 isa_features_(isa_features),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00002396 uint32_literals_(std::less<uint32_t>(),
2397 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01002398 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01002399 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01002400 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00002401 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01002402 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01002403 baker_read_barrier_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00002404 jit_string_patches_(StringReferenceValueComparator(),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00002405 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
2406 jit_class_patches_(TypeReferenceValueComparator(),
2407 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Andreas Gampe501fd632015-09-10 16:11:06 -07002408 // Always save the LR register to mimic Quick.
2409 AddAllocatedRegister(Location::RegisterLocation(LR));
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +01002410}
2411
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002412void CodeGeneratorARM::Finalize(CodeAllocator* allocator) {
2413 // Ensure that we fix up branches and literal loads and emit the literal pool.
2414 __ FinalizeCode();
2415
2416 // Adjust native pc offsets in stack maps.
2417 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08002418 uint32_t old_position =
2419 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kThumb2);
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002420 uint32_t new_position = __ GetAdjustedPosition(old_position);
2421 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
2422 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +01002423 // Adjust pc offsets for the disassembly information.
2424 if (disasm_info_ != nullptr) {
2425 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
2426 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
2427 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
2428 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
2429 it.second.start = __ GetAdjustedPosition(it.second.start);
2430 it.second.end = __ GetAdjustedPosition(it.second.end);
2431 }
2432 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
2433 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
2434 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
2435 }
2436 }
Vladimir Markocf93a5c2015-06-16 11:33:24 +00002437
2438 CodeGenerator::Finalize(allocator);
2439}
2440
David Brazdil58282f42016-01-14 12:45:10 +00002441void CodeGeneratorARM::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002442 // Stack register, LR and PC are always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002443 blocked_core_registers_[SP] = true;
2444 blocked_core_registers_[LR] = true;
2445 blocked_core_registers_[PC] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002446
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002447 // Reserve thread register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002448 blocked_core_registers_[TR] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002449
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002450 // Reserve temp register.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01002451 blocked_core_registers_[IP] = true;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01002452
David Brazdil58282f42016-01-14 12:45:10 +00002453 if (GetGraph()->IsDebuggable()) {
Nicolas Geoffrayecf680d2015-10-05 11:15:37 +01002454 // Stubs do not save callee-save floating point registers. If the graph
2455 // is debuggable, we need to deal with these registers differently. For
2456 // now, just block them.
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002457 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
2458 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
2459 }
2460 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002461}
2462
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002463InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002464 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01002465 assembler_(codegen->GetAssembler()),
2466 codegen_(codegen) {}
2467
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002468void CodeGeneratorARM::ComputeSpillMask() {
2469 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
2470 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
David Brazdil58282f42016-01-14 12:45:10 +00002471 // There is no easy instruction to restore just the PC on thumb2. We spill and
2472 // restore another arbitrary register.
2473 core_spill_mask_ |= (1 << kCoreAlwaysSpillRegister);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002474 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
2475 // We use vpush and vpop for saving and restoring floating point registers, which take
2476 // a SRegister and the number of registers to save/restore after that SRegister. We
2477 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated,
2478 // but in the range.
2479 if (fpu_spill_mask_ != 0) {
2480 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_);
2481 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_);
2482 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) {
2483 fpu_spill_mask_ |= (1 << i);
2484 }
2485 }
2486}
2487
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002488static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002489 return dwarf::Reg::ArmCore(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002490}
2491
2492static dwarf::Reg DWARFReg(SRegister reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01002493 return dwarf::Reg::ArmFp(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002494}
2495
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002496void CodeGeneratorARM::GenerateFrameEntry() {
Roland Levillain199f3362014-11-27 17:15:16 +00002497 bool skip_overflow_check =
2498 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002499 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002500 __ Bind(&frame_entry_label_);
2501
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002502 if (HasEmptyFrame()) {
2503 return;
2504 }
2505
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002506 if (!skip_overflow_check) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00002507 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm)));
2508 __ LoadFromOffset(kLoadWord, IP, IP, 0);
2509 RecordPcInfo(nullptr, 0);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002510 }
2511
Andreas Gampe501fd632015-09-10 16:11:06 -07002512 __ PushList(core_spill_mask_);
2513 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_));
2514 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002515 if (fpu_spill_mask_ != 0) {
2516 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2517 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002518 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_));
David Srbecky9d8606d2015-04-12 09:35:32 +01002519 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002520 }
Mingyao Yang063fc772016-08-02 11:02:54 -07002521
2522 if (GetGraph()->HasShouldDeoptimizeFlag()) {
2523 // Initialize should_deoptimize flag to 0.
2524 __ mov(IP, ShifterOperand(0));
2525 __ StoreToOffset(kStoreWord, IP, SP, -kShouldDeoptimizeFlagSize);
2526 }
2527
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002528 int adjust = GetFrameSize() - FrameEntrySpillSize();
2529 __ AddConstant(SP, -adjust);
2530 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01002531
2532 // Save the current method if we need it. Note that we do not
2533 // do this in HCurrentMethod, as the instruction might have been removed
2534 // in the SSA graph.
2535 if (RequiresCurrentMethod()) {
2536 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0);
2537 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002538}
2539
2540void CodeGeneratorARM::GenerateFrameExit() {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00002541 if (HasEmptyFrame()) {
2542 __ bx(LR);
2543 return;
2544 }
David Srbeckyc34dc932015-04-12 09:27:43 +01002545 __ cfi().RememberState();
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002546 int adjust = GetFrameSize() - FrameEntrySpillSize();
2547 __ AddConstant(SP, adjust);
2548 __ cfi().AdjustCFAOffset(-adjust);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002549 if (fpu_spill_mask_ != 0) {
2550 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_));
2551 __ vpops(start_register, POPCOUNT(fpu_spill_mask_));
Andreas Gampe542451c2016-07-26 09:02:02 -07002552 __ cfi().AdjustCFAOffset(-static_cast<int>(kArmPointerSize) * POPCOUNT(fpu_spill_mask_));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01002553 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_);
Nicolas Geoffray4dee6362015-01-23 18:23:14 +00002554 }
Andreas Gampe501fd632015-09-10 16:11:06 -07002555 // Pop LR into PC to return.
2556 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U);
2557 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC;
2558 __ PopList(pop_mask);
David Srbeckyc34dc932015-04-12 09:27:43 +01002559 __ cfi().RestoreState();
2560 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002561}
2562
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01002563void CodeGeneratorARM::Bind(HBasicBlock* block) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07002564 Label* label = GetLabelOf(block);
2565 __ BindTrackedLabel(label);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002566}
2567
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002568Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002569 switch (type) {
2570 case Primitive::kPrimBoolean:
2571 case Primitive::kPrimByte:
2572 case Primitive::kPrimChar:
2573 case Primitive::kPrimShort:
2574 case Primitive::kPrimInt:
2575 case Primitive::kPrimNot: {
2576 uint32_t index = gp_index_++;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002577 uint32_t stack_index = stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002578 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002579 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002580 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002581 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002582 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002583 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002584
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002585 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002586 uint32_t index = gp_index_;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002587 uint32_t stack_index = stack_index_;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002588 gp_index_ += 2;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002589 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002590 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002591 if (calling_convention.GetRegisterAt(index) == R1) {
2592 // Skip R1, and use R2_R3 instead.
2593 gp_index_++;
2594 index++;
2595 }
2596 }
2597 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
2598 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1,
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002599 calling_convention.GetRegisterAt(index + 1));
Calin Juravle175dc732015-08-25 15:42:32 +01002600
Nicolas Geoffray69c15d32015-01-13 11:42:13 +00002601 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index),
Nicolas Geoffrayaf2c65c2015-01-14 09:40:32 +00002602 calling_convention.GetRegisterAt(index + 1));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002603 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002604 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
2605 }
2606 }
2607
2608 case Primitive::kPrimFloat: {
2609 uint32_t stack_index = stack_index_++;
2610 if (float_index_ % 2 == 0) {
2611 float_index_ = std::max(double_index_, float_index_);
2612 }
2613 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) {
2614 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++));
2615 } else {
2616 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index));
2617 }
2618 }
2619
2620 case Primitive::kPrimDouble: {
2621 double_index_ = std::max(double_index_, RoundUp(float_index_, 2));
2622 uint32_t stack_index = stack_index_;
2623 stack_index_ += 2;
2624 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) {
2625 uint32_t index = double_index_;
2626 double_index_ += 2;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002627 Location result = Location::FpuRegisterPairLocation(
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002628 calling_convention.GetFpuRegisterAt(index),
2629 calling_convention.GetFpuRegisterAt(index + 1));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002630 DCHECK(ExpectedPairLayout(result));
2631 return result;
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002632 } else {
2633 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002634 }
2635 }
2636
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002637 case Primitive::kPrimVoid:
2638 LOG(FATAL) << "Unexpected parameter type " << type;
2639 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002640 }
Roland Levillain3b359c72015-11-17 19:35:12 +00002641 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01002642}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01002643
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002644Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002645 switch (type) {
2646 case Primitive::kPrimBoolean:
2647 case Primitive::kPrimByte:
2648 case Primitive::kPrimChar:
2649 case Primitive::kPrimShort:
2650 case Primitive::kPrimInt:
2651 case Primitive::kPrimNot: {
2652 return Location::RegisterLocation(R0);
2653 }
2654
2655 case Primitive::kPrimFloat: {
2656 return Location::FpuRegisterLocation(S0);
2657 }
2658
2659 case Primitive::kPrimLong: {
2660 return Location::RegisterPairLocation(R0, R1);
2661 }
2662
2663 case Primitive::kPrimDouble: {
2664 return Location::FpuRegisterPairLocation(S0, S1);
2665 }
2666
2667 case Primitive::kPrimVoid:
Roland Levillain3b359c72015-11-17 19:35:12 +00002668 return Location::NoLocation();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002669 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01002670
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002671 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002672}
2673
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002674Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const {
2675 return Location::RegisterLocation(kMethodRegisterArgument);
2676}
2677
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002678void CodeGeneratorARM::Move32(Location destination, Location source) {
2679 if (source.Equals(destination)) {
2680 return;
2681 }
2682 if (destination.IsRegister()) {
2683 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002684 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002685 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002686 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002687 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002688 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002689 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002690 } else if (destination.IsFpuRegister()) {
2691 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002692 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002693 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002694 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002695 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002696 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002697 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002698 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002699 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002700 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002701 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002702 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002703 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002704 } else {
Calin Juravlea21f5982014-11-13 15:53:04 +00002705 DCHECK(source.IsStackSlot()) << source;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002706 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
2707 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002708 }
2709 }
2710}
2711
2712void CodeGeneratorARM::Move64(Location destination, Location source) {
2713 if (source.Equals(destination)) {
2714 return;
2715 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002716 if (destination.IsRegisterPair()) {
2717 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002718 EmitParallelMoves(
2719 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
2720 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002721 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002722 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002723 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
2724 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002725 } else if (source.IsFpuRegister()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002726 UNIMPLEMENTED(FATAL);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002727 } else if (source.IsFpuRegisterPair()) {
2728 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
2729 destination.AsRegisterPairHigh<Register>(),
2730 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002731 } else {
2732 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00002733 DCHECK(ExpectedPairLayout(destination));
2734 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(),
2735 SP, source.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002736 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002737 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002738 if (source.IsDoubleStackSlot()) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002739 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2740 SP,
2741 source.GetStackIndex());
Calin Juravlee460d1d2015-09-29 04:52:17 +01002742 } else if (source.IsRegisterPair()) {
2743 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
2744 source.AsRegisterPairLow<Register>(),
2745 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002746 } else {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002747 UNIMPLEMENTED(FATAL);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002748 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002749 } else {
2750 DCHECK(destination.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002751 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002752 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002753 if (source.AsRegisterPairLow<Register>() == R1) {
2754 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2);
Nicolas Geoffray360231a2014-10-08 21:07:48 +01002755 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex());
2756 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002757 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002758 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002759 SP, destination.GetStackIndex());
2760 }
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00002761 } else if (source.IsFpuRegisterPair()) {
2762 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
2763 SP,
2764 destination.GetStackIndex());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002765 } else {
2766 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002767 EmitParallelMoves(
2768 Location::StackSlot(source.GetStackIndex()),
2769 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002770 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00002771 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01002772 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)),
2773 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002774 }
2775 }
2776}
2777
Calin Juravle175dc732015-08-25 15:42:32 +01002778void CodeGeneratorARM::MoveConstant(Location location, int32_t value) {
2779 DCHECK(location.IsRegister());
2780 __ LoadImmediate(location.AsRegister<Register>(), value);
2781}
2782
Calin Juravlee460d1d2015-09-29 04:52:17 +01002783void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00002784 HParallelMove move(GetGraph()->GetArena());
2785 move.AddMove(src, dst, dst_type, nullptr);
2786 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01002787}
2788
2789void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) {
2790 if (location.IsRegister()) {
2791 locations->AddTemp(location);
2792 } else if (location.IsRegisterPair()) {
2793 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
2794 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
2795 } else {
2796 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
2797 }
2798}
2799
Calin Juravle175dc732015-08-25 15:42:32 +01002800void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint,
2801 HInstruction* instruction,
2802 uint32_t dex_pc,
2803 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01002804 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002805 GenerateInvokeRuntime(GetThreadOffset<kArmPointerSize>(entrypoint).Int32Value());
Serban Constantinescuda8ffec2016-03-09 12:02:11 +00002806 if (EntrypointRequiresStackMap(entrypoint)) {
2807 RecordPcInfo(instruction, dex_pc, slow_path);
2808 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01002809}
2810
Roland Levillaindec8f632016-07-22 17:10:06 +01002811void CodeGeneratorARM::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
2812 HInstruction* instruction,
2813 SlowPathCode* slow_path) {
2814 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01002815 GenerateInvokeRuntime(entry_point_offset);
2816}
2817
2818void CodeGeneratorARM::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +01002819 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset);
2820 __ blx(LR);
2821}
2822
David Brazdilfc6a86a2015-06-26 10:33:45 +00002823void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002824 DCHECK(!successor->IsExitBlock());
2825
2826 HBasicBlock* block = got->GetBlock();
2827 HInstruction* previous = got->GetPrevious();
2828
2829 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00002830 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01002831 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
2832 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
2833 return;
2834 }
2835
2836 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
2837 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
2838 }
2839 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002840 __ b(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002841 }
2842}
2843
David Brazdilfc6a86a2015-06-26 10:33:45 +00002844void LocationsBuilderARM::VisitGoto(HGoto* got) {
2845 got->SetLocations(nullptr);
2846}
2847
2848void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) {
2849 HandleGoto(got, got->GetSuccessor());
2850}
2851
2852void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2853 try_boundary->SetLocations(nullptr);
2854}
2855
2856void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) {
2857 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
2858 if (!successor->IsExitBlock()) {
2859 HandleGoto(try_boundary, successor);
2860 }
2861}
2862
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002863void LocationsBuilderARM::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002864 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002865}
2866
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002867void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002868}
2869
David Brazdil0debae72015-11-12 18:37:00 +00002870void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HCondition* condition,
2871 Label* true_target_in,
2872 Label* false_target_in) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002873 if (CanGenerateTest(condition, codegen_->GetAssembler())) {
2874 Label* non_fallthrough_target;
2875 bool invert;
2876
2877 if (true_target_in == nullptr) {
2878 DCHECK(false_target_in != nullptr);
2879 non_fallthrough_target = false_target_in;
2880 invert = true;
2881 } else {
2882 non_fallthrough_target = true_target_in;
2883 invert = false;
2884 }
2885
2886 const auto cond = GenerateTest(condition, invert, codegen_);
2887
2888 __ b(non_fallthrough_target, cond.first);
2889
2890 if (false_target_in != nullptr && false_target_in != non_fallthrough_target) {
2891 __ b(false_target_in);
2892 }
2893
2894 return;
2895 }
2896
David Brazdil0debae72015-11-12 18:37:00 +00002897 // Generated branching requires both targets to be explicit. If either of the
2898 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
2899 Label fallthrough_target;
2900 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
2901 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
2902
Anton Kirilov217b2ce2017-03-16 11:47:12 +00002903 DCHECK_EQ(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
Anton Kirilov5601d4e2017-05-11 19:33:50 +01002904 GenerateLongComparesAndJumps(condition, true_target, false_target, codegen_);
Roland Levillain4fa13f62015-07-06 18:11:54 +01002905
David Brazdil0debae72015-11-12 18:37:00 +00002906 if (false_target != &fallthrough_target) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01002907 __ b(false_target);
2908 }
David Brazdil0debae72015-11-12 18:37:00 +00002909
2910 if (fallthrough_target.IsLinked()) {
2911 __ Bind(&fallthrough_target);
2912 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01002913}
2914
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002915void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00002916 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07002917 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00002918 Label* false_target) {
2919 HInstruction* cond = instruction->InputAt(condition_input_index);
2920
2921 if (true_target == nullptr && false_target == nullptr) {
2922 // Nothing to do. The code always falls through.
2923 return;
2924 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00002925 // Constant condition, statically compared against "true" (integer value 1).
2926 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00002927 if (true_target != nullptr) {
2928 __ b(true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002929 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002930 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00002931 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00002932 if (false_target != nullptr) {
2933 __ b(false_target);
2934 }
2935 }
2936 return;
2937 }
2938
2939 // The following code generates these patterns:
2940 // (1) true_target == nullptr && false_target != nullptr
2941 // - opposite condition true => branch to false_target
2942 // (2) true_target != nullptr && false_target == nullptr
2943 // - condition true => branch to true_target
2944 // (3) true_target != nullptr && false_target != nullptr
2945 // - condition true => branch to true_target
2946 // - branch to false_target
2947 if (IsBooleanValueOrMaterializedCondition(cond)) {
2948 // Condition has been materialized, compare the output to 0.
2949 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
2950 DCHECK(cond_val.IsRegister());
2951 if (true_target == nullptr) {
2952 __ CompareAndBranchIfZero(cond_val.AsRegister<Register>(), false_target);
2953 } else {
2954 __ CompareAndBranchIfNonZero(cond_val.AsRegister<Register>(), true_target);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002955 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01002956 } else {
David Brazdil0debae72015-11-12 18:37:00 +00002957 // Condition has not been materialized. Use its inputs as the comparison and
2958 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04002959 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00002960
2961 // If this is a long or FP comparison that has been folded into
2962 // the HCondition, generate the comparison directly.
2963 Primitive::Type type = condition->InputAt(0)->GetType();
2964 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
2965 GenerateCompareTestAndBranch(condition, true_target, false_target);
2966 return;
2967 }
2968
Donghui Bai426b49c2016-11-08 14:55:38 +08002969 Label* non_fallthrough_target;
2970 Condition arm_cond;
David Brazdil0debae72015-11-12 18:37:00 +00002971 LocationSummary* locations = cond->GetLocations();
2972 DCHECK(locations->InAt(0).IsRegister());
2973 Register left = locations->InAt(0).AsRegister<Register>();
2974 Location right = locations->InAt(1);
Donghui Bai426b49c2016-11-08 14:55:38 +08002975
David Brazdil0debae72015-11-12 18:37:00 +00002976 if (true_target == nullptr) {
Donghui Bai426b49c2016-11-08 14:55:38 +08002977 arm_cond = ARMCondition(condition->GetOppositeCondition());
2978 non_fallthrough_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00002979 } else {
Donghui Bai426b49c2016-11-08 14:55:38 +08002980 arm_cond = ARMCondition(condition->GetCondition());
2981 non_fallthrough_target = true_target;
2982 }
2983
2984 if (right.IsConstant() && (arm_cond == NE || arm_cond == EQ) &&
2985 CodeGenerator::GetInt32ValueOf(right.GetConstant()) == 0) {
2986 if (arm_cond == EQ) {
2987 __ CompareAndBranchIfZero(left, non_fallthrough_target);
2988 } else {
2989 DCHECK_EQ(arm_cond, NE);
2990 __ CompareAndBranchIfNonZero(left, non_fallthrough_target);
2991 }
2992 } else {
2993 if (right.IsRegister()) {
2994 __ cmp(left, ShifterOperand(right.AsRegister<Register>()));
2995 } else {
2996 DCHECK(right.IsConstant());
2997 __ CmpConstant(left, CodeGenerator::GetInt32ValueOf(right.GetConstant()));
2998 }
2999
3000 __ b(non_fallthrough_target, arm_cond);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01003001 }
Dave Allison20dfc792014-06-16 20:44:29 -07003002 }
David Brazdil0debae72015-11-12 18:37:00 +00003003
3004 // If neither branch falls through (case 3), the conditional branch to `true_target`
3005 // was already emitted (case 2) and we need to emit a jump to `false_target`.
3006 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003007 __ b(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003008 }
3009}
3010
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003011void LocationsBuilderARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003012 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
3013 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003014 locations->SetInAt(0, Location::RequiresRegister());
3015 }
3016}
3017
3018void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00003019 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
3020 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
3021 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
3022 nullptr : codegen_->GetLabelOf(true_successor);
3023 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
3024 nullptr : codegen_->GetLabelOf(false_successor);
3025 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003026}
3027
3028void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) {
3029 LocationSummary* locations = new (GetGraph()->GetArena())
3030 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01003031 InvokeRuntimeCallingConvention calling_convention;
3032 RegisterSet caller_saves = RegisterSet::Empty();
3033 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3034 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00003035 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003036 locations->SetInAt(0, Location::RequiresRegister());
3037 }
3038}
3039
3040void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01003041 SlowPathCodeARM* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathARM>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00003042 GenerateTestAndBranch(deoptimize,
3043 /* condition_input_index */ 0,
3044 slow_path->GetEntryLabel(),
3045 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07003046}
Dave Allison20dfc792014-06-16 20:44:29 -07003047
Mingyao Yang063fc772016-08-02 11:02:54 -07003048void LocationsBuilderARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3049 LocationSummary* locations = new (GetGraph()->GetArena())
3050 LocationSummary(flag, LocationSummary::kNoCall);
3051 locations->SetOut(Location::RequiresRegister());
3052}
3053
3054void InstructionCodeGeneratorARM::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
3055 __ LoadFromOffset(kLoadWord,
3056 flag->GetLocations()->Out().AsRegister<Register>(),
3057 SP,
3058 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
3059}
3060
David Brazdil74eb1b22015-12-14 11:44:01 +00003061void LocationsBuilderARM::VisitSelect(HSelect* select) {
3062 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Donghui Bai426b49c2016-11-08 14:55:38 +08003063 const bool is_floating_point = Primitive::IsFloatingPointType(select->GetType());
3064
3065 if (is_floating_point) {
David Brazdil74eb1b22015-12-14 11:44:01 +00003066 locations->SetInAt(0, Location::RequiresFpuRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003067 locations->SetInAt(1, Location::FpuRegisterOrConstant(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00003068 } else {
3069 locations->SetInAt(0, Location::RequiresRegister());
Donghui Bai426b49c2016-11-08 14:55:38 +08003070 locations->SetInAt(1, Arm8BitEncodableConstantOrRegister(select->GetTrueValue()));
David Brazdil74eb1b22015-12-14 11:44:01 +00003071 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003072
David Brazdil74eb1b22015-12-14 11:44:01 +00003073 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003074 locations->SetInAt(2, Location::RegisterOrConstant(select->GetCondition()));
3075 // The code generator handles overlap with the values, but not with the condition.
3076 locations->SetOut(Location::SameAsFirstInput());
3077 } else if (is_floating_point) {
3078 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3079 } else {
3080 if (!locations->InAt(1).IsConstant()) {
3081 locations->SetInAt(0, Arm8BitEncodableConstantOrRegister(select->GetFalseValue()));
3082 }
3083
3084 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
David Brazdil74eb1b22015-12-14 11:44:01 +00003085 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003086}
3087
3088void InstructionCodeGeneratorARM::VisitSelect(HSelect* select) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003089 HInstruction* const condition = select->GetCondition();
3090 const LocationSummary* const locations = select->GetLocations();
3091 const Primitive::Type type = select->GetType();
3092 const Location first = locations->InAt(0);
3093 const Location out = locations->Out();
3094 const Location second = locations->InAt(1);
3095 Location src;
3096
3097 if (condition->IsIntConstant()) {
3098 if (condition->AsIntConstant()->IsFalse()) {
3099 src = first;
3100 } else {
3101 src = second;
3102 }
3103
3104 codegen_->MoveLocation(out, src, type);
3105 return;
3106 }
3107
3108 if (!Primitive::IsFloatingPointType(type) &&
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003109 (IsBooleanValueOrMaterializedCondition(condition) ||
3110 CanGenerateTest(condition->AsCondition(), codegen_->GetAssembler()))) {
Donghui Bai426b49c2016-11-08 14:55:38 +08003111 bool invert = false;
3112
3113 if (out.Equals(second)) {
3114 src = first;
3115 invert = true;
3116 } else if (out.Equals(first)) {
3117 src = second;
3118 } else if (second.IsConstant()) {
3119 DCHECK(CanEncodeConstantAs8BitImmediate(second.GetConstant()));
3120 src = second;
3121 } else if (first.IsConstant()) {
3122 DCHECK(CanEncodeConstantAs8BitImmediate(first.GetConstant()));
3123 src = first;
3124 invert = true;
3125 } else {
3126 src = second;
3127 }
3128
3129 if (CanGenerateConditionalMove(out, src)) {
3130 if (!out.Equals(first) && !out.Equals(second)) {
3131 codegen_->MoveLocation(out, src.Equals(first) ? second : first, type);
3132 }
3133
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003134 std::pair<Condition, Condition> cond;
3135
3136 if (IsBooleanValueOrMaterializedCondition(condition)) {
3137 __ CmpConstant(locations->InAt(2).AsRegister<Register>(), 0);
3138 cond = invert ? std::make_pair(EQ, NE) : std::make_pair(NE, EQ);
3139 } else {
3140 cond = GenerateTest(condition->AsCondition(), invert, codegen_);
3141 }
Donghui Bai426b49c2016-11-08 14:55:38 +08003142
3143 if (out.IsRegister()) {
3144 ShifterOperand operand;
3145
3146 if (src.IsConstant()) {
3147 operand = ShifterOperand(CodeGenerator::GetInt32ValueOf(src.GetConstant()));
3148 } else {
3149 DCHECK(src.IsRegister());
3150 operand = ShifterOperand(src.AsRegister<Register>());
3151 }
3152
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003153 __ it(cond.first);
3154 __ mov(out.AsRegister<Register>(), operand, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08003155 } else {
3156 DCHECK(out.IsRegisterPair());
3157
3158 ShifterOperand operand_high;
3159 ShifterOperand operand_low;
3160
3161 if (src.IsConstant()) {
3162 const int64_t value = src.GetConstant()->AsLongConstant()->GetValue();
3163
3164 operand_high = ShifterOperand(High32Bits(value));
3165 operand_low = ShifterOperand(Low32Bits(value));
3166 } else {
3167 DCHECK(src.IsRegisterPair());
3168 operand_high = ShifterOperand(src.AsRegisterPairHigh<Register>());
3169 operand_low = ShifterOperand(src.AsRegisterPairLow<Register>());
3170 }
3171
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003172 __ it(cond.first);
3173 __ mov(out.AsRegisterPairLow<Register>(), operand_low, cond.first);
3174 __ it(cond.first);
3175 __ mov(out.AsRegisterPairHigh<Register>(), operand_high, cond.first);
Donghui Bai426b49c2016-11-08 14:55:38 +08003176 }
3177
3178 return;
3179 }
3180 }
3181
3182 Label* false_target = nullptr;
3183 Label* true_target = nullptr;
3184 Label select_end;
3185 Label* target = codegen_->GetFinalLabel(select, &select_end);
3186
3187 if (out.Equals(second)) {
3188 true_target = target;
3189 src = first;
3190 } else {
3191 false_target = target;
3192 src = second;
3193
3194 if (!out.Equals(first)) {
3195 codegen_->MoveLocation(out, first, type);
3196 }
3197 }
3198
3199 GenerateTestAndBranch(select, 2, true_target, false_target);
3200 codegen_->MoveLocation(out, src, type);
3201
3202 if (select_end.IsLinked()) {
3203 __ Bind(&select_end);
3204 }
David Brazdil74eb1b22015-12-14 11:44:01 +00003205}
3206
David Srbecky0cf44932015-12-09 14:09:59 +00003207void LocationsBuilderARM::VisitNativeDebugInfo(HNativeDebugInfo* info) {
3208 new (GetGraph()->GetArena()) LocationSummary(info);
3209}
3210
David Srbeckyd28f4a02016-03-14 17:14:24 +00003211void InstructionCodeGeneratorARM::VisitNativeDebugInfo(HNativeDebugInfo*) {
3212 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00003213}
3214
3215void CodeGeneratorARM::GenerateNop() {
3216 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00003217}
3218
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003219// `temp` is an extra temporary register that is used for some conditions;
3220// callers may not specify it, in which case the method will use a scratch
3221// register instead.
3222void CodeGeneratorARM::GenerateConditionWithZero(IfCondition condition,
3223 Register out,
3224 Register in,
3225 Register temp) {
3226 switch (condition) {
3227 case kCondEQ:
3228 // x <= 0 iff x == 0 when the comparison is unsigned.
3229 case kCondBE:
3230 if (temp == kNoRegister || (ArmAssembler::IsLowRegister(out) && out != in)) {
3231 temp = out;
3232 }
3233
3234 // Avoid 32-bit instructions if possible; note that `in` and `temp` must be
3235 // different as well.
3236 if (ArmAssembler::IsLowRegister(in) && ArmAssembler::IsLowRegister(temp) && in != temp) {
3237 // temp = - in; only 0 sets the carry flag.
3238 __ rsbs(temp, in, ShifterOperand(0));
3239
3240 if (out == in) {
3241 std::swap(in, temp);
3242 }
3243
3244 // out = - in + in + carry = carry
3245 __ adc(out, temp, ShifterOperand(in));
3246 } else {
3247 // If `in` is 0, then it has 32 leading zeros, and less than that otherwise.
3248 __ clz(out, in);
3249 // Any number less than 32 logically shifted right by 5 bits results in 0;
3250 // the same operation on 32 yields 1.
3251 __ Lsr(out, out, 5);
3252 }
3253
3254 break;
3255 case kCondNE:
3256 // x > 0 iff x != 0 when the comparison is unsigned.
3257 case kCondA:
3258 if (out == in) {
3259 if (temp == kNoRegister || in == temp) {
3260 temp = IP;
3261 }
3262 } else if (temp == kNoRegister || !ArmAssembler::IsLowRegister(temp)) {
3263 temp = out;
3264 }
3265
3266 // temp = in - 1; only 0 does not set the carry flag.
3267 __ subs(temp, in, ShifterOperand(1));
3268 // out = in + ~temp + carry = in + (-(in - 1) - 1) + carry = in - in + 1 - 1 + carry = carry
3269 __ sbc(out, in, ShifterOperand(temp));
3270 break;
3271 case kCondGE:
3272 __ mvn(out, ShifterOperand(in));
3273 in = out;
3274 FALLTHROUGH_INTENDED;
3275 case kCondLT:
3276 // We only care about the sign bit.
3277 __ Lsr(out, in, 31);
3278 break;
3279 case kCondAE:
3280 // Trivially true.
3281 __ mov(out, ShifterOperand(1));
3282 break;
3283 case kCondB:
3284 // Trivially false.
3285 __ mov(out, ShifterOperand(0));
3286 break;
3287 default:
3288 LOG(FATAL) << "Unexpected condition " << condition;
3289 UNREACHABLE();
3290 }
3291}
3292
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003293void LocationsBuilderARM::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003294 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01003295 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003296 // Handle the long/FP comparisons made in instruction simplification.
3297 switch (cond->InputAt(0)->GetType()) {
3298 case Primitive::kPrimLong:
3299 locations->SetInAt(0, Location::RequiresRegister());
3300 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00003301 if (!cond->IsEmittedAtUseSite()) {
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003302 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Roland Levillain4fa13f62015-07-06 18:11:54 +01003303 }
3304 break;
3305
3306 case Primitive::kPrimFloat:
3307 case Primitive::kPrimDouble:
3308 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01003309 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00003310 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01003311 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3312 }
3313 break;
3314
3315 default:
3316 locations->SetInAt(0, Location::RequiresRegister());
3317 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00003318 if (!cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01003319 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3320 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003321 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003322}
3323
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003324void InstructionCodeGeneratorARM::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003325 if (cond->IsEmittedAtUseSite()) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01003326 return;
Dave Allison20dfc792014-06-16 20:44:29 -07003327 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01003328
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003329 const Primitive::Type type = cond->GetLeft()->GetType();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003330
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003331 if (Primitive::IsFloatingPointType(type)) {
3332 GenerateConditionGeneric(cond, codegen_);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003333 return;
Roland Levillain4fa13f62015-07-06 18:11:54 +01003334 }
3335
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003336 DCHECK(Primitive::IsIntegralType(type) || type == Primitive::kPrimNot) << type;
Roland Levillain4fa13f62015-07-06 18:11:54 +01003337
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003338 const IfCondition condition = cond->GetCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +01003339
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003340 // A condition with only one boolean input, or two boolean inputs without being equality or
3341 // inequality results from transformations done by the instruction simplifier, and is handled
3342 // as a regular condition with integral inputs.
3343 if (type == Primitive::kPrimBoolean &&
3344 cond->GetRight()->GetType() == Primitive::kPrimBoolean &&
3345 (condition == kCondEQ || condition == kCondNE)) {
3346 const LocationSummary* const locations = cond->GetLocations();
3347 Register left = locations->InAt(0).AsRegister<Register>();
3348 const Register out = locations->Out().AsRegister<Register>();
3349 const Location right_loc = locations->InAt(1);
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003350
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003351 // The constant case is handled by the instruction simplifier.
3352 DCHECK(!right_loc.IsConstant());
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003353
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003354 Register right = right_loc.AsRegister<Register>();
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003355
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003356 // Avoid 32-bit instructions if possible.
3357 if (out == right) {
3358 std::swap(left, right);
3359 }
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003360
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003361 __ eor(out, left, ShifterOperand(right));
3362
3363 if (condition == kCondEQ) {
3364 __ eor(out, out, ShifterOperand(1));
3365 }
3366
3367 return;
Anton Kirilov217b2ce2017-03-16 11:47:12 +00003368 }
Anton Kirilov6f644202017-02-27 18:29:45 +00003369
Anton Kirilov5601d4e2017-05-11 19:33:50 +01003370 GenerateConditionIntegralOrNonPrimitive(cond, codegen_);
Dave Allison20dfc792014-06-16 20:44:29 -07003371}
3372
3373void LocationsBuilderARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003374 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003375}
3376
3377void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003378 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003379}
3380
3381void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003382 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003383}
3384
3385void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003386 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003387}
3388
3389void LocationsBuilderARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003390 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003391}
3392
3393void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003394 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003395}
3396
3397void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003398 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003399}
3400
3401void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003402 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003403}
3404
3405void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003406 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003407}
3408
3409void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003410 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003411}
3412
3413void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003414 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07003415}
3416
3417void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003418 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003419}
3420
Aart Bike9f37602015-10-09 11:15:55 -07003421void LocationsBuilderARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003422 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003423}
3424
3425void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003426 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003427}
3428
3429void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003430 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003431}
3432
3433void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003434 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003435}
3436
3437void LocationsBuilderARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003438 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003439}
3440
3441void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003442 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003443}
3444
3445void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003446 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003447}
3448
3449void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003450 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07003451}
3452
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003453void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003454 LocationSummary* locations =
3455 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003456 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003457}
3458
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003459void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01003460 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003461}
3462
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003463void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) {
3464 LocationSummary* locations =
3465 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3466 locations->SetOut(Location::ConstantLocation(constant));
3467}
3468
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003469void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003470 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00003471}
3472
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003473void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003474 LocationSummary* locations =
3475 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01003476 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003477}
3478
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003479void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003480 // Will be generated at use site.
3481}
3482
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003483void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) {
3484 LocationSummary* locations =
3485 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3486 locations->SetOut(Location::ConstantLocation(constant));
3487}
3488
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003489void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003490 // Will be generated at use site.
3491}
3492
3493void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) {
3494 LocationSummary* locations =
3495 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3496 locations->SetOut(Location::ConstantLocation(constant));
3497}
3498
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003499void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01003500 // Will be generated at use site.
3501}
3502
Igor Murashkind01745e2017-04-05 16:40:31 -07003503void LocationsBuilderARM::VisitConstructorFence(HConstructorFence* constructor_fence) {
3504 constructor_fence->SetLocations(nullptr);
3505}
3506
3507void InstructionCodeGeneratorARM::VisitConstructorFence(
3508 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
3509 codegen_->GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
3510}
3511
Calin Juravle27df7582015-04-17 19:12:31 +01003512void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
3513 memory_barrier->SetLocations(nullptr);
3514}
3515
3516void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00003517 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01003518}
3519
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003520void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003521 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00003522}
3523
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003524void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003525 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00003526}
3527
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003528void LocationsBuilderARM::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003529 LocationSummary* locations =
3530 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00003531 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType()));
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003532}
3533
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003534void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00003535 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00003536}
3537
Calin Juravle175dc732015-08-25 15:42:32 +01003538void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3539 // The trampoline uses the same calling convention as dex calling conventions,
3540 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
3541 // the method_idx.
3542 HandleInvoke(invoke);
3543}
3544
3545void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
3546 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
3547}
3548
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003549void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003550 // Explicit clinit checks triggered by static invokes must have been pruned by
3551 // art::PrepareForRegisterAllocation.
3552 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003553
Vladimir Marko68c981f2016-08-26 13:13:33 +01003554 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003555 if (intrinsic.TryDispatch(invoke)) {
3556 return;
3557 }
3558
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003559 HandleInvoke(invoke);
3560}
3561
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003562static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) {
3563 if (invoke->GetLocations()->Intrinsified()) {
3564 IntrinsicCodeGeneratorARM intrinsic(codegen);
3565 intrinsic.Dispatch(invoke);
3566 return true;
3567 }
3568 return false;
3569}
3570
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00003571void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00003572 // Explicit clinit checks triggered by static invokes must have been pruned by
3573 // art::PrepareForRegisterAllocation.
3574 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01003575
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003576 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3577 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00003578 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003579
Nicolas Geoffray38207af2015-06-01 15:46:22 +01003580 LocationSummary* locations = invoke->GetLocations();
3581 codegen_->GenerateStaticOrDirectCall(
3582 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003583}
3584
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003585void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01003586 InvokeDexCallingConventionVisitorARM calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01003587 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003588}
3589
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003590void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Vladimir Marko68c981f2016-08-26 13:13:33 +01003591 IntrinsicLocationsBuilderARM intrinsic(codegen_);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003592 if (intrinsic.TryDispatch(invoke)) {
3593 return;
3594 }
3595
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003596 HandleInvoke(invoke);
3597}
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003598
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01003599void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08003600 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
3601 return;
3602 }
3603
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003604 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003605 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00003606}
3607
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003608void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3609 HandleInvoke(invoke);
3610 // Add the hidden argument.
3611 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12));
3612}
3613
3614void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) {
3615 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain3b359c72015-11-17 19:35:12 +00003616 LocationSummary* locations = invoke->GetLocations();
3617 Register temp = locations->GetTemp(0).AsRegister<Register>();
3618 Register hidden_reg = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003619 Location receiver = locations->InAt(0);
3620 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3621
Roland Levillain3b359c72015-11-17 19:35:12 +00003622 // Set the hidden argument. This is safe to do this here, as R12
3623 // won't be modified thereafter, before the `blx` (call) instruction.
3624 DCHECK_EQ(R12, hidden_reg);
3625 __ LoadImmediate(hidden_reg, invoke->GetDexMethodIndex());
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003626
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003627 if (receiver.IsStackSlot()) {
3628 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
Roland Levillain3b359c72015-11-17 19:35:12 +00003629 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003630 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
3631 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00003632 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00003633 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003634 }
Calin Juravle77520bc2015-01-12 18:45:46 +00003635 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00003636 // Instead of simply (possibly) unpoisoning `temp` here, we should
3637 // emit a read barrier for the previous class reference load.
3638 // However this is not required in practice, as this is an
3639 // intermediate/temporary reference and because the current
3640 // concurrent copying collector keeps the from-space memory
3641 // intact/accessible until the end of the marking phase (the
3642 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01003643 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003644 __ LoadFromOffset(kLoadWord, temp, temp,
3645 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
3646 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00003647 invoke->GetImtIndex(), kArmPointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003648 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00003649 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00003650 uint32_t entry_point =
Andreas Gampe542451c2016-07-26 09:02:02 -07003651 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00003652 // LR = temp->GetEntryPoint();
3653 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
3654 // LR();
3655 __ blx(LR);
3656 DCHECK(!codegen_->IsLeafMethod());
3657 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
3658}
3659
Orion Hodsonac141392017-01-13 11:53:47 +00003660void LocationsBuilderARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3661 HandleInvoke(invoke);
3662}
3663
3664void InstructionCodeGeneratorARM::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
3665 codegen_->GenerateInvokePolymorphicCall(invoke);
3666}
3667
Roland Levillain88cb1752014-10-20 16:36:47 +01003668void LocationsBuilderARM::VisitNeg(HNeg* neg) {
3669 LocationSummary* locations =
3670 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
3671 switch (neg->GetResultType()) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003672 case Primitive::kPrimInt: {
Roland Levillain88cb1752014-10-20 16:36:47 +01003673 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00003674 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3675 break;
3676 }
3677 case Primitive::kPrimLong: {
3678 locations->SetInAt(0, Location::RequiresRegister());
3679 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003680 break;
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003681 }
Roland Levillain88cb1752014-10-20 16:36:47 +01003682
Roland Levillain88cb1752014-10-20 16:36:47 +01003683 case Primitive::kPrimFloat:
3684 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003685 locations->SetInAt(0, Location::RequiresFpuRegister());
3686 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillain88cb1752014-10-20 16:36:47 +01003687 break;
3688
3689 default:
3690 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3691 }
3692}
3693
3694void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) {
3695 LocationSummary* locations = neg->GetLocations();
3696 Location out = locations->Out();
3697 Location in = locations->InAt(0);
3698 switch (neg->GetResultType()) {
3699 case Primitive::kPrimInt:
3700 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003701 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0));
Roland Levillain88cb1752014-10-20 16:36:47 +01003702 break;
3703
3704 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01003705 DCHECK(in.IsRegisterPair());
3706 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag)
3707 __ rsbs(out.AsRegisterPairLow<Register>(),
3708 in.AsRegisterPairLow<Register>(),
3709 ShifterOperand(0));
3710 // We cannot emit an RSC (Reverse Subtract with Carry)
3711 // instruction here, as it does not exist in the Thumb-2
3712 // instruction set. We use the following approach
3713 // using SBC and SUB instead.
3714 //
3715 // out.hi = -C
3716 __ sbc(out.AsRegisterPairHigh<Register>(),
3717 out.AsRegisterPairHigh<Register>(),
3718 ShifterOperand(out.AsRegisterPairHigh<Register>()));
3719 // out.hi = out.hi - in.hi
3720 __ sub(out.AsRegisterPairHigh<Register>(),
3721 out.AsRegisterPairHigh<Register>(),
3722 ShifterOperand(in.AsRegisterPairHigh<Register>()));
3723 break;
3724
Roland Levillain88cb1752014-10-20 16:36:47 +01003725 case Primitive::kPrimFloat:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003726 DCHECK(in.IsFpuRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00003727 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>());
Roland Levillain3dbcb382014-10-28 17:30:07 +00003728 break;
3729
Roland Levillain88cb1752014-10-20 16:36:47 +01003730 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00003731 DCHECK(in.IsFpuRegisterPair());
3732 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
3733 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain88cb1752014-10-20 16:36:47 +01003734 break;
3735
3736 default:
3737 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
3738 }
3739}
3740
Roland Levillaindff1f282014-11-05 14:15:05 +00003741void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00003742 Primitive::Type result_type = conversion->GetResultType();
3743 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003744 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00003745
Roland Levillain5b3ee562015-04-14 16:02:41 +01003746 // The float-to-long, double-to-long and long-to-float type conversions
3747 // rely on a call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00003748 LocationSummary::CallKind call_kind =
Roland Levillain5b3ee562015-04-14 16:02:41 +01003749 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
3750 && result_type == Primitive::kPrimLong)
3751 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat))
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003752 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00003753 : LocationSummary::kNoCall;
3754 LocationSummary* locations =
3755 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
3756
David Brazdilb2bd1c52015-03-25 11:17:37 +00003757 // The Java language does not allow treating boolean as an integral type but
3758 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00003759
Roland Levillaindff1f282014-11-05 14:15:05 +00003760 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003761 case Primitive::kPrimByte:
3762 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003763 case Primitive::kPrimLong:
3764 // Type conversion from long to byte is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003765 case Primitive::kPrimBoolean:
3766 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003767 case Primitive::kPrimShort:
3768 case Primitive::kPrimInt:
3769 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003770 // Processing a Dex `int-to-byte' instruction.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003771 locations->SetInAt(0, Location::RequiresRegister());
3772 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3773 break;
3774
3775 default:
3776 LOG(FATAL) << "Unexpected type conversion from " << input_type
3777 << " to " << result_type;
3778 }
3779 break;
3780
Roland Levillain01a8d712014-11-14 16:27:39 +00003781 case Primitive::kPrimShort:
3782 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003783 case Primitive::kPrimLong:
3784 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003785 case Primitive::kPrimBoolean:
3786 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003787 case Primitive::kPrimByte:
3788 case Primitive::kPrimInt:
3789 case Primitive::kPrimChar:
3790 // Processing a Dex `int-to-short' instruction.
3791 locations->SetInAt(0, Location::RequiresRegister());
3792 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3793 break;
3794
3795 default:
3796 LOG(FATAL) << "Unexpected type conversion from " << input_type
3797 << " to " << result_type;
3798 }
3799 break;
3800
Roland Levillain946e1432014-11-11 17:35:19 +00003801 case Primitive::kPrimInt:
3802 switch (input_type) {
3803 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00003804 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00003805 locations->SetInAt(0, Location::Any());
3806 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3807 break;
3808
3809 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00003810 // Processing a Dex `float-to-int' instruction.
3811 locations->SetInAt(0, Location::RequiresFpuRegister());
3812 locations->SetOut(Location::RequiresRegister());
3813 locations->AddTemp(Location::RequiresFpuRegister());
3814 break;
3815
Roland Levillain946e1432014-11-11 17:35:19 +00003816 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003817 // Processing a Dex `double-to-int' instruction.
3818 locations->SetInAt(0, Location::RequiresFpuRegister());
3819 locations->SetOut(Location::RequiresRegister());
3820 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00003821 break;
3822
3823 default:
3824 LOG(FATAL) << "Unexpected type conversion from " << input_type
3825 << " to " << result_type;
3826 }
3827 break;
3828
Roland Levillaindff1f282014-11-05 14:15:05 +00003829 case Primitive::kPrimLong:
3830 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003831 case Primitive::kPrimBoolean:
3832 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00003833 case Primitive::kPrimByte:
3834 case Primitive::kPrimShort:
3835 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00003836 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003837 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00003838 locations->SetInAt(0, Location::RequiresRegister());
3839 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3840 break;
3841
Roland Levillain624279f2014-12-04 11:54:28 +00003842 case Primitive::kPrimFloat: {
3843 // Processing a Dex `float-to-long' instruction.
3844 InvokeRuntimeCallingConvention calling_convention;
3845 locations->SetInAt(0, Location::FpuRegisterLocation(
3846 calling_convention.GetFpuRegisterAt(0)));
3847 locations->SetOut(Location::RegisterPairLocation(R0, R1));
3848 break;
3849 }
3850
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003851 case Primitive::kPrimDouble: {
3852 // Processing a Dex `double-to-long' instruction.
3853 InvokeRuntimeCallingConvention calling_convention;
3854 locations->SetInAt(0, Location::FpuRegisterPairLocation(
3855 calling_convention.GetFpuRegisterAt(0),
3856 calling_convention.GetFpuRegisterAt(1)));
3857 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Roland Levillaindff1f282014-11-05 14:15:05 +00003858 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00003859 }
Roland Levillaindff1f282014-11-05 14:15:05 +00003860
3861 default:
3862 LOG(FATAL) << "Unexpected type conversion from " << input_type
3863 << " to " << result_type;
3864 }
3865 break;
3866
Roland Levillain981e4542014-11-14 11:47:14 +00003867 case Primitive::kPrimChar:
3868 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003869 case Primitive::kPrimLong:
3870 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00003871 case Primitive::kPrimBoolean:
3872 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00003873 case Primitive::kPrimByte:
3874 case Primitive::kPrimShort:
3875 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00003876 // Processing a Dex `int-to-char' instruction.
3877 locations->SetInAt(0, Location::RequiresRegister());
3878 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3879 break;
3880
3881 default:
3882 LOG(FATAL) << "Unexpected type conversion from " << input_type
3883 << " to " << result_type;
3884 }
3885 break;
3886
Roland Levillaindff1f282014-11-05 14:15:05 +00003887 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00003888 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003889 case Primitive::kPrimBoolean:
3890 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003891 case Primitive::kPrimByte:
3892 case Primitive::kPrimShort:
3893 case Primitive::kPrimInt:
3894 case Primitive::kPrimChar:
3895 // Processing a Dex `int-to-float' instruction.
3896 locations->SetInAt(0, Location::RequiresRegister());
3897 locations->SetOut(Location::RequiresFpuRegister());
3898 break;
3899
Roland Levillain5b3ee562015-04-14 16:02:41 +01003900 case Primitive::kPrimLong: {
Roland Levillain6d0e4832014-11-27 18:31:21 +00003901 // Processing a Dex `long-to-float' instruction.
Roland Levillain5b3ee562015-04-14 16:02:41 +01003902 InvokeRuntimeCallingConvention calling_convention;
3903 locations->SetInAt(0, Location::RegisterPairLocation(
3904 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3905 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
Roland Levillain6d0e4832014-11-27 18:31:21 +00003906 break;
Roland Levillain5b3ee562015-04-14 16:02:41 +01003907 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00003908
Roland Levillaincff13742014-11-17 14:32:17 +00003909 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003910 // Processing a Dex `double-to-float' instruction.
3911 locations->SetInAt(0, Location::RequiresFpuRegister());
3912 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003913 break;
3914
3915 default:
3916 LOG(FATAL) << "Unexpected type conversion from " << input_type
3917 << " to " << result_type;
3918 };
3919 break;
3920
Roland Levillaindff1f282014-11-05 14:15:05 +00003921 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00003922 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00003923 case Primitive::kPrimBoolean:
3924 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00003925 case Primitive::kPrimByte:
3926 case Primitive::kPrimShort:
3927 case Primitive::kPrimInt:
3928 case Primitive::kPrimChar:
3929 // Processing a Dex `int-to-double' instruction.
3930 locations->SetInAt(0, Location::RequiresRegister());
3931 locations->SetOut(Location::RequiresFpuRegister());
3932 break;
3933
3934 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00003935 // Processing a Dex `long-to-double' instruction.
3936 locations->SetInAt(0, Location::RequiresRegister());
3937 locations->SetOut(Location::RequiresFpuRegister());
Roland Levillain682393c2015-04-14 15:57:52 +01003938 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain647b9ed2014-11-27 12:06:00 +00003939 locations->AddTemp(Location::RequiresFpuRegister());
3940 break;
3941
Roland Levillaincff13742014-11-17 14:32:17 +00003942 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00003943 // Processing a Dex `float-to-double' instruction.
3944 locations->SetInAt(0, Location::RequiresFpuRegister());
3945 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00003946 break;
3947
3948 default:
3949 LOG(FATAL) << "Unexpected type conversion from " << input_type
3950 << " to " << result_type;
3951 };
Roland Levillaindff1f282014-11-05 14:15:05 +00003952 break;
3953
3954 default:
3955 LOG(FATAL) << "Unexpected type conversion from " << input_type
3956 << " to " << result_type;
3957 }
3958}
3959
3960void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) {
3961 LocationSummary* locations = conversion->GetLocations();
3962 Location out = locations->Out();
3963 Location in = locations->InAt(0);
3964 Primitive::Type result_type = conversion->GetResultType();
3965 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00003966 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00003967 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00003968 case Primitive::kPrimByte:
3969 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003970 case Primitive::kPrimLong:
3971 // Type conversion from long to byte is a result of code transformations.
3972 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 8);
3973 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003974 case Primitive::kPrimBoolean:
3975 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00003976 case Primitive::kPrimShort:
3977 case Primitive::kPrimInt:
3978 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00003979 // Processing a Dex `int-to-byte' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00003980 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8);
Roland Levillain51d3fc42014-11-13 14:11:42 +00003981 break;
3982
3983 default:
3984 LOG(FATAL) << "Unexpected type conversion from " << input_type
3985 << " to " << result_type;
3986 }
3987 break;
3988
Roland Levillain01a8d712014-11-14 16:27:39 +00003989 case Primitive::kPrimShort:
3990 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00003991 case Primitive::kPrimLong:
3992 // Type conversion from long to short is a result of code transformations.
3993 __ sbfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
3994 break;
David Brazdil46e2a392015-03-16 17:31:52 +00003995 case Primitive::kPrimBoolean:
3996 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00003997 case Primitive::kPrimByte:
3998 case Primitive::kPrimInt:
3999 case Primitive::kPrimChar:
4000 // Processing a Dex `int-to-short' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004001 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain01a8d712014-11-14 16:27:39 +00004002 break;
4003
4004 default:
4005 LOG(FATAL) << "Unexpected type conversion from " << input_type
4006 << " to " << result_type;
4007 }
4008 break;
4009
Roland Levillain946e1432014-11-11 17:35:19 +00004010 case Primitive::kPrimInt:
4011 switch (input_type) {
4012 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00004013 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00004014 DCHECK(out.IsRegister());
4015 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004016 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00004017 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004018 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex());
Roland Levillain946e1432014-11-11 17:35:19 +00004019 } else {
4020 DCHECK(in.IsConstant());
4021 DCHECK(in.GetConstant()->IsLongConstant());
4022 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004023 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value));
Roland Levillain946e1432014-11-11 17:35:19 +00004024 }
4025 break;
4026
Roland Levillain3f8f9362014-12-02 17:45:01 +00004027 case Primitive::kPrimFloat: {
4028 // Processing a Dex `float-to-int' instruction.
4029 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01004030 __ vcvtis(temp, in.AsFpuRegister<SRegister>());
Roland Levillain3f8f9362014-12-02 17:45:01 +00004031 __ vmovrs(out.AsRegister<Register>(), temp);
4032 break;
4033 }
4034
Roland Levillain4c0b61f2014-12-05 12:06:01 +00004035 case Primitive::kPrimDouble: {
4036 // Processing a Dex `double-to-int' instruction.
4037 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Vladimir Marko8c5d3102016-07-07 12:07:44 +01004038 __ vcvtid(temp_s, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillain4c0b61f2014-12-05 12:06:01 +00004039 __ vmovrs(out.AsRegister<Register>(), temp_s);
Roland Levillain946e1432014-11-11 17:35:19 +00004040 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00004041 }
Roland Levillain946e1432014-11-11 17:35:19 +00004042
4043 default:
4044 LOG(FATAL) << "Unexpected type conversion from " << input_type
4045 << " to " << result_type;
4046 }
4047 break;
4048
Roland Levillaindff1f282014-11-05 14:15:05 +00004049 case Primitive::kPrimLong:
4050 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00004051 case Primitive::kPrimBoolean:
4052 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00004053 case Primitive::kPrimByte:
4054 case Primitive::kPrimShort:
4055 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00004056 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00004057 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00004058 DCHECK(out.IsRegisterPair());
4059 DCHECK(in.IsRegister());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004060 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>());
Roland Levillaindff1f282014-11-05 14:15:05 +00004061 // Sign extension.
4062 __ Asr(out.AsRegisterPairHigh<Register>(),
4063 out.AsRegisterPairLow<Register>(),
4064 31);
4065 break;
4066
4067 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00004068 // Processing a Dex `float-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004069 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004070 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00004071 break;
4072
Roland Levillaindff1f282014-11-05 14:15:05 +00004073 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00004074 // Processing a Dex `double-to-long' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004075 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004076 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00004077 break;
4078
4079 default:
4080 LOG(FATAL) << "Unexpected type conversion from " << input_type
4081 << " to " << result_type;
4082 }
4083 break;
4084
Roland Levillain981e4542014-11-14 11:47:14 +00004085 case Primitive::kPrimChar:
4086 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00004087 case Primitive::kPrimLong:
4088 // Type conversion from long to char is a result of code transformations.
4089 __ ubfx(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>(), 0, 16);
4090 break;
David Brazdil46e2a392015-03-16 17:31:52 +00004091 case Primitive::kPrimBoolean:
4092 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00004093 case Primitive::kPrimByte:
4094 case Primitive::kPrimShort:
4095 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00004096 // Processing a Dex `int-to-char' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004097 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16);
Roland Levillain981e4542014-11-14 11:47:14 +00004098 break;
4099
4100 default:
4101 LOG(FATAL) << "Unexpected type conversion from " << input_type
4102 << " to " << result_type;
4103 }
4104 break;
4105
Roland Levillaindff1f282014-11-05 14:15:05 +00004106 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00004107 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00004108 case Primitive::kPrimBoolean:
4109 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00004110 case Primitive::kPrimByte:
4111 case Primitive::kPrimShort:
4112 case Primitive::kPrimInt:
4113 case Primitive::kPrimChar: {
4114 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004115 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>());
4116 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00004117 break;
4118 }
4119
Roland Levillain5b3ee562015-04-14 16:02:41 +01004120 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00004121 // Processing a Dex `long-to-float' instruction.
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004122 codegen_->InvokeRuntime(kQuickL2f, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004123 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
Roland Levillain6d0e4832014-11-27 18:31:21 +00004124 break;
Roland Levillain6d0e4832014-11-27 18:31:21 +00004125
Roland Levillaincff13742014-11-17 14:32:17 +00004126 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00004127 // Processing a Dex `double-to-float' instruction.
4128 __ vcvtsd(out.AsFpuRegister<SRegister>(),
4129 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>()));
Roland Levillaincff13742014-11-17 14:32:17 +00004130 break;
4131
4132 default:
4133 LOG(FATAL) << "Unexpected type conversion from " << input_type
4134 << " to " << result_type;
4135 };
4136 break;
4137
Roland Levillaindff1f282014-11-05 14:15:05 +00004138 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00004139 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00004140 case Primitive::kPrimBoolean:
4141 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00004142 case Primitive::kPrimByte:
4143 case Primitive::kPrimShort:
4144 case Primitive::kPrimInt:
4145 case Primitive::kPrimChar: {
4146 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00004147 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00004148 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4149 out.AsFpuRegisterPairLow<SRegister>());
4150 break;
4151 }
4152
Roland Levillain647b9ed2014-11-27 12:06:00 +00004153 case Primitive::kPrimLong: {
4154 // Processing a Dex `long-to-double' instruction.
4155 Register low = in.AsRegisterPairLow<Register>();
4156 Register high = in.AsRegisterPairHigh<Register>();
4157 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>();
4158 DRegister out_d = FromLowSToD(out_s);
Roland Levillain682393c2015-04-14 15:57:52 +01004159 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>();
Roland Levillain647b9ed2014-11-27 12:06:00 +00004160 DRegister temp_d = FromLowSToD(temp_s);
Roland Levillain682393c2015-04-14 15:57:52 +01004161 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>();
4162 DRegister constant_d = FromLowSToD(constant_s);
Roland Levillain647b9ed2014-11-27 12:06:00 +00004163
Roland Levillain682393c2015-04-14 15:57:52 +01004164 // temp_d = int-to-double(high)
4165 __ vmovsr(temp_s, high);
4166 __ vcvtdi(temp_d, temp_s);
4167 // constant_d = k2Pow32EncodingForDouble
4168 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble));
4169 // out_d = unsigned-to-double(low)
4170 __ vmovsr(out_s, low);
4171 __ vcvtdu(out_d, out_s);
4172 // out_d += temp_d * constant_d
4173 __ vmlad(out_d, temp_d, constant_d);
Roland Levillain647b9ed2014-11-27 12:06:00 +00004174 break;
4175 }
4176
Roland Levillaincff13742014-11-17 14:32:17 +00004177 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00004178 // Processing a Dex `float-to-double' instruction.
4179 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4180 in.AsFpuRegister<SRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00004181 break;
4182
4183 default:
4184 LOG(FATAL) << "Unexpected type conversion from " << input_type
4185 << " to " << result_type;
4186 };
Roland Levillaindff1f282014-11-05 14:15:05 +00004187 break;
4188
4189 default:
4190 LOG(FATAL) << "Unexpected type conversion from " << input_type
4191 << " to " << result_type;
4192 }
4193}
4194
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004195void LocationsBuilderARM::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004196 LocationSummary* locations =
4197 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004198 switch (add->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004199 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004200 locations->SetInAt(0, Location::RequiresRegister());
4201 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004202 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4203 break;
4204 }
4205
4206 case Primitive::kPrimLong: {
4207 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01004208 locations->SetInAt(1, ArmEncodableConstantOrRegister(add->InputAt(1), ADD));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004209 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004210 break;
4211 }
4212
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004213 case Primitive::kPrimFloat:
4214 case Primitive::kPrimDouble: {
4215 locations->SetInAt(0, Location::RequiresFpuRegister());
4216 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004217 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004218 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004219 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004220
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004221 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004222 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004223 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004224}
4225
4226void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) {
4227 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004228 Location out = locations->Out();
4229 Location first = locations->InAt(0);
4230 Location second = locations->InAt(1);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004231 switch (add->GetResultType()) {
4232 case Primitive::kPrimInt:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004233 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004234 __ add(out.AsRegister<Register>(),
4235 first.AsRegister<Register>(),
4236 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004237 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004238 __ AddConstant(out.AsRegister<Register>(),
4239 first.AsRegister<Register>(),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004240 second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004241 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004242 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004243
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004244 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01004245 if (second.IsConstant()) {
4246 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4247 GenerateAddLongConst(out, first, value);
4248 } else {
4249 DCHECK(second.IsRegisterPair());
4250 __ adds(out.AsRegisterPairLow<Register>(),
4251 first.AsRegisterPairLow<Register>(),
4252 ShifterOperand(second.AsRegisterPairLow<Register>()));
4253 __ adc(out.AsRegisterPairHigh<Register>(),
4254 first.AsRegisterPairHigh<Register>(),
4255 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4256 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004257 break;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004258 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004259
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004260 case Primitive::kPrimFloat:
Roland Levillain199f3362014-11-27 17:15:16 +00004261 __ vadds(out.AsFpuRegister<SRegister>(),
4262 first.AsFpuRegister<SRegister>(),
4263 second.AsFpuRegister<SRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004264 break;
4265
4266 case Primitive::kPrimDouble:
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004267 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4268 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4269 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004270 break;
4271
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004272 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01004273 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00004274 }
4275}
4276
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004277void LocationsBuilderARM::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004278 LocationSummary* locations =
4279 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004280 switch (sub->GetResultType()) {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004281 case Primitive::kPrimInt: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004282 locations->SetInAt(0, Location::RequiresRegister());
4283 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004284 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4285 break;
4286 }
4287
4288 case Primitive::kPrimLong: {
4289 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko59751a72016-08-05 14:37:27 +01004290 locations->SetInAt(1, ArmEncodableConstantOrRegister(sub->InputAt(1), SUB));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00004291 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004292 break;
4293 }
Calin Juravle11351682014-10-23 15:38:15 +01004294 case Primitive::kPrimFloat:
4295 case Primitive::kPrimDouble: {
4296 locations->SetInAt(0, Location::RequiresFpuRegister());
4297 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004298 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004299 break;
Calin Juravle11351682014-10-23 15:38:15 +01004300 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004301 default:
Calin Juravle11351682014-10-23 15:38:15 +01004302 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004303 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004304}
4305
4306void InstructionCodeGeneratorARM::VisitSub(HSub* sub) {
4307 LocationSummary* locations = sub->GetLocations();
Calin Juravle11351682014-10-23 15:38:15 +01004308 Location out = locations->Out();
4309 Location first = locations->InAt(0);
4310 Location second = locations->InAt(1);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004311 switch (sub->GetResultType()) {
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004312 case Primitive::kPrimInt: {
Calin Juravle11351682014-10-23 15:38:15 +01004313 if (second.IsRegister()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004314 __ sub(out.AsRegister<Register>(),
4315 first.AsRegister<Register>(),
4316 ShifterOperand(second.AsRegister<Register>()));
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004317 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004318 __ AddConstant(out.AsRegister<Register>(),
4319 first.AsRegister<Register>(),
Calin Juravle11351682014-10-23 15:38:15 +01004320 -second.GetConstant()->AsIntConstant()->GetValue());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004321 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004322 break;
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004323 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004324
Calin Juravle11351682014-10-23 15:38:15 +01004325 case Primitive::kPrimLong: {
Vladimir Marko59751a72016-08-05 14:37:27 +01004326 if (second.IsConstant()) {
4327 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
4328 GenerateAddLongConst(out, first, -value);
4329 } else {
4330 DCHECK(second.IsRegisterPair());
4331 __ subs(out.AsRegisterPairLow<Register>(),
4332 first.AsRegisterPairLow<Register>(),
4333 ShifterOperand(second.AsRegisterPairLow<Register>()));
4334 __ sbc(out.AsRegisterPairHigh<Register>(),
4335 first.AsRegisterPairHigh<Register>(),
4336 ShifterOperand(second.AsRegisterPairHigh<Register>()));
4337 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004338 break;
Calin Juravle11351682014-10-23 15:38:15 +01004339 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004340
Calin Juravle11351682014-10-23 15:38:15 +01004341 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004342 __ vsubs(out.AsFpuRegister<SRegister>(),
4343 first.AsFpuRegister<SRegister>(),
4344 second.AsFpuRegister<SRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004345 break;
Calin Juravle11351682014-10-23 15:38:15 +01004346 }
4347
4348 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004349 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4350 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4351 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravle11351682014-10-23 15:38:15 +01004352 break;
4353 }
4354
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01004355
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004356 default:
Calin Juravle11351682014-10-23 15:38:15 +01004357 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004358 }
4359}
4360
Calin Juravle34bacdf2014-10-07 20:23:36 +01004361void LocationsBuilderARM::VisitMul(HMul* mul) {
4362 LocationSummary* locations =
4363 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
4364 switch (mul->GetResultType()) {
4365 case Primitive::kPrimInt:
4366 case Primitive::kPrimLong: {
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004367 locations->SetInAt(0, Location::RequiresRegister());
4368 locations->SetInAt(1, Location::RequiresRegister());
4369 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004370 break;
4371 }
4372
Calin Juravleb5bfa962014-10-21 18:02:24 +01004373 case Primitive::kPrimFloat:
4374 case Primitive::kPrimDouble: {
4375 locations->SetInAt(0, Location::RequiresFpuRegister());
4376 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004377 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Calin Juravle34bacdf2014-10-07 20:23:36 +01004378 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004379 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004380
4381 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004382 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004383 }
4384}
4385
4386void InstructionCodeGeneratorARM::VisitMul(HMul* mul) {
4387 LocationSummary* locations = mul->GetLocations();
4388 Location out = locations->Out();
4389 Location first = locations->InAt(0);
4390 Location second = locations->InAt(1);
4391 switch (mul->GetResultType()) {
4392 case Primitive::kPrimInt: {
Roland Levillain199f3362014-11-27 17:15:16 +00004393 __ mul(out.AsRegister<Register>(),
4394 first.AsRegister<Register>(),
4395 second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004396 break;
4397 }
4398 case Primitive::kPrimLong: {
4399 Register out_hi = out.AsRegisterPairHigh<Register>();
4400 Register out_lo = out.AsRegisterPairLow<Register>();
4401 Register in1_hi = first.AsRegisterPairHigh<Register>();
4402 Register in1_lo = first.AsRegisterPairLow<Register>();
4403 Register in2_hi = second.AsRegisterPairHigh<Register>();
4404 Register in2_lo = second.AsRegisterPairLow<Register>();
4405
4406 // Extra checks to protect caused by the existence of R1_R2.
4407 // The algorithm is wrong if out.hi is either in1.lo or in2.lo:
4408 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2);
4409 DCHECK_NE(out_hi, in1_lo);
4410 DCHECK_NE(out_hi, in2_lo);
4411
4412 // input: in1 - 64 bits, in2 - 64 bits
4413 // output: out
4414 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
4415 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
4416 // parts: out.lo = (in1.lo * in2.lo)[31:0]
4417
4418 // IP <- in1.lo * in2.hi
4419 __ mul(IP, in1_lo, in2_hi);
4420 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo
4421 __ mla(out_hi, in1_hi, in2_lo, IP);
4422 // out.lo <- (in1.lo * in2.lo)[31:0];
4423 __ umull(out_lo, IP, in1_lo, in2_lo);
4424 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
4425 __ add(out_hi, out_hi, ShifterOperand(IP));
4426 break;
4427 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01004428
4429 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004430 __ vmuls(out.AsFpuRegister<SRegister>(),
4431 first.AsFpuRegister<SRegister>(),
4432 second.AsFpuRegister<SRegister>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01004433 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01004434 }
4435
4436 case Primitive::kPrimDouble: {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +00004437 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4438 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4439 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
Calin Juravleb5bfa962014-10-21 18:02:24 +01004440 break;
4441 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01004442
4443 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01004444 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01004445 }
4446}
4447
Zheng Xuc6667102015-05-15 16:08:45 +08004448void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
4449 DCHECK(instruction->IsDiv() || instruction->IsRem());
4450 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4451
4452 LocationSummary* locations = instruction->GetLocations();
4453 Location second = locations->InAt(1);
4454 DCHECK(second.IsConstant());
4455
4456 Register out = locations->Out().AsRegister<Register>();
4457 Register dividend = locations->InAt(0).AsRegister<Register>();
4458 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4459 DCHECK(imm == 1 || imm == -1);
4460
4461 if (instruction->IsRem()) {
4462 __ LoadImmediate(out, 0);
4463 } else {
4464 if (imm == 1) {
4465 __ Mov(out, dividend);
4466 } else {
4467 __ rsb(out, dividend, ShifterOperand(0));
4468 }
4469 }
4470}
4471
4472void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
4473 DCHECK(instruction->IsDiv() || instruction->IsRem());
4474 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4475
4476 LocationSummary* locations = instruction->GetLocations();
4477 Location second = locations->InAt(1);
4478 DCHECK(second.IsConstant());
4479
4480 Register out = locations->Out().AsRegister<Register>();
4481 Register dividend = locations->InAt(0).AsRegister<Register>();
4482 Register temp = locations->GetTemp(0).AsRegister<Register>();
4483 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004484 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Zheng Xuc6667102015-05-15 16:08:45 +08004485 int ctz_imm = CTZ(abs_imm);
4486
4487 if (ctz_imm == 1) {
4488 __ Lsr(temp, dividend, 32 - ctz_imm);
4489 } else {
4490 __ Asr(temp, dividend, 31);
4491 __ Lsr(temp, temp, 32 - ctz_imm);
4492 }
4493 __ add(out, temp, ShifterOperand(dividend));
4494
4495 if (instruction->IsDiv()) {
4496 __ Asr(out, out, ctz_imm);
4497 if (imm < 0) {
4498 __ rsb(out, out, ShifterOperand(0));
4499 }
4500 } else {
4501 __ ubfx(out, out, 0, ctz_imm);
4502 __ sub(out, out, ShifterOperand(temp));
4503 }
4504}
4505
4506void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4507 DCHECK(instruction->IsDiv() || instruction->IsRem());
4508 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4509
4510 LocationSummary* locations = instruction->GetLocations();
4511 Location second = locations->InAt(1);
4512 DCHECK(second.IsConstant());
4513
4514 Register out = locations->Out().AsRegister<Register>();
4515 Register dividend = locations->InAt(0).AsRegister<Register>();
4516 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
4517 Register temp2 = locations->GetTemp(1).AsRegister<Register>();
4518 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4519
4520 int64_t magic;
4521 int shift;
4522 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4523
4524 __ LoadImmediate(temp1, magic);
4525 __ smull(temp2, temp1, dividend, temp1);
4526
4527 if (imm > 0 && magic < 0) {
4528 __ add(temp1, temp1, ShifterOperand(dividend));
4529 } else if (imm < 0 && magic > 0) {
4530 __ sub(temp1, temp1, ShifterOperand(dividend));
4531 }
4532
4533 if (shift != 0) {
4534 __ Asr(temp1, temp1, shift);
4535 }
4536
4537 if (instruction->IsDiv()) {
4538 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31));
4539 } else {
4540 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31));
4541 // TODO: Strength reduction for mls.
4542 __ LoadImmediate(temp2, imm);
4543 __ mls(out, temp1, temp2, dividend);
4544 }
4545}
4546
4547void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) {
4548 DCHECK(instruction->IsDiv() || instruction->IsRem());
4549 DCHECK(instruction->GetResultType() == Primitive::kPrimInt);
4550
4551 LocationSummary* locations = instruction->GetLocations();
4552 Location second = locations->InAt(1);
4553 DCHECK(second.IsConstant());
4554
4555 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4556 if (imm == 0) {
4557 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4558 } else if (imm == 1 || imm == -1) {
4559 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004560 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004561 DivRemByPowerOfTwo(instruction);
4562 } else {
4563 DCHECK(imm <= -2 || imm >= 2);
4564 GenerateDivRemWithAnyConstant(instruction);
4565 }
4566}
4567
Calin Juravle7c4954d2014-10-28 16:57:40 +00004568void LocationsBuilderARM::VisitDiv(HDiv* div) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004569 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
4570 if (div->GetResultType() == Primitive::kPrimLong) {
4571 // pLdiv runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004572 call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004573 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) {
4574 // sdiv will be replaced by other instruction sequence.
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004575 } else if (div->GetResultType() == Primitive::kPrimInt &&
4576 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
4577 // pIdivmod runtime call.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004578 call_kind = LocationSummary::kCallOnMainOnly;
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004579 }
4580
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004581 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
4582
Calin Juravle7c4954d2014-10-28 16:57:40 +00004583 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004584 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004585 if (div->InputAt(1)->IsConstant()) {
4586 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004587 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004588 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004589 int32_t value = div->InputAt(1)->AsIntConstant()->GetValue();
4590 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004591 // No temp register required.
4592 } else {
4593 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004594 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004595 locations->AddTemp(Location::RequiresRegister());
4596 }
4597 }
4598 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004599 locations->SetInAt(0, Location::RequiresRegister());
4600 locations->SetInAt(1, Location::RequiresRegister());
4601 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4602 } else {
4603 InvokeRuntimeCallingConvention calling_convention;
4604 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4605 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004606 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004607 // we only need the former.
4608 locations->SetOut(Location::RegisterLocation(R0));
4609 }
Calin Juravled0d48522014-11-04 16:40:20 +00004610 break;
4611 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00004612 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004613 InvokeRuntimeCallingConvention calling_convention;
4614 locations->SetInAt(0, Location::RegisterPairLocation(
4615 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4616 locations->SetInAt(1, Location::RegisterPairLocation(
4617 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004618 locations->SetOut(Location::RegisterPairLocation(R0, R1));
Calin Juravle7c4954d2014-10-28 16:57:40 +00004619 break;
4620 }
4621 case Primitive::kPrimFloat:
4622 case Primitive::kPrimDouble: {
4623 locations->SetInAt(0, Location::RequiresFpuRegister());
4624 locations->SetInAt(1, Location::RequiresFpuRegister());
4625 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4626 break;
4627 }
4628
4629 default:
4630 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4631 }
4632}
4633
4634void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) {
4635 LocationSummary* locations = div->GetLocations();
4636 Location out = locations->Out();
4637 Location first = locations->InAt(0);
4638 Location second = locations->InAt(1);
4639
4640 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00004641 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004642 if (second.IsConstant()) {
4643 GenerateDivRemConstantIntegral(div);
4644 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004645 __ sdiv(out.AsRegister<Register>(),
4646 first.AsRegister<Register>(),
4647 second.AsRegister<Register>());
4648 } else {
4649 InvokeRuntimeCallingConvention calling_convention;
4650 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4651 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4652 DCHECK_EQ(R0, out.AsRegister<Register>());
4653
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004654 codegen_->InvokeRuntime(kQuickIdivmod, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004655 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004656 }
Calin Juravled0d48522014-11-04 16:40:20 +00004657 break;
4658 }
4659
Calin Juravle7c4954d2014-10-28 16:57:40 +00004660 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004661 InvokeRuntimeCallingConvention calling_convention;
4662 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
4663 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
4664 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
4665 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
4666 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>());
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00004667 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004668
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004669 codegen_->InvokeRuntime(kQuickLdiv, div, div->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004670 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravle7c4954d2014-10-28 16:57:40 +00004671 break;
4672 }
4673
4674 case Primitive::kPrimFloat: {
Roland Levillain199f3362014-11-27 17:15:16 +00004675 __ vdivs(out.AsFpuRegister<SRegister>(),
4676 first.AsFpuRegister<SRegister>(),
4677 second.AsFpuRegister<SRegister>());
Calin Juravle7c4954d2014-10-28 16:57:40 +00004678 break;
4679 }
4680
4681 case Primitive::kPrimDouble: {
4682 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()),
4683 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()),
4684 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>()));
4685 break;
4686 }
4687
4688 default:
4689 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
4690 }
4691}
4692
Calin Juravlebacfec32014-11-14 15:54:36 +00004693void LocationsBuilderARM::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004694 Primitive::Type type = rem->GetResultType();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004695
4696 // Most remainders are implemented in the runtime.
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004697 LocationSummary::CallKind call_kind = LocationSummary::kCallOnMainOnly;
Zheng Xuc6667102015-05-15 16:08:45 +08004698 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) {
4699 // sdiv will be replaced by other instruction sequence.
4700 call_kind = LocationSummary::kNoCall;
4701 } else if ((rem->GetResultType() == Primitive::kPrimInt)
4702 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004703 // Have hardware divide instruction for int, do it with three instructions.
4704 call_kind = LocationSummary::kNoCall;
4705 }
4706
Calin Juravlebacfec32014-11-14 15:54:36 +00004707 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
4708
Calin Juravled2ec87d2014-12-08 14:24:46 +00004709 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004710 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004711 if (rem->InputAt(1)->IsConstant()) {
4712 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko13c86fd2015-11-11 12:37:46 +00004713 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
Zheng Xuc6667102015-05-15 16:08:45 +08004714 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004715 int32_t value = rem->InputAt(1)->AsIntConstant()->GetValue();
4716 if (value == 1 || value == 0 || value == -1) {
Zheng Xuc6667102015-05-15 16:08:45 +08004717 // No temp register required.
4718 } else {
4719 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004720 if (!IsPowerOfTwo(AbsOrMin(value))) {
Zheng Xuc6667102015-05-15 16:08:45 +08004721 locations->AddTemp(Location::RequiresRegister());
4722 }
4723 }
4724 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004725 locations->SetInAt(0, Location::RequiresRegister());
4726 locations->SetInAt(1, Location::RequiresRegister());
4727 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4728 locations->AddTemp(Location::RequiresRegister());
4729 } else {
4730 InvokeRuntimeCallingConvention calling_convention;
4731 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4732 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Roland Levillain5e8d5f02016-10-18 18:03:43 +01004733 // Note: divmod will compute both the quotient and the remainder as the pair R0 and R1, but
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004734 // we only need the latter.
4735 locations->SetOut(Location::RegisterLocation(R1));
4736 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004737 break;
4738 }
4739 case Primitive::kPrimLong: {
4740 InvokeRuntimeCallingConvention calling_convention;
4741 locations->SetInAt(0, Location::RegisterPairLocation(
4742 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4743 locations->SetInAt(1, Location::RegisterPairLocation(
4744 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4745 // The runtime helper puts the output in R2,R3.
4746 locations->SetOut(Location::RegisterPairLocation(R2, R3));
4747 break;
4748 }
Calin Juravled2ec87d2014-12-08 14:24:46 +00004749 case Primitive::kPrimFloat: {
4750 InvokeRuntimeCallingConvention calling_convention;
4751 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
4752 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
4753 locations->SetOut(Location::FpuRegisterLocation(S0));
4754 break;
4755 }
4756
Calin Juravlebacfec32014-11-14 15:54:36 +00004757 case Primitive::kPrimDouble: {
Calin Juravled2ec87d2014-12-08 14:24:46 +00004758 InvokeRuntimeCallingConvention calling_convention;
4759 locations->SetInAt(0, Location::FpuRegisterPairLocation(
4760 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1)));
4761 locations->SetInAt(1, Location::FpuRegisterPairLocation(
4762 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3)));
4763 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1));
Calin Juravlebacfec32014-11-14 15:54:36 +00004764 break;
4765 }
4766
4767 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004768 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004769 }
4770}
4771
4772void InstructionCodeGeneratorARM::VisitRem(HRem* rem) {
4773 LocationSummary* locations = rem->GetLocations();
4774 Location out = locations->Out();
4775 Location first = locations->InAt(0);
4776 Location second = locations->InAt(1);
4777
Calin Juravled2ec87d2014-12-08 14:24:46 +00004778 Primitive::Type type = rem->GetResultType();
4779 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00004780 case Primitive::kPrimInt: {
Zheng Xuc6667102015-05-15 16:08:45 +08004781 if (second.IsConstant()) {
4782 GenerateDivRemConstantIntegral(rem);
4783 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) {
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004784 Register reg1 = first.AsRegister<Register>();
4785 Register reg2 = second.AsRegister<Register>();
4786 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004787
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004788 // temp = reg1 / reg2 (integer division)
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004789 // dest = reg1 - temp * reg2
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004790 __ sdiv(temp, reg1, reg2);
Vladimir Marko73cf0fb2015-07-30 15:07:22 +01004791 __ mls(out.AsRegister<Register>(), temp, reg2, reg1);
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004792 } else {
4793 InvokeRuntimeCallingConvention calling_convention;
4794 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>());
4795 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>());
4796 DCHECK_EQ(R1, out.AsRegister<Register>());
4797
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004798 codegen_->InvokeRuntime(kQuickIdivmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004799 CheckEntrypointTypes<kQuickIdivmod, int32_t, int32_t, int32_t>();
Andreas Gampeb51cdb32015-03-29 17:32:48 -07004800 }
Calin Juravlebacfec32014-11-14 15:54:36 +00004801 break;
4802 }
4803
4804 case Primitive::kPrimLong: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004805 codegen_->InvokeRuntime(kQuickLmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004806 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004807 break;
4808 }
4809
Calin Juravled2ec87d2014-12-08 14:24:46 +00004810 case Primitive::kPrimFloat: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004811 codegen_->InvokeRuntime(kQuickFmodf, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004812 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Calin Juravled2ec87d2014-12-08 14:24:46 +00004813 break;
4814 }
4815
Calin Juravlebacfec32014-11-14 15:54:36 +00004816 case Primitive::kPrimDouble: {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01004817 codegen_->InvokeRuntime(kQuickFmod, rem, rem->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004818 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Calin Juravlebacfec32014-11-14 15:54:36 +00004819 break;
4820 }
4821
4822 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00004823 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00004824 }
4825}
4826
Calin Juravled0d48522014-11-04 16:40:20 +00004827void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004828 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004829 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Calin Juravled0d48522014-11-04 16:40:20 +00004830}
4831
4832void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01004833 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00004834 codegen_->AddSlowPath(slow_path);
4835
4836 LocationSummary* locations = instruction->GetLocations();
4837 Location value = locations->InAt(0);
4838
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004839 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00004840 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06004841 case Primitive::kPrimByte:
4842 case Primitive::kPrimChar:
4843 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004844 case Primitive::kPrimInt: {
4845 if (value.IsRegister()) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01004846 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00004847 } else {
4848 DCHECK(value.IsConstant()) << value;
4849 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4850 __ b(slow_path->GetEntryLabel());
4851 }
4852 }
4853 break;
4854 }
4855 case Primitive::kPrimLong: {
4856 if (value.IsRegisterPair()) {
4857 __ orrs(IP,
4858 value.AsRegisterPairLow<Register>(),
4859 ShifterOperand(value.AsRegisterPairHigh<Register>()));
4860 __ b(slow_path->GetEntryLabel(), EQ);
4861 } else {
4862 DCHECK(value.IsConstant()) << value;
4863 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4864 __ b(slow_path->GetEntryLabel());
4865 }
4866 }
4867 break;
4868 default:
4869 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
4870 }
4871 }
Calin Juravled0d48522014-11-04 16:40:20 +00004872}
4873
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004874void InstructionCodeGeneratorARM::HandleIntegerRotate(LocationSummary* locations) {
4875 Register in = locations->InAt(0).AsRegister<Register>();
4876 Location rhs = locations->InAt(1);
4877 Register out = locations->Out().AsRegister<Register>();
4878
4879 if (rhs.IsConstant()) {
4880 // Arm32 and Thumb2 assemblers require a rotation on the interval [1,31],
4881 // so map all rotations to a +ve. equivalent in that range.
4882 // (e.g. left *or* right by -2 bits == 30 bits in the same direction.)
4883 uint32_t rot = CodeGenerator::GetInt32ValueOf(rhs.GetConstant()) & 0x1F;
4884 if (rot) {
4885 // Rotate, mapping left rotations to right equivalents if necessary.
4886 // (e.g. left by 2 bits == right by 30.)
4887 __ Ror(out, in, rot);
4888 } else if (out != in) {
4889 __ Mov(out, in);
4890 }
4891 } else {
4892 __ Ror(out, in, rhs.AsRegister<Register>());
4893 }
4894}
4895
4896// Gain some speed by mapping all Long rotates onto equivalent pairs of Integer
4897// rotates by swapping input regs (effectively rotating by the first 32-bits of
4898// a larger rotation) or flipping direction (thus treating larger right/left
4899// rotations as sub-word sized rotations in the other direction) as appropriate.
Anton Kirilov6f644202017-02-27 18:29:45 +00004900void InstructionCodeGeneratorARM::HandleLongRotate(HRor* ror) {
4901 LocationSummary* locations = ror->GetLocations();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004902 Register in_reg_lo = locations->InAt(0).AsRegisterPairLow<Register>();
4903 Register in_reg_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
4904 Location rhs = locations->InAt(1);
4905 Register out_reg_lo = locations->Out().AsRegisterPairLow<Register>();
4906 Register out_reg_hi = locations->Out().AsRegisterPairHigh<Register>();
4907
4908 if (rhs.IsConstant()) {
4909 uint64_t rot = CodeGenerator::GetInt64ValueOf(rhs.GetConstant());
4910 // Map all rotations to +ve. equivalents on the interval [0,63].
Roland Levillain5b5b9312016-03-22 14:57:31 +00004911 rot &= kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004912 // For rotates over a word in size, 'pre-rotate' by 32-bits to keep rotate
4913 // logic below to a simple pair of binary orr.
4914 // (e.g. 34 bits == in_reg swap + 2 bits right.)
4915 if (rot >= kArmBitsPerWord) {
4916 rot -= kArmBitsPerWord;
4917 std::swap(in_reg_hi, in_reg_lo);
4918 }
4919 // Rotate, or mov to out for zero or word size rotations.
4920 if (rot != 0u) {
4921 __ Lsr(out_reg_hi, in_reg_hi, rot);
4922 __ orr(out_reg_hi, out_reg_hi, ShifterOperand(in_reg_lo, arm::LSL, kArmBitsPerWord - rot));
4923 __ Lsr(out_reg_lo, in_reg_lo, rot);
4924 __ orr(out_reg_lo, out_reg_lo, ShifterOperand(in_reg_hi, arm::LSL, kArmBitsPerWord - rot));
4925 } else {
4926 __ Mov(out_reg_lo, in_reg_lo);
4927 __ Mov(out_reg_hi, in_reg_hi);
4928 }
4929 } else {
4930 Register shift_right = locations->GetTemp(0).AsRegister<Register>();
4931 Register shift_left = locations->GetTemp(1).AsRegister<Register>();
4932 Label end;
4933 Label shift_by_32_plus_shift_right;
Anton Kirilov6f644202017-02-27 18:29:45 +00004934 Label* final_label = codegen_->GetFinalLabel(ror, &end);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004935
4936 __ and_(shift_right, rhs.AsRegister<Register>(), ShifterOperand(0x1F));
4937 __ Lsrs(shift_left, rhs.AsRegister<Register>(), 6);
4938 __ rsb(shift_left, shift_right, ShifterOperand(kArmBitsPerWord), AL, kCcKeep);
4939 __ b(&shift_by_32_plus_shift_right, CC);
4940
4941 // out_reg_hi = (reg_hi << shift_left) | (reg_lo >> shift_right).
4942 // out_reg_lo = (reg_lo << shift_left) | (reg_hi >> shift_right).
4943 __ Lsl(out_reg_hi, in_reg_hi, shift_left);
4944 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4945 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4946 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4947 __ Lsr(shift_left, in_reg_hi, shift_right);
4948 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_left));
Anton Kirilov6f644202017-02-27 18:29:45 +00004949 __ b(final_label);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004950
4951 __ Bind(&shift_by_32_plus_shift_right); // Shift by 32+shift_right.
4952 // out_reg_hi = (reg_hi >> shift_right) | (reg_lo << shift_left).
4953 // out_reg_lo = (reg_lo >> shift_right) | (reg_hi << shift_left).
4954 __ Lsr(out_reg_hi, in_reg_hi, shift_right);
4955 __ Lsl(out_reg_lo, in_reg_lo, shift_left);
4956 __ add(out_reg_hi, out_reg_hi, ShifterOperand(out_reg_lo));
4957 __ Lsr(out_reg_lo, in_reg_lo, shift_right);
4958 __ Lsl(shift_right, in_reg_hi, shift_left);
4959 __ add(out_reg_lo, out_reg_lo, ShifterOperand(shift_right));
4960
Anton Kirilov6f644202017-02-27 18:29:45 +00004961 if (end.IsLinked()) {
4962 __ Bind(&end);
4963 }
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004964 }
4965}
Roland Levillain22c49222016-03-18 14:04:28 +00004966
4967void LocationsBuilderARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004968 LocationSummary* locations =
4969 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4970 switch (ror->GetResultType()) {
4971 case Primitive::kPrimInt: {
4972 locations->SetInAt(0, Location::RequiresRegister());
4973 locations->SetInAt(1, Location::RegisterOrConstant(ror->InputAt(1)));
4974 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4975 break;
4976 }
4977 case Primitive::kPrimLong: {
4978 locations->SetInAt(0, Location::RequiresRegister());
4979 if (ror->InputAt(1)->IsConstant()) {
4980 locations->SetInAt(1, Location::ConstantLocation(ror->InputAt(1)->AsConstant()));
4981 } else {
4982 locations->SetInAt(1, Location::RequiresRegister());
4983 locations->AddTemp(Location::RequiresRegister());
4984 locations->AddTemp(Location::RequiresRegister());
4985 }
4986 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
4987 break;
4988 }
4989 default:
4990 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4991 }
4992}
4993
Roland Levillain22c49222016-03-18 14:04:28 +00004994void InstructionCodeGeneratorARM::VisitRor(HRor* ror) {
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004995 LocationSummary* locations = ror->GetLocations();
4996 Primitive::Type type = ror->GetResultType();
4997 switch (type) {
4998 case Primitive::kPrimInt: {
4999 HandleIntegerRotate(locations);
5000 break;
5001 }
5002 case Primitive::kPrimLong: {
Anton Kirilov6f644202017-02-27 18:29:45 +00005003 HandleLongRotate(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005004 break;
5005 }
5006 default:
5007 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko351dddf2015-12-11 16:34:46 +00005008 UNREACHABLE();
Scott Wakeling40a04bf2015-12-11 09:50:36 +00005009 }
5010}
5011
Calin Juravle9aec02f2014-11-18 23:06:35 +00005012void LocationsBuilderARM::HandleShift(HBinaryOperation* op) {
5013 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5014
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005015 LocationSummary* locations =
5016 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005017
5018 switch (op->GetResultType()) {
5019 case Primitive::kPrimInt: {
5020 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005021 if (op->InputAt(1)->IsConstant()) {
5022 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5023 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5024 } else {
5025 locations->SetInAt(1, Location::RequiresRegister());
5026 // Make the output overlap, as it will be used to hold the masked
5027 // second input.
5028 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5029 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005030 break;
5031 }
5032 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005033 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005034 if (op->InputAt(1)->IsConstant()) {
5035 locations->SetInAt(1, Location::ConstantLocation(op->InputAt(1)->AsConstant()));
5036 // For simplicity, use kOutputOverlap even though we only require that low registers
5037 // don't clash with high registers which the register allocator currently guarantees.
5038 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5039 } else {
5040 locations->SetInAt(1, Location::RequiresRegister());
5041 locations->AddTemp(Location::RequiresRegister());
5042 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
5043 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005044 break;
5045 }
5046 default:
5047 LOG(FATAL) << "Unexpected operation type " << op->GetResultType();
5048 }
5049}
5050
5051void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) {
5052 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
5053
5054 LocationSummary* locations = op->GetLocations();
5055 Location out = locations->Out();
5056 Location first = locations->InAt(0);
5057 Location second = locations->InAt(1);
5058
5059 Primitive::Type type = op->GetResultType();
5060 switch (type) {
5061 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005062 Register out_reg = out.AsRegister<Register>();
5063 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005064 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005065 Register second_reg = second.AsRegister<Register>();
Roland Levillainc9285912015-12-18 10:38:42 +00005066 // ARM doesn't mask the shift count so we need to do it ourselves.
Roland Levillain5b5b9312016-03-22 14:57:31 +00005067 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftDistance));
Calin Juravle9aec02f2014-11-18 23:06:35 +00005068 if (op->IsShl()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01005069 __ Lsl(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005070 } else if (op->IsShr()) {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01005071 __ Asr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005072 } else {
Nicolas Geoffraya4f35812015-06-22 23:12:45 +01005073 __ Lsr(out_reg, first_reg, out_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00005074 }
5075 } else {
5076 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00005077 uint32_t shift_value = cst & kMaxIntShiftDistance;
Roland Levillainc9285912015-12-18 10:38:42 +00005078 if (shift_value == 0) { // ARM does not support shifting with 0 immediate.
Calin Juravle9aec02f2014-11-18 23:06:35 +00005079 __ Mov(out_reg, first_reg);
5080 } else if (op->IsShl()) {
5081 __ Lsl(out_reg, first_reg, shift_value);
5082 } else if (op->IsShr()) {
5083 __ Asr(out_reg, first_reg, shift_value);
5084 } else {
5085 __ Lsr(out_reg, first_reg, shift_value);
5086 }
5087 }
5088 break;
5089 }
5090 case Primitive::kPrimLong: {
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005091 Register o_h = out.AsRegisterPairHigh<Register>();
5092 Register o_l = out.AsRegisterPairLow<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005093
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005094 Register high = first.AsRegisterPairHigh<Register>();
5095 Register low = first.AsRegisterPairLow<Register>();
5096
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005097 if (second.IsRegister()) {
5098 Register temp = locations->GetTemp(0).AsRegister<Register>();
Guillaume "Vermeille" Sanchezfd18f5a2015-03-11 14:57:40 +00005099
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005100 Register second_reg = second.AsRegister<Register>();
5101
5102 if (op->IsShl()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005103 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005104 // Shift the high part
5105 __ Lsl(o_h, high, o_l);
5106 // Shift the low part and `or` what overflew on the high part
5107 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord));
5108 __ Lsr(temp, low, temp);
5109 __ orr(o_h, o_h, ShifterOperand(temp));
5110 // If the shift is > 32 bits, override the high part
5111 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord));
5112 __ it(PL);
5113 __ Lsl(o_h, low, temp, PL);
5114 // Shift the low part
5115 __ Lsl(o_l, low, o_l);
5116 } else if (op->IsShr()) {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005117 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005118 // Shift the low part
5119 __ Lsr(o_l, low, o_h);
5120 // Shift the high part and `or` what underflew on the low part
5121 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
5122 __ Lsl(temp, high, temp);
5123 __ orr(o_l, o_l, ShifterOperand(temp));
5124 // If the shift is > 32 bits, override the low part
5125 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
5126 __ it(PL);
5127 __ Asr(o_l, high, temp, PL);
5128 // Shift the high part
5129 __ Asr(o_h, high, o_h);
5130 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00005131 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftDistance));
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005132 // same as Shr except we use `Lsr`s and not `Asr`s
5133 __ Lsr(o_l, low, o_h);
5134 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord));
5135 __ Lsl(temp, high, temp);
5136 __ orr(o_l, o_l, ShifterOperand(temp));
5137 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord));
5138 __ it(PL);
5139 __ Lsr(o_l, high, temp, PL);
5140 __ Lsr(o_h, high, o_h);
5141 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005142 } else {
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005143 // Register allocator doesn't create partial overlap.
5144 DCHECK_NE(o_l, high);
5145 DCHECK_NE(o_h, low);
5146 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain5b5b9312016-03-22 14:57:31 +00005147 uint32_t shift_value = cst & kMaxLongShiftDistance;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005148 if (shift_value > 32) {
5149 if (op->IsShl()) {
5150 __ Lsl(o_h, low, shift_value - 32);
5151 __ LoadImmediate(o_l, 0);
5152 } else if (op->IsShr()) {
5153 __ Asr(o_l, high, shift_value - 32);
5154 __ Asr(o_h, high, 31);
5155 } else {
5156 __ Lsr(o_l, high, shift_value - 32);
5157 __ LoadImmediate(o_h, 0);
5158 }
5159 } else if (shift_value == 32) {
5160 if (op->IsShl()) {
5161 __ mov(o_h, ShifterOperand(low));
5162 __ LoadImmediate(o_l, 0);
5163 } else if (op->IsShr()) {
5164 __ mov(o_l, ShifterOperand(high));
5165 __ Asr(o_h, high, 31);
5166 } else {
5167 __ mov(o_l, ShifterOperand(high));
5168 __ LoadImmediate(o_h, 0);
5169 }
Vladimir Markof9d741e2015-11-20 15:08:11 +00005170 } else if (shift_value == 1) {
5171 if (op->IsShl()) {
5172 __ Lsls(o_l, low, 1);
5173 __ adc(o_h, high, ShifterOperand(high));
5174 } else if (op->IsShr()) {
5175 __ Asrs(o_h, high, 1);
5176 __ Rrx(o_l, low);
5177 } else {
5178 __ Lsrs(o_h, high, 1);
5179 __ Rrx(o_l, low);
5180 }
5181 } else {
5182 DCHECK(2 <= shift_value && shift_value < 32) << shift_value;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005183 if (op->IsShl()) {
5184 __ Lsl(o_h, high, shift_value);
5185 __ orr(o_h, o_h, ShifterOperand(low, LSR, 32 - shift_value));
5186 __ Lsl(o_l, low, shift_value);
5187 } else if (op->IsShr()) {
5188 __ Lsr(o_l, low, shift_value);
5189 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
5190 __ Asr(o_h, high, shift_value);
5191 } else {
5192 __ Lsr(o_l, low, shift_value);
5193 __ orr(o_l, o_l, ShifterOperand(high, LSL, 32 - shift_value));
5194 __ Lsr(o_h, high, shift_value);
5195 }
5196 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005197 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00005198 break;
5199 }
5200 default:
5201 LOG(FATAL) << "Unexpected operation type " << type;
Vladimir Marko33ad10e2015-11-10 19:31:26 +00005202 UNREACHABLE();
Calin Juravle9aec02f2014-11-18 23:06:35 +00005203 }
5204}
5205
5206void LocationsBuilderARM::VisitShl(HShl* shl) {
5207 HandleShift(shl);
5208}
5209
5210void InstructionCodeGeneratorARM::VisitShl(HShl* shl) {
5211 HandleShift(shl);
5212}
5213
5214void LocationsBuilderARM::VisitShr(HShr* shr) {
5215 HandleShift(shr);
5216}
5217
5218void InstructionCodeGeneratorARM::VisitShr(HShr* shr) {
5219 HandleShift(shr);
5220}
5221
5222void LocationsBuilderARM::VisitUShr(HUShr* ushr) {
5223 HandleShift(ushr);
5224}
5225
5226void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) {
5227 HandleShift(ushr);
5228}
5229
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005230void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005231 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005232 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
David Brazdil6de19382016-01-08 17:37:10 +00005233 if (instruction->IsStringAlloc()) {
5234 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
5235 } else {
5236 InvokeRuntimeCallingConvention calling_convention;
5237 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00005238 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005239 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005240}
5241
5242void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005243 // Note: if heap poisoning is enabled, the entry point takes cares
5244 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00005245 if (instruction->IsStringAlloc()) {
5246 // String is allocated through StringFactory. Call NewEmptyString entry point.
5247 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07005248 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00005249 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
5250 __ LoadFromOffset(kLoadWord, LR, temp, code_offset.Int32Value());
5251 __ blx(LR);
5252 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
5253 } else {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01005254 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00005255 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00005256 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01005257}
5258
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005259void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) {
5260 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01005261 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005262 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005263 locations->SetOut(Location::RegisterLocation(R0));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005264 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5265 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005266}
5267
5268void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01005269 // Note: if heap poisoning is enabled, the entry point takes cares
5270 // of poisoning the reference.
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00005271 QuickEntrypointEnum entrypoint =
5272 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
5273 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00005274 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Nicolas Geoffrayd0958442017-01-30 14:57:16 +00005275 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01005276}
5277
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005278void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005279 LocationSummary* locations =
5280 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01005281 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
5282 if (location.IsStackSlot()) {
5283 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
5284 } else if (location.IsDoubleStackSlot()) {
5285 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005286 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01005287 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005288}
5289
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005290void InstructionCodeGeneratorARM::VisitParameterValue(
5291 HParameterValue* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01005292 // Nothing to do, the parameter is already at its location.
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005293}
5294
5295void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) {
5296 LocationSummary* locations =
5297 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5298 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
5299}
5300
5301void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
5302 // Nothing to do, the method is already at its location.
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01005303}
5304
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005305void LocationsBuilderARM::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005306 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005307 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005308 locations->SetInAt(0, Location::RequiresRegister());
5309 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005310}
5311
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005312void InstructionCodeGeneratorARM::VisitNot(HNot* not_) {
5313 LocationSummary* locations = not_->GetLocations();
5314 Location out = locations->Out();
5315 Location in = locations->InAt(0);
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00005316 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005317 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00005318 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005319 break;
5320
5321 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01005322 __ mvn(out.AsRegisterPairLow<Register>(),
5323 ShifterOperand(in.AsRegisterPairLow<Register>()));
5324 __ mvn(out.AsRegisterPairHigh<Register>(),
5325 ShifterOperand(in.AsRegisterPairHigh<Register>()));
Roland Levillain1cc5f2512014-10-22 18:06:21 +01005326 break;
5327
5328 default:
5329 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
5330 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01005331}
5332
David Brazdil66d126e2015-04-03 16:02:44 +01005333void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) {
5334 LocationSummary* locations =
5335 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
5336 locations->SetInAt(0, Location::RequiresRegister());
5337 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5338}
5339
5340void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01005341 LocationSummary* locations = bool_not->GetLocations();
5342 Location out = locations->Out();
5343 Location in = locations->InAt(0);
5344 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1));
5345}
5346
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005347void LocationsBuilderARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005348 LocationSummary* locations =
5349 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00005350 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00005351 case Primitive::kPrimBoolean:
5352 case Primitive::kPrimByte:
5353 case Primitive::kPrimShort:
5354 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08005355 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00005356 case Primitive::kPrimLong: {
5357 locations->SetInAt(0, Location::RequiresRegister());
5358 locations->SetInAt(1, Location::RequiresRegister());
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005359 // Output overlaps because it is written before doing the low comparison.
5360 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Calin Juravleddb7df22014-11-25 20:56:51 +00005361 break;
5362 }
5363 case Primitive::kPrimFloat:
5364 case Primitive::kPrimDouble: {
5365 locations->SetInAt(0, Location::RequiresFpuRegister());
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005366 locations->SetInAt(1, ArithmeticZeroOrFpuRegister(compare->InputAt(1)));
Calin Juravleddb7df22014-11-25 20:56:51 +00005367 locations->SetOut(Location::RequiresRegister());
5368 break;
5369 }
5370 default:
5371 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
5372 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005373}
5374
5375void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005376 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005377 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00005378 Location left = locations->InAt(0);
5379 Location right = locations->InAt(1);
5380
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005381 Label less, greater, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00005382 Label* final_label = codegen_->GetFinalLabel(compare, &done);
Calin Juravleddb7df22014-11-25 20:56:51 +00005383 Primitive::Type type = compare->InputAt(0)->GetType();
Vladimir Markod6e069b2016-01-18 11:11:01 +00005384 Condition less_cond;
Calin Juravleddb7df22014-11-25 20:56:51 +00005385 switch (type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00005386 case Primitive::kPrimBoolean:
5387 case Primitive::kPrimByte:
5388 case Primitive::kPrimShort:
5389 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08005390 case Primitive::kPrimInt: {
5391 __ LoadImmediate(out, 0);
5392 __ cmp(left.AsRegister<Register>(),
5393 ShifterOperand(right.AsRegister<Register>())); // Signed compare.
5394 less_cond = LT;
5395 break;
5396 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005397 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005398 __ cmp(left.AsRegisterPairHigh<Register>(),
5399 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare.
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005400 __ b(&less, LT);
5401 __ b(&greater, GT);
Roland Levillain4fa13f62015-07-06 18:11:54 +01005402 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags.
Calin Juravleddb7df22014-11-25 20:56:51 +00005403 __ LoadImmediate(out, 0);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005404 __ cmp(left.AsRegisterPairLow<Register>(),
5405 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005406 less_cond = LO;
Calin Juravleddb7df22014-11-25 20:56:51 +00005407 break;
5408 }
5409 case Primitive::kPrimFloat:
5410 case Primitive::kPrimDouble: {
5411 __ LoadImmediate(out, 0);
Donghui Bai426b49c2016-11-08 14:55:38 +08005412 GenerateVcmp(compare, codegen_);
Calin Juravleddb7df22014-11-25 20:56:51 +00005413 __ vmstat(); // transfer FP status register to ARM APSR.
Vladimir Markod6e069b2016-01-18 11:11:01 +00005414 less_cond = ARMFPCondition(kCondLT, compare->IsGtBias());
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005415 break;
5416 }
5417 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00005418 LOG(FATAL) << "Unexpected compare type " << type;
Vladimir Markod6e069b2016-01-18 11:11:01 +00005419 UNREACHABLE();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005420 }
Aart Bika19616e2016-02-01 18:57:58 -08005421
Anton Kirilov6f644202017-02-27 18:29:45 +00005422 __ b(final_label, EQ);
Vladimir Markod6e069b2016-01-18 11:11:01 +00005423 __ b(&less, less_cond);
Calin Juravleddb7df22014-11-25 20:56:51 +00005424
5425 __ Bind(&greater);
5426 __ LoadImmediate(out, 1);
Anton Kirilov6f644202017-02-27 18:29:45 +00005427 __ b(final_label);
Calin Juravleddb7df22014-11-25 20:56:51 +00005428
5429 __ Bind(&less);
5430 __ LoadImmediate(out, -1);
5431
Anton Kirilov6f644202017-02-27 18:29:45 +00005432 if (done.IsLinked()) {
5433 __ Bind(&done);
5434 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01005435}
5436
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005437void LocationsBuilderARM::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005438 LocationSummary* locations =
5439 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01005440 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01005441 locations->SetInAt(i, Location::Any());
5442 }
5443 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005444}
5445
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005446void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005447 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01005448}
5449
Roland Levillainc9285912015-12-18 10:38:42 +00005450void CodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) {
5451 // TODO (ported from quick): revisit ARM barrier kinds.
5452 DmbOptions flavor = DmbOptions::ISH; // Quiet C++ warnings.
Calin Juravle52c48962014-12-16 17:02:57 +00005453 switch (kind) {
5454 case MemBarrierKind::kAnyStore:
5455 case MemBarrierKind::kLoadAny:
5456 case MemBarrierKind::kAnyAny: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005457 flavor = DmbOptions::ISH;
Calin Juravle52c48962014-12-16 17:02:57 +00005458 break;
5459 }
5460 case MemBarrierKind::kStoreStore: {
Kenny Root1d8199d2015-06-02 11:01:10 -07005461 flavor = DmbOptions::ISHST;
Calin Juravle52c48962014-12-16 17:02:57 +00005462 break;
5463 }
5464 default:
5465 LOG(FATAL) << "Unexpected memory barrier " << kind;
5466 }
Kenny Root1d8199d2015-06-02 11:01:10 -07005467 __ dmb(flavor);
Calin Juravle52c48962014-12-16 17:02:57 +00005468}
5469
5470void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr,
5471 uint32_t offset,
5472 Register out_lo,
5473 Register out_hi) {
5474 if (offset != 0) {
Roland Levillain3b359c72015-11-17 19:35:12 +00005475 // Ensure `out_lo` is different from `addr`, so that loading
5476 // `offset` into `out_lo` does not clutter `addr`.
5477 DCHECK_NE(out_lo, addr);
Calin Juravle52c48962014-12-16 17:02:57 +00005478 __ LoadImmediate(out_lo, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005479 __ add(IP, addr, ShifterOperand(out_lo));
5480 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005481 }
5482 __ ldrexd(out_lo, out_hi, addr);
5483}
5484
5485void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr,
5486 uint32_t offset,
5487 Register value_lo,
5488 Register value_hi,
5489 Register temp1,
Calin Juravle77520bc2015-01-12 18:45:46 +00005490 Register temp2,
5491 HInstruction* instruction) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00005492 Label fail;
Calin Juravle52c48962014-12-16 17:02:57 +00005493 if (offset != 0) {
5494 __ LoadImmediate(temp1, offset);
Nicolas Geoffraybdcedd32015-01-09 08:48:29 +00005495 __ add(IP, addr, ShifterOperand(temp1));
5496 addr = IP;
Calin Juravle52c48962014-12-16 17:02:57 +00005497 }
5498 __ Bind(&fail);
5499 // We need a load followed by store. (The address used in a STREX instruction must
5500 // be the same as the address in the most recently executed LDREX instruction.)
5501 __ ldrexd(temp1, temp2, addr);
Calin Juravle77520bc2015-01-12 18:45:46 +00005502 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005503 __ strexd(temp1, value_lo, value_hi, addr);
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01005504 __ CompareAndBranchIfNonZero(temp1, &fail);
Calin Juravle52c48962014-12-16 17:02:57 +00005505}
5506
5507void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
5508 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5509
Nicolas Geoffray39468442014-09-02 15:17:15 +01005510 LocationSummary* locations =
5511 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005512 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005513
Calin Juravle52c48962014-12-16 17:02:57 +00005514 Primitive::Type field_type = field_info.GetFieldType();
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005515 if (Primitive::IsFloatingPointType(field_type)) {
5516 locations->SetInAt(1, Location::RequiresFpuRegister());
5517 } else {
5518 locations->SetInAt(1, Location::RequiresRegister());
5519 }
5520
Calin Juravle52c48962014-12-16 17:02:57 +00005521 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble;
Calin Juravle34166012014-12-19 17:22:29 +00005522 bool generate_volatile = field_info.IsVolatile()
5523 && is_wide
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005524 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain4d027112015-07-01 15:41:14 +01005525 bool needs_write_barrier =
5526 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005527 // Temporary registers for the write barrier.
Calin Juravle52c48962014-12-16 17:02:57 +00005528 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark.
Roland Levillain4d027112015-07-01 15:41:14 +01005529 if (needs_write_barrier) {
5530 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005531 locations->AddTemp(Location::RequiresRegister());
Calin Juravle34166012014-12-19 17:22:29 +00005532 } else if (generate_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005533 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005534 // - registers need to be consecutive
5535 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005536 // We don't test for ARM yet, and the assertion makes sure that we
5537 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005538 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5539
5540 locations->AddTemp(Location::RequiresRegister());
5541 locations->AddTemp(Location::RequiresRegister());
5542 if (field_type == Primitive::kPrimDouble) {
5543 // For doubles we need two more registers to copy the value.
5544 locations->AddTemp(Location::RegisterLocation(R2));
5545 locations->AddTemp(Location::RegisterLocation(R3));
5546 }
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01005547 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005548}
5549
Calin Juravle52c48962014-12-16 17:02:57 +00005550void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005551 const FieldInfo& field_info,
5552 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00005553 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
5554
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005555 LocationSummary* locations = instruction->GetLocations();
Calin Juravle52c48962014-12-16 17:02:57 +00005556 Register base = locations->InAt(0).AsRegister<Register>();
5557 Location value = locations->InAt(1);
5558
5559 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005560 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005561 Primitive::Type field_type = field_info.GetFieldType();
5562 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01005563 bool needs_write_barrier =
5564 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00005565
5566 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005567 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00005568 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005569
5570 switch (field_type) {
5571 case Primitive::kPrimBoolean:
5572 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00005573 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005574 break;
5575 }
5576
5577 case Primitive::kPrimShort:
5578 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00005579 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005580 break;
5581 }
5582
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005583 case Primitive::kPrimInt:
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005584 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01005585 if (kPoisonHeapReferences && needs_write_barrier) {
5586 // Note that in the case where `value` is a null reference,
5587 // we do not enter this block, as a null reference does not
5588 // need poisoning.
5589 DCHECK_EQ(field_type, Primitive::kPrimNot);
5590 Register temp = locations->GetTemp(0).AsRegister<Register>();
5591 __ Mov(temp, value.AsRegister<Register>());
5592 __ PoisonHeapReference(temp);
5593 __ StoreToOffset(kStoreWord, temp, base, offset);
5594 } else {
5595 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset);
5596 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005597 break;
5598 }
5599
5600 case Primitive::kPrimLong: {
Calin Juravle34166012014-12-19 17:22:29 +00005601 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005602 GenerateWideAtomicStore(base, offset,
5603 value.AsRegisterPairLow<Register>(),
5604 value.AsRegisterPairHigh<Register>(),
5605 locations->GetTemp(0).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005606 locations->GetTemp(1).AsRegister<Register>(),
5607 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005608 } else {
5609 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005610 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005611 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005612 break;
5613 }
5614
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005615 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00005616 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005617 break;
5618 }
5619
5620 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005621 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005622 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005623 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>();
5624 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>();
5625
5626 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg);
5627
5628 GenerateWideAtomicStore(base, offset,
5629 value_reg_lo,
5630 value_reg_hi,
5631 locations->GetTemp(2).AsRegister<Register>(),
Calin Juravle77520bc2015-01-12 18:45:46 +00005632 locations->GetTemp(3).AsRegister<Register>(),
5633 instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005634 } else {
5635 __ StoreDToOffset(value_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005636 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005637 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005638 break;
5639 }
5640
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005641 case Primitive::kPrimVoid:
5642 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005643 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005644 }
Calin Juravle52c48962014-12-16 17:02:57 +00005645
Calin Juravle77520bc2015-01-12 18:45:46 +00005646 // Longs and doubles are handled in the switch.
5647 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) {
5648 codegen_->MaybeRecordImplicitNullCheck(instruction);
5649 }
5650
5651 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
5652 Register temp = locations->GetTemp(0).AsRegister<Register>();
5653 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005654 codegen_->MarkGCCard(
5655 temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005656 }
5657
Calin Juravle52c48962014-12-16 17:02:57 +00005658 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005659 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005660 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005661}
5662
Calin Juravle52c48962014-12-16 17:02:57 +00005663void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5664 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain3b359c72015-11-17 19:35:12 +00005665
5666 bool object_field_get_with_read_barrier =
5667 kEmitCompilerReadBarrier && (field_info.GetFieldType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005668 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00005669 new (GetGraph()->GetArena()) LocationSummary(instruction,
5670 object_field_get_with_read_barrier ?
5671 LocationSummary::kCallOnSlowPath :
5672 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005673 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005674 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005675 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005676 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00005677
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005678 bool volatile_for_double = field_info.IsVolatile()
Calin Juravle34166012014-12-19 17:22:29 +00005679 && (field_info.GetFieldType() == Primitive::kPrimDouble)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005680 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Roland Levillain3b359c72015-11-17 19:35:12 +00005681 // The output overlaps in case of volatile long: we don't want the
5682 // code generated by GenerateWideAtomicLoad to overwrite the
5683 // object's location. Likewise, in the case of an object field get
5684 // with read barriers enabled, we do not want the load to overwrite
5685 // the object's location, as we need it to emit the read barrier.
5686 bool overlap = (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) ||
5687 object_field_get_with_read_barrier;
Nicolas Geoffrayacc0b8e2015-04-20 12:39:57 +01005688
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005689 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5690 locations->SetOut(Location::RequiresFpuRegister());
5691 } else {
5692 locations->SetOut(Location::RequiresRegister(),
5693 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap));
5694 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +00005695 if (volatile_for_double) {
Roland Levillainc9285912015-12-18 10:38:42 +00005696 // ARM encoding have some additional constraints for ldrexd/strexd:
Calin Juravle52c48962014-12-16 17:02:57 +00005697 // - registers need to be consecutive
5698 // - the first register should be even but not R14.
Roland Levillainc9285912015-12-18 10:38:42 +00005699 // We don't test for ARM yet, and the assertion makes sure that we
5700 // revisit this if we ever enable ARM encoding.
Calin Juravle52c48962014-12-16 17:02:57 +00005701 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet());
5702 locations->AddTemp(Location::RequiresRegister());
5703 locations->AddTemp(Location::RequiresRegister());
Roland Levillainc9285912015-12-18 10:38:42 +00005704 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5705 // We need a temporary register for the read barrier marking slow
5706 // path in CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01005707 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
5708 !Runtime::Current()->UseJitCompilation()) {
5709 // If link-time thunks for the Baker read barrier are enabled, for AOT
5710 // loads we need a temporary only if the offset is too big.
5711 if (field_info.GetFieldOffset().Uint32Value() >= kReferenceLoadMinFarOffset) {
5712 locations->AddTemp(Location::RequiresRegister());
5713 }
5714 // And we always need the reserved entrypoint register.
5715 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
5716 } else {
5717 locations->AddTemp(Location::RequiresRegister());
5718 }
Calin Juravle52c48962014-12-16 17:02:57 +00005719 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005720}
5721
Vladimir Marko37dd80d2016-08-01 17:41:45 +01005722Location LocationsBuilderARM::ArithmeticZeroOrFpuRegister(HInstruction* input) {
5723 DCHECK(input->GetType() == Primitive::kPrimDouble || input->GetType() == Primitive::kPrimFloat)
5724 << input->GetType();
5725 if ((input->IsFloatConstant() && (input->AsFloatConstant()->IsArithmeticZero())) ||
5726 (input->IsDoubleConstant() && (input->AsDoubleConstant()->IsArithmeticZero()))) {
5727 return Location::ConstantLocation(input->AsConstant());
5728 } else {
5729 return Location::RequiresFpuRegister();
5730 }
5731}
5732
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005733Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant,
5734 Opcode opcode) {
5735 DCHECK(!Primitive::IsFloatingPointType(constant->GetType()));
5736 if (constant->IsConstant() &&
5737 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) {
5738 return Location::ConstantLocation(constant->AsConstant());
5739 }
5740 return Location::RequiresRegister();
5741}
5742
5743bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst,
5744 Opcode opcode) {
5745 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst));
5746 if (Primitive::Is64BitType(input_cst->GetType())) {
Vladimir Marko59751a72016-08-05 14:37:27 +01005747 Opcode high_opcode = opcode;
5748 SetCc low_set_cc = kCcDontCare;
5749 switch (opcode) {
5750 case SUB:
5751 // Flip the operation to an ADD.
5752 value = -value;
5753 opcode = ADD;
5754 FALLTHROUGH_INTENDED;
5755 case ADD:
5756 if (Low32Bits(value) == 0u) {
5757 return CanEncodeConstantAsImmediate(High32Bits(value), opcode, kCcDontCare);
5758 }
5759 high_opcode = ADC;
5760 low_set_cc = kCcSet;
5761 break;
5762 default:
5763 break;
5764 }
5765 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode, low_set_cc) &&
5766 CanEncodeConstantAsImmediate(High32Bits(value), high_opcode, kCcDontCare);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005767 } else {
5768 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode);
5769 }
5770}
5771
Vladimir Marko59751a72016-08-05 14:37:27 +01005772bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value,
5773 Opcode opcode,
5774 SetCc set_cc) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005775 ShifterOperand so;
5776 ArmAssembler* assembler = codegen_->GetAssembler();
Vladimir Marko59751a72016-08-05 14:37:27 +01005777 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, set_cc, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005778 return true;
5779 }
5780 Opcode neg_opcode = kNoOperand;
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005781 uint32_t neg_value = 0;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005782 switch (opcode) {
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005783 case AND: neg_opcode = BIC; neg_value = ~value; break;
5784 case ORR: neg_opcode = ORN; neg_value = ~value; break;
5785 case ADD: neg_opcode = SUB; neg_value = -value; break;
5786 case ADC: neg_opcode = SBC; neg_value = ~value; break;
5787 case SUB: neg_opcode = ADD; neg_value = -value; break;
5788 case SBC: neg_opcode = ADC; neg_value = ~value; break;
5789 case MOV: neg_opcode = MVN; neg_value = ~value; break;
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005790 default:
5791 return false;
5792 }
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00005793
5794 if (assembler->ShifterOperandCanHold(kNoRegister,
5795 kNoRegister,
5796 neg_opcode,
5797 neg_value,
5798 set_cc,
5799 &so)) {
5800 return true;
5801 }
5802
5803 return opcode == AND && IsPowerOfTwo(value + 1);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01005804}
5805
Calin Juravle52c48962014-12-16 17:02:57 +00005806void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction,
5807 const FieldInfo& field_info) {
5808 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005809
Calin Juravle52c48962014-12-16 17:02:57 +00005810 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00005811 Location base_loc = locations->InAt(0);
5812 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00005813 Location out = locations->Out();
5814 bool is_volatile = field_info.IsVolatile();
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005815 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd();
Calin Juravle52c48962014-12-16 17:02:57 +00005816 Primitive::Type field_type = field_info.GetFieldType();
5817 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
5818
5819 switch (field_type) {
Roland Levillainc9285912015-12-18 10:38:42 +00005820 case Primitive::kPrimBoolean:
Calin Juravle52c48962014-12-16 17:02:57 +00005821 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005822 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005823
Roland Levillainc9285912015-12-18 10:38:42 +00005824 case Primitive::kPrimByte:
Calin Juravle52c48962014-12-16 17:02:57 +00005825 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005826 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005827
Roland Levillainc9285912015-12-18 10:38:42 +00005828 case Primitive::kPrimShort:
Calin Juravle52c48962014-12-16 17:02:57 +00005829 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005830 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005831
Roland Levillainc9285912015-12-18 10:38:42 +00005832 case Primitive::kPrimChar:
Calin Juravle52c48962014-12-16 17:02:57 +00005833 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005834 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005835
5836 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00005837 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005838 break;
Roland Levillainc9285912015-12-18 10:38:42 +00005839
5840 case Primitive::kPrimNot: {
5841 // /* HeapReference<Object> */ out = *(base + offset)
5842 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5843 Location temp_loc = locations->GetTemp(0);
5844 // Note that a potential implicit null check is handled in this
5845 // CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier call.
5846 codegen_->GenerateFieldLoadWithBakerReadBarrier(
5847 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
5848 if (is_volatile) {
5849 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5850 }
5851 } else {
5852 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset);
5853 codegen_->MaybeRecordImplicitNullCheck(instruction);
5854 if (is_volatile) {
5855 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5856 }
5857 // If read barriers are enabled, emit read barriers other than
5858 // Baker's using a slow path (and also unpoison the loaded
5859 // reference, if heap poisoning is enabled).
5860 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
5861 }
5862 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005863 }
5864
Roland Levillainc9285912015-12-18 10:38:42 +00005865 case Primitive::kPrimLong:
Calin Juravle34166012014-12-19 17:22:29 +00005866 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005867 GenerateWideAtomicLoad(base, offset,
5868 out.AsRegisterPairLow<Register>(),
5869 out.AsRegisterPairHigh<Register>());
5870 } else {
5871 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset);
5872 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005873 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005874
Roland Levillainc9285912015-12-18 10:38:42 +00005875 case Primitive::kPrimFloat:
Calin Juravle52c48962014-12-16 17:02:57 +00005876 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset);
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005877 break;
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005878
5879 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00005880 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>());
Calin Juravle34166012014-12-19 17:22:29 +00005881 if (is_volatile && !atomic_ldrd_strd) {
Calin Juravle52c48962014-12-16 17:02:57 +00005882 Register lo = locations->GetTemp(0).AsRegister<Register>();
5883 Register hi = locations->GetTemp(1).AsRegister<Register>();
5884 GenerateWideAtomicLoad(base, offset, lo, hi);
Calin Juravle77520bc2015-01-12 18:45:46 +00005885 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005886 __ vmovdrr(out_reg, lo, hi);
5887 } else {
5888 __ LoadDFromOffset(out_reg, base, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00005889 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005890 }
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00005891 break;
5892 }
5893
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005894 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00005895 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005896 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005897 }
Calin Juravle52c48962014-12-16 17:02:57 +00005898
Roland Levillainc9285912015-12-18 10:38:42 +00005899 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimDouble) {
5900 // Potential implicit null checks, in the case of reference or
5901 // double fields, are handled in the previous switch statement.
5902 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005903 codegen_->MaybeRecordImplicitNullCheck(instruction);
5904 }
5905
Calin Juravle52c48962014-12-16 17:02:57 +00005906 if (is_volatile) {
Roland Levillainc9285912015-12-18 10:38:42 +00005907 if (field_type == Primitive::kPrimNot) {
5908 // Memory barriers, in the case of references, are also handled
5909 // in the previous switch statement.
5910 } else {
5911 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
5912 }
Roland Levillain4d027112015-07-01 15:41:14 +01005913 }
Calin Juravle52c48962014-12-16 17:02:57 +00005914}
5915
5916void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5917 HandleFieldSet(instruction, instruction->GetFieldInfo());
5918}
5919
5920void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005921 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005922}
5923
5924void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5925 HandleFieldGet(instruction, instruction->GetFieldInfo());
5926}
5927
5928void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5929 HandleFieldGet(instruction, instruction->GetFieldInfo());
5930}
5931
5932void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5933 HandleFieldGet(instruction, instruction->GetFieldInfo());
5934}
5935
5936void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5937 HandleFieldGet(instruction, instruction->GetFieldInfo());
5938}
5939
5940void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5941 HandleFieldSet(instruction, instruction->GetFieldInfo());
5942}
5943
5944void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005945 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005946}
5947
Calin Juravlee460d1d2015-09-29 04:52:17 +01005948void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet(
5949 HUnresolvedInstanceFieldGet* instruction) {
5950 FieldAccessCallingConventionARM calling_convention;
5951 codegen_->CreateUnresolvedFieldLocationSummary(
5952 instruction, instruction->GetFieldType(), calling_convention);
5953}
5954
5955void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet(
5956 HUnresolvedInstanceFieldGet* instruction) {
5957 FieldAccessCallingConventionARM calling_convention;
5958 codegen_->GenerateUnresolvedFieldAccess(instruction,
5959 instruction->GetFieldType(),
5960 instruction->GetFieldIndex(),
5961 instruction->GetDexPc(),
5962 calling_convention);
5963}
5964
5965void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet(
5966 HUnresolvedInstanceFieldSet* instruction) {
5967 FieldAccessCallingConventionARM calling_convention;
5968 codegen_->CreateUnresolvedFieldLocationSummary(
5969 instruction, instruction->GetFieldType(), calling_convention);
5970}
5971
5972void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet(
5973 HUnresolvedInstanceFieldSet* instruction) {
5974 FieldAccessCallingConventionARM calling_convention;
5975 codegen_->GenerateUnresolvedFieldAccess(instruction,
5976 instruction->GetFieldType(),
5977 instruction->GetFieldIndex(),
5978 instruction->GetDexPc(),
5979 calling_convention);
5980}
5981
5982void LocationsBuilderARM::VisitUnresolvedStaticFieldGet(
5983 HUnresolvedStaticFieldGet* instruction) {
5984 FieldAccessCallingConventionARM calling_convention;
5985 codegen_->CreateUnresolvedFieldLocationSummary(
5986 instruction, instruction->GetFieldType(), calling_convention);
5987}
5988
5989void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet(
5990 HUnresolvedStaticFieldGet* instruction) {
5991 FieldAccessCallingConventionARM calling_convention;
5992 codegen_->GenerateUnresolvedFieldAccess(instruction,
5993 instruction->GetFieldType(),
5994 instruction->GetFieldIndex(),
5995 instruction->GetDexPc(),
5996 calling_convention);
5997}
5998
5999void LocationsBuilderARM::VisitUnresolvedStaticFieldSet(
6000 HUnresolvedStaticFieldSet* instruction) {
6001 FieldAccessCallingConventionARM calling_convention;
6002 codegen_->CreateUnresolvedFieldLocationSummary(
6003 instruction, instruction->GetFieldType(), calling_convention);
6004}
6005
6006void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet(
6007 HUnresolvedStaticFieldSet* instruction) {
6008 FieldAccessCallingConventionARM calling_convention;
6009 codegen_->GenerateUnresolvedFieldAccess(instruction,
6010 instruction->GetFieldType(),
6011 instruction->GetFieldIndex(),
6012 instruction->GetDexPc(),
6013 calling_convention);
6014}
6015
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006016void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006017 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
6018 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006019}
6020
Calin Juravle2ae48182016-03-16 14:05:09 +00006021void CodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) {
6022 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006023 return;
6024 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006025 Location obj = instruction->GetLocations()->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00006026
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006027 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00006028 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006029}
6030
Calin Juravle2ae48182016-03-16 14:05:09 +00006031void CodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) {
Artem Serovf4d6aee2016-07-11 10:41:45 +01006032 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00006033 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006034
6035 LocationSummary* locations = instruction->GetLocations();
6036 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006037
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006038 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01006039}
6040
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006041void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00006042 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00006043}
6044
Artem Serov6c916792016-07-11 14:02:34 +01006045static LoadOperandType GetLoadOperandType(Primitive::Type type) {
6046 switch (type) {
6047 case Primitive::kPrimNot:
6048 return kLoadWord;
6049 case Primitive::kPrimBoolean:
6050 return kLoadUnsignedByte;
6051 case Primitive::kPrimByte:
6052 return kLoadSignedByte;
6053 case Primitive::kPrimChar:
6054 return kLoadUnsignedHalfword;
6055 case Primitive::kPrimShort:
6056 return kLoadSignedHalfword;
6057 case Primitive::kPrimInt:
6058 return kLoadWord;
6059 case Primitive::kPrimLong:
6060 return kLoadWordPair;
6061 case Primitive::kPrimFloat:
6062 return kLoadSWord;
6063 case Primitive::kPrimDouble:
6064 return kLoadDWord;
6065 default:
6066 LOG(FATAL) << "Unreachable type " << type;
6067 UNREACHABLE();
6068 }
6069}
6070
6071static StoreOperandType GetStoreOperandType(Primitive::Type type) {
6072 switch (type) {
6073 case Primitive::kPrimNot:
6074 return kStoreWord;
6075 case Primitive::kPrimBoolean:
6076 case Primitive::kPrimByte:
6077 return kStoreByte;
6078 case Primitive::kPrimChar:
6079 case Primitive::kPrimShort:
6080 return kStoreHalfword;
6081 case Primitive::kPrimInt:
6082 return kStoreWord;
6083 case Primitive::kPrimLong:
6084 return kStoreWordPair;
6085 case Primitive::kPrimFloat:
6086 return kStoreSWord;
6087 case Primitive::kPrimDouble:
6088 return kStoreDWord;
6089 default:
6090 LOG(FATAL) << "Unreachable type " << type;
6091 UNREACHABLE();
6092 }
6093}
6094
6095void CodeGeneratorARM::LoadFromShiftedRegOffset(Primitive::Type type,
6096 Location out_loc,
6097 Register base,
6098 Register reg_offset,
6099 Condition cond) {
6100 uint32_t shift_count = Primitive::ComponentSizeShift(type);
6101 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
6102
6103 switch (type) {
6104 case Primitive::kPrimByte:
6105 __ ldrsb(out_loc.AsRegister<Register>(), mem_address, cond);
6106 break;
6107 case Primitive::kPrimBoolean:
6108 __ ldrb(out_loc.AsRegister<Register>(), mem_address, cond);
6109 break;
6110 case Primitive::kPrimShort:
6111 __ ldrsh(out_loc.AsRegister<Register>(), mem_address, cond);
6112 break;
6113 case Primitive::kPrimChar:
6114 __ ldrh(out_loc.AsRegister<Register>(), mem_address, cond);
6115 break;
6116 case Primitive::kPrimNot:
6117 case Primitive::kPrimInt:
6118 __ ldr(out_loc.AsRegister<Register>(), mem_address, cond);
6119 break;
6120 // T32 doesn't support LoadFromShiftedRegOffset mem address mode for these types.
6121 case Primitive::kPrimLong:
6122 case Primitive::kPrimFloat:
6123 case Primitive::kPrimDouble:
6124 default:
6125 LOG(FATAL) << "Unreachable type " << type;
6126 UNREACHABLE();
6127 }
6128}
6129
6130void CodeGeneratorARM::StoreToShiftedRegOffset(Primitive::Type type,
6131 Location loc,
6132 Register base,
6133 Register reg_offset,
6134 Condition cond) {
6135 uint32_t shift_count = Primitive::ComponentSizeShift(type);
6136 Address mem_address(base, reg_offset, Shift::LSL, shift_count);
6137
6138 switch (type) {
6139 case Primitive::kPrimByte:
6140 case Primitive::kPrimBoolean:
6141 __ strb(loc.AsRegister<Register>(), mem_address, cond);
6142 break;
6143 case Primitive::kPrimShort:
6144 case Primitive::kPrimChar:
6145 __ strh(loc.AsRegister<Register>(), mem_address, cond);
6146 break;
6147 case Primitive::kPrimNot:
6148 case Primitive::kPrimInt:
6149 __ str(loc.AsRegister<Register>(), mem_address, cond);
6150 break;
6151 // T32 doesn't support StoreToShiftedRegOffset mem address mode for these types.
6152 case Primitive::kPrimLong:
6153 case Primitive::kPrimFloat:
6154 case Primitive::kPrimDouble:
6155 default:
6156 LOG(FATAL) << "Unreachable type " << type;
6157 UNREACHABLE();
6158 }
6159}
6160
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006161void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain3b359c72015-11-17 19:35:12 +00006162 bool object_array_get_with_read_barrier =
6163 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006164 LocationSummary* locations =
Roland Levillain3b359c72015-11-17 19:35:12 +00006165 new (GetGraph()->GetArena()) LocationSummary(instruction,
6166 object_array_get_with_read_barrier ?
6167 LocationSummary::kCallOnSlowPath :
6168 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01006169 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006170 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006171 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006172 locations->SetInAt(0, Location::RequiresRegister());
6173 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006174 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6175 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
6176 } else {
Roland Levillain3b359c72015-11-17 19:35:12 +00006177 // The output overlaps in the case of an object array get with
6178 // read barriers enabled: we do not want the move to overwrite the
6179 // array's location, as we need it to emit the read barrier.
6180 locations->SetOut(
6181 Location::RequiresRegister(),
6182 object_array_get_with_read_barrier ? Location::kOutputOverlap : Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01006183 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006184 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
6185 // We need a temporary register for the read barrier marking slow
6186 // path in CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier.
6187 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
6188 !Runtime::Current()->UseJitCompilation() &&
6189 instruction->GetIndex()->IsConstant()) {
6190 // Array loads with constant index are treated as field loads.
6191 // If link-time thunks for the Baker read barrier are enabled, for AOT
6192 // constant index loads we need a temporary only if the offset is too big.
6193 uint32_t offset = CodeGenerator::GetArrayDataOffset(instruction);
6194 uint32_t index = instruction->GetIndex()->AsIntConstant()->GetValue();
6195 offset += index << Primitive::ComponentSizeShift(Primitive::kPrimNot);
6196 if (offset >= kReferenceLoadMinFarOffset) {
6197 locations->AddTemp(Location::RequiresRegister());
6198 }
6199 // And we always need the reserved entrypoint register.
6200 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6201 } else if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
6202 !Runtime::Current()->UseJitCompilation() &&
6203 !instruction->GetIndex()->IsConstant()) {
6204 // We need a non-scratch temporary for the array data pointer.
6205 locations->AddTemp(Location::RequiresRegister());
6206 // And we always need the reserved entrypoint register.
6207 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
6208 } else {
6209 locations->AddTemp(Location::RequiresRegister());
6210 }
6211 } else if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
6212 // Also need a temporary for String compression feature.
Roland Levillainc9285912015-12-18 10:38:42 +00006213 locations->AddTemp(Location::RequiresRegister());
6214 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006215}
6216
6217void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) {
6218 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006219 Location obj_loc = locations->InAt(0);
6220 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006221 Location index = locations->InAt(1);
Roland Levillainc9285912015-12-18 10:38:42 +00006222 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01006223 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Roland Levillainc9285912015-12-18 10:38:42 +00006224 Primitive::Type type = instruction->GetType();
jessicahandojo05765752016-09-09 19:01:32 -07006225 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
6226 instruction->IsStringCharAt();
Artem Serov328429f2016-07-06 16:23:04 +01006227 HInstruction* array_instr = instruction->GetArray();
6228 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Artem Serov6c916792016-07-11 14:02:34 +01006229
Roland Levillain4d027112015-07-01 15:41:14 +01006230 switch (type) {
Artem Serov6c916792016-07-11 14:02:34 +01006231 case Primitive::kPrimBoolean:
6232 case Primitive::kPrimByte:
6233 case Primitive::kPrimShort:
6234 case Primitive::kPrimChar:
Roland Levillainc9285912015-12-18 10:38:42 +00006235 case Primitive::kPrimInt: {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006236 Register length;
6237 if (maybe_compressed_char_at) {
6238 length = locations->GetTemp(0).AsRegister<Register>();
6239 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
6240 __ LoadFromOffset(kLoadWord, length, obj, count_offset);
6241 codegen_->MaybeRecordImplicitNullCheck(instruction);
6242 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006243 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01006244 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
jessicahandojo05765752016-09-09 19:01:32 -07006245 if (maybe_compressed_char_at) {
jessicahandojo05765752016-09-09 19:01:32 -07006246 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006247 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006248 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6249 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6250 "Expecting 0=compressed, 1=uncompressed");
6251 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07006252 __ LoadFromOffset(kLoadUnsignedByte,
6253 out_loc.AsRegister<Register>(),
6254 obj,
6255 data_offset + const_index);
Anton Kirilov6f644202017-02-27 18:29:45 +00006256 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07006257 __ Bind(&uncompressed_load);
6258 __ LoadFromOffset(GetLoadOperandType(Primitive::kPrimChar),
6259 out_loc.AsRegister<Register>(),
6260 obj,
6261 data_offset + (const_index << 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006262 if (done.IsLinked()) {
6263 __ Bind(&done);
6264 }
jessicahandojo05765752016-09-09 19:01:32 -07006265 } else {
6266 uint32_t full_offset = data_offset + (const_index << Primitive::ComponentSizeShift(type));
Artem Serov6c916792016-07-11 14:02:34 +01006267
jessicahandojo05765752016-09-09 19:01:32 -07006268 LoadOperandType load_type = GetLoadOperandType(type);
6269 __ LoadFromOffset(load_type, out_loc.AsRegister<Register>(), obj, full_offset);
6270 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006271 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006272 Register temp = IP;
6273
6274 if (has_intermediate_address) {
6275 // We do not need to compute the intermediate address from the array: the
6276 // input instruction has done it already. See the comment in
6277 // `TryExtractArrayAccessAddress()`.
6278 if (kIsDebugBuild) {
6279 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6280 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6281 }
6282 temp = obj;
6283 } else {
6284 __ add(temp, obj, ShifterOperand(data_offset));
6285 }
jessicahandojo05765752016-09-09 19:01:32 -07006286 if (maybe_compressed_char_at) {
6287 Label uncompressed_load, done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006288 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006289 __ Lsrs(length, length, 1u); // LSRS has a 16-bit encoding, TST (immediate) does not.
6290 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
6291 "Expecting 0=compressed, 1=uncompressed");
6292 __ b(&uncompressed_load, CS);
jessicahandojo05765752016-09-09 19:01:32 -07006293 __ ldrb(out_loc.AsRegister<Register>(),
6294 Address(temp, index.AsRegister<Register>(), Shift::LSL, 0));
Anton Kirilov6f644202017-02-27 18:29:45 +00006295 __ b(final_label);
jessicahandojo05765752016-09-09 19:01:32 -07006296 __ Bind(&uncompressed_load);
6297 __ ldrh(out_loc.AsRegister<Register>(),
6298 Address(temp, index.AsRegister<Register>(), Shift::LSL, 1));
Anton Kirilov6f644202017-02-27 18:29:45 +00006299 if (done.IsLinked()) {
6300 __ Bind(&done);
6301 }
jessicahandojo05765752016-09-09 19:01:32 -07006302 } else {
6303 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
6304 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006305 }
6306 break;
6307 }
6308
Roland Levillainc9285912015-12-18 10:38:42 +00006309 case Primitive::kPrimNot: {
Roland Levillain19c54192016-11-04 13:44:09 +00006310 // The read barrier instrumentation of object ArrayGet
6311 // instructions does not support the HIntermediateAddress
6312 // instruction.
6313 DCHECK(!(has_intermediate_address && kEmitCompilerReadBarrier));
6314
Roland Levillainc9285912015-12-18 10:38:42 +00006315 static_assert(
6316 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6317 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillainc9285912015-12-18 10:38:42 +00006318 // /* HeapReference<Object> */ out =
6319 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6320 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
6321 Location temp = locations->GetTemp(0);
6322 // Note that a potential implicit null check is handled in this
6323 // CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier call.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01006324 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
6325 if (index.IsConstant()) {
6326 // Array load with a constant index can be treated as a field load.
6327 data_offset += helpers::Int32ConstantFrom(index) << Primitive::ComponentSizeShift(type);
6328 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6329 out_loc,
6330 obj,
6331 data_offset,
6332 locations->GetTemp(0),
6333 /* needs_null_check */ false);
6334 } else {
6335 codegen_->GenerateArrayLoadWithBakerReadBarrier(
6336 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ false);
6337 }
Roland Levillainc9285912015-12-18 10:38:42 +00006338 } else {
6339 Register out = out_loc.AsRegister<Register>();
6340 if (index.IsConstant()) {
6341 size_t offset =
6342 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6343 __ LoadFromOffset(kLoadWord, out, obj, offset);
6344 codegen_->MaybeRecordImplicitNullCheck(instruction);
6345 // If read barriers are enabled, emit read barriers other than
6346 // Baker's using a slow path (and also unpoison the loaded
6347 // reference, if heap poisoning is enabled).
6348 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
6349 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006350 Register temp = IP;
6351
6352 if (has_intermediate_address) {
6353 // We do not need to compute the intermediate address from the array: the
6354 // input instruction has done it already. See the comment in
6355 // `TryExtractArrayAccessAddress()`.
6356 if (kIsDebugBuild) {
6357 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6358 DCHECK_EQ(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64(), data_offset);
6359 }
6360 temp = obj;
6361 } else {
6362 __ add(temp, obj, ShifterOperand(data_offset));
6363 }
6364 codegen_->LoadFromShiftedRegOffset(type, out_loc, temp, index.AsRegister<Register>());
Artem Serov6c916792016-07-11 14:02:34 +01006365
Roland Levillainc9285912015-12-18 10:38:42 +00006366 codegen_->MaybeRecordImplicitNullCheck(instruction);
6367 // If read barriers are enabled, emit read barriers other than
6368 // Baker's using a slow path (and also unpoison the loaded
6369 // reference, if heap poisoning is enabled).
6370 codegen_->MaybeGenerateReadBarrierSlow(
6371 instruction, out_loc, out_loc, obj_loc, data_offset, index);
6372 }
6373 }
6374 break;
6375 }
6376
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006377 case Primitive::kPrimLong: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006378 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006379 size_t offset =
6380 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006381 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), obj, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006382 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006383 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00006384 __ LoadFromOffset(kLoadWordPair, out_loc.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006385 }
6386 break;
6387 }
6388
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006389 case Primitive::kPrimFloat: {
Roland Levillainc9285912015-12-18 10:38:42 +00006390 SRegister out = out_loc.AsFpuRegister<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006391 if (index.IsConstant()) {
6392 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006393 __ LoadSFromOffset(out, obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006394 } else {
6395 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Roland Levillainc9285912015-12-18 10:38:42 +00006396 __ LoadSFromOffset(out, IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006397 }
6398 break;
6399 }
6400
6401 case Primitive::kPrimDouble: {
Roland Levillainc9285912015-12-18 10:38:42 +00006402 SRegister out = out_loc.AsFpuRegisterPairLow<SRegister>();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006403 if (index.IsConstant()) {
6404 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillainc9285912015-12-18 10:38:42 +00006405 __ LoadDFromOffset(FromLowSToD(out), obj, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006406 } else {
6407 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Roland Levillainc9285912015-12-18 10:38:42 +00006408 __ LoadDFromOffset(FromLowSToD(out), IP, data_offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006409 }
6410 break;
6411 }
6412
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006413 case Primitive::kPrimVoid:
Roland Levillain4d027112015-07-01 15:41:14 +01006414 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006415 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006416 }
Roland Levillain4d027112015-07-01 15:41:14 +01006417
6418 if (type == Primitive::kPrimNot) {
Roland Levillainc9285912015-12-18 10:38:42 +00006419 // Potential implicit null checks, in the case of reference
6420 // arrays, are handled in the previous switch statement.
jessicahandojo05765752016-09-09 19:01:32 -07006421 } else if (!maybe_compressed_char_at) {
Roland Levillainc9285912015-12-18 10:38:42 +00006422 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain4d027112015-07-01 15:41:14 +01006423 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006424}
6425
6426void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006427 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006428
6429 bool needs_write_barrier =
6430 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain3b359c72015-11-17 19:35:12 +00006431 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006432
Nicolas Geoffray39468442014-09-02 15:17:15 +01006433 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006434 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01006435 may_need_runtime_call_for_type_check ?
Roland Levillain3b359c72015-11-17 19:35:12 +00006436 LocationSummary::kCallOnSlowPath :
6437 LocationSummary::kNoCall);
6438
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006439 locations->SetInAt(0, Location::RequiresRegister());
6440 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
6441 if (Primitive::IsFloatingPointType(value_type)) {
6442 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006443 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006444 locations->SetInAt(2, Location::RequiresRegister());
6445 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006446 if (needs_write_barrier) {
6447 // Temporary registers for the write barrier.
6448 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00006449 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006450 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006451}
6452
6453void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) {
6454 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00006455 Location array_loc = locations->InAt(0);
6456 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006457 Location index = locations->InAt(1);
Nicolas Geoffray39468442014-09-02 15:17:15 +01006458 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain3b359c72015-11-17 19:35:12 +00006459 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006460 bool needs_write_barrier =
6461 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Artem Serov6c916792016-07-11 14:02:34 +01006462 uint32_t data_offset =
6463 mirror::Array::DataOffset(Primitive::ComponentSize(value_type)).Uint32Value();
6464 Location value_loc = locations->InAt(2);
Artem Serov328429f2016-07-06 16:23:04 +01006465 HInstruction* array_instr = instruction->GetArray();
6466 bool has_intermediate_address = array_instr->IsIntermediateAddress();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006467
6468 switch (value_type) {
6469 case Primitive::kPrimBoolean:
Artem Serov6c916792016-07-11 14:02:34 +01006470 case Primitive::kPrimByte:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006471 case Primitive::kPrimShort:
Artem Serov6c916792016-07-11 14:02:34 +01006472 case Primitive::kPrimChar:
6473 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006474 if (index.IsConstant()) {
Artem Serov6c916792016-07-11 14:02:34 +01006475 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
6476 uint32_t full_offset =
6477 data_offset + (const_index << Primitive::ComponentSizeShift(value_type));
6478 StoreOperandType store_type = GetStoreOperandType(value_type);
6479 __ StoreToOffset(store_type, value_loc.AsRegister<Register>(), array, full_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006480 } else {
Artem Serov328429f2016-07-06 16:23:04 +01006481 Register temp = IP;
6482
6483 if (has_intermediate_address) {
6484 // We do not need to compute the intermediate address from the array: the
6485 // input instruction has done it already. See the comment in
6486 // `TryExtractArrayAccessAddress()`.
6487 if (kIsDebugBuild) {
6488 HIntermediateAddress* tmp = array_instr->AsIntermediateAddress();
6489 DCHECK(tmp->GetOffset()->AsIntConstant()->GetValueAsUint64() == data_offset);
6490 }
6491 temp = array;
6492 } else {
6493 __ add(temp, array, ShifterOperand(data_offset));
6494 }
Artem Serov6c916792016-07-11 14:02:34 +01006495 codegen_->StoreToShiftedRegOffset(value_type,
6496 value_loc,
Artem Serov328429f2016-07-06 16:23:04 +01006497 temp,
Artem Serov6c916792016-07-11 14:02:34 +01006498 index.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006499 }
6500 break;
6501 }
6502
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006503 case Primitive::kPrimNot: {
Roland Levillain3b359c72015-11-17 19:35:12 +00006504 Register value = value_loc.AsRegister<Register>();
Artem Serov328429f2016-07-06 16:23:04 +01006505 // TryExtractArrayAccessAddress optimization is never applied for non-primitive ArraySet.
6506 // See the comment in instruction_simplifier_shared.cc.
6507 DCHECK(!has_intermediate_address);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006508
6509 if (instruction->InputAt(2)->IsNullConstant()) {
6510 // Just setting null.
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006511 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006512 size_t offset =
6513 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Artem Serov6c916792016-07-11 14:02:34 +01006514 __ StoreToOffset(kStoreWord, value, array, offset);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006515 } else {
6516 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006517 __ add(IP, array, ShifterOperand(data_offset));
6518 codegen_->StoreToShiftedRegOffset(value_type,
6519 value_loc,
6520 IP,
6521 index.AsRegister<Register>());
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006522 }
Roland Levillain1407ee72016-01-08 15:56:19 +00006523 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain3b359c72015-11-17 19:35:12 +00006524 DCHECK(!needs_write_barrier);
6525 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006526 break;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00006527 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006528
6529 DCHECK(needs_write_barrier);
Roland Levillain16d9f942016-08-25 17:27:56 +01006530 Location temp1_loc = locations->GetTemp(0);
6531 Register temp1 = temp1_loc.AsRegister<Register>();
6532 Location temp2_loc = locations->GetTemp(1);
6533 Register temp2 = temp2_loc.AsRegister<Register>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006534 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6535 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6536 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6537 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00006538 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01006539 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006540
Roland Levillain3b359c72015-11-17 19:35:12 +00006541 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006542 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction);
6543 codegen_->AddSlowPath(slow_path);
6544 if (instruction->GetValueCanBeNull()) {
6545 Label non_zero;
6546 __ CompareAndBranchIfNonZero(value, &non_zero);
6547 if (index.IsConstant()) {
6548 size_t offset =
6549 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6550 __ StoreToOffset(kStoreWord, value, array, offset);
6551 } else {
6552 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006553 __ add(IP, array, ShifterOperand(data_offset));
6554 codegen_->StoreToShiftedRegOffset(value_type,
6555 value_loc,
6556 IP,
6557 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006558 }
6559 codegen_->MaybeRecordImplicitNullCheck(instruction);
Anton Kirilov6f644202017-02-27 18:29:45 +00006560 __ b(final_label);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006561 __ Bind(&non_zero);
6562 }
6563
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006564 // Note that when read barriers are enabled, the type checks
6565 // are performed without read barriers. This is fine, even in
6566 // the case where a class object is in the from-space after
6567 // the flip, as a comparison involving such a type would not
6568 // produce a false positive; it may of course produce a false
6569 // negative, in which case we would take the ArraySet slow
6570 // path.
Roland Levillain16d9f942016-08-25 17:27:56 +01006571
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006572 // /* HeapReference<Class> */ temp1 = array->klass_
6573 __ LoadFromOffset(kLoadWord, temp1, array, class_offset);
6574 codegen_->MaybeRecordImplicitNullCheck(instruction);
6575 __ MaybeUnpoisonHeapReference(temp1);
Roland Levillain16d9f942016-08-25 17:27:56 +01006576
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006577 // /* HeapReference<Class> */ temp1 = temp1->component_type_
6578 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
6579 // /* HeapReference<Class> */ temp2 = value->klass_
6580 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
6581 // If heap poisoning is enabled, no need to unpoison `temp1`
6582 // nor `temp2`, as we are comparing two poisoned references.
6583 __ cmp(temp1, ShifterOperand(temp2));
Roland Levillain16d9f942016-08-25 17:27:56 +01006584
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006585 if (instruction->StaticTypeOfArrayIsObjectArray()) {
6586 Label do_put;
6587 __ b(&do_put, EQ);
6588 // If heap poisoning is enabled, the `temp1` reference has
6589 // not been unpoisoned yet; unpoison it now.
Roland Levillain3b359c72015-11-17 19:35:12 +00006590 __ MaybeUnpoisonHeapReference(temp1);
6591
Roland Levillain9d6e1f82016-09-05 15:57:33 +01006592 // /* HeapReference<Class> */ temp1 = temp1->super_class_
6593 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
6594 // If heap poisoning is enabled, no need to unpoison
6595 // `temp1`, as we are comparing against null below.
6596 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel());
6597 __ Bind(&do_put);
6598 } else {
6599 __ b(slow_path->GetEntryLabel(), NE);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006600 }
6601 }
6602
Artem Serov6c916792016-07-11 14:02:34 +01006603 Register source = value;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006604 if (kPoisonHeapReferences) {
6605 // Note that in the case where `value` is a null reference,
6606 // we do not enter this block, as a null reference does not
6607 // need poisoning.
6608 DCHECK_EQ(value_type, Primitive::kPrimNot);
6609 __ Mov(temp1, value);
6610 __ PoisonHeapReference(temp1);
6611 source = temp1;
6612 }
6613
6614 if (index.IsConstant()) {
6615 size_t offset =
6616 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
6617 __ StoreToOffset(kStoreWord, source, array, offset);
6618 } else {
6619 DCHECK(index.IsRegister()) << index;
Artem Serov6c916792016-07-11 14:02:34 +01006620
6621 __ add(IP, array, ShifterOperand(data_offset));
6622 codegen_->StoreToShiftedRegOffset(value_type,
6623 Location::RegisterLocation(source),
6624 IP,
6625 index.AsRegister<Register>());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006626 }
6627
Roland Levillain3b359c72015-11-17 19:35:12 +00006628 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006629 codegen_->MaybeRecordImplicitNullCheck(instruction);
6630 }
6631
6632 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull());
6633
6634 if (done.IsLinked()) {
6635 __ Bind(&done);
6636 }
6637
6638 if (slow_path != nullptr) {
6639 __ Bind(slow_path->GetExitLabel());
6640 }
6641
6642 break;
6643 }
6644
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006645 case Primitive::kPrimLong: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006646 Location value = locations->InAt(2);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006647 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006648 size_t offset =
6649 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006650 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006651 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006652 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01006653 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006654 }
6655 break;
6656 }
6657
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006658 case Primitive::kPrimFloat: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006659 Location value = locations->InAt(2);
6660 DCHECK(value.IsFpuRegister());
6661 if (index.IsConstant()) {
6662 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006663 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006664 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006665 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006666 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset);
6667 }
6668 break;
6669 }
6670
6671 case Primitive::kPrimDouble: {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006672 Location value = locations->InAt(2);
6673 DCHECK(value.IsFpuRegisterPair());
6674 if (index.IsConstant()) {
6675 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006676 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006677 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01006678 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8));
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006679 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset);
6680 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006681
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006682 break;
6683 }
6684
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006685 case Primitive::kPrimVoid:
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006686 LOG(FATAL) << "Unreachable type " << value_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07006687 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006688 }
Calin Juravle77520bc2015-01-12 18:45:46 +00006689
Roland Levillain80e67092016-01-08 16:04:55 +00006690 // Objects are handled in the switch.
6691 if (value_type != Primitive::kPrimNot) {
Calin Juravle77520bc2015-01-12 18:45:46 +00006692 codegen_->MaybeRecordImplicitNullCheck(instruction);
6693 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006694}
6695
6696void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01006697 LocationSummary* locations =
6698 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01006699 locations->SetInAt(0, Location::RequiresRegister());
6700 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006701}
6702
6703void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) {
6704 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01006705 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00006706 Register obj = locations->InAt(0).AsRegister<Register>();
6707 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006708 __ LoadFromOffset(kLoadWord, out, obj, offset);
Calin Juravle77520bc2015-01-12 18:45:46 +00006709 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo05765752016-09-09 19:01:32 -07006710 // Mask out compression flag from String's array length.
6711 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01006712 __ Lsr(out, out, 1u);
jessicahandojo05765752016-09-09 19:01:32 -07006713 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006714}
6715
Artem Serov328429f2016-07-06 16:23:04 +01006716void LocationsBuilderARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
Artem Serov328429f2016-07-06 16:23:04 +01006717 LocationSummary* locations =
6718 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6719
6720 locations->SetInAt(0, Location::RequiresRegister());
6721 locations->SetInAt(1, Location::RegisterOrConstant(instruction->GetOffset()));
6722 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
6723}
6724
6725void InstructionCodeGeneratorARM::VisitIntermediateAddress(HIntermediateAddress* instruction) {
6726 LocationSummary* locations = instruction->GetLocations();
6727 Location out = locations->Out();
6728 Location first = locations->InAt(0);
6729 Location second = locations->InAt(1);
6730
Artem Serov328429f2016-07-06 16:23:04 +01006731 if (second.IsRegister()) {
6732 __ add(out.AsRegister<Register>(),
6733 first.AsRegister<Register>(),
6734 ShifterOperand(second.AsRegister<Register>()));
6735 } else {
6736 __ AddConstant(out.AsRegister<Register>(),
6737 first.AsRegister<Register>(),
6738 second.GetConstant()->AsIntConstant()->GetValue());
6739 }
6740}
6741
Artem Serove1811ed2017-04-27 16:50:47 +01006742void LocationsBuilderARM::VisitIntermediateAddressIndex(HIntermediateAddressIndex* instruction) {
6743 LOG(FATAL) << "Unreachable " << instruction->GetId();
6744}
6745
6746void InstructionCodeGeneratorARM::VisitIntermediateAddressIndex(
6747 HIntermediateAddressIndex* instruction) {
6748 LOG(FATAL) << "Unreachable " << instruction->GetId();
6749}
6750
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006751void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006752 RegisterSet caller_saves = RegisterSet::Empty();
6753 InvokeRuntimeCallingConvention calling_convention;
6754 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6755 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
6756 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Artem Serov2dd053d2017-03-08 14:54:06 +00006757
6758 HInstruction* index = instruction->InputAt(0);
6759 HInstruction* length = instruction->InputAt(1);
6760 // If both index and length are constants we can statically check the bounds. But if at least one
6761 // of them is not encodable ArmEncodableConstantOrRegister will create
6762 // Location::RequiresRegister() which is not desired to happen. Instead we create constant
6763 // locations.
6764 bool both_const = index->IsConstant() && length->IsConstant();
6765 locations->SetInAt(0, both_const
6766 ? Location::ConstantLocation(index->AsConstant())
6767 : ArmEncodableConstantOrRegister(index, CMP));
6768 locations->SetInAt(1, both_const
6769 ? Location::ConstantLocation(length->AsConstant())
6770 : ArmEncodableConstantOrRegister(length, CMP));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006771}
6772
6773void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) {
6774 LocationSummary* locations = instruction->GetLocations();
Artem Serov2dd053d2017-03-08 14:54:06 +00006775 Location index_loc = locations->InAt(0);
6776 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006777
Artem Serov2dd053d2017-03-08 14:54:06 +00006778 if (length_loc.IsConstant()) {
6779 int32_t length = helpers::Int32ConstantFrom(length_loc);
6780 if (index_loc.IsConstant()) {
6781 // BCE will remove the bounds check if we are guaranteed to pass.
6782 int32_t index = helpers::Int32ConstantFrom(index_loc);
6783 if (index < 0 || index >= length) {
6784 SlowPathCodeARM* slow_path =
6785 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6786 codegen_->AddSlowPath(slow_path);
6787 __ b(slow_path->GetEntryLabel());
6788 } else {
6789 // Some optimization after BCE may have generated this, and we should not
6790 // generate a bounds check if it is a valid range.
6791 }
6792 return;
6793 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006794
Artem Serov2dd053d2017-03-08 14:54:06 +00006795 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6796 __ cmp(index_loc.AsRegister<Register>(), ShifterOperand(length));
6797 codegen_->AddSlowPath(slow_path);
6798 __ b(slow_path->GetEntryLabel(), HS);
6799 } else {
6800 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction);
6801 if (index_loc.IsConstant()) {
6802 int32_t index = helpers::Int32ConstantFrom(index_loc);
6803 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index));
6804 } else {
6805 __ cmp(length_loc.AsRegister<Register>(), ShifterOperand(index_loc.AsRegister<Register>()));
6806 }
6807 codegen_->AddSlowPath(slow_path);
6808 __ b(slow_path->GetEntryLabel(), LS);
6809 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006810}
6811
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006812void CodeGeneratorARM::MarkGCCard(Register temp,
6813 Register card,
6814 Register object,
6815 Register value,
6816 bool can_be_null) {
Vladimir Markocf93a5c2015-06-16 11:33:24 +00006817 Label is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006818 if (can_be_null) {
6819 __ CompareAndBranchIfZero(value, &is_null);
6820 }
Andreas Gampe542451c2016-07-26 09:02:02 -07006821 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006822 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift);
6823 __ strb(card, Address(card, temp));
Nicolas Geoffray07276db2015-05-18 14:22:09 +01006824 if (can_be_null) {
6825 __ Bind(&is_null);
6826 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01006827}
6828
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006829void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006830 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006831}
6832
6833void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006834 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
6835}
6836
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006837void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01006838 LocationSummary* locations =
6839 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01006840 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006841}
6842
6843void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006844 HBasicBlock* block = instruction->GetBlock();
6845 if (block->GetLoopInformation() != nullptr) {
6846 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
6847 // The back edge will generate the suspend check.
6848 return;
6849 }
6850 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
6851 // The goto will generate the suspend check.
6852 return;
6853 }
6854 GenerateSuspendCheck(instruction, nullptr);
6855}
6856
6857void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction,
6858 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006859 SuspendCheckSlowPathARM* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01006860 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath());
6861 if (slow_path == nullptr) {
6862 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor);
6863 instruction->SetSlowPath(slow_path);
6864 codegen_->AddSlowPath(slow_path);
6865 if (successor != nullptr) {
6866 DCHECK(successor->IsLoopHeader());
6867 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
6868 }
6869 } else {
6870 DCHECK_EQ(slow_path->GetSuccessor(), successor);
6871 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006872
Nicolas Geoffray44b819e2014-11-06 12:00:54 +00006873 __ LoadFromOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07006874 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmPointerSize>().Int32Value());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006875 if (successor == nullptr) {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006876 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel());
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006877 __ Bind(slow_path->GetReturnLabel());
6878 } else {
Nicolas Geoffray2bcb4312015-07-01 12:22:56 +01006879 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01006880 __ b(slow_path->GetEntryLabel());
6881 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00006882}
6883
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006884ArmAssembler* ParallelMoveResolverARM::GetAssembler() const {
6885 return codegen_->GetAssembler();
6886}
6887
6888void ParallelMoveResolverARM::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01006889 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006890 Location source = move->GetSource();
6891 Location destination = move->GetDestination();
6892
6893 if (source.IsRegister()) {
6894 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006895 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006896 } else if (destination.IsFpuRegister()) {
6897 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006898 } else {
6899 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006900 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006901 SP, destination.GetStackIndex());
6902 }
6903 } else if (source.IsStackSlot()) {
6904 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006905 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(),
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006906 SP, source.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006907 } else if (destination.IsFpuRegister()) {
6908 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01006909 } else {
6910 DCHECK(destination.IsStackSlot());
6911 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex());
6912 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6913 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006914 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006915 if (destination.IsRegister()) {
6916 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>());
6917 } else if (destination.IsFpuRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006918 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>());
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01006919 } else {
6920 DCHECK(destination.IsStackSlot());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006921 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex());
6922 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006923 } else if (source.IsDoubleStackSlot()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006924 if (destination.IsDoubleStackSlot()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00006925 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex());
6926 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006927 } else if (destination.IsRegisterPair()) {
6928 DCHECK(ExpectedPairLayout(destination));
6929 __ LoadFromOffset(
6930 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex());
6931 } else {
6932 DCHECK(destination.IsFpuRegisterPair()) << destination;
6933 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6934 SP,
6935 source.GetStackIndex());
6936 }
6937 } else if (source.IsRegisterPair()) {
6938 if (destination.IsRegisterPair()) {
6939 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
6940 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00006941 } else if (destination.IsFpuRegisterPair()) {
6942 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6943 source.AsRegisterPairLow<Register>(),
6944 source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006945 } else {
6946 DCHECK(destination.IsDoubleStackSlot()) << destination;
6947 DCHECK(ExpectedPairLayout(source));
6948 __ StoreToOffset(
6949 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex());
6950 }
6951 } else if (source.IsFpuRegisterPair()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00006952 if (destination.IsRegisterPair()) {
6953 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
6954 destination.AsRegisterPairHigh<Register>(),
6955 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6956 } else if (destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006957 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()),
6958 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()));
6959 } else {
6960 DCHECK(destination.IsDoubleStackSlot()) << destination;
6961 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()),
6962 SP,
6963 destination.GetStackIndex());
6964 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006965 } else {
6966 DCHECK(source.IsConstant()) << source;
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00006967 HConstant* constant = source.GetConstant();
6968 if (constant->IsIntConstant() || constant->IsNullConstant()) {
6969 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00006970 if (destination.IsRegister()) {
6971 __ LoadImmediate(destination.AsRegister<Register>(), value);
6972 } else {
6973 DCHECK(destination.IsStackSlot());
6974 __ LoadImmediate(IP, value);
6975 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6976 }
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006977 } else if (constant->IsLongConstant()) {
6978 int64_t value = constant->AsLongConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006979 if (destination.IsRegisterPair()) {
6980 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value));
6981 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value));
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006982 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006983 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006984 __ LoadImmediate(IP, Low32Bits(value));
6985 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6986 __ LoadImmediate(IP, High32Bits(value));
6987 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
6988 }
6989 } else if (constant->IsDoubleConstant()) {
6990 double value = constant->AsDoubleConstant()->GetValue();
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006991 if (destination.IsFpuRegisterPair()) {
6992 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006993 } else {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00006994 DCHECK(destination.IsDoubleStackSlot()) << destination;
6995 uint64_t int_value = bit_cast<uint64_t, double>(value);
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00006996 __ LoadImmediate(IP, Low32Bits(int_value));
6997 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
6998 __ LoadImmediate(IP, High32Bits(int_value));
6999 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize));
7000 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007001 } else {
Nicolas Geoffray6c2dff82015-01-21 14:56:54 +00007002 DCHECK(constant->IsFloatConstant()) << constant->DebugName();
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007003 float value = constant->AsFloatConstant()->GetValue();
7004 if (destination.IsFpuRegister()) {
7005 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value);
7006 } else {
7007 DCHECK(destination.IsStackSlot());
7008 __ LoadImmediate(IP, bit_cast<int32_t, float>(value));
7009 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex());
7010 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01007011 }
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007012 }
7013}
7014
7015void ParallelMoveResolverARM::Exchange(Register reg, int mem) {
7016 __ Mov(IP, reg);
7017 __ LoadFromOffset(kLoadWord, reg, SP, mem);
7018 __ StoreToOffset(kStoreWord, IP, SP, mem);
7019}
7020
7021void ParallelMoveResolverARM::Exchange(int mem1, int mem2) {
7022 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters());
7023 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0;
7024 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()),
7025 SP, mem1 + stack_offset);
7026 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset);
7027 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()),
7028 SP, mem2 + stack_offset);
7029 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset);
7030}
7031
7032void ParallelMoveResolverARM::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01007033 MoveOperands* move = moves_[index];
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007034 Location source = move->GetSource();
7035 Location destination = move->GetDestination();
7036
7037 if (source.IsRegister() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007038 DCHECK_NE(source.AsRegister<Register>(), IP);
7039 DCHECK_NE(destination.AsRegister<Register>(), IP);
7040 __ Mov(IP, source.AsRegister<Register>());
7041 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>());
7042 __ Mov(destination.AsRegister<Register>(), IP);
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007043 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007044 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007045 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007046 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007047 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
7048 Exchange(source.GetStackIndex(), destination.GetStackIndex());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007049 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00007050 __ vmovrs(IP, source.AsFpuRegister<SRegister>());
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007051 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>());
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00007052 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007053 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) {
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007054 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007055 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>());
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007056 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007057 __ vmovrrd(destination.AsRegisterPairLow<Register>(),
7058 destination.AsRegisterPairHigh<Register>(),
7059 DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007060 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007061 Register low_reg = source.IsRegisterPair()
7062 ? source.AsRegisterPairLow<Register>()
7063 : destination.AsRegisterPairLow<Register>();
7064 int mem = source.IsRegisterPair()
7065 ? destination.GetStackIndex()
7066 : source.GetStackIndex();
7067 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination));
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007068 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1));
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007069 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007070 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007071 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) {
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007072 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>());
7073 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007074 __ vmovd(DTMP, first);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007075 __ vmovd(first, second);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007076 __ vmovd(second, DTMP);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007077 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) {
7078 DRegister reg = source.IsFpuRegisterPair()
7079 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())
7080 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>());
7081 int mem = source.IsFpuRegisterPair()
7082 ? destination.GetStackIndex()
7083 : source.GetStackIndex();
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007084 __ vmovd(DTMP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007085 __ LoadDFromOffset(reg, SP, mem);
Nicolas Geoffrayffe8a572015-02-11 01:10:39 +00007086 __ StoreDToOffset(DTMP, SP, mem);
Nicolas Geoffray840e5462015-01-07 16:01:24 +00007087 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) {
7088 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>()
7089 : destination.AsFpuRegister<SRegister>();
7090 int mem = source.IsFpuRegister()
7091 ? destination.GetStackIndex()
7092 : source.GetStackIndex();
7093
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00007094 __ vmovrs(IP, reg);
Nicolas Geoffrayf7a0c4e2015-02-10 17:08:47 +00007095 __ LoadSFromOffset(reg, SP, mem);
Nicolas Geoffraya8eef822015-01-16 11:14:27 +00007096 __ StoreToOffset(kStoreWord, IP, SP, mem);
Nicolas Geoffray53f12622015-01-13 18:04:41 +00007097 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00007098 Exchange(source.GetStackIndex(), destination.GetStackIndex());
7099 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize));
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007100 } else {
Nicolas Geoffray53f12622015-01-13 18:04:41 +00007101 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination;
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01007102 }
7103}
7104
7105void ParallelMoveResolverARM::SpillScratch(int reg) {
7106 __ Push(static_cast<Register>(reg));
7107}
7108
7109void ParallelMoveResolverARM::RestoreScratch(int reg) {
7110 __ Pop(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01007111}
7112
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007113HLoadClass::LoadKind CodeGeneratorARM::GetSupportedLoadClassKind(
7114 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007115 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007116 case HLoadClass::LoadKind::kInvalid:
7117 LOG(FATAL) << "UNREACHABLE";
7118 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007119 case HLoadClass::LoadKind::kReferrersClass:
7120 break;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007121 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007122 case HLoadClass::LoadKind::kBssEntry:
7123 DCHECK(!Runtime::Current()->UseJitCompilation());
7124 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007125 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007126 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007127 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007128 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007129 case HLoadClass::LoadKind::kRuntimeCall:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007130 break;
7131 }
7132 return desired_class_load_kind;
7133}
7134
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007135void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007136 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007137 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007138 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko41559982017-01-06 14:04:23 +00007139 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007140 cls,
7141 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko41559982017-01-06 14:04:23 +00007142 Location::RegisterLocation(R0));
Vladimir Markoea4c1262017-02-06 19:59:33 +00007143 DCHECK_EQ(calling_convention.GetRegisterAt(0), R0);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007144 return;
7145 }
Vladimir Marko41559982017-01-06 14:04:23 +00007146 DCHECK(!cls->NeedsAccessCheck());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007147
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007148 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7149 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007150 ? LocationSummary::kCallOnSlowPath
7151 : LocationSummary::kNoCall;
7152 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007153 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007154 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007155 }
7156
Vladimir Marko41559982017-01-06 14:04:23 +00007157 if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007158 locations->SetInAt(0, Location::RequiresRegister());
7159 }
7160 locations->SetOut(Location::RequiresRegister());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007161 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7162 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7163 // Rely on the type resolution or initialization and marking to save everything we need.
7164 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7165 // to the custom calling convention) or by marking, so we request a different temp.
7166 locations->AddTemp(Location::RequiresRegister());
7167 RegisterSet caller_saves = RegisterSet::Empty();
7168 InvokeRuntimeCallingConvention calling_convention;
7169 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7170 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7171 // that the the kPrimNot result register is the same as the first argument register.
7172 locations->SetCustomSlowPathCallerSaves(caller_saves);
7173 } else {
7174 // For non-Baker read barrier we have a temp-clobbering call.
7175 }
7176 }
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007177 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7178 if (load_kind == HLoadClass::LoadKind::kBssEntry ||
7179 (load_kind == HLoadClass::LoadKind::kReferrersClass &&
7180 !Runtime::Current()->UseJitCompilation())) {
7181 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
7182 }
7183 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007184}
7185
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007186// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7187// move.
7188void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007189 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007190 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007191 codegen_->GenerateLoadClassRuntimeCall(cls);
Calin Juravle580b6092015-10-06 17:35:58 +01007192 return;
7193 }
Vladimir Marko41559982017-01-06 14:04:23 +00007194 DCHECK(!cls->NeedsAccessCheck());
Calin Juravle580b6092015-10-06 17:35:58 +01007195
Vladimir Marko41559982017-01-06 14:04:23 +00007196 LocationSummary* locations = cls->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007197 Location out_loc = locations->Out();
7198 Register out = out_loc.AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007199
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007200 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7201 ? kWithoutReadBarrier
7202 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007203 bool generate_null_check = false;
Vladimir Marko41559982017-01-06 14:04:23 +00007204 switch (load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007205 case HLoadClass::LoadKind::kReferrersClass: {
7206 DCHECK(!cls->CanCallRuntime());
7207 DCHECK(!cls->MustGenerateClinitCheck());
7208 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7209 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007210 GenerateGcRootFieldLoad(cls,
7211 out_loc,
7212 current_method,
7213 ArtMethod::DeclaringClassOffset().Int32Value(),
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007214 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007215 break;
7216 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007217 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007218 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007219 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007220 CodeGeneratorARM::PcRelativePatchInfo* labels =
7221 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7222 __ BindTrackedLabel(&labels->movw_label);
7223 __ movw(out, /* placeholder */ 0u);
7224 __ BindTrackedLabel(&labels->movt_label);
7225 __ movt(out, /* placeholder */ 0u);
7226 __ BindTrackedLabel(&labels->add_pc_label);
7227 __ add(out, out, ShifterOperand(PC));
7228 break;
7229 }
7230 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007231 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007232 uint32_t address = dchecked_integral_cast<uint32_t>(
7233 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7234 DCHECK_NE(address, 0u);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007235 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7236 break;
7237 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007238 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007239 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7240 ? locations->GetTemp(0).AsRegister<Register>()
7241 : out;
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007242 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007243 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007244 __ BindTrackedLabel(&labels->movw_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00007245 __ movw(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007246 __ BindTrackedLabel(&labels->movt_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00007247 __ movt(temp, /* placeholder */ 0u);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007248 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Markoea4c1262017-02-06 19:59:33 +00007249 __ add(temp, temp, ShifterOperand(PC));
7250 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007251 generate_null_check = true;
7252 break;
7253 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007254 case HLoadClass::LoadKind::kJitTableAddress: {
7255 __ LoadLiteral(out, codegen_->DeduplicateJitClassLiteral(cls->GetDexFile(),
7256 cls->GetTypeIndex(),
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007257 cls->GetClass()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007258 // /* GcRoot<mirror::Class> */ out = *out
Vladimir Markoea4c1262017-02-06 19:59:33 +00007259 GenerateGcRootFieldLoad(cls, out_loc, out, /* offset */ 0, read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007260 break;
7261 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007262 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007263 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007264 LOG(FATAL) << "UNREACHABLE";
7265 UNREACHABLE();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007266 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007267
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007268 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7269 DCHECK(cls->CanCallRuntime());
Artem Serovf4d6aee2016-07-11 10:41:45 +01007270 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007271 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7272 codegen_->AddSlowPath(slow_path);
7273 if (generate_null_check) {
7274 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7275 }
7276 if (cls->MustGenerateClinitCheck()) {
7277 GenerateClassInitializationCheck(slow_path, out);
7278 } else {
7279 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007280 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007281 }
7282}
7283
7284void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) {
7285 LocationSummary* locations =
7286 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
7287 locations->SetInAt(0, Location::RequiresRegister());
7288 if (check->HasUses()) {
7289 locations->SetOut(Location::SameAsFirstInput());
7290 }
7291}
7292
7293void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007294 // We assume the class is not null.
Artem Serovf4d6aee2016-07-11 10:41:45 +01007295 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007296 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007297 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00007298 GenerateClassInitializationCheck(slow_path,
7299 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007300}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007301
Nicolas Geoffray424f6762014-11-03 14:51:25 +00007302void InstructionCodeGeneratorARM::GenerateClassInitializationCheck(
Artem Serovf4d6aee2016-07-11 10:41:45 +01007303 SlowPathCodeARM* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01007304 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value());
7305 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized));
7306 __ b(slow_path->GetEntryLabel(), LT);
7307 // Even if the initialized flag is set, we may be in a situation where caches are not synced
7308 // properly. Therefore, we do a memory fence.
7309 __ dmb(ISH);
7310 __ Bind(slow_path->GetExitLabel());
7311}
7312
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007313HLoadString::LoadKind CodeGeneratorARM::GetSupportedLoadStringKind(
7314 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007315 switch (desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007316 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007317 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01007318 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007319 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007320 case HLoadString::LoadKind::kJitTableAddress:
7321 DCHECK(Runtime::Current()->UseJitCompilation());
7322 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007323 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007324 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007325 break;
7326 }
7327 return desired_string_load_kind;
7328}
7329
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007330void LocationsBuilderARM::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007331 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007332 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007333 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007334 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007335 locations->SetOut(Location::RegisterLocation(R0));
7336 } else {
7337 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007338 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7339 if (!kUseReadBarrier || kUseBakerReadBarrier) {
Vladimir Markoea4c1262017-02-06 19:59:33 +00007340 // Rely on the pResolveString and marking to save everything we need, including temps.
7341 // Note that IP may be clobbered by saving/restoring the live register (only one thanks
7342 // to the custom calling convention) or by marking, so we request a different temp.
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007343 locations->AddTemp(Location::RequiresRegister());
7344 RegisterSet caller_saves = RegisterSet::Empty();
7345 InvokeRuntimeCallingConvention calling_convention;
7346 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7347 // TODO: Add GetReturnLocation() to the calling convention so that we can DCHECK()
7348 // that the the kPrimNot result register is the same as the first argument register.
7349 locations->SetCustomSlowPathCallerSaves(caller_saves);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007350 if (kUseBakerReadBarrier && kBakerReadBarrierLinkTimeThunksEnableForGcRoots) {
7351 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
7352 }
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007353 } else {
7354 // For non-Baker read barrier we have a temp-clobbering call.
7355 }
7356 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007357 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007358}
7359
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007360// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7361// move.
7362void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01007363 LocationSummary* locations = load->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007364 Location out_loc = locations->Out();
7365 Register out = out_loc.AsRegister<Register>();
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007366 HLoadString::LoadKind load_kind = load->GetLoadKind();
Roland Levillain3b359c72015-11-17 19:35:12 +00007367
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007368 switch (load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007369 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007370 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007371 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007372 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007373 __ BindTrackedLabel(&labels->movw_label);
7374 __ movw(out, /* placeholder */ 0u);
7375 __ BindTrackedLabel(&labels->movt_label);
7376 __ movt(out, /* placeholder */ 0u);
7377 __ BindTrackedLabel(&labels->add_pc_label);
7378 __ add(out, out, ShifterOperand(PC));
7379 return; // No dex cache slow path.
7380 }
7381 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007382 uint32_t address = dchecked_integral_cast<uint32_t>(
7383 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7384 DCHECK_NE(address, 0u);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007385 __ LoadLiteral(out, codegen_->DeduplicateBootImageAddressLiteral(address));
7386 return; // No dex cache slow path.
7387 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007388 case HLoadString::LoadKind::kBssEntry: {
7389 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoea4c1262017-02-06 19:59:33 +00007390 Register temp = (!kUseReadBarrier || kUseBakerReadBarrier)
7391 ? locations->GetTemp(0).AsRegister<Register>()
7392 : out;
Vladimir Markoaad75c62016-10-03 08:46:48 +00007393 CodeGeneratorARM::PcRelativePatchInfo* labels =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007394 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Vladimir Markoaad75c62016-10-03 08:46:48 +00007395 __ BindTrackedLabel(&labels->movw_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007396 __ movw(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007397 __ BindTrackedLabel(&labels->movt_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007398 __ movt(temp, /* placeholder */ 0u);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007399 __ BindTrackedLabel(&labels->add_pc_label);
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007400 __ add(temp, temp, ShifterOperand(PC));
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007401 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007402 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load);
7403 codegen_->AddSlowPath(slow_path);
7404 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel());
7405 __ Bind(slow_path->GetExitLabel());
7406 return;
7407 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007408 case HLoadString::LoadKind::kJitTableAddress: {
7409 __ LoadLiteral(out, codegen_->DeduplicateJitStringLiteral(load->GetDexFile(),
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007410 load->GetStringIndex(),
7411 load->GetString()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007412 // /* GcRoot<mirror::String> */ out = *out
7413 GenerateGcRootFieldLoad(load, out_loc, out, /* offset */ 0, kCompilerReadBarrierOption);
7414 return;
7415 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007416 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007417 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007418 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007419
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007420 // TODO: Consider re-adding the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007421 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007422 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01007423 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007424 __ LoadImmediate(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Christina Wadsworthd8ec6db2016-08-30 17:19:14 -07007425 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7426 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00007427}
7428
David Brazdilcb1c0552015-08-04 16:22:25 +01007429static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007430 return Thread::ExceptionOffset<kArmPointerSize>().Int32Value();
David Brazdilcb1c0552015-08-04 16:22:25 +01007431}
7432
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007433void LocationsBuilderARM::VisitLoadException(HLoadException* load) {
7434 LocationSummary* locations =
7435 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7436 locations->SetOut(Location::RequiresRegister());
7437}
7438
7439void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00007440 Register out = load->GetLocations()->Out().AsRegister<Register>();
David Brazdilcb1c0552015-08-04 16:22:25 +01007441 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7442}
7443
7444void LocationsBuilderARM::VisitClearException(HClearException* clear) {
7445 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7446}
7447
7448void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007449 __ LoadImmediate(IP, 0);
David Brazdilcb1c0552015-08-04 16:22:25 +01007450 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset());
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007451}
7452
7453void LocationsBuilderARM::VisitThrow(HThrow* instruction) {
7454 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007455 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007456 InvokeRuntimeCallingConvention calling_convention;
7457 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7458}
7459
7460void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01007461 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00007462 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00007463}
7464
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007465// Temp is used for read barrier.
7466static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
7467 if (kEmitCompilerReadBarrier &&
7468 (kUseBakerReadBarrier ||
7469 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7470 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7471 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
7472 return 1;
7473 }
7474 return 0;
7475}
7476
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007477// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007478// interface pointer, one for loading the current interface.
7479// The other checks have one temp for loading the object's class.
7480static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
7481 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
7482 return 3;
7483 }
7484 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillainc9285912015-12-18 10:38:42 +00007485}
7486
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007487void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007488 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain3b359c72015-11-17 19:35:12 +00007489 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01007490 bool baker_read_barrier_slow_path = false;
Roland Levillain3b359c72015-11-17 19:35:12 +00007491 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007492 case TypeCheckKind::kExactCheck:
7493 case TypeCheckKind::kAbstractClassCheck:
7494 case TypeCheckKind::kClassHierarchyCheck:
7495 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007496 call_kind =
7497 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01007498 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007499 break;
7500 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007501 case TypeCheckKind::kUnresolvedCheck:
7502 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007503 call_kind = LocationSummary::kCallOnSlowPath;
7504 break;
7505 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007506
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007507 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01007508 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007509 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01007510 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007511 locations->SetInAt(0, Location::RequiresRegister());
7512 locations->SetInAt(1, Location::RequiresRegister());
7513 // The "out" register is used as a temporary, so it overlaps with the inputs.
7514 // Note that TypeCheckSlowPathARM uses this register too.
7515 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007516 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01007517 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
7518 codegen_->MaybeAddBakerCcEntrypointTempForFields(locations);
7519 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007520}
7521
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007522void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007523 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007524 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007525 Location obj_loc = locations->InAt(0);
7526 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007527 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007528 Location out_loc = locations->Out();
7529 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007530 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7531 DCHECK_LE(num_temps, 1u);
7532 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007533 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007534 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7535 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7536 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007537 Label done;
7538 Label* const final_label = codegen_->GetFinalLabel(instruction, &done);
Artem Serovf4d6aee2016-07-11 10:41:45 +01007539 SlowPathCodeARM* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007540
7541 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007542 // avoid null check if we know obj is not null.
7543 if (instruction->MustDoNullCheck()) {
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007544 DCHECK_NE(out, obj);
7545 __ LoadImmediate(out, 0);
7546 __ CompareAndBranchIfZero(obj, final_label);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007547 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007548
Roland Levillainc9285912015-12-18 10:38:42 +00007549 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007550 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007551 // /* HeapReference<Class> */ out = obj->klass_
7552 GenerateReferenceLoadTwoRegisters(instruction,
7553 out_loc,
7554 obj_loc,
7555 class_offset,
7556 maybe_temp_loc,
7557 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007558 // Classes must be equal for the instanceof to succeed.
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007559 __ cmp(out, ShifterOperand(cls));
7560 // We speculatively set the result to false without changing the condition
7561 // flags, which allows us to avoid some branching later.
7562 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7563
7564 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7565 // we check that the output is in a low register, so that a 16-bit MOV
7566 // encoding can be used.
7567 if (ArmAssembler::IsLowRegister(out)) {
7568 __ it(EQ);
7569 __ mov(out, ShifterOperand(1), EQ);
7570 } else {
7571 __ b(final_label, NE);
7572 __ LoadImmediate(out, 1);
7573 }
7574
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007575 break;
7576 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007577
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007578 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007579 // /* HeapReference<Class> */ out = obj->klass_
7580 GenerateReferenceLoadTwoRegisters(instruction,
7581 out_loc,
7582 obj_loc,
7583 class_offset,
7584 maybe_temp_loc,
7585 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007586 // If the class is abstract, we eagerly fetch the super class of the
7587 // object to avoid doing a comparison we know will fail.
7588 Label loop;
7589 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007590 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007591 GenerateReferenceLoadOneRegister(instruction,
7592 out_loc,
7593 super_offset,
7594 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007595 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007596 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007597 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007598 __ cmp(out, ShifterOperand(cls));
7599 __ b(&loop, NE);
7600 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007601 break;
7602 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007603
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007604 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007605 // /* HeapReference<Class> */ out = obj->klass_
7606 GenerateReferenceLoadTwoRegisters(instruction,
7607 out_loc,
7608 obj_loc,
7609 class_offset,
7610 maybe_temp_loc,
7611 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007612 // Walk over the class hierarchy to find a match.
7613 Label loop, success;
7614 __ Bind(&loop);
7615 __ cmp(out, ShifterOperand(cls));
7616 __ b(&success, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007617 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007618 GenerateReferenceLoadOneRegister(instruction,
7619 out_loc,
7620 super_offset,
7621 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007622 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007623 // This is essentially a null check, but it sets the condition flags to the
7624 // proper value for the code that follows the loop, i.e. not `EQ`.
7625 __ cmp(out, ShifterOperand(1));
7626 __ b(&loop, HS);
7627
7628 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7629 // we check that the output is in a low register, so that a 16-bit MOV
7630 // encoding can be used.
7631 if (ArmAssembler::IsLowRegister(out)) {
7632 // If `out` is null, we use it for the result, and the condition flags
7633 // have already been set to `NE`, so the IT block that comes afterwards
7634 // (and which handles the successful case) turns into a NOP (instead of
7635 // overwriting `out`).
7636 __ Bind(&success);
7637 // There is only one branch to the `success` label (which is bound to this
7638 // IT block), and it has the same condition, `EQ`, so in that case the MOV
7639 // is executed.
7640 __ it(EQ);
7641 __ mov(out, ShifterOperand(1), EQ);
7642 } else {
7643 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007644 __ b(final_label);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007645 __ Bind(&success);
7646 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007647 }
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007648
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007649 break;
7650 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007651
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007652 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007653 // /* HeapReference<Class> */ out = obj->klass_
7654 GenerateReferenceLoadTwoRegisters(instruction,
7655 out_loc,
7656 obj_loc,
7657 class_offset,
7658 maybe_temp_loc,
7659 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007660 // Do an exact check.
7661 Label exact_check;
7662 __ cmp(out, ShifterOperand(cls));
7663 __ b(&exact_check, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007664 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007665 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007666 GenerateReferenceLoadOneRegister(instruction,
7667 out_loc,
7668 component_offset,
7669 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007670 kCompilerReadBarrierOption);
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007671 // If `out` is null, we use it for the result, and jump to the final label.
Anton Kirilov6f644202017-02-27 18:29:45 +00007672 __ CompareAndBranchIfZero(out, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007673 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7674 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
Anton Kirilov1e7bb5a2017-03-17 12:30:44 +00007675 __ cmp(out, ShifterOperand(0));
7676 // We speculatively set the result to false without changing the condition
7677 // flags, which allows us to avoid some branching later.
7678 __ mov(out, ShifterOperand(0), AL, kCcKeep);
7679
7680 // Since IT blocks longer than a 16-bit instruction are deprecated by ARMv8,
7681 // we check that the output is in a low register, so that a 16-bit MOV
7682 // encoding can be used.
7683 if (ArmAssembler::IsLowRegister(out)) {
7684 __ Bind(&exact_check);
7685 __ it(EQ);
7686 __ mov(out, ShifterOperand(1), EQ);
7687 } else {
7688 __ b(final_label, NE);
7689 __ Bind(&exact_check);
7690 __ LoadImmediate(out, 1);
7691 }
7692
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007693 break;
7694 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007695
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007696 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08007697 // No read barrier since the slow path will retry upon failure.
7698 // /* HeapReference<Class> */ out = obj->klass_
7699 GenerateReferenceLoadTwoRegisters(instruction,
7700 out_loc,
7701 obj_loc,
7702 class_offset,
7703 maybe_temp_loc,
7704 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007705 __ cmp(out, ShifterOperand(cls));
7706 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain3b359c72015-11-17 19:35:12 +00007707 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7708 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007709 codegen_->AddSlowPath(slow_path);
7710 __ b(slow_path->GetEntryLabel(), NE);
7711 __ LoadImmediate(out, 1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007712 break;
7713 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007714
Calin Juravle98893e12015-10-02 21:05:03 +01007715 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007716 case TypeCheckKind::kInterfaceCheck: {
7717 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007718 // into the slow path for the unresolved and interface check
Roland Levillain3b359c72015-11-17 19:35:12 +00007719 // cases.
7720 //
7721 // We cannot directly call the InstanceofNonTrivial runtime
7722 // entry point without resorting to a type checking slow path
7723 // here (i.e. by calling InvokeRuntime directly), as it would
7724 // require to assign fixed registers for the inputs of this
7725 // HInstanceOf instruction (following the runtime calling
7726 // convention), which might be cluttered by the potential first
7727 // read barrier emission at the beginning of this method.
Roland Levillainc9285912015-12-18 10:38:42 +00007728 //
7729 // TODO: Introduce a new runtime entry point taking the object
7730 // to test (instead of its class) as argument, and let it deal
7731 // with the read barrier issues. This will let us refactor this
7732 // case of the `switch` code as it was previously (with a direct
7733 // call to the runtime not using a type checking slow path).
7734 // This should also be beneficial for the other cases above.
Roland Levillain3b359c72015-11-17 19:35:12 +00007735 DCHECK(locations->OnlyCallsOnSlowPath());
7736 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7737 /* is_fatal */ false);
7738 codegen_->AddSlowPath(slow_path);
7739 __ b(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007740 break;
7741 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007742 }
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01007743
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007744 if (done.IsLinked()) {
7745 __ Bind(&done);
7746 }
7747
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007748 if (slow_path != nullptr) {
7749 __ Bind(slow_path->GetExitLabel());
7750 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00007751}
7752
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007753void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007754 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7755 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
7756
Roland Levillain3b359c72015-11-17 19:35:12 +00007757 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
7758 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007759 case TypeCheckKind::kExactCheck:
7760 case TypeCheckKind::kAbstractClassCheck:
7761 case TypeCheckKind::kClassHierarchyCheck:
7762 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007763 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
7764 LocationSummary::kCallOnSlowPath :
7765 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007766 break;
7767 case TypeCheckKind::kArrayCheck:
Roland Levillain3b359c72015-11-17 19:35:12 +00007768 case TypeCheckKind::kUnresolvedCheck:
7769 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007770 call_kind = LocationSummary::kCallOnSlowPath;
7771 break;
7772 }
7773
Roland Levillain3b359c72015-11-17 19:35:12 +00007774 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
7775 locations->SetInAt(0, Location::RequiresRegister());
7776 locations->SetInAt(1, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007777 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007778}
7779
7780void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) {
Roland Levillainc9285912015-12-18 10:38:42 +00007781 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007782 LocationSummary* locations = instruction->GetLocations();
Roland Levillain3b359c72015-11-17 19:35:12 +00007783 Location obj_loc = locations->InAt(0);
7784 Register obj = obj_loc.AsRegister<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00007785 Register cls = locations->InAt(1).AsRegister<Register>();
Roland Levillain3b359c72015-11-17 19:35:12 +00007786 Location temp_loc = locations->GetTemp(0);
7787 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007788 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
7789 DCHECK_LE(num_temps, 3u);
7790 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
7791 Location maybe_temp3_loc = (num_temps >= 3) ? locations->GetTemp(2) : Location::NoLocation();
7792 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7793 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7794 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7795 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
7796 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
7797 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
7798 const uint32_t object_array_data_offset =
7799 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007800
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007801 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
7802 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
7803 // read barriers is done for performance and code size reasons.
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007804 bool is_type_check_slow_path_fatal = false;
7805 if (!kEmitCompilerReadBarrier) {
7806 is_type_check_slow_path_fatal =
7807 (type_check_kind == TypeCheckKind::kExactCheck ||
7808 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
7809 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
7810 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
7811 !instruction->CanThrowIntoCatchBlock();
7812 }
Artem Serovf4d6aee2016-07-11 10:41:45 +01007813 SlowPathCodeARM* type_check_slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00007814 new (GetGraph()->GetArena()) TypeCheckSlowPathARM(instruction,
7815 is_type_check_slow_path_fatal);
7816 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007817
7818 Label done;
Anton Kirilov6f644202017-02-27 18:29:45 +00007819 Label* final_label = codegen_->GetFinalLabel(instruction, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007820 // Avoid null check if we know obj is not null.
7821 if (instruction->MustDoNullCheck()) {
Anton Kirilov6f644202017-02-27 18:29:45 +00007822 __ CompareAndBranchIfZero(obj, final_label);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007823 }
7824
Roland Levillain3b359c72015-11-17 19:35:12 +00007825 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007826 case TypeCheckKind::kExactCheck:
7827 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007828 // /* HeapReference<Class> */ temp = obj->klass_
7829 GenerateReferenceLoadTwoRegisters(instruction,
7830 temp_loc,
7831 obj_loc,
7832 class_offset,
7833 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007834 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007835
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007836 __ cmp(temp, ShifterOperand(cls));
7837 // Jump to slow path for throwing the exception or doing a
7838 // more involved array check.
Roland Levillain3b359c72015-11-17 19:35:12 +00007839 __ b(type_check_slow_path->GetEntryLabel(), NE);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007840 break;
7841 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007842
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007843 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007844 // /* HeapReference<Class> */ temp = obj->klass_
7845 GenerateReferenceLoadTwoRegisters(instruction,
7846 temp_loc,
7847 obj_loc,
7848 class_offset,
7849 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007850 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007851
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007852 // If the class is abstract, we eagerly fetch the super class of the
7853 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007854 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007855 __ Bind(&loop);
Roland Levillain3b359c72015-11-17 19:35:12 +00007856 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007857 GenerateReferenceLoadOneRegister(instruction,
7858 temp_loc,
7859 super_offset,
7860 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007861 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007862
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007863 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7864 // exception.
7865 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
Roland Levillain3b359c72015-11-17 19:35:12 +00007866
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007867 // Otherwise, compare the classes.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007868 __ cmp(temp, ShifterOperand(cls));
7869 __ b(&loop, NE);
7870 break;
7871 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007872
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007873 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007874 // /* HeapReference<Class> */ temp = obj->klass_
7875 GenerateReferenceLoadTwoRegisters(instruction,
7876 temp_loc,
7877 obj_loc,
7878 class_offset,
7879 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007880 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007881
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007882 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007883 Label loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007884 __ Bind(&loop);
7885 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007886 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007887
Roland Levillain3b359c72015-11-17 19:35:12 +00007888 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007889 GenerateReferenceLoadOneRegister(instruction,
7890 temp_loc,
7891 super_offset,
7892 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007893 kWithoutReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00007894
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007895 // If the class reference currently in `temp` is null, jump to the slow path to throw the
7896 // exception.
7897 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7898 // Otherwise, jump to the beginning of the loop.
7899 __ b(&loop);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007900 break;
7901 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007902
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007903 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007904 // /* HeapReference<Class> */ temp = obj->klass_
7905 GenerateReferenceLoadTwoRegisters(instruction,
7906 temp_loc,
7907 obj_loc,
7908 class_offset,
7909 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007910 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007911
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01007912 // Do an exact check.
7913 __ cmp(temp, ShifterOperand(cls));
Anton Kirilov6f644202017-02-27 18:29:45 +00007914 __ b(final_label, EQ);
Roland Levillain3b359c72015-11-17 19:35:12 +00007915
7916 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain3b359c72015-11-17 19:35:12 +00007917 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08007918 GenerateReferenceLoadOneRegister(instruction,
7919 temp_loc,
7920 component_offset,
7921 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007922 kWithoutReadBarrier);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007923 // If the component type is null, jump to the slow path to throw the exception.
7924 __ CompareAndBranchIfZero(temp, type_check_slow_path->GetEntryLabel());
7925 // Otherwise,the object is indeed an array, jump to label `check_non_primitive_component_type`
7926 // to further check that this component type is not a primitive type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007927 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
Roland Levillain3b359c72015-11-17 19:35:12 +00007928 static_assert(Primitive::kPrimNot == 0, "Expected 0 for art::Primitive::kPrimNot");
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08007929 __ CompareAndBranchIfNonZero(temp, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007930 break;
7931 }
Roland Levillain3b359c72015-11-17 19:35:12 +00007932
Calin Juravle98893e12015-10-02 21:05:03 +01007933 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007934 // We always go into the type check slow path for the unresolved check case.
Roland Levillain3b359c72015-11-17 19:35:12 +00007935 // We cannot directly call the CheckCast runtime entry point
7936 // without resorting to a type checking slow path here (i.e. by
7937 // calling InvokeRuntime directly), as it would require to
7938 // assign fixed registers for the inputs of this HInstanceOf
7939 // instruction (following the runtime calling convention), which
7940 // might be cluttered by the potential first read barrier
7941 // emission at the beginning of this method.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007942
Roland Levillain3b359c72015-11-17 19:35:12 +00007943 __ b(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007944 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007945
7946 case TypeCheckKind::kInterfaceCheck: {
7947 // Avoid read barriers to improve performance of the fast path. We can not get false
7948 // positives by doing this.
7949 // /* HeapReference<Class> */ temp = obj->klass_
7950 GenerateReferenceLoadTwoRegisters(instruction,
7951 temp_loc,
7952 obj_loc,
7953 class_offset,
7954 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007955 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007956
7957 // /* HeapReference<Class> */ temp = temp->iftable_
7958 GenerateReferenceLoadTwoRegisters(instruction,
7959 temp_loc,
7960 temp_loc,
7961 iftable_offset,
7962 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007963 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08007964 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007965 __ ldr(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08007966 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007967 Label start_loop;
7968 __ Bind(&start_loop);
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007969 __ CompareAndBranchIfZero(maybe_temp2_loc.AsRegister<Register>(),
7970 type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007971 __ ldr(maybe_temp3_loc.AsRegister<Register>(), Address(temp, object_array_data_offset));
7972 __ MaybeUnpoisonHeapReference(maybe_temp3_loc.AsRegister<Register>());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007973 // Go to next interface.
7974 __ add(temp, temp, ShifterOperand(2 * kHeapReferenceSize));
7975 __ sub(maybe_temp2_loc.AsRegister<Register>(),
7976 maybe_temp2_loc.AsRegister<Register>(),
7977 ShifterOperand(2));
Mathieu Chartierafbcdaf2016-11-14 10:50:29 -08007978 // Compare the classes and continue the loop if they do not match.
7979 __ cmp(cls, ShifterOperand(maybe_temp3_loc.AsRegister<Register>()));
7980 __ b(&start_loop, NE);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07007981 break;
7982 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007983 }
Anton Kirilov6f644202017-02-27 18:29:45 +00007984
7985 if (done.IsLinked()) {
7986 __ Bind(&done);
7987 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00007988
Roland Levillain3b359c72015-11-17 19:35:12 +00007989 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00007990}
7991
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007992void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) {
7993 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007994 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00007995 InvokeRuntimeCallingConvention calling_convention;
7996 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7997}
7998
7999void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescu4bb30ac2016-06-22 17:04:45 +01008000 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject : kQuickUnlockObject,
8001 instruction,
8002 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008003 if (instruction->IsEnter()) {
8004 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8005 } else {
8006 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8007 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00008008}
8009
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008010void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); }
8011void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); }
8012void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008013
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008014void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008015 LocationSummary* locations =
8016 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8017 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
8018 || instruction->GetResultType() == Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008019 // Note: GVN reorders commutative operations to have the constant on the right hand side.
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008020 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008021 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode));
Nicolas Geoffray829280c2015-01-28 10:20:37 +00008022 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008023}
8024
8025void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) {
8026 HandleBitwiseOperation(instruction);
8027}
8028
8029void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) {
8030 HandleBitwiseOperation(instruction);
8031}
8032
8033void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) {
8034 HandleBitwiseOperation(instruction);
8035}
8036
Artem Serov7fc63502016-02-09 17:15:29 +00008037
8038void LocationsBuilderARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8039 LocationSummary* locations =
8040 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8041 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
8042 || instruction->GetResultType() == Primitive::kPrimLong);
8043
8044 locations->SetInAt(0, Location::RequiresRegister());
8045 locations->SetInAt(1, Location::RequiresRegister());
8046 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8047}
8048
8049void InstructionCodeGeneratorARM::VisitBitwiseNegatedRight(HBitwiseNegatedRight* instruction) {
8050 LocationSummary* locations = instruction->GetLocations();
8051 Location first = locations->InAt(0);
8052 Location second = locations->InAt(1);
8053 Location out = locations->Out();
8054
8055 if (instruction->GetResultType() == Primitive::kPrimInt) {
8056 Register first_reg = first.AsRegister<Register>();
8057 ShifterOperand second_reg(second.AsRegister<Register>());
8058 Register out_reg = out.AsRegister<Register>();
8059
8060 switch (instruction->GetOpKind()) {
8061 case HInstruction::kAnd:
8062 __ bic(out_reg, first_reg, second_reg);
8063 break;
8064 case HInstruction::kOr:
8065 __ orn(out_reg, first_reg, second_reg);
8066 break;
8067 // There is no EON on arm.
8068 case HInstruction::kXor:
8069 default:
8070 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8071 UNREACHABLE();
8072 }
8073 return;
8074
8075 } else {
8076 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8077 Register first_low = first.AsRegisterPairLow<Register>();
8078 Register first_high = first.AsRegisterPairHigh<Register>();
8079 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
8080 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
8081 Register out_low = out.AsRegisterPairLow<Register>();
8082 Register out_high = out.AsRegisterPairHigh<Register>();
8083
8084 switch (instruction->GetOpKind()) {
8085 case HInstruction::kAnd:
8086 __ bic(out_low, first_low, second_low);
8087 __ bic(out_high, first_high, second_high);
8088 break;
8089 case HInstruction::kOr:
8090 __ orn(out_low, first_low, second_low);
8091 __ orn(out_high, first_high, second_high);
8092 break;
8093 // There is no EON on arm.
8094 case HInstruction::kXor:
8095 default:
8096 LOG(FATAL) << "Unexpected instruction " << instruction->DebugName();
8097 UNREACHABLE();
8098 }
8099 }
8100}
8101
Anton Kirilov74234da2017-01-13 14:42:47 +00008102void LocationsBuilderARM::VisitDataProcWithShifterOp(
8103 HDataProcWithShifterOp* instruction) {
8104 DCHECK(instruction->GetType() == Primitive::kPrimInt ||
8105 instruction->GetType() == Primitive::kPrimLong);
8106 LocationSummary* locations =
8107 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8108 const bool overlap = instruction->GetType() == Primitive::kPrimLong &&
8109 HDataProcWithShifterOp::IsExtensionOp(instruction->GetOpKind());
8110
8111 locations->SetInAt(0, Location::RequiresRegister());
8112 locations->SetInAt(1, Location::RequiresRegister());
8113 locations->SetOut(Location::RequiresRegister(),
8114 overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap);
8115}
8116
8117void InstructionCodeGeneratorARM::VisitDataProcWithShifterOp(
8118 HDataProcWithShifterOp* instruction) {
8119 const LocationSummary* const locations = instruction->GetLocations();
8120 const HInstruction::InstructionKind kind = instruction->GetInstrKind();
8121 const HDataProcWithShifterOp::OpKind op_kind = instruction->GetOpKind();
8122 const Location left = locations->InAt(0);
8123 const Location right = locations->InAt(1);
8124 const Location out = locations->Out();
8125
8126 if (instruction->GetType() == Primitive::kPrimInt) {
8127 DCHECK(!HDataProcWithShifterOp::IsExtensionOp(op_kind));
8128
8129 const Register second = instruction->InputAt(1)->GetType() == Primitive::kPrimLong
8130 ? right.AsRegisterPairLow<Register>()
8131 : right.AsRegister<Register>();
8132
8133 GenerateDataProcInstruction(kind,
8134 out.AsRegister<Register>(),
8135 left.AsRegister<Register>(),
8136 ShifterOperand(second,
8137 ShiftFromOpKind(op_kind),
8138 instruction->GetShiftAmount()),
8139 codegen_);
8140 } else {
8141 DCHECK_EQ(instruction->GetType(), Primitive::kPrimLong);
8142
8143 if (HDataProcWithShifterOp::IsExtensionOp(op_kind)) {
8144 const Register second = right.AsRegister<Register>();
8145
8146 DCHECK_NE(out.AsRegisterPairLow<Register>(), second);
8147 GenerateDataProc(kind,
8148 out,
8149 left,
8150 ShifterOperand(second),
8151 ShifterOperand(second, ASR, 31),
8152 codegen_);
8153 } else {
8154 GenerateLongDataProc(instruction, codegen_);
8155 }
8156 }
8157}
8158
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008159void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) {
8160 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier).
8161 if (value == 0xffffffffu) {
8162 if (out != first) {
8163 __ mov(out, ShifterOperand(first));
8164 }
8165 return;
8166 }
8167 if (value == 0u) {
8168 __ mov(out, ShifterOperand(0));
8169 return;
8170 }
8171 ShifterOperand so;
8172 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) {
8173 __ and_(out, first, so);
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008174 } else if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so)) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008175 __ bic(out, first, ShifterOperand(~value));
Anton Kiriloveffd5bf2017-02-28 16:59:15 +00008176 } else {
8177 DCHECK(IsPowerOfTwo(value + 1));
8178 __ ubfx(out, first, 0, WhichPowerOf2(value + 1));
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008179 }
8180}
8181
8182void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) {
8183 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier).
8184 if (value == 0u) {
8185 if (out != first) {
8186 __ mov(out, ShifterOperand(first));
8187 }
8188 return;
8189 }
8190 if (value == 0xffffffffu) {
8191 __ mvn(out, ShifterOperand(0));
8192 return;
8193 }
8194 ShifterOperand so;
8195 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) {
8196 __ orr(out, first, so);
8197 } else {
8198 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so));
8199 __ orn(out, first, ShifterOperand(~value));
8200 }
8201}
8202
8203void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) {
8204 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier).
8205 if (value == 0u) {
8206 if (out != first) {
8207 __ mov(out, ShifterOperand(first));
8208 }
8209 return;
8210 }
8211 __ eor(out, first, ShifterOperand(value));
8212}
8213
Vladimir Marko59751a72016-08-05 14:37:27 +01008214void InstructionCodeGeneratorARM::GenerateAddLongConst(Location out,
8215 Location first,
8216 uint64_t value) {
8217 Register out_low = out.AsRegisterPairLow<Register>();
8218 Register out_high = out.AsRegisterPairHigh<Register>();
8219 Register first_low = first.AsRegisterPairLow<Register>();
8220 Register first_high = first.AsRegisterPairHigh<Register>();
8221 uint32_t value_low = Low32Bits(value);
8222 uint32_t value_high = High32Bits(value);
8223 if (value_low == 0u) {
8224 if (out_low != first_low) {
8225 __ mov(out_low, ShifterOperand(first_low));
8226 }
8227 __ AddConstant(out_high, first_high, value_high);
8228 return;
8229 }
8230 __ AddConstantSetFlags(out_low, first_low, value_low);
8231 ShifterOperand so;
8232 if (__ ShifterOperandCanHold(out_high, first_high, ADC, value_high, kCcDontCare, &so)) {
8233 __ adc(out_high, first_high, so);
8234 } else if (__ ShifterOperandCanHold(out_low, first_low, SBC, ~value_high, kCcDontCare, &so)) {
8235 __ sbc(out_high, first_high, so);
8236 } else {
8237 LOG(FATAL) << "Unexpected constant " << value_high;
8238 UNREACHABLE();
8239 }
8240}
8241
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008242void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) {
8243 LocationSummary* locations = instruction->GetLocations();
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008244 Location first = locations->InAt(0);
8245 Location second = locations->InAt(1);
8246 Location out = locations->Out();
8247
8248 if (second.IsConstant()) {
8249 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant()));
8250 uint32_t value_low = Low32Bits(value);
8251 if (instruction->GetResultType() == Primitive::kPrimInt) {
8252 Register first_reg = first.AsRegister<Register>();
8253 Register out_reg = out.AsRegister<Register>();
8254 if (instruction->IsAnd()) {
8255 GenerateAndConst(out_reg, first_reg, value_low);
8256 } else if (instruction->IsOr()) {
8257 GenerateOrrConst(out_reg, first_reg, value_low);
8258 } else {
8259 DCHECK(instruction->IsXor());
8260 GenerateEorConst(out_reg, first_reg, value_low);
8261 }
8262 } else {
8263 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
8264 uint32_t value_high = High32Bits(value);
8265 Register first_low = first.AsRegisterPairLow<Register>();
8266 Register first_high = first.AsRegisterPairHigh<Register>();
8267 Register out_low = out.AsRegisterPairLow<Register>();
8268 Register out_high = out.AsRegisterPairHigh<Register>();
8269 if (instruction->IsAnd()) {
8270 GenerateAndConst(out_low, first_low, value_low);
8271 GenerateAndConst(out_high, first_high, value_high);
8272 } else if (instruction->IsOr()) {
8273 GenerateOrrConst(out_low, first_low, value_low);
8274 GenerateOrrConst(out_high, first_high, value_high);
8275 } else {
8276 DCHECK(instruction->IsXor());
8277 GenerateEorConst(out_low, first_low, value_low);
8278 GenerateEorConst(out_high, first_high, value_high);
8279 }
8280 }
8281 return;
8282 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008283
8284 if (instruction->GetResultType() == Primitive::kPrimInt) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008285 Register first_reg = first.AsRegister<Register>();
8286 ShifterOperand second_reg(second.AsRegister<Register>());
8287 Register out_reg = out.AsRegister<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008288 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008289 __ and_(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008290 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008291 __ orr(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008292 } else {
8293 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008294 __ eor(out_reg, first_reg, second_reg);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008295 }
8296 } else {
8297 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008298 Register first_low = first.AsRegisterPairLow<Register>();
8299 Register first_high = first.AsRegisterPairHigh<Register>();
8300 ShifterOperand second_low(second.AsRegisterPairLow<Register>());
8301 ShifterOperand second_high(second.AsRegisterPairHigh<Register>());
8302 Register out_low = out.AsRegisterPairLow<Register>();
8303 Register out_high = out.AsRegisterPairHigh<Register>();
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008304 if (instruction->IsAnd()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008305 __ and_(out_low, first_low, second_low);
8306 __ and_(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008307 } else if (instruction->IsOr()) {
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008308 __ orr(out_low, first_low, second_low);
8309 __ orr(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008310 } else {
8311 DCHECK(instruction->IsXor());
Vladimir Markod2b4ca22015-09-14 15:13:26 +01008312 __ eor(out_low, first_low, second_low);
8313 __ eor(out_high, first_high, second_high);
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00008314 }
8315 }
8316}
8317
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008318void InstructionCodeGeneratorARM::GenerateReferenceLoadOneRegister(
8319 HInstruction* instruction,
8320 Location out,
8321 uint32_t offset,
8322 Location maybe_temp,
8323 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008324 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008325 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08008326 CHECK(kEmitCompilerReadBarrier);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008327 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00008328 if (kUseBakerReadBarrier) {
8329 // Load with fast path based Baker's read barrier.
8330 // /* HeapReference<Object> */ out = *(out + offset)
8331 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008332 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00008333 } else {
8334 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008335 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillainc9285912015-12-18 10:38:42 +00008336 // in the following move operation, as we will need it for the
8337 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008338 __ Mov(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00008339 // /* HeapReference<Object> */ out = *(out + offset)
8340 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008341 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillainc9285912015-12-18 10:38:42 +00008342 }
8343 } else {
8344 // Plain load with no read barrier.
8345 // /* HeapReference<Object> */ out = *(out + offset)
8346 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
8347 __ MaybeUnpoisonHeapReference(out_reg);
8348 }
8349}
8350
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008351void InstructionCodeGeneratorARM::GenerateReferenceLoadTwoRegisters(
8352 HInstruction* instruction,
8353 Location out,
8354 Location obj,
8355 uint32_t offset,
8356 Location maybe_temp,
8357 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008358 Register out_reg = out.AsRegister<Register>();
8359 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008360 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08008361 CHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00008362 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008363 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillainc9285912015-12-18 10:38:42 +00008364 // Load with fast path based Baker's read barrier.
8365 // /* HeapReference<Object> */ out = *(obj + offset)
8366 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00008367 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillainc9285912015-12-18 10:38:42 +00008368 } else {
8369 // Load with slow path based read barrier.
8370 // /* HeapReference<Object> */ out = *(obj + offset)
8371 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8372 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
8373 }
8374 } else {
8375 // Plain load with no read barrier.
8376 // /* HeapReference<Object> */ out = *(obj + offset)
8377 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
8378 __ MaybeUnpoisonHeapReference(out_reg);
8379 }
8380}
8381
8382void InstructionCodeGeneratorARM::GenerateGcRootFieldLoad(HInstruction* instruction,
8383 Location root,
8384 Register obj,
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008385 uint32_t offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008386 ReadBarrierOption read_barrier_option) {
Roland Levillainc9285912015-12-18 10:38:42 +00008387 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08008388 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07008389 DCHECK(kEmitCompilerReadBarrier);
Roland Levillainc9285912015-12-18 10:38:42 +00008390 if (kUseBakerReadBarrier) {
8391 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
Roland Levillainba650a42017-03-06 13:52:32 +00008392 // Baker's read barrier are used.
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008393 if (kBakerReadBarrierLinkTimeThunksEnableForGcRoots &&
8394 !Runtime::Current()->UseJitCompilation()) {
8395 // Note that we do not actually check the value of `GetIsGcMarking()`
8396 // to decide whether to mark the loaded GC root or not. Instead, we
8397 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8398 // barrier mark introspection entrypoint. If `temp` is null, it means
8399 // that `GetIsGcMarking()` is false, and vice versa.
8400 //
8401 // We use link-time generated thunks for the slow path. That thunk
8402 // checks the reference and jumps to the entrypoint if needed.
8403 //
8404 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8405 // lr = &return_address;
8406 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8407 // if (temp != nullptr) {
8408 // goto gc_root_thunk<root_reg>(lr)
8409 // }
8410 // return_address:
Roland Levillainc9285912015-12-18 10:38:42 +00008411
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008412 CheckLastTempIsBakerCcEntrypointRegister(instruction);
Vladimir Marko88abba22017-05-03 17:09:25 +01008413 bool narrow = CanEmitNarrowLdr(root_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008414 uint32_t custom_data =
Vladimir Marko88abba22017-05-03 17:09:25 +01008415 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierGcRootData(root_reg, narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008416 Label* bne_label = codegen_->NewBakerReadBarrierPatch(custom_data);
Roland Levillainba650a42017-03-06 13:52:32 +00008417
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008418 // entrypoint_reg =
8419 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8420 DCHECK_EQ(IP, 12);
8421 const int32_t entry_point_offset =
8422 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8423 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008424
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008425 Label return_address;
8426 __ AdrCode(LR, &return_address);
8427 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008428 // Currently the offset is always within range. If that changes,
8429 // we shall have to split the load the same way as for fields.
8430 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008431 DCHECK(!down_cast<Thumb2Assembler*>(GetAssembler())->IsForced32Bit());
8432 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()), !narrow);
8433 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008434 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8435 EmitPlaceholderBne(codegen_, bne_label);
8436 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008437 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8438 narrow ? BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_NARROW_OFFSET
8439 : BAKER_MARK_INTROSPECTION_GC_ROOT_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008440 } else {
8441 // Note that we do not actually check the value of
8442 // `GetIsGcMarking()` to decide whether to mark the loaded GC
8443 // root or not. Instead, we load into `temp` the read barrier
8444 // mark entry point corresponding to register `root`. If `temp`
8445 // is null, it means that `GetIsGcMarking()` is false, and vice
8446 // versa.
8447 //
8448 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8449 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
8450 // if (temp != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8451 // // Slow path.
8452 // root = temp(root); // root = ReadBarrier::Mark(root); // Runtime entry point call.
8453 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008454
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008455 // Slow path marking the GC root `root`. The entrypoint will already be loaded in `temp`.
8456 Location temp = Location::RegisterLocation(LR);
8457 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathARM(
8458 instruction, root, /* entrypoint */ temp);
8459 codegen_->AddSlowPath(slow_path);
8460
8461 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8462 const int32_t entry_point_offset =
8463 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(root.reg());
8464 // Loading the entrypoint does not require a load acquire since it is only changed when
8465 // threads are suspended or running a checkpoint.
8466 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
8467
8468 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8469 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8470 static_assert(
8471 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
8472 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
8473 "have different sizes.");
8474 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
8475 "art::mirror::CompressedReference<mirror::Object> and int32_t "
8476 "have different sizes.");
8477
8478 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8479 // checking GetIsGcMarking.
8480 __ CompareAndBranchIfNonZero(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
8481 __ Bind(slow_path->GetExitLabel());
8482 }
Roland Levillainc9285912015-12-18 10:38:42 +00008483 } else {
8484 // GC root loaded through a slow path for read barriers other
8485 // than Baker's.
8486 // /* GcRoot<mirror::Object>* */ root = obj + offset
8487 __ AddConstant(root_reg, obj, offset);
8488 // /* mirror::Object* */ root = root->Read()
8489 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
8490 }
8491 } else {
8492 // Plain GC root load with no read barrier.
8493 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
8494 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
8495 // Note that GC roots are not affected by heap poisoning, thus we
8496 // do not have to unpoison `root_reg` here.
8497 }
8498}
8499
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008500void CodeGeneratorARM::MaybeAddBakerCcEntrypointTempForFields(LocationSummary* locations) {
8501 DCHECK(kEmitCompilerReadBarrier);
8502 DCHECK(kUseBakerReadBarrier);
8503 if (kBakerReadBarrierLinkTimeThunksEnableForFields) {
8504 if (!Runtime::Current()->UseJitCompilation()) {
8505 locations->AddTemp(Location::RegisterLocation(kBakerCcEntrypointRegister));
8506 }
8507 }
8508}
8509
Roland Levillainc9285912015-12-18 10:38:42 +00008510void CodeGeneratorARM::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
8511 Location ref,
8512 Register obj,
8513 uint32_t offset,
8514 Location temp,
8515 bool needs_null_check) {
8516 DCHECK(kEmitCompilerReadBarrier);
8517 DCHECK(kUseBakerReadBarrier);
8518
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008519 if (kBakerReadBarrierLinkTimeThunksEnableForFields &&
8520 !Runtime::Current()->UseJitCompilation()) {
8521 // Note that we do not actually check the value of `GetIsGcMarking()`
8522 // to decide whether to mark the loaded reference or not. Instead, we
8523 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8524 // barrier mark introspection entrypoint. If `temp` is null, it means
8525 // that `GetIsGcMarking()` is false, and vice versa.
8526 //
8527 // We use link-time generated thunks for the slow path. That thunk checks
8528 // the holder and jumps to the entrypoint if needed. If the holder is not
8529 // gray, it creates a fake dependency and returns to the LDR instruction.
8530 //
8531 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8532 // lr = &gray_return_address;
8533 // if (temp != nullptr) {
8534 // goto field_thunk<holder_reg, base_reg>(lr)
8535 // }
8536 // not_gray_return_address:
8537 // // Original reference load. If the offset is too large to fit
8538 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008539 // HeapReference<mirror::Object> reference = *(obj+offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008540 // gray_return_address:
8541
8542 DCHECK_ALIGNED(offset, sizeof(mirror::HeapReference<mirror::Object>));
Vladimir Marko88abba22017-05-03 17:09:25 +01008543 Register ref_reg = ref.AsRegister<Register>();
8544 bool narrow = CanEmitNarrowLdr(ref_reg, obj, offset);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008545 Register base = obj;
8546 if (offset >= kReferenceLoadMinFarOffset) {
8547 base = temp.AsRegister<Register>();
8548 DCHECK_NE(base, kBakerCcEntrypointRegister);
8549 static_assert(IsPowerOfTwo(kReferenceLoadMinFarOffset), "Expecting a power of 2.");
8550 __ AddConstant(base, obj, offset & ~(kReferenceLoadMinFarOffset - 1u));
8551 offset &= (kReferenceLoadMinFarOffset - 1u);
Vladimir Marko88abba22017-05-03 17:09:25 +01008552 // Use narrow LDR only for small offsets. Generating narrow encoding LDR for the large
8553 // offsets with `(offset & (kReferenceLoadMinFarOffset - 1u)) < 32u` would most likely
8554 // increase the overall code size when taking the generated thunks into account.
8555 DCHECK(!narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008556 }
8557 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8558 uint32_t custom_data =
Vladimir Marko88abba22017-05-03 17:09:25 +01008559 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierFieldData(base, obj, narrow);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008560 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8561
8562 // entrypoint_reg =
8563 // Thread::Current()->pReadBarrierMarkReg12, i.e. pReadBarrierMarkIntrospection.
8564 DCHECK_EQ(IP, 12);
8565 const int32_t entry_point_offset =
8566 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8567 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8568
8569 Label return_address;
8570 __ AdrCode(LR, &return_address);
8571 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008572 EmitPlaceholderBne(this, bne_label);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008573 DCHECK_LT(offset, kReferenceLoadMinFarOffset);
Vladimir Marko88abba22017-05-03 17:09:25 +01008574 DCHECK(!down_cast<Thumb2Assembler*>(GetAssembler())->IsForced32Bit());
8575 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()), !narrow);
8576 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008577 __ LoadFromOffset(kLoadWord, ref_reg, base, offset);
8578 if (needs_null_check) {
8579 MaybeRecordImplicitNullCheck(instruction);
8580 }
8581 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8582 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008583 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8584 narrow ? BAKER_MARK_INTROSPECTION_FIELD_LDR_NARROW_OFFSET
8585 : BAKER_MARK_INTROSPECTION_FIELD_LDR_WIDE_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008586 return;
8587 }
8588
Roland Levillainc9285912015-12-18 10:38:42 +00008589 // /* HeapReference<Object> */ ref = *(obj + offset)
8590 Location no_index = Location::NoLocation();
Roland Levillainbfea3352016-06-23 13:48:47 +01008591 ScaleFactor no_scale_factor = TIMES_1;
Roland Levillainc9285912015-12-18 10:38:42 +00008592 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008593 instruction, ref, obj, offset, no_index, no_scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008594}
8595
8596void CodeGeneratorARM::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
8597 Location ref,
8598 Register obj,
8599 uint32_t data_offset,
8600 Location index,
8601 Location temp,
8602 bool needs_null_check) {
8603 DCHECK(kEmitCompilerReadBarrier);
8604 DCHECK(kUseBakerReadBarrier);
8605
Roland Levillainbfea3352016-06-23 13:48:47 +01008606 static_assert(
8607 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
8608 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008609 ScaleFactor scale_factor = TIMES_4;
8610
8611 if (kBakerReadBarrierLinkTimeThunksEnableForArrays &&
8612 !Runtime::Current()->UseJitCompilation()) {
8613 // Note that we do not actually check the value of `GetIsGcMarking()`
8614 // to decide whether to mark the loaded reference or not. Instead, we
8615 // load into `temp` (actually kBakerCcEntrypointRegister) the read
8616 // barrier mark introspection entrypoint. If `temp` is null, it means
8617 // that `GetIsGcMarking()` is false, and vice versa.
8618 //
8619 // We use link-time generated thunks for the slow path. That thunk checks
8620 // the holder and jumps to the entrypoint if needed. If the holder is not
8621 // gray, it creates a fake dependency and returns to the LDR instruction.
8622 //
8623 // temp = Thread::Current()->pReadBarrierMarkIntrospection
8624 // lr = &gray_return_address;
8625 // if (temp != nullptr) {
8626 // goto field_thunk<holder_reg, base_reg>(lr)
8627 // }
8628 // not_gray_return_address:
8629 // // Original reference load. If the offset is too large to fit
8630 // // into LDR, we use an adjusted base register here.
Vladimir Marko88abba22017-05-03 17:09:25 +01008631 // HeapReference<mirror::Object> reference = data[index];
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008632 // gray_return_address:
8633
8634 DCHECK(index.IsValid());
8635 Register index_reg = index.AsRegister<Register>();
8636 Register ref_reg = ref.AsRegister<Register>();
8637 Register data_reg = temp.AsRegister<Register>();
8638 DCHECK_NE(data_reg, kBakerCcEntrypointRegister);
8639
8640 CheckLastTempIsBakerCcEntrypointRegister(instruction);
8641 uint32_t custom_data =
8642 linker::Thumb2RelativePatcher::EncodeBakerReadBarrierArrayData(data_reg);
8643 Label* bne_label = NewBakerReadBarrierPatch(custom_data);
8644
8645 // entrypoint_reg =
8646 // Thread::Current()->pReadBarrierMarkReg16, i.e. pReadBarrierMarkIntrospection.
8647 DCHECK_EQ(IP, 12);
8648 const int32_t entry_point_offset =
8649 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(IP);
8650 __ LoadFromOffset(kLoadWord, kBakerCcEntrypointRegister, TR, entry_point_offset);
8651 __ AddConstant(data_reg, obj, data_offset);
8652
8653 Label return_address;
8654 __ AdrCode(LR, &return_address);
8655 __ CmpConstant(kBakerCcEntrypointRegister, 0);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008656 EmitPlaceholderBne(this, bne_label);
Vladimir Marko88abba22017-05-03 17:09:25 +01008657 ScopedForce32Bit maybe_force_32bit(down_cast<Thumb2Assembler*>(GetAssembler()));
8658 int old_position = GetAssembler()->GetBuffer()->GetPosition();
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008659 __ ldr(ref_reg, Address(data_reg, index_reg, LSL, scale_factor));
8660 DCHECK(!needs_null_check); // The thunk cannot handle the null check.
8661 GetAssembler()->MaybeUnpoisonHeapReference(ref_reg);
8662 __ Bind(&return_address);
Vladimir Marko88abba22017-05-03 17:09:25 +01008663 DCHECK_EQ(old_position - GetAssembler()->GetBuffer()->GetPosition(),
8664 BAKER_MARK_INTROSPECTION_ARRAY_LDR_OFFSET);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01008665 return;
8666 }
8667
Roland Levillainc9285912015-12-18 10:38:42 +00008668 // /* HeapReference<Object> */ ref =
8669 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
8670 GenerateReferenceLoadWithBakerReadBarrier(
Roland Levillainbfea3352016-06-23 13:48:47 +01008671 instruction, ref, obj, data_offset, index, scale_factor, temp, needs_null_check);
Roland Levillainc9285912015-12-18 10:38:42 +00008672}
8673
8674void CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
8675 Location ref,
8676 Register obj,
8677 uint32_t offset,
8678 Location index,
Roland Levillainbfea3352016-06-23 13:48:47 +01008679 ScaleFactor scale_factor,
Roland Levillainc9285912015-12-18 10:38:42 +00008680 Location temp,
Roland Levillainff487002017-03-07 16:50:01 +00008681 bool needs_null_check) {
Roland Levillainc9285912015-12-18 10:38:42 +00008682 DCHECK(kEmitCompilerReadBarrier);
8683 DCHECK(kUseBakerReadBarrier);
8684
Roland Levillain54f869e2017-03-06 13:54:11 +00008685 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8686 // whether we need to enter the slow path to mark the reference.
8687 // Then, in the slow path, check the gray bit in the lock word of
8688 // the reference's holder (`obj`) to decide whether to mark `ref` or
8689 // not.
Roland Levillainc9285912015-12-18 10:38:42 +00008690 //
Roland Levillainba650a42017-03-06 13:52:32 +00008691 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainff487002017-03-07 16:50:01 +00008692 // instead, we load into `temp2` the read barrier mark entry point
8693 // corresponding to register `ref`. If `temp2` is null, it means
8694 // that `GetIsGcMarking()` is false, and vice versa.
8695 //
8696 // temp2 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
8697 // if (temp2 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8698 // // Slow path.
8699 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8700 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8701 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8702 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8703 // if (is_gray) {
8704 // ref = temp2(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
8705 // }
8706 // } else {
8707 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8708 // }
8709
8710 Register temp_reg = temp.AsRegister<Register>();
8711
8712 // Slow path marking the object `ref` when the GC is marking. The
8713 // entrypoint will already be loaded in `temp2`.
8714 Location temp2 = Location::RegisterLocation(LR);
8715 SlowPathCodeARM* slow_path =
8716 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierSlowPathARM(
8717 instruction,
8718 ref,
8719 obj,
8720 offset,
8721 index,
8722 scale_factor,
8723 needs_null_check,
8724 temp_reg,
8725 /* entrypoint */ temp2);
8726 AddSlowPath(slow_path);
8727
8728 // temp2 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8729 const int32_t entry_point_offset =
8730 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8731 // Loading the entrypoint does not require a load acquire since it is only changed when
8732 // threads are suspended or running a checkpoint.
8733 __ LoadFromOffset(kLoadWord, temp2.AsRegister<Register>(), TR, entry_point_offset);
8734 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8735 // checking GetIsGcMarking.
8736 __ CompareAndBranchIfNonZero(temp2.AsRegister<Register>(), slow_path->GetEntryLabel());
8737 // Fast path: the GC is not marking: just load the reference.
8738 GenerateRawReferenceLoad(instruction, ref, obj, offset, index, scale_factor, needs_null_check);
8739 __ Bind(slow_path->GetExitLabel());
8740}
8741
8742void CodeGeneratorARM::UpdateReferenceFieldWithBakerReadBarrier(HInstruction* instruction,
8743 Location ref,
8744 Register obj,
8745 Location field_offset,
8746 Location temp,
8747 bool needs_null_check,
8748 Register temp2) {
8749 DCHECK(kEmitCompilerReadBarrier);
8750 DCHECK(kUseBakerReadBarrier);
8751
8752 // Query `art::Thread::Current()->GetIsGcMarking()` to decide
8753 // whether we need to enter the slow path to update the reference
8754 // field within `obj`. Then, in the slow path, check the gray bit
8755 // in the lock word of the reference's holder (`obj`) to decide
8756 // whether to mark `ref` and update the field or not.
8757 //
8758 // Note that we do not actually check the value of `GetIsGcMarking()`;
Roland Levillainba650a42017-03-06 13:52:32 +00008759 // instead, we load into `temp3` the read barrier mark entry point
8760 // corresponding to register `ref`. If `temp3` is null, it means
8761 // that `GetIsGcMarking()` is false, and vice versa.
8762 //
8763 // temp3 = Thread::Current()->pReadBarrierMarkReg ## root.reg()
Roland Levillainba650a42017-03-06 13:52:32 +00008764 // if (temp3 != nullptr) { // <=> Thread::Current()->GetIsGcMarking()
8765 // // Slow path.
Roland Levillain54f869e2017-03-06 13:54:11 +00008766 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
8767 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
8768 // HeapReference<mirror::Object> ref = *src; // Original reference load.
8769 // bool is_gray = (rb_state == ReadBarrier::GrayState());
8770 // if (is_gray) {
Roland Levillainff487002017-03-07 16:50:01 +00008771 // old_ref = ref;
Roland Levillain54f869e2017-03-06 13:54:11 +00008772 // ref = temp3(ref); // ref = ReadBarrier::Mark(ref); // Runtime entry point call.
Roland Levillainff487002017-03-07 16:50:01 +00008773 // compareAndSwapObject(obj, field_offset, old_ref, ref);
Roland Levillain54f869e2017-03-06 13:54:11 +00008774 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008775 // }
Roland Levillainc9285912015-12-18 10:38:42 +00008776
Roland Levillain35345a52017-02-27 14:32:08 +00008777 Register temp_reg = temp.AsRegister<Register>();
Roland Levillain1372c9f2017-01-13 11:47:39 +00008778
Roland Levillainff487002017-03-07 16:50:01 +00008779 // Slow path updating the object reference at address `obj +
8780 // field_offset` when the GC is marking. The entrypoint will already
8781 // be loaded in `temp3`.
Roland Levillainba650a42017-03-06 13:52:32 +00008782 Location temp3 = Location::RegisterLocation(LR);
Roland Levillainff487002017-03-07 16:50:01 +00008783 SlowPathCodeARM* slow_path =
8784 new (GetGraph()->GetArena()) LoadReferenceWithBakerReadBarrierAndUpdateFieldSlowPathARM(
8785 instruction,
8786 ref,
8787 obj,
8788 /* offset */ 0u,
8789 /* index */ field_offset,
8790 /* scale_factor */ ScaleFactor::TIMES_1,
8791 needs_null_check,
8792 temp_reg,
8793 temp2,
8794 /* entrypoint */ temp3);
Roland Levillainba650a42017-03-06 13:52:32 +00008795 AddSlowPath(slow_path);
Roland Levillain35345a52017-02-27 14:32:08 +00008796
Roland Levillainba650a42017-03-06 13:52:32 +00008797 // temp3 = Thread::Current()->pReadBarrierMarkReg ## ref.reg()
8798 const int32_t entry_point_offset =
8799 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kArmPointerSize>(ref.reg());
8800 // Loading the entrypoint does not require a load acquire since it is only changed when
8801 // threads are suspended or running a checkpoint.
8802 __ LoadFromOffset(kLoadWord, temp3.AsRegister<Register>(), TR, entry_point_offset);
Roland Levillainba650a42017-03-06 13:52:32 +00008803 // The entrypoint is null when the GC is not marking, this prevents one load compared to
8804 // checking GetIsGcMarking.
8805 __ CompareAndBranchIfNonZero(temp3.AsRegister<Register>(), slow_path->GetEntryLabel());
Roland Levillainff487002017-03-07 16:50:01 +00008806 // Fast path: the GC is not marking: nothing to do (the field is
8807 // up-to-date, and we don't need to load the reference).
Roland Levillainba650a42017-03-06 13:52:32 +00008808 __ Bind(slow_path->GetExitLabel());
8809}
Roland Levillain35345a52017-02-27 14:32:08 +00008810
Roland Levillainba650a42017-03-06 13:52:32 +00008811void CodeGeneratorARM::GenerateRawReferenceLoad(HInstruction* instruction,
8812 Location ref,
8813 Register obj,
8814 uint32_t offset,
8815 Location index,
8816 ScaleFactor scale_factor,
8817 bool needs_null_check) {
8818 Register ref_reg = ref.AsRegister<Register>();
8819
Roland Levillainc9285912015-12-18 10:38:42 +00008820 if (index.IsValid()) {
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008821 // Load types involving an "index": ArrayGet,
8822 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8823 // intrinsics.
Roland Levillainba650a42017-03-06 13:52:32 +00008824 // /* HeapReference<mirror::Object> */ ref = *(obj + offset + (index << scale_factor))
Roland Levillainc9285912015-12-18 10:38:42 +00008825 if (index.IsConstant()) {
8826 size_t computed_offset =
Roland Levillainbfea3352016-06-23 13:48:47 +01008827 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
Roland Levillainc9285912015-12-18 10:38:42 +00008828 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
8829 } else {
Roland Levillainbfea3352016-06-23 13:48:47 +01008830 // Handle the special case of the
Roland Levillaina1aa3b12016-10-26 13:03:38 +01008831 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
8832 // intrinsics, which use a register pair as index ("long
8833 // offset"), of which only the low part contains data.
Roland Levillainbfea3352016-06-23 13:48:47 +01008834 Register index_reg = index.IsRegisterPair()
8835 ? index.AsRegisterPairLow<Register>()
8836 : index.AsRegister<Register>();
8837 __ add(IP, obj, ShifterOperand(index_reg, LSL, scale_factor));
Roland Levillainc9285912015-12-18 10:38:42 +00008838 __ LoadFromOffset(kLoadWord, ref_reg, IP, offset);
8839 }
8840 } else {
Roland Levillainba650a42017-03-06 13:52:32 +00008841 // /* HeapReference<mirror::Object> */ ref = *(obj + offset)
Roland Levillainc9285912015-12-18 10:38:42 +00008842 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
8843 }
8844
Roland Levillainba650a42017-03-06 13:52:32 +00008845 if (needs_null_check) {
8846 MaybeRecordImplicitNullCheck(instruction);
8847 }
8848
Roland Levillainc9285912015-12-18 10:38:42 +00008849 // Object* ref = ref_addr->AsMirrorPtr()
8850 __ MaybeUnpoisonHeapReference(ref_reg);
Roland Levillainc9285912015-12-18 10:38:42 +00008851}
8852
8853void CodeGeneratorARM::GenerateReadBarrierSlow(HInstruction* instruction,
8854 Location out,
8855 Location ref,
8856 Location obj,
8857 uint32_t offset,
8858 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008859 DCHECK(kEmitCompilerReadBarrier);
8860
Roland Levillainc9285912015-12-18 10:38:42 +00008861 // Insert a slow path based read barrier *after* the reference load.
8862 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008863 // If heap poisoning is enabled, the unpoisoning of the loaded
8864 // reference will be carried out by the runtime within the slow
8865 // path.
8866 //
8867 // Note that `ref` currently does not get unpoisoned (when heap
8868 // poisoning is enabled), which is alright as the `ref` argument is
8869 // not used by the artReadBarrierSlow entry point.
8870 //
8871 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008872 SlowPathCodeARM* slow_path = new (GetGraph()->GetArena())
Roland Levillain3b359c72015-11-17 19:35:12 +00008873 ReadBarrierForHeapReferenceSlowPathARM(instruction, out, ref, obj, offset, index);
8874 AddSlowPath(slow_path);
8875
Roland Levillain3b359c72015-11-17 19:35:12 +00008876 __ b(slow_path->GetEntryLabel());
8877 __ Bind(slow_path->GetExitLabel());
8878}
8879
Roland Levillainc9285912015-12-18 10:38:42 +00008880void CodeGeneratorARM::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
8881 Location out,
8882 Location ref,
8883 Location obj,
8884 uint32_t offset,
8885 Location index) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008886 if (kEmitCompilerReadBarrier) {
Roland Levillainc9285912015-12-18 10:38:42 +00008887 // Baker's read barriers shall be handled by the fast path
8888 // (CodeGeneratorARM::GenerateReferenceLoadWithBakerReadBarrier).
8889 DCHECK(!kUseBakerReadBarrier);
Roland Levillain3b359c72015-11-17 19:35:12 +00008890 // If heap poisoning is enabled, unpoisoning will be taken care of
8891 // by the runtime within the slow path.
Roland Levillainc9285912015-12-18 10:38:42 +00008892 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain3b359c72015-11-17 19:35:12 +00008893 } else if (kPoisonHeapReferences) {
8894 __ UnpoisonHeapReference(out.AsRegister<Register>());
8895 }
8896}
8897
Roland Levillainc9285912015-12-18 10:38:42 +00008898void CodeGeneratorARM::GenerateReadBarrierForRootSlow(HInstruction* instruction,
8899 Location out,
8900 Location root) {
Roland Levillain3b359c72015-11-17 19:35:12 +00008901 DCHECK(kEmitCompilerReadBarrier);
8902
Roland Levillainc9285912015-12-18 10:38:42 +00008903 // Insert a slow path based read barrier *after* the GC root load.
8904 //
Roland Levillain3b359c72015-11-17 19:35:12 +00008905 // Note that GC roots are not affected by heap poisoning, so we do
8906 // not need to do anything special for this here.
Artem Serovf4d6aee2016-07-11 10:41:45 +01008907 SlowPathCodeARM* slow_path =
Roland Levillain3b359c72015-11-17 19:35:12 +00008908 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathARM(instruction, out, root);
8909 AddSlowPath(slow_path);
8910
Roland Levillain3b359c72015-11-17 19:35:12 +00008911 __ b(slow_path->GetEntryLabel());
8912 __ Bind(slow_path->GetExitLabel());
8913}
8914
Vladimir Markodc151b22015-10-15 18:02:30 +01008915HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch(
8916 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +00008917 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffraye807ff72017-01-23 09:03:12 +00008918 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01008919}
8920
Vladimir Markob4536b72015-11-24 13:45:23 +00008921Register CodeGeneratorARM::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
8922 Register temp) {
8923 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
8924 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
8925 if (!invoke->GetLocations()->Intrinsified()) {
8926 return location.AsRegister<Register>();
8927 }
8928 // For intrinsics we allow any location, so it may be on the stack.
8929 if (!location.IsRegister()) {
8930 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
8931 return temp;
8932 }
8933 // For register locations, check if the register was saved. If so, get it from the stack.
8934 // Note: There is a chance that the register was saved but not overwritten, so we could
8935 // save one load. However, since this is just an intrinsic slow path we prefer this
8936 // simple and more robust approach rather that trying to determine if that's the case.
8937 SlowPathCode* slow_path = GetCurrentSlowPath();
Vladimir Markod254f5c2017-06-02 15:18:36 +00008938 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
8939 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
Vladimir Markob4536b72015-11-24 13:45:23 +00008940 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
8941 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
8942 return temp;
8943 }
8944 return location.AsRegister<Register>();
8945}
8946
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008947void CodeGeneratorARM::GenerateStaticOrDirectCall(
8948 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Vladimir Marko58155012015-08-19 12:49:41 +00008949 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
8950 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008951 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
8952 uint32_t offset =
8953 GetThreadOffset<kArmPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Vladimir Marko58155012015-08-19 12:49:41 +00008954 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008955 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, offset);
Vladimir Marko58155012015-08-19 12:49:41 +00008956 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01008957 }
Vladimir Marko58155012015-08-19 12:49:41 +00008958 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00008959 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00008960 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008961 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
8962 DCHECK(GetCompilerOptions().IsBootImage());
8963 Register temp_reg = temp.AsRegister<Register>();
8964 PcRelativePatchInfo* labels = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
8965 __ BindTrackedLabel(&labels->movw_label);
8966 __ movw(temp_reg, /* placeholder */ 0u);
8967 __ BindTrackedLabel(&labels->movt_label);
8968 __ movt(temp_reg, /* placeholder */ 0u);
8969 __ BindTrackedLabel(&labels->add_pc_label);
8970 __ add(temp_reg, temp_reg, ShifterOperand(PC));
8971 break;
8972 }
Vladimir Marko58155012015-08-19 12:49:41 +00008973 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
8974 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress());
8975 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008976 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
8977 Register temp_reg = temp.AsRegister<Register>();
8978 PcRelativePatchInfo* labels = NewMethodBssEntryPatch(
8979 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
8980 __ BindTrackedLabel(&labels->movw_label);
8981 __ movw(temp_reg, /* placeholder */ 0u);
8982 __ BindTrackedLabel(&labels->movt_label);
8983 __ movt(temp_reg, /* placeholder */ 0u);
8984 __ BindTrackedLabel(&labels->add_pc_label);
8985 __ add(temp_reg, temp_reg, ShifterOperand(PC));
8986 __ LoadFromOffset(kLoadWord, temp_reg, temp_reg, /* offset */ 0);
Vladimir Markob4536b72015-11-24 13:45:23 +00008987 break;
8988 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008989 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8990 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8991 return; // No code pointer retrieval; the runtime performs the call directly.
Nicolas Geoffrayae71a052015-06-09 14:12:28 +01008992 }
Vladimir Marko58155012015-08-19 12:49:41 +00008993 }
8994
8995 switch (invoke->GetCodePtrLocation()) {
8996 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
8997 __ bl(GetFrameEntryLabel());
8998 break;
Vladimir Marko58155012015-08-19 12:49:41 +00008999 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
9000 // LR = callee_method->entry_point_from_quick_compiled_code_
9001 __ LoadFromOffset(
9002 kLoadWord, LR, callee_method.AsRegister<Register>(),
Andreas Gampe542451c2016-07-26 09:02:02 -07009003 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmPointerSize).Int32Value());
Vladimir Marko58155012015-08-19 12:49:41 +00009004 // LR()
9005 __ blx(LR);
9006 break;
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08009007 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009008 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08009009
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -08009010 DCHECK(!IsLeafMethod());
9011}
9012
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009013void CodeGeneratorARM::GenerateVirtualCall(
9014 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009015 Register temp = temp_location.AsRegister<Register>();
9016 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
9017 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00009018
9019 // Use the calling convention instead of the location of the receiver, as
9020 // intrinsics may have put the receiver in a different register. In the intrinsics
9021 // slow path, the arguments have been moved to the right place, so here we are
9022 // guaranteed that the receiver is the first register of the calling convention.
9023 InvokeDexCallingConvention calling_convention;
9024 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009025 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain3b359c72015-11-17 19:35:12 +00009026 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00009027 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009028 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain3b359c72015-11-17 19:35:12 +00009029 // Instead of simply (possibly) unpoisoning `temp` here, we should
9030 // emit a read barrier for the previous class reference load.
9031 // However this is not required in practice, as this is an
9032 // intermediate/temporary reference and because the current
9033 // concurrent copying collector keeps the from-space memory
9034 // intact/accessible until the end of the marking phase (the
9035 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009036 __ MaybeUnpoisonHeapReference(temp);
9037 // temp = temp->GetMethodAt(method_offset);
9038 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07009039 kArmPointerSize).Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009040 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
9041 // LR = temp->GetEntryPoint();
9042 __ LoadFromOffset(kLoadWord, LR, temp, entry_point);
9043 // LR();
9044 __ blx(LR);
Vladimir Markoe7197bf2017-06-02 17:00:23 +01009045 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00009046}
9047
Vladimir Marko65979462017-05-19 17:25:12 +01009048CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeMethodPatch(
9049 MethodReference target_method) {
9050 return NewPcRelativePatch(*target_method.dex_file,
9051 target_method.dex_method_index,
9052 &pc_relative_method_patches_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009053}
9054
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009055CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewMethodBssEntryPatch(
9056 MethodReference target_method) {
9057 return NewPcRelativePatch(*target_method.dex_file,
9058 target_method.dex_method_index,
9059 &method_bss_entry_patches_);
9060}
9061
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01009062CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08009063 const DexFile& dex_file, dex::TypeIndex type_index) {
9064 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01009065}
9066
Vladimir Marko1998cd02017-01-13 13:02:58 +00009067CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewTypeBssEntryPatch(
9068 const DexFile& dex_file, dex::TypeIndex type_index) {
9069 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
9070}
9071
Vladimir Marko65979462017-05-19 17:25:12 +01009072CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativeStringPatch(
9073 const DexFile& dex_file, dex::StringIndex string_index) {
9074 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
9075}
9076
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009077CodeGeneratorARM::PcRelativePatchInfo* CodeGeneratorARM::NewPcRelativePatch(
9078 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
9079 patches->emplace_back(dex_file, offset_or_index);
9080 return &patches->back();
9081}
9082
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009083Label* CodeGeneratorARM::NewBakerReadBarrierPatch(uint32_t custom_data) {
9084 baker_read_barrier_patches_.emplace_back(custom_data);
9085 return &baker_read_barrier_patches_.back().label;
9086}
9087
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009088Literal* CodeGeneratorARM::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00009089 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009090}
9091
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009092Literal* CodeGeneratorARM::DeduplicateJitStringLiteral(const DexFile& dex_file,
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00009093 dex::StringIndex string_index,
9094 Handle<mirror::String> handle) {
9095 jit_string_roots_.Overwrite(StringReference(&dex_file, string_index),
9096 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009097 return jit_string_patches_.GetOrCreate(
9098 StringReference(&dex_file, string_index),
9099 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
9100}
9101
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009102Literal* CodeGeneratorARM::DeduplicateJitClassLiteral(const DexFile& dex_file,
9103 dex::TypeIndex type_index,
Nicolas Geoffray5247c082017-01-13 14:17:29 +00009104 Handle<mirror::Class> handle) {
9105 jit_class_roots_.Overwrite(TypeReference(&dex_file, type_index),
9106 reinterpret_cast64<uint64_t>(handle.GetReference()));
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009107 return jit_class_patches_.GetOrCreate(
9108 TypeReference(&dex_file, type_index),
9109 [this]() { return __ NewLiteral<uint32_t>(/* placeholder */ 0u); });
9110}
9111
Vladimir Markoaad75c62016-10-03 08:46:48 +00009112template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
9113inline void CodeGeneratorARM::EmitPcRelativeLinkerPatches(
9114 const ArenaDeque<PcRelativePatchInfo>& infos,
9115 ArenaVector<LinkerPatch>* linker_patches) {
9116 for (const PcRelativePatchInfo& info : infos) {
9117 const DexFile& dex_file = info.target_dex_file;
9118 size_t offset_or_index = info.offset_or_index;
9119 DCHECK(info.add_pc_label.IsBound());
9120 uint32_t add_pc_offset = dchecked_integral_cast<uint32_t>(info.add_pc_label.Position());
9121 // Add MOVW patch.
9122 DCHECK(info.movw_label.IsBound());
9123 uint32_t movw_offset = dchecked_integral_cast<uint32_t>(info.movw_label.Position());
9124 linker_patches->push_back(Factory(movw_offset, &dex_file, add_pc_offset, offset_or_index));
9125 // Add MOVT patch.
9126 DCHECK(info.movt_label.IsBound());
9127 uint32_t movt_offset = dchecked_integral_cast<uint32_t>(info.movt_label.Position());
9128 linker_patches->push_back(Factory(movt_offset, &dex_file, add_pc_offset, offset_or_index));
9129 }
9130}
9131
Vladimir Marko58155012015-08-19 12:49:41 +00009132void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
9133 DCHECK(linker_patches->empty());
Vladimir Markob4536b72015-11-24 13:45:23 +00009134 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01009135 /* MOVW+MOVT for each entry */ 2u * pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009136 /* MOVW+MOVT for each entry */ 2u * method_bss_entry_patches_.size() +
Vladimir Markoaad75c62016-10-03 08:46:48 +00009137 /* MOVW+MOVT for each entry */ 2u * pc_relative_type_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009138 /* MOVW+MOVT for each entry */ 2u * type_bss_entry_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01009139 /* MOVW+MOVT for each entry */ 2u * pc_relative_string_patches_.size() +
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009140 baker_read_barrier_patches_.size();
Vladimir Marko58155012015-08-19 12:49:41 +00009141 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01009142 if (GetCompilerOptions().IsBootImage()) {
9143 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00009144 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00009145 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
9146 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00009147 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
9148 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01009149 } else {
9150 DCHECK(pc_relative_method_patches_.empty());
9151 DCHECK(pc_relative_type_patches_.empty());
9152 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
9153 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009154 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01009155 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
9156 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00009157 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
9158 linker_patches);
Vladimir Markoeee1c0e2017-04-21 17:58:41 +01009159 for (const BakerReadBarrierPatchInfo& info : baker_read_barrier_patches_) {
9160 linker_patches->push_back(LinkerPatch::BakerReadBarrierBranchPatch(info.label.Position(),
9161 info.custom_data));
9162 }
Vladimir Marko1998cd02017-01-13 13:02:58 +00009163 DCHECK_EQ(size, linker_patches->size());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00009164}
9165
9166Literal* CodeGeneratorARM::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
9167 return map->GetOrCreate(
9168 value,
9169 [this, value]() { return __ NewLiteral<uint32_t>(value); });
Vladimir Marko58155012015-08-19 12:49:41 +00009170}
9171
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03009172void LocationsBuilderARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9173 LocationSummary* locations =
9174 new (GetGraph()->GetArena()) LocationSummary(instr, LocationSummary::kNoCall);
9175 locations->SetInAt(HMultiplyAccumulate::kInputAccumulatorIndex,
9176 Location::RequiresRegister());
9177 locations->SetInAt(HMultiplyAccumulate::kInputMulLeftIndex, Location::RequiresRegister());
9178 locations->SetInAt(HMultiplyAccumulate::kInputMulRightIndex, Location::RequiresRegister());
9179 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9180}
9181
9182void InstructionCodeGeneratorARM::VisitMultiplyAccumulate(HMultiplyAccumulate* instr) {
9183 LocationSummary* locations = instr->GetLocations();
9184 Register res = locations->Out().AsRegister<Register>();
9185 Register accumulator =
9186 locations->InAt(HMultiplyAccumulate::kInputAccumulatorIndex).AsRegister<Register>();
9187 Register mul_left =
9188 locations->InAt(HMultiplyAccumulate::kInputMulLeftIndex).AsRegister<Register>();
9189 Register mul_right =
9190 locations->InAt(HMultiplyAccumulate::kInputMulRightIndex).AsRegister<Register>();
9191
9192 if (instr->GetOpKind() == HInstruction::kAdd) {
9193 __ mla(res, mul_left, mul_right, accumulator);
9194 } else {
9195 __ mls(res, mul_left, mul_right, accumulator);
9196 }
9197}
9198
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01009199void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00009200 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00009201 LOG(FATAL) << "Unreachable";
9202}
9203
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01009204void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00009205 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00009206 LOG(FATAL) << "Unreachable";
9207}
9208
Mark Mendellfe57faa2015-09-18 09:26:15 -04009209// Simple implementation of packed switch - generate cascaded compare/jumps.
9210void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9211 LocationSummary* locations =
9212 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9213 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009214 if (switch_instr->GetNumEntries() > kPackedSwitchCompareJumpThreshold &&
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009215 codegen_->GetAssembler()->IsThumb()) {
9216 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base.
9217 if (switch_instr->GetStartValue() != 0) {
9218 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias.
9219 }
9220 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04009221}
9222
9223void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9224 int32_t lower_bound = switch_instr->GetStartValue();
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009225 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendellfe57faa2015-09-18 09:26:15 -04009226 LocationSummary* locations = switch_instr->GetLocations();
9227 Register value_reg = locations->InAt(0).AsRegister<Register>();
9228 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9229
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009230 if (num_entries <= kPackedSwitchCompareJumpThreshold || !codegen_->GetAssembler()->IsThumb()) {
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009231 // Create a series of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009232 Register temp_reg = IP;
9233 // Note: It is fine for the below AddConstantSetFlags() using IP register to temporarily store
9234 // the immediate, because IP is used as the destination register. For the other
9235 // AddConstantSetFlags() and GenerateCompareWithImmediate(), the immediate values are constant,
9236 // and they can be encoded in the instruction without making use of IP register.
9237 __ AddConstantSetFlags(temp_reg, value_reg, -lower_bound);
9238
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009239 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009240 // Jump to successors[0] if value == lower_bound.
9241 __ b(codegen_->GetLabelOf(successors[0]), EQ);
9242 int32_t last_index = 0;
9243 for (; num_entries - last_index > 2; last_index += 2) {
9244 __ AddConstantSetFlags(temp_reg, temp_reg, -2);
9245 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9246 __ b(codegen_->GetLabelOf(successors[last_index + 1]), LO);
9247 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9248 __ b(codegen_->GetLabelOf(successors[last_index + 2]), EQ);
9249 }
9250 if (num_entries - last_index == 2) {
9251 // The last missing case_value.
Vladimir Markoac6ac102015-12-17 12:14:00 +00009252 __ CmpConstant(temp_reg, 1);
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009253 __ b(codegen_->GetLabelOf(successors[last_index + 1]), EQ);
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009254 }
Mark Mendellfe57faa2015-09-18 09:26:15 -04009255
Andreas Gampe7cffc3b2015-10-19 21:31:53 -07009256 // And the default for any other value.
9257 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
9258 __ b(codegen_->GetLabelOf(default_block));
9259 }
9260 } else {
9261 // Create a table lookup.
9262 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
9263
9264 // Materialize a pointer to the switch table
9265 std::vector<Label*> labels(num_entries);
9266 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
9267 for (uint32_t i = 0; i < num_entries; i++) {
9268 labels[i] = codegen_->GetLabelOf(successors[i]);
9269 }
9270 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg);
9271
9272 // Remove the bias.
9273 Register key_reg;
9274 if (lower_bound != 0) {
9275 key_reg = locations->GetTemp(1).AsRegister<Register>();
9276 __ AddConstant(key_reg, value_reg, -lower_bound);
9277 } else {
9278 key_reg = value_reg;
9279 }
9280
9281 // Check whether the value is in the table, jump to default block if not.
9282 __ CmpConstant(key_reg, num_entries - 1);
9283 __ b(codegen_->GetLabelOf(default_block), Condition::HI);
9284
9285 // Load the displacement from the table.
9286 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2));
9287
9288 // Dispatch is a direct add to the PC (for Thumb2).
9289 __ EmitJumpTableDispatch(table, temp_reg);
Mark Mendellfe57faa2015-09-18 09:26:15 -04009290 }
9291}
9292
Andreas Gampe85b62f22015-09-09 13:15:38 -07009293void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) {
9294 if (!trg.IsValid()) {
Roland Levillainc9285912015-12-18 10:38:42 +00009295 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07009296 return;
9297 }
9298
9299 DCHECK_NE(type, Primitive::kPrimVoid);
9300
9301 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type);
9302 if (return_loc.Equals(trg)) {
9303 return;
9304 }
9305
9306 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
9307 // with the last branch.
9308 if (type == Primitive::kPrimLong) {
9309 HParallelMove parallel_move(GetGraph()->GetArena());
9310 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr);
9311 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr);
9312 GetMoveResolver()->EmitNativeCode(&parallel_move);
9313 } else if (type == Primitive::kPrimDouble) {
9314 HParallelMove parallel_move(GetGraph()->GetArena());
9315 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr);
9316 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr);
9317 GetMoveResolver()->EmitNativeCode(&parallel_move);
9318 } else {
9319 // Let the parallel move resolver take care of all of this.
9320 HParallelMove parallel_move(GetGraph()->GetArena());
9321 parallel_move.AddMove(return_loc, trg, type, nullptr);
9322 GetMoveResolver()->EmitNativeCode(&parallel_move);
9323 }
9324}
9325
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009326void LocationsBuilderARM::VisitClassTableGet(HClassTableGet* instruction) {
9327 LocationSummary* locations =
9328 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9329 locations->SetInAt(0, Location::RequiresRegister());
9330 locations->SetOut(Location::RequiresRegister());
9331}
9332
9333void InstructionCodeGeneratorARM::VisitClassTableGet(HClassTableGet* instruction) {
9334 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009335 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009336 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009337 instruction->GetIndex(), kArmPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009338 __ LoadFromOffset(kLoadWord,
9339 locations->Out().AsRegister<Register>(),
9340 locations->InAt(0).AsRegister<Register>(),
9341 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009342 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009343 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009344 instruction->GetIndex(), kArmPointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009345 __ LoadFromOffset(kLoadWord,
9346 locations->Out().AsRegister<Register>(),
9347 locations->InAt(0).AsRegister<Register>(),
9348 mirror::Class::ImtPtrOffset(kArmPointerSize).Uint32Value());
9349 __ LoadFromOffset(kLoadWord,
9350 locations->Out().AsRegister<Register>(),
9351 locations->Out().AsRegister<Register>(),
9352 method_offset);
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009353 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009354}
9355
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009356static void PatchJitRootUse(uint8_t* code,
9357 const uint8_t* roots_data,
9358 Literal* literal,
9359 uint64_t index_in_table) {
9360 DCHECK(literal->GetLabel()->IsBound());
9361 uint32_t literal_offset = literal->GetLabel()->Position();
9362 uintptr_t address =
9363 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
9364 uint8_t* data = code + literal_offset;
9365 reinterpret_cast<uint32_t*>(data)[0] = dchecked_integral_cast<uint32_t>(address);
9366}
9367
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009368void CodeGeneratorARM::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
9369 for (const auto& entry : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009370 const StringReference& string_reference = entry.first;
9371 Literal* table_entry_literal = entry.second;
9372 const auto it = jit_string_roots_.find(string_reference);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009373 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009374 uint64_t index_in_table = it->second;
9375 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009376 }
9377 for (const auto& entry : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009378 const TypeReference& type_reference = entry.first;
9379 Literal* table_entry_literal = entry.second;
9380 const auto it = jit_class_roots_.find(type_reference);
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00009381 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01009382 uint64_t index_in_table = it->second;
9383 PatchJitRootUse(code, roots_data, table_entry_literal, index_in_table);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00009384 }
9385}
9386
Roland Levillain4d027112015-07-01 15:41:14 +01009387#undef __
9388#undef QUICK_ENTRY_POINT
9389
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00009390} // namespace arm
9391} // namespace art